refactor: migrate promotion module (#10410)

This commit is contained in:
Harminder Virk
2024-12-11 13:12:39 +05:30
committed by GitHub
parent 777582ac43
commit fad85a9d29
22 changed files with 682 additions and 745 deletions

View File

@@ -1,126 +1,45 @@
import {
ApplicationMethodAllocationValues,
ApplicationMethodTargetTypeValues,
ApplicationMethodTypeValues,
BigNumberRawValue,
} from "@medusajs/framework/types"
import {
BigNumber,
DALUtils,
MikroOrmBigNumberProperty,
PromotionUtils,
createPsqlIndexStatementHelper,
generateEntityId,
} from "@medusajs/framework/utils"
import {
BeforeCreate,
Collection,
Entity,
Enum,
Filter,
Index,
ManyToMany,
OnInit,
OneToOne,
PrimaryKey,
Property,
Rel,
} from "@mikro-orm/core"
import { PromotionUtils, model } from "@medusajs/framework/utils"
import Promotion from "./promotion"
import PromotionRule from "./promotion-rule"
const tableName = "promotion_application_method"
const CurrencyCodeIndex = createPsqlIndexStatementHelper({
tableName,
columns: "currency_code",
where: "deleted_at IS NOT NULL",
})
const ApplicationMethod = model
.define(
{ name: "ApplicationMethod", tableName: "promotion_application_method" },
{
id: model.id({ prefix: "proappmet" }).primaryKey(),
value: model.bigNumber().nullable(),
currency_code: model.text().nullable(),
max_quantity: model.number().nullable(),
apply_to_quantity: model.number().nullable(),
buy_rules_min_quantity: model.number().nullable(),
type: model
.enum(PromotionUtils.ApplicationMethodType)
.index("IDX_application_method_type"),
target_type: model
.enum(PromotionUtils.ApplicationMethodTargetType)
.index("IDX_application_method_target_type"),
allocation: model
.enum(PromotionUtils.ApplicationMethodAllocation)
.index("IDX_application_method_allocation")
.nullable(),
promotion: model.belongsTo(() => Promotion, {
mappedBy: "application_method",
}),
target_rules: model.manyToMany(() => PromotionRule, {
pivotTable: "application_method_target_rules",
mappedBy: "method_target_rules",
}),
buy_rules: model.manyToMany(() => PromotionRule, {
pivotTable: "application_method_buy_rules",
mappedBy: "method_buy_rules",
}),
}
)
.indexes([
{
on: ["currency_code"],
where: "deleted_at IS NOT NULL",
},
])
@Entity({ tableName })
@Filter(DALUtils.mikroOrmSoftDeletableFilterOptions)
export default class ApplicationMethod {
@PrimaryKey({ columnType: "text" })
id!: string
@MikroOrmBigNumberProperty()
value: BigNumber | number | null
@Property({ columnType: "jsonb" })
raw_value: BigNumberRawValue | null
@Property({ columnType: "text", nullable: true })
@CurrencyCodeIndex.MikroORMIndex()
currency_code: string | null = null
@Property({ columnType: "numeric", nullable: true, serializer: Number })
max_quantity?: number | null = null
@Property({ columnType: "numeric", nullable: true, serializer: Number })
apply_to_quantity?: number | null = null
@Property({ columnType: "numeric", nullable: true, serializer: Number })
buy_rules_min_quantity?: number | null = null
@Index({ name: "IDX_application_method_type" })
@Enum(() => PromotionUtils.ApplicationMethodType)
type: ApplicationMethodTypeValues
@Index({ name: "IDX_application_method_target_type" })
@Enum(() => PromotionUtils.ApplicationMethodTargetType)
target_type: ApplicationMethodTargetTypeValues
@Index({ name: "IDX_application_method_allocation" })
@Enum({
items: () => PromotionUtils.ApplicationMethodAllocation,
nullable: true,
})
allocation?: ApplicationMethodAllocationValues
@OneToOne({
entity: () => Promotion,
onDelete: "cascade",
})
promotion: Rel<Promotion>
@ManyToMany(() => PromotionRule, "method_target_rules", {
owner: true,
pivotTable: "application_method_target_rules",
cascade: ["soft-remove"] as any,
})
target_rules = new Collection<Rel<PromotionRule>>(this)
@ManyToMany(() => PromotionRule, "method_buy_rules", {
owner: true,
pivotTable: "application_method_buy_rules",
cascade: ["soft-remove"] as any,
})
buy_rules = new Collection<Rel<PromotionRule>>(this)
@Property({
onCreate: () => new Date(),
columnType: "timestamptz",
defaultRaw: "now()",
})
created_at: Date
@Property({
onCreate: () => new Date(),
onUpdate: () => new Date(),
columnType: "timestamptz",
defaultRaw: "now()",
})
updated_at: Date
@Property({ columnType: "timestamptz", nullable: true })
deleted_at: Date | null = null
@BeforeCreate()
onCreate() {
this.id = generateEntityId(this.id, "proappmet")
}
@OnInit()
onInit() {
this.id = generateEntityId(this.id, "proappmet")
}
}
export default ApplicationMethod

View File

@@ -1,93 +1,20 @@
import {
BigNumberRawValue,
CampaignBudgetTypeValues,
DAL,
} from "@medusajs/framework/types"
import {
BigNumber,
DALUtils,
MikroOrmBigNumberProperty,
PromotionUtils,
generateEntityId,
} from "@medusajs/framework/utils"
import {
BeforeCreate,
Entity,
Enum,
Filter,
Index,
OnInit,
OneToOne,
OptionalProps,
PrimaryKey,
Property,
Rel,
} from "@mikro-orm/core"
import { PromotionUtils, model } from "@medusajs/framework/utils"
import Campaign from "./campaign"
type OptionalFields =
| "description"
| "limit"
| "used"
| DAL.SoftDeletableModelDateColumns
@Entity({ tableName: "promotion_campaign_budget" })
@Filter(DALUtils.mikroOrmSoftDeletableFilterOptions)
export default class CampaignBudget {
[OptionalProps]?: OptionalFields
@PrimaryKey({ columnType: "text" })
id!: string
@Index({ name: "IDX_campaign_budget_type" })
@Enum(() => PromotionUtils.CampaignBudgetType)
type: CampaignBudgetTypeValues
@OneToOne({
entity: () => Campaign,
})
campaign: Rel<Campaign> | null = null
@Property({ columnType: "text", nullable: true })
currency_code: string | null = null
@MikroOrmBigNumberProperty({ nullable: true })
limit: BigNumber | number | null = null
@Property({ columnType: "jsonb", nullable: true })
raw_limit: BigNumberRawValue | null = null
@MikroOrmBigNumberProperty({ default: 0 })
used: BigNumber | number = 0
@Property({ columnType: "jsonb" })
raw_used: BigNumberRawValue
@Property({
onCreate: () => new Date(),
columnType: "timestamptz",
defaultRaw: "now()",
})
created_at: Date
@Property({
onCreate: () => new Date(),
onUpdate: () => new Date(),
columnType: "timestamptz",
defaultRaw: "now()",
})
updated_at: Date
@Property({ columnType: "timestamptz", nullable: true })
deleted_at: Date | null = null
@BeforeCreate()
onCreate() {
this.id = generateEntityId(this.id, "probudg")
const CampaignBudget = model.define(
{ name: "CampaignBudget", tableName: "promotion_campaign_budget" },
{
id: model.id({ prefix: "probudg" }).primaryKey(),
type: model
.enum(PromotionUtils.CampaignBudgetType)
.index("IDX_campaign_budget_type"),
currency_code: model.text().nullable(),
limit: model.bigNumber().nullable(),
used: model.bigNumber().default(0),
campaign: model.belongsTo(() => Campaign, {
mappedBy: "budget",
}),
}
)
@OnInit()
onInit() {
this.id = generateEntityId(this.id, "probudg")
}
}
export default CampaignBudget

View File

@@ -1,109 +1,36 @@
import { DAL } from "@medusajs/framework/types"
import {
DALUtils,
Searchable,
createPsqlIndexStatementHelper,
generateEntityId,
} from "@medusajs/framework/utils"
import {
BeforeCreate,
Collection,
Entity,
Filter,
OnInit,
OneToMany,
OneToOne,
OptionalProps,
PrimaryKey,
Property,
Rel,
} from "@mikro-orm/core"
import { model } from "@medusajs/framework/utils"
import CampaignBudget from "./campaign-budget"
import Promotion from "./promotion"
type OptionalRelations = "budget"
type OptionalFields =
| "description"
| "starts_at"
| "ends_at"
| DAL.SoftDeletableModelDateColumns
const tableName = "promotion_campaign"
const CampaignUniqueCampaignIdentifier = createPsqlIndexStatementHelper({
tableName,
columns: ["campaign_identifier"],
unique: true,
where: "deleted_at IS NULL",
})
@Entity({ tableName })
@Filter(DALUtils.mikroOrmSoftDeletableFilterOptions)
export default class Campaign {
[OptionalProps]?: OptionalFields | OptionalRelations
@PrimaryKey({ columnType: "text" })
id!: string
@Searchable()
@Property({ columnType: "text" })
name: string
@Searchable()
@Property({ columnType: "text", nullable: true })
description: string | null = null
@Property({ columnType: "text" })
@CampaignUniqueCampaignIdentifier.MikroORMIndex()
campaign_identifier: string
@Property({
columnType: "timestamptz",
nullable: true,
const Campaign = model
.define(
{ name: "Campaign", tableName: "promotion_campaign" },
{
id: model.id({ prefix: "procamp" }).primaryKey(),
name: model.text().searchable(),
description: model.text().searchable().nullable(),
campaign_identifier: model.text(),
starts_at: model.dateTime().nullable(),
ends_at: model.dateTime().nullable(),
budget: model
.hasOne<() => typeof CampaignBudget>(() => CampaignBudget, {
mappedBy: "campaign",
})
.nullable(),
promotions: model.hasMany(() => Promotion, {
mappedBy: "campaign",
}),
}
)
.cascades({
delete: ["budget"],
})
starts_at: Date | null = null
.indexes([
{
on: ["campaign_identifier"],
unique: true,
where: "deleted_at IS NULL",
},
])
@Property({
columnType: "timestamptz",
nullable: true,
})
ends_at: Date | null = null
@OneToOne({
entity: () => CampaignBudget,
mappedBy: (cb) => cb.campaign,
cascade: ["soft-remove"] as any,
nullable: true,
})
budget: Rel<CampaignBudget> | null = null
@OneToMany(() => Promotion, (promotion) => promotion.campaign)
promotions = new Collection<Rel<Promotion>>(this)
@Property({
onCreate: () => new Date(),
columnType: "timestamptz",
defaultRaw: "now()",
})
created_at: Date
@Property({
onCreate: () => new Date(),
onUpdate: () => new Date(),
columnType: "timestamptz",
defaultRaw: "now()",
})
updated_at: Date
@Property({ columnType: "timestamptz", nullable: true })
deleted_at: Date | null = null
@BeforeCreate()
onCreate() {
this.id = generateEntityId(this.id, "procamp")
}
@OnInit()
onInit() {
this.id = generateEntityId(this.id, "procamp")
}
}
export default Campaign

View File

@@ -1,57 +1,15 @@
import { DALUtils, generateEntityId } from "@medusajs/framework/utils"
import {
BeforeCreate,
Entity,
Filter,
ManyToOne,
OnInit,
PrimaryKey,
Property,
Rel,
} from "@mikro-orm/core"
import { model } from "@medusajs/framework/utils"
import PromotionRule from "./promotion-rule"
@Entity({ tableName: "promotion_rule_value" })
@Filter(DALUtils.mikroOrmSoftDeletableFilterOptions)
export default class PromotionRuleValue {
@PrimaryKey({ columnType: "text" })
id!: string
@ManyToOne(() => PromotionRule, {
onDelete: "cascade",
fieldName: "promotion_rule_id",
index: "IDX_promotion_rule_promotion_rule_value_id",
})
promotion_rule: Rel<PromotionRule>
@Property({ columnType: "text" })
value: string
@Property({
onCreate: () => new Date(),
columnType: "timestamptz",
defaultRaw: "now()",
})
created_at: Date
@Property({
onCreate: () => new Date(),
onUpdate: () => new Date(),
columnType: "timestamptz",
defaultRaw: "now()",
})
updated_at: Date
@Property({ columnType: "timestamptz", nullable: true })
deleted_at: Date | null = null
@BeforeCreate()
onCreate() {
this.id = generateEntityId(this.id, "prorulval")
const PromotionRuleValue = model.define(
{ name: "PromotionRuleValue", tableName: "promotion_rule_value" },
{
id: model.id({ prefix: "prorulval" }).primaryKey(),
value: model.text(),
promotion_rule: model.belongsTo(() => PromotionRule, {
mappedBy: "values",
}),
}
)
@OnInit()
onInit() {
this.id = generateEntityId(this.id, "prorulval")
}
}
export default PromotionRuleValue

View File

@@ -1,96 +1,37 @@
import { DAL, PromotionRuleOperatorValues } from "@medusajs/framework/types"
import {
DALUtils,
PromotionUtils,
generateEntityId,
} from "@medusajs/framework/utils"
import {
BeforeCreate,
Cascade,
Collection,
Entity,
Enum,
Filter,
Index,
ManyToMany,
OnInit,
OneToMany,
OptionalProps,
PrimaryKey,
Property,
Rel,
} from "@mikro-orm/core"
import { PromotionUtils, model } from "@medusajs/framework/utils"
import ApplicationMethod from "./application-method"
import Promotion from "./promotion"
import PromotionRuleValue from "./promotion-rule-value"
type OptionalFields = "description" | DAL.SoftDeletableModelDateColumns
type OptionalRelations = "values" | "promotions"
@Entity({ tableName: "promotion_rule" })
@Filter(DALUtils.mikroOrmSoftDeletableFilterOptions)
export default class PromotionRule {
[OptionalProps]?: OptionalFields | OptionalRelations
@PrimaryKey({ columnType: "text" })
id!: string
@Property({ columnType: "text", nullable: true })
description: string | null = null
@Index({ name: "IDX_promotion_rule_attribute" })
@Property({ columnType: "text" })
attribute: string
@Index({ name: "IDX_promotion_rule_operator" })
@Enum(() => PromotionUtils.PromotionRuleOperator)
operator: PromotionRuleOperatorValues
@OneToMany(() => PromotionRuleValue, (prv) => prv.promotion_rule, {
cascade: [Cascade.REMOVE],
})
values = new Collection<Rel<PromotionRuleValue>>(this)
@ManyToMany(() => Promotion, (promotion) => promotion.rules)
promotions = new Collection<Rel<Promotion>>(this)
@ManyToMany(
() => ApplicationMethod,
(applicationMethod) => applicationMethod.target_rules
const PromotionRule = model
.define(
{
name: "PromotionRule",
tableName: "promotion_rule",
},
{
id: model.id({ prefix: "prorul" }).primaryKey(),
description: model.text().nullable(),
attribute: model.text().index("IDX_promotion_rule_attribute"),
operator: model
.enum(PromotionUtils.PromotionRuleOperator)
.index("IDX_promotion_rule_operator"),
values: model.hasMany(() => PromotionRuleValue, {
mappedBy: "promotion_rule",
}),
promotions: model.manyToMany(() => Promotion, {
mappedBy: "rules",
}),
method_target_rules: model.manyToMany(() => ApplicationMethod, {
mappedBy: "target_rules",
}),
method_buy_rules: model.manyToMany(() => ApplicationMethod, {
mappedBy: "buy_rules",
}),
}
)
method_target_rules = new Collection<Rel<ApplicationMethod>>(this)
@ManyToMany(
() => ApplicationMethod,
(applicationMethod) => applicationMethod.buy_rules
)
method_buy_rules = new Collection<Rel<ApplicationMethod>>(this)
@Property({
onCreate: () => new Date(),
columnType: "timestamptz",
defaultRaw: "now()",
.cascades({
delete: ["values"],
})
created_at: Date
@Property({
onCreate: () => new Date(),
onUpdate: () => new Date(),
columnType: "timestamptz",
defaultRaw: "now()",
})
updated_at: Date
@Property({ columnType: "timestamptz", nullable: true })
deleted_at: Date | null = null
@BeforeCreate()
onCreate() {
this.id = generateEntityId(this.id, "prorul")
}
@OnInit()
onInit() {
this.id = generateEntityId(this.id, "prorul")
}
}
export default PromotionRule

View File

@@ -1,108 +1,35 @@
import { DAL, PromotionTypeValues } from "@medusajs/framework/types"
import {
DALUtils,
PromotionUtils,
Searchable,
generateEntityId,
} from "@medusajs/framework/utils"
import {
BeforeCreate,
Collection,
Entity,
Enum,
Filter,
Index,
ManyToMany,
ManyToOne,
OnInit,
OneToOne,
OptionalProps,
PrimaryKey,
Property,
Rel,
Unique,
} from "@mikro-orm/core"
import { PromotionUtils, model } from "@medusajs/framework/utils"
import ApplicationMethod from "./application-method"
import Campaign from "./campaign"
import PromotionRule from "./promotion-rule"
type OptionalFields = "is_automatic" | DAL.SoftDeletableModelDateColumns
type OptionalRelations = "application_method" | "campaign"
@Entity({ tableName: "promotion" })
@Filter(DALUtils.mikroOrmSoftDeletableFilterOptions)
export default class Promotion {
[OptionalProps]?: OptionalFields | OptionalRelations
@PrimaryKey({ columnType: "text" })
id!: string
@Searchable()
@Property({ columnType: "text" })
@Index({ name: "IDX_promotion_code" })
@Unique({
name: "IDX_promotion_code_unique",
properties: ["code"],
const Promotion = model
.define("Promotion", {
id: model.id({ prefix: "promo" }).primaryKey(),
code: model
.text()
.searchable()
.unique("IDX_promotion_code_unique")
.index("IDX_promotion_code"),
is_automatic: model.boolean().default(false),
type: model.enum(PromotionUtils.PromotionType).index("IDX_promotion_type"),
campaign: model
.belongsTo(() => Campaign, {
mappedBy: "promotions",
})
.nullable(),
application_method: model
.hasOne<() => typeof ApplicationMethod>(() => ApplicationMethod, {
mappedBy: "promotion",
})
.nullable(),
rules: model.manyToMany<() => typeof PromotionRule>(() => PromotionRule, {
pivotTable: "promotion_promotion_rule",
mappedBy: "promotions",
}),
})
code: string
@ManyToOne(() => Campaign, {
columnType: "text",
fieldName: "campaign_id",
nullable: true,
mapToPk: true,
.cascades({
delete: ["application_method"],
})
campaign_id: string | null = null
@ManyToOne(() => Campaign, { persist: false })
campaign: Rel<Campaign> | null
@Property({ columnType: "boolean", default: false })
is_automatic: boolean = false
@Index({ name: "IDX_promotion_type" })
@Enum(() => PromotionUtils.PromotionType)
type: PromotionTypeValues
@OneToOne({
entity: () => ApplicationMethod,
mappedBy: (am) => am.promotion,
cascade: ["soft-remove"] as any,
})
application_method: Rel<ApplicationMethod>
@ManyToMany(() => PromotionRule, "promotions", {
owner: true,
pivotTable: "promotion_promotion_rule",
cascade: ["soft-remove"] as any,
})
rules = new Collection<Rel<PromotionRule>>(this)
@Property({
onCreate: () => new Date(),
columnType: "timestamptz",
defaultRaw: "now()",
})
created_at: Date
@Property({
onCreate: () => new Date(),
onUpdate: () => new Date(),
columnType: "timestamptz",
defaultRaw: "now()",
})
updated_at: Date
@Property({ columnType: "timestamptz", nullable: true })
deleted_at: Date | null = null
@BeforeCreate()
onCreate() {
this.id = generateEntityId(this.id, "promo")
}
@OnInit()
onInit() {
this.id = generateEntityId(this.id, "promo")
}
}
export default Promotion