discount profiles and discounts overall implemented and working
This commit is contained in:
@@ -37,6 +37,7 @@ mod m20260618_000004_account_ownership;
|
||||
mod m20260620_000001_add_totp_to_users;
|
||||
mod m20260621_000001_add_sale_price_to_products;
|
||||
mod m20260621_000002_account_product_prices;
|
||||
mod m20260621_000003_discount_profiles;
|
||||
pub struct Migrator;
|
||||
|
||||
#[async_trait::async_trait]
|
||||
@@ -78,6 +79,7 @@ impl MigratorTrait for Migrator {
|
||||
Box::new(m20260620_000001_add_totp_to_users::Migration),
|
||||
Box::new(m20260621_000001_add_sale_price_to_products::Migration),
|
||||
Box::new(m20260621_000002_account_product_prices::Migration),
|
||||
Box::new(m20260621_000003_discount_profiles::Migration),
|
||||
// inject-above (do not remove this comment)
|
||||
]
|
||||
}
|
||||
|
||||
91
migration/src/m20260621_000003_discount_profiles.rs
Normal file
91
migration/src/m20260621_000003_discount_profiles.rs
Normal file
@@ -0,0 +1,91 @@
|
||||
use loco_rs::schema::*;
|
||||
use sea_orm_migration::prelude::*;
|
||||
|
||||
#[derive(DeriveMigrationName)]
|
||||
pub struct Migration;
|
||||
|
||||
#[async_trait::async_trait]
|
||||
impl MigrationTrait for Migration {
|
||||
async fn up(&self, m: &SchemaManager) -> Result<(), DbErr> {
|
||||
// A reusable, named discount layer: a percentage (basis points, 5% = 500)
|
||||
// over a product scope. `scope_type` is 'include' (covers the listed
|
||||
// products) or 'all_except' (covers everything but the listed products).
|
||||
create_table(
|
||||
m,
|
||||
"discount_profiles",
|
||||
&[
|
||||
("id", ColType::PkAuto),
|
||||
("name", ColType::String),
|
||||
("percent_bp", ColType::Integer),
|
||||
("scope_type", ColType::StringWithDefault("include".to_string())),
|
||||
],
|
||||
&[],
|
||||
)
|
||||
.await?;
|
||||
|
||||
// Which products the scope lists (meaning depends on scope_type).
|
||||
create_table(
|
||||
m,
|
||||
"discount_profile_products",
|
||||
&[("id", ColType::PkAuto)],
|
||||
&[("discount_profile", ""), ("product", "")],
|
||||
)
|
||||
.await?;
|
||||
m.create_index(
|
||||
Index::create()
|
||||
.name("idx_discount_profile_products_unique")
|
||||
.table(Alias::new("discount_profile_products"))
|
||||
.col(Alias::new("discount_profile_id"))
|
||||
.col(Alias::new("product_id"))
|
||||
.unique()
|
||||
.to_owned(),
|
||||
)
|
||||
.await?;
|
||||
|
||||
// Which profiles a business account has (mixable).
|
||||
create_table(
|
||||
m,
|
||||
"account_discount_profiles",
|
||||
&[("id", ColType::PkAuto)],
|
||||
&[("user", ""), ("discount_profile", "")],
|
||||
)
|
||||
.await?;
|
||||
m.create_index(
|
||||
Index::create()
|
||||
.name("idx_account_discount_profiles_unique")
|
||||
.table(Alias::new("account_discount_profiles"))
|
||||
.col(Alias::new("user_id"))
|
||||
.col(Alias::new("discount_profile_id"))
|
||||
.unique()
|
||||
.to_owned(),
|
||||
)
|
||||
.await?;
|
||||
|
||||
// The admin's chosen winning profile when two assigned profiles cover the
|
||||
// same product for an account (collision resolution).
|
||||
create_table(
|
||||
m,
|
||||
"account_product_resolutions",
|
||||
&[("id", ColType::PkAuto)],
|
||||
&[("user", ""), ("product", ""), ("discount_profile", "")],
|
||||
)
|
||||
.await?;
|
||||
m.create_index(
|
||||
Index::create()
|
||||
.name("idx_account_product_resolutions_unique")
|
||||
.table(Alias::new("account_product_resolutions"))
|
||||
.col(Alias::new("user_id"))
|
||||
.col(Alias::new("product_id"))
|
||||
.unique()
|
||||
.to_owned(),
|
||||
)
|
||||
.await
|
||||
}
|
||||
|
||||
async fn down(&self, m: &SchemaManager) -> Result<(), DbErr> {
|
||||
drop_table(m, "account_product_resolutions").await?;
|
||||
drop_table(m, "account_discount_profiles").await?;
|
||||
drop_table(m, "discount_profile_products").await?;
|
||||
drop_table(m, "discount_profiles").await
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user