feat: Completely revamp the pricing module (#7852)

* feat: Completely revamp the pricing module

* chore: Update all places to the new pricing interfaces

* fix: Remove unnecessary join to itself

* chore: Add data migration for existing users

* fix: Apply the correct index to price rule
This commit is contained in:
Stevche Radevski
2024-07-01 09:47:03 +02:00
committed by GitHub
parent 1f360a3245
commit c661180c44
84 changed files with 661 additions and 3725 deletions
@@ -1,8 +1,5 @@
export { default as Price } from "./price"
export { default as PriceList } from "./price-list"
export { default as PriceListRule } from "./price-list-rule"
export { default as PriceListRuleValue } from "./price-list-rule-value"
export { default as PriceRule } from "./price-rule"
export { default as PriceSet } from "./price-set"
export { default as PriceSetRuleType } from "./price-set-rule-type"
export { default as RuleType } from "./rule-type"
@@ -1,88 +0,0 @@
import { DAL } from "@medusajs/types"
import {
createPsqlIndexStatementHelper,
DALUtils,
generateEntityId,
} from "@medusajs/utils"
import {
BeforeCreate,
Entity,
Filter,
ManyToOne,
OnInit,
OptionalProps,
PrimaryKey,
Property,
Rel,
} from "@mikro-orm/core"
import PriceListRule from "./price-list-rule"
type OptionalFields = DAL.SoftDeletableEntityDateColumns
const tableName = "price_list_rule_value"
const PriceListRuleValueDeletedAtIndex = createPsqlIndexStatementHelper({
tableName: tableName,
columns: "deleted_at",
where: "deleted_at IS NOT NULL",
})
const PriceListPriceListRuleIdIndex = createPsqlIndexStatementHelper({
tableName: tableName,
columns: "price_list_rule_id",
where: "deleted_at IS NULL",
})
@Entity({ tableName })
@Filter(DALUtils.mikroOrmSoftDeletableFilterOptions)
export default class PriceListRuleValue {
[OptionalProps]?: OptionalFields
@PrimaryKey({ columnType: "text" })
id!: string
@PriceListPriceListRuleIdIndex.MikroORMIndex()
@ManyToOne(() => PriceListRule, {
columnType: "text",
mapToPk: true,
fieldName: "price_list_rule_id",
onDelete: "cascade",
})
price_list_rule_id: string
@ManyToOne(() => PriceListRule, { persist: false })
price_list_rule: Rel<PriceListRule>
@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
@PriceListRuleValueDeletedAtIndex.MikroORMIndex()
@Property({ columnType: "timestamptz", nullable: true })
deleted_at: Date | null = null
@BeforeCreate()
onCreate() {
this.id = generateEntityId(this.id, "plrv")
this.price_list_rule_id ??= this.price_list_rule?.id
}
@OnInit()
onInit() {
this.id = generateEntityId(this.id, "plrv")
this.price_list_rule_id ??= this.price_list_rule?.id
}
}
@@ -6,12 +6,9 @@ import {
} from "@medusajs/utils"
import {
BeforeCreate,
Cascade,
Collection,
Entity,
Filter,
ManyToOne,
OneToMany,
OnInit,
OptionalProps,
PrimaryKey,
@@ -19,8 +16,6 @@ import {
Rel,
} from "@mikro-orm/core"
import PriceList from "./price-list"
import PriceListRuleValue from "./price-list-rule-value"
import RuleType from "./rule-type"
type OptionalFields = DAL.SoftDeletableEntityDateColumns
@@ -31,13 +26,6 @@ const PriceListRuleDeletedAtIndex = createPsqlIndexStatementHelper({
where: "deleted_at IS NOT NULL",
})
const PriceListRuleRuleTypeIdIndex = createPsqlIndexStatementHelper({
tableName: tableName,
columns: "rule_type_id",
where: "deleted_at IS NULL",
unique: true,
})
const PriceListRulePriceListIdIndex = createPsqlIndexStatementHelper({
tableName: tableName,
columns: "price_list_id",
@@ -52,21 +40,11 @@ export default class PriceListRule {
@PrimaryKey({ columnType: "text" })
id!: string
@PriceListRuleRuleTypeIdIndex.MikroORMIndex()
@ManyToOne(() => RuleType, {
columnType: "text",
mapToPk: true,
fieldName: "rule_type_id",
})
rule_type_id: string
@Property({ columnType: "text" })
attribute: string
@ManyToOne(() => RuleType, { persist: false })
rule_type: Rel<RuleType>
@OneToMany(() => PriceListRuleValue, (plrv) => plrv.price_list_rule, {
cascade: [Cascade.PERSIST, "soft-remove" as Cascade],
})
price_list_rule_values = new Collection<Rel<PriceListRuleValue>>(this)
@Property({ columnType: "jsonb", nullable: true })
value: string | string[] | null = null
@PriceListRulePriceListIdIndex.MikroORMIndex()
@ManyToOne(() => PriceList, {
@@ -103,13 +81,11 @@ export default class PriceListRule {
beforeCreate() {
this.id = generateEntityId(this.id, "plrule")
this.price_list_id ??= this.price_list?.id!
this.rule_type_id ??= this.rule_type?.id!
}
@OnInit()
onInit() {
this.id = generateEntityId(this.id, "plrule")
this.price_list_id ??= this.price_list?.id!
this.rule_type_id ??= this.rule_type?.id!
}
}
@@ -14,7 +14,6 @@ import {
Entity,
Enum,
Filter,
ManyToMany,
OneToMany,
OnInit,
OptionalProps,
@@ -24,7 +23,6 @@ import {
} from "@mikro-orm/core"
import Price from "./price"
import PriceListRule from "./price-list-rule"
import RuleType from "./rule-type"
type OptionalFields =
| "starts_at"
@@ -84,12 +82,6 @@ export default class PriceList {
})
price_list_rules = new Collection<Rel<PriceListRule>>(this)
@ManyToMany({
entity: () => RuleType,
pivotEntity: () => PriceListRule,
})
rule_types = new Collection<Rel<RuleType>>(this)
@Property({ columnType: "integer", default: 0 })
rules_count: number = 0
@@ -16,8 +16,6 @@ import {
Rel,
} from "@mikro-orm/core"
import Price from "./price"
import PriceSet from "./price-set"
import RuleType from "./rule-type"
type OptionalFields = DAL.SoftDeletableEntityDateColumns
@@ -28,21 +26,9 @@ const PriceRuleDeletedAtIndex = createPsqlIndexStatementHelper({
where: "deleted_at IS NOT NULL",
})
const PriceRulePriceSetIdIndex = createPsqlIndexStatementHelper({
tableName: tableName,
columns: "price_set_id",
where: "deleted_at IS NULL",
})
const PriceRuleRuleTypeIdIndex = createPsqlIndexStatementHelper({
tableName: tableName,
columns: "rule_type_id",
where: "deleted_at IS NULL",
})
const PriceRulePriceIdIndex = createPsqlIndexStatementHelper({
tableName: tableName,
columns: ["price_id", "rule_type_id"],
columns: ["price_id", "attribute"],
where: "deleted_at IS NULL",
unique: true,
})
@@ -55,28 +41,8 @@ export default class PriceRule {
@PrimaryKey({ columnType: "text" })
id!: string
@PriceRulePriceSetIdIndex.MikroORMIndex()
@ManyToOne(() => PriceSet, {
columnType: "text",
mapToPk: true,
fieldName: "price_set_id",
onDelete: "cascade",
})
price_set_id: string
@ManyToOne(() => PriceSet, { persist: false })
price_set: Rel<PriceSet>
@PriceRuleRuleTypeIdIndex.MikroORMIndex()
@ManyToOne(() => RuleType, {
columnType: "text",
mapToPk: true,
fieldName: "rule_type_id",
})
rule_type_id: string
@ManyToOne(() => RuleType, { persist: false })
rule_type: Rel<RuleType>
@Property({ columnType: "text" })
attribute: string
@Property({ columnType: "text" })
value: string
@@ -118,16 +84,12 @@ export default class PriceRule {
@BeforeCreate()
beforeCreate() {
this.id = generateEntityId(this.id, "prule")
this.rule_type_id ??= this.rule_type?.id!
this.price_set_id ??= this.price_set?.id!
this.price_id ??= this.price?.id!
}
@OnInit()
onInit() {
this.id = generateEntityId(this.id, "prule")
this.rule_type_id ??= this.rule_type?.id!
this.price_set_id ??= this.price_set?.id!
this.price_id ??= this.price?.id!
}
}
@@ -1,89 +0,0 @@
import {
DALUtils,
createPsqlIndexStatementHelper,
generateEntityId,
} from "@medusajs/utils"
import {
BeforeCreate,
Entity,
Filter,
ManyToOne,
OnInit,
PrimaryKey,
Property,
} from "@mikro-orm/core"
import PriceSet from "./price-set"
import RuleType from "./rule-type"
const tableName = "price_set_rule_type"
const PriceSetRuleTypeDeletedAtIndex = createPsqlIndexStatementHelper({
tableName: tableName,
columns: "deleted_at",
where: "deleted_at IS NOT NULL",
})
const PriceSetRuleTypePriceSetIdIndex = createPsqlIndexStatementHelper({
tableName: tableName,
columns: "price_set_id",
where: "deleted_at IS NULL",
})
const PriceSetRuleTypeRuleTypeIdIndex = createPsqlIndexStatementHelper({
tableName: tableName,
columns: "rule_type_id",
where: "deleted_at IS NULL",
})
@Entity({ tableName })
@Filter(DALUtils.mikroOrmSoftDeletableFilterOptions)
export default class PriceSetRuleType {
@PrimaryKey({ columnType: "text" })
id!: string
@PriceSetRuleTypePriceSetIdIndex.MikroORMIndex()
@ManyToOne(() => PriceSet, {
columnType: "text",
mapToPk: true,
fieldName: "price_set_id",
onDelete: "cascade",
})
price_set_id: string
@PriceSetRuleTypeRuleTypeIdIndex.MikroORMIndex()
@ManyToOne(() => RuleType, {
columnType: "text",
mapToPk: true,
fieldName: "rule_type_id",
onDelete: "cascade",
})
rule_type_id: 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
@PriceSetRuleTypeDeletedAtIndex.MikroORMIndex()
@Property({ columnType: "timestamptz", nullable: true })
deleted_at: Date | null = null
@BeforeCreate()
onCreate() {
this.id = generateEntityId(this.id, "psrt")
}
@OnInit()
onInit() {
this.id = generateEntityId(this.id, "psrt")
}
}
@@ -9,7 +9,6 @@ import {
Collection,
Entity,
Filter,
ManyToMany,
OneToMany,
OnInit,
PrimaryKey,
@@ -17,9 +16,6 @@ import {
Rel,
} from "@mikro-orm/core"
import Price from "./price"
import PriceRule from "./price-rule"
import PriceSetRuleType from "./price-set-rule-type"
import RuleType from "./rule-type"
const tableName = "price_set"
const PriceSetDeletedAtIndex = createPsqlIndexStatementHelper({
@@ -41,18 +37,6 @@ export default class PriceSet {
})
prices = new Collection<Rel<Price>>(this)
@OneToMany(() => PriceRule, (pr) => pr.price_set, {
cascade: [Cascade.PERSIST, "soft-remove" as Cascade],
})
price_rules = new Collection<Rel<PriceRule>>(this)
@ManyToMany({
entity: () => RuleType,
pivotEntity: () => PriceSetRuleType,
cascade: ["soft-remove" as Cascade],
})
rule_types = new Collection<Rel<RuleType>>(this)
@Property({
onCreate: () => new Date(),
columnType: "timestamptz",
@@ -1,86 +0,0 @@
import {
DALUtils,
createPsqlIndexStatementHelper,
generateEntityId,
} from "@medusajs/utils"
import {
BeforeCreate,
Collection,
Entity,
Filter,
ManyToMany,
OnInit,
OptionalProps,
PrimaryKey,
Property,
Rel,
} from "@mikro-orm/core"
import PriceSet from "./price-set"
type OptionalFields = "default_priority"
const tableName = "rule_type"
const RuleTypeDeletedAtIndex = createPsqlIndexStatementHelper({
tableName: tableName,
columns: "deleted_at",
where: "deleted_at IS NOT NULL",
})
const RuleTypeRuleAttributeIndex = createPsqlIndexStatementHelper({
tableName: tableName,
columns: "rule_attribute",
where: "deleted_at IS NULL",
})
@Entity({ tableName })
@Filter(DALUtils.mikroOrmSoftDeletableFilterOptions)
class RuleType {
[OptionalProps]?: OptionalFields
@PrimaryKey({ columnType: "text" })
id!: string
@Property({ columnType: "text" })
name: string
@RuleTypeRuleAttributeIndex.MikroORMIndex()
@Property({ columnType: "text" })
rule_attribute: string
@Property({ columnType: "integer", default: 0 })
default_priority: number
@ManyToMany(() => PriceSet, (priceSet) => priceSet.rule_types)
price_sets = new Collection<Rel<PriceSet>>(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
@RuleTypeDeletedAtIndex.MikroORMIndex()
@Property({ columnType: "timestamptz", nullable: true })
deleted_at: Date | null = null
@BeforeCreate()
onCreate() {
this.id = generateEntityId(this.id, "rul-typ")
}
@OnInit()
onInit() {
this.id = generateEntityId(this.id, "rul-typ")
}
}
export default RuleType