refactor: migrate pricing entities to DML models (#10335)

Fixes: FRMW-2810

## Breaking changes
There is only one breaking change

- The `min_quantity` and `max_quantity` properties are now consistently typed as numbers. Earlier, it was [typed as numbers](https://github.com/medusajs/medusa/blob/develop/integration-tests/http/__tests__/price-list/admin/price-list.spec.ts#L68-L69) in some API responses and as [string in others](https://github.com/medusajs/medusa/blob/develop/integration-tests/http/__tests__/price-list/admin/price-list.spec.ts#L186-L187). I did not go to the bottom of this inconsistency, but the tests reveals them.

Co-authored-by: Adrien de Peretti <25098370+adrien2p@users.noreply.github.com>
This commit is contained in:
Harminder Virk
2024-12-02 09:44:41 +00:00
committed by GitHub
co-authored by Adrien de Peretti
parent b4d6a4b3f0
commit 913cf15e2b
18 changed files with 387 additions and 726 deletions
@@ -1,91 +1,20 @@
import { DAL } from "@medusajs/framework/types"
import {
createPsqlIndexStatementHelper,
DALUtils,
generateEntityId,
} from "@medusajs/framework/utils"
import {
BeforeCreate,
Entity,
Filter,
ManyToOne,
OnInit,
OptionalProps,
PrimaryKey,
Property,
Rel,
} from "@mikro-orm/core"
import { model } from "@medusajs/framework/utils"
import PriceList from "./price-list"
type OptionalFields = DAL.SoftDeletableModelDateColumns
const tableName = "price_list_rule"
const PriceListRuleDeletedAtIndex = createPsqlIndexStatementHelper({
tableName: tableName,
columns: "deleted_at",
where: "deleted_at IS NOT NULL",
})
const PriceListRulePriceListIdIndex = createPsqlIndexStatementHelper({
tableName: tableName,
columns: "price_list_id",
where: "deleted_at IS NULL",
})
@Entity({ tableName })
@Filter(DALUtils.mikroOrmSoftDeletableFilterOptions)
export default class PriceListRule {
[OptionalProps]: OptionalFields
@PrimaryKey({ columnType: "text" })
id!: string
@Property({ columnType: "text" })
attribute: string
@Property({ columnType: "jsonb", nullable: true })
value: string | string[] | null = null
@PriceListRulePriceListIdIndex.MikroORMIndex()
@ManyToOne(() => PriceList, {
columnType: "text",
mapToPk: true,
fieldName: "price_list_id",
onDelete: "cascade",
const PriceListRule = model
.define("PriceListRule", {
id: model.id({ prefix: "prule" }).primaryKey(),
attribute: model.text(),
value: model.json().nullable(),
price_list: model.belongsTo(() => PriceList, {
mappedBy: "price_list_rules",
}),
})
price_list_id: string
.indexes([
{
on: ["price_list_id"],
where: "deleted_at IS NULL",
},
])
@ManyToOne(() => PriceList, { persist: false })
price_list: Rel<PriceList>
@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
@PriceListRuleDeletedAtIndex.MikroORMIndex()
@Property({ columnType: "timestamptz", nullable: true })
deleted_at: Date | null = null
@BeforeCreate()
beforeCreate() {
this.id = generateEntityId(this.id, "plrule")
this.price_list_id ??= this.price_list?.id!
}
@OnInit()
onInit() {
this.id = generateEntityId(this.id, "plrule")
this.price_list_id ??= this.price_list?.id!
}
}
export default PriceListRule
+20 -106
View File
@@ -1,116 +1,30 @@
import { DAL } from "@medusajs/framework/types"
import {
createPsqlIndexStatementHelper,
DALUtils,
generateEntityId,
model,
PriceListStatus,
PriceListType,
Searchable,
} from "@medusajs/framework/utils"
import {
BeforeCreate,
Cascade,
Collection,
Entity,
Enum,
Filter,
OneToMany,
OnInit,
OptionalProps,
PrimaryKey,
Property,
Rel,
} from "@mikro-orm/core"
import Price from "./price"
import PriceListRule from "./price-list-rule"
type OptionalFields =
| "starts_at"
| "ends_at"
| DAL.SoftDeletableModelDateColumns
const tableName = "price_list"
const PriceListDeletedAtIndex = createPsqlIndexStatementHelper({
tableName: tableName,
columns: "deleted_at",
where: "deleted_at IS NOT NULL",
})
export const PriceListIdPrefix = "plist"
@Entity({ tableName })
@Filter(DALUtils.mikroOrmSoftDeletableFilterOptions)
export default class PriceList {
[OptionalProps]: OptionalFields
@PrimaryKey({ columnType: "text" })
id!: string
@Searchable()
@Property({ columnType: "text" })
title: string
@Searchable()
@Property({ columnType: "text" })
description: string
@Enum({ items: () => PriceListStatus, default: PriceListStatus.DRAFT })
status: PriceListStatus
@Enum({ items: () => PriceListType, default: PriceListType.SALE })
type: PriceListType
@Property({
columnType: "timestamptz",
nullable: true,
const PriceList = model
.define("PriceList", {
id: model.id({ prefix: "plist" }).primaryKey(),
title: model.text().searchable(),
description: model.text().searchable(),
status: model.enum(PriceListStatus).default(PriceListStatus.DRAFT),
type: model.enum(PriceListType).default(PriceListType.SALE),
starts_at: model.dateTime().nullable(),
ends_at: model.dateTime().nullable(),
rules_count: model.number().default(0).nullable(),
prices: model.hasMany(() => Price, {
mappedBy: "price_list",
}),
price_list_rules: model.hasMany(() => PriceListRule, {
mappedBy: "price_list",
}),
})
starts_at: Date | null = null
@Property({
columnType: "timestamptz",
nullable: true,
.cascades({
delete: ["price_list_rules", "prices"],
})
ends_at: Date | null = null
@OneToMany(() => Price, (price) => price.price_list, {
cascade: [Cascade.PERSIST, "soft-remove" as Cascade],
})
prices = new Collection<Rel<Price>>(this)
@OneToMany(() => PriceListRule, (pr) => pr.price_list, {
cascade: [Cascade.PERSIST, "soft-remove" as Cascade],
})
price_list_rules = new Collection<Rel<PriceListRule>>(this)
@Property({ columnType: "integer", default: 0 })
rules_count: number = 0
@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
@PriceListDeletedAtIndex.MikroORMIndex()
@Property({ columnType: "timestamptz", nullable: true })
deleted_at: Date | null = null
@BeforeCreate()
onCreate() {
this.id = generateEntityId(this.id, PriceListIdPrefix)
}
@OnInit()
onInit() {
this.id = generateEntityId(this.id, PriceListIdPrefix)
}
}
export default PriceList
@@ -1,75 +1,19 @@
import {
createPsqlIndexStatementHelper,
DALUtils,
generateEntityId,
} from "@medusajs/framework/utils"
import {
BeforeCreate,
Entity,
Filter,
OnInit,
PrimaryKey,
Property,
} from "@mikro-orm/core"
import { model } from "@medusajs/framework/utils"
export const uniquePreferenceRuleIndexName =
"IDX_price_preference_attribute_value"
const UniquePreferenceRuleIndexStatement = createPsqlIndexStatementHelper({
name: uniquePreferenceRuleIndexName,
tableName: "price_preference",
columns: ["attribute", "value"],
unique: true,
where: "deleted_at IS NULL",
})
const DeletedAtIndex = createPsqlIndexStatementHelper({
tableName: "price_preference",
columns: "deleted_at",
where: "deleted_at IS NOT NULL",
})
@Entity()
@Filter(DALUtils.mikroOrmSoftDeletableFilterOptions)
@UniquePreferenceRuleIndexStatement.MikroORMIndex()
export default class PricePreference {
@PrimaryKey({ columnType: "text" })
id: string
@Property({ columnType: "text" })
attribute: string
@Property({ columnType: "text", nullable: true })
value: string | null = null
@Property({ default: false })
is_tax_inclusive: boolean
@Property({
onCreate: () => new Date(),
columnType: "timestamptz",
defaultRaw: "now()",
const PricePreference = model
.define("PricePreference", {
id: model.id({ prefix: "prpref" }).primaryKey(),
attribute: model.text(),
value: model.text().nullable(),
is_tax_inclusive: model.boolean().default(false),
})
created_at: Date
.indexes([
{
name: "IDX_price_preference_attribute_value",
on: ["attribute", "value"],
unique: true,
where: "deleted_at IS NULL",
},
])
@Property({
onCreate: () => new Date(),
onUpdate: () => new Date(),
columnType: "timestamptz",
defaultRaw: "now()",
})
updated_at: Date
@DeletedAtIndex.MikroORMIndex()
@Property({ columnType: "timestamptz", nullable: true })
deleted_at: Date | null = null
@BeforeCreate()
onCreate() {
this.id = generateEntityId(this.id, "prpref")
}
@OnInit()
onInit() {
this.id = generateEntityId(this.id, "prpref")
}
}
export default PricePreference
@@ -1,102 +1,23 @@
import { DAL, PricingRuleOperatorValues } from "@medusajs/framework/types"
import {
createPsqlIndexStatementHelper,
DALUtils,
generateEntityId,
PricingRuleOperator,
} from "@medusajs/framework/utils"
import {
BeforeCreate,
Entity,
Enum,
Filter,
Index,
ManyToOne,
OnInit,
OptionalProps,
PrimaryKey,
Property,
Rel,
} from "@mikro-orm/core"
import { model, PricingRuleOperator } from "@medusajs/framework/utils"
import Price from "./price"
type OptionalFields = DAL.SoftDeletableModelDateColumns
const tableName = "price_rule"
const PriceRuleDeletedAtIndex = createPsqlIndexStatementHelper({
tableName: tableName,
columns: "deleted_at",
where: "deleted_at IS NOT NULL",
})
const PriceRulePriceIdIndex = createPsqlIndexStatementHelper({
tableName: tableName,
columns: ["price_id", "attribute", "operator"],
where: "deleted_at IS NULL",
unique: true,
})
@Entity({ tableName })
@Filter(DALUtils.mikroOrmSoftDeletableFilterOptions)
export default class PriceRule {
[OptionalProps]?: OptionalFields
@PrimaryKey({ columnType: "text" })
id!: string
@Property({ columnType: "text" })
attribute: string
@Index({ name: "IDX_price_rule_operator" })
@Enum({ items: () => PricingRuleOperator, default: PricingRuleOperator.EQ })
operator: PricingRuleOperatorValues = PricingRuleOperator.EQ
@Property({ columnType: "text" })
value: string
@Property({ columnType: "integer", default: 0 })
priority: number = 0
@PriceRulePriceIdIndex.MikroORMIndex()
@ManyToOne(() => Price, {
columnType: "text",
mapToPk: true,
fieldName: "price_id",
onDelete: "cascade",
const PriceRule = model
.define("PriceRule", {
id: model.id({ prefix: "prule" }).primaryKey(),
attribute: model.text(),
value: model.text(),
operator: model.enum(PricingRuleOperator).default(PricingRuleOperator.EQ),
priority: model.number().default(0),
price: model.belongsTo(() => Price, {
mappedBy: "price_rules",
}),
})
price_id: string
.indexes([
{
on: ["price_id", "attribute", "operator"],
where: "deleted_at IS NULL",
unique: true,
},
])
@ManyToOne(() => Price, { persist: false })
price: Rel<Price>
@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
@PriceRuleDeletedAtIndex.MikroORMIndex()
@Property({ columnType: "timestamptz", nullable: true })
deleted_at: Date | null = null
@BeforeCreate()
beforeCreate() {
this.id = generateEntityId(this.id, "prule")
this.price_id ??= this.price?.id!
}
@OnInit()
onInit() {
this.id = generateEntityId(this.id, "prule")
this.price_id ??= this.price?.id!
}
}
export default PriceRule
@@ -1,68 +1,15 @@
import {
createPsqlIndexStatementHelper,
DALUtils,
generateEntityId,
} from "@medusajs/framework/utils"
import {
BeforeCreate,
Cascade,
Collection,
Entity,
Filter,
OneToMany,
OnInit,
PrimaryKey,
Property,
Rel,
} from "@mikro-orm/core"
import { model } from "@medusajs/framework/utils"
import Price from "./price"
const tableName = "price_set"
const PriceSetDeletedAtIndex = createPsqlIndexStatementHelper({
tableName: tableName,
columns: "deleted_at",
where: "deleted_at IS NOT NULL",
})
export const PriceSetIdPrefix = "pset"
@Entity({ tableName })
@Filter(DALUtils.mikroOrmSoftDeletableFilterOptions)
export default class PriceSet {
@PrimaryKey({ columnType: "text" })
id!: string
@OneToMany(() => Price, (price) => price.price_set, {
cascade: [Cascade.PERSIST, "soft-remove" as Cascade],
const PriceSet = model
.define("PriceSet", {
id: model.id({ prefix: "pset" }).primaryKey(),
prices: model.hasMany(() => Price, {
mappedBy: "price_set",
}),
})
prices = new Collection<Rel<Price>>(this)
@Property({
onCreate: () => new Date(),
columnType: "timestamptz",
defaultRaw: "now()",
.cascades({
delete: ["prices"],
})
created_at: Date
@Property({
onCreate: () => new Date(),
onUpdate: () => new Date(),
columnType: "timestamptz",
defaultRaw: "now()",
})
updated_at: Date
@PriceSetDeletedAtIndex.MikroORMIndex()
@Property({ columnType: "timestamptz", nullable: true })
deleted_at: Date | null = null
@BeforeCreate()
onCreate() {
this.id = generateEntityId(this.id, PriceSetIdPrefix)
}
@OnInit()
onInit() {
this.id = generateEntityId(this.id, PriceSetIdPrefix)
}
}
export default PriceSet
+38 -141
View File
@@ -1,148 +1,45 @@
import { DAL } from "@medusajs/framework/types"
import {
BigNumber,
createPsqlIndexStatementHelper,
DALUtils,
generateEntityId,
MikroOrmBigNumberProperty,
} from "@medusajs/framework/utils"
import {
BeforeCreate,
Cascade,
Collection,
Entity,
Filter,
ManyToOne,
OneToMany,
OnInit,
OptionalProps,
PrimaryKey,
Property,
Rel,
} from "@mikro-orm/core"
import { model } from "@medusajs/framework/utils"
import PriceList from "./price-list"
import PriceRule from "./price-rule"
import PriceSet from "./price-set"
type OptionalFields = DAL.SoftDeletableModelDateColumns
const tableName = "price"
const PriceDeletedAtIndex = createPsqlIndexStatementHelper({
tableName: tableName,
columns: "deleted_at",
where: "deleted_at IS NOT NULL",
})
const PricePriceSetIdIndex = createPsqlIndexStatementHelper({
tableName: tableName,
columns: "price_set_id",
where: "deleted_at IS NULL",
})
const PricePriceListIdIndex = createPsqlIndexStatementHelper({
tableName: tableName,
columns: "price_list_id",
where: "deleted_at IS NULL",
})
const PriceCurrencyCodeIndex = createPsqlIndexStatementHelper({
tableName: tableName,
columns: "currency_code",
where: "deleted_at IS NULL",
})
@Entity({ tableName })
@Filter(DALUtils.mikroOrmSoftDeletableFilterOptions)
export default class Price {
[OptionalProps]?: OptionalFields
@PrimaryKey({ columnType: "text" })
id!: string
@Property({ columnType: "text", nullable: true })
title: string | null = null
@PriceCurrencyCodeIndex.MikroORMIndex()
@Property({ columnType: "text" })
currency_code: string
@MikroOrmBigNumberProperty()
amount: BigNumber | number
@Property({ columnType: "jsonb" })
raw_amount: Record<string, unknown>
@Property({ columnType: "numeric", nullable: true })
min_quantity: number | null = null
@Property({ columnType: "numeric", nullable: true })
max_quantity: number | null = null
@PricePriceSetIdIndex.MikroORMIndex()
@ManyToOne(() => PriceSet, {
columnType: "text",
mapToPk: true,
fieldName: "price_set_id",
onDelete: "cascade",
const Price = model
.define("Price", {
id: model.id({ prefix: "price" }).primaryKey(),
title: model.text().nullable(),
currency_code: model.text(),
amount: model.bigNumber(),
min_quantity: model.number().nullable(),
max_quantity: model.number().nullable(),
rules_count: model.number().default(0).nullable(),
price_set: model.belongsTo(() => PriceSet, {
mappedBy: "prices",
}),
price_rules: model.hasMany(() => PriceRule, {
mappedBy: "price",
}),
price_list: model
.belongsTo(() => PriceList, {
mappedBy: "prices",
})
.nullable(),
})
price_set_id: string
@ManyToOne(() => PriceSet, { persist: false })
price_set?: Rel<PriceSet>
@Property({ columnType: "integer", default: 0 })
rules_count: number = 0
@OneToMany({
entity: () => PriceRule,
mappedBy: (pr) => pr.price,
cascade: [Cascade.PERSIST, "soft-remove" as Cascade],
.cascades({
delete: ["price_rules"],
})
price_rules = new Collection<Rel<PriceRule>>(this)
.indexes([
{
on: ["price_set_id"],
where: "deleted_at IS NULL",
},
{
on: ["price_list_id"],
where: "deleted_at IS NULL",
},
{
on: ["currency_code"],
where: "deleted_at IS NULL",
},
])
@PricePriceListIdIndex.MikroORMIndex()
@ManyToOne(() => PriceList, {
columnType: "text",
mapToPk: true,
nullable: true,
fieldName: "price_list_id",
onDelete: "cascade",
})
price_list_id: string | null = null
@ManyToOne(() => PriceList, { persist: false, nullable: true })
price_list: Rel<PriceList> | null = 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
@PriceDeletedAtIndex.MikroORMIndex()
@Property({ columnType: "timestamptz", nullable: true })
deleted_at: Date | null = null
@BeforeCreate()
onCreate() {
this.id = generateEntityId(this.id, "price")
this.price_set_id ??= this.price_set?.id!
this.price_list_id ??= this.price_list?.id!
}
@OnInit()
onInit() {
this.id = generateEntityId(this.id, "price")
this.price_set_id ??= this.price_set?.id!
this.price_list_id ??= this.price_list?.id!
}
}
export default Price