From 20243e22eed917cfd2d5c2d6d82a98584aa28ed3 Mon Sep 17 00:00:00 2001 From: Riqwan Thamir Date: Wed, 20 Mar 2024 17:03:17 +0100 Subject: [PATCH] feat(pricing): pricing models are made soft deletable (#6732) what: - pricing models are made soft deletable - adds missing timestamp attributes - removes unwanted relationships + cascade cleanup --- .../helpers/create-variant-price-set.ts | 2 +- .../price-list/steps/delete-price-lists.ts | 6 +- .../steps/remove-price-list-prices.ts | 9 +- .../src/api-v2/admin/products/helpers.ts | 14 +- .../medusa/src/api-v2/admin/products/route.ts | 5 +- .../price-set-money-amount-rules/data.ts | 20 - .../price-set-money-amount-rules/index.ts | 22 - .../__fixtures__/seed-price-data.ts | 9 - .../pricing-module/calculate-price.spec.ts | 1 - .../pricing-module/money-amount.spec.ts | 16 +- .../pricing-module/price-rule.spec.ts | 7 +- .../price-set-money-amount-rules.spec.ts | 278 -------- .../services/pricing-module/price-set.spec.ts | 109 ++-- .../migrations/.snapshot-medusa-pricing.json | 605 +++++++++--------- .../src/migrations/Migration20230929122253.ts | 202 ++++-- .../src/migrations/Migration20231101232834.ts | 120 ---- .../src/migrations/Migration20240103140327.ts | 55 -- packages/pricing/src/models/index.ts | 1 - packages/pricing/src/models/money-amount.ts | 72 +-- .../src/models/price-list-rule-value.ts | 51 +- .../pricing/src/models/price-list-rule.ts | 76 ++- packages/pricing/src/models/price-list.ts | 46 +- packages/pricing/src/models/price-rule.ts | 69 +- .../models/price-set-money-amount-rules.ts | 67 +- .../src/models/price-set-money-amount.ts | 93 +-- .../pricing/src/models/price-set-rule-type.ts | 61 +- packages/pricing/src/models/price-set.ts | 56 +- packages/pricing/src/models/rule-type.ts | 30 +- .../pricing/src/services/pricing-module.ts | 89 +-- .../pricing/src/types/repositories/index.ts | 7 +- .../price-set-money-amount-rules.ts | 12 - packages/pricing/src/types/services/index.ts | 7 +- .../services/price-set-money-amount-rules.ts | 33 - packages/types/src/pricing/common/index.ts | 3 +- .../common/price-set-money-amount-rules.ts | 100 --- packages/types/src/pricing/service.ts | 382 +---------- 36 files changed, 1006 insertions(+), 1729 deletions(-) delete mode 100644 packages/pricing/integration-tests/__fixtures__/price-set-money-amount-rules/data.ts delete mode 100644 packages/pricing/integration-tests/__fixtures__/price-set-money-amount-rules/index.ts delete mode 100644 packages/pricing/integration-tests/__tests__/services/pricing-module/price-set-money-amount-rules.spec.ts delete mode 100644 packages/pricing/src/migrations/Migration20231101232834.ts delete mode 100644 packages/pricing/src/migrations/Migration20240103140327.ts delete mode 100644 packages/pricing/src/types/repositories/price-set-money-amount-rules.ts delete mode 100644 packages/pricing/src/types/services/price-set-money-amount-rules.ts delete mode 100644 packages/types/src/pricing/common/price-set-money-amount-rules.ts diff --git a/integration-tests/modules/helpers/create-variant-price-set.ts b/integration-tests/modules/helpers/create-variant-price-set.ts index 1aef64c4b2..c8259dd64e 100644 --- a/integration-tests/modules/helpers/create-variant-price-set.ts +++ b/integration-tests/modules/helpers/create-variant-price-set.ts @@ -45,6 +45,6 @@ export const createVariantPriceSet = async ({ }) return await pricingModuleService.retrieve(priceSet.id, { - relations: ["money_amounts"], + relations: ["price_set_money_amounts.money_amount"], }) } diff --git a/packages/core-flows/src/price-list/steps/delete-price-lists.ts b/packages/core-flows/src/price-list/steps/delete-price-lists.ts index f91974e6ec..923f1d570e 100644 --- a/packages/core-flows/src/price-list/steps/delete-price-lists.ts +++ b/packages/core-flows/src/price-list/steps/delete-price-lists.ts @@ -10,8 +10,7 @@ export const deletePriceListsStep = createStep( ModuleRegistrationName.PRICING ) - // TODO: Implement soft delete price lists - await pricingModule.deletePriceLists(ids) + await pricingModule.softDeletePriceLists(ids) return new StepResponse(void 0, ids) }, @@ -24,7 +23,6 @@ export const deletePriceListsStep = createStep( ModuleRegistrationName.PRICING ) - // TODO: Implement restore price lists - // await pricingModule.restorePriceLists(idsToRestore) + await pricingModule.restorePriceLists(idsToRestore) } ) diff --git a/packages/core-flows/src/price-list/steps/remove-price-list-prices.ts b/packages/core-flows/src/price-list/steps/remove-price-list-prices.ts index c441cea082..89bf6f6f32 100644 --- a/packages/core-flows/src/price-list/steps/remove-price-list-prices.ts +++ b/packages/core-flows/src/price-list/steps/remove-price-list-prices.ts @@ -19,7 +19,9 @@ export const removePriceListPricesStep = createStep( { relations: ["price_list"] } ) - await pricingModule.removePrices(psmas.map((psma) => psma.id)) + await pricingModule.softDeletePriceSetMoneyAmounts( + psmas.map((psma) => psma.id) + ) return new StepResponse( null, @@ -27,7 +29,7 @@ export const removePriceListPricesStep = createStep( ) }, async (ids, { container }) => { - if (!ids) { + if (!ids?.length) { return } @@ -35,7 +37,6 @@ export const removePriceListPricesStep = createStep( ModuleRegistrationName.PRICING ) - // TODO: This needs to be implemented - // pricingModule.restorePrices(ids) + await pricingModule.restorePriceSetMoneyAmounts(ids) } ) diff --git a/packages/medusa/src/api-v2/admin/products/helpers.ts b/packages/medusa/src/api-v2/admin/products/helpers.ts index b5ac86a991..f04ad4f5e3 100644 --- a/packages/medusa/src/api-v2/admin/products/helpers.ts +++ b/packages/medusa/src/api-v2/admin/products/helpers.ts @@ -15,7 +15,10 @@ export const remapKeysForProduct = (selectFields: string[]) => { const pricingFields = selectFields .filter((fieldName: string) => isPricing(fieldName)) .map((fieldName: string) => - fieldName.replace("variants.prices", "variants.price_set.money_amounts") + fieldName.replace( + "variants.prices.", + "variants.price_set.price_set_money_amounts.money_amount." + ) ) return [...productFields, ...pricingFields] @@ -28,7 +31,10 @@ export const remapKeysForVariant = (selectFields: string[]) => { const pricingFields = selectFields .filter((fieldName: string) => isPricing(fieldName)) .map((fieldName: string) => - fieldName.replace("prices", "price_set.money_amounts") + fieldName.replace( + "prices.", + "price_set.price_set_money_amounts.money_amount." + ) ) return [...variantFields, ...pricingFields] @@ -44,8 +50,8 @@ export const remapProduct = (p: ProductDTO) => { export const remapVariant = (v: ProductVariantDTO) => { return { ...v, - prices: (v as any).price_set?.money_amounts?.map((ma) => ({ - ...ma, + prices: (v as any).price_set?.price_set_money_amounts?.map((psma) => ({ + ...psma.money_amount, variant_id: v.id, })), price_set: undefined, diff --git a/packages/medusa/src/api-v2/admin/products/route.ts b/packages/medusa/src/api-v2/admin/products/route.ts index 7bd2169612..09d5f0bc6e 100644 --- a/packages/medusa/src/api-v2/admin/products/route.ts +++ b/packages/medusa/src/api-v2/admin/products/route.ts @@ -5,14 +5,14 @@ import { isString, remoteQueryObjectFromString, } from "@medusajs/utils" +import { MedusaContainer } from "medusa-core-utils" import { AuthenticatedMedusaRequest, MedusaResponse, } from "../../../types/routing" import { listPriceLists } from "../price-lists/queries" -import { AdminGetProductsParams } from "./validators" import { refetchProduct, remapKeysForProduct, remapProduct } from "./helpers" -import { MedusaContainer } from "medusa-core-utils" +import { AdminGetProductsParams } from "./validators" const applyVariantFiltersForPriceList = async ( scope: MedusaContainer, @@ -59,6 +59,7 @@ export const GET = async ( ) => { const remoteQuery = req.scope.resolve(ContainerRegistrationKeys.REMOTE_QUERY) let filterableFields: AdminGetProductsParams = { ...req.filterableFields } + filterableFields = await applyVariantFiltersForPriceList( req.scope, filterableFields diff --git a/packages/pricing/integration-tests/__fixtures__/price-set-money-amount-rules/data.ts b/packages/pricing/integration-tests/__fixtures__/price-set-money-amount-rules/data.ts deleted file mode 100644 index daa4212543..0000000000 --- a/packages/pricing/integration-tests/__fixtures__/price-set-money-amount-rules/data.ts +++ /dev/null @@ -1,20 +0,0 @@ -export const defaultPriceSetMoneyAmountRulesData = [ - { - id: "psmar-1", - value: "EUR", - price_set_money_amount: "price-set-money-amount-USD", - rule_type: "rule-type-1", - }, - { - id: "psmar-2", - value: "EU", - price_set_money_amount: "price-set-money-amount-EUR", - rule_type: "rule-type-2", - }, - { - id: "psmar-3", - value: "CAD", - price_set_money_amount: "price-set-money-amount-CAD", - rule_type: "rule-type-2", - }, -] diff --git a/packages/pricing/integration-tests/__fixtures__/price-set-money-amount-rules/index.ts b/packages/pricing/integration-tests/__fixtures__/price-set-money-amount-rules/index.ts deleted file mode 100644 index 9d5ea516df..0000000000 --- a/packages/pricing/integration-tests/__fixtures__/price-set-money-amount-rules/index.ts +++ /dev/null @@ -1,22 +0,0 @@ -import { SqlEntityManager } from "@mikro-orm/postgresql" -import { PriceSetMoneyAmountRules } from "@models" -import { defaultPriceSetMoneyAmountRulesData } from "./data" - -export * from "./data" - -export async function createPriceSetMoneyAmountRules( - manager: SqlEntityManager, - psmarData: any[] = defaultPriceSetMoneyAmountRulesData -): Promise { - const priceSetMoneyAmountRules: PriceSetMoneyAmountRules[] = [] - - for (let data of psmarData) { - const psmar = manager.create(PriceSetMoneyAmountRules, data) - - priceSetMoneyAmountRules.push(psmar) - } - - await manager.persistAndFlush(priceSetMoneyAmountRules) - - return priceSetMoneyAmountRules -} diff --git a/packages/pricing/integration-tests/__fixtures__/seed-price-data.ts b/packages/pricing/integration-tests/__fixtures__/seed-price-data.ts index 26a481ed7d..8a6f9c1376 100644 --- a/packages/pricing/integration-tests/__fixtures__/seed-price-data.ts +++ b/packages/pricing/integration-tests/__fixtures__/seed-price-data.ts @@ -7,10 +7,6 @@ import { createPriceSetMoneyAmounts, defaultPriceSetMoneyAmountsData, } from "./price-set-money-amount" -import { - createPriceSetMoneyAmountRules, - defaultPriceSetMoneyAmountRulesData, -} from "./price-set-money-amount-rules" import { createRuleTypes, defaultRuleTypesData } from "./rule-type" jest.setTimeout(30000) @@ -22,7 +18,6 @@ export async function seedPriceData( priceSetsData = defaultPriceSetsData, priceRuleData = defaultPriceRuleData, priceSetMoneyAmountsData = defaultPriceSetMoneyAmountsData, - priceSetMoneyAmountRulesData = defaultPriceSetMoneyAmountRulesData, ruleTypesData = defaultRuleTypesData, } = {} ) { @@ -31,8 +26,4 @@ export async function seedPriceData( await createPriceSetMoneyAmounts(testManager, priceSetMoneyAmountsData) await createRuleTypes(testManager, ruleTypesData) await createPriceRules(testManager, priceRuleData) - await createPriceSetMoneyAmountRules( - testManager, - priceSetMoneyAmountRulesData - ) } diff --git a/packages/pricing/integration-tests/__tests__/services/pricing-module/calculate-price.spec.ts b/packages/pricing/integration-tests/__tests__/services/pricing-module/calculate-price.spec.ts index 61fbd0209f..9c8d1b4490 100644 --- a/packages/pricing/integration-tests/__tests__/services/pricing-module/calculate-price.spec.ts +++ b/packages/pricing/integration-tests/__tests__/services/pricing-module/calculate-price.spec.ts @@ -336,7 +336,6 @@ moduleIntegrationTestRunner({ moneyAmountsData, priceSetsData, priceSetMoneyAmountsData, - priceSetMoneyAmountRulesData: [], priceRuleData, ruleTypesData, }) diff --git a/packages/pricing/integration-tests/__tests__/services/pricing-module/money-amount.spec.ts b/packages/pricing/integration-tests/__tests__/services/pricing-module/money-amount.spec.ts index 096854f25c..5bd10d197b 100644 --- a/packages/pricing/integration-tests/__tests__/services/pricing-module/money-amount.spec.ts +++ b/packages/pricing/integration-tests/__tests__/services/pricing-module/money-amount.spec.ts @@ -1,13 +1,12 @@ import { Modules } from "@medusajs/modules-sdk" import { IPricingModuleService } from "@medusajs/types" import { SqlEntityManager } from "@mikro-orm/postgresql" +import { moduleIntegrationTestRunner, SuiteOptions } from "medusa-test-utils" import { createMoneyAmounts } from "../../../__fixtures__/money-amount" import { createPriceRules } from "../../../__fixtures__/price-rule" import { createPriceSets } from "../../../__fixtures__/price-set" import { createPriceSetMoneyAmounts } from "../../../__fixtures__/price-set-money-amount" -import { createPriceSetMoneyAmountRules } from "../../../__fixtures__/price-set-money-amount-rules" import { createRuleTypes } from "../../../__fixtures__/rule-type" -import { moduleIntegrationTestRunner, SuiteOptions } from "medusa-test-utils" jest.setTimeout(30000) @@ -245,18 +244,16 @@ moduleIntegrationTestRunner({ describe("softDeleteMoneyAmounts", () => { const id = "money-amount-USD" - it("should softDelete priceSetMoneyAmount and PriceRule when soft-deleting money amount", async () => { + it("should softDelete money amounts successfully", async () => { await createPriceSets(testManager) await createRuleTypes(testManager) await createPriceSetMoneyAmounts(testManager) await createPriceRules(testManager) - await createPriceSetMoneyAmountRules(testManager) + await service.softDeleteMoneyAmounts([id]) const [moneyAmount] = await service.listMoneyAmounts( - { - id: [id], - }, + { id: [id] }, { relations: [ "price_set_money_amount", @@ -274,10 +271,10 @@ moduleIntegrationTestRunner({ expect.objectContaining({ deleted_at: deletedAt, price_set_money_amount: expect.objectContaining({ - deleted_at: deletedAt, + deleted_at: null, price_rules: [ expect.objectContaining({ - deleted_at: deletedAt, + deleted_at: null, }), ], }), @@ -294,7 +291,6 @@ moduleIntegrationTestRunner({ await createRuleTypes(testManager) await createPriceSetMoneyAmounts(testManager) await createPriceRules(testManager) - await createPriceSetMoneyAmountRules(testManager) await service.softDeleteMoneyAmounts([id]) await service.restoreMoneyAmounts([id]) diff --git a/packages/pricing/integration-tests/__tests__/services/pricing-module/price-rule.spec.ts b/packages/pricing/integration-tests/__tests__/services/pricing-module/price-rule.spec.ts index b2750149ab..0125f92965 100644 --- a/packages/pricing/integration-tests/__tests__/services/pricing-module/price-rule.spec.ts +++ b/packages/pricing/integration-tests/__tests__/services/pricing-module/price-rule.spec.ts @@ -1,15 +1,13 @@ +import { Modules } from "@medusajs/modules-sdk" import { CreatePriceRuleDTO, IPricingModuleService } from "@medusajs/types" import { SqlEntityManager } from "@mikro-orm/postgresql" - +import { SuiteOptions, moduleIntegrationTestRunner } from "medusa-test-utils" import { PriceSetMoneyAmount } from "../../../../src" import { createMoneyAmounts } from "../../../__fixtures__/money-amount" import { createPriceRules } from "../../../__fixtures__/price-rule" import { createPriceSets } from "../../../__fixtures__/price-set" import { createPriceSetMoneyAmounts } from "../../../__fixtures__/price-set-money-amount" -import { createPriceSetMoneyAmountRules } from "../../../__fixtures__/price-set-money-amount-rules" import { createRuleTypes } from "../../../__fixtures__/rule-type" -import { Modules } from "@medusajs/modules-sdk" -import { moduleIntegrationTestRunner, SuiteOptions } from "medusa-test-utils" jest.setTimeout(30000) @@ -28,7 +26,6 @@ moduleIntegrationTestRunner({ await createPriceSets(testManager) await createRuleTypes(testManager) await createPriceSetMoneyAmounts(testManager) - await createPriceSetMoneyAmountRules(testManager) await createPriceRules(testManager) }) diff --git a/packages/pricing/integration-tests/__tests__/services/pricing-module/price-set-money-amount-rules.spec.ts b/packages/pricing/integration-tests/__tests__/services/pricing-module/price-set-money-amount-rules.spec.ts deleted file mode 100644 index 63029afcda..0000000000 --- a/packages/pricing/integration-tests/__tests__/services/pricing-module/price-set-money-amount-rules.spec.ts +++ /dev/null @@ -1,278 +0,0 @@ -import { IPricingModuleService } from "@medusajs/types" -import { SqlEntityManager } from "@mikro-orm/postgresql" -import { createMoneyAmounts } from "../../../__fixtures__/money-amount" -import { createPriceSets } from "../../../__fixtures__/price-set" -import { createPriceSetMoneyAmounts } from "../../../__fixtures__/price-set-money-amount" -import { createPriceSetMoneyAmountRules } from "../../../__fixtures__/price-set-money-amount-rules" -import { createRuleTypes } from "../../../__fixtures__/rule-type" -import { Modules } from "@medusajs/modules-sdk" -import { moduleIntegrationTestRunner, SuiteOptions } from "medusa-test-utils" - -jest.setTimeout(30000) - -moduleIntegrationTestRunner({ - moduleName: Modules.PRICING, - testSuite: ({ - MikroOrmWrapper, - service, - }: SuiteOptions) => { - describe("PricingModule Service - PriceSetMoneyAmountRules", () => { - beforeEach(async () => { - const testManager = await MikroOrmWrapper.forkManager() - - await createMoneyAmounts(testManager) - await createPriceSets(testManager) - await createRuleTypes(testManager) - await createPriceSetMoneyAmounts(testManager) - await createPriceSetMoneyAmountRules(testManager) - }) - - describe("listPriceSetMoneyAmountRules", () => { - it("should list psmar records", async () => { - const priceSetMoneyAmountRulesResult = - await service.listPriceSetMoneyAmountRules() - - expect(priceSetMoneyAmountRulesResult).toEqual([ - expect.objectContaining({ - id: "psmar-1", - }), - expect.objectContaining({ - id: "psmar-2", - }), - expect.objectContaining({ - id: "psmar-3", - }), - ]) - }) - - it("should list psmar record by id", async () => { - const priceSetMoneyAmountRulesResult = - await service.listPriceSetMoneyAmountRules({ - id: ["psmar-1"], - }) - - expect(priceSetMoneyAmountRulesResult).toEqual([ - expect.objectContaining({ - id: "psmar-1", - }), - ]) - }) - }) - - describe("listAndCount", () => { - it("should return psmar records and count", async () => { - const [priceSetMoneyAmountRulesResult, count] = - await service.listAndCountPriceSetMoneyAmountRules() - - expect(count).toEqual(3) - expect(priceSetMoneyAmountRulesResult).toEqual([ - expect.objectContaining({ - id: "psmar-1", - }), - expect.objectContaining({ - id: "psmar-2", - }), - expect.objectContaining({ - id: "psmar-3", - }), - ]) - }) - - it("should return psmar records and count when filtered", async () => { - const [priceSetMoneyAmountRulesResult, count] = - await service.listAndCountPriceSetMoneyAmountRules({ - id: ["psmar-1"], - }) - - expect(count).toEqual(1) - expect(priceSetMoneyAmountRulesResult).toEqual([ - expect.objectContaining({ - id: "psmar-1", - }), - ]) - }) - - it("should return psmar and count when using skip and take", async () => { - const [priceSetMoneyAmountRulesResult, count] = - await service.listAndCountPriceSetMoneyAmountRules( - {}, - { skip: 1, take: 1 } - ) - - expect(count).toEqual(3) - expect(priceSetMoneyAmountRulesResult).toEqual([ - expect.objectContaining({ - id: "psmar-2", - }), - ]) - }) - - it("should return requested fields", async () => { - const [priceSetMoneyAmountRulesResult, count] = - await service.listAndCountPriceSetMoneyAmountRules( - {}, - { - take: 1, - select: ["value"], - } - ) - - const serialized = JSON.parse( - JSON.stringify(priceSetMoneyAmountRulesResult) - ) - - expect(count).toEqual(3) - expect(serialized).toEqual([ - { - id: "psmar-1", - value: "EUR", - }, - ]) - }) - }) - - describe("retrievePriceSetMoneyAmountRules", () => { - it("should return priceSetMoneyAmountRules for the given id", async () => { - const priceSetMoneyAmountRules = - await service.retrievePriceSetMoneyAmountRules("psmar-1") - - expect(priceSetMoneyAmountRules).toEqual( - expect.objectContaining({ - id: "psmar-1", - }) - ) - }) - - it("should throw an error when priceSetMoneyAmountRules with id does not exist", async () => { - let error - - try { - await service.retrievePriceSetMoneyAmountRules("does-not-exist") - } catch (e) { - error = e - } - - expect(error.message).toEqual( - "PriceSetMoneyAmountRules with id: does-not-exist was not found" - ) - }) - - it("should throw an error when an id is not provided", async () => { - let error - - try { - await service.retrievePriceSetMoneyAmountRules( - undefined as unknown as string - ) - } catch (e) { - error = e - } - - expect(error.message).toEqual( - "priceSetMoneyAmountRules - id must be defined" - ) - }) - - it("should return priceSetMoneyAmountRules based on config select param", async () => { - const priceSetMoneyAmountRulesResult = - await service.retrievePriceSetMoneyAmountRules("psmar-1", { - select: ["value"], - }) - - const serialized = JSON.parse( - JSON.stringify(priceSetMoneyAmountRulesResult) - ) - - expect(serialized).toEqual({ - value: "EUR", - id: "psmar-1", - }) - }) - }) - - describe("deletePriceSetMoneyAmountRules", () => { - const id = "psmar-1" - - it("should delete the priceSetMoneyAmountRuless given an id successfully", async () => { - await service.deletePriceSetMoneyAmountRules([id]) - - const currencies = await service.listPriceSetMoneyAmountRules({ - id: [id], - }) - - expect(currencies).toHaveLength(0) - }) - }) - - describe("updatePriceSetMoneyAmountRules", () => { - const id = "psmar-1" - - it("should update the value of the priceSetMoneyAmountRules successfully", async () => { - await service.updatePriceSetMoneyAmountRules([ - { - id, - value: "New value", - }, - ]) - - const psmar = await service.retrievePriceSetMoneyAmountRules(id) - - expect(psmar.value).toEqual("New value") - }) - - it("should throw an error when a id does not exist", async () => { - let error - - try { - await service.updatePriceSetMoneyAmountRules([ - { - id: "does-not-exist", - value: "random value", - }, - ]) - } catch (e) { - error = e - } - - expect(error.message).toEqual( - 'PriceSetMoneyAmountRules with id "does-not-exist" not found' - ) - }) - }) - - describe("createPriceSetMoneyAmountRules", () => { - it("should create a priceSetMoneyAmountRules successfully", async () => { - await service.createPriceSetMoneyAmountRules([ - { - price_set_money_amount: "price-set-money-amount-EUR", - rule_type: "rule-type-2", - value: "New priceSetMoneyAmountRule", - }, - ]) - - const [created] = await service.listPriceSetMoneyAmountRules( - { - value: ["New priceSetMoneyAmountRule"], - }, - { - relations: ["price_set_money_amount", "rule_type"], - } - ) - - expect(created).toEqual( - expect.objectContaining({ - id: expect.any(String), - value: "New priceSetMoneyAmountRule", - price_set_money_amount: expect.objectContaining({ - id: "price-set-money-amount-EUR", - }), - rule_type: expect.objectContaining({ - id: "rule-type-2", - }), - }) - ) - }) - }) - }) - }, -}) diff --git a/packages/pricing/integration-tests/__tests__/services/pricing-module/price-set.spec.ts b/packages/pricing/integration-tests/__tests__/services/pricing-module/price-set.spec.ts index 236d9b67fb..1a18f1f2f5 100644 --- a/packages/pricing/integration-tests/__tests__/services/pricing-module/price-set.spec.ts +++ b/packages/pricing/integration-tests/__tests__/services/pricing-module/price-set.spec.ts @@ -84,8 +84,12 @@ moduleIntegrationTestRunner({ id: ["price-set-1"], }, { - select: ["id", "money_amounts.id", "money_amounts.amount"], - relations: ["money_amounts"], + select: [ + "id", + "price_set_money_amounts.money_amount.id", + "price_set_money_amounts.money_amount.amount", + ], + relations: ["price_set_money_amounts.money_amount"], } ) @@ -94,10 +98,12 @@ moduleIntegrationTestRunner({ expect(serialized).toEqual([ { id: "price-set-1", - money_amounts: [ + price_set_money_amounts: [ expect.objectContaining({ - id: "money-amount-USD", - amount: 500, + money_amount: expect.objectContaining({ + id: "money-amount-USD", + amount: 500, + }), }), ], }, @@ -143,7 +149,7 @@ moduleIntegrationTestRunner({ }, { select: ["id", "min_quantity", "money_amounts.id"], - relations: ["money_amounts"], + relations: ["price_set_money_amounts.money_amount"], } ) @@ -153,8 +159,12 @@ moduleIntegrationTestRunner({ expect(serialized).toEqual([ { id: "price-set-1", - money_amounts: [ - expect.objectContaining({ id: "money-amount-USD" }), + price_set_money_amounts: [ + expect.objectContaining({ + money_amount: expect.objectContaining({ + id: "money-amount-USD", + }), + }), ], }, ]) @@ -368,10 +378,12 @@ moduleIntegrationTestRunner({ rule_attribute: "region_id", }), ], - money_amounts: [ + price_set_money_amounts: [ expect.objectContaining({ - amount: 100, - currency_code: "USD", + money_amount: expect.objectContaining({ + amount: 100, + currency_code: "USD", + }), }), ], }) @@ -405,14 +417,18 @@ moduleIntegrationTestRunner({ rule_attribute: "region_id", }), ], - money_amounts: expect.arrayContaining([ + price_set_money_amounts: expect.arrayContaining([ expect.objectContaining({ - amount: 100, - currency_code: "USD", + money_amount: expect.objectContaining({ + amount: 100, + currency_code: "USD", + }), }), expect.objectContaining({ - amount: 150, - currency_code: "USD", + money_amount: expect.objectContaining({ + amount: 150, + currency_code: "USD", + }), }), ]), }) @@ -442,10 +458,12 @@ moduleIntegrationTestRunner({ rule_attribute: "region_id", }), ], - money_amounts: [ + price_set_money_amounts: [ expect.objectContaining({ - amount: 100, - currency_code: "USD", + money_amount: expect.objectContaining({ + amount: 100, + currency_code: "USD", + }), }), ], price_rules: [ @@ -520,7 +538,11 @@ moduleIntegrationTestRunner({ id: [createdPriceSet[0].id], }, { - relations: ["rule_types", "money_amounts", "price_rules"], + relations: [ + "rule_types", + "price_set_money_amounts.money_amount", + "price_rules", + ], } ) @@ -536,10 +558,12 @@ moduleIntegrationTestRunner({ }), }, ], - money_amounts: [ + price_set_money_amounts: [ expect.objectContaining({ - amount: 500, - currency_code: "EUR", + money_amount: expect.objectContaining({ + amount: 500, + currency_code: "EUR", + }), }), ], rule_types: [ @@ -563,15 +587,22 @@ moduleIntegrationTestRunner({ id: [createdPriceSet[0].id], }, { - relations: ["rule_types", "money_amounts", "price_rules"], + relations: [ + "rule_types", + "price_set_money_amounts", + "price_rules", + ], } ) expect(priceSet).toEqual([ { id: expect.any(String), price_rules: [], - money_amounts: [], + price_set_money_amounts: [], rule_types: [], + created_at: expect.any(Date), + updated_at: expect.any(Date), + deleted_at: null, }, ]) }) @@ -594,16 +625,18 @@ moduleIntegrationTestRunner({ const [priceSet] = await service.list( { id: ["price-set-1"] }, - { relations: ["money_amounts"] } + { relations: ["price_set_money_amounts.money_amount"] } ) expect(priceSet).toEqual( expect.objectContaining({ id: "price-set-1", - money_amounts: expect.arrayContaining([ + price_set_money_amounts: expect.arrayContaining([ expect.objectContaining({ - amount: 100, - currency_code: "USD", + money_amount: expect.objectContaining({ + amount: 100, + currency_code: "USD", + }), }), ]), }) @@ -636,25 +669,29 @@ moduleIntegrationTestRunner({ const priceSets = await service.list( { id: ["price-set-1", "price-set-2"] }, - { relations: ["money_amounts"] } + { relations: ["price_set_money_amounts.money_amount"] } ) expect(priceSets).toEqual([ expect.objectContaining({ id: "price-set-1", - money_amounts: expect.arrayContaining([ + price_set_money_amounts: expect.arrayContaining([ expect.objectContaining({ - amount: 100, - currency_code: "USD", + money_amount: expect.objectContaining({ + amount: 100, + currency_code: "USD", + }), }), ]), }), expect.objectContaining({ id: "price-set-2", - money_amounts: expect.arrayContaining([ + price_set_money_amounts: expect.arrayContaining([ expect.objectContaining({ - amount: 150, - currency_code: "EUR", + money_amount: expect.objectContaining({ + amount: 150, + currency_code: "EUR", + }), }), ]), }), diff --git a/packages/pricing/src/migrations/.snapshot-medusa-pricing.json b/packages/pricing/src/migrations/.snapshot-medusa-pricing.json index 6bdc010d3d..83410a16a9 100644 --- a/packages/pricing/src/migrations/.snapshot-medusa-pricing.json +++ b/packages/pricing/src/migrations/.snapshot-medusa-pricing.json @@ -1,7 +1,5 @@ { - "namespaces": [ - "public" - ], + "namespaces": ["public"], "name": "public", "tables": [ { @@ -21,7 +19,7 @@ "unsigned": false, "autoincrement": false, "primary": false, - "nullable": true, + "nullable": false, "mappedType": "text" }, "amount": { @@ -30,7 +28,7 @@ "unsigned": false, "autoincrement": false, "primary": false, - "nullable": true, + "nullable": false, "mappedType": "decimal" }, "min_quantity": { @@ -88,28 +86,24 @@ "schema": "public", "indexes": [ { - "columnNames": [ - "currency_code" - ], - "composite": false, "keyName": "IDX_money_amount_currency_code", + "columnNames": ["currency_code"], + "composite": false, "primary": false, - "unique": false + "unique": false, + "expression": "CREATE INDEX IF NOT EXISTS \"IDX_money_amount_currency_code\" ON \"money_amount\" (currency_code) WHERE deleted_at IS NULL" }, { - "columnNames": [ - "deleted_at" - ], - "composite": false, "keyName": "IDX_money_amount_deleted_at", + "columnNames": ["deleted_at"], + "composite": false, "primary": false, - "unique": false + "unique": false, + "expression": "CREATE INDEX IF NOT EXISTS \"IDX_money_amount_deleted_at\" ON \"money_amount\" (deleted_at) WHERE deleted_at IS NOT NULL" }, { "keyName": "money_amount_pkey", - "columnNames": [ - "id" - ], + "columnNames": ["id"], "composite": false, "primary": true, "unique": true @@ -155,10 +149,7 @@ "primary": false, "nullable": false, "default": "'draft'", - "enumItems": [ - "active", - "draft" - ], + "enumItems": ["active", "draft"], "mappedType": "enum" }, "type": { @@ -169,10 +160,7 @@ "primary": false, "nullable": false, "default": "'sale'", - "enumItems": [ - "sale", - "override" - ], + "enumItems": ["sale", "override"], "mappedType": "enum" }, "starts_at": { @@ -242,19 +230,16 @@ "schema": "public", "indexes": [ { - "columnNames": [ - "deleted_at" - ], - "composite": false, "keyName": "IDX_price_list_deleted_at", + "columnNames": ["deleted_at"], + "composite": 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" }, { "keyName": "price_list_pkey", - "columnNames": [ - "id" - ], + "columnNames": ["id"], "composite": false, "primary": true, "unique": true @@ -273,16 +258,54 @@ "primary": false, "nullable": false, "mappedType": "text" + }, + "created_at": { + "name": "created_at", + "type": "timestamptz", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": false, + "length": 6, + "default": "now()", + "mappedType": "datetime" + }, + "updated_at": { + "name": "updated_at", + "type": "timestamptz", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": false, + "length": 6, + "default": "now()", + "mappedType": "datetime" + }, + "deleted_at": { + "name": "deleted_at", + "type": "timestamptz", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": true, + "length": 6, + "mappedType": "datetime" } }, "name": "price_set", "schema": "public", "indexes": [ + { + "keyName": "IDX_price_set_deleted_at", + "columnNames": ["deleted_at"], + "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" + }, { "keyName": "price_set_pkey", - "columnNames": [ - "id" - ], + "columnNames": ["id"], "composite": false, "primary": true, "unique": true @@ -308,7 +331,7 @@ "unsigned": false, "autoincrement": false, "primary": false, - "nullable": false, + "nullable": true, "mappedType": "text" }, "price_set_id": { @@ -385,55 +408,47 @@ "schema": "public", "indexes": [ { - "columnNames": [ - "price_set_id" - ], - "composite": false, - "keyName": "IDX_price_set_money_amount_price_set_id", - "primary": false, - "unique": false - }, - { - "columnNames": [ - "money_amount_id" - ], - "composite": false, - "keyName": "IDX_price_set_money_amount_money_amount_id", - "primary": false, - "unique": false - }, - { - "columnNames": [ - "money_amount_id" - ], + "columnNames": ["money_amount_id"], "composite": false, "keyName": "price_set_money_amount_money_amount_id_unique", "primary": false, "unique": true }, { - "columnNames": [ - "price_list_id" - ], + "keyName": "IDX_price_set_money_amount_price_set_id", + "columnNames": ["price_set_id"], "composite": false, - "keyName": "IDX_price_rule_price_list_id", "primary": false, - "unique": false + "unique": false, + "expression": "CREATE INDEX IF NOT EXISTS \"IDX_price_set_money_amount_price_set_id\" ON \"price_set_money_amount\" (price_set_id) WHERE deleted_at IS NULL" }, { - "columnNames": [ - "deleted_at" - ], + "keyName": "IDX_price_set_money_amount_money_amount_id", + "columnNames": ["money_amount_id"], "composite": false, - "keyName": "IDX_price_set_money_amount_deleted_at", "primary": false, - "unique": false + "unique": false, + "expression": "CREATE INDEX IF NOT EXISTS \"IDX_price_set_money_amount_money_amount_id\" ON \"price_set_money_amount\" (money_amount_id) WHERE deleted_at IS NULL" + }, + { + "keyName": "IDX_price_set_money_amount_price_list_id", + "columnNames": ["price_list_id"], + "composite": false, + "primary": false, + "unique": false, + "expression": "CREATE INDEX IF NOT EXISTS \"IDX_price_set_money_amount_price_list_id\" ON \"price_set_money_amount\" (price_list_id) WHERE deleted_at IS NULL" + }, + { + "keyName": "IDX_price_set_money_amount_deleted_at", + "columnNames": ["deleted_at"], + "composite": false, + "primary": false, + "unique": false, + "expression": "CREATE INDEX IF NOT EXISTS \"IDX_price_set_money_amount_deleted_at\" ON \"price_set_money_amount\" (deleted_at) WHERE deleted_at IS NOT NULL" }, { "keyName": "price_set_money_amount_pkey", - "columnNames": [ - "id" - ], + "columnNames": ["id"], "composite": false, "primary": true, "unique": true @@ -443,41 +458,30 @@ "foreignKeys": { "price_set_money_amount_price_set_id_foreign": { "constraintName": "price_set_money_amount_price_set_id_foreign", - "columnNames": [ - "price_set_id" - ], + "columnNames": ["price_set_id"], "localTableName": "public.price_set_money_amount", - "referencedColumnNames": [ - "id" - ], + "referencedColumnNames": ["id"], "referencedTableName": "public.price_set", "deleteRule": "cascade", "updateRule": "cascade" }, "price_set_money_amount_money_amount_id_foreign": { "constraintName": "price_set_money_amount_money_amount_id_foreign", - "columnNames": [ - "money_amount_id" - ], + "columnNames": ["money_amount_id"], "localTableName": "public.price_set_money_amount", - "referencedColumnNames": [ - "id" - ], + "referencedColumnNames": ["id"], "referencedTableName": "public.money_amount", "deleteRule": "cascade", "updateRule": "cascade" }, "price_set_money_amount_price_list_id_foreign": { "constraintName": "price_set_money_amount_price_list_id_foreign", - "columnNames": [ - "price_list_id" - ], + "columnNames": ["price_list_id"], "localTableName": "public.price_set_money_amount", - "referencedColumnNames": [ - "id" - ], + "referencedColumnNames": ["id"], "referencedTableName": "public.price_list", - "deleteRule": "cascade" + "deleteRule": "cascade", + "updateRule": "cascade" } } }, @@ -541,25 +545,40 @@ "length": 6, "default": "now()", "mappedType": "datetime" + }, + "deleted_at": { + "name": "deleted_at", + "type": "timestamptz", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": true, + "length": 6, + "mappedType": "datetime" } }, "name": "rule_type", "schema": "public", "indexes": [ { - "columnNames": [ - "rule_attribute" - ], - "composite": false, "keyName": "IDX_rule_type_rule_attribute", + "columnNames": ["rule_attribute"], + "composite": false, "primary": false, - "unique": false + "unique": false, + "expression": "CREATE INDEX IF NOT EXISTS \"IDX_rule_type_rule_attribute\" ON \"rule_type\" (rule_attribute) WHERE deleted_at IS NULL" + }, + { + "keyName": "IDX_rule_type_deleted_at", + "columnNames": ["deleted_at"], + "composite": false, + "primary": false, + "unique": false, + "expression": "CREATE INDEX IF NOT EXISTS \"IDX_rule_type_deleted_at\" ON \"rule_type\" (deleted_at) WHERE deleted_at IS NOT NULL" }, { "keyName": "rule_type_pkey", - "columnNames": [ - "id" - ], + "columnNames": ["id"], "composite": false, "primary": true, "unique": true @@ -596,34 +615,70 @@ "primary": false, "nullable": false, "mappedType": "text" + }, + "created_at": { + "name": "created_at", + "type": "timestamptz", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": false, + "length": 6, + "default": "now()", + "mappedType": "datetime" + }, + "updated_at": { + "name": "updated_at", + "type": "timestamptz", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": false, + "length": 6, + "default": "now()", + "mappedType": "datetime" + }, + "deleted_at": { + "name": "deleted_at", + "type": "timestamptz", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": true, + "length": 6, + "mappedType": "datetime" } }, "name": "price_set_rule_type", "schema": "public", "indexes": [ { - "columnNames": [ - "price_set_id" - ], - "composite": false, "keyName": "IDX_price_set_rule_type_price_set_id", + "columnNames": ["price_set_id"], + "composite": false, "primary": false, - "unique": false + "unique": false, + "expression": "CREATE INDEX IF NOT EXISTS \"IDX_price_set_rule_type_price_set_id\" ON \"price_set_rule_type\" (price_set_id) WHERE deleted_at IS NULL" }, { - "columnNames": [ - "rule_type_id" - ], - "composite": false, "keyName": "IDX_price_set_rule_type_rule_type_id", + "columnNames": ["rule_type_id"], + "composite": false, "primary": false, - "unique": false + "unique": false, + "expression": "CREATE INDEX IF NOT EXISTS \"IDX_price_set_rule_type_rule_type_id\" ON \"price_set_rule_type\" (rule_type_id) WHERE deleted_at IS NULL" + }, + { + "keyName": "IDX_price_set_rule_type_deleted_at", + "columnNames": ["deleted_at"], + "composite": false, + "primary": false, + "unique": false, + "expression": "CREATE INDEX IF NOT EXISTS \"IDX_price_set_rule_type_deleted_at\" ON \"price_set_rule_type\" (deleted_at) WHERE deleted_at IS NOT NULL" }, { "keyName": "price_set_rule_type_pkey", - "columnNames": [ - "id" - ], + "columnNames": ["id"], "composite": false, "primary": true, "unique": true @@ -633,127 +688,21 @@ "foreignKeys": { "price_set_rule_type_price_set_id_foreign": { "constraintName": "price_set_rule_type_price_set_id_foreign", - "columnNames": [ - "price_set_id" - ], + "columnNames": ["price_set_id"], "localTableName": "public.price_set_rule_type", - "referencedColumnNames": [ - "id" - ], + "referencedColumnNames": ["id"], "referencedTableName": "public.price_set", "deleteRule": "cascade", "updateRule": "cascade" }, "price_set_rule_type_rule_type_id_foreign": { "constraintName": "price_set_rule_type_rule_type_id_foreign", - "columnNames": [ - "rule_type_id" - ], + "columnNames": ["rule_type_id"], "localTableName": "public.price_set_rule_type", - "referencedColumnNames": [ - "id" - ], + "referencedColumnNames": ["id"], "referencedTableName": "public.rule_type", - "updateRule": "cascade" - } - } - }, - { - "columns": { - "id": { - "name": "id", - "type": "text", - "unsigned": false, - "autoincrement": false, - "primary": false, - "nullable": false, - "mappedType": "text" - }, - "price_set_money_amount_id": { - "name": "price_set_money_amount_id", - "type": "text", - "unsigned": false, - "autoincrement": false, - "primary": false, - "nullable": false, - "mappedType": "text" - }, - "rule_type_id": { - "name": "rule_type_id", - "type": "text", - "unsigned": false, - "autoincrement": false, - "primary": false, - "nullable": false, - "mappedType": "text" - }, - "value": { - "name": "value", - "type": "text", - "unsigned": false, - "autoincrement": false, - "primary": false, - "nullable": false, - "mappedType": "text" - } - }, - "name": "price_set_money_amount_rules", - "schema": "public", - "indexes": [ - { - "columnNames": [ - "price_set_money_amount_id" - ], - "composite": false, - "keyName": "IDX_price_set_money_amount_rules_price_set_money_amount_id", - "primary": false, - "unique": false - }, - { - "columnNames": [ - "rule_type_id" - ], - "composite": false, - "keyName": "IDX_price_set_money_amount_rules_rule_type_id", - "primary": false, - "unique": false - }, - { - "keyName": "price_set_money_amount_rules_pkey", - "columnNames": [ - "id" - ], - "composite": false, - "primary": true, - "unique": true - } - ], - "checks": [], - "foreignKeys": { - "price_set_money_amount_rules_price_set_money_amount_id_foreign": { - "constraintName": "price_set_money_amount_rules_price_set_money_amount_id_foreign", - "columnNames": [ - "price_set_money_amount_id" - ], - "localTableName": "public.price_set_money_amount_rules", - "referencedColumnNames": [ - "id" - ], - "referencedTableName": "public.price_set_money_amount", "deleteRule": "cascade", "updateRule": "cascade" - }, - "price_set_money_amount_rules_rule_type_id_foreign": { - "constraintName": "price_set_money_amount_rules_rule_type_id_foreign", - "columnNames": [ - "rule_type_id" - ], - "localTableName": "public.price_set_money_amount_rules", - "referencedColumnNames": [ - "id" - ], - "referencedTableName": "public.rule_type", - "updateRule": "cascade" } } }, @@ -851,46 +800,40 @@ "schema": "public", "indexes": [ { - "columnNames": [ - "price_set_id" - ], - "composite": false, "keyName": "IDX_price_rule_price_set_id", + "columnNames": ["price_set_id"], + "composite": false, "primary": false, - "unique": false + "unique": false, + "expression": "CREATE INDEX IF NOT EXISTS \"IDX_price_rule_price_set_id\" ON \"price_rule\" (price_set_id) WHERE deleted_at IS NULL" }, { - "columnNames": [ - "rule_type_id" - ], - "composite": false, "keyName": "IDX_price_rule_rule_type_id", + "columnNames": ["rule_type_id"], + "composite": false, "primary": false, - "unique": false + "unique": false, + "expression": "CREATE INDEX IF NOT EXISTS \"IDX_price_rule_rule_type_id\" ON \"price_rule\" (rule_type_id) WHERE deleted_at IS NULL" }, { - "columnNames": [ - "price_set_money_amount_id" - ], + "keyName": "IDX_price_rule_price_set_money_amount_id_unique", + "columnNames": ["price_set_money_amount_id"], "composite": false, - "keyName": "IDX_price_rule_price_set_money_amount_id", "primary": false, - "unique": false + "unique": false, + "expression": "CREATE UNIQUE INDEX IF NOT EXISTS \"IDX_price_rule_price_set_money_amount_id_unique\" ON \"price_rule\" (price_set_money_amount_id) WHERE deleted_at IS NULL" }, { - "columnNames": [ - "deleted_at" - ], - "composite": false, "keyName": "IDX_price_rule_deleted_at", + "columnNames": ["deleted_at"], + "composite": false, "primary": false, - "unique": 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", - "columnNames": [ - "id" - ], + "columnNames": ["id"], "composite": false, "primary": true, "unique": true @@ -900,38 +843,26 @@ "foreignKeys": { "price_rule_price_set_id_foreign": { "constraintName": "price_rule_price_set_id_foreign", - "columnNames": [ - "price_set_id" - ], + "columnNames": ["price_set_id"], "localTableName": "public.price_rule", - "referencedColumnNames": [ - "id" - ], + "referencedColumnNames": ["id"], "referencedTableName": "public.price_set", "deleteRule": "cascade", "updateRule": "cascade" }, "price_rule_rule_type_id_foreign": { "constraintName": "price_rule_rule_type_id_foreign", - "columnNames": [ - "rule_type_id" - ], + "columnNames": ["rule_type_id"], "localTableName": "public.price_rule", - "referencedColumnNames": [ - "id" - ], + "referencedColumnNames": ["id"], "referencedTableName": "public.rule_type", "updateRule": "cascade" }, "price_rule_price_set_money_amount_id_foreign": { "constraintName": "price_rule_price_set_money_amount_id_foreign", - "columnNames": [ - "price_set_money_amount_id" - ], + "columnNames": ["price_set_money_amount_id"], "localTableName": "public.price_rule", - "referencedColumnNames": [ - "id" - ], + "referencedColumnNames": ["id"], "referencedTableName": "public.price_set_money_amount", "deleteRule": "cascade", "updateRule": "cascade" @@ -958,6 +889,38 @@ "nullable": false, "mappedType": "text" }, + "created_at": { + "name": "created_at", + "type": "timestamptz", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": false, + "length": 6, + "default": "now()", + "mappedType": "datetime" + }, + "updated_at": { + "name": "updated_at", + "type": "timestamptz", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": false, + "length": 6, + "default": "now()", + "mappedType": "datetime" + }, + "deleted_at": { + "name": "deleted_at", + "type": "timestamptz", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": true, + "length": 6, + "mappedType": "datetime" + }, "rule_type_id": { "name": "rule_type_id", "type": "text", @@ -972,38 +935,32 @@ "schema": "public", "indexes": [ { - "columnNames": [ - "price_list_id" - ], + "keyName": "IDX_price_list_rule_rule_type_id_unique", + "columnNames": ["rule_type_id"], "composite": false, + "primary": false, + "unique": false, + "expression": "CREATE UNIQUE INDEX IF NOT EXISTS \"IDX_price_list_rule_rule_type_id_unique\" ON \"price_list_rule\" (rule_type_id) WHERE deleted_at IS NULL" + }, + { "keyName": "IDX_price_list_rule_price_list_id", - "primary": false, - "unique": false - }, - { - "columnNames": [ - "rule_type_id" - ], + "columnNames": ["price_list_id"], "composite": false, - "keyName": "IDX_price_list_rule_rule_type_id", "primary": false, - "unique": false + "unique": false, + "expression": "CREATE INDEX IF NOT EXISTS \"IDX_price_list_rule_price_list_id\" ON \"price_list_rule\" (price_list_id) WHERE deleted_at IS NULL" }, { - "keyName": "IDX_price_list_rule_rule_type_id_price_list_id_unique", - "columnNames": [ - "price_list_id", - "rule_type_id" - ], - "composite": true, + "keyName": "IDX_price_list_rule_deleted_at", + "columnNames": ["deleted_at"], + "composite": false, "primary": false, - "unique": true + "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" }, { "keyName": "price_list_rule_pkey", - "columnNames": [ - "id" - ], + "columnNames": ["id"], "composite": false, "primary": true, "unique": true @@ -1013,25 +970,18 @@ "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" }, "price_list_rule_rule_type_id_foreign": { "constraintName": "price_list_rule_rule_type_id_foreign", - "columnNames": [ - "rule_type_id" - ], + "columnNames": ["rule_type_id"], "localTableName": "public.price_list_rule", - "referencedColumnNames": [ - "id" - ], + "referencedColumnNames": ["id"], "referencedTableName": "public.rule_type", "updateRule": "cascade" } @@ -1065,25 +1015,62 @@ "primary": false, "nullable": false, "mappedType": "text" + }, + "created_at": { + "name": "created_at", + "type": "timestamptz", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": false, + "length": 6, + "default": "now()", + "mappedType": "datetime" + }, + "updated_at": { + "name": "updated_at", + "type": "timestamptz", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": false, + "length": 6, + "default": "now()", + "mappedType": "datetime" + }, + "deleted_at": { + "name": "deleted_at", + "type": "timestamptz", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": true, + "length": 6, + "mappedType": "datetime" } }, "name": "price_list_rule_value", "schema": "public", "indexes": [ { - "columnNames": [ - "price_list_rule_id" - ], + "keyName": "IDX_price_list_rule_value_price_list_rule_id", + "columnNames": ["price_list_rule_id"], "composite": false, - "keyName": "IDX_price_list_rule_price_list_rule_value_id", "primary": false, - "unique": false + "unique": false, + "expression": "CREATE INDEX IF NOT EXISTS \"IDX_price_list_rule_value_price_list_rule_id\" ON \"price_list_rule_value\" (price_list_rule_id) WHERE deleted_at IS NULL" + }, + { + "keyName": "IDX_price_list_rule_value_deleted_at", + "columnNames": ["deleted_at"], + "composite": false, + "primary": false, + "unique": false, + "expression": "CREATE INDEX IF NOT EXISTS \"IDX_price_list_rule_value_deleted_at\" ON \"price_list_rule_value\" (deleted_at) WHERE deleted_at IS NOT NULL" }, { "keyName": "price_list_rule_value_pkey", - "columnNames": [ - "id" - ], + "columnNames": ["id"], "composite": false, "primary": true, "unique": true @@ -1093,13 +1080,9 @@ "foreignKeys": { "price_list_rule_value_price_list_rule_id_foreign": { "constraintName": "price_list_rule_value_price_list_rule_id_foreign", - "columnNames": [ - "price_list_rule_id" - ], + "columnNames": ["price_list_rule_id"], "localTableName": "public.price_list_rule_value", - "referencedColumnNames": [ - "id" - ], + "referencedColumnNames": ["id"], "referencedTableName": "public.price_list_rule", "deleteRule": "cascade", "updateRule": "cascade" diff --git a/packages/pricing/src/migrations/Migration20230929122253.ts b/packages/pricing/src/migrations/Migration20230929122253.ts index 7a2f8665fa..3aee33b143 100644 --- a/packages/pricing/src/migrations/Migration20230929122253.ts +++ b/packages/pricing/src/migrations/Migration20230929122253.ts @@ -3,65 +3,31 @@ import { Migration } from "@mikro-orm/migrations" export class Migration20230929122253 extends Migration { async up(): Promise { this.addSql( - 'create table if not exists "money_amount" ("id" text not null, "currency_code" text null, "amount" numeric null, "min_quantity" numeric null, "max_quantity" numeric null, constraint "money_amount_pkey" primary key ("id"));' - ) - this.addSql( - 'create index if not exists "IDX_money_amount_currency_code" on "money_amount" ("currency_code");' + 'create table if not exists "money_amount" ("id" text not null, "currency_code" text not null, "amount" numeric not null, "min_quantity" numeric null, "max_quantity" numeric null, "created_at" timestamptz not null default now(), "updated_at" timestamptz not null default now(), "deleted_at" timestamptz null, constraint "money_amount_pkey" primary key ("id"));' ) this.addSql( - 'create table "price_set" ("id" text not null, constraint "price_set_pkey" primary key ("id"));' + 'create table "price_set" ("id" text not null, "created_at" timestamptz not null default now(), "updated_at" timestamptz not null default now(), "deleted_at" timestamptz null, constraint "price_set_pkey" primary key ("id"));' ) this.addSql( - 'create table "price_set_money_amount" ("id" text not null, "title" text not null, "price_set_id" text not null, "money_amount_id" text not null, "rules_count" integer not null default 0, constraint "price_set_money_amount_pkey" primary key ("id"));' - ) - this.addSql( - 'create index "IDX_price_set_money_amount_price_set_id" on "price_set_money_amount" ("price_set_id");' - ) - this.addSql( - 'create index "IDX_price_set_money_amount_money_amount_id" on "price_set_money_amount" ("money_amount_id");' + 'create table "price_set_money_amount" ("id" text not null, "title" text, "price_set_id" text not null, "money_amount_id" text not null, "rules_count" integer not null default 0, "created_at" timestamptz not null default now(), "updated_at" timestamptz not null default now(), "deleted_at" timestamptz null, constraint "price_set_money_amount_pkey" primary key ("id"));' ) this.addSql( - 'create table "rule_type" ("id" text not null, "name" text not null, "rule_attribute" text not null, "default_priority" integer not null default 0, "created_at" timestamptz not null default now(), "updated_at" timestamptz not null default now(), constraint "rule_type_pkey" primary key ("id"));' + 'alter table "price_set_money_amount" add constraint "price_set_money_amount_money_amount_id_unique" unique ("money_amount_id");' ) this.addSql( - 'create index "IDX_rule_type_rule_attribute" on "rule_type" ("rule_attribute");' + 'create table "rule_type" ("id" text not null, "name" text not null, "rule_attribute" text not null, "default_priority" integer not null default 0, "created_at" timestamptz not null default now(), "updated_at" timestamptz not null default now(), "deleted_at" timestamptz null, constraint "rule_type_pkey" primary key ("id"));' ) this.addSql( - 'create table "price_set_rule_type" ("id" text not null, "price_set_id" text not null, "rule_type_id" text not null, constraint "price_set_rule_type_pkey" primary key ("id"));' - ) - this.addSql( - 'create index "IDX_price_set_rule_type_price_set_id" on "price_set_rule_type" ("price_set_id");' - ) - this.addSql( - 'create index "IDX_price_set_rule_type_rule_type_id" on "price_set_rule_type" ("rule_type_id");' + 'create table "price_set_rule_type" ("id" text not null, "price_set_id" text not null, "rule_type_id" text not null, "created_at" timestamptz not null default now(), "updated_at" timestamptz not null default now(), "deleted_at" timestamptz null, constraint "price_set_rule_type_pkey" primary key ("id"));' ) this.addSql( - 'create table "price_set_money_amount_rules" ("id" text not null, "price_set_money_amount_id" text not null, "rule_type_id" text not null, "value" text not null, constraint "price_set_money_amount_rules_pkey" primary key ("id"));' - ) - this.addSql( - 'create index "IDX_price_set_money_amount_rules_price_set_money_amount_id" on "price_set_money_amount_rules" ("price_set_money_amount_id");' - ) - this.addSql( - 'create index "IDX_price_set_money_amount_rules_rule_type_id" on "price_set_money_amount_rules" ("rule_type_id");' - ) - - this.addSql( - 'create table "price_rule" ("id" text not null, "price_set_id" text not null, "rule_type_id" text not null, "is_dynamic" boolean not null default false, "value" text not null, "priority" integer not null default 0, "price_set_money_amount_id" text not null, "price_list_id" text not null, constraint "price_rule_pkey" primary key ("id"));' - ) - this.addSql( - 'create index "IDX_price_rule_price_set_id" on "price_rule" ("price_set_id");' - ) - this.addSql( - 'create index "IDX_price_rule_rule_type_id" on "price_rule" ("rule_type_id");' - ) - this.addSql( - 'create index "IDX_price_rule_price_set_money_amount_id" on "price_rule" ("price_set_money_amount_id");' + 'create table "price_rule" ("id" text not null, "price_set_id" text not null, "rule_type_id" text not null, "value" text not null, "priority" integer not null default 0, "price_set_money_amount_id" text not null, "price_list_id" text not null, "created_at" timestamptz not null default now(), "updated_at" timestamptz not null default now(), "deleted_at" timestamptz null, constraint "price_rule_pkey" primary key ("id"));' ) this.addSql( @@ -78,13 +44,6 @@ export class Migration20230929122253 extends Migration { 'alter table "price_set_rule_type" add constraint "price_set_rule_type_rule_type_id_foreign" foreign key ("rule_type_id") references "rule_type" ("id") on update cascade;' ) - this.addSql( - 'alter table "price_set_money_amount_rules" add constraint "price_set_money_amount_rules_price_set_money_amount_id_foreign" foreign key ("price_set_money_amount_id") references "price_set_money_amount" ("id") on update cascade on delete cascade;' - ) - this.addSql( - 'alter table "price_set_money_amount_rules" add constraint "price_set_money_amount_rules_rule_type_id_foreign" foreign key ("rule_type_id") references "rule_type" ("id") on update cascade;' - ) - this.addSql( 'alter table "price_rule" add constraint "price_rule_price_set_id_foreign" foreign key ("price_set_id") references "price_set" ("id") on update cascade on delete cascade;' ) @@ -100,14 +59,7 @@ export class Migration20230929122253 extends Migration { ) this.addSql( - 'create table "price_list_rule" ("id" text not null, "rule_type_id" text not null, "price_list_id" text not null, constraint "price_list_rule_pkey" primary key ("id"));' - ) - - this.addSql( - 'create index "IDX_price_list_rule_rule_type_id" on "price_list_rule" ("rule_type_id");' - ) - this.addSql( - 'create index "IDX_price_list_rule_price_list_id" on "price_list_rule" ("price_list_id");' + 'create table "price_list_rule" ("id" text not null, "rule_type_id" text not null, "price_list_id" text not null, "created_at" timestamptz not null default now(), "updated_at" timestamptz not null default now(), "deleted_at" timestamptz null, constraint "price_list_rule_pkey" primary key ("id"));' ) this.addSql( @@ -120,13 +72,139 @@ export class Migration20230929122253 extends Migration { this.addSql( 'alter table "price_set_money_amount" add column "price_list_id" text null;' ) - this.addSql( - 'alter table "price_set_money_amount" add constraint "price_set_money_amount_price_list_id_foreign" foreign key ("price_list_id") references "price_list" ("id") on update cascade on delete set null;' - ) - this.addSql( - 'create index "IDX_price_rule_price_list_id" on "price_set_money_amount" ("price_list_id");' - ) this.addSql('alter table "price_rule" drop column "price_list_id";') + + this.addSql( + 'create table "price_list_rule_value" ("id" text not null, "value" text not null, "price_list_rule_id" text not null, "created_at" timestamptz not null default now(), "updated_at" timestamptz not null default now(), "deleted_at" timestamptz null, constraint "price_list_rule_value_pkey" primary key ("id"));' + ) + + this.addSql( + 'alter table "price_list_rule_value" add constraint "price_list_rule_value_price_list_rule_id_foreign" foreign key ("price_list_rule_id") references "price_list_rule" ("id") on update cascade on delete cascade;' + ) + + this.addSql( + `ALTER TABLE price_list + ADD COLUMN IF NOT EXISTS rules_count integer not null default 0` + ) + + this.addSql( + 'alter table "price_list" add column if not exists "title" text, add column if not exists "name" text, add column if not exists "description" text not null, add column if not exists "type" text check ("type" in (\'sale\', \'override\')) not null default \'sale\', add column if not exists "created_at" timestamptz not null default now(), add column if not exists "updated_at" timestamptz not null default now(), add column if not exists "deleted_at" timestamptz null;' + ) + + this.addSql(` + UPDATE "price_list" + SET title = name + `) + + this.addSql(`alter table "price_list" alter column "title" set not null `) + + this.addSql( + 'CREATE INDEX IF NOT EXISTS "IDX_money_amount_currency_code" ON "money_amount" (currency_code) WHERE deleted_at IS NULL;' + ) + this.addSql( + 'CREATE INDEX IF NOT EXISTS "IDX_money_amount_deleted_at" ON "money_amount" (deleted_at) WHERE deleted_at IS NOT NULL;' + ) + this.addSql( + 'CREATE INDEX IF NOT EXISTS "IDX_price_list_deleted_at" ON "price_list" (deleted_at) WHERE deleted_at IS NOT NULL;' + ) + this.addSql( + 'CREATE INDEX IF NOT EXISTS "IDX_price_set_deleted_at" ON "price_set" (deleted_at) WHERE deleted_at IS NOT NULL;' + ) + this.addSql( + 'CREATE INDEX IF NOT EXISTS "IDX_price_set_money_amount_price_set_id" ON "price_set_money_amount" (price_set_id) WHERE deleted_at IS NULL;' + ) + this.addSql( + 'CREATE INDEX IF NOT EXISTS "IDX_price_set_money_amount_money_amount_id" ON "price_set_money_amount" (money_amount_id) WHERE deleted_at IS NULL;' + ) + this.addSql( + 'CREATE INDEX IF NOT EXISTS "IDX_price_set_money_amount_price_list_id" ON "price_set_money_amount" (price_list_id) WHERE deleted_at IS NULL;' + ) + this.addSql( + 'CREATE INDEX IF NOT EXISTS "IDX_price_set_money_amount_deleted_at" ON "price_set_money_amount" (deleted_at) WHERE deleted_at IS NOT NULL;' + ) + this.addSql( + 'CREATE INDEX IF NOT EXISTS "IDX_rule_type_rule_attribute" ON "rule_type" (rule_attribute) WHERE deleted_at IS NOT NULL;' + ) + this.addSql( + 'CREATE INDEX IF NOT EXISTS "IDX_rule_type_deleted_at" ON "rule_type" (deleted_at) WHERE deleted_at IS NOT NULL;' + ) + this.addSql( + 'CREATE INDEX IF NOT EXISTS "IDX_price_set_rule_type_price_set_id" ON "price_set_rule_type" (price_set_id) WHERE deleted_at IS NOT NULL;' + ) + this.addSql( + 'CREATE INDEX IF NOT EXISTS "IDX_price_set_rule_type_rule_type_id" ON "price_set_rule_type" (rule_type_id) WHERE deleted_at IS NOT NULL;' + ) + this.addSql( + 'CREATE INDEX IF NOT EXISTS "IDX_price_set_rule_type_deleted_at" ON "price_set_rule_type" (deleted_at) WHERE deleted_at IS NOT NULL;' + ) + + this.addSql( + 'CREATE INDEX IF NOT EXISTS "IDX_price_rule_price_set_id" ON "price_rule" (price_set_id) WHERE deleted_at IS NOT NULL;' + ) + this.addSql( + 'CREATE INDEX IF NOT EXISTS "IDX_price_rule_rule_type_id" ON "price_rule" (rule_type_id) WHERE deleted_at IS NOT NULL;' + ) + this.addSql( + 'CREATE UNIQUE INDEX IF NOT EXISTS "IDX_price_rule_price_set_money_amount_id_unique" ON "price_rule" (price_set_money_amount_id) WHERE deleted_at IS NOT NULL;' + ) + this.addSql( + 'CREATE INDEX IF NOT EXISTS "IDX_price_rule_deleted_at" ON "price_rule" (deleted_at) WHERE deleted_at IS NOT NULL;' + ) + this.addSql( + 'CREATE UNIQUE INDEX IF NOT EXISTS "IDX_price_list_rule_rule_type_id_unique" ON "price_list_rule" (rule_type_id) WHERE deleted_at IS NOT NULL;' + ) + this.addSql( + 'CREATE INDEX IF NOT EXISTS "IDX_price_list_rule_price_list_id" ON "price_list_rule" (price_list_id) WHERE deleted_at IS NOT NULL;' + ) + this.addSql( + 'CREATE INDEX IF NOT EXISTS "IDX_price_list_rule_deleted_at" ON "price_list_rule" (deleted_at) WHERE deleted_at IS NOT NULL;' + ) + this.addSql( + 'CREATE INDEX IF NOT EXISTS "IDX_price_list_rule_value_price_list_rule_id" ON "price_list_rule_value" (price_list_rule_id) WHERE deleted_at IS NULL;' + ) + this.addSql( + 'CREATE INDEX IF NOT EXISTS "IDX_price_list_rule_value_deleted_at" ON "price_list_rule_value" (deleted_at) WHERE deleted_at IS NOT NULL;' + ) + + this.addSql( + 'alter table if exists "price_list" drop column if exists "name";' + ) + + this.addSql( + 'alter table if exists "price_set_money_amount" add constraint "price_set_money_amount_price_list_id_foreign" foreign key ("price_list_id") references "price_list" ("id") on update cascade on delete cascade;' + ) + + this.addSql( + 'alter table if exists "price_rule" drop constraint if exists "price_rule_rule_type_id_foreign";' + ) + + this.addSql( + 'alter table if exists "price_rule" add constraint "price_rule_rule_type_id_foreign" foreign key ("rule_type_id") references "rule_type" ("id") on update cascade on delete cascade;' + ) + + this.addSql( + 'alter table if exists "price_rule" drop constraint if exists "price_rule_rule_type_id_foreign";' + ) + + this.addSql( + 'alter table if exists "price_list_rule" drop constraint if exists "price_list_rule_price_list_id_foreign";' + ) + + this.addSql( + 'alter table if exists "price_rule" add constraint "price_rule_rule_type_id_foreign" foreign key ("rule_type_id") references "rule_type" ("id") on update cascade;' + ) + + this.addSql( + 'alter table if exists "price_list_rule" add constraint "price_list_rule_price_list_id_foreign" foreign key ("price_list_id") references "price_list" ("id") on update cascade on delete cascade;' + ) + + this.addSql( + 'alter table if exists "price_set_rule_type" drop constraint if exists "price_set_rule_type_rule_type_id_foreign";' + ) + + this.addSql( + 'alter table if exists "price_set_rule_type" add constraint "price_set_rule_type_rule_type_id_foreign" foreign key ("rule_type_id") references "rule_type" ("id") on update cascade on delete cascade;' + ) } } diff --git a/packages/pricing/src/migrations/Migration20231101232834.ts b/packages/pricing/src/migrations/Migration20231101232834.ts deleted file mode 100644 index 5b812d57d3..0000000000 --- a/packages/pricing/src/migrations/Migration20231101232834.ts +++ /dev/null @@ -1,120 +0,0 @@ -import { Migration } from "@mikro-orm/migrations" - -export class Migration20231101232834 extends Migration { - async up(): Promise { - this.addSql( - 'create table "price_list_rule_value" ("id" text not null, "value" text not null, "price_list_rule_id" text not null, constraint "price_list_rule_value_pkey" primary key ("id"));' - ) - this.addSql( - 'create index "IDX_price_list_rule_price_list_rule_value_id" on "price_list_rule_value" ("price_list_rule_id");' - ) - - this.addSql( - 'alter table "price_list_rule_value" add constraint "price_list_rule_value_price_list_rule_id_foreign" foreign key ("price_list_rule_id") references "price_list_rule" ("id") on update cascade on delete cascade;' - ) - - this.addSql( - `ALTER TABLE price_list - ADD COLUMN IF NOT EXISTS rules_count integer not null default 0` - ) - - this.addSql( - 'alter table "price_set_money_amount" drop constraint "price_set_money_amount_price_list_id_foreign";' - ) - - this.addSql( - 'alter table "price_set_money_amount" add constraint "price_set_money_amount_price_list_id_foreign" foreign key ("price_list_id") references "price_list" ("id") on update cascade on delete cascade;' - ) - - this.addSql( - 'alter table "price_list" add column if not exists "title" text, add column if not exists "name" text, add column if not exists "description" text not null, add column if not exists "type" text check ("type" in (\'sale\', \'override\')) not null default \'sale\', add column if not exists "created_at" timestamptz not null default now(), add column if not exists "updated_at" timestamptz not null default now(), add column if not exists "deleted_at" timestamptz null;' - ) - - this.addSql(` - UPDATE "price_list" - SET title = name - `) - - this.addSql(`alter table "price_list" alter column "title" set not null `) - - this.addSql( - 'create index if not exists "IDX_price_list_deleted_at" on "price_list" ("deleted_at");' - ) - - this.addSql( - 'alter table "price_list_rule" drop constraint if exists "IDX_price_list_rule_rule_type_id_price_list_id_unique"' - ) - - this.addSql( - 'alter table "price_list_rule" add constraint "IDX_price_list_rule_rule_type_id_price_list_id_unique" unique ("price_list_id", "rule_type_id");' - ) - - this.addSql( - 'alter table "money_amount" add column if not exists "created_at" timestamptz not null default now(), add column if not exists "updated_at" timestamptz not null default now(), add column if not exists "deleted_at" timestamptz null;' - ) - - this.addSql( - 'create index if not exists "IDX_money_amount_deleted_at" on "money_amount" ("deleted_at");' - ) - - this.addSql( - 'alter table "price_set_money_amount" add constraint "price_set_money_amount_money_amount_id_unique" unique ("money_amount_id");' - ) - } - - async down(): Promise { - this.addSql('drop table if exists "price_list_rule_value" cascade;') - - this.addSql(`ALTER TABLE price_list DROP COLUMN IF EXISTS rules_count`) - - this.addSql('alter table "price_list" drop column if exists "title";') - - this.addSql('alter table "price_list" drop column if exists "description";') - - this.addSql('alter table "price_list" drop column if exists "type";') - - this.addSql( - 'alter table "price_set_money_amount" drop constraint "price_set_money_amount_price_list_id_foreign";' - ) - - this.addSql( - 'alter table "price_set_money_amount" add constraint "price_set_money_amount_price_list_id_foreign" foreign key ("price_list_id") references "price_list" ("id") on update cascade on delete set null;' - ) - - this.addSql('drop index if exists "IDX_price_list_deleted_at";') - - this.addSql('alter table "price_list" drop column if exists "title";') - - this.addSql('alter table "price_list" drop column if exists "description";') - - this.addSql('alter table "price_list" drop column if exists "type";') - - this.addSql('alter table "price_list" drop column if exists "created_at";') - - this.addSql('alter table "price_list" drop column if exists "updated_at";') - - this.addSql('alter table "price_list" drop column if exists "deleted_at";') - - this.addSql( - 'alter table "price_list_rule" drop constraint if exists "IDX_price_list_rule_rule_type_id_price_list_id_unique";' - ) - - this.addSql('drop index if exists "IDX_money_amount_deleted_at";') - - this.addSql( - 'alter table "money_amount" drop column if exists "created_at";' - ) - - this.addSql( - 'alter table "money_amount" drop column if exists "updated_at";' - ) - - this.addSql( - 'alter table "money_amount" drop column if exists "deleted_at";' - ) - - this.addSql( - 'alter table "price_set_money_amount" drop constraint if exists "price_set_money_amount_money_amount_id_unique";' - ) - } -} diff --git a/packages/pricing/src/migrations/Migration20240103140327.ts b/packages/pricing/src/migrations/Migration20240103140327.ts deleted file mode 100644 index c53c56d874..0000000000 --- a/packages/pricing/src/migrations/Migration20240103140327.ts +++ /dev/null @@ -1,55 +0,0 @@ -import { Migration } from "@mikro-orm/migrations" - -export class Migration20240103140327 extends Migration { - async up(): Promise { - this.addSql( - 'alter table "price_set_money_amount" drop constraint "price_set_money_amount_price_list_id_foreign";' - ) - - this.addSql( - 'alter table "price_set_money_amount" add column "created_at" timestamptz not null default now(), add column "updated_at" timestamptz not null default now(), add column "deleted_at" timestamptz null;' - ) - this.addSql( - 'alter table "price_set_money_amount" add constraint "price_set_money_amount_price_list_id_foreign" foreign key ("price_list_id") references "price_list" ("id") on delete cascade;' - ) - this.addSql( - 'create index "IDX_price_set_money_amount_deleted_at" on "price_set_money_amount" ("deleted_at");' - ) - - this.addSql( - 'alter table "price_rule" add column "created_at" timestamptz not null default now(), add column "updated_at" timestamptz not null default now(), add column "deleted_at" timestamptz null;' - ) - this.addSql('alter table "price_rule" drop column "is_dynamic";') - this.addSql( - 'create index "IDX_price_rule_deleted_at" on "price_rule" ("deleted_at");' - ) - } - - async down(): Promise { - this.addSql( - 'alter table "price_set_money_amount" drop constraint "price_set_money_amount_price_list_id_foreign";' - ) - - this.addSql('drop index "IDX_price_set_money_amount_deleted_at";') - this.addSql( - 'alter table "price_set_money_amount" drop column "created_at";' - ) - this.addSql( - 'alter table "price_set_money_amount" drop column "updated_at";' - ) - this.addSql( - 'alter table "price_set_money_amount" drop column "deleted_at";' - ) - this.addSql( - 'alter table "price_set_money_amount" add constraint "price_set_money_amount_price_list_id_foreign" foreign key ("price_list_id") references "price_list" ("id") on update cascade on delete cascade;' - ) - - this.addSql( - 'alter table "price_rule" add column "is_dynamic" boolean not null default false;' - ) - this.addSql('drop index "IDX_price_rule_deleted_at";') - this.addSql('alter table "price_rule" drop column "created_at";') - this.addSql('alter table "price_rule" drop column "updated_at";') - this.addSql('alter table "price_rule" drop column "deleted_at";') - } -} diff --git a/packages/pricing/src/models/index.ts b/packages/pricing/src/models/index.ts index 03385bee99..5d45ecb0ef 100644 --- a/packages/pricing/src/models/index.ts +++ b/packages/pricing/src/models/index.ts @@ -5,6 +5,5 @@ export { default as PriceListRuleValue } from "./price-list-rule-value" export { default as PriceRule } from "./price-rule" export { default as PriceSet } from "./price-set" export { default as PriceSetMoneyAmount } from "./price-set-money-amount" -export { default as PriceSetMoneyAmountRules } from "./price-set-money-amount-rules" export { default as PriceSetRuleType } from "./price-set-rule-type" export { default as RuleType } from "./rule-type" diff --git a/packages/pricing/src/models/money-amount.ts b/packages/pricing/src/models/money-amount.ts index b676659f7f..83a4fed3f1 100644 --- a/packages/pricing/src/models/money-amount.ts +++ b/packages/pricing/src/models/money-amount.ts @@ -1,60 +1,53 @@ -import { DALUtils, generateEntityId } from "@medusajs/utils" +import { DAL } from "@medusajs/types" +import { + DALUtils, + createPsqlIndexStatementHelper, + generateEntityId, +} from "@medusajs/utils" import { BeforeCreate, - Collection, Entity, Filter, - Index, - ManyToMany, OnInit, OneToOne, OptionalProps, PrimaryKey, Property, } from "@mikro-orm/core" - import { PriceSetMoneyAmount } from "./index" -import PriceSet from "./price-set" -@Entity() +type OptionalFields = DAL.SoftDeletableEntityDateColumns + +const tableName = "money_amount" +const MoneyAmountDeletedAtIndex = createPsqlIndexStatementHelper({ + tableName: tableName, + columns: "deleted_at", + where: "deleted_at IS NOT NULL", +}) + +const MoneyAmountCurrencyCodeIndex = createPsqlIndexStatementHelper({ + tableName: tableName, + columns: "currency_code", + where: "deleted_at IS NULL", +}) + +@Entity({ tableName }) @Filter(DALUtils.mikroOrmSoftDeletableFilterOptions) class MoneyAmount { - [OptionalProps]?: - | "created_at" - | "updated_at" - | "deleted_at" - | "price_set_money_amount" - | "amount" + [OptionalProps]?: OptionalFields @PrimaryKey({ columnType: "text" }) id!: string - @Property({ - columnType: "text", - nullable: true, - index: "IDX_money_amount_currency_code", - }) - currency_code: string | null - - @ManyToMany({ - entity: () => PriceSet, - mappedBy: (ps) => ps.money_amounts, - }) - price_sets = new Collection(this) - - @OneToOne({ - entity: () => PriceSetMoneyAmount, - mappedBy: (psma) => psma.money_amount, - cascade: ["soft-remove"] as any, - }) - price_set_money_amount: PriceSetMoneyAmount + @MoneyAmountCurrencyCodeIndex.MikroORMIndex() + @Property({ columnType: "text" }) + currency_code: string @Property({ columnType: "numeric", - nullable: true, serializer: Number, }) - amount: number | null + amount: number @Property({ columnType: "numeric", nullable: true }) min_quantity: number | null @@ -62,6 +55,13 @@ class MoneyAmount { @Property({ columnType: "numeric", nullable: true }) max_quantity: number | null + @OneToOne({ + entity: () => PriceSetMoneyAmount, + mappedBy: (psma) => psma.money_amount, + onDelete: "cascade", + }) + price_set_money_amount: PriceSetMoneyAmount + @Property({ onCreate: () => new Date(), columnType: "timestamptz", @@ -77,9 +77,9 @@ class MoneyAmount { }) updated_at: Date - @Index({ name: "IDX_money_amount_deleted_at" }) + @MoneyAmountDeletedAtIndex.MikroORMIndex() @Property({ columnType: "timestamptz", nullable: true }) - deleted_at: Date | null + deleted_at: Date | null = null @BeforeCreate() onCreate() { diff --git a/packages/pricing/src/models/price-list-rule-value.ts b/packages/pricing/src/models/price-list-rule-value.ts index 58f7cb0bfc..ecaedc1a7f 100644 --- a/packages/pricing/src/models/price-list-rule-value.ts +++ b/packages/pricing/src/models/price-list-rule-value.ts @@ -1,30 +1,73 @@ +import { DAL } from "@medusajs/types" +import { + DALUtils, + createPsqlIndexStatementHelper, + generateEntityId, +} from "@medusajs/utils" import { BeforeCreate, Entity, + Filter, ManyToOne, OnInit, + OptionalProps, PrimaryKey, Property, } from "@mikro-orm/core" - import PriceListRule from "./price-list-rule" -import { generateEntityId } from "@medusajs/utils" -@Entity() +type OptionalFields = DAL.SoftDeletableEntityDateColumns + +const tableName = "price_list_rule_value" +const PriceListRuleValueDeletedAtIndex = createPsqlIndexStatementHelper({ + tableName: tableName, + columns: "deleted_at", + where: "deleted_at IS NOT NULL", +}) + +const PriceListPriceListRuleIdIndex = createPsqlIndexStatementHelper({ + tableName: tableName, + columns: "price_list_rule_id", + where: "deleted_at IS NULL", +}) + +@Entity({ tableName }) +@Filter(DALUtils.mikroOrmSoftDeletableFilterOptions) export default class PriceListRuleValue { + [OptionalProps]?: OptionalFields + @PrimaryKey({ columnType: "text" }) id!: string + @PriceListPriceListRuleIdIndex.MikroORMIndex() @ManyToOne(() => PriceListRule, { onDelete: "cascade", fieldName: "price_list_rule_id", - index: "IDX_price_list_rule_price_list_rule_value_id", }) price_list_rule: PriceListRule @Property({ columnType: "text" }) value: string + @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 + + @PriceListRuleValueDeletedAtIndex.MikroORMIndex() + @Property({ columnType: "timestamptz", nullable: true }) + deleted_at: Date | null = null + @BeforeCreate() onCreate() { this.id = generateEntityId(this.id, "plrv") diff --git a/packages/pricing/src/models/price-list-rule.ts b/packages/pricing/src/models/price-list-rule.ts index 239048f18b..8633bc2cf1 100644 --- a/packages/pricing/src/models/price-list-rule.ts +++ b/packages/pricing/src/models/price-list-rule.ts @@ -1,56 +1,92 @@ +import { DAL } from "@medusajs/types" +import { + DALUtils, + createPsqlIndexStatementHelper, + generateEntityId, +} from "@medusajs/utils" import { BeforeCreate, Cascade, Collection, Entity, + Filter, ManyToOne, - OneToMany, OnInit, + OneToMany, OptionalProps, PrimaryKey, - Unique, + Property, } from "@mikro-orm/core" - -import { generateEntityId } from "@medusajs/utils" import PriceList from "./price-list" import PriceListRuleValue from "./price-list-rule-value" import RuleType from "./rule-type" -type OptionalFields = "id" -type OptionalRelations = "rule_type" | "price_list" +type OptionalFields = DAL.SoftDeletableEntityDateColumns -@Entity() -@Unique({ - name: "IDX_price_list_rule_rule_type_id_price_list_id_unique", - properties: ["price_list", "rule_type"], +const tableName = "price_list_rule" +const PriceListRuleDeletedAtIndex = createPsqlIndexStatementHelper({ + tableName: tableName, + columns: "deleted_at", + where: "deleted_at IS NOT NULL", }) + +const PriceListRuleRuleTypeIdIndex = createPsqlIndexStatementHelper({ + tableName: tableName, + columns: "rule_type_id", + where: "deleted_at IS NULL", + unique: true, +}) + +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 | OptionalRelations + [OptionalProps]: OptionalFields @PrimaryKey({ columnType: "text" }) id!: string - @ManyToOne({ - entity: () => RuleType, - fieldName: "rule_type_id", - name: "price_rule_rule_type_id_unique", - index: "IDX_price_list_rule_rule_type_id", - }) + @PriceListRuleRuleTypeIdIndex.MikroORMIndex() + @ManyToOne({ entity: () => RuleType, fieldName: "rule_type_id" }) rule_type: RuleType @OneToMany(() => PriceListRuleValue, (plrv) => plrv.price_list_rule, { - cascade: [Cascade.REMOVE], + cascade: ["soft-remove" as Cascade], }) price_list_rule_values = new Collection(this) + @PriceListRulePriceListIdIndex.MikroORMIndex() @ManyToOne({ entity: () => PriceList, fieldName: "price_list_id", - name: "price_rule_price_list_id", - index: "IDX_price_list_rule_price_list_id", + onDelete: "cascade", }) price_list: 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") diff --git a/packages/pricing/src/models/price-list.ts b/packages/pricing/src/models/price-list.ts index 89468074e4..0175da3383 100644 --- a/packages/pricing/src/models/price-list.ts +++ b/packages/pricing/src/models/price-list.ts @@ -1,7 +1,10 @@ +import { DAL } from "@medusajs/types" import { - generateEntityId, + DALUtils, PriceListStatus, PriceListType, + createPsqlIndexStatementHelper, + generateEntityId, } from "@medusajs/utils" import { BeforeCreate, @@ -9,10 +12,10 @@ import { Collection, Entity, Enum, - Index, + Filter, ManyToMany, - OneToMany, OnInit, + OneToMany, OptionalProps, PrimaryKey, Property, @@ -22,23 +25,21 @@ import PriceSetMoneyAmount from "./price-set-money-amount" import RuleType from "./rule-type" type OptionalFields = - | "status" - | "type" - | "rules_count" | "starts_at" | "ends_at" - | "created_at" - | "updated_at" - | "deleted_at" + | DAL.SoftDeletableEntityDateColumns -type OptionalRelations = - | "price_set_money_amounts" - | "rule_types" - | "price_list_rules" +const tableName = "price_list" +const PriceListDeletedAtIndex = createPsqlIndexStatementHelper({ + tableName: tableName, + columns: "deleted_at", + where: "deleted_at IS NOT NULL", +}) -@Entity() +@Entity({ tableName }) +@Filter(DALUtils.mikroOrmSoftDeletableFilterOptions) export default class PriceList { - [OptionalProps]: OptionalFields | OptionalRelations + [OptionalProps]: OptionalFields @PrimaryKey({ columnType: "text" }) id!: string @@ -59,33 +60,32 @@ export default class PriceList { columnType: "timestamptz", nullable: true, }) - starts_at: Date | null + starts_at: Date | null = null @Property({ columnType: "timestamptz", nullable: true, }) - ends_at: Date | null + ends_at: Date | null = null @OneToMany(() => PriceSetMoneyAmount, (psma) => psma.price_list, { - cascade: [Cascade.REMOVE], + cascade: ["soft-remove" as Cascade], }) price_set_money_amounts = new Collection(this) @OneToMany(() => PriceListRule, (pr) => pr.price_list, { - cascade: [Cascade.REMOVE], + cascade: ["soft-remove" as Cascade], }) price_list_rules = new Collection(this) @ManyToMany({ entity: () => RuleType, pivotEntity: () => PriceListRule, - cascade: [Cascade.REMOVE], }) rule_types = new Collection(this) @Property({ columnType: "integer", default: 0 }) - rules_count?: number + rules_count: number = 0 @Property({ onCreate: () => new Date(), @@ -102,9 +102,9 @@ export default class PriceList { }) updated_at: Date - @Index({ name: "IDX_price_list_deleted_at" }) + @PriceListDeletedAtIndex.MikroORMIndex() @Property({ columnType: "timestamptz", nullable: true }) - deleted_at: Date | null + deleted_at: Date | null = null @BeforeCreate() onCreate() { diff --git a/packages/pricing/src/models/price-rule.ts b/packages/pricing/src/models/price-rule.ts index d0e6a7c310..34eb3ab59a 100644 --- a/packages/pricing/src/models/price-rule.ts +++ b/packages/pricing/src/models/price-rule.ts @@ -1,66 +1,81 @@ +import { DAL } from "@medusajs/types" +import { + DALUtils, + createPsqlIndexStatementHelper, + generateEntityId, +} from "@medusajs/utils" import { BeforeCreate, Entity, Filter, - Index, ManyToOne, OnInit, OptionalProps, PrimaryKey, Property, } from "@mikro-orm/core" -import { DALUtils, generateEntityId } from "@medusajs/utils" - import PriceSet from "./price-set" import PriceSetMoneyAmount from "./price-set-money-amount" import RuleType from "./rule-type" -type OptionalFields = - | "id" - | "is_dynamic" - | "priority" - | "created_at" - | "updated_at" - | "deleted_at" -type OptionalRelations = "price_set" | "rule_type" | "price_set_money_amount" +type OptionalFields = DAL.SoftDeletableEntityDateColumns -@Entity() +const tableName = "price_rule" +const PriceRuleDeletedAtIndex = createPsqlIndexStatementHelper({ + tableName: tableName, + columns: "deleted_at", + where: "deleted_at IS NOT NULL", +}) + +const PriceRulePriceSetIdIndex = createPsqlIndexStatementHelper({ + tableName: tableName, + columns: "price_set_id", + where: "deleted_at IS NULL", +}) + +const PriceRuleRuleTypeIdIndex = createPsqlIndexStatementHelper({ + tableName: tableName, + columns: "rule_type_id", + where: "deleted_at IS NULL", +}) + +const PriceRulePriceSetMoneyAmountIdIndex = createPsqlIndexStatementHelper({ + tableName: tableName, + columns: "price_set_money_amount_id", + where: "deleted_at IS NULL", + unique: true, +}) + +@Entity({ tableName }) @Filter(DALUtils.mikroOrmSoftDeletableFilterOptions) export default class PriceRule { - [OptionalProps]: OptionalFields | OptionalRelations + [OptionalProps]?: OptionalFields @PrimaryKey({ columnType: "text" }) id!: string + @PriceRulePriceSetIdIndex.MikroORMIndex() @ManyToOne({ entity: () => PriceSet, fieldName: "price_set_id", - name: "price_rule_price_set_id_unique", onDelete: "cascade", - index: "IDX_price_rule_price_set_id", }) price_set: PriceSet - @ManyToOne({ - entity: () => RuleType, - fieldName: "rule_type_id", - name: "price_rule_rule_type_id_unique", - index: "IDX_price_rule_rule_type_id", - }) + @PriceRuleRuleTypeIdIndex.MikroORMIndex() + @ManyToOne({ entity: () => RuleType }) rule_type: RuleType @Property({ columnType: "text" }) value: string @Property({ columnType: "integer", default: 0 }) - priority: number + priority: number = 0 + @PriceRulePriceSetMoneyAmountIdIndex.MikroORMIndex() @ManyToOne({ onDelete: "cascade", entity: () => PriceSetMoneyAmount, - fieldName: "price_set_money_amount_id", - name: "price_set_money_amount_id_unique", - index: "IDX_price_rule_price_set_money_amount_id", }) price_set_money_amount: PriceSetMoneyAmount @@ -79,9 +94,9 @@ export default class PriceRule { }) updated_at: Date - @Index({ name: "IDX_price_rule_deleted_at" }) + @PriceRuleDeletedAtIndex.MikroORMIndex() @Property({ columnType: "timestamptz", nullable: true }) - deleted_at: Date | null + deleted_at: Date | null = null @BeforeCreate() beforeCreate() { diff --git a/packages/pricing/src/models/price-set-money-amount-rules.ts b/packages/pricing/src/models/price-set-money-amount-rules.ts index 184ced8ebc..2324472cea 100644 --- a/packages/pricing/src/models/price-set-money-amount-rules.ts +++ b/packages/pricing/src/models/price-set-money-amount-rules.ts @@ -1,35 +1,82 @@ -import { generateEntityId } from "@medusajs/utils" +import { DAL } from "@medusajs/types" +import { + DALUtils, + createPsqlIndexStatementHelper, + generateEntityId, +} from "@medusajs/utils" import { BeforeCreate, Entity, + Filter, ManyToOne, OnInit, + OptionalProps, PrimaryKey, Property, } from "@mikro-orm/core" - import PriceSetMoneyAmount from "./price-set-money-amount" import RuleType from "./rule-type" -@Entity() +type OptionalFields = DAL.SoftDeletableEntityDateColumns + +const tableName = "price_set_money_amount_rules" +const PriceSetMoneyAmountRulesDeletedAtIndex = createPsqlIndexStatementHelper({ + tableName: tableName, + columns: "deleted_at", + where: "deleted_at IS NOT NULL", +}) + +const PriceSetMoneyAmountRulesPriceSetMoneyAmountIdIndex = + createPsqlIndexStatementHelper({ + tableName: tableName, + columns: "price_set_money_amount_id", + where: "deleted_at IS NULL", + }) + +const PriceSetMoneyAmountRulesRuleTypeIdIndex = createPsqlIndexStatementHelper({ + tableName: tableName, + columns: "rule_type_id", + where: "deleted_at IS NULL", +}) + +@Entity({ tableName }) +@Filter(DALUtils.mikroOrmSoftDeletableFilterOptions) export default class PriceSetMoneyAmountRules { + [OptionalProps]?: OptionalFields + @PrimaryKey({ columnType: "text" }) id!: string - @ManyToOne(() => PriceSetMoneyAmount, { - onDelete: "cascade", - index: "IDX_price_set_money_amount_rules_price_set_money_amount_id", - }) + @PriceSetMoneyAmountRulesPriceSetMoneyAmountIdIndex.MikroORMIndex() + @ManyToOne(() => PriceSetMoneyAmount, { onDelete: "cascade" }) price_set_money_amount: PriceSetMoneyAmount - @ManyToOne(() => RuleType, { - index: "IDX_price_set_money_amount_rules_rule_type_id", - }) + @PriceSetMoneyAmountRulesRuleTypeIdIndex.MikroORMIndex() + @ManyToOne(() => RuleType, { onDelete: "cascade" }) rule_type: RuleType @Property({ columnType: "text" }) value: string + @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 + + @PriceSetMoneyAmountRulesDeletedAtIndex.MikroORMIndex() + @Property({ columnType: "timestamptz", nullable: true }) + deleted_at: Date | null = null + @BeforeCreate() onCreate() { this.id = generateEntityId(this.id, "psmar") diff --git a/packages/pricing/src/models/price-set-money-amount.ts b/packages/pricing/src/models/price-set-money-amount.ts index 1198e9a95c..0cd2020426 100644 --- a/packages/pricing/src/models/price-set-money-amount.ts +++ b/packages/pricing/src/models/price-set-money-amount.ts @@ -1,3 +1,9 @@ +import { DAL } from "@medusajs/types" +import { + DALUtils, + createPsqlIndexStatementHelper, + generateEntityId, +} from "@medusajs/utils" import { BeforeCreate, Cascade, @@ -5,67 +11,76 @@ import { Entity, Filter, ManyToOne, + OnInit, OneToMany, OneToOne, - OnInit, OptionalProps, PrimaryKey, - PrimaryKeyType, Property, } from "@mikro-orm/core" -import { DALUtils, generateEntityId } from "@medusajs/utils" - import MoneyAmount from "./money-amount" import PriceList from "./price-list" import PriceRule from "./price-rule" import PriceSet from "./price-set" -import PriceSetMoneyAmountRules from "./price-set-money-amount-rules" -@Entity() +type OptionalFields = DAL.SoftDeletableEntityDateColumns + +const tableName = "price_set_money_amount" +const PriceSetMoneyAmountDeletedAtIndex = createPsqlIndexStatementHelper({ + tableName: tableName, + columns: "deleted_at", + where: "deleted_at IS NOT NULL", +}) + +const PriceSetMoneyAmountPriceSetIdIndex = createPsqlIndexStatementHelper({ + tableName: tableName, + columns: "price_set_id", + where: "deleted_at IS NULL", +}) + +const PriceSetMoneyAmountMoneyAmountIdIndex = createPsqlIndexStatementHelper({ + tableName: tableName, + columns: "money_amount_id", + where: "deleted_at IS NULL", +}) + +const PriceSetMoneyAmountPriceListIdIndex = createPsqlIndexStatementHelper({ + tableName: tableName, + columns: "price_list_id", + where: "deleted_at IS NULL", +}) + +@Entity({ tableName }) @Filter(DALUtils.mikroOrmSoftDeletableFilterOptions) export default class PriceSetMoneyAmount { - [OptionalProps]?: "created_at" | "updated_at" | "deleted_at" + [OptionalProps]?: OptionalFields @PrimaryKey({ columnType: "text" }) id!: string - @Property({ columnType: "text" }) - title!: string + @Property({ columnType: "text", nullable: true }) + title: string | null = null - @ManyToOne(() => PriceSet, { - onDelete: "cascade", - index: "IDX_price_set_money_amount_price_set_id", - }) + @PriceSetMoneyAmountPriceSetIdIndex.MikroORMIndex() + @ManyToOne(() => PriceSet, { onDelete: "cascade" }) price_set: PriceSet - @OneToOne(() => MoneyAmount, { - onDelete: "cascade", - index: "IDX_price_set_money_amount_money_amount_id", - }) + @PriceSetMoneyAmountMoneyAmountIdIndex.MikroORMIndex() + @OneToOne(() => MoneyAmount, { onDelete: "cascade" }) money_amount: MoneyAmount @Property({ columnType: "integer", default: 0 }) - rules_count: number + rules_count: number = 0 @OneToMany({ entity: () => PriceRule, mappedBy: (pr) => pr.price_set_money_amount, - cascade: ["soft-remove"] as any, + cascade: ["soft-remove" as Cascade], }) price_rules = new Collection(this) - @OneToMany({ - entity: () => PriceSetMoneyAmountRules, - mappedBy: (psmar) => psmar.price_set_money_amount, - }) - price_set_money_amount_rules = new Collection(this) - - @ManyToOne(() => PriceList, { - index: "IDX_price_rule_price_list_id", - onDelete: "cascade", - cascade: [Cascade.REMOVE, "soft-remove"] as any, - nullable: true, - }) + @PriceSetMoneyAmountPriceListIdIndex.MikroORMIndex() + @ManyToOne(() => PriceList, { onDelete: "cascade", nullable: true }) price_list: PriceList | null @Property({ @@ -83,12 +98,9 @@ export default class PriceSetMoneyAmount { }) updated_at: Date - @Property({ - columnType: "timestamptz", - nullable: true, - index: "IDX_price_set_money_amount_deleted_at", - }) - deleted_at: Date | null + @PriceSetMoneyAmountDeletedAtIndex.MikroORMIndex() + @Property({ columnType: "timestamptz", nullable: true }) + deleted_at: Date | null = null @BeforeCreate() onCreate() { @@ -99,11 +111,4 @@ export default class PriceSetMoneyAmount { onInit() { this.id = generateEntityId(this.id, "psma") } - - [PrimaryKeyType]?: [string, string] - - constructor(money_amount: MoneyAmount, price_set: PriceSet) { - this.money_amount = money_amount - this.price_set = price_set - } } diff --git a/packages/pricing/src/models/price-set-rule-type.ts b/packages/pricing/src/models/price-set-rule-type.ts index 0cb27b3b2d..458867a63e 100644 --- a/packages/pricing/src/models/price-set-rule-type.ts +++ b/packages/pricing/src/models/price-set-rule-type.ts @@ -1,31 +1,72 @@ +import { + DALUtils, + createPsqlIndexStatementHelper, + generateEntityId, +} from "@medusajs/utils" import { BeforeCreate, Entity, + Filter, ManyToOne, OnInit, PrimaryKey, + Property, } from "@mikro-orm/core" - import PriceSet from "./price-set" import RuleType from "./rule-type" -import { generateEntityId } from "@medusajs/utils" -@Entity() +const tableName = "price_set_rule_type" +const PriceSetRuleTypeDeletedAtIndex = createPsqlIndexStatementHelper({ + tableName: tableName, + columns: "deleted_at", + where: "deleted_at IS NOT NULL", +}) + +const PriceSetRuleTypePriceSetIdIndex = createPsqlIndexStatementHelper({ + tableName: tableName, + columns: "price_set_id", + where: "deleted_at IS NULL", +}) + +const PriceSetRuleTypeRuleTypeIdIndex = createPsqlIndexStatementHelper({ + tableName: tableName, + columns: "rule_type_id", + where: "deleted_at IS NULL", +}) + +@Entity({ tableName }) +@Filter(DALUtils.mikroOrmSoftDeletableFilterOptions) export default class PriceSetRuleType { @PrimaryKey({ columnType: "text" }) id!: string - @ManyToOne(() => PriceSet, { - onDelete: "cascade", - index: "IDX_price_set_rule_type_price_set_id", - }) + @PriceSetRuleTypePriceSetIdIndex.MikroORMIndex() + @ManyToOne(() => PriceSet, { onDelete: "cascade" }) price_set: PriceSet - @ManyToOne(() => RuleType, { - index: "IDX_price_set_rule_type_rule_type_id", - }) + @PriceSetRuleTypeRuleTypeIdIndex.MikroORMIndex() + @ManyToOne(() => RuleType, { onDelete: "cascade" }) rule_type: RuleType + @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 + + @PriceSetRuleTypeDeletedAtIndex.MikroORMIndex() + @Property({ columnType: "timestamptz", nullable: true }) + deleted_at: Date | null = null + @BeforeCreate() onCreate() { this.id = generateEntityId(this.id, "psrt") diff --git a/packages/pricing/src/models/price-set.ts b/packages/pricing/src/models/price-set.ts index ebe2264081..6727dbd218 100644 --- a/packages/pricing/src/models/price-set.ts +++ b/packages/pricing/src/models/price-set.ts @@ -1,52 +1,74 @@ -import { generateEntityId } from "@medusajs/utils" +import { + DALUtils, + createPsqlIndexStatementHelper, + generateEntityId, +} from "@medusajs/utils" import { BeforeCreate, Cascade, Collection, Entity, + Filter, ManyToMany, - OneToMany, OnInit, - OptionalProps, + OneToMany, PrimaryKey, + Property, } from "@mikro-orm/core" - -import MoneyAmount from "./money-amount" import PriceRule from "./price-rule" import PriceSetMoneyAmount from "./price-set-money-amount" import PriceSetRuleType from "./price-set-rule-type" import RuleType from "./rule-type" -@Entity() -export default class PriceSet { - [OptionalProps]?: "price_set_money_amounts" | "rule_types" | "money_amounts" +const tableName = "price_set" +const PriceSetDeletedAtIndex = createPsqlIndexStatementHelper({ + tableName: tableName, + columns: "deleted_at", + where: "deleted_at IS NOT NULL", +}) +@Entity({ tableName }) +@Filter(DALUtils.mikroOrmSoftDeletableFilterOptions) +export default class PriceSet { @PrimaryKey({ columnType: "text" }) id!: string @OneToMany(() => PriceSetMoneyAmount, (psma) => psma.price_set, { - cascade: [Cascade.REMOVE], + cascade: ["soft-remove" as Cascade], }) price_set_money_amounts = new Collection(this) @OneToMany(() => PriceRule, (pr) => pr.price_set, { - cascade: [Cascade.REMOVE], + cascade: ["soft-remove" as Cascade], }) price_rules = new Collection(this) - @ManyToMany({ - entity: () => MoneyAmount, - pivotEntity: () => PriceSetMoneyAmount, - }) - money_amounts = new Collection(this) - @ManyToMany({ entity: () => RuleType, pivotEntity: () => PriceSetRuleType, - cascade: [Cascade.REMOVE], + cascade: ["soft-remove" as Cascade], }) rule_types = new Collection(this) + @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 + + @PriceSetDeletedAtIndex.MikroORMIndex() + @Property({ columnType: "timestamptz", nullable: true }) + deleted_at: Date | null = null + @BeforeCreate() onCreate() { this.id = generateEntityId(this.id, "pset") diff --git a/packages/pricing/src/models/rule-type.ts b/packages/pricing/src/models/rule-type.ts index 9a98395fac..95777416c9 100644 --- a/packages/pricing/src/models/rule-type.ts +++ b/packages/pricing/src/models/rule-type.ts @@ -1,8 +1,13 @@ -import { generateEntityId } from "@medusajs/utils" +import { + DALUtils, + createPsqlIndexStatementHelper, + generateEntityId, +} from "@medusajs/utils" import { BeforeCreate, Collection, Entity, + Filter, ManyToMany, OnInit, OptionalProps, @@ -13,7 +18,21 @@ import PriceSet from "./price-set" type OptionalFields = "default_priority" -@Entity() +const tableName = "rule_type" +const RuleTypeDeletedAtIndex = createPsqlIndexStatementHelper({ + tableName: tableName, + columns: "deleted_at", + where: "deleted_at IS NOT NULL", +}) + +const RuleTypeRuleAttributeIndex = createPsqlIndexStatementHelper({ + tableName: tableName, + columns: "rule_attribute", + where: "deleted_at IS NULL", +}) + +@Entity({ tableName }) +@Filter(DALUtils.mikroOrmSoftDeletableFilterOptions) class RuleType { [OptionalProps]?: OptionalFields @@ -23,7 +42,8 @@ class RuleType { @Property({ columnType: "text" }) name: string - @Property({ columnType: "text", index: "IDX_rule_type_rule_attribute" }) + @RuleTypeRuleAttributeIndex.MikroORMIndex() + @Property({ columnType: "text" }) rule_attribute: string @Property({ columnType: "integer", default: 0 }) @@ -47,6 +67,10 @@ class RuleType { }) updated_at: Date + @RuleTypeDeletedAtIndex.MikroORMIndex() + @Property({ columnType: "timestamptz", nullable: true }) + deleted_at: Date | null = null + @BeforeCreate() onCreate() { this.id = generateEntityId(this.id, "rul-typ") diff --git a/packages/pricing/src/services/pricing-module.ts b/packages/pricing/src/services/pricing-module.ts index c6ea777992..8f3b45c314 100644 --- a/packages/pricing/src/services/pricing-module.ts +++ b/packages/pricing/src/services/pricing-module.ts @@ -35,7 +35,6 @@ import { PriceRule, PriceSet, PriceSetMoneyAmount, - PriceSetMoneyAmountRules, PriceSetRuleType, RuleType, } from "@models" @@ -56,7 +55,6 @@ type InjectedDependencies = { pricingRepository: PricingRepositoryService moneyAmountService: ModulesSdkTypes.InternalModuleService priceSetService: ModulesSdkTypes.InternalModuleService - priceSetMoneyAmountRulesService: ModulesSdkTypes.InternalModuleService ruleTypeService: RuleTypeService priceRuleService: PriceRuleService priceSetRuleTypeService: ModulesSdkTypes.InternalModuleService @@ -73,7 +71,6 @@ const generateMethodForModels = [ PriceListRuleValue, PriceRule, PriceSetMoneyAmount, - PriceSetMoneyAmountRules, PriceSetRuleType, RuleType, ] @@ -82,7 +79,6 @@ export default class PricingModuleService< TPriceSet extends PriceSet = PriceSet, TMoneyAmount extends MoneyAmount = MoneyAmount, TRuleType extends RuleType = RuleType, - TPriceSetMoneyAmountRules extends PriceSetMoneyAmountRules = PriceSetMoneyAmountRules, TPriceRule extends PriceRule = PriceRule, TPriceSetRuleType extends PriceSetRuleType = PriceSetRuleType, TPriceSetMoneyAmount extends PriceSetMoneyAmount = PriceSetMoneyAmount, @@ -100,10 +96,11 @@ export default class PricingModuleService< update: PricingTypes.UpdateMoneyAmountDTO } PriceSetMoneyAmount: { dto: PricingTypes.PriceSetMoneyAmountDTO } - PriceSetMoneyAmountRules: { - dto: PricingTypes.PriceSetMoneyAmountRulesDTO + PriceRule: { + dto: PricingTypes.PriceRuleDTO + create: PricingTypes.CreatePriceRuleDTO + update: PricingTypes.UpdatePriceRuleDTO } - PriceRule: { dto: PricingTypes.PriceRuleDTO } RuleType: { dto: PricingTypes.RuleTypeDTO create: PricingTypes.CreateRuleTypeDTO @@ -120,7 +117,6 @@ export default class PricingModuleService< protected readonly moneyAmountService_: ModulesSdkTypes.InternalModuleService protected readonly ruleTypeService_: RuleTypeService protected readonly priceSetService_: ModulesSdkTypes.InternalModuleService - protected readonly priceSetMoneyAmountRulesService_: ModulesSdkTypes.InternalModuleService protected readonly priceRuleService_: PriceRuleService protected readonly priceSetRuleTypeService_: ModulesSdkTypes.InternalModuleService protected readonly priceSetMoneyAmountService_: ModulesSdkTypes.InternalModuleService @@ -135,7 +131,6 @@ export default class PricingModuleService< moneyAmountService, ruleTypeService, priceSetService, - priceSetMoneyAmountRulesService, priceRuleService, priceSetRuleTypeService, priceSetMoneyAmountService, @@ -153,7 +148,6 @@ export default class PricingModuleService< this.moneyAmountService_ = moneyAmountService this.ruleTypeService_ = ruleTypeService this.priceSetService_ = priceSetService - this.priceSetMoneyAmountRulesService_ = priceSetMoneyAmountRulesService this.ruleTypeService_ = ruleTypeService this.priceRuleService_ = priceRuleService this.priceSetRuleTypeService_ = priceSetRuleTypeService @@ -259,7 +253,11 @@ export default class PricingModuleService< const dbPriceSets = await this.list( { id: priceSets.filter((p) => !!p).map((p) => p!.id) }, { - relations: ["rule_types", "money_amounts", "price_rules"], + relations: [ + "rule_types", + "price_set_money_amounts.money_amount", + "price_rules", + ], }, sharedContext ) @@ -538,7 +536,7 @@ export default class PricingModuleService< return (await this.list( { id: input.map((d) => d.priceSetId) }, - { relations: ["money_amounts"] }, + { relations: ["price_set_money_amounts"] }, sharedContext )) as unknown as PricingTypes.PriceSetDTO[] | PricingTypes.PriceSetDTO } @@ -713,73 +711,6 @@ export default class PricingModuleService< ) } - @InjectTransactionManager("baseRepository_") - async createPriceSetMoneyAmountRules( - data: PricingTypes.CreatePriceSetMoneyAmountRulesDTO[], - @MedusaContext() sharedContext: Context = {} - ): Promise { - const records = await this.priceSetMoneyAmountRulesService_.create( - data, - sharedContext - ) - - return await this.baseRepository_.serialize< - PricingTypes.PriceSetMoneyAmountRulesDTO[] - >(records, { - populate: true, - }) - } - - @InjectTransactionManager("baseRepository_") - async updatePriceSetMoneyAmountRules( - data: PricingTypes.UpdatePriceSetMoneyAmountRulesDTO[], - @MedusaContext() sharedContext: Context = {} - ): Promise { - const records = await this.priceSetMoneyAmountRulesService_.update( - data, - sharedContext - ) - - return await this.baseRepository_.serialize< - PricingTypes.PriceSetMoneyAmountRulesDTO[] - >(records, { - populate: true, - }) - } - - @InjectTransactionManager("baseRepository_") - async createPriceRules( - data: PricingTypes.CreatePriceRuleDTO[], - @MedusaContext() sharedContext: Context = {} - ): Promise { - const priceRules = await this.priceRuleService_.create( - data as ServiceTypes.CreatePriceRuleDTO[], - sharedContext - ) - - return await this.baseRepository_.serialize( - priceRules, - { - populate: true, - } - ) - } - - @InjectTransactionManager("baseRepository_") - async updatePriceRules( - data: PricingTypes.UpdatePriceRuleDTO[], - @MedusaContext() sharedContext: Context = {} - ): Promise { - const priceRules = await this.priceRuleService_.update(data, sharedContext) - - return await this.baseRepository_.serialize( - priceRules, - { - populate: true, - } - ) - } - @InjectManager("baseRepository_") async createPriceLists( data: PricingTypes.CreatePriceListDTO[], diff --git a/packages/pricing/src/types/repositories/index.ts b/packages/pricing/src/types/repositories/index.ts index af1c5d0f79..a69b66b436 100644 --- a/packages/pricing/src/types/repositories/index.ts +++ b/packages/pricing/src/types/repositories/index.ts @@ -1,10 +1,9 @@ export * from "./money-amount" -export * from "./price-list-rule-value" -export * from "./price-list-rule" export * from "./price-list" +export * from "./price-list-rule" +export * from "./price-list-rule-value" export * from "./price-rule" -export * from "./price-set-money-amount-rules" +export * from "./price-set" export * from "./price-set-money-amount" export * from "./price-set-rule-type" -export * from "./price-set" export * from "./rule-type" diff --git a/packages/pricing/src/types/repositories/price-set-money-amount-rules.ts b/packages/pricing/src/types/repositories/price-set-money-amount-rules.ts deleted file mode 100644 index f41a050f3a..0000000000 --- a/packages/pricing/src/types/repositories/price-set-money-amount-rules.ts +++ /dev/null @@ -1,12 +0,0 @@ -export interface CreatePriceSetMoneyAmountRulesDTO { - price_set_money_amount: string - rule_type: string - value: string -} - -export interface UpdatePriceSetMoneyAmountRulesDTO { - id: string - price_set_money_amount?: string - rule_type?: string - value?: string -} diff --git a/packages/pricing/src/types/services/index.ts b/packages/pricing/src/types/services/index.ts index 428c763739..611eaef3e5 100644 --- a/packages/pricing/src/types/services/index.ts +++ b/packages/pricing/src/types/services/index.ts @@ -1,11 +1,10 @@ export * from "./money-amount" -export * from "./price-list-rule-value" -export * from "./price-list-rule" export * from "./price-list" +export * from "./price-list-rule" +export * from "./price-list-rule-value" export * from "./price-rule" -export * from "./price-set-money-amount-rules" +export * from "./price-set" export * from "./price-set-money-amount" export * from "./price-set-rule-type" -export * from "./price-set" export * from "./pricing" export * from "./rule-type" diff --git a/packages/pricing/src/types/services/price-set-money-amount-rules.ts b/packages/pricing/src/types/services/price-set-money-amount-rules.ts deleted file mode 100644 index 562d8a22e0..0000000000 --- a/packages/pricing/src/types/services/price-set-money-amount-rules.ts +++ /dev/null @@ -1,33 +0,0 @@ -import { - BaseFilterable, - PriceSetMoneyAmountDTO, - RuleTypeDTO, -} from "@medusajs/types" - -export interface CreatePriceSetMoneyAmountRulesDTO { - price_set_money_amount: string - rule_type: string - value: string -} - -export interface UpdatePriceSetMoneyAmountRulesDTO { - id: string - price_set_money_amount?: string - rule_type?: string - value?: string -} - -export interface PriceSetMoneyAmountRulesDTO { - id: string - price_set_money_amount: PriceSetMoneyAmountDTO - rule_type: RuleTypeDTO - value: string -} - -export interface FilterablePriceSetMoneyAmountRulesProps - extends BaseFilterable { - id?: string[] - rule_type_id?: string[] - price_set_money_amount_id?: string[] - value?: string[] -} diff --git a/packages/types/src/pricing/common/index.ts b/packages/types/src/pricing/common/index.ts index ef108cf3bb..fc553870b4 100644 --- a/packages/types/src/pricing/common/index.ts +++ b/packages/types/src/pricing/common/index.ts @@ -1,8 +1,7 @@ export * from "./money-amount" +export * from "./price-list" export * from "./price-rule" export * from "./price-set" export * from "./price-set-money-amount" -export * from "./price-set-money-amount-rules" export * from "./price-set-rule-type" export * from "./rule-type" -export * from "./price-list" diff --git a/packages/types/src/pricing/common/price-set-money-amount-rules.ts b/packages/types/src/pricing/common/price-set-money-amount-rules.ts deleted file mode 100644 index 79b5daef1d..0000000000 --- a/packages/types/src/pricing/common/price-set-money-amount-rules.ts +++ /dev/null @@ -1,100 +0,0 @@ -import { BaseFilterable } from "../../dal" -import { PriceSetMoneyAmountDTO } from "./price-set-money-amount" -import { RuleTypeDTO } from "./rule-type" - -/** - * @interface - * - * A price set money amount rule's data. - */ -export interface PriceSetMoneyAmountRulesDTO { - /** - * The ID of the price set money amount. - */ - id: string - /** - * The associated price set money amount. - * - * @expandable - */ - price_set_money_amount: PriceSetMoneyAmountDTO - /** - * The associated rule type. - * - * @expandable - */ - rule_type: RuleTypeDTO - /** - * The value of the price set money amount rule. - */ - value: string -} - -/** - * @interface - * - * The price set money amount rule to create. - */ -export interface CreatePriceSetMoneyAmountRulesDTO { - /** - * The ID of a price set money amount. - */ - price_set_money_amount: string - /** - * The ID of a rule type. - */ - rule_type: string - /** - * The value of the price set money amount rule. - */ - value: string -} - -/** - * @interface - * - * The data to update in a price set money amount rule. The `id` is used to identify which money amount to update. - */ -export interface UpdatePriceSetMoneyAmountRulesDTO { - /** - * The ID of the price set money amount rule to update. - */ - id: string - /** - * The ID of a price set money amount. - */ - price_set_money_amount?: string - /** - * The ID of a rule type. - */ - rule_type?: string - /** - * The value of the price set money amount rule. - */ - value?: string -} - -/** - * @interface - * - * Filters to apply on price set money amount rules. - */ -export interface FilterablePriceSetMoneyAmountRulesProps - extends BaseFilterable { - /** - * The ID to filter price set money amount rules by. - */ - id?: string[] - /** - * The IDs to filter the price set money amount rule's associated rule type. - */ - rule_type_id?: string[] - /** - * The IDs to filter the price set money amount rule's associated price set money amount. - */ - price_set_money_amount_id?: string[] - /** - * The value to filter price set money amount rules by. - */ - value?: string[] -} diff --git a/packages/types/src/pricing/service.ts b/packages/types/src/pricing/service.ts index 200f11cf9d..db36b7158c 100644 --- a/packages/types/src/pricing/service.ts +++ b/packages/types/src/pricing/service.ts @@ -8,14 +8,12 @@ import { CreatePriceListRuleDTO, CreatePriceRuleDTO, CreatePriceSetDTO, - CreatePriceSetMoneyAmountRulesDTO, CreateRuleTypeDTO, FilterableMoneyAmountProps, FilterablePriceListProps, FilterablePriceListRuleProps, FilterablePriceRuleProps, FilterablePriceSetMoneyAmountProps, - FilterablePriceSetMoneyAmountRulesProps, FilterablePriceSetProps, FilterableRuleTypeProps, MoneyAmountDTO, @@ -24,7 +22,6 @@ import { PriceRuleDTO, PriceSetDTO, PriceSetMoneyAmountDTO, - PriceSetMoneyAmountRulesDTO, PricingContext, PricingFilters, RemovePriceListRulesDTO, @@ -37,7 +34,6 @@ import { UpdatePriceListRuleDTO, UpdatePriceRuleDTO, UpdatePriceSetDTO, - UpdatePriceSetMoneyAmountRulesDTO, UpdateRuleTypeDTO, } from "./common" @@ -1697,272 +1693,6 @@ export interface IPricingModuleService extends IModuleService { */ deleteRuleTypes(ruleTypeIds: string[], sharedContext?: Context): Promise - /** - * This method is used to a price set money amount rule by its ID based on the provided configuration. - * - * @param {string} id - The ID of the price set money amount rule to retrieve. - * @param {FindConfig} config - - * The configurations determining how the price set money amount rule is retrieved. Its properties, such as `select` or `relations`, accept the - * attributes or relations associated with a price set money amount rule. - * @param {Context} sharedContext - A context used to share resources, such as transaction manager, between the application and the module. - * @returns {Promise} The retrieved price set money amount rule. - * - * @example - * A simple example that retrieves a price set money amount rule by its ID: - * - * ```ts - * import { - * initialize as initializePricingModule, - * } from "@medusajs/pricing" - * - * async function retrievePriceSetMoneyAmountRule (id: string) { - * const pricingService = await initializePricingModule() - * - * const priceSetMoneyAmountRule = await pricingService.retrievePriceSetMoneyAmountRules(id) - * - * // do something with the price set money amount rule or return it - * } - * ``` - * - * To specify relations that should be retrieved: - * - * ```ts - * import { - * initialize as initializePricingModule, - * } from "@medusajs/pricing" - * - * async function retrievePriceSetMoneyAmountRule (id: string) { - * const pricingService = await initializePricingModule() - * - * const priceSetMoneyAmountRule = await pricingService.retrievePriceSetMoneyAmountRules(id, { - * relations: ["price_set_money_amount"] - * }) - * - * // do something with the price set money amount rule or return it - * } - * ``` - */ - retrievePriceSetMoneyAmountRules( - id: string, - config?: FindConfig, - sharedContext?: Context - ): Promise - - /** - * This method is used to retrieve a paginated list of price set money amount rules based on optional filters and configuration. - * - * @param {FilterablePriceSetMoneyAmountRulesProps} filters - The filters to apply on the retrieved price set money amount rules. - * @param {FindConfig} config - - * The configurations determining how the price set money amount rules are retrieved. Its properties, such as `select` or `relations`, accept the - * attributes or relations associated with a price set money amount rule. - * @param {Context} sharedContext - A context used to share resources, such as transaction manager, between the application and the module. - * @returns {Promise} The list of price set money amount rules. - * - * @example - * - * To retrieve a list of price set money amount rules using their IDs: - * - * ```ts - * import { - * initialize as initializePricingModule, - * } from "@medusajs/pricing" - * - * async function retrievePriceSetMoneyAmountRules (id: string) { - * const pricingService = await initializePricingModule() - * - * const priceSetMoneyAmountRules = await pricingService.listPriceSetMoneyAmountRules({ - * id: [id] - * }) - * - * // do something with the price set money amount rules or return them - * } - * ``` - * - * To specify relations that should be retrieved within the price set money amount rules: - * - * ```ts - * import { - * initialize as initializePricingModule, - * } from "@medusajs/pricing" - * - * async function retrievePriceSetMoneyAmountRules (id: string) { - * const pricingService = await initializePricingModule() - * - * const priceSetMoneyAmountRules = await pricingService.listPriceSetMoneyAmountRules({ - * id: [id] - * }, { - * relations: ["price_set_money_amount"] - * }) - * - * // do something with the price set money amount rules or return them - * } - * ``` - * - * By default, only the first `15` records are retrieved. You can control pagination by specifying the `skip` and `take` properties of the `config` parameter: - * - * ```ts - * import { - * initialize as initializePricingModule, - * } from "@medusajs/pricing" - * - * async function retrievePriceSetMoneyAmountRules (id: string, skip: number, take: number) { - * const pricingService = await initializePricingModule() - * - * const priceSetMoneyAmountRules = await pricingService.listPriceSetMoneyAmountRules({ - * id: [id] - * }, { - * relations: ["price_set_money_amount"], - * skip, - * take - * }) - * - * // do something with the price set money amount rules or return them - * } - * ``` - * - * You can also use the `$and` or `$or` properties of the `filter` parameter to use and/or conditions in your filters. For example: - * - * ```ts - * import { - * initialize as initializePricingModule, - * } from "@medusajs/pricing" - * - * async function retrievePriceSetMoneyAmountRules (ids: string[], ruleTypeId: string[], skip: number, take: number) { - * const pricingService = await initializePricingModule() - * - * const priceSetMoneyAmountRules = await pricingService.listPriceSetMoneyAmountRules({ - * $and: [ - * { - * id: ids - * }, - * { - * rule_type_id: ruleTypeId - * } - * ] - * }, { - * relations: ["price_set_money_amount"], - * skip, - * take - * }) - * - * // do something with the price set money amount rules or return them - * } - * ``` - */ - listPriceSetMoneyAmountRules( - filters?: FilterablePriceSetMoneyAmountRulesProps, - config?: FindConfig, - sharedContext?: Context - ): Promise - - /** - * This method is used to retrieve a paginated list of price set money amount rules along with the total count of - * available price set money amount rules satisfying the provided filters. - * - * @param {FilterablePriceSetMoneyAmountRulesProps} filters - The filters to apply on the retrieved price set money amount rules. - * @param {FindConfig} config - - * The configurations determining how the price set money amount rules are retrieved. Its properties, such as `select` or `relations`, accept the - * attributes or relations associated with a price set money amount rule. - * @param {Context} sharedContext - A context used to share resources, such as transaction manager, between the application and the module. - * @returns {Promise<[PriceSetMoneyAmountRulesDTO[], number]>} The list of price set money amount rules and their total count. - * - * @example - * - * To retrieve a list of price set money amounts using their IDs: - * - * ```ts - * import { - * initialize as initializePricingModule, - * } from "@medusajs/pricing" - * - * async function retrievePriceSetMoneyAmountRules (id: string) { - * const pricingService = await initializePricingModule() - * - * const [priceSetMoneyAmountRules, count] = await pricingService.listAndCountPriceSetMoneyAmountRules({ - * id: [id] - * }) - * - * // do something with the price set money amount rules or return them - * } - * ``` - * - * To specify relations that should be retrieved within the price set money amount rules: - * - * ```ts - * import { - * initialize as initializePricingModule, - * } from "@medusajs/pricing" - * - * async function retrievePriceSetMoneyAmountRules (id: string) { - * const pricingService = await initializePricingModule() - * - * const [priceSetMoneyAmountRules, count] = await pricingService.listAndCountPriceSetMoneyAmountRules({ - * id: [id] - * }, { - * relations: ["price_set_money_amount"], - * }) - * - * // do something with the price set money amount rules or return them - * } - * ``` - * - * By default, only the first `15` records are retrieved. You can control pagination by specifying the `skip` and `take` properties of the `config` parameter: - * - * ```ts - * import { - * initialize as initializePricingModule, - * } from "@medusajs/pricing" - * - * async function retrievePriceSetMoneyAmountRules (id: string, skip: number, take: number) { - * const pricingService = await initializePricingModule() - * - * const [priceSetMoneyAmountRules, count] = await pricingService.listAndCountPriceSetMoneyAmountRules({ - * id: [id] - * }, { - * relations: ["price_set_money_amount"], - * skip, - * take - * }) - * - * // do something with the price set money amount rules or return them - * } - * ``` - * - * You can also use the `$and` or `$or` properties of the `filter` parameter to use and/or conditions in your filters. For example: - * - * ```ts - * import { - * initialize as initializePricingModule, - * } from "@medusajs/pricing" - * - * async function retrievePriceSetMoneyAmountRules (ids: string[], ruleTypeId: string[], skip: number, take: number) { - * const pricingService = await initializePricingModule() - * - * const [priceSetMoneyAmountRules, count] = await pricingService.listAndCountPriceSetMoneyAmountRules({ - * $and: [ - * { - * id: ids - * }, - * { - * rule_type_id: ruleTypeId - * } - * ] - * }, { - * relations: ["price_set_money_amount"], - * skip, - * take - * }) - * - * // do something with the price set money amount rules or return them - * } - * ``` - */ - listAndCountPriceSetMoneyAmountRules( - filters?: FilterablePriceSetMoneyAmountRulesProps, - config?: FindConfig, - sharedContext?: Context - ): Promise<[PriceSetMoneyAmountRulesDTO[], number]> - /** * This method is used to retrieve a paginated list of price set money amounts based on optional filters and configuration. * @@ -2070,6 +1800,20 @@ export interface IPricingModuleService extends IModuleService { sharedContext?: Context ): Promise + softDeletePriceSetMoneyAmounts< + TReturnableLinkableKeys extends string = string + >( + psmaIds: string[], + config?: SoftDeleteReturn, + sharedContext?: Context + ): Promise | void> + + restorePriceSetMoneyAmounts( + psmaIds: string[], + config?: RestoreReturn, + sharedContext?: Context + ): Promise | void> + /** * This method is used to retrieve a paginated list of price set money amounts along with the total count of * available price set money amounts satisfying the provided filters. @@ -2178,92 +1922,6 @@ export interface IPricingModuleService extends IModuleService { sharedContext?: Context ): Promise<[PriceSetMoneyAmountDTO[], number]> - /** - * This method is used to create new price set money amount rules. A price set money amount rule creates an association between a price set money amount and - * a rule type. - * - * @param {CreatePriceSetMoneyAmountRulesDTO[]} data - The price set money amount rules to create. - * @param {Context} sharedContext - A context used to share resources, such as transaction manager, between the application and the module. - * @returns {Promise} The list of created price set money amount rules. - * - * @example - * import { - * initialize as initializePricingModule, - * } from "@medusajs/pricing" - * - * async function createPriceSetMoneyAmountRules (priceSetMoneyAmountId: string, ruleTypeId: string, value: string) { - * const pricingService = await initializePricingModule() - * - * const priceSetMoneyAmountRules = await pricingService.createPriceSetMoneyAmountRules([ - * { - * price_set_money_amount: priceSetMoneyAmountId, - * rule_type: ruleTypeId, - * value - * } - * ]) - * - * // do something with the price set money amount rules or return them - * } - */ - createPriceSetMoneyAmountRules( - data: CreatePriceSetMoneyAmountRulesDTO[], - sharedContext?: Context - ): Promise - - /** - * This method is used to update price set money amount rules, each with their provided data. - * - * @param {UpdatePriceSetMoneyAmountRulesDTO[]} data - - * The price set money amounts to update, each having the attributes to update in a price set money amount. - * @param {Context} sharedContext - A context used to share resources, such as transaction manager, between the application and the module. - * @returns {Promise} The list of updated price set money amount rules. - * - * @example - * import { - * initialize as initializePricingModule, - * } from "@medusajs/pricing" - * - * async function updatePriceSetMoneyAmountRules (id: string, value: string) { - * const pricingService = await initializePricingModule() - * - * const priceSetMoneyAmountRules = await pricingService.updatePriceSetMoneyAmountRules([ - * { - * id, - * value - * } - * ]) - * - * // do something with the price set money amount rules or return them - * } - */ - updatePriceSetMoneyAmountRules( - data: UpdatePriceSetMoneyAmountRulesDTO[], - sharedContext?: Context - ): Promise - - /** - * This method is used to delete price set money amount rules based on the specified IDs. - * - * @param {string[]} ids - The IDs of the price set money amount rules to delete. - * @param {Context} sharedContext - A context used to share resources, such as transaction manager, between the application and the module. - * @returns {Promise} Resolves once the price set money amount rules are deleted. - * - * @example - * import { - * initialize as initializePricingModule, - * } from "@medusajs/pricing" - * - * async function deletePriceSetMoneyAmountRule (id: string) { - * const pricingService = await initializePricingModule() - * - * await pricingService.deletePriceSetMoneyAmountRules([id]) - * } - */ - deletePriceSetMoneyAmountRules( - ids: string[], - sharedContext?: Context - ): Promise - /** * This method is used to retrieve a price rule by its ID. * @@ -3004,6 +2662,18 @@ export interface IPricingModuleService extends IModuleService { sharedContext?: Context ): Promise + softDeletePriceLists( + priceListIds: string[], + config?: SoftDeleteReturn, + sharedContext?: Context + ): Promise | void> + + restorePriceLists( + priceListIds: string[], + config?: RestoreReturn, + sharedContext?: Context + ): Promise | void> + /** * This method is used to retrieve a price list rule by its ID. *