diff --git a/integration-tests/http/__tests__/price-list/admin/price-list.spec.ts b/integration-tests/http/__tests__/price-list/admin/price-list.spec.ts index 615ee5459549d..341a902b5e869 100644 --- a/integration-tests/http/__tests__/price-list/admin/price-list.spec.ts +++ b/integration-tests/http/__tests__/price-list/admin/price-list.spec.ts @@ -183,8 +183,8 @@ medusaIntegrationTestRunner({ amount: 100, currency_code: "usd", // BREAKING: Min and max quantity are returned as string - min_quantity: "1", - max_quantity: "100", + min_quantity: 1, + max_quantity: 100, variant_id: product1.variants[0].id, created_at: expect.any(String), updated_at: expect.any(String), @@ -194,8 +194,8 @@ medusaIntegrationTestRunner({ id: expect.any(String), amount: 80, currency_code: "usd", - min_quantity: "101", - max_quantity: "500", + min_quantity: 101, + max_quantity: 500, variant_id: product1.variants[0].id, created_at: expect.any(String), updated_at: expect.any(String), @@ -360,15 +360,15 @@ medusaIntegrationTestRunner({ amount: 100, currency_code: "usd", id: expect.any(String), - max_quantity: "100", - min_quantity: "1", + max_quantity: 100, + min_quantity: 1, }), expect.objectContaining({ amount: 80, currency_code: "usd", id: expect.any(String), - max_quantity: "500", - min_quantity: "101", + max_quantity: 500, + min_quantity: 101, }), ]), rules: { @@ -409,15 +409,15 @@ medusaIntegrationTestRunner({ amount: 100, currency_code: "usd", id: expect.any(String), - max_quantity: "100", - min_quantity: "1", + max_quantity: 100, + min_quantity: 1, }), expect.objectContaining({ amount: 250, currency_code: "eur", id: expect.any(String), - max_quantity: "500", - min_quantity: "101", + max_quantity: 500, + min_quantity: 101, }), ]) }) @@ -470,16 +470,16 @@ medusaIntegrationTestRunner({ id: expect.any(String), amount: 100, currency_code: "usd", - min_quantity: "1", - max_quantity: "100", + min_quantity: 1, + max_quantity: 100, variant_id: product1.variants[0].id, }), expect.objectContaining({ id: expect.any(String), amount: 80, currency_code: "usd", - min_quantity: "101", - max_quantity: "500", + min_quantity: 101, + max_quantity: 500, variant_id: product1.variants[0].id, }), expect.objectContaining({ @@ -487,24 +487,24 @@ medusaIntegrationTestRunner({ amount: 45, currency_code: "usd", variant_id: product1.variants[0].id, - min_quantity: "1001", - max_quantity: "2000", + min_quantity: 1001, + max_quantity: 2000, }), expect.objectContaining({ id: expect.any(String), amount: 35, currency_code: "usd", variant_id: product1.variants[0].id, - min_quantity: "2001", - max_quantity: "3000", + min_quantity: 2001, + max_quantity: 3000, }), expect.objectContaining({ id: expect.any(String), amount: 25, currency_code: "usd", variant_id: product1.variants[0].id, - min_quantity: "3001", - max_quantity: "4000", + min_quantity: 3001, + max_quantity: 4000, }), ]) ) @@ -551,16 +551,16 @@ medusaIntegrationTestRunner({ id: expect.any(String), amount: 45, currency_code: "usd", - min_quantity: "1", - max_quantity: "100", + min_quantity: 1, + max_quantity: 100, variant_id: product1.variants[0].id, }), expect.objectContaining({ id: expect.any(String), amount: 35, currency_code: "usd", - min_quantity: "101", - max_quantity: "500", + min_quantity: 101, + max_quantity: 500, variant_id: product1.variants[0].id, }), ]) @@ -605,16 +605,16 @@ medusaIntegrationTestRunner({ id: expect.any(String), amount: 100, currency_code: "usd", - min_quantity: "1", - max_quantity: "100", + min_quantity: 1, + max_quantity: 100, variant_id: product1.variants[0].id, }), expect.objectContaining({ id: expect.any(String), amount: 80, currency_code: "usd", - min_quantity: "101", - max_quantity: "500", + min_quantity: 101, + max_quantity: 500, variant_id: product1.variants[0].id, }), expect.objectContaining({ @@ -698,8 +698,8 @@ medusaIntegrationTestRunner({ expect.objectContaining({ amount: 80, currency_code: "usd", - min_quantity: "101", - max_quantity: "500", + min_quantity: 101, + max_quantity: 500, variant_id: product1.variants[0].id, }), ]) diff --git a/integration-tests/http/__tests__/shipping-option/admin/shipping-option.spec.ts b/integration-tests/http/__tests__/shipping-option/admin/shipping-option.spec.ts index 47320a5865857..19644d9e8de89 100644 --- a/integration-tests/http/__tests__/shipping-option/admin/shipping-option.spec.ts +++ b/integration-tests/http/__tests__/shipping-option/admin/shipping-option.spec.ts @@ -191,6 +191,7 @@ medusaIntegrationTestRunner({ max_quantity: null, min_quantity: null, price_list: null, + price_list_id: null, price_set_id: expect.any(String), raw_amount: { precision: 20, @@ -210,6 +211,7 @@ medusaIntegrationTestRunner({ max_quantity: null, min_quantity: null, price_list: null, + price_list_id: null, price_set_id: expect.any(String), raw_amount: { precision: 20, diff --git a/packages/modules/pricing/integration-tests/__fixtures__/price-list-rules/index.ts b/packages/modules/pricing/integration-tests/__fixtures__/price-list-rules/index.ts index 07af1818cdea0..9f747f26c547a 100644 --- a/packages/modules/pricing/integration-tests/__fixtures__/price-list-rules/index.ts +++ b/packages/modules/pricing/integration-tests/__fixtures__/price-list-rules/index.ts @@ -1,5 +1,6 @@ import { SqlEntityManager } from "@mikro-orm/postgresql" import { PriceListRule } from "@models" +import { toMikroORMEntity } from "@medusajs/framework/utils" import { defaultPriceListRuleData } from "./data" export * from "./data" @@ -11,7 +12,7 @@ export async function createPriceListRules( const priceListRules: PriceListRule[] = [] for (let data of priceListRuleData) { - const plr = manager.create(PriceListRule, data) + const plr = manager.create(toMikroORMEntity(PriceListRule), data) priceListRules.push(plr) } diff --git a/packages/modules/pricing/integration-tests/__fixtures__/price-list/index.ts b/packages/modules/pricing/integration-tests/__fixtures__/price-list/index.ts index cf28eda46c0a4..d105eaafa9d74 100644 --- a/packages/modules/pricing/integration-tests/__fixtures__/price-list/index.ts +++ b/packages/modules/pricing/integration-tests/__fixtures__/price-list/index.ts @@ -1,5 +1,6 @@ import { SqlEntityManager } from "@mikro-orm/postgresql" import { PriceList } from "@models" +import { toMikroORMEntity } from "@medusajs/framework/utils" import { defaultPriceListData } from "./data" export * from "./data" @@ -11,7 +12,7 @@ export async function createPriceLists( const priceLists: PriceList[] = [] for (let data of priceListData) { - const pl = manager.create(PriceList, data) + const pl = manager.create(toMikroORMEntity(PriceList), data) priceLists.push(pl) } diff --git a/packages/modules/pricing/integration-tests/__fixtures__/price-rule/index.ts b/packages/modules/pricing/integration-tests/__fixtures__/price-rule/index.ts index 2aa5927088479..4aa2457bd24ca 100644 --- a/packages/modules/pricing/integration-tests/__fixtures__/price-rule/index.ts +++ b/packages/modules/pricing/integration-tests/__fixtures__/price-rule/index.ts @@ -2,6 +2,7 @@ import { PriceRule } from "@models" import { CreatePriceRuleDTO } from "@medusajs/framework/types" import { SqlEntityManager } from "@mikro-orm/postgresql" +import { toMikroORMEntity } from "@medusajs/framework/utils" import { defaultPriceRuleData } from "./data" export * from "./data" @@ -20,7 +21,10 @@ export async function createPriceRules( priceRuleDataClone.attribute = priceRuleDataClone.attribute priceRuleDataClone.price_id = priceRuleDataClone.price_id - const priceRule = manager.create(PriceRule, priceRuleDataClone) + const priceRule = manager.create( + toMikroORMEntity(PriceRule), + priceRuleDataClone + ) priceRules.push(priceRule) } diff --git a/packages/modules/pricing/integration-tests/__fixtures__/price-set/index.ts b/packages/modules/pricing/integration-tests/__fixtures__/price-set/index.ts index 688055c5134ce..25fa848374dda 100644 --- a/packages/modules/pricing/integration-tests/__fixtures__/price-set/index.ts +++ b/packages/modules/pricing/integration-tests/__fixtures__/price-set/index.ts @@ -1,6 +1,7 @@ import { CreatePriceSetDTO } from "@medusajs/framework/types" import { SqlEntityManager } from "@mikro-orm/postgresql" import { Price, PriceSet } from "@models" +import { toMikroORMEntity } from "@medusajs/framework/utils" import { defaultPriceSetsData } from "./data" export * from "./data" @@ -16,12 +17,15 @@ export async function createPriceSets( const prices = 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) for (let priceData of prices) { - const price = manager.create(Price, { + const price = manager.create(toMikroORMEntity(Price), { ...priceData, price_set_id: priceSet.id, title: "test", diff --git a/packages/modules/pricing/integration-tests/__fixtures__/price/index.ts b/packages/modules/pricing/integration-tests/__fixtures__/price/index.ts index 37ddebc1fc945..a5a9a43715d71 100644 --- a/packages/modules/pricing/integration-tests/__fixtures__/price/index.ts +++ b/packages/modules/pricing/integration-tests/__fixtures__/price/index.ts @@ -1,6 +1,7 @@ -import { SqlEntityManager } from "@mikro-orm/postgresql" import { Price } from "@models" import { defaultPricesData } from "./data" +import { SqlEntityManager } from "@mikro-orm/postgresql" +import { toMikroORMEntity } from "@medusajs/framework/utils" export * from "./data" @@ -11,7 +12,7 @@ export async function createPrices( const prices: Price[] = [] for (let data of pricesData) { - const price = manager.create(Price, data) + const price = manager.create(toMikroORMEntity(Price), data) prices.push(price) } diff --git a/packages/modules/pricing/integration-tests/__tests__/services/pricing-module/price-list.spec.ts b/packages/modules/pricing/integration-tests/__tests__/services/pricing-module/price-list.spec.ts index 378e9836a8b3c..8b3ecf89dd9b5 100644 --- a/packages/modules/pricing/integration-tests/__tests__/services/pricing-module/price-list.spec.ts +++ b/packages/modules/pricing/integration-tests/__tests__/services/pricing-module/price-list.spec.ts @@ -254,11 +254,14 @@ moduleIntegrationTestRunner({ ends_at: "10/20/2030", }, ]) - expect(priceList).toEqual( - expect.objectContaining({ - starts_at: new Date("10/10/2010").toISOString(), - ends_at: new Date("10/20/2030").toISOString(), - }) + + expect(priceList).toHaveProperty("starts_at") + expect(priceList).toHaveProperty("ends_at") + 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({ ends_at: "10/20/2030", }, ]) - expect(priceList).toEqual( - expect.objectContaining({ - starts_at: new Date("10/10/2010").toISOString(), - ends_at: new Date("10/20/2030").toISOString(), - }) + + expect(priceList).toHaveProperty("starts_at") + expect(priceList).toHaveProperty("ends_at") + 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({ currency_code: "EUR", }), expect.objectContaining({ - rules_count: 0, price_rules: [], amount: 600, currency_code: "EUR", diff --git a/packages/modules/pricing/integration-tests/__tests__/services/pricing-module/price-rule.spec.ts b/packages/modules/pricing/integration-tests/__tests__/services/pricing-module/price-rule.spec.ts index 73c7056e10ee7..f931ee992d348 100644 --- a/packages/modules/pricing/integration-tests/__tests__/services/pricing-module/price-rule.spec.ts +++ b/packages/modules/pricing/integration-tests/__tests__/services/pricing-module/price-rule.spec.ts @@ -5,7 +5,7 @@ import { Price } from "../../../../src/models" import { createPrices } from "../../../__fixtures__/price" import { createPriceRules } from "../../../__fixtures__/price-rule" import { createPriceSets } from "../../../__fixtures__/price-set" -import { Modules } from "@medusajs/framework/utils" +import { Modules, toMikroORMEntity } from "@medusajs/framework/utils" jest.setTimeout(30000) @@ -268,13 +268,13 @@ moduleIntegrationTestRunner({ }) it("should create a PriceRule successfully", async () => { - const price: Price = testManager.create(Price, { + const price = testManager.create(toMikroORMEntity(Price), { currency_code: "EUR", amount: 100, price_set_id: "price-set-1", title: "test", rules_count: 0, - } as Price) + }) await testManager.persist(price).flush() diff --git a/packages/modules/pricing/src/migrations/.snapshot-medusa-pricing.json b/packages/modules/pricing/src/migrations/.snapshot-medusa-pricing.json index fb6ac7c677c80..0901fddaa276e 100644 --- a/packages/modules/pricing/src/migrations/.snapshot-medusa-pricing.json +++ b/packages/modules/pricing/src/migrations/.snapshot-medusa-pricing.json @@ -1,5 +1,7 @@ { - "namespaces": ["public"], + "namespaces": [ + "public" + ], "name": "public", "tables": [ { @@ -39,7 +41,10 @@ "primary": false, "nullable": false, "default": "'draft'", - "enumItems": ["active", "draft"], + "enumItems": [ + "active", + "draft" + ], "mappedType": "enum" }, "type": { @@ -50,7 +55,10 @@ "primary": false, "nullable": false, "default": "'sale'", - "enumItems": ["sale", "override"], + "enumItems": [ + "sale", + "override" + ], "mappedType": "enum" }, "starts_at": { @@ -79,7 +87,7 @@ "unsigned": false, "autoincrement": false, "primary": false, - "nullable": false, + "nullable": true, "default": "0", "mappedType": "integer" }, @@ -121,15 +129,17 @@ "indexes": [ { "keyName": "IDX_price_list_deleted_at", - "columnNames": ["deleted_at"], + "columnNames": [], "composite": false, "primary": 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", - "columnNames": ["id"], + "columnNames": [ + "id" + ], "composite": false, "primary": true, "unique": true @@ -214,7 +224,7 @@ "indexes": [ { "keyName": "IDX_price_list_rule_price_list_id", - "columnNames": ["price_list_id"], + "columnNames": [], "composite": false, "primary": false, "unique": false, @@ -222,15 +232,17 @@ }, { "keyName": "IDX_price_list_rule_deleted_at", - "columnNames": ["deleted_at"], + "columnNames": [], "composite": false, "primary": 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", - "columnNames": ["id"], + "columnNames": [ + "id" + ], "composite": false, "primary": true, "unique": true @@ -240,9 +252,13 @@ "foreignKeys": { "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", - "referencedColumnNames": ["id"], + "referencedColumnNames": [ + "id" + ], "referencedTableName": "public.price_list", "deleteRule": "cascade", "updateRule": "cascade" @@ -326,11 +342,11 @@ "indexes": [ { "keyName": "IDX_price_preference_deleted_at", - "columnNames": ["deleted_at"], + "columnNames": [], "composite": false, "primary": 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", @@ -342,7 +358,9 @@ }, { "keyName": "price_preference_pkey", - "columnNames": ["id"], + "columnNames": [ + "id" + ], "composite": false, "primary": true, "unique": true @@ -400,15 +418,17 @@ "indexes": [ { "keyName": "IDX_price_set_deleted_at", - "columnNames": ["deleted_at"], + "columnNames": [], "composite": false, "primary": 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", - "columnNames": ["id"], + "columnNames": [ + "id" + ], "composite": false, "primary": true, "unique": true @@ -455,41 +475,23 @@ "nullable": false, "mappedType": "decimal" }, - "raw_amount": { - "name": "raw_amount", - "type": "jsonb", - "unsigned": false, - "autoincrement": false, - "primary": false, - "nullable": false, - "mappedType": "json" - }, "min_quantity": { "name": "min_quantity", - "type": "numeric", + "type": "integer", "unsigned": false, "autoincrement": false, "primary": false, "nullable": true, - "mappedType": "decimal" + "mappedType": "integer" }, "max_quantity": { "name": "max_quantity", - "type": "numeric", + "type": "integer", "unsigned": false, "autoincrement": false, "primary": false, "nullable": true, - "mappedType": "decimal" - }, - "price_set_id": { - "name": "price_set_id", - "type": "text", - "unsigned": false, - "autoincrement": false, - "primary": false, - "nullable": false, - "mappedType": "text" + "mappedType": "integer" }, "rules_count": { "name": "rules_count", @@ -497,10 +499,19 @@ "unsigned": false, "autoincrement": false, "primary": false, - "nullable": false, + "nullable": true, "default": "0", "mappedType": "integer" }, + "price_set_id": { + "name": "price_set_id", + "type": "text", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": false, + "mappedType": "text" + }, "price_list_id": { "name": "price_list_id", "type": "text", @@ -510,6 +521,15 @@ "nullable": true, "mappedType": "text" }, + "raw_amount": { + "name": "raw_amount", + "type": "jsonb", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": false, + "mappedType": "json" + }, "created_at": { "name": "created_at", "type": "timestamptz", @@ -546,17 +566,9 @@ "name": "price", "schema": "public", "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", - "columnNames": ["price_set_id"], + "columnNames": [], "composite": false, "primary": false, "unique": false, @@ -564,7 +576,7 @@ }, { "keyName": "IDX_price_price_list_id", - "columnNames": ["price_list_id"], + "columnNames": [], "composite": false, "primary": false, "unique": false, @@ -572,15 +584,25 @@ }, { "keyName": "IDX_price_deleted_at", - "columnNames": ["deleted_at"], + "columnNames": [], + "composite": false, + "primary": false, + "unique": false, + "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_deleted_at\" ON \"price\" (deleted_at) WHERE deleted_at IS NOT NULL" + "expression": "CREATE INDEX IF NOT EXISTS \"IDX_price_currency_code\" ON \"price\" (currency_code) WHERE deleted_at IS NULL" }, { "keyName": "price_pkey", - "columnNames": ["id"], + "columnNames": [ + "id" + ], "composite": false, "primary": true, "unique": true @@ -590,18 +612,26 @@ "foreignKeys": { "price_price_set_id_foreign": { "constraintName": "price_price_set_id_foreign", - "columnNames": ["price_set_id"], + "columnNames": [ + "price_set_id" + ], "localTableName": "public.price", - "referencedColumnNames": ["id"], + "referencedColumnNames": [ + "id" + ], "referencedTableName": "public.price_set", "deleteRule": "cascade", "updateRule": "cascade" }, "price_price_list_id_foreign": { "constraintName": "price_price_list_id_foreign", - "columnNames": ["price_list_id"], + "columnNames": [ + "price_list_id" + ], "localTableName": "public.price", - "referencedColumnNames": ["id"], + "referencedColumnNames": [ + "id" + ], "referencedTableName": "public.price_list", "deleteRule": "cascade", "updateRule": "cascade" @@ -628,25 +658,31 @@ "nullable": false, "mappedType": "text" }, - "operator": { - "name": "operator", + "value": { + "name": "value", "type": "text", "unsigned": false, "autoincrement": false, "primary": false, "nullable": false, - "default": "'eq'", - "enumItems": ["gte", "lte", "gt", "lt", "eq"], - "mappedType": "enum" + "mappedType": "text" }, - "value": { - "name": "value", + "operator": { + "name": "operator", "type": "text", "unsigned": false, "autoincrement": false, "primary": false, "nullable": false, - "mappedType": "text" + "default": "'eq'", + "enumItems": [ + "gte", + "lte", + "gt", + "lt", + "eq" + ], + "mappedType": "enum" }, "priority": { "name": "priority", @@ -704,31 +740,34 @@ "schema": "public", "indexes": [ { - "columnNames": ["operator"], + "keyName": "IDX_price_rule_price_id", + "columnNames": [], "composite": false, - "keyName": "IDX_price_rule_operator", "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_price_id_attribute_operator_unique", - "columnNames": ["price_id"], + "keyName": "IDX_price_rule_deleted_at", + "columnNames": [], "composite": false, "primary": 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 INDEX IF NOT EXISTS \"IDX_price_rule_deleted_at\" ON \"price_rule\" (deleted_at) WHERE deleted_at IS NULL" }, { - "keyName": "IDX_price_rule_deleted_at", - "columnNames": ["deleted_at"], + "keyName": "IDX_price_rule_price_id_attribute_operator_unique", + "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 NOT 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": "price_rule_pkey", - "columnNames": ["id"], + "columnNames": [ + "id" + ], "composite": false, "primary": true, "unique": true @@ -738,9 +777,13 @@ "foreignKeys": { "price_rule_price_id_foreign": { "constraintName": "price_rule_price_id_foreign", - "columnNames": ["price_id"], + "columnNames": [ + "price_id" + ], "localTableName": "public.price_rule", - "referencedColumnNames": ["id"], + "referencedColumnNames": [ + "id" + ], "referencedTableName": "public.price", "deleteRule": "cascade", "updateRule": "cascade" diff --git a/packages/modules/pricing/src/migrations/Migration20241128055359.ts b/packages/modules/pricing/src/migrations/Migration20241128055359.ts new file mode 100644 index 0000000000000..c9eeb8e874b38 --- /dev/null +++ b/packages/modules/pricing/src/migrations/Migration20241128055359.ts @@ -0,0 +1,29 @@ +import { Migration } from '@mikro-orm/migrations'; + +export class Migration20241128055359 extends Migration { + + async up(): Promise { + 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 { + 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";'); + } + +} diff --git a/packages/modules/pricing/src/models/price-list-rule.ts b/packages/modules/pricing/src/models/price-list-rule.ts index 2ef67341a1279..d498a0f93535a 100644 --- a/packages/modules/pricing/src/models/price-list-rule.ts +++ b/packages/modules/pricing/src/models/price-list-rule.ts @@ -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 - - @ManyToOne(() => PriceList, { persist: false }) - price_list: Rel - - @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! - } -} + .indexes([ + { + on: ["price_list_id"], + where: "deleted_at IS NULL", + }, + ]) + +export default PriceListRule diff --git a/packages/modules/pricing/src/models/price-list.ts b/packages/modules/pricing/src/models/price-list.ts index b1635aaa28145..cff5983b47347 100644 --- a/packages/modules/pricing/src/models/price-list.ts +++ b/packages/modules/pricing/src/models/price-list.ts @@ -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, - }) - starts_at: Date | null = null - - @Property({ - columnType: "timestamptz", - nullable: true, - }) - ends_at: Date | null = null - - @OneToMany(() => Price, (price) => price.price_list, { - cascade: [Cascade.PERSIST, "soft-remove" as Cascade], +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", + }), }) - prices = new Collection>(this) - - @OneToMany(() => PriceListRule, (pr) => pr.price_list, { - cascade: [Cascade.PERSIST, "soft-remove" as Cascade], + .cascades({ + delete: ["price_list_rules", "prices"], }) - price_list_rules = new Collection>(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 diff --git a/packages/modules/pricing/src/models/price-preference.ts b/packages/modules/pricing/src/models/price-preference.ts index 6625ef5db52c0..ffb7539d250d9 100644 --- a/packages/modules/pricing/src/models/price-preference.ts +++ b/packages/modules/pricing/src/models/price-preference.ts @@ -1,75 +1,19 @@ -import { - createPsqlIndexStatementHelper, - DALUtils, - generateEntityId, -} from "@medusajs/framework/utils" -import { - BeforeCreate, - Entity, - Filter, - OnInit, - PrimaryKey, - Property, -} from "@mikro-orm/core" - -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()", - }) - created_at: Date - - @Property({ - onCreate: () => new Date(), - onUpdate: () => new Date(), - columnType: "timestamptz", - defaultRaw: "now()", +import { model } from "@medusajs/framework/utils" + +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), }) - 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") - } -} + .indexes([ + { + name: "IDX_price_preference_attribute_value", + on: ["attribute", "value"], + unique: true, + where: "deleted_at IS NULL", + }, + ]) + +export default PricePreference diff --git a/packages/modules/pricing/src/models/price-rule.ts b/packages/modules/pricing/src/models/price-rule.ts index 76955bd275c55..480f285a59a29 100644 --- a/packages/modules/pricing/src/models/price-rule.ts +++ b/packages/modules/pricing/src/models/price-rule.ts @@ -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 - - @ManyToOne(() => Price, { persist: false }) - price: Rel - - @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! - } -} + .indexes([ + { + on: ["price_id", "attribute", "operator"], + where: "deleted_at IS NULL", + unique: true, + }, + ]) + +export default PriceRule diff --git a/packages/modules/pricing/src/models/price-set.ts b/packages/modules/pricing/src/models/price-set.ts index 2c5f9d50ed0d1..2d8cd5afc5f02 100644 --- a/packages/modules/pricing/src/models/price-set.ts +++ b/packages/modules/pricing/src/models/price-set.ts @@ -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], - }) - prices = new Collection>(this) - - @Property({ - onCreate: () => new Date(), - columnType: "timestamptz", - defaultRaw: "now()", +const PriceSet = model + .define("PriceSet", { + id: model.id({ prefix: "pset" }).primaryKey(), + prices: model.hasMany(() => Price, { + mappedBy: "price_set", + }), }) - created_at: Date - - @Property({ - onCreate: () => new Date(), - onUpdate: () => new Date(), - columnType: "timestamptz", - defaultRaw: "now()", + .cascades({ + delete: ["prices"], }) - 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 diff --git a/packages/modules/pricing/src/models/price.ts b/packages/modules/pricing/src/models/price.ts index ef243b7fb99d2..e445cb5a595d0 100644 --- a/packages/modules/pricing/src/models/price.ts +++ b/packages/modules/pricing/src/models/price.ts @@ -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 - - @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 - - @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>(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 | 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! - } -} + .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", + }, + ]) + +export default Price diff --git a/packages/modules/pricing/src/services/pricing-module.ts b/packages/modules/pricing/src/services/pricing-module.ts index e7fa9824014bc..cf0d3eabab05d 100644 --- a/packages/modules/pricing/src/services/pricing-module.ts +++ b/packages/modules/pricing/src/services/pricing-module.ts @@ -7,6 +7,7 @@ import { CreatePriceSetDTO, DAL, FindConfig, + InferEntityType, InternalModuleDeclaration, ModuleJoinerConfig, ModulesSdkTypes, @@ -52,6 +53,7 @@ import { import { ServiceTypes } from "@types" import { eventBuilders, validatePriceListDates } from "@utils" import { joinerConfig } from "../joiner-config" +import { Collection } from "@mikro-orm/core" type InjectedDependencies = { baseRepository: DAL.RepositoryService @@ -91,12 +93,24 @@ export default class PricingModuleService { protected baseRepository_: DAL.RepositoryService protected readonly pricingRepository_: PricingRepositoryService - protected readonly priceSetService_: ModulesSdkTypes.IMedusaInternalService - protected readonly priceRuleService_: ModulesSdkTypes.IMedusaInternalService - protected readonly priceService_: ModulesSdkTypes.IMedusaInternalService - protected readonly priceListService_: ModulesSdkTypes.IMedusaInternalService - protected readonly priceListRuleService_: ModulesSdkTypes.IMedusaInternalService - protected readonly pricePreferenceService_: ModulesSdkTypes.IMedusaInternalService + protected readonly priceSetService_: ModulesSdkTypes.IMedusaInternalService< + InferEntityType + > + protected readonly priceRuleService_: ModulesSdkTypes.IMedusaInternalService< + InferEntityType + > + protected readonly priceService_: ModulesSdkTypes.IMedusaInternalService< + InferEntityType + > + protected readonly priceListService_: ModulesSdkTypes.IMedusaInternalService< + InferEntityType + > + protected readonly priceListRuleService_: ModulesSdkTypes.IMedusaInternalService< + InferEntityType + > + protected readonly pricePreferenceService_: ModulesSdkTypes.IMedusaInternalService< + InferEntityType + > constructor( { @@ -446,7 +460,7 @@ export default class PricingModuleService (priceSet): priceSet is CreatePriceSetDTO => !priceSet.id ) - const operations: Promise[] = [] + const operations: Promise[]>[] = [] if (forCreate.length) { operations.push(this.createPriceSets_(forCreate, sharedContext)) @@ -512,7 +526,7 @@ export default class PricingModuleService protected async updatePriceSets_( data: ServiceTypes.UpdatePriceSetInput[], @MedusaContext() sharedContext: Context = {} - ): Promise { + ): Promise[]> { // 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 const normalizedData = await this.normalizeUpdateData(data) @@ -828,7 +842,7 @@ export default class PricingModuleService !pricePreference.id ) - const operations: Promise[] = [] + const operations: Promise[]>[] = [] if (forCreate.length) { operations.push(this.createPricePreferences_(forCreate, sharedContext)) @@ -1249,7 +1263,7 @@ export default class PricingModuleService protected async updatePriceListPrices_( data: PricingTypes.UpdatePriceListPricesDTO[], sharedContext: Context = {} - ): Promise { + ): Promise[]> { const priceLists = await this.listPriceLists( { id: data.map((p) => p.price_list_id) }, { relations: ["prices", "prices.price_rules"] }, @@ -1305,7 +1319,7 @@ export default class PricingModuleService protected async addPriceListPrices_( data: PricingTypes.AddPriceListPricesDTO[], sharedContext: Context = {} - ): Promise { + ): Promise[]> { const priceLists = await this.listPriceLists( { id: data.map((p) => p.price_list_id) }, { relations: ["prices", "prices.price_rules"] }, @@ -1379,7 +1393,7 @@ export default class PricingModuleService protected async setPriceListRules_( data: PricingTypes.SetPriceListRulesDTO[], sharedContext: Context = {} - ): Promise { + ): Promise[]> { // TODO: re think this method const priceLists = await this.priceListService_.list( { id: data.map((d) => d.price_list_id) }, @@ -1402,10 +1416,12 @@ export default class PricingModuleService const priceListsUpsert = priceLists .map((priceList) => { + const priceListRules = + priceList.price_list_rules as unknown as Collection< + InferEntityType + > const allRules = new Map( - priceList.price_list_rules - .toArray() - .map((r) => [r.attribute, r.value]) + priceListRules.toArray().map((r) => [r.attribute, r.value]) ) const rules = rulesMap.get(priceList.id) @@ -1441,7 +1457,7 @@ export default class PricingModuleService protected async removePriceListRules_( data: PricingTypes.RemovePriceListRulesDTO[], sharedContext: Context = {} - ): Promise { + ): Promise[]> { // TODO: re think this method const priceLists = await this.priceListService_.list( { id: data.map((d) => d.price_list_id) }, @@ -1464,10 +1480,13 @@ export default class PricingModuleService const priceListsUpsert = priceLists .map((priceList) => { + const priceListRules = + priceList.price_list_rules as unknown as Collection< + InferEntityType + > + const allRules = new Map( - priceList.price_list_rules - .toArray() - .map((r) => [r.attribute, r.value]) + priceListRules.toArray().map((r) => [r.attribute, r.value]) ) const rules = rulesMap.get(priceList.id) @@ -1536,8 +1555,8 @@ export default class PricingModuleService } const isTaxInclusive = ( - priceRules: PriceRule[], - preferences: PricePreference[], + priceRules: InferEntityType[], + preferences: InferEntityType[], currencyCode: string, regionId?: string ) => {