feat(pricing, types, utils): Soft delete pricing entities (#5858)

This commit is contained in:
Philip Korsholm
2024-01-04 09:02:49 +01:00
committed by GitHub
parent 1468646b99
commit 3550750975
30 changed files with 795 additions and 159 deletions
+22 -8
View File
@@ -1,12 +1,14 @@
import { generateEntityId } from "@medusajs/utils"
import { DALUtils, generateEntityId } from "@medusajs/utils"
import {
BeforeCreate,
Collection,
Entity,
Filter,
Index,
ManyToMany,
ManyToOne,
OneToOne,
OnInit,
OptionalProps,
PrimaryKey,
Property,
@@ -17,18 +19,20 @@ import { PriceSetMoneyAmount } from "./index"
import PriceSet from "./price-set"
@Entity()
@Filter(DALUtils.mikroOrmSoftDeletableFilterOptions)
class MoneyAmount {
[OptionalProps]?:
| "created_at"
| "updated_at"
| "deleted_at"
| "price_set_money_amount"
| "amount"
@PrimaryKey({ columnType: "text" })
id!: string
@Property({ columnType: "text", nullable: true })
currency_code?: string
currency_code: string | null
@ManyToMany({
entity: () => PriceSet,
@@ -39,6 +43,7 @@ class MoneyAmount {
@OneToOne({
entity: () => PriceSetMoneyAmount,
mappedBy: (psma) => psma.money_amount,
cascade: ["soft-remove"] as any,
})
price_set_money_amount: PriceSetMoneyAmount
@@ -47,16 +52,20 @@ class MoneyAmount {
index: "IDX_money_amount_currency_code",
fieldName: "currency_code",
})
currency?: Currency
currency: Currency
@Property({ columnType: "numeric", nullable: true, serializer: Number })
amount?: number
@Property({
columnType: "numeric",
nullable: true,
serializer: Number,
})
amount: number | null
@Property({ columnType: "numeric", nullable: true })
min_quantity?: number | null
min_quantity: number | null
@Property({ columnType: "numeric", nullable: true })
max_quantity?: number | null
max_quantity: number | null
@Property({
onCreate: () => new Date(),
@@ -75,12 +84,17 @@ class MoneyAmount {
@Index({ name: "IDX_money_amount_deleted_at" })
@Property({ columnType: "timestamptz", nullable: true })
deleted_at: Date
deleted_at: Date | null
@BeforeCreate()
onCreate() {
this.id = generateEntityId(this.id, "ma")
}
@OnInit()
onInit() {
this.id = generateEntityId(this.id, "ma")
}
}
export default MoneyAmount
@@ -2,6 +2,7 @@ import {
BeforeCreate,
Entity,
ManyToOne,
OnInit,
PrimaryKey,
Property,
} from "@mikro-orm/core"
@@ -16,7 +17,7 @@ export default class PriceListRuleValue {
@ManyToOne(() => PriceListRule, {
onDelete: "cascade",
fieldName: 'price_list_rule_id',
fieldName: "price_list_rule_id",
index: "IDX_price_list_rule_price_list_rule_value_id",
})
price_list_rule: PriceListRule
@@ -28,4 +29,9 @@ export default class PriceListRuleValue {
onCreate() {
this.id = generateEntityId(this.id, "plrv")
}
@OnInit()
onInit() {
this.id = generateEntityId(this.id, "plrv")
}
}
@@ -5,6 +5,7 @@ import {
Entity,
ManyToOne,
OneToMany,
OnInit,
OptionalProps,
PrimaryKey,
Unique,
@@ -54,4 +55,9 @@ export default class PriceListRule {
beforeCreate() {
this.id = generateEntityId(this.id, "plrule")
}
@OnInit()
onInit() {
this.id = generateEntityId(this.id, "plrule")
}
}
+18 -10
View File
@@ -1,4 +1,8 @@
import { generateEntityId } from "@medusajs/utils"
import {
generateEntityId,
PriceListStatus,
PriceListType,
} from "@medusajs/utils"
import {
BeforeCreate,
Cascade,
@@ -8,12 +12,11 @@ import {
Index,
ManyToMany,
OneToMany,
OnInit,
OptionalProps,
PrimaryKey,
Property,
} from "@mikro-orm/core"
import { PriceListStatus, PriceListType } from "@medusajs/utils"
import PriceListRule from "./price-list-rule"
import PriceSetMoneyAmount from "./price-set-money-amount"
import RuleType from "./rule-type"
@@ -47,22 +50,22 @@ export default class PriceList {
description: string
@Enum({ items: () => PriceListStatus, default: PriceListStatus.DRAFT })
status?: PriceListStatus
status: PriceListStatus
@Enum({ items: () => PriceListType, default: PriceListType.SALE })
type?: PriceListType
type: PriceListType
@Property({
columnType: "timestamptz",
nullable: true,
})
starts_at?: Date | null
starts_at: Date | null
@Property({
columnType: "timestamptz",
nullable: true,
})
ends_at?: Date | null
ends_at: Date | null
@OneToMany(() => PriceSetMoneyAmount, (psma) => psma.price_list, {
cascade: [Cascade.REMOVE],
@@ -89,7 +92,7 @@ export default class PriceList {
columnType: "timestamptz",
defaultRaw: "now()",
})
created_at?: Date
created_at: Date
@Property({
onCreate: () => new Date(),
@@ -97,14 +100,19 @@ export default class PriceList {
columnType: "timestamptz",
defaultRaw: "now()",
})
updated_at?: Date
updated_at: Date
@Index({ name: "IDX_price_list_deleted_at" })
@Property({ columnType: "timestamptz", nullable: true })
deleted_at?: Date
deleted_at: Date | null
@BeforeCreate()
onCreate() {
this.id = generateEntityId(this.id, "plist")
}
@OnInit()
onInit() {
this.id = generateEntityId(this.id, "plist")
}
}
+36 -2
View File
@@ -1,21 +1,31 @@
import {
BeforeCreate,
Entity,
Filter,
Index,
ManyToOne,
OnInit,
OptionalProps,
PrimaryKey,
Property,
} from "@mikro-orm/core"
import { DALUtils, generateEntityId } from "@medusajs/utils"
import { generateEntityId } from "@medusajs/utils"
import PriceSet from "./price-set"
import PriceSetMoneyAmount from "./price-set-money-amount"
import RuleType from "./rule-type"
type OptionalFields = "id" | "is_dynamic" | "priority"
type OptionalFields =
| "id"
| "is_dynamic"
| "priority"
| "created_at"
| "updated_at"
| "deleted_at"
type OptionalRelations = "price_set" | "rule_type" | "price_set_money_amount"
@Entity()
@Filter(DALUtils.mikroOrmSoftDeletableFilterOptions)
export default class PriceRule {
[OptionalProps]: OptionalFields | OptionalRelations
@@ -54,8 +64,32 @@ export default class PriceRule {
})
price_set_money_amount: PriceSetMoneyAmount
@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
@Index({ name: "IDX_price_rule_deleted_at" })
@Property({ columnType: "timestamptz", nullable: true })
deleted_at: Date | null
@BeforeCreate()
beforeCreate() {
this.id = generateEntityId(this.id, "prule")
}
@OnInit()
onInit() {
this.id = generateEntityId(this.id, "prule")
}
}
@@ -3,6 +3,7 @@ import {
BeforeCreate,
Entity,
ManyToOne,
OnInit,
PrimaryKey,
Property,
} from "@mikro-orm/core"
@@ -19,12 +20,12 @@ export default class PriceSetMoneyAmountRules {
onDelete: "cascade",
index: "IDX_price_set_money_amount_rules_price_set_money_amount_id",
})
price_set_money_amount?: PriceSetMoneyAmount | string
price_set_money_amount: PriceSetMoneyAmount
@ManyToOne(() => RuleType, {
index: "IDX_price_set_money_amount_rules_rule_type_id",
})
rule_type?: RuleType | string
rule_type: RuleType
@Property({ columnType: "text" })
value: string
@@ -33,4 +34,9 @@ export default class PriceSetMoneyAmountRules {
onCreate() {
this.id = generateEntityId(this.id, "psmar")
}
@OnInit()
onInit() {
this.id = generateEntityId(this.id, "psmar")
}
}
@@ -1,15 +1,20 @@
import { generateEntityId } from "@medusajs/utils"
import {
BeforeCreate,
Cascade,
Collection,
Entity,
Filter,
Index,
ManyToOne,
OnInit,
OneToMany,
OneToOne,
OptionalProps,
PrimaryKey,
PrimaryKeyType,
Property,
} from "@mikro-orm/core"
import { DALUtils, generateEntityId } from "@medusajs/utils"
import MoneyAmount from "./money-amount"
import PriceList from "./price-list"
@@ -18,7 +23,10 @@ import PriceSet from "./price-set"
import PriceSetMoneyAmountRules from "./price-set-money-amount-rules"
@Entity()
@Filter(DALUtils.mikroOrmSoftDeletableFilterOptions)
export default class PriceSetMoneyAmount {
[OptionalProps]?: "created_at" | "updated_at" | "deleted_at"
@PrimaryKey({ columnType: "text" })
id!: string
@@ -29,20 +37,21 @@ export default class PriceSetMoneyAmount {
onDelete: "cascade",
index: "IDX_price_set_money_amount_price_set_id",
})
price_set?: PriceSet
price_set: PriceSet
@OneToOne(() => MoneyAmount, {
onDelete: "cascade",
index: "IDX_price_set_money_amount_money_amount_id",
})
money_amount?: MoneyAmount
money_amount: MoneyAmount
@Property({ columnType: "integer", default: 0 })
rules_count?: number
rules_count: number
@OneToMany({
entity: () => PriceRule,
mappedBy: (pr) => pr.price_set_money_amount,
cascade: ["soft-remove"] as any,
})
price_rules = new Collection<PriceRule>(this)
@@ -55,15 +64,39 @@ export default class PriceSetMoneyAmount {
@ManyToOne(() => PriceList, {
index: "IDX_price_rule_price_list_id",
onDelete: "cascade",
cascade: [Cascade.REMOVE, "soft-remove"] as any,
nullable: true,
})
price_list?: PriceList
price_list: PriceList | null
@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 , index: "IDX_price_set_money_amount_deleted_at"})
deleted_at: Date | null
@BeforeCreate()
onCreate() {
this.id = generateEntityId(this.id, "psma")
}
@OnInit()
onInit() {
this.id = generateEntityId(this.id, "psma")
}
[PrimaryKeyType]?: [string, string]
constructor(money_amount: MoneyAmount, price_set: PriceSet) {
@@ -2,8 +2,8 @@ import {
BeforeCreate,
Entity,
ManyToOne,
OnInit,
PrimaryKey,
PrimaryKeyType,
} from "@mikro-orm/core"
import PriceSet from "./price-set"
@@ -30,4 +30,9 @@ export default class PriceSetRuleType {
onCreate() {
this.id = generateEntityId(this.id, "psrt")
}
@OnInit()
onInit() {
this.id = generateEntityId(this.id, "psrt")
}
}
+6
View File
@@ -6,6 +6,7 @@ import {
Entity,
ManyToMany,
OneToMany,
OnInit,
OptionalProps,
PrimaryKey,
} from "@mikro-orm/core"
@@ -50,4 +51,9 @@ export default class PriceSet {
onCreate() {
this.id = generateEntityId(this.id, "pset")
}
@OnInit()
onInit() {
this.id = generateEntityId(this.id, "pset")
}
}
+8 -2
View File
@@ -4,6 +4,7 @@ import {
Collection,
Entity,
ManyToMany,
OnInit,
OptionalProps,
PrimaryKey,
Property,
@@ -28,13 +29,18 @@ class RuleType {
@Property({ columnType: "integer", default: 0 })
default_priority: number
@ManyToMany(() => PriceSet, priceSet => priceSet.rule_types)
price_sets = new Collection<PriceSet>(this);
@ManyToMany(() => PriceSet, (priceSet) => priceSet.rule_types)
price_sets = new Collection<PriceSet>(this)
@BeforeCreate()
onCreate() {
this.id = generateEntityId(this.id, "rul-typ")
}
@OnInit()
onInit() {
this.id = generateEntityId(this.id, "rul-typ")
}
}
export default RuleType