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 VirkandAdrien de Peretti authored and GitHub committed 2024-12-02 09:44:41 +00:00
1 parent b4d6a4b3f0
commit 913cf15e2b
18 files changed
+384 -723

No files matched your search

@@ -183,8 +183,8 @@ medusaIntegrationTestRunner({
amount: 100, amount: 100,
currency_code: "usd", currency_code: "usd",
// BREAKING: Min and max quantity are returned as string // BREAKING: Min and max quantity are returned as string
min_quantity: "1", min_quantity: 1,
max_quantity: "100", max_quantity: 100,
variant_id: product1.variants[0].id, variant_id: product1.variants[0].id,
created_at: expect.any(String), created_at: expect.any(String),
updated_at: expect.any(String), updated_at: expect.any(String),
@@ -194,8 +194,8 @@ medusaIntegrationTestRunner({
id: expect.any(String), id: expect.any(String),
amount: 80, amount: 80,
currency_code: "usd", currency_code: "usd",
min_quantity: "101", min_quantity: 101,
max_quantity: "500", max_quantity: 500,
variant_id: product1.variants[0].id, variant_id: product1.variants[0].id,
created_at: expect.any(String), created_at: expect.any(String),
updated_at: expect.any(String), updated_at: expect.any(String),
@@ -360,15 +360,15 @@ medusaIntegrationTestRunner({
amount: 100, amount: 100,
currency_code: "usd", currency_code: "usd",
id: expect.any(String), id: expect.any(String),
max_quantity: "100", max_quantity: 100,
min_quantity: "1", min_quantity: 1,
}), }),
expect.objectContaining({ expect.objectContaining({
amount: 80, amount: 80,
currency_code: "usd", currency_code: "usd",
id: expect.any(String), id: expect.any(String),
max_quantity: "500", max_quantity: 500,
min_quantity: "101", min_quantity: 101,
}), }),
]), ]),
rules: { rules: {
@@ -409,15 +409,15 @@ medusaIntegrationTestRunner({
amount: 100, amount: 100,
currency_code: "usd", currency_code: "usd",
id: expect.any(String), id: expect.any(String),
max_quantity: "100", max_quantity: 100,
min_quantity: "1", min_quantity: 1,
}), }),
expect.objectContaining({ expect.objectContaining({
amount: 250, amount: 250,
currency_code: "eur", currency_code: "eur",
id: expect.any(String), id: expect.any(String),
max_quantity: "500", max_quantity: 500,
min_quantity: "101", min_quantity: 101,
}), }),
]) ])
}) })
@@ -470,16 +470,16 @@ medusaIntegrationTestRunner({
id: expect.any(String), id: expect.any(String),
amount: 100, amount: 100,
currency_code: "usd", currency_code: "usd",
min_quantity: "1", min_quantity: 1,
max_quantity: "100", max_quantity: 100,
variant_id: product1.variants[0].id, variant_id: product1.variants[0].id,
}), }),
expect.objectContaining({ expect.objectContaining({
id: expect.any(String), id: expect.any(String),
amount: 80, amount: 80,
currency_code: "usd", currency_code: "usd",
min_quantity: "101", min_quantity: 101,
max_quantity: "500", max_quantity: 500,
variant_id: product1.variants[0].id, variant_id: product1.variants[0].id,
}), }),
expect.objectContaining({ expect.objectContaining({
@@ -487,24 +487,24 @@ medusaIntegrationTestRunner({
amount: 45, amount: 45,
currency_code: "usd", currency_code: "usd",
variant_id: product1.variants[0].id, variant_id: product1.variants[0].id,
min_quantity: "1001", min_quantity: 1001,
max_quantity: "2000", max_quantity: 2000,
}), }),
expect.objectContaining({ expect.objectContaining({
id: expect.any(String), id: expect.any(String),
amount: 35, amount: 35,
currency_code: "usd", currency_code: "usd",
variant_id: product1.variants[0].id, variant_id: product1.variants[0].id,
min_quantity: "2001", min_quantity: 2001,
max_quantity: "3000", max_quantity: 3000,
}), }),
expect.objectContaining({ expect.objectContaining({
id: expect.any(String), id: expect.any(String),
amount: 25, amount: 25,
currency_code: "usd", currency_code: "usd",
variant_id: product1.variants[0].id, variant_id: product1.variants[0].id,
min_quantity: "3001", min_quantity: 3001,
max_quantity: "4000", max_quantity: 4000,
}), }),
]) ])
) )
@@ -551,16 +551,16 @@ medusaIntegrationTestRunner({
id: expect.any(String), id: expect.any(String),
amount: 45, amount: 45,
currency_code: "usd", currency_code: "usd",
min_quantity: "1", min_quantity: 1,
max_quantity: "100", max_quantity: 100,
variant_id: product1.variants[0].id, variant_id: product1.variants[0].id,
}), }),
expect.objectContaining({ expect.objectContaining({
id: expect.any(String), id: expect.any(String),
amount: 35, amount: 35,
currency_code: "usd", currency_code: "usd",
min_quantity: "101", min_quantity: 101,
max_quantity: "500", max_quantity: 500,
variant_id: product1.variants[0].id, variant_id: product1.variants[0].id,
}), }),
]) ])
@@ -605,16 +605,16 @@ medusaIntegrationTestRunner({
id: expect.any(String), id: expect.any(String),
amount: 100, amount: 100,
currency_code: "usd", currency_code: "usd",
min_quantity: "1", min_quantity: 1,
max_quantity: "100", max_quantity: 100,
variant_id: product1.variants[0].id, variant_id: product1.variants[0].id,
}), }),
expect.objectContaining({ expect.objectContaining({
id: expect.any(String), id: expect.any(String),
amount: 80, amount: 80,
currency_code: "usd", currency_code: "usd",
min_quantity: "101", min_quantity: 101,
max_quantity: "500", max_quantity: 500,
variant_id: product1.variants[0].id, variant_id: product1.variants[0].id,
}), }),
expect.objectContaining({ expect.objectContaining({
@@ -698,8 +698,8 @@ medusaIntegrationTestRunner({
expect.objectContaining({ expect.objectContaining({
amount: 80, amount: 80,
currency_code: "usd", currency_code: "usd",
min_quantity: "101", min_quantity: 101,
max_quantity: "500", max_quantity: 500,
variant_id: product1.variants[0].id, variant_id: product1.variants[0].id,
}), }),
]) ])
@@ -191,6 +191,7 @@ medusaIntegrationTestRunner({
max_quantity: null, max_quantity: null,
min_quantity: null, min_quantity: null,
price_list: null, price_list: null,
price_list_id: null,
price_set_id: expect.any(String), price_set_id: expect.any(String),
raw_amount: { raw_amount: {
precision: 20, precision: 20,
@@ -210,6 +211,7 @@ medusaIntegrationTestRunner({
max_quantity: null, max_quantity: null,
min_quantity: null, min_quantity: null,
price_list: null, price_list: null,
price_list_id: null,
price_set_id: expect.any(String), price_set_id: expect.any(String),
raw_amount: { raw_amount: {
precision: 20, precision: 20,
@@ -1,5 +1,6 @@
import { SqlEntityManager } from "@mikro-orm/postgresql" import { SqlEntityManager } from "@mikro-orm/postgresql"
import { PriceListRule } from "@models" import { PriceListRule } from "@models"
import { toMikroORMEntity } from "@medusajs/framework/utils"
import { defaultPriceListRuleData } from "./data" import { defaultPriceListRuleData } from "./data"
export * from "./data" export * from "./data"
@@ -11,7 +12,7 @@ export async function createPriceListRules(
const priceListRules: PriceListRule[] = [] const priceListRules: PriceListRule[] = []
for (let data of priceListRuleData) { for (let data of priceListRuleData) {
const plr = manager.create(PriceListRule, data) const plr = manager.create(toMikroORMEntity(PriceListRule), data)
priceListRules.push(plr) priceListRules.push(plr)
} }
@@ -1,5 +1,6 @@
import { SqlEntityManager } from "@mikro-orm/postgresql" import { SqlEntityManager } from "@mikro-orm/postgresql"
import { PriceList } from "@models" import { PriceList } from "@models"
import { toMikroORMEntity } from "@medusajs/framework/utils"
import { defaultPriceListData } from "./data" import { defaultPriceListData } from "./data"
export * from "./data" export * from "./data"
@@ -11,7 +12,7 @@ export async function createPriceLists(
const priceLists: PriceList[] = [] const priceLists: PriceList[] = []
for (let data of priceListData) { for (let data of priceListData) {
const pl = manager.create(PriceList, data) const pl = manager.create(toMikroORMEntity(PriceList), data)
priceLists.push(pl) priceLists.push(pl)
} }
@@ -2,6 +2,7 @@ import { PriceRule } from "@models"
import { CreatePriceRuleDTO } from "@medusajs/framework/types" import { CreatePriceRuleDTO } from "@medusajs/framework/types"
import { SqlEntityManager } from "@mikro-orm/postgresql" import { SqlEntityManager } from "@mikro-orm/postgresql"
import { toMikroORMEntity } from "@medusajs/framework/utils"
import { defaultPriceRuleData } from "./data" import { defaultPriceRuleData } from "./data"
export * from "./data" export * from "./data"
@@ -20,7 +21,10 @@ export async function createPriceRules(
priceRuleDataClone.attribute = priceRuleDataClone.attribute priceRuleDataClone.attribute = priceRuleDataClone.attribute
priceRuleDataClone.price_id = priceRuleDataClone.price_id priceRuleDataClone.price_id = priceRuleDataClone.price_id
const priceRule = manager.create(PriceRule, priceRuleDataClone) const priceRule = manager.create(
toMikroORMEntity(PriceRule),
priceRuleDataClone
)
priceRules.push(priceRule) priceRules.push(priceRule)
} }
@@ -1,6 +1,7 @@
import { CreatePriceSetDTO } from "@medusajs/framework/types" import { CreatePriceSetDTO } from "@medusajs/framework/types"
import { SqlEntityManager } from "@mikro-orm/postgresql" import { SqlEntityManager } from "@mikro-orm/postgresql"
import { Price, PriceSet } from "@models" import { Price, PriceSet } from "@models"
import { toMikroORMEntity } from "@medusajs/framework/utils"
import { defaultPriceSetsData } from "./data" import { defaultPriceSetsData } from "./data"
export * from "./data" export * from "./data"
@@ -16,12 +17,15 @@ export async function createPriceSets(
const prices = priceSetDataClone.prices || [] const prices = priceSetDataClone.prices || []
delete priceSetDataClone.prices delete priceSetDataClone.prices
let priceSet = manager.create(PriceSet, priceSetDataClone) as PriceSet let priceSet = manager.create(
toMikroORMEntity(PriceSet),
priceSetDataClone
) as PriceSet
manager.persist(priceSet) manager.persist(priceSet)
for (let priceData of prices) { for (let priceData of prices) {
const price = manager.create(Price, { const price = manager.create(toMikroORMEntity(Price), {
...priceData, ...priceData,
price_set_id: priceSet.id, price_set_id: priceSet.id,
title: "test", title: "test",
@@ -1,6 +1,7 @@
import { SqlEntityManager } from "@mikro-orm/postgresql"
import { Price } from "@models" import { Price } from "@models"
import { defaultPricesData } from "./data" import { defaultPricesData } from "./data"
import { SqlEntityManager } from "@mikro-orm/postgresql"
import { toMikroORMEntity } from "@medusajs/framework/utils"
export * from "./data" export * from "./data"
@@ -11,7 +12,7 @@ export async function createPrices(
const prices: Price[] = [] const prices: Price[] = []
for (let data of pricesData) { for (let data of pricesData) {
const price = manager.create(Price, data) const price = manager.create(toMikroORMEntity(Price), data)
prices.push(price) prices.push(price)
} }
@@ -254,11 +254,14 @@ moduleIntegrationTestRunner<IPricingModuleService>({
ends_at: "10/20/2030", ends_at: "10/20/2030",
}, },
]) ])
expect(priceList).toEqual(
expect.objectContaining({ expect(priceList).toHaveProperty("starts_at")
starts_at: new Date("10/10/2010").toISOString(), expect(priceList).toHaveProperty("ends_at")
ends_at: new Date("10/20/2030").toISOString(), expect(priceList.starts_at?.toString()).toEqual(
}) new Date("10/10/2010").toISOString()
)
expect(priceList.ends_at?.toString()).toEqual(
new Date("10/20/2030").toISOString()
) )
}) })
@@ -378,11 +381,14 @@ moduleIntegrationTestRunner<IPricingModuleService>({
ends_at: "10/20/2030", ends_at: "10/20/2030",
}, },
]) ])
expect(priceList).toEqual(
expect.objectContaining({ expect(priceList).toHaveProperty("starts_at")
starts_at: new Date("10/10/2010").toISOString(), expect(priceList).toHaveProperty("ends_at")
ends_at: new Date("10/20/2030").toISOString(), expect(priceList.starts_at?.toString()).toEqual(
}) new Date("10/10/2010").toString()
)
expect(priceList.ends_at?.toString()).toEqual(
new Date("10/20/2030").toString()
) )
}) })
@@ -562,7 +568,6 @@ moduleIntegrationTestRunner<IPricingModuleService>({
currency_code: "EUR", currency_code: "EUR",
}), }),
expect.objectContaining({ expect.objectContaining({
rules_count: 0,
price_rules: [], price_rules: [],
amount: 600, amount: 600,
currency_code: "EUR", currency_code: "EUR",
@@ -5,7 +5,7 @@ import { Price } from "../../../../src/models"
import { createPrices } from "../../../__fixtures__/price" import { createPrices } from "../../../__fixtures__/price"
import { createPriceRules } from "../../../__fixtures__/price-rule" import { createPriceRules } from "../../../__fixtures__/price-rule"
import { createPriceSets } from "../../../__fixtures__/price-set" import { createPriceSets } from "../../../__fixtures__/price-set"
import { Modules } from "@medusajs/framework/utils" import { Modules, toMikroORMEntity } from "@medusajs/framework/utils"
jest.setTimeout(30000) jest.setTimeout(30000)
@@ -268,13 +268,13 @@ moduleIntegrationTestRunner<IPricingModuleService>({
}) })
it("should create a PriceRule successfully", async () => { it("should create a PriceRule successfully", async () => {
const price: Price = testManager.create(Price, { const price = testManager.create(toMikroORMEntity(Price), {
currency_code: "EUR", currency_code: "EUR",
amount: 100, amount: 100,
price_set_id: "price-set-1", price_set_id: "price-set-1",
title: "test", title: "test",
rules_count: 0, rules_count: 0,
} as Price) })
await testManager.persist(price).flush() await testManager.persist(price).flush()
@@ -1,5 +1,7 @@
{ {
"namespaces": ["public"], "namespaces": [
"public"
],
"name": "public", "name": "public",
"tables": [ "tables": [
{ {
@@ -39,7 +41,10 @@
"primary": false, "primary": false,
"nullable": false, "nullable": false,
"default": "'draft'", "default": "'draft'",
"enumItems": ["active", "draft"], "enumItems": [
"active",
"draft"
],
"mappedType": "enum" "mappedType": "enum"
}, },
"type": { "type": {
@@ -50,7 +55,10 @@
"primary": false, "primary": false,
"nullable": false, "nullable": false,
"default": "'sale'", "default": "'sale'",
"enumItems": ["sale", "override"], "enumItems": [
"sale",
"override"
],
"mappedType": "enum" "mappedType": "enum"
}, },
"starts_at": { "starts_at": {
@@ -79,7 +87,7 @@
"unsigned": false, "unsigned": false,
"autoincrement": false, "autoincrement": false,
"primary": false, "primary": false,
"nullable": false, "nullable": true,
"default": "0", "default": "0",
"mappedType": "integer" "mappedType": "integer"
}, },
@@ -121,15 +129,17 @@
"indexes": [ "indexes": [
{ {
"keyName": "IDX_price_list_deleted_at", "keyName": "IDX_price_list_deleted_at",
"columnNames": ["deleted_at"], "columnNames": [],
"composite": false, "composite": false,
"primary": false, "primary": false,
"unique": false, "unique": false,
"expression": "CREATE INDEX IF NOT EXISTS \"IDX_price_list_deleted_at\" ON \"price_list\" (deleted_at) WHERE deleted_at IS NOT NULL" "expression": "CREATE INDEX IF NOT EXISTS \"IDX_price_list_deleted_at\" ON \"price_list\" (deleted_at) WHERE deleted_at IS NULL"
}, },
{ {
"keyName": "price_list_pkey", "keyName": "price_list_pkey",
"columnNames": ["id"], "columnNames": [
"id"
],
"composite": false, "composite": false,
"primary": true, "primary": true,
"unique": true "unique": true
@@ -214,7 +224,7 @@
"indexes": [ "indexes": [
{ {
"keyName": "IDX_price_list_rule_price_list_id", "keyName": "IDX_price_list_rule_price_list_id",
"columnNames": ["price_list_id"], "columnNames": [],
"composite": false, "composite": false,
"primary": false, "primary": false,
"unique": false, "unique": false,
@@ -222,15 +232,17 @@
}, },
{ {
"keyName": "IDX_price_list_rule_deleted_at", "keyName": "IDX_price_list_rule_deleted_at",
"columnNames": ["deleted_at"], "columnNames": [],
"composite": false, "composite": false,
"primary": false, "primary": false,
"unique": false, "unique": false,
"expression": "CREATE INDEX IF NOT EXISTS \"IDX_price_list_rule_deleted_at\" ON \"price_list_rule\" (deleted_at) WHERE deleted_at IS NOT NULL" "expression": "CREATE INDEX IF NOT EXISTS \"IDX_price_list_rule_deleted_at\" ON \"price_list_rule\" (deleted_at) WHERE deleted_at IS NULL"
}, },
{ {
"keyName": "price_list_rule_pkey", "keyName": "price_list_rule_pkey",
"columnNames": ["id"], "columnNames": [
"id"
],
"composite": false, "composite": false,
"primary": true, "primary": true,
"unique": true "unique": true
@@ -240,9 +252,13 @@
"foreignKeys": { "foreignKeys": {
"price_list_rule_price_list_id_foreign": { "price_list_rule_price_list_id_foreign": {
"constraintName": "price_list_rule_price_list_id_foreign", "constraintName": "price_list_rule_price_list_id_foreign",
"columnNames": ["price_list_id"], "columnNames": [
"price_list_id"
],
"localTableName": "public.price_list_rule", "localTableName": "public.price_list_rule",
"referencedColumnNames": ["id"], "referencedColumnNames": [
"id"
],
"referencedTableName": "public.price_list", "referencedTableName": "public.price_list",
"deleteRule": "cascade", "deleteRule": "cascade",
"updateRule": "cascade" "updateRule": "cascade"
@@ -326,11 +342,11 @@
"indexes": [ "indexes": [
{ {
"keyName": "IDX_price_preference_deleted_at", "keyName": "IDX_price_preference_deleted_at",
"columnNames": ["deleted_at"], "columnNames": [],
"composite": false, "composite": false,
"primary": false, "primary": false,
"unique": false, "unique": false,
"expression": "CREATE INDEX IF NOT EXISTS \"IDX_price_preference_deleted_at\" ON \"price_preference\" (deleted_at) WHERE deleted_at IS NOT NULL" "expression": "CREATE INDEX IF NOT EXISTS \"IDX_price_preference_deleted_at\" ON \"price_preference\" (deleted_at) WHERE deleted_at IS NULL"
}, },
{ {
"keyName": "IDX_price_preference_attribute_value", "keyName": "IDX_price_preference_attribute_value",
@@ -342,7 +358,9 @@
}, },
{ {
"keyName": "price_preference_pkey", "keyName": "price_preference_pkey",
"columnNames": ["id"], "columnNames": [
"id"
],
"composite": false, "composite": false,
"primary": true, "primary": true,
"unique": true "unique": true
@@ -400,15 +418,17 @@
"indexes": [ "indexes": [
{ {
"keyName": "IDX_price_set_deleted_at", "keyName": "IDX_price_set_deleted_at",
"columnNames": ["deleted_at"], "columnNames": [],
"composite": false, "composite": false,
"primary": false, "primary": false,
"unique": false, "unique": false,
"expression": "CREATE INDEX IF NOT EXISTS \"IDX_price_set_deleted_at\" ON \"price_set\" (deleted_at) WHERE deleted_at IS NOT NULL" "expression": "CREATE INDEX IF NOT EXISTS \"IDX_price_set_deleted_at\" ON \"price_set\" (deleted_at) WHERE deleted_at IS NULL"
}, },
{ {
"keyName": "price_set_pkey", "keyName": "price_set_pkey",
"columnNames": ["id"], "columnNames": [
"id"
],
"composite": false, "composite": false,
"primary": true, "primary": true,
"unique": true "unique": true
@@ -455,32 +475,33 @@
"nullable": false, "nullable": false,
"mappedType": "decimal" "mappedType": "decimal"
}, },
"raw_amount": {
"name": "raw_amount",
"type": "jsonb",
"unsigned": false,
"autoincrement": false,
"primary": false,
"nullable": false,
"mappedType": "json"
},
"min_quantity": { "min_quantity": {
"name": "min_quantity", "name": "min_quantity",
"type": "numeric", "type": "integer",
"unsigned": false, "unsigned": false,
"autoincrement": false, "autoincrement": false,
"primary": false, "primary": false,
"nullable": true, "nullable": true,
"mappedType": "decimal" "mappedType": "integer"
}, },
"max_quantity": { "max_quantity": {
"name": "max_quantity", "name": "max_quantity",
"type": "numeric", "type": "integer",
"unsigned": false, "unsigned": false,
"autoincrement": false, "autoincrement": false,
"primary": false, "primary": false,
"nullable": true, "nullable": true,
"mappedType": "decimal" "mappedType": "integer"
},
"rules_count": {
"name": "rules_count",
"type": "integer",
"unsigned": false,
"autoincrement": false,
"primary": false,
"nullable": true,
"default": "0",
"mappedType": "integer"
}, },
"price_set_id": { "price_set_id": {
"name": "price_set_id", "name": "price_set_id",
@@ -491,16 +512,6 @@
"nullable": false, "nullable": false,
"mappedType": "text" "mappedType": "text"
}, },
"rules_count": {
"name": "rules_count",
"type": "integer",
"unsigned": false,
"autoincrement": false,
"primary": false,
"nullable": false,
"default": "0",
"mappedType": "integer"
},
"price_list_id": { "price_list_id": {
"name": "price_list_id", "name": "price_list_id",
"type": "text", "type": "text",
@@ -510,6 +521,15 @@
"nullable": true, "nullable": true,
"mappedType": "text" "mappedType": "text"
}, },
"raw_amount": {
"name": "raw_amount",
"type": "jsonb",
"unsigned": false,
"autoincrement": false,
"primary": false,
"nullable": false,
"mappedType": "json"
},
"created_at": { "created_at": {
"name": "created_at", "name": "created_at",
"type": "timestamptz", "type": "timestamptz",
@@ -546,17 +566,9 @@
"name": "price", "name": "price",
"schema": "public", "schema": "public",
"indexes": [ "indexes": [
{
"keyName": "IDX_price_currency_code",
"columnNames": ["currency_code"],
"composite": false,
"primary": false,
"unique": false,
"expression": "CREATE INDEX IF NOT EXISTS \"IDX_price_currency_code\" ON \"price\" (currency_code) WHERE deleted_at IS NULL"
},
{ {
"keyName": "IDX_price_price_set_id", "keyName": "IDX_price_price_set_id",
"columnNames": ["price_set_id"], "columnNames": [],
"composite": false, "composite": false,
"primary": false, "primary": false,
"unique": false, "unique": false,
@@ -564,7 +576,7 @@
}, },
{ {
"keyName": "IDX_price_price_list_id", "keyName": "IDX_price_price_list_id",
"columnNames": ["price_list_id"], "columnNames": [],
"composite": false, "composite": false,
"primary": false, "primary": false,
"unique": false, "unique": false,
@@ -572,15 +584,25 @@
}, },
{ {
"keyName": "IDX_price_deleted_at", "keyName": "IDX_price_deleted_at",
"columnNames": ["deleted_at"], "columnNames": [],
"composite": false, "composite": false,
"primary": false, "primary": false,
"unique": false, "unique": false,
"expression": "CREATE INDEX IF NOT EXISTS \"IDX_price_deleted_at\" ON \"price\" (deleted_at) WHERE deleted_at IS NOT NULL" "expression": "CREATE INDEX IF NOT EXISTS \"IDX_price_deleted_at\" ON \"price\" (deleted_at) WHERE deleted_at IS NULL"
},
{
"keyName": "IDX_price_currency_code",
"columnNames": [],
"composite": false,
"primary": false,
"unique": false,
"expression": "CREATE INDEX IF NOT EXISTS \"IDX_price_currency_code\" ON \"price\" (currency_code) WHERE deleted_at IS NULL"
}, },
{ {
"keyName": "price_pkey", "keyName": "price_pkey",
"columnNames": ["id"], "columnNames": [
"id"
],
"composite": false, "composite": false,
"primary": true, "primary": true,
"unique": true "unique": true
@@ -590,18 +612,26 @@
"foreignKeys": { "foreignKeys": {
"price_price_set_id_foreign": { "price_price_set_id_foreign": {
"constraintName": "price_price_set_id_foreign", "constraintName": "price_price_set_id_foreign",
"columnNames": ["price_set_id"], "columnNames": [
"price_set_id"
],
"localTableName": "public.price", "localTableName": "public.price",
"referencedColumnNames": ["id"], "referencedColumnNames": [
"id"
],
"referencedTableName": "public.price_set", "referencedTableName": "public.price_set",
"deleteRule": "cascade", "deleteRule": "cascade",
"updateRule": "cascade" "updateRule": "cascade"
}, },
"price_price_list_id_foreign": { "price_price_list_id_foreign": {
"constraintName": "price_price_list_id_foreign", "constraintName": "price_price_list_id_foreign",
"columnNames": ["price_list_id"], "columnNames": [
"price_list_id"
],
"localTableName": "public.price", "localTableName": "public.price",
"referencedColumnNames": ["id"], "referencedColumnNames": [
"id"
],
"referencedTableName": "public.price_list", "referencedTableName": "public.price_list",
"deleteRule": "cascade", "deleteRule": "cascade",
"updateRule": "cascade" "updateRule": "cascade"
@@ -628,17 +658,6 @@
"nullable": false, "nullable": false,
"mappedType": "text" "mappedType": "text"
}, },
"operator": {
"name": "operator",
"type": "text",
"unsigned": false,
"autoincrement": false,
"primary": false,
"nullable": false,
"default": "'eq'",
"enumItems": ["gte", "lte", "gt", "lt", "eq"],
"mappedType": "enum"
},
"value": { "value": {
"name": "value", "name": "value",
"type": "text", "type": "text",
@@ -648,6 +667,23 @@
"nullable": false, "nullable": false,
"mappedType": "text" "mappedType": "text"
}, },
"operator": {
"name": "operator",
"type": "text",
"unsigned": false,
"autoincrement": false,
"primary": false,
"nullable": false,
"default": "'eq'",
"enumItems": [
"gte",
"lte",
"gt",
"lt",
"eq"
],
"mappedType": "enum"
},
"priority": { "priority": {
"name": "priority", "name": "priority",
"type": "integer", "type": "integer",
@@ -704,31 +740,34 @@
"schema": "public", "schema": "public",
"indexes": [ "indexes": [
{ {
"columnNames": ["operator"], "keyName": "IDX_price_rule_price_id",
"columnNames": [],
"composite": false, "composite": false,
"keyName": "IDX_price_rule_operator",
"primary": false, "primary": false,
"unique": false "unique": false,
"expression": "CREATE INDEX IF NOT EXISTS \"IDX_price_rule_price_id\" ON \"price_rule\" (price_id) WHERE deleted_at IS NULL"
},
{
"keyName": "IDX_price_rule_deleted_at",
"columnNames": [],
"composite": false,
"primary": false,
"unique": false,
"expression": "CREATE INDEX IF NOT EXISTS \"IDX_price_rule_deleted_at\" ON \"price_rule\" (deleted_at) WHERE deleted_at IS NULL"
}, },
{ {
"keyName": "IDX_price_rule_price_id_attribute_operator_unique", "keyName": "IDX_price_rule_price_id_attribute_operator_unique",
"columnNames": ["price_id"], "columnNames": [],
"composite": false, "composite": false,
"primary": false, "primary": false,
"unique": false, "unique": false,
"expression": "CREATE UNIQUE INDEX IF NOT EXISTS \"IDX_price_rule_price_id_attribute_operator_unique\" ON \"price_rule\" (price_id, attribute, operator) WHERE deleted_at IS NULL" "expression": "CREATE UNIQUE INDEX IF NOT EXISTS \"IDX_price_rule_price_id_attribute_operator_unique\" ON \"price_rule\" (price_id, attribute, operator) WHERE deleted_at IS NULL"
}, },
{
"keyName": "IDX_price_rule_deleted_at",
"columnNames": ["deleted_at"],
"composite": false,
"primary": false,
"unique": false,
"expression": "CREATE INDEX IF NOT EXISTS \"IDX_price_rule_deleted_at\" ON \"price_rule\" (deleted_at) WHERE deleted_at IS NOT NULL"
},
{ {
"keyName": "price_rule_pkey", "keyName": "price_rule_pkey",
"columnNames": ["id"], "columnNames": [
"id"
],
"composite": false, "composite": false,
"primary": true, "primary": true,
"unique": true "unique": true
@@ -738,9 +777,13 @@
"foreignKeys": { "foreignKeys": {
"price_rule_price_id_foreign": { "price_rule_price_id_foreign": {
"constraintName": "price_rule_price_id_foreign", "constraintName": "price_rule_price_id_foreign",
"columnNames": ["price_id"], "columnNames": [
"price_id"
],
"localTableName": "public.price_rule", "localTableName": "public.price_rule",
"referencedColumnNames": ["id"], "referencedColumnNames": [
"id"
],
"referencedTableName": "public.price", "referencedTableName": "public.price",
"deleteRule": "cascade", "deleteRule": "cascade",
"updateRule": "cascade" "updateRule": "cascade"
@@ -0,0 +1,29 @@
import { Migration } from '@mikro-orm/migrations';
export class Migration20241128055359 extends Migration {
async up(): Promise<void> {
this.addSql('alter table if exists "price_list" alter column "rules_count" type integer using ("rules_count"::integer);');
this.addSql('alter table if exists "price_list" alter column "rules_count" drop not null;');
this.addSql('alter table if exists "price" alter column "min_quantity" type integer using ("min_quantity"::integer);');
this.addSql('alter table if exists "price" alter column "max_quantity" type integer using ("max_quantity"::integer);');
this.addSql('alter table if exists "price" alter column "rules_count" type integer using ("rules_count"::integer);');
this.addSql('alter table if exists "price" alter column "rules_count" drop not null;');
this.addSql('CREATE INDEX IF NOT EXISTS "IDX_price_rule_price_id" ON "price_rule" (price_id) WHERE deleted_at IS NULL;');
}
async down(): Promise<void> {
this.addSql('alter table if exists "price_list" alter column "rules_count" type integer using ("rules_count"::integer);');
this.addSql('alter table if exists "price_list" alter column "rules_count" set not null;');
this.addSql('alter table if exists "price" alter column "min_quantity" type numeric using ("min_quantity"::numeric);');
this.addSql('alter table if exists "price" alter column "max_quantity" type numeric using ("max_quantity"::numeric);');
this.addSql('alter table if exists "price" alter column "rules_count" type integer using ("rules_count"::integer);');
this.addSql('alter table if exists "price" alter column "rules_count" set not null;');
this.addSql('drop index if exists "IDX_price_rule_price_id";');
}
}
@@ -1,91 +1,20 @@
import { DAL } from "@medusajs/framework/types" import { model } from "@medusajs/framework/utils"
import {
createPsqlIndexStatementHelper,
DALUtils,
generateEntityId,
} from "@medusajs/framework/utils"
import {
BeforeCreate,
Entity,
Filter,
ManyToOne,
OnInit,
OptionalProps,
PrimaryKey,
Property,
Rel,
} from "@mikro-orm/core"
import PriceList from "./price-list" import PriceList from "./price-list"
type OptionalFields = DAL.SoftDeletableModelDateColumns const PriceListRule = model
.define("PriceListRule", {
const tableName = "price_list_rule" id: model.id({ prefix: "prule" }).primaryKey(),
const PriceListRuleDeletedAtIndex = createPsqlIndexStatementHelper({ attribute: model.text(),
tableName: tableName, value: model.json().nullable(),
columns: "deleted_at", price_list: model.belongsTo(() => PriceList, {
where: "deleted_at IS NOT NULL", mappedBy: "price_list_rules",
}) }),
})
const PriceListRulePriceListIdIndex = createPsqlIndexStatementHelper({ .indexes([
tableName: tableName, {
columns: "price_list_id", on: ["price_list_id"],
where: "deleted_at IS NULL", where: "deleted_at IS NULL",
}) },
])
@Entity({ tableName }) export default PriceListRule
@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",
})
price_list_id: string
@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!
}
}
+20 -106
View File
@@ -1,116 +1,30 @@
import { DAL } from "@medusajs/framework/types"
import { import {
createPsqlIndexStatementHelper, model,
DALUtils,
generateEntityId,
PriceListStatus, PriceListStatus,
PriceListType, PriceListType,
Searchable,
} from "@medusajs/framework/utils" } 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 Price from "./price"
import PriceListRule from "./price-list-rule" import PriceListRule from "./price-list-rule"
type OptionalFields = const PriceList = model
| "starts_at" .define("PriceList", {
| "ends_at" id: model.id({ prefix: "plist" }).primaryKey(),
| DAL.SoftDeletableModelDateColumns title: model.text().searchable(),
description: model.text().searchable(),
const tableName = "price_list" status: model.enum(PriceListStatus).default(PriceListStatus.DRAFT),
const PriceListDeletedAtIndex = createPsqlIndexStatementHelper({ type: model.enum(PriceListType).default(PriceListType.SALE),
tableName: tableName, starts_at: model.dateTime().nullable(),
columns: "deleted_at", ends_at: model.dateTime().nullable(),
where: "deleted_at IS NOT NULL", rules_count: model.number().default(0).nullable(),
}) prices: model.hasMany(() => Price, {
mappedBy: "price_list",
export const PriceListIdPrefix = "plist" }),
price_list_rules: model.hasMany(() => PriceListRule, {
@Entity({ tableName }) mappedBy: "price_list",
@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,
}) })
starts_at: Date | null = null .cascades({
delete: ["price_list_rules", "prices"],
@Property({
columnType: "timestamptz",
nullable: true,
}) })
ends_at: Date | null = null
@OneToMany(() => Price, (price) => price.price_list, { export default PriceList
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)
}
}
@@ -1,75 +1,19 @@
import { import { model } from "@medusajs/framework/utils"
createPsqlIndexStatementHelper,
DALUtils,
generateEntityId,
} from "@medusajs/framework/utils"
import {
BeforeCreate,
Entity,
Filter,
OnInit,
PrimaryKey,
Property,
} from "@mikro-orm/core"
export const uniquePreferenceRuleIndexName = const PricePreference = model
"IDX_price_preference_attribute_value" .define("PricePreference", {
const UniquePreferenceRuleIndexStatement = createPsqlIndexStatementHelper({ id: model.id({ prefix: "prpref" }).primaryKey(),
name: uniquePreferenceRuleIndexName, attribute: model.text(),
tableName: "price_preference", value: model.text().nullable(),
columns: ["attribute", "value"], is_tax_inclusive: model.boolean().default(false),
})
.indexes([
{
name: "IDX_price_preference_attribute_value",
on: ["attribute", "value"],
unique: true, unique: true,
where: "deleted_at IS NULL", where: "deleted_at IS NULL",
}) },
])
const DeletedAtIndex = createPsqlIndexStatementHelper({ export default PricePreference
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()",
})
created_at: Date
@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")
}
}
@@ -1,102 +1,23 @@
import { DAL, PricingRuleOperatorValues } from "@medusajs/framework/types" import { model, PricingRuleOperator } from "@medusajs/framework/utils"
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 Price from "./price" import Price from "./price"
type OptionalFields = DAL.SoftDeletableModelDateColumns const PriceRule = model
.define("PriceRule", {
const tableName = "price_rule" id: model.id({ prefix: "prule" }).primaryKey(),
const PriceRuleDeletedAtIndex = createPsqlIndexStatementHelper({ attribute: model.text(),
tableName: tableName, value: model.text(),
columns: "deleted_at", operator: model.enum(PricingRuleOperator).default(PricingRuleOperator.EQ),
where: "deleted_at IS NOT NULL", priority: model.number().default(0),
}) price: model.belongsTo(() => Price, {
mappedBy: "price_rules",
const PriceRulePriceIdIndex = createPsqlIndexStatementHelper({ }),
tableName: tableName, })
columns: ["price_id", "attribute", "operator"], .indexes([
{
on: ["price_id", "attribute", "operator"],
where: "deleted_at IS NULL", where: "deleted_at IS NULL",
unique: true, unique: true,
}) },
])
@Entity({ tableName }) export default PriceRule
@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",
})
price_id: string
@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!
}
}
@@ -1,68 +1,15 @@
import { import { model } from "@medusajs/framework/utils"
createPsqlIndexStatementHelper,
DALUtils,
generateEntityId,
} from "@medusajs/framework/utils"
import {
BeforeCreate,
Cascade,
Collection,
Entity,
Filter,
OneToMany,
OnInit,
PrimaryKey,
Property,
Rel,
} from "@mikro-orm/core"
import Price from "./price" import Price from "./price"
const tableName = "price_set" const PriceSet = model
const PriceSetDeletedAtIndex = createPsqlIndexStatementHelper({ .define("PriceSet", {
tableName: tableName, id: model.id({ prefix: "pset" }).primaryKey(),
columns: "deleted_at", prices: model.hasMany(() => Price, {
where: "deleted_at IS NOT NULL", mappedBy: "price_set",
}) }),
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],
}) })
prices = new Collection<Rel<Price>>(this) .cascades({
delete: ["prices"],
@Property({
onCreate: () => new Date(),
columnType: "timestamptz",
defaultRaw: "now()",
}) })
created_at: Date
@Property({ export default PriceSet
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)
}
}
+37 -140
View File
@@ -1,148 +1,45 @@
import { DAL } from "@medusajs/framework/types" import { model } from "@medusajs/framework/utils"
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 PriceList from "./price-list" import PriceList from "./price-list"
import PriceRule from "./price-rule" import PriceRule from "./price-rule"
import PriceSet from "./price-set" import PriceSet from "./price-set"
type OptionalFields = DAL.SoftDeletableModelDateColumns const Price = model
.define("Price", {
const tableName = "price" id: model.id({ prefix: "price" }).primaryKey(),
const PriceDeletedAtIndex = createPsqlIndexStatementHelper({ title: model.text().nullable(),
tableName: tableName, currency_code: model.text(),
columns: "deleted_at", amount: model.bigNumber(),
where: "deleted_at IS NOT NULL", min_quantity: model.number().nullable(),
}) max_quantity: model.number().nullable(),
rules_count: model.number().default(0).nullable(),
const PricePriceSetIdIndex = createPsqlIndexStatementHelper({ price_set: model.belongsTo(() => PriceSet, {
tableName: tableName, mappedBy: "prices",
columns: "price_set_id", }),
price_rules: model.hasMany(() => PriceRule, {
mappedBy: "price",
}),
price_list: model
.belongsTo(() => PriceList, {
mappedBy: "prices",
})
.nullable(),
})
.cascades({
delete: ["price_rules"],
})
.indexes([
{
on: ["price_set_id"],
where: "deleted_at IS NULL", where: "deleted_at IS NULL",
}) },
{
const PricePriceListIdIndex = createPsqlIndexStatementHelper({ on: ["price_list_id"],
tableName: tableName,
columns: "price_list_id",
where: "deleted_at IS NULL", where: "deleted_at IS NULL",
}) },
{
const PriceCurrencyCodeIndex = createPsqlIndexStatementHelper({ on: ["currency_code"],
tableName: tableName,
columns: "currency_code",
where: "deleted_at IS NULL", where: "deleted_at IS NULL",
}) },
])
@Entity({ tableName }) export default Price
@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",
})
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],
})
price_rules = new Collection<Rel<PriceRule>>(this)
@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!
}
}
@@ -7,6 +7,7 @@ import {
CreatePriceSetDTO, CreatePriceSetDTO,
DAL, DAL,
FindConfig, FindConfig,
InferEntityType,
InternalModuleDeclaration, InternalModuleDeclaration,
ModuleJoinerConfig, ModuleJoinerConfig,
ModulesSdkTypes, ModulesSdkTypes,
@@ -52,6 +53,7 @@ import {
import { ServiceTypes } from "@types" import { ServiceTypes } from "@types"
import { eventBuilders, validatePriceListDates } from "@utils" import { eventBuilders, validatePriceListDates } from "@utils"
import { joinerConfig } from "../joiner-config" import { joinerConfig } from "../joiner-config"
import { Collection } from "@mikro-orm/core"
type InjectedDependencies = { type InjectedDependencies = {
baseRepository: DAL.RepositoryService baseRepository: DAL.RepositoryService
@@ -91,12 +93,24 @@ export default class PricingModuleService
{ {
protected baseRepository_: DAL.RepositoryService protected baseRepository_: DAL.RepositoryService
protected readonly pricingRepository_: PricingRepositoryService protected readonly pricingRepository_: PricingRepositoryService
protected readonly priceSetService_: ModulesSdkTypes.IMedusaInternalService<PriceSet> protected readonly priceSetService_: ModulesSdkTypes.IMedusaInternalService<
protected readonly priceRuleService_: ModulesSdkTypes.IMedusaInternalService<PriceRule> InferEntityType<typeof PriceSet>
protected readonly priceService_: ModulesSdkTypes.IMedusaInternalService<Price> >
protected readonly priceListService_: ModulesSdkTypes.IMedusaInternalService<PriceList> protected readonly priceRuleService_: ModulesSdkTypes.IMedusaInternalService<
protected readonly priceListRuleService_: ModulesSdkTypes.IMedusaInternalService<PriceListRule> InferEntityType<typeof PriceRule>
protected readonly pricePreferenceService_: ModulesSdkTypes.IMedusaInternalService<PricePreference> >
protected readonly priceService_: ModulesSdkTypes.IMedusaInternalService<
InferEntityType<typeof Price>
>
protected readonly priceListService_: ModulesSdkTypes.IMedusaInternalService<
InferEntityType<typeof PriceList>
>
protected readonly priceListRuleService_: ModulesSdkTypes.IMedusaInternalService<
InferEntityType<typeof PriceListRule>
>
protected readonly pricePreferenceService_: ModulesSdkTypes.IMedusaInternalService<
InferEntityType<typeof PricePreference>
>
constructor( constructor(
{ {
@@ -446,7 +460,7 @@ export default class PricingModuleService
(priceSet): priceSet is CreatePriceSetDTO => !priceSet.id (priceSet): priceSet is CreatePriceSetDTO => !priceSet.id
) )
const operations: Promise<PriceSet[]>[] = [] const operations: Promise<InferEntityType<typeof PriceSet>[]>[] = []
if (forCreate.length) { if (forCreate.length) {
operations.push(this.createPriceSets_(forCreate, sharedContext)) operations.push(this.createPriceSets_(forCreate, sharedContext))
@@ -512,7 +526,7 @@ export default class PricingModuleService
protected async updatePriceSets_( protected async updatePriceSets_(
data: ServiceTypes.UpdatePriceSetInput[], data: ServiceTypes.UpdatePriceSetInput[],
@MedusaContext() sharedContext: Context = {} @MedusaContext() sharedContext: Context = {}
): Promise<PriceSet[]> { ): Promise<InferEntityType<typeof PriceSet>[]> {
// TODO: Since money IDs are rarely passed, this will delete all previous data and insert new entries. // TODO: Since money IDs are rarely passed, this will delete all previous data and insert new entries.
// We can make the `insert` inside upsertWithReplace do an `upsert` instead to avoid this // We can make the `insert` inside upsertWithReplace do an `upsert` instead to avoid this
const normalizedData = await this.normalizeUpdateData(data) const normalizedData = await this.normalizeUpdateData(data)
@@ -828,7 +842,7 @@ export default class PricingModuleService
!pricePreference.id !pricePreference.id
) )
const operations: Promise<PricePreference[]>[] = [] const operations: Promise<InferEntityType<typeof PricePreference>[]>[] = []
if (forCreate.length) { if (forCreate.length) {
operations.push(this.createPricePreferences_(forCreate, sharedContext)) operations.push(this.createPricePreferences_(forCreate, sharedContext))
@@ -1249,7 +1263,7 @@ export default class PricingModuleService
protected async updatePriceListPrices_( protected async updatePriceListPrices_(
data: PricingTypes.UpdatePriceListPricesDTO[], data: PricingTypes.UpdatePriceListPricesDTO[],
sharedContext: Context = {} sharedContext: Context = {}
): Promise<Price[]> { ): Promise<InferEntityType<typeof Price>[]> {
const priceLists = await this.listPriceLists( const priceLists = await this.listPriceLists(
{ id: data.map((p) => p.price_list_id) }, { id: data.map((p) => p.price_list_id) },
{ relations: ["prices", "prices.price_rules"] }, { relations: ["prices", "prices.price_rules"] },
@@ -1305,7 +1319,7 @@ export default class PricingModuleService
protected async addPriceListPrices_( protected async addPriceListPrices_(
data: PricingTypes.AddPriceListPricesDTO[], data: PricingTypes.AddPriceListPricesDTO[],
sharedContext: Context = {} sharedContext: Context = {}
): Promise<Price[]> { ): Promise<InferEntityType<typeof Price>[]> {
const priceLists = await this.listPriceLists( const priceLists = await this.listPriceLists(
{ id: data.map((p) => p.price_list_id) }, { id: data.map((p) => p.price_list_id) },
{ relations: ["prices", "prices.price_rules"] }, { relations: ["prices", "prices.price_rules"] },
@@ -1379,7 +1393,7 @@ export default class PricingModuleService
protected async setPriceListRules_( protected async setPriceListRules_(
data: PricingTypes.SetPriceListRulesDTO[], data: PricingTypes.SetPriceListRulesDTO[],
sharedContext: Context = {} sharedContext: Context = {}
): Promise<PriceList[]> { ): Promise<InferEntityType<typeof PriceList>[]> {
// TODO: re think this method // TODO: re think this method
const priceLists = await this.priceListService_.list( const priceLists = await this.priceListService_.list(
{ id: data.map((d) => d.price_list_id) }, { id: data.map((d) => d.price_list_id) },
@@ -1402,10 +1416,12 @@ export default class PricingModuleService
const priceListsUpsert = priceLists const priceListsUpsert = priceLists
.map((priceList) => { .map((priceList) => {
const priceListRules =
priceList.price_list_rules as unknown as Collection<
InferEntityType<typeof PriceListRule>
>
const allRules = new Map( const allRules = new Map(
priceList.price_list_rules priceListRules.toArray().map((r) => [r.attribute, r.value])
.toArray()
.map((r) => [r.attribute, r.value])
) )
const rules = rulesMap.get(priceList.id) const rules = rulesMap.get(priceList.id)
@@ -1441,7 +1457,7 @@ export default class PricingModuleService
protected async removePriceListRules_( protected async removePriceListRules_(
data: PricingTypes.RemovePriceListRulesDTO[], data: PricingTypes.RemovePriceListRulesDTO[],
sharedContext: Context = {} sharedContext: Context = {}
): Promise<PriceList[]> { ): Promise<InferEntityType<typeof PriceList>[]> {
// TODO: re think this method // TODO: re think this method
const priceLists = await this.priceListService_.list( const priceLists = await this.priceListService_.list(
{ id: data.map((d) => d.price_list_id) }, { id: data.map((d) => d.price_list_id) },
@@ -1464,10 +1480,13 @@ export default class PricingModuleService
const priceListsUpsert = priceLists const priceListsUpsert = priceLists
.map((priceList) => { .map((priceList) => {
const priceListRules =
priceList.price_list_rules as unknown as Collection<
InferEntityType<typeof PriceListRule>
>
const allRules = new Map( const allRules = new Map(
priceList.price_list_rules priceListRules.toArray().map((r) => [r.attribute, r.value])
.toArray()
.map((r) => [r.attribute, r.value])
) )
const rules = rulesMap.get(priceList.id) const rules = rulesMap.get(priceList.id)
@@ -1536,8 +1555,8 @@ export default class PricingModuleService
} }
const isTaxInclusive = ( const isTaxInclusive = (
priceRules: PriceRule[], priceRules: InferEntityType<typeof PriceRule>[],
preferences: PricePreference[], preferences: InferEntityType<typeof PricePreference>[],
currencyCode: string, currencyCode: string,
regionId?: string regionId?: string
) => { ) => {