From 9a3d61aaf63dcaebe3b4cf630a531cf9688bd383 Mon Sep 17 00:00:00 2001 From: eraden Date: Tue, 6 Jun 2023 21:39:53 +0200 Subject: [PATCH] Add gift cars (part 1) --- .../carts/m20230603_120815_cart_discounts.rs | 66 +++++ migration/src/carts/mod.rs | 2 + migration/src/lib.rs | 10 + .../src/public/m20230603_120815_claims.rs | 256 ++++++++++++++++++ migration/src/public/mod.rs | 2 + migrations/20230603073510_init.sql | 17 +- 6 files changed, 344 insertions(+), 9 deletions(-) create mode 100644 migration/src/carts/m20230603_120815_cart_discounts.rs create mode 100644 migration/src/public/m20230603_120815_claims.rs diff --git a/migration/src/carts/m20230603_120815_cart_discounts.rs b/migration/src/carts/m20230603_120815_cart_discounts.rs new file mode 100644 index 0000000..511fdfe --- /dev/null +++ b/migration/src/carts/m20230603_120815_cart_discounts.rs @@ -0,0 +1,66 @@ +use sea_orm_migration::prelude::*; +use sea_query::expr::SimpleExpr; + +/// ```sql +/// CREATE TABLE cart_discounts +/// ( +/// cart_id uuid NOT NULL, +/// discount_id uuid NOT NULL +/// ); +/// CREATE TABLE cart_gift_cards +/// ( +/// cart_id uuid NOT NULL, +/// gift_card_id uuid NOT NULL +/// ); +/// ``` +#[derive(DeriveMigrationName)] +pub struct Migration; + +#[async_trait::async_trait] +impl MigrationTrait for Migration { + async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> { + manager + .create_table( + Table::create() + .table(CartDiscount::CartDiscounts) + .col(ColumnDef::new(CartDiscount::CartId).uuid().not_null()) + .col(ColumnDef::new(CartDiscount::DiscountId).uuid().not_null()) + .to_owned(), + ) + .await?; + manager + .create_table( + Table::create() + .table(CartGiftCard::CartGiftCards) + .col(ColumnDef::new(CartGiftCard::CartId).uuid().not_null()) + .col(ColumnDef::new(CartGiftCard::GiftCardId).uuid().not_null()) + .to_owned(), + ) + .await?; + Ok(()) + } + + async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> { + manager + .drop_table(Table::drop().table(CartDiscount::CartDiscounts).to_owned()) + .await?; + manager + .drop_table(Table::drop().table(CartGiftCard::CartGiftCards).to_owned()) + .await?; + Ok(()) + } +} + +#[derive(Iden)] +enum CartDiscount { + CartDiscounts, + CartId, + DiscountId, +} + +#[derive(Iden)] +enum CartGiftCard { + CartGiftCards, + CartId, + GiftCardId, +} diff --git a/migration/src/carts/mod.rs b/migration/src/carts/mod.rs index b7b6e89..28d9b4a 100644 --- a/migration/src/carts/mod.rs +++ b/migration/src/carts/mod.rs @@ -2,6 +2,7 @@ use sea_orm_migration::{MigrationTrait, MigratorTrait}; mod m20230603_102634_types; mod m20230603_120814_carts; +mod m20230603_120815_cart_discounts; pub struct CartsMigrator; @@ -11,6 +12,7 @@ impl MigratorTrait for CartsMigrator { vec![ Box::new(m20230603_102634_types::Migration), Box::new(m20230603_120814_carts::Migration), + Box::new(m20230603_120815_cart_discounts::Migration), ] } } diff --git a/migration/src/lib.rs b/migration/src/lib.rs index 45cb14b..c7ee9fe 100644 --- a/migration/src/lib.rs +++ b/migration/src/lib.rs @@ -5,3 +5,13 @@ pub mod jobs; pub mod public; pub mod schema_list; pub mod types; + +#[macro_export] +macro_rules! ts_def_now_not_null { + ($identifier: expr) => { + ColumnDef::new($identifier) + .timestamp() + .default(SimpleExpr::Custom("now()".into())) + .not_null() + }; +} diff --git a/migration/src/public/m20230603_120815_claims.rs b/migration/src/public/m20230603_120815_claims.rs new file mode 100644 index 0000000..9190f68 --- /dev/null +++ b/migration/src/public/m20230603_120815_claims.rs @@ -0,0 +1,256 @@ +use sea_orm_migration::manager; +use sea_orm_migration::prelude::*; +use sea_orm_migration::sea_orm::Statement; +use sea_query::expr::SimpleExpr; + +use crate::sea_orm::DatabaseBackend; +use crate::ts_def_now_not_null; + +/// ```sql +/// CREATE TABLE claim_images +/// ( +/// id uuid NOT NULL, +/// claim_item_id uuid NOT NULL, +/// url character varying NOT NULL, +/// created_at timestamp with time zone DEFAULT now() NOT NULL, +/// updated_at timestamp with time zone DEFAULT now() NOT NULL, +/// deleted_at timestamp with time zone, +/// metadata jsonb +/// ); +/// +/// CREATE TABLE claim_items +/// ( +/// id uuid NOT NULL, +/// claim_order_id uuid NOT NULL, +/// item_id uuid NOT NULL, +/// variant_id uuid NOT NULL, +/// reason claim_item_reasons NOT NULL, +/// note character varying, +/// quantity integer NOT NULL, +/// created_at timestamp with time zone DEFAULT now() NOT NULL, +/// updated_at timestamp with time zone DEFAULT now() NOT NULL, +/// deleted_at timestamp with time zone, +/// metadata jsonb +/// ); +/// +/// CREATE TABLE claim_item_tags +/// ( +/// item_id uuid NOT NULL, +/// tag_id uuid NOT NULL +/// ); +/// +/// CREATE TABLE claim_orders +/// ( +/// id uuid NOT NULL, +/// payment_status public.claim_order_payment_statuses DEFAULT 'na'::public.claim_order_payment_statuses NOT NULL, +/// fulfillment_status public.claim_order_fulfillment_statuses DEFAULT 'not_fulfilled'::public.claim_order_fulfillment_statuses NOT NULL, +/// claim_order_type public.claim_order_types NOT NULL, +/// order_id uuid NOT NULL, +/// shipping_address_id character varying, +/// refund_amount integer, +/// canceled_at timestamp with time zone, +/// created_at timestamp with time zone DEFAULT now() NOT NULL, +/// updated_at timestamp with time zone DEFAULT now() NOT NULL, +/// deleted_at timestamp with time zone, +/// metadata jsonb, +/// idempotency_key character varying, +/// no_notification boolean +/// ); +/// +/// CREATE TABLE claim_tags +/// ( +/// id uuid NOT NULL, +/// value character varying NOT NULL, +/// created_at timestamp with time zone DEFAULT now() NOT NULL, +/// updated_at timestamp with time zone DEFAULT now() NOT NULL, +/// deleted_at timestamp with time zone, +/// metadata jsonb +/// ); +/// ``` +#[derive(DeriveMigrationName)] +pub struct Migration; + +#[async_trait::async_trait] +impl MigrationTrait for Migration { + async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> { + manager + .create_table( + Table::create() + .table(ClaimImage::ClaimImages) + .col( + ColumnDef::new(ClaimImage::Id) + .uuid() + .not_null() + .default(SimpleExpr::Custom("uuid_generate_v4()".into())) + .primary_key(), + ) + .col(ColumnDef::new(ClaimImage::ClaimItemId).uuid().not_null()) + .col(ColumnDef::new(ClaimImage::Url).string().not_null()) + .col(ColumnDef::new(ClaimImage::CreatedAt).string()) + .col(ColumnDef::new(ClaimImage::UpdatedAt).string()) + .col(ColumnDef::new(ClaimImage::DeletedAt).string()) + .col(ColumnDef::new(ClaimImage::Metadata).string()) + .to_owned(), + ) + .await?; + + manager + .create_table( + Table::create() + .table(ClaimItem::ClaimItems) + .col( + ColumnDef::new(ClaimItem::Id) + .uuid() + .not_null() + .default(SimpleExpr::Custom("uuid_generate_v4()".into())) + .primary_key(), + ) + .to_owned(), + ) + .await?; + + manager + .create_table( + Table::create() + .table(ClaimItemTag::ClaimItemTags) + .col(ColumnDef::new(ClaimItemTag::ItemId).uuid().not_null()) + .col(ColumnDef::new(ClaimItemTag::TagId).uuid().not_null()) + .to_owned(), + ) + .await?; + manager + .create_table( + Table::create() + .table(ClaimOrder::ClaimOrders) + .col( + ColumnDef::new(ClaimOrder::Id) + .uuid() + .not_null() + .default(SimpleExpr::Custom("uuid_generate_v4()".into())) + .primary_key(), + ) + .to_owned(), + ) + .await?; + manager + .create_table( + Table::create() + .table(ClaimTag::ClaimTags) + .col(ColumnDef::new(ClaimTag::Id).uuid()) + .to_owned(), + ) + .await?; + + //// + // manager + // .create_table( + // Table::create() + // .table(AnalyticsConfig::AnalyticsConfigs) + // .col( + // ColumnDef::new(AnalyticsConfig::Id) + // .uuid() + // .not_null() + // .default(SimpleExpr::Custom("uuid_generate_v4()".into())) + // .primary_key(), + // ) + // .col(ts_def_now_not_null!(AnalyticsConfig::CreatedAt)) + // .col(ts_def_now_not_null!(AnalyticsConfig::UpdatedAt)) + // .col(ColumnDef::new(AnalyticsConfig::DeletedAt).timestamp()) + // .col(ColumnDef::new(AnalyticsConfig::UserId).uuid().not_null()) + // .col( + // ColumnDef::new(AnalyticsConfig::OptOut) + // .boolean() + // .default(false) + // .not_null(), + // ) + // .col( + // ColumnDef::new(AnalyticsConfig::Anonymize) + // .boolean() + // .default(false) + // .not_null(), + // ) + // .to_owned(), + // ) + // .await?; + Ok(()) + } + + async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> { + manager + .drop_table(Table::drop().table(ClaimImage::ClaimImages).to_owned()) + .await?; + manager + .drop_table(Table::drop().table(ClaimItem::ClaimItems).to_owned()) + .await?; + manager + .drop_table(Table::drop().table(ClaimItemTag::ClaimItemTags).to_owned()) + .await?; + manager + .drop_table(Table::drop().table(ClaimOrder::ClaimOrders).to_owned()) + .await?; + manager + .drop_table(Table::drop().table(ClaimTag::ClaimTags).to_owned()) + .await?; + Ok(()) + } +} + +#[derive(Iden)] +pub enum ClaimImage { + ClaimImages, + Id, + ClaimItemId, + Url, + CreatedAt, + UpdatedAt, + DeletedAt, + Metadata, +} + +#[derive(Iden)] +pub enum ClaimItem { + ClaimItems, + Id, + ClaimOrderId, + ItemId, + VariantId, + Reason, + Note, + Quantity, + CreatedAt, + UpdatedAt, + DeletedAt, + Metadata, +} + +#[derive(Iden)] +pub enum ClaimItemTag { + ClaimItemTags, + ItemId, + TagId, +} + +#[derive(Iden)] +pub enum ClaimOrder { + ClaimOrders, + Id, + PaymentStatus, + FulfillmentStatus, + ClaimOrderType, + OrderId, + ShippingAddressId, + RefundAmount, + CanceledAt, + CreatedAt, +} + +#[derive(Iden)] +pub enum ClaimTag { + ClaimTags, + Id, + Value, + CreatedAt, + UpdatedAt, + DeletedAt, + Metadata, +} diff --git a/migration/src/public/mod.rs b/migration/src/public/mod.rs index 2589127..29ec79c 100644 --- a/migration/src/public/mod.rs +++ b/migration/src/public/mod.rs @@ -3,6 +3,7 @@ use sea_orm_migration::{MigrationTrait, MigratorTrait}; mod m20230603_102630_schema; mod m20230603_102634_types; mod m20230603_120814_addresses; +mod m20230603_120815_claims; pub struct PublicMigrator; @@ -14,6 +15,7 @@ impl MigratorTrait for PublicMigrator { Box::new(m20230603_102630_schema::Migration), Box::new(m20230603_102634_types::Migration), Box::new(m20230603_120814_addresses::Migration), + Box::new(m20230603_120815_claims::Migration), ] } } diff --git a/migrations/20230603073510_init.sql b/migrations/20230603073510_init.sql index 9cf50d9..856d1a2 100644 --- a/migrations/20230603073510_init.sql +++ b/migrations/20230603073510_init.sql @@ -275,15 +275,6 @@ CREATE TABLE public.carts sales_channel_id character varying ); ----- ########################################################### ----- ########################################################### ----- ########################################################### ----- ########################################################### ----- ########################################################### ----- ########################################################### ----- ########################################################### ----- ########################################################### - CREATE TABLE public.cart_discounts ( cart_id uuid NOT NULL, @@ -296,6 +287,14 @@ CREATE TABLE public.cart_gift_cards gift_card_id uuid NOT NULL ); +---- ########################################################### +---- ########################################################### +---- ########################################################### +---- ########################################################### +---- ########################################################### +---- ########################################################### +---- ########################################################### +---- ########################################################### CREATE TABLE public.claim_images (