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,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