diff --git a/integration-tests/http/__tests__/price-list/admin/price-list.spec.ts b/integration-tests/http/__tests__/price-list/admin/price-list.spec.ts index b57eeb84eb..6e569a1016 100644 --- a/integration-tests/http/__tests__/price-list/admin/price-list.spec.ts +++ b/integration-tests/http/__tests__/price-list/admin/price-list.spec.ts @@ -92,22 +92,6 @@ medusaIntegrationTestRunner({ adminHeaders ) ).data.price_list - - // BREAKING: You need to register rule types before you can use them - await api.post( - "/admin/pricing/rule-types", - { name: "Region ID", rule_attribute: "region_id", default_priority: 0 }, - adminHeaders - ) - await api.post( - "/admin/pricing/rule-types", - { - name: "Customer Group ID", - rule_attribute: "customer_group_id", - default_priority: 0, - }, - adminHeaders - ) }) describe("/admin/price-lists", () => { diff --git a/integration-tests/http/__tests__/product/admin/product.spec.ts b/integration-tests/http/__tests__/product/admin/product.spec.ts index bd479932cd..976c8fe496 100644 --- a/integration-tests/http/__tests__/product/admin/product.spec.ts +++ b/integration-tests/http/__tests__/product/admin/product.spec.ts @@ -808,12 +808,6 @@ medusaIntegrationTestRunner({ // }, // async () => { // const variantId = baseProduct.variants[0].id - // await pricingService.createRuleTypes([ - // { - // name: "Region ID", - // rule_attribute: "region_id", - // }, - // ]) // const priceSet = await createVariantPriceSet({ // container, // variantId, @@ -1228,16 +1222,6 @@ medusaIntegrationTestRunner({ }) it("creates a product variant with price rules", async () => { - await api.post( - `/admin/pricing/rule-types`, - { - name: "Region", - rule_attribute: "region_id", - default_priority: 1, - }, - adminHeaders - ) - const response = await api.post( "/admin/products", { diff --git a/integration-tests/http/__tests__/product/admin/variant.spec.ts b/integration-tests/http/__tests__/product/admin/variant.spec.ts index bbd73863de..b2197b741b 100644 --- a/integration-tests/http/__tests__/product/admin/variant.spec.ts +++ b/integration-tests/http/__tests__/product/admin/variant.spec.ts @@ -96,12 +96,6 @@ medusaIntegrationTestRunner({ describe("updates a variant's default prices (ignores prices associated with a Price List)", () => { it("successfully updates a variant's default prices by changing an existing price (currency_code)", async () => { - await api.post( - `/admin/pricing/rule-types`, - { name: "Region", rule_attribute: "region_id", default_priority: 1 }, - adminHeaders - ) - const data = { prices: [ { diff --git a/integration-tests/http/__tests__/product/store/product.spec.ts b/integration-tests/http/__tests__/product/store/product.spec.ts index 5237fa0680..6d36cec22e 100644 --- a/integration-tests/http/__tests__/product/store/product.spec.ts +++ b/integration-tests/http/__tests__/product/store/product.spec.ts @@ -10,7 +10,6 @@ import { createAdminUser, } from "../../../../helpers/create-admin-user" import { getProductFixture } from "../../../../helpers/fixtures" -import { createDefaultRuleTypes } from "../../../../modules/helpers/create-default-rule-types" jest.setTimeout(30000) @@ -83,7 +82,6 @@ medusaIntegrationTestRunner({ beforeEach(async () => { appContainer = getContainer() await createAdminUser(dbConnection, adminHeaders, appContainer) - await createDefaultRuleTypes(appContainer) const storeModule: IStoreModuleService = appContainer.resolve( ModuleRegistrationName.STORE diff --git a/integration-tests/modules/__tests__/link-modules/product-variant-price-set.spec.ts b/integration-tests/modules/__tests__/link-modules/product-variant-price-set.spec.ts index 31fbd8dfcc..d3ab8f70b5 100644 --- a/integration-tests/modules/__tests__/link-modules/product-variant-price-set.spec.ts +++ b/integration-tests/modules/__tests__/link-modules/product-variant-price-set.spec.ts @@ -46,16 +46,8 @@ medusaIntegrationTestRunner({ }, ]) - await pricingModule.createRuleTypes([ - { - name: "customer_group_id", - rule_attribute: "customer_group_id", - }, - ]) - const [priceSet1, priceSet2] = await pricingModule.createPriceSets([ { - rules: [{ rule_attribute: "customer_group_id" }], prices: [ { amount: 3000, @@ -71,7 +63,6 @@ medusaIntegrationTestRunner({ ], }, { - rules: [{ rule_attribute: "customer_group_id" }], prices: [ { amount: 400, diff --git a/integration-tests/modules/__tests__/price-lists/admin/add-price-list-price-batch.spec.ts b/integration-tests/modules/__tests__/price-lists/admin/add-price-list-price-batch.spec.ts index 94e20ad5c9..52616beff7 100644 --- a/integration-tests/modules/__tests__/price-lists/admin/add-price-list-price-batch.spec.ts +++ b/integration-tests/modules/__tests__/price-lists/admin/add-price-list-price-batch.spec.ts @@ -6,7 +6,6 @@ import { simpleRegionFactory, } from "../../../../factories" import { createAdminUser } from "../../../../helpers/create-admin-user" -import { createDefaultRuleTypes } from "../../../helpers/create-default-rule-types" import { createVariantPriceSet } from "../../../helpers/create-variant-price-set" jest.setTimeout(50000) @@ -37,7 +36,6 @@ medusaIntegrationTestRunner({ beforeEach(async () => { await createAdminUser(dbConnection, adminHeaders, appContainer) - await createDefaultRuleTypes(appContainer) await simpleRegionFactory(dbConnection, { id: "test-region", diff --git a/integration-tests/modules/__tests__/price-lists/admin/delete-price-list-prices-by-product.ts b/integration-tests/modules/__tests__/price-lists/admin/delete-price-list-prices-by-product.ts index f3cc69f3fd..205d7eca02 100644 --- a/integration-tests/modules/__tests__/price-lists/admin/delete-price-list-prices-by-product.ts +++ b/integration-tests/modules/__tests__/price-lists/admin/delete-price-list-prices-by-product.ts @@ -5,7 +5,6 @@ import { import { IPricingModuleService } from "@medusajs/types" import { medusaIntegrationTestRunner } from "medusa-test-utils" -import { createDefaultRuleTypes } from "../../../helpers/create-default-rule-types" import { createVariantPriceSet } from "../../../helpers/create-variant-price-set" import { createAdminUser } from "../../../../helpers/create-admin-user" @@ -39,7 +38,6 @@ medusaIntegrationTestRunner({ beforeEach(async () => { await createAdminUser(dbConnection, adminHeaders, appContainer) - await createDefaultRuleTypes(appContainer) await simpleRegionFactory(dbConnection, { id: "test-region", diff --git a/integration-tests/modules/__tests__/price-lists/admin/delete-price-list-prices-by-variant.ts b/integration-tests/modules/__tests__/price-lists/admin/delete-price-list-prices-by-variant.ts index 6e1c563380..37784f2411 100644 --- a/integration-tests/modules/__tests__/price-lists/admin/delete-price-list-prices-by-variant.ts +++ b/integration-tests/modules/__tests__/price-lists/admin/delete-price-list-prices-by-variant.ts @@ -5,7 +5,6 @@ import { import { IPricingModuleService } from "@medusajs/types" import { medusaIntegrationTestRunner } from "medusa-test-utils" -import { createDefaultRuleTypes } from "../../../helpers/create-default-rule-types" import { createVariantPriceSet } from "../../../helpers/create-variant-price-set" import { createAdminUser } from "../../../../helpers/create-admin-user" @@ -37,7 +36,6 @@ medusaIntegrationTestRunner({ beforeEach(async () => { await createAdminUser(dbConnection, adminHeaders, appContainer) - await createDefaultRuleTypes(appContainer) await simpleRegionFactory(dbConnection, { id: "test-region", diff --git a/integration-tests/modules/__tests__/price-lists/admin/delete-price-list-prices.ts b/integration-tests/modules/__tests__/price-lists/admin/delete-price-list-prices.ts index 0cba13f972..b7abcb274e 100644 --- a/integration-tests/modules/__tests__/price-lists/admin/delete-price-list-prices.ts +++ b/integration-tests/modules/__tests__/price-lists/admin/delete-price-list-prices.ts @@ -5,7 +5,6 @@ import { import { IPricingModuleService } from "@medusajs/types" import { medusaIntegrationTestRunner } from "medusa-test-utils" -import { createDefaultRuleTypes } from "../../../helpers/create-default-rule-types" import { createVariantPriceSet } from "../../../helpers/create-variant-price-set" import { createAdminUser } from "../../../../helpers/create-admin-user" @@ -37,7 +36,6 @@ medusaIntegrationTestRunner({ beforeEach(async () => { await createAdminUser(dbConnection, adminHeaders, appContainer) - await createDefaultRuleTypes(appContainer) await simpleRegionFactory(dbConnection, { id: "test-region", diff --git a/integration-tests/modules/__tests__/price-lists/admin/price-lists.spec.ts b/integration-tests/modules/__tests__/price-lists/admin/price-lists.spec.ts index 362a04a36a..6ed7cc18e5 100644 --- a/integration-tests/modules/__tests__/price-lists/admin/price-lists.spec.ts +++ b/integration-tests/modules/__tests__/price-lists/admin/price-lists.spec.ts @@ -66,11 +66,6 @@ medusaIntegrationTestRunner({ variant = product.variants[0] variant2 = product.variants[1] - - await pricingModule.createRuleTypes([ - { name: "Customer Group ID", rule_attribute: "customer_group_id" }, - { name: "Region ID", rule_attribute: "region_id" }, - ]) }) describe("GET /admin/price-lists", () => { diff --git a/integration-tests/modules/__tests__/price-lists/store/get-product.ts b/integration-tests/modules/__tests__/price-lists/store/get-product.ts index 7a8a83a9c2..b67c10522a 100644 --- a/integration-tests/modules/__tests__/price-lists/store/get-product.ts +++ b/integration-tests/modules/__tests__/price-lists/store/get-product.ts @@ -7,7 +7,6 @@ import { simpleRegionFactory, } from "../../../../factories" import { createAdminUser } from "../../../../helpers/create-admin-user" -import { createDefaultRuleTypes } from "../../../helpers/create-default-rule-types" import { createVariantPriceSet } from "../../../helpers/create-variant-price-set" jest.setTimeout(50000) @@ -37,7 +36,6 @@ medusaIntegrationTestRunner({ beforeEach(async () => { await createAdminUser(dbConnection, adminHeaders, appContainer) - await createDefaultRuleTypes(appContainer) await simpleRegionFactory(dbConnection, { id: "test-region", diff --git a/integration-tests/modules/__tests__/pricing/admin/rule-types.spec.ts b/integration-tests/modules/__tests__/pricing/admin/rule-types.spec.ts deleted file mode 100644 index 09bb895f04..0000000000 --- a/integration-tests/modules/__tests__/pricing/admin/rule-types.spec.ts +++ /dev/null @@ -1,228 +0,0 @@ -import { ModuleRegistrationName } from "@medusajs/modules-sdk" -import { IPricingModuleService, RuleTypeDTO } from "@medusajs/types" -import { medusaIntegrationTestRunner } from "medusa-test-utils" -import { createAdminUser } from "../../../../helpers/create-admin-user" - -jest.setTimeout(50000) - -const env = { MEDUSA_FF_MEDUSA_V2: true } -const adminHeaders = { headers: { "x-medusa-access-token": "test_token" } } - -medusaIntegrationTestRunner({ - env, - testSuite: ({ dbConnection, getContainer, api }) => { - describe("Admin: Pricing Rule Types API", () => { - let appContainer - let pricingModule: IPricingModuleService - let ruleTypes: RuleTypeDTO[] - - beforeAll(async () => { - appContainer = getContainer() - pricingModule = appContainer.resolve(ModuleRegistrationName.PRICING) - }) - - beforeEach(async () => { - await createAdminUser(dbConnection, adminHeaders, appContainer) - - ruleTypes = await pricingModule.createRuleTypes([ - { name: "Customer Group ID", rule_attribute: "customer_group_id" }, - { name: "Region ID", rule_attribute: "region_id" }, - ]) - }) - - describe("GET /admin/pricing", () => { - it("should get all rule types and its prices with rules", async () => { - let response = await api.get( - `/admin/pricing/rule-types`, - adminHeaders - ) - - expect(response.status).toEqual(200) - expect(response.data.count).toEqual(2) - expect(response.data.rule_types).toEqual([ - expect.objectContaining({ id: expect.any(String) }), - expect.objectContaining({ id: expect.any(String) }), - ]) - - response = await api.get( - `/admin/pricing/rule-types?fields=id,rule_attribute,created_at`, - adminHeaders - ) - - expect(response.status).toEqual(200) - expect(response.data.count).toEqual(2) - expect(response.data.rule_types).toEqual( - expect.arrayContaining([ - { - id: ruleTypes[0].id, - rule_attribute: ruleTypes[0].rule_attribute, - created_at: expect.any(String), - }, - { - id: ruleTypes[1].id, - rule_attribute: ruleTypes[1].rule_attribute, - created_at: expect.any(String), - }, - ]) - ) - }) - }) - - describe("GET /admin/pricing/:id", () => { - it("should retrieve a rule type and its prices with rules", async () => { - const ruleType = ruleTypes[0] - - let response = await api.get( - `/admin/pricing/rule-types/${ruleType.id}`, - adminHeaders - ) - - expect(response.status).toEqual(200) - expect(response.data.rule_type).toEqual( - expect.objectContaining({ - id: ruleType.id, - }) - ) - - response = await api.get( - `/admin/pricing/rule-types/${ruleType.id}?fields=id,created_at`, - adminHeaders - ) - - expect(response.data.rule_type).toEqual({ - id: ruleType.id, - created_at: expect.any(String), - }) - }) - - it("should throw an error when rule type is not found", async () => { - const error = await api - .get(`/admin/pricing/rule-types/does-not-exist`, adminHeaders) - .catch((e) => e) - - expect(error.response.status).toBe(404) - expect(error.response.data).toEqual({ - type: "not_found", - message: "RuleType with id: does-not-exist was not found", - }) - }) - }) - - describe("POST /admin/pricing/rule-types", () => { - it("should throw an error if required params are not passed", async () => { - const { response } = await api - .post( - `/admin/pricing/rule-types`, - { - rule_attribute: "rule_attr_test1", - default_priority: 7, - }, - adminHeaders - ) - .catch((e) => e) - - expect(response.status).toEqual(400) - // expect(response.data.message).toEqual( - // "name must be a string, name should not be empty" - // ) - }) - - it("should create a rule type successfully", async () => { - const response = await api.post( - `/admin/pricing/rule-types`, - { - name: "test", - rule_attribute: "rule_attr_test", - default_priority: 6, - }, - adminHeaders - ) - - expect(response.status).toEqual(200) - expect(response.data.rule_type).toEqual( - expect.objectContaining({ - id: expect.any(String), - name: "test", - rule_attribute: "rule_attr_test", - default_priority: 6, - }) - ) - }) - }) - - describe("POST /admin/pricing/rule-types/:id", () => { - it("should throw an error if id does not exist", async () => { - const { response } = await api - .post(`/admin/pricing/rule-types/does-not-exist`, {}, adminHeaders) - .catch((e) => e) - - expect(response.status).toEqual(404) - expect(response.data.message).toEqual( - `RuleType with id "does-not-exist" not found` - ) - }) - - it("should update a rule type successfully", async () => { - const [ruleType] = ruleTypes - - const response = await api.post( - `/admin/pricing/rule-types/${ruleType.id}`, - { - name: "test update", - rule_attribute: "test_update", - default_priority: 7, - }, - adminHeaders - ) - - expect(response.status).toEqual(200) - expect(response.data.rule_type).toEqual( - expect.objectContaining({ - id: expect.any(String), - name: "test update", - rule_attribute: "test_update", - default_priority: 7, - }) - ) - }) - }) - - describe("DELETE /admin/pricing/rule-types/:id", () => { - it("should delete rule type successfully", async () => { - const [ruleType] = ruleTypes - const response = await api.delete( - `/admin/pricing/rule-types/${ruleType.id}`, - adminHeaders - ) - - expect(response.status).toEqual(200) - expect(response.data).toEqual({ - id: ruleType.id, - object: "rule_type", - deleted: true, - }) - - const deletedRuleTypes = await pricingModule.listRuleTypes({ - id: [ruleType.id], - }) - - expect(deletedRuleTypes.length).toEqual(0) - }) - - it("should return 200 when id does not exist", async () => { - const response = await api.delete( - `/admin/pricing/rule-types/does-not-exist`, - adminHeaders - ) - - expect(response.status).toEqual(200) - expect(response.data).toEqual({ - id: "does-not-exist", - object: "rule_type", - deleted: true, - }) - }) - }) - }) - }, -}) diff --git a/integration-tests/modules/__tests__/pricing/get-product.ts b/integration-tests/modules/__tests__/pricing/get-product.ts index 3fffcb4d91..0582af47b7 100644 --- a/integration-tests/modules/__tests__/pricing/get-product.ts +++ b/integration-tests/modules/__tests__/pricing/get-product.ts @@ -1,7 +1,6 @@ import { simpleCartFactory, simpleRegionFactory } from "../../../factories" import { ModuleRegistrationName } from "@medusajs/modules-sdk" -import { createDefaultRuleTypes } from "../../helpers/create-default-rule-types" import { medusaIntegrationTestRunner } from "medusa-test-utils" import { createAdminUser } from "../../../helpers/create-admin-user" @@ -33,7 +32,6 @@ medusaIntegrationTestRunner({ medusaContainer = getContainer() }) beforeEach(async () => { - await createDefaultRuleTypes(medusaContainer) await createAdminUser(dbConnection, adminHeaders, medusaContainer) await simpleRegionFactory(dbConnection, { id: "region-1", @@ -78,12 +76,7 @@ medusaIntegrationTestRunner({ productId = response.data.product.id const variant = response.data.product.variants[0] - ruleType = await pricingModuleService.createRuleTypes([ - { name: "region_id", rule_attribute: "region_id" }, - ]) - priceSet = await pricingModuleService.createPriceSets({ - rules: [{ rule_attribute: "region_id" }], prices: [ { amount: 1000, diff --git a/integration-tests/modules/__tests__/product/admin/export-products.spec.ts b/integration-tests/modules/__tests__/product/admin/export-products.spec.ts index ea37a80908..4bee267b29 100644 --- a/integration-tests/modules/__tests__/product/admin/export-products.spec.ts +++ b/integration-tests/modules/__tests__/product/admin/export-products.spec.ts @@ -6,7 +6,6 @@ import { getContainer } from "../../../../environment-helpers/use-container" import { initDb, useDb } from "../../../../environment-helpers/use-db" import { simpleSalesChannelFactory } from "../../../../factories" import productSeeder from "../../../../helpers/product-seeder" -import { createDefaultRuleTypes } from "../../../helpers/create-default-rule-types" import { adminHeaders, createAdminUser, @@ -65,7 +64,6 @@ describe.skip("Batch job of product-export type", () => { beforeEach(async () => { const container = getContainer() - await createDefaultRuleTypes(container) await productSeeder(dbConnection) await createAdminUser(dbConnection, adminHeaders, container) await userSeeder(dbConnection) diff --git a/integration-tests/modules/__tests__/product/admin/import-products.spec.ts b/integration-tests/modules/__tests__/product/admin/import-products.spec.ts index 9d55da2c1b..28e1fc0273 100644 --- a/integration-tests/modules/__tests__/product/admin/import-products.spec.ts +++ b/integration-tests/modules/__tests__/product/admin/import-products.spec.ts @@ -8,7 +8,6 @@ import { initDb, useDb } from "../../../../environment-helpers/use-db" import { simpleProductFactory } from "../../../../factories" import { simpleProductCollectionFactory } from "../../../../factories/simple-product-collection-factory" import batchJobSeeder from "../../../../helpers/batch-job-seeder" -import { createDefaultRuleTypes } from "../../../helpers/create-default-rule-types" import { adminHeaders, createAdminUser, @@ -99,7 +98,6 @@ describe.skip("Product import batch job", () => { beforeEach(async () => { const container = getContainer() - await createDefaultRuleTypes(container) await batchJobSeeder(dbConnection) await createAdminUser(dbConnection, adminHeaders, container) await userSeeder(dbConnection) diff --git a/integration-tests/modules/helpers/create-default-rule-types.ts b/integration-tests/modules/helpers/create-default-rule-types.ts deleted file mode 100644 index 7ef8284090..0000000000 --- a/integration-tests/modules/helpers/create-default-rule-types.ts +++ /dev/null @@ -1,18 +0,0 @@ -import { IPricingModuleService } from "@medusajs/types" - -export const createDefaultRuleTypes = async (container) => { - const pricingModuleService: IPricingModuleService = container.resolve( - "pricingModuleService" - ) - - return await pricingModuleService.createRuleTypes([ - { - name: "region_id", - rule_attribute: "region_id", - }, - { - name: "customer_group_id", - rule_attribute: "customer_group_id", - }, - ]) -} diff --git a/integration-tests/modules/helpers/create-variant-price-set.ts b/integration-tests/modules/helpers/create-variant-price-set.ts index d78e896b32..ab4efd4f0d 100644 --- a/integration-tests/modules/helpers/create-variant-price-set.ts +++ b/integration-tests/modules/helpers/create-variant-price-set.ts @@ -13,18 +13,14 @@ const defaultPrices = [ }, ] -const defaultPriceSetRules = [{ rule_attribute: "region_id" }] - export const createVariantPriceSet = async ({ container, variantId, prices = defaultPrices, - rules = defaultPriceSetRules, }: { container: MedusaContainer variantId: string prices?: CreatePriceSetDTO["prices"] - rules?: CreatePriceSetDTO["rules"] }): Promise => { const remoteLink = container.resolve("remoteLink") const pricingModuleService: IPricingModuleService = container.resolve( @@ -32,7 +28,6 @@ export const createVariantPriceSet = async ({ ) const priceSet = await pricingModuleService.createPriceSets({ - rules, prices, }) diff --git a/packages/core/core-flows/src/fulfillment/steps/add-shipping-options-prices.ts b/packages/core/core-flows/src/fulfillment/steps/add-shipping-options-prices.ts index f2f7f6034c..9914692412 100644 --- a/packages/core/core-flows/src/fulfillment/steps/add-shipping-options-prices.ts +++ b/packages/core/core-flows/src/fulfillment/steps/add-shipping-options-prices.ts @@ -25,8 +25,6 @@ function buildPriceSet( prices: StepInput[0]["prices"], regionToCurrencyMap: Map ): CreatePriceSetDTO { - const rules: CreatePriceSetDTO["rules"] = [] - const shippingOptionPrices = prices.map((price) => { if ("currency_code" in price) { return { @@ -35,10 +33,6 @@ function buildPriceSet( } } - rules.push({ - rule_attribute: "region_id", - }) - return { currency_code: regionToCurrencyMap.get(price.region_id)!, amount: price.amount, @@ -48,7 +42,7 @@ function buildPriceSet( } }) - return { rules, prices: shippingOptionPrices } + return { prices: shippingOptionPrices } } export const createShippingOptionsPriceSetsStepId = diff --git a/packages/core/core-flows/src/fulfillment/workflows/create-shipping-options.ts b/packages/core/core-flows/src/fulfillment/workflows/create-shipping-options.ts index 2a7dd5c722..3af2c2b7ba 100644 --- a/packages/core/core-flows/src/fulfillment/workflows/create-shipping-options.ts +++ b/packages/core/core-flows/src/fulfillment/workflows/create-shipping-options.ts @@ -1,4 +1,4 @@ -import { CreateRuleTypeDTO, FulfillmentWorkflow } from "@medusajs/types" +import { FulfillmentWorkflow } from "@medusajs/types" import { createWorkflow, transform, @@ -9,7 +9,6 @@ import { upsertShippingOptionsStep, } from "../steps" import { setShippingOptionsPriceSetsStep } from "../steps/set-shipping-options-price-sets" -import { createPricingRuleTypesStep } from "../../pricing" export const createShippingOptionsWorkflowId = "create-shipping-options-workflow" @@ -45,18 +44,8 @@ export const createShippingOptionsWorkflow = createWorkflow( shippingOptionsIndexToPrices: data.shippingOptionsIndexToPrices, }, (data) => { - const ruleTypes = new Set() const shippingOptionsPrices = data.shippingOptionsIndexToPrices.map( ({ shipping_option_index, prices }) => { - prices.forEach((price) => { - if ("region_id" in price) { - ruleTypes.add({ - name: "region_id", - rule_attribute: "region_id", - }) - } - }) - return { id: data.shippingOptions[shipping_option_index].id, prices, @@ -66,13 +55,10 @@ export const createShippingOptionsWorkflow = createWorkflow( return { shippingOptionsPrices, - ruleTypes: Array.from(ruleTypes) as CreateRuleTypeDTO[], } } ) - createPricingRuleTypesStep(normalizedShippingOptionsPrices.ruleTypes) - const shippingOptionsPriceSetsLinkData = createShippingOptionsPriceSetsStep( normalizedShippingOptionsPrices.shippingOptionsPrices ) diff --git a/packages/core/core-flows/src/fulfillment/workflows/update-shipping-options.ts b/packages/core/core-flows/src/fulfillment/workflows/update-shipping-options.ts index 25bb86c3fb..5f73a32aef 100644 --- a/packages/core/core-flows/src/fulfillment/workflows/update-shipping-options.ts +++ b/packages/core/core-flows/src/fulfillment/workflows/update-shipping-options.ts @@ -1,8 +1,4 @@ -import { - CreateRuleTypeDTO, - FulfillmentWorkflow, - RuleTypeDTO, -} from "@medusajs/types" +import { FulfillmentWorkflow } from "@medusajs/types" import { createWorkflow, transform, @@ -12,7 +8,6 @@ import { setShippingOptionsPricesStep, upsertShippingOptionsStep, } from "../steps" -import { createPricingRuleTypesStep } from "../../pricing" export const updateShippingOptionsWorkflowId = "update-shipping-options-workflow" @@ -49,18 +44,8 @@ export const updateShippingOptionsWorkflow = createWorkflow( shippingOptionsIndexToPrices: data.shippingOptionsIndexToPrices, }, (data) => { - const ruleTypes = new Set>() const shippingOptionsPrices = data.shippingOptionsIndexToPrices.map( ({ shipping_option_index, prices }) => { - prices?.forEach((price) => { - if ("region_id" in price) { - ruleTypes.add({ - name: "region_id", - rule_attribute: "region_id", - }) - } - }) - return { id: data.shippingOptions[shipping_option_index].id, prices, @@ -70,13 +55,10 @@ export const updateShippingOptionsWorkflow = createWorkflow( return { shippingOptionsPrices, - ruleTypes: Array.from(ruleTypes) as CreateRuleTypeDTO[], } } ) - createPricingRuleTypesStep(normalizedShippingOptionsPrices.ruleTypes) - setShippingOptionsPricesStep( normalizedShippingOptionsPrices.shippingOptionsPrices ) diff --git a/packages/core/core-flows/src/price-list/steps/update-price-lists.ts b/packages/core/core-flows/src/price-list/steps/update-price-lists.ts index 9c3dc1e01b..257f678950 100644 --- a/packages/core/core-flows/src/price-list/steps/update-price-lists.ts +++ b/packages/core/core-flows/src/price-list/steps/update-price-lists.ts @@ -44,7 +44,7 @@ export const updatePriceListsStep = createStep( await pricingModule.updatePriceLists( dataBeforeUpdate.map((data) => { - const { price_list_rules: priceListRules = [], rules, ...rest } = data + const { price_list_rules: priceListRules = [], ...rest } = data const updateData: UpdatePriceListDTO = { ...rest, @@ -82,14 +82,8 @@ async function getDataBeforeUpdate( selectsClone.splice(index, 1) } - selectsClone.push( - "price_list_rules.price_list_rule_values.value", - "price_list_rules.rule_type.rule_attribute" - ) - relationsClone.push( - "price_list_rules.price_list_rule_values", - "price_list_rules.rule_type" - ) + selectsClone.push("price_list_rules.value", "price_list_rules.attribute") + relationsClone.push("price_list_rules") } const dataBeforeUpdate = await pricingModule.listPriceLists( diff --git a/packages/core/core-flows/src/pricing/index.ts b/packages/core/core-flows/src/pricing/index.ts index 68de82c9f9..c1f49c23fa 100644 --- a/packages/core/core-flows/src/pricing/index.ts +++ b/packages/core/core-flows/src/pricing/index.ts @@ -1,2 +1 @@ export * from "./steps" -export * from "./workflows" diff --git a/packages/core/core-flows/src/pricing/steps/create-pricing-rule-types.ts b/packages/core/core-flows/src/pricing/steps/create-pricing-rule-types.ts deleted file mode 100644 index d31969363c..0000000000 --- a/packages/core/core-flows/src/pricing/steps/create-pricing-rule-types.ts +++ /dev/null @@ -1,47 +0,0 @@ -import { ModuleRegistrationName } from "@medusajs/modules-sdk" -import { CreateRuleTypeDTO, IPricingModuleService } from "@medusajs/types" -import { createStep, StepResponse } from "@medusajs/workflows-sdk" - -export const createPricingRuleTypesStepId = "create-pricing-rule-types" -export const createPricingRuleTypesStep = createStep( - createPricingRuleTypesStepId, - async (data: CreateRuleTypeDTO[], { container }) => { - if (!data?.length) { - return - } - - const pricingModule = container.resolve( - ModuleRegistrationName.PRICING - ) - - const existingRuleTypes = await pricingModule.listRuleTypes({ - rule_attribute: data.map((d) => d.rule_attribute), - }) - - const existingRuleTypeAttributes = new Set( - existingRuleTypes.map((ruleType) => ruleType.rule_attribute) - ) - - const ruleTypesToCreate = data.filter( - (dataItem) => !existingRuleTypeAttributes.has(dataItem.rule_attribute) - ) - - const ruleTypes = await pricingModule.createRuleTypes(ruleTypesToCreate) - - return new StepResponse( - ruleTypes, - ruleTypes.map((ruleType) => ruleType.id) - ) - }, - async (ruleTypeIds, { container }) => { - if (!ruleTypeIds?.length) { - return - } - - const pricingModule = container.resolve( - ModuleRegistrationName.PRICING - ) - - await pricingModule.deleteRuleTypes(ruleTypeIds) - } -) diff --git a/packages/core/core-flows/src/pricing/steps/delete-pricing-rule-types.ts b/packages/core/core-flows/src/pricing/steps/delete-pricing-rule-types.ts deleted file mode 100644 index 6ecec07a9c..0000000000 --- a/packages/core/core-flows/src/pricing/steps/delete-pricing-rule-types.ts +++ /dev/null @@ -1,31 +0,0 @@ -import { ModuleRegistrationName } from "@medusajs/modules-sdk" -import { IPricingModuleService } from "@medusajs/types" -import { StepResponse, createStep } from "@medusajs/workflows-sdk" - -export const deletePricingRuleTypesStepId = "delete-pricing-rule-types" -export const deletePricingRuleTypesStep = createStep( - deletePricingRuleTypesStepId, - async (ids: string[], { container }) => { - const pricingModule = container.resolve( - ModuleRegistrationName.PRICING - ) - - // TODO: implement soft deleting rule types - // await pricingModule.softDeleteRuleTypes(ids) - await pricingModule.deleteRuleTypes(ids) - - return new StepResponse(void 0, ids) - }, - async (ids, { container }) => { - if (!ids?.length) { - return - } - - const pricingModule = container.resolve( - ModuleRegistrationName.PRICING - ) - - // TODO: implement restoring soft deleted rule types - // await pricingModule.restoreRuleTypes(ids) - } -) diff --git a/packages/core/core-flows/src/pricing/steps/index.ts b/packages/core/core-flows/src/pricing/steps/index.ts index ac84365e0e..04bc6d7d03 100644 --- a/packages/core/core-flows/src/pricing/steps/index.ts +++ b/packages/core/core-flows/src/pricing/steps/index.ts @@ -1,5 +1,2 @@ export * from "./create-price-sets" export * from "./update-price-sets" -export * from "./create-pricing-rule-types" -export * from "./delete-pricing-rule-types" -export * from "./update-pricing-rule-types" diff --git a/packages/core/core-flows/src/pricing/steps/update-pricing-rule-types.ts b/packages/core/core-flows/src/pricing/steps/update-pricing-rule-types.ts deleted file mode 100644 index 02e6bd1e25..0000000000 --- a/packages/core/core-flows/src/pricing/steps/update-pricing-rule-types.ts +++ /dev/null @@ -1,48 +0,0 @@ -import { ModuleRegistrationName } from "@medusajs/modules-sdk" -import { IPricingModuleService, UpdateRuleTypeDTO } from "@medusajs/types" -import { - convertItemResponseToUpdateRequest, - getSelectsAndRelationsFromObjectArray, -} from "@medusajs/utils" -import { StepResponse, createStep } from "@medusajs/workflows-sdk" - -export const updatePricingRuleTypesStepId = "update-pricing-rule-types" -export const updatePricingRuleTypesStep = createStep( - updatePricingRuleTypesStepId, - async (data: UpdateRuleTypeDTO[], { container }) => { - const pricingModule = container.resolve( - ModuleRegistrationName.PRICING - ) - - const { selects, relations } = getSelectsAndRelationsFromObjectArray(data) - const dataBeforeUpdate = await pricingModule.listRuleTypes( - { id: data.map((d) => d.id) }, - { relations, select: selects } - ) - - const updatedRuleTypes = await pricingModule.updateRuleTypes(data) - - return new StepResponse(updatedRuleTypes, { - dataBeforeUpdate, - selects, - relations, - }) - }, - async (revertInput, { container }) => { - if (!revertInput) { - return - } - - const { dataBeforeUpdate = [], selects, relations } = revertInput - - const pricingModule = container.resolve( - ModuleRegistrationName.PRICING - ) - - await pricingModule.updateRuleTypes( - dataBeforeUpdate.map((data) => - convertItemResponseToUpdateRequest(data, selects, relations) - ) - ) - } -) diff --git a/packages/core/core-flows/src/pricing/workflows/create-pricing-rule-types.ts b/packages/core/core-flows/src/pricing/workflows/create-pricing-rule-types.ts deleted file mode 100644 index 8a605b04a1..0000000000 --- a/packages/core/core-flows/src/pricing/workflows/create-pricing-rule-types.ts +++ /dev/null @@ -1,13 +0,0 @@ -import { CreateRuleTypeDTO, RuleTypeDTO } from "@medusajs/types" -import { WorkflowData, createWorkflow } from "@medusajs/workflows-sdk" -import { createPricingRuleTypesStep } from "../steps" - -type WorkflowInput = { data: CreateRuleTypeDTO[] } - -export const createPricingRuleTypesWorkflowId = "create-pricing-rule-types" -export const createPricingRuleTypesWorkflow = createWorkflow( - createPricingRuleTypesWorkflowId, - (input: WorkflowData): WorkflowData => { - return createPricingRuleTypesStep(input.data) - } -) diff --git a/packages/core/core-flows/src/pricing/workflows/delete-pricing-rule-types.ts b/packages/core/core-flows/src/pricing/workflows/delete-pricing-rule-types.ts deleted file mode 100644 index e4e7e03318..0000000000 --- a/packages/core/core-flows/src/pricing/workflows/delete-pricing-rule-types.ts +++ /dev/null @@ -1,12 +0,0 @@ -import { createWorkflow, WorkflowData } from "@medusajs/workflows-sdk" -import { deletePricingRuleTypesStep } from "../steps" - -type WorkflowInput = { ids: string[] } - -export const deletePricingRuleTypesWorkflowId = "delete-pricing-rule-types" -export const deletePricingRuleTypesWorkflow = createWorkflow( - deletePricingRuleTypesWorkflowId, - (input: WorkflowData): WorkflowData => { - deletePricingRuleTypesStep(input.ids) - } -) diff --git a/packages/core/core-flows/src/pricing/workflows/index.ts b/packages/core/core-flows/src/pricing/workflows/index.ts deleted file mode 100644 index 551d2fee21..0000000000 --- a/packages/core/core-flows/src/pricing/workflows/index.ts +++ /dev/null @@ -1,3 +0,0 @@ -export * from "./create-pricing-rule-types" -export * from "./delete-pricing-rule-types" -export * from "./update-pricing-rule-types" diff --git a/packages/core/core-flows/src/pricing/workflows/update-pricing-rule-types.ts b/packages/core/core-flows/src/pricing/workflows/update-pricing-rule-types.ts deleted file mode 100644 index d2f4e358ff..0000000000 --- a/packages/core/core-flows/src/pricing/workflows/update-pricing-rule-types.ts +++ /dev/null @@ -1,13 +0,0 @@ -import { RuleTypeDTO, UpdateRuleTypeDTO } from "@medusajs/types" -import { WorkflowData, createWorkflow } from "@medusajs/workflows-sdk" -import { updatePricingRuleTypesStep } from "../steps" - -type WorkflowInput = { data: UpdateRuleTypeDTO[] } - -export const updatePricingRuleTypesWorkflowId = "update-pricing-rule-types" -export const updatePricingRuleTypesWorkflow = createWorkflow( - updatePricingRuleTypesWorkflowId, - (input: WorkflowData): WorkflowData => { - return updatePricingRuleTypesStep(input.data) - } -) diff --git a/packages/core/types/src/pricing/common/index.ts b/packages/core/types/src/pricing/common/index.ts index 771a2bc34d..0000be1707 100644 --- a/packages/core/types/src/pricing/common/index.ts +++ b/packages/core/types/src/pricing/common/index.ts @@ -3,6 +3,4 @@ export * from "./price" export * from "./price-list" export * from "./price-rule" export * from "./price-set" -export * from "./price-set-rule-type" export * from "./pricing-context" -export * from "./rule-type" diff --git a/packages/core/types/src/pricing/common/price-list.ts b/packages/core/types/src/pricing/common/price-list.ts index 01ecca99ca..3e2514f6ab 100644 --- a/packages/core/types/src/pricing/common/price-list.ts +++ b/packages/core/types/src/pricing/common/price-list.ts @@ -5,7 +5,6 @@ import { UpdateMoneyAmountDTO, } from "./money-amount" import { PriceDTO } from "./price" -import { RuleTypeDTO } from "./rule-type" /** * @enum @@ -63,7 +62,7 @@ export interface PriceListDTO { */ rules_count?: number /** - * The associated price set money amounts. + * The associated price list money amounts. * * @expandable */ @@ -75,22 +74,7 @@ export interface PriceListDTO { */ money_amounts?: MoneyAmountDTO[] /** - * The associated rule types. - * - * @expandable - */ - rule_types?: RuleTypeDTO[] - /** - * The price set's rules. - * - * @expandable - */ - rules?: PriceListRuleDTO[] - /** - * The price set's rules. - * - * @privateRemarks - * Do we need both this and `rules`? + * The price list's rules. * * @expandable */ @@ -108,9 +92,9 @@ export interface CreatePriceListPriceDTO extends CreateMoneyAmountDTO { */ price_set_id: string /** - * The rules to add to the price. The object's keys are rule types' `rule_attribute` attribute, and values are the value of that rule associated with this price. + * The rules to add to the price. The object's keys are the attribute, and values are the value of that rule associated with this price. */ - rules?: CreatePriceSetPriceRules + rules?: CreatePriceListPriceRules } export interface UpdatePriceListPriceDTO extends UpdateMoneyAmountDTO { @@ -119,25 +103,25 @@ export interface UpdatePriceListPriceDTO extends UpdateMoneyAmountDTO { */ price_set_id: string /** - * The rules to add to the price. The object's keys are rule types' `rule_attribute` attribute, and values are the value of that rule associated with this price. + * The rules to add to the price. The object's keys are the attribute, and values are the value of that rule associated with this price. */ - rules?: CreatePriceSetPriceRules + rules?: CreatePriceListPriceRules } /** * @interface * - * The price rules to be set for each price in the price set. + * The price rules to be set for each price in the price list. * - * Each key of the object is a rule type's `rule_attribute`, and its value + * Each key of the object is an attribute, and its value * is the values of the rule. */ -export interface CreatePriceSetPriceRules extends Record {} +export interface CreatePriceListPriceRules extends Record {} /** * @interface * - * The price list's rules to be set. Each key of the object is a rule type's `rule_attribute`, and its value + * The price list's rules to be set. Each key of the object the attribute, and its value * is the values of the rule. */ export interface CreatePriceListRules extends Record {} @@ -172,10 +156,6 @@ export interface CreatePriceListDTO { * The price list's type. */ type?: PriceListType - /** - * The number of rules associated with the price list. - */ - rules_count?: number /** * The rules to be created and associated with the price list. */ @@ -216,10 +196,6 @@ export interface UpdatePriceListDTO { * The price list's status. */ status?: PriceListStatus - /** - * The number of rules associated with the price list. - */ - rules_count?: number /** * The rules to be created and associated with the price list. */ @@ -274,23 +250,12 @@ export interface FilterablePriceListRuleProps * The values to filter price list rules by. */ value?: string[] - /** - * Filter price list rules by the ID of their associated rule types. - */ - rule_type?: string[] /** * Filter price list rules by the ID of their associated price lists. */ price_list_id?: string[] } -export interface FilterablePriceListRuleValueProps - extends BaseFilterable { - id?: string[] - value?: string[] - price_list_rule_id?: string[] -} - /** * @interface * @@ -301,31 +266,22 @@ export interface PriceListRuleDTO { * The price list rule's ID. */ id: string + /** + * The attribute of the rule. + * + */ + attribute: string /** * The value of the rule. * - * @privateRemarks - * Shouldn't this be in PriceListRuleValueDTO only? */ - value: string - /** - * The associated rule type. - * - * @expandable - */ - rule_type: RuleTypeDTO + value: string | string[] /** * The associated price list. * * @expandable */ price_list: PriceListDTO - /** - * The associated rule values. - * - * @expandable - */ - price_list_rule_values?: PriceListRuleValueDTO[] } /** @@ -334,14 +290,6 @@ export interface PriceListRuleDTO { * The price list rule to create. */ export interface CreatePriceListRuleDTO { - /** - * The ID of a rule type to be associated with the price list rule. - */ - rule_type_id?: string - /** - * The ID of a rule type or the details of an existing rule type to be associated with the price list rule. - */ - rule_type?: string | RuleTypeDTO /** * The ID of a price list to be associated with the price list rule. */ @@ -352,68 +300,6 @@ export interface CreatePriceListRuleDTO { price_list?: string | PriceListDTO } -/** - * @interface - * - * The attributes to update in a price list rule. - */ -export interface UpdatePriceListRuleDTO { - /** - * The ID of the price list rule to update. - */ - id: string - /** - * The ID of a price list to be associated with the price list rule. - */ - price_list_id?: string - /** - * The ID of a rule type to be associated with the price list rule. - */ - rule_type_id?: string - /** - * The ID of a price list to be associated with the price list rule. - */ - price_list?: string - /** - * The ID of a rule type or the details of an existing rule type to be associated with the price list rule. - */ - rule_type?: string -} - -/** - * @interface - * - * The price list rule value's details. - */ -export interface PriceListRuleValueDTO { - /** - * The price list rule value's ID. - */ - id: string - /** - * The rule's value. - */ - value: string - /** - * The associated price list rule. - * - * @expandable - */ - price_list_rule: PriceListRuleDTO -} - -export interface CreatePriceListRuleValueDTO { - value: string - price_list_rule_id?: string - price_list_rule?: PriceListRuleDTO | string -} - -export interface UpdatePriceListRuleValueDTO { - id: string - value: string - price_list_rule_id: string -} - /** * @interface * @@ -457,7 +343,7 @@ export interface SetPriceListRulesDTO { */ price_list_id: string /** - * The rules to add to the price list. Each key of the object is a rule type's `rule_attribute`, and its value + * The rules to add to the price list. Each key of the object is the attribute, and its value * is the value(s) of the rule. */ rules: Record @@ -474,7 +360,7 @@ export interface RemovePriceListRulesDTO { */ price_list_id: string /** - * The rules to remove from the price list. Each item being a rule type's `rule_attribute`. + * The rules to remove from the price list. Each item being the attribute. */ rules: string[] } diff --git a/packages/core/types/src/pricing/common/price-rule.ts b/packages/core/types/src/pricing/common/price-rule.ts index c02df09d51..7b29301295 100644 --- a/packages/core/types/src/pricing/common/price-rule.ts +++ b/packages/core/types/src/pricing/common/price-rule.ts @@ -1,6 +1,5 @@ import { BaseFilterable } from "../../dal" import { PriceSetDTO } from "./price-set" -import { RuleTypeDTO } from "./rule-type" /** * @interface @@ -22,15 +21,9 @@ export interface PriceRuleDTO { */ price_set: PriceSetDTO /** - * The ID of the associated rule type. + * The attribute of the price rule */ - rule_type_id: string - /** - * The associated rule type. - * - * @expandable - */ - rule_type: RuleTypeDTO + attribute: string /** * The value of the price rule. */ @@ -73,9 +66,9 @@ export interface CreatePriceRuleDTO { */ price_set_id?: string /** - * The ID of the associated rule type. + * The attribute of the price rule */ - rule_type_id?: string + attribute: string /** * The value of the price rule. */ @@ -99,7 +92,10 @@ export interface CreatePriceRuleDTO { export interface UpdatePriceRuleDTO { id: string price_set_id?: string - rule_type_id?: string + /** + * The attribute of the price rule + */ + attribute?: string /** * The value of the price rule. */ @@ -136,8 +132,4 @@ export interface FilterablePriceRuleProps * The IDs to filter the price rule's associated price set. */ price_set_id?: string[] - /** - * The IDs to filter the price rule's associated rule type. - */ - rule_type_id?: string[] } diff --git a/packages/core/types/src/pricing/common/price-set-rule-type.ts b/packages/core/types/src/pricing/common/price-set-rule-type.ts deleted file mode 100644 index 6df15c5d05..0000000000 --- a/packages/core/types/src/pricing/common/price-set-rule-type.ts +++ /dev/null @@ -1,28 +0,0 @@ -import { BaseFilterable } from "../../dal" -import { PriceSetDTO } from "./price-set" -import { RuleTypeDTO } from "./rule-type" - -export interface PriceSetRuleTypeDTO { - id: string - price_set: PriceSetDTO - rule_type: RuleTypeDTO - value: string -} - -export interface CreatePriceSetRuleTypeDTO { - price_set_id: string - rule_type_id: string -} - -export interface UpdatePriceSetRuleTypeDTO { - id: string - price_set?: string - rule_type?: string -} - -export interface FilterablePriceSetRuleTypeProps - extends BaseFilterable { - id?: string[] - rule_type_id?: string[] - price_set_id?: string[] -} diff --git a/packages/core/types/src/pricing/common/price-set.ts b/packages/core/types/src/pricing/common/price-set.ts index c337a7ace2..cfcddb740c 100644 --- a/packages/core/types/src/pricing/common/price-set.ts +++ b/packages/core/types/src/pricing/common/price-set.ts @@ -6,8 +6,6 @@ import { FilterableMoneyAmountProps, MoneyAmountDTO, } from "./money-amount" -import { CreatePriceSetPriceRules } from "./price-list" -import { RuleTypeDTO } from "./rule-type" export interface PricingRepositoryService { calculatePrices( @@ -63,11 +61,6 @@ export interface PriceSetDTO { * The calculated price based on the context. */ calculated_price?: CalculatedPriceSet - - /** - * The rule types applied on this price set. - */ - rule_types?: RuleTypeDTO[] } /** @@ -204,23 +197,12 @@ export interface CalculatedPriceSet { /** * @interface * - * The rules to add to a price set. + * The price rules to be set for each price in the price set. + * + * Each key of the object is a the attribute, and its value + * is the values of the rule. */ -export interface AddRulesDTO { - /** - * The ID of the price set to add the rules to. - */ - priceSetId: string - /** - * The rules to add to a price set. - */ - rules: { - /** - * The value of the rule's `rule_attribute` attribute. - */ - attribute: string - }[] -} +export interface CreatePriceSetPriceRules extends Record {} /** * @interface @@ -229,7 +211,7 @@ export interface AddRulesDTO { */ export interface CreatePricesDTO extends CreateMoneyAmountDTO { /** - * The rules to add to the price. The object's keys are rule types' `rule_attribute` attribute, and values are the value of that rule associated with this price. + * The rules to add to the price. The object's keys are the attribute, and values are the value of that rule associated with this price. */ rules?: CreatePriceSetPriceRules } @@ -250,37 +232,12 @@ export interface AddPricesDTO { prices: CreatePricesDTO[] } -/** - * @interface - * - * The rules to remove from a price set. - */ -export interface RemovePriceSetRulesDTO { - /** - * The ID of the price set. - */ - id: string - /** - * The rules to remove. Each string is the `rule_attribute` of a rule to remove. - */ - rules: string[] -} - /** * @interface * * A price set to create. */ export interface CreatePriceSetDTO { - /** - * The rules to associate with the price set. - */ - rules?: { - /** - * the value of the rule's `rule_attribute` attribute. - */ - rule_attribute: string - }[] /** * The prices to create and add to this price set. */ @@ -306,15 +263,6 @@ export interface UpsertPriceSetDTO extends UpdatePriceSetDTO { * The data to update in a price set. */ export interface UpdatePriceSetDTO { - /** - * The rules to associate with the price set. - */ - rules?: { - /** - * the value of the rule's `rule_attribute` attribute. - */ - rule_attribute: string - }[] /** * The prices to create and add to this price set. */ diff --git a/packages/core/types/src/pricing/common/rule-type.ts b/packages/core/types/src/pricing/common/rule-type.ts deleted file mode 100644 index 4d3e6381e5..0000000000 --- a/packages/core/types/src/pricing/common/rule-type.ts +++ /dev/null @@ -1,106 +0,0 @@ -import { BaseFilterable } from "../../dal" - -/** - * @interface - * - * A rule type's data. - */ -export interface RuleTypeDTO { - /** - * The ID of the rule type. - */ - id: string - /** - * The display name of the rule type. - */ - name: string - /** - * The unique name used to later identify the rule_attribute. For example, it can be used in the `context` parameter of - * the `calculatePrices` method to specify a rule for calculating the price. - */ - rule_attribute: string - /** - * The priority of the rule type. This is useful when calculating the price of a price set, and multiple rules satisfy - * the provided context. The higher the value, the higher the priority of the rule type. - */ - default_priority: number - /** - * The creation date of the rule type. - */ - created_at?: Date | string - /** - * The update date of the rule type. - */ - updated_at?: Date | string -} - -/** - * @interface - * - * The rule type to create. - */ -export interface CreateRuleTypeDTO { - /** - * The ID of the rule type. - */ - id?: string - /** - * The display name of the rule type. - */ - name: string - /** - * The unique name used to later identify the rule_attribute. For example, it can be used in the `context` parameter of the `calculatePrices` - * method to specify a rule for calculating the price. - */ - rule_attribute: string - /** - * The priority of the rule type. This is useful when calculating the price of a price set, and multiple rules satisfy the provided context. - * The higher the value, the higher the priority of the rule type. - */ - default_priority?: number -} - -/** - * @interface - * - * The data to update in a rule type. The `id` is used to identify which price set to update. - */ -export interface UpdateRuleTypeDTO { - /** - * The ID of the rule type to update. - */ - id: string - /** - * The display name of the rule type. - */ - name?: string - /** - * The unique name used to later identify the rule_attribute. For example, it can be used in the `context` parameter of the `calculatePrices` method to specify a rule for calculating the price. - */ - rule_attribute?: string - /** - * The priority of the rule type. This is useful when calculating the price of a price set, and multiple rules satisfy the provided context. The higher the value, the higher the priority of the rule type. - */ - default_priority?: number -} - -/** - * @interface - * - * Filters to apply on rule types. - */ -export interface FilterableRuleTypeProps - extends BaseFilterable { - /** - * The IDs to filter rule types by. - */ - id?: string[] - /** - * The names to filter rule types by. - */ - name?: string[] - /** - * The rule attributes to filter rule types by. - */ - rule_attribute?: string[] -} diff --git a/packages/core/types/src/pricing/service.ts b/packages/core/types/src/pricing/service.ts index ef6a2edeae..4e688cbf3d 100644 --- a/packages/core/types/src/pricing/service.ts +++ b/packages/core/types/src/pricing/service.ts @@ -9,13 +9,11 @@ import { CreatePriceListDTO, CreatePriceRuleDTO, CreatePriceSetDTO, - CreateRuleTypeDTO, FilterablePriceListProps, FilterablePriceListRuleProps, FilterablePriceProps, FilterablePriceRuleProps, FilterablePriceSetProps, - FilterableRuleTypeProps, PriceDTO, PriceListDTO, PriceListRuleDTO, @@ -24,13 +22,11 @@ import { PricingContext, PricingFilters, RemovePriceListRulesDTO, - RuleTypeDTO, SetPriceListRulesDTO, UpdatePriceListDTO, UpdatePriceListPricesDTO, UpdatePriceRuleDTO, UpdatePriceSetDTO, - UpdateRuleTypeDTO, UpsertPriceSetDTO, } from "./common" @@ -284,10 +280,6 @@ export interface IPricingModuleService extends IModuleService { * * ```ts * const priceSet = await pricingModuleService.createPriceSets({ - * rules: [ - * { rule_attribute: "region_id" }, - * { rule_attribute: "city" }, - * ], * prices: [ * { * amount: 300, @@ -351,10 +343,6 @@ export interface IPricingModuleService extends IModuleService { * }, * // price set with rules * { - * rules: [ - * { rule_attribute: "region_id" }, - * { rule_attribute: "city" }, - * ], * prices: [ * { * amount: 300, @@ -404,10 +392,6 @@ export interface IPricingModuleService extends IModuleService { * }, * ], * }, - * { - * id: "pset_123", - * rules: [{ rule_attribute: "region_id" }], - * }, * ]) */ upsertPriceSets( @@ -425,7 +409,7 @@ export interface IPricingModuleService extends IModuleService { * @example * const priceSet = await pricingModuleService.upsertPriceSets({ * id: "pset_123", - * rules: [{ rule_attribute: "region_id" }], + * prices: [{ amount: 100, currency_code: "USD" }], * }) */ upsertPriceSets( @@ -445,7 +429,7 @@ export interface IPricingModuleService extends IModuleService { * const priceSet = await pricingModuleService.updatePriceSets( * "pset_123", * { - * rules: [{ rule_attribute: "region_id" }], + * prices: [{ amount: 100, currency_code: "USD" }], * } * ) */ @@ -469,7 +453,7 @@ export interface IPricingModuleService extends IModuleService { * id: ["pset_123", "pset_321"], * }, * { - * rules: [{ rule_attribute: "region_id" }], + * prices: [{ amount: 100, currency_code: "USD" }], * } * ) */ @@ -593,215 +577,6 @@ export interface IPricingModuleService extends IModuleService { sharedContext?: Context ): Promise - /** - * This method is used to retrieve a rule type by its ID and and optionally based on the provided configurations. - * - * @param {string} id - The ID of the rule type to retrieve. - * @param {FindConfig} config - - * The configurations determining how the rule type is retrieved. Its properties, such as `select` or `relations`, accept the - * attributes or relations associated with a rule type. - * @param {Context} sharedContext - A context used to share resources, such as transaction manager, between the application and the module. - * @returns {Promise} The retrieved rule type. - * - * @example - * A simple example that retrieves a rule type by its code: - * - * ```ts - * const ruleType = - * await pricingModuleService.retrieveRuleType("rul-typ_123") - * ``` - * - * To specify relations that should be retrieved: - * - * ```ts - * const ruleType = await pricingModuleService.retrieveRuleType( - * "rul-typ_123", - * { - * relations: ["price_sets"], - * } - * ) - * ``` - */ - retrieveRuleType( - id: string, - config?: FindConfig, - sharedContext?: Context - ): Promise - - /** - * This method is used to retrieve a paginated list of rule types based on optional filters and configuration. - * - * @param {FilterableRuleTypeProps} filters - The filters to apply on the retrieved rule types. - * @param {FindConfig} config - - * The configurations determining how the rule types are retrieved. Its properties, such as `select` or `relations`, accept the - * attributes or relations associated with a rule type. - * @param {Context} sharedContext - A context used to share resources, such as transaction manager, between the application and the module. - * @returns {Promise} The list of rule types. - * - * @example - * - * To retrieve a list of rule types using their IDs: - * - * ```ts - * const ruleTypes = await pricingModuleService.listRuleTypes({ - * id: ["rul-typ_123", "rul-typ_321"], - * }) - * ``` - * - * To specify relations that should be retrieved: - * - * ```ts - * const ruleTypes = await pricingModuleService.listRuleTypes( - * { - * id: ["rul-typ_123", "rul-typ_321"], - * }, - * { - * relations: ["price_sets"], - * } - * ) - * ``` - * - * 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 - * const ruleTypes = await pricingModuleService.listRuleTypes( - * { - * id: ["rul-typ_123", "rul-typ_321"], - * }, - * { - * relations: ["price_sets"], - * take: 20, - * skip: 2, - * } - * ) - * ``` - */ - listRuleTypes( - filters?: FilterableRuleTypeProps, - config?: FindConfig, - sharedContext?: Context - ): Promise - - /** - * This method is used to retrieve a paginated list of rule types along with the total count of available rule types satisfying the provided filters. - * - * @param {FilterableRuleTypeProps} filters - The filters to apply on the retrieved rule types. - * @param {FindConfig} config - - * The configurations determining how the rule types are retrieved. Its properties, such as `select` or `relations`, accept the - * attributes or relations associated with a rule type. - * @param {Context} sharedContext - A context used to share resources, such as transaction manager, between the application and the module. - * @returns {Promise<[RuleTypeDTO[], number]>} The list of rule types along with their total count. - * - * @example - * - * To retrieve a list of rule types using their IDs: - * - * ```ts - * const [ruleTypes, count] = - * await pricingModuleService.listAndCountRuleTypes({ - * id: ["rul-typ_123", "rul-typ_321"], - * }) - * ``` - * - * To specify attributes that should be retrieved within the rule types: - * - * ```ts - * const [ruleTypes, count] = - * await pricingModuleService.listAndCountRuleTypes( - * { - * id: ["rul-typ_123", "rul-typ_321"], - * }, - * { - * relations: ["price_sets"], - * } - * ) - * ``` - * - * 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 - * const [ruleTypes, count] = - * await pricingModuleService.listAndCountRuleTypes( - * { - * id: ["rul-typ_123", "rul-typ_321"], - * }, - * { - * relations: ["price_sets"], - * take: 20, - * skip: 2, - * } - * ) - * ``` - */ - listAndCountRuleTypes( - filters?: FilterableRuleTypeProps, - config?: FindConfig, - sharedContext?: Context - ): Promise<[RuleTypeDTO[], number]> - - /** - * This method is used to create new rule types. - * - * @param {CreateRuleTypeDTO[]} data - The rule types 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 rule types. - * - * @example - * const ruleTypes = await pricingModuleService.createRuleTypes([ - * { - * name: "Region", - * rule_attribute: "region_id", - * }, - * { - * name: "Customer Group", - * rule_attribute: "customer_group_id", - * }, - * ]) - */ - createRuleTypes( - data: CreateRuleTypeDTO[], - sharedContext?: Context - ): Promise - - /** - * This method is used to update existing rule types with the provided data. - * - * @param {UpdateRuleTypeDTO[]} data - The rule types to update, each having the attributes that should be updated in a rule type. - * @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 rule types. - * - * @example - * const ruleTypes = await pricingModuleService.updateRuleTypes([ - * { - * id: "rul-typ_123", - * name: "Region", - * }, - * { - * id: "rul-typ_321", - * name: "Customer Group", - * }, - * ]) - */ - updateRuleTypes( - data: UpdateRuleTypeDTO[], - sharedContext?: Context - ): Promise - - /** - * This method is used to delete rule types based on the provided IDs. - * - * @param {string[]} ruleTypeIds - The IDs of the rule types 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 rule types are deleted. - * - * @example - * const ruleTypes = await pricingModuleService.deleteRuleTypes([ - * "rul-typ_123", - * "rul-typ_321", - * ]) - */ - deleteRuleTypes(ruleTypeIds: string[], sharedContext?: Context): Promise - /** * This method is used to retrieve a paginated list of prices based on optional filters and configuration. * @@ -1119,7 +894,7 @@ export interface IPricingModuleService extends IModuleService { * await pricingModuleService.createPriceRules([ * { * value: "VIP", - * rule_type_id: "rul-typ_123", + * attribute: "customer_group", * price_set_id: "pset_123", * }, * ]) diff --git a/packages/core/types/src/workflow/price-list/create-price-list.ts b/packages/core/types/src/workflow/price-list/create-price-list.ts index 38e9aae4d9..7075270765 100644 --- a/packages/core/types/src/workflow/price-list/create-price-list.ts +++ b/packages/core/types/src/workflow/price-list/create-price-list.ts @@ -8,7 +8,6 @@ export interface CreatePriceListDTO { starts_at?: string ends_at?: string status?: PriceListStatus - rules_count?: number rules?: PriceListRuleDTO[] prices?: { amount: number @@ -23,7 +22,7 @@ export interface CreatePriceListDTO { } export interface CreatePriceListRuleDTO { - rule_attribute: string + attribute: string value: string[] } diff --git a/packages/core/utils/src/pricing/__tests__/get-invalid-rule-attributes.spec.ts b/packages/core/utils/src/pricing/__tests__/get-invalid-rule-attributes.spec.ts deleted file mode 100644 index 2febb0d55d..0000000000 --- a/packages/core/utils/src/pricing/__tests__/get-invalid-rule-attributes.spec.ts +++ /dev/null @@ -1,17 +0,0 @@ -import { getInvalidRuleAttributes } from ".." - -describe("getInvalidRuleAttributes", function () { - it("should return list of rule attributes that matches reserved keywords", function () { - let result = getInvalidRuleAttributes(["shouldnotmatch"]) - expect(result).toEqual([]) - - result = getInvalidRuleAttributes(["currency_code", "shouldnotmatch"]) - expect(result).toEqual(["currency_code"]) - - result = getInvalidRuleAttributes(["currency_code", "price_list_id"]) - expect(result).toEqual(["currency_code", "price_list_id"]) - - result = getInvalidRuleAttributes(["shouldnotmatch", "quantity"]) - expect(result).toEqual(["quantity"]) - }) -}) diff --git a/packages/core/utils/src/pricing/__tests__/validate-rule-attributes.spec.ts b/packages/core/utils/src/pricing/__tests__/validate-rule-attributes.spec.ts deleted file mode 100644 index 994886023b..0000000000 --- a/packages/core/utils/src/pricing/__tests__/validate-rule-attributes.spec.ts +++ /dev/null @@ -1,28 +0,0 @@ -import { validateRuleAttributes } from ".." - -describe("validateRuleAttributes", function () { - it("should return void if there are no validation errors", function () { - let result = validateRuleAttributes(["shouldpasswithouterrors"]) - - expect(result).toEqual(undefined) - }) - - it("should throw an error if one of the array strings matches a reserved keyword", function () { - let error - - try { - validateRuleAttributes([ - "currency_code", - "shouldnotbepresent", - "quantity", - "price_list_id", - ]) - } catch (e) { - error = e - } - - expect(error.message).toEqual( - "Can't create rule_attribute with reserved keywords [quantity, currency_code, price_list_id] - quantity, currency_code, price_list_id" - ) - }) -}) diff --git a/packages/core/utils/src/pricing/builders.ts b/packages/core/utils/src/pricing/builders.ts index 4173b75c37..23aae3f22f 100644 --- a/packages/core/utils/src/pricing/builders.ts +++ b/packages/core/utils/src/pricing/builders.ts @@ -10,11 +10,10 @@ export function buildPriceListRules( priceListRules?: PriceListRuleDTO[] ): Record | undefined { return priceListRules?.reduce((acc, curr) => { - const ruleAttribute = curr.rule_type.rule_attribute - const ruleValues = curr.price_list_rule_values || [] - - acc[ruleAttribute] = ruleValues.map((ruleValue) => ruleValue.value) + const ruleAttribute = curr.attribute + const ruleValues = curr.value || [] + acc[ruleAttribute] = ruleValues return acc }, {}) } @@ -27,11 +26,10 @@ export function buildPriceSetRules( } return priceRules?.reduce((acc, curr) => { - const ruleAttribute = curr.rule_type.rule_attribute + const ruleAttribute = curr.attribute const ruleValue = curr.value acc[ruleAttribute] = ruleValue - return acc }, {}) } diff --git a/packages/core/utils/src/pricing/events.ts b/packages/core/utils/src/pricing/events.ts index 4597c30308..dfcfa584ec 100644 --- a/packages/core/utils/src/pricing/events.ts +++ b/packages/core/utils/src/pricing/events.ts @@ -2,24 +2,12 @@ import { buildEventNamesFromEntityName } from "../event-bus" import { Modules } from "../modules-sdk" const eventBaseNames: [ - "priceListRuleValue", "priceListRule", "priceList", "priceRule", - "priceSetRuleType", "priceSet", - "price", - "ruleType" -] = [ - "priceListRuleValue", - "priceListRule", - "priceList", - "priceRule", - "priceSetRuleType", - "priceSet", - "price", - "ruleType", -] + "price" +] = ["priceListRule", "priceList", "priceRule", "priceSet", "price"] export const PricingEvents = buildEventNamesFromEntityName( eventBaseNames, diff --git a/packages/core/utils/src/pricing/index.ts b/packages/core/utils/src/pricing/index.ts index 258ed59fe2..7e0a00cb5e 100644 --- a/packages/core/utils/src/pricing/index.ts +++ b/packages/core/utils/src/pricing/index.ts @@ -1,4 +1,3 @@ export * from "./builders" export * from "./price-list" -export * from "./rule-type" export * from "./events" diff --git a/packages/core/utils/src/pricing/rule-type.ts b/packages/core/utils/src/pricing/rule-type.ts deleted file mode 100644 index 9904f8c179..0000000000 --- a/packages/core/utils/src/pricing/rule-type.ts +++ /dev/null @@ -1,38 +0,0 @@ -import { MedusaError } from "../common" - -type RuleAttributeInput = string | undefined - -export const ReservedPricingRuleAttributes = [ - "quantity", - "currency_code", - "price_list_id", -] - -export const getInvalidRuleAttributes = ( - ruleAttributes: RuleAttributeInput[] -): string[] => { - const invalidRuleAttributes: string[] = [] - - for (const attribute of ReservedPricingRuleAttributes) { - if (ruleAttributes.indexOf(attribute) > -1) { - invalidRuleAttributes.push(attribute) - } - } - - return invalidRuleAttributes -} - -export const validateRuleAttributes = ( - ruleAttributes: RuleAttributeInput[] -): void => { - const invalidRuleAttributes = getInvalidRuleAttributes(ruleAttributes) - - if (invalidRuleAttributes.length) { - throw new MedusaError( - MedusaError.Types.INVALID_DATA, - `Can't create rule_attribute with reserved keywords [${ReservedPricingRuleAttributes.join( - ", " - )}] - ${invalidRuleAttributes.join(", ")}` - ) - } -} diff --git a/packages/medusa/src/api/admin/price-lists/query-config.ts b/packages/medusa/src/api/admin/price-lists/query-config.ts index 8ab6cae732..9c34e6fc6c 100644 --- a/packages/medusa/src/api/admin/price-lists/query-config.ts +++ b/packages/medusa/src/api/admin/price-lists/query-config.ts @@ -13,7 +13,7 @@ export const adminPriceListPriceRemoteQueryFields = [ "updated_at", "price_set.variant.id", "price_rules.value", - "price_rules.rule_type.rule_attribute", + "price_rules.attribute", ] export const adminPriceListRemoteQueryFields = [ @@ -27,8 +27,8 @@ export const adminPriceListRemoteQueryFields = [ "created_at", "updated_at", "deleted_at", - "price_list_rules.price_list_rule_values.value", - "price_list_rules.rule_type.rule_attribute", + "price_list_rules.value", + "price_list_rules.attribute", ...adminPriceListPriceRemoteQueryFields.map((field) => `prices.${field}`), ] diff --git a/packages/medusa/src/api/admin/pricing/helpers.ts b/packages/medusa/src/api/admin/pricing/helpers.ts deleted file mode 100644 index 2f9746d0c7..0000000000 --- a/packages/medusa/src/api/admin/pricing/helpers.ts +++ /dev/null @@ -1,23 +0,0 @@ -import { MedusaContainer } from "@medusajs/types" -import { - ContainerRegistrationKeys, - remoteQueryObjectFromString, -} from "@medusajs/utils" - -export const refetchRuleType = async ( - ruleTypeId: string, - scope: MedusaContainer, - fields: string[] -) => { - const remoteQuery = scope.resolve(ContainerRegistrationKeys.REMOTE_QUERY) - const queryObject = remoteQueryObjectFromString({ - entryPoint: "rule_type", - variables: { - filters: { id: ruleTypeId }, - }, - fields: fields, - }) - - const ruleTypes = await remoteQuery(queryObject) - return ruleTypes[0] -} diff --git a/packages/medusa/src/api/admin/pricing/middlewares.ts b/packages/medusa/src/api/admin/pricing/middlewares.ts deleted file mode 100644 index 1315190c35..0000000000 --- a/packages/medusa/src/api/admin/pricing/middlewares.ts +++ /dev/null @@ -1,60 +0,0 @@ -import { MiddlewareRoute } from "../../../loaders/helpers/routing/types" -import { validateAndTransformBody } from "../../utils/validate-body" -import { validateAndTransformQuery } from "../../utils/validate-query" -import * as QueryConfig from "./query-config" -import { - AdminCreatePricingRuleType, - AdminGetPricingRuleTypeParams, - AdminGetPricingRuleTypesParams, - AdminUpdatePricingRuleType, -} from "./validators" - -export const adminPricingRoutesMiddlewares: MiddlewareRoute[] = [ - { - method: ["GET"], - matcher: "/admin/pricing/rule-types", - middlewares: [ - validateAndTransformQuery( - AdminGetPricingRuleTypesParams, - QueryConfig.listTransformQueryConfig - ), - ], - }, - { - method: ["POST"], - matcher: "/admin/pricing/rule-types", - middlewares: [ - validateAndTransformBody(AdminCreatePricingRuleType), - validateAndTransformQuery( - AdminGetPricingRuleTypeParams, - QueryConfig.retrieveTransformQueryConfig - ), - ], - }, - { - method: ["GET"], - matcher: "/admin/pricing/rule-types/:id", - middlewares: [ - validateAndTransformQuery( - AdminGetPricingRuleTypeParams, - QueryConfig.retrieveTransformQueryConfig - ), - ], - }, - { - method: ["POST"], - matcher: "/admin/pricing/rule-types/:id", - middlewares: [ - validateAndTransformBody(AdminUpdatePricingRuleType), - validateAndTransformQuery( - AdminGetPricingRuleTypeParams, - QueryConfig.retrieveTransformQueryConfig - ), - ], - }, - { - method: ["DELETE"], - matcher: "/admin/pricing/rule-types/:id", - middlewares: [], - }, -] diff --git a/packages/medusa/src/api/admin/pricing/query-config.ts b/packages/medusa/src/api/admin/pricing/query-config.ts deleted file mode 100644 index c65e24e510..0000000000 --- a/packages/medusa/src/api/admin/pricing/query-config.ts +++ /dev/null @@ -1,16 +0,0 @@ -export const defaultAdminPricingRuleTypeFields = [ - "id", - "name", - "rule_attribute", - "default_priority", -] - -export const retrieveTransformQueryConfig = { - defaults: defaultAdminPricingRuleTypeFields, - isList: false, -} - -export const listTransformQueryConfig = { - ...retrieveTransformQueryConfig, - isList: true, -} diff --git a/packages/medusa/src/api/admin/pricing/rule-types/[id]/route.ts b/packages/medusa/src/api/admin/pricing/rule-types/[id]/route.ts deleted file mode 100644 index 914ef8592d..0000000000 --- a/packages/medusa/src/api/admin/pricing/rule-types/[id]/route.ts +++ /dev/null @@ -1,74 +0,0 @@ -import { - deletePricingRuleTypesWorkflow, - updatePricingRuleTypesWorkflow, -} from "@medusajs/core-flows" -import { - AuthenticatedMedusaRequest, - MedusaResponse, -} from "../../../../../types/routing" -import { - AdminGetPricingRuleTypeParamsType, - AdminUpdatePricingRuleTypeType, -} from "../../validators" -import { refetchRuleType } from "../../helpers" -import { MedusaError } from "@medusajs/utils" - -export const GET = async ( - req: AuthenticatedMedusaRequest, - res: MedusaResponse -) => { - const ruleType = await refetchRuleType( - req.params.id, - req.scope, - req.remoteQueryConfig.fields - ) - - if (!ruleType) { - throw new MedusaError( - MedusaError.Types.NOT_FOUND, - `RuleType with id: ${req.params.id} was not found` - ) - } - - res.status(200).json({ rule_type: ruleType }) -} - -export const POST = async ( - req: AuthenticatedMedusaRequest, - res: MedusaResponse -) => { - const workflow = updatePricingRuleTypesWorkflow(req.scope) - await workflow.run({ - input: { - data: [{ ...req.validatedBody, id: req.params.id }], - }, - }) - - const ruleType = await refetchRuleType( - req.params.id, - req.scope, - req.remoteQueryConfig.fields - ) - - res.status(200).json({ - rule_type: ruleType, - }) -} - -export const DELETE = async ( - req: AuthenticatedMedusaRequest, - res: MedusaResponse -) => { - const id = req.params.id - const workflow = deletePricingRuleTypesWorkflow(req.scope) - - await workflow.run({ - input: { ids: [id] }, - }) - - res.status(200).json({ - id, - object: "rule_type", - deleted: true, - }) -} diff --git a/packages/medusa/src/api/admin/pricing/rule-types/route.ts b/packages/medusa/src/api/admin/pricing/rule-types/route.ts deleted file mode 100644 index be5498bbb7..0000000000 --- a/packages/medusa/src/api/admin/pricing/rule-types/route.ts +++ /dev/null @@ -1,60 +0,0 @@ -import { createPricingRuleTypesWorkflow } from "@medusajs/core-flows" -import { - AuthenticatedMedusaRequest, - MedusaResponse, -} from "../../../../types/routing" -import { - AdminCreatePricingRuleTypeType, - AdminGetPricingRuleTypesParamsType, -} from "../validators" -import { - ContainerRegistrationKeys, - remoteQueryObjectFromString, -} from "@medusajs/utils" -import { refetchRuleType } from "../helpers" - -export const GET = async ( - req: AuthenticatedMedusaRequest, - res: MedusaResponse -) => { - const remoteQuery = req.scope.resolve(ContainerRegistrationKeys.REMOTE_QUERY) - const queryObject = remoteQueryObjectFromString({ - entryPoint: "rule_type", - variables: { - filters: req.filterableFields, - ...req.remoteQueryConfig.pagination, - }, - fields: req.remoteQueryConfig.fields, - }) - - const { rows: rule_types, metadata } = await remoteQuery(queryObject) - - res.json({ - rule_types: rule_types, - count: metadata.count, - offset: metadata.skip, - limit: metadata.take, - }) -} - -export const POST = async ( - req: AuthenticatedMedusaRequest, - res: MedusaResponse -) => { - const workflow = createPricingRuleTypesWorkflow(req.scope) - const ruleTypesData = [req.validatedBody] - - const { result } = await workflow.run({ - input: { data: ruleTypesData }, - }) - - const ruleType = await refetchRuleType( - result[0].id, - req.scope, - req.remoteQueryConfig.fields - ) - - res.status(200).json({ - rule_type: ruleType, - }) -} diff --git a/packages/medusa/src/api/admin/pricing/validators.ts b/packages/medusa/src/api/admin/pricing/validators.ts deleted file mode 100644 index 97d0a130d4..0000000000 --- a/packages/medusa/src/api/admin/pricing/validators.ts +++ /dev/null @@ -1,43 +0,0 @@ -import { z } from "zod" -import { createFindParams, createSelectParams } from "../../utils/validators" - -export type AdminGetPricingRuleTypeParamsType = z.infer< - typeof AdminGetPricingRuleTypeParams -> -export const AdminGetPricingRuleTypeParams = createSelectParams() - -export type AdminGetPricingRuleTypesParamsType = z.infer< - typeof AdminGetPricingRuleTypesParams -> -export const AdminGetPricingRuleTypesParams = createFindParams({ - limit: 100, - offset: 0, -}).merge( - z.object({ - rule_attribute: z.union([z.string(), z.array(z.string())]).optional(), - $and: z.lazy(() => AdminGetPricingRuleTypesParams.array()).optional(), - $or: z.lazy(() => AdminGetPricingRuleTypesParams.array()).optional(), - }) -) - -export type AdminCreatePricingRuleTypeType = z.infer< - typeof AdminCreatePricingRuleType -> -export const AdminCreatePricingRuleType = z - .object({ - name: z.string(), - rule_attribute: z.string(), - default_priority: z.number(), - }) - .strict() - -export type AdminUpdatePricingRuleTypeType = z.infer< - typeof AdminUpdatePricingRuleType -> -export const AdminUpdatePricingRuleType = z - .object({ - name: z.string().optional(), - rule_attribute: z.string().optional(), - default_priority: z.number().optional(), - }) - .strict() diff --git a/packages/medusa/src/api/admin/products/helpers.ts b/packages/medusa/src/api/admin/products/helpers.ts index 98ee6aae8e..f263503edf 100644 --- a/packages/medusa/src/api/admin/products/helpers.ts +++ b/packages/medusa/src/api/admin/products/helpers.ts @@ -92,7 +92,7 @@ export const buildRules = (price: PriceDTO) => { const rules: Record = {} for (const priceRule of price.price_rules || []) { - const ruleAttribute = priceRule.rule_type?.rule_attribute + const ruleAttribute = priceRule.attribute if (ruleAttribute) { rules[ruleAttribute] = priceRule.value diff --git a/packages/medusa/src/api/admin/products/query-config.ts b/packages/medusa/src/api/admin/products/query-config.ts index 6155530199..1b7264b2e5 100644 --- a/packages/medusa/src/api/admin/products/query-config.ts +++ b/packages/medusa/src/api/admin/products/query-config.ts @@ -23,7 +23,7 @@ export const defaultAdminProductsVariantFields = [ "barcode", "*prices", "prices.price_rules.value", - "prices.price_rules.rule_type.rule_attribute", + "prices.price_rules.attribute", "*options", ] @@ -85,7 +85,7 @@ export const defaultAdminProductFields = [ "*variants", "*variants.prices", "variants.prices.price_rules.value", - "variants.prices.price_rules.rule_type.rule_attribute", + "variants.prices.price_rules.attribute", "*variants.options", "*sales_channels", ] diff --git a/packages/medusa/src/api/middlewares.ts b/packages/medusa/src/api/middlewares.ts index d1928d954d..98773b386e 100644 --- a/packages/medusa/src/api/middlewares.ts +++ b/packages/medusa/src/api/middlewares.ts @@ -14,7 +14,6 @@ import { adminInviteRoutesMiddlewares } from "./admin/invites/middlewares" import { adminOrderRoutesMiddlewares } from "./admin/orders/middlewares" import { adminPaymentRoutesMiddlewares } from "./admin/payments/middlewares" import { adminPriceListsRoutesMiddlewares } from "./admin/price-lists/middlewares" -import { adminPricingRoutesMiddlewares } from "./admin/pricing/middlewares" import { adminProductCategoryRoutesMiddlewares } from "./admin/product-categories/middlewares" import { adminProductTypeRoutesMiddlewares } from "./admin/product-types/middlewares" import { adminProductTagRoutesMiddlewares } from "./admin/product-tags/middlewares" @@ -81,7 +80,6 @@ export const config: MiddlewaresConfig = { ...adminPriceListsRoutesMiddlewares, ...adminInventoryRoutesMiddlewares, ...adminCollectionRoutesMiddlewares, - ...adminPricingRoutesMiddlewares, ...adminShippingOptionRoutesMiddlewares, ...adminDraftOrderRoutesMiddlewares, ...adminSalesChannelRoutesMiddlewares, diff --git a/packages/modules/pricing/integration-tests/__fixtures__/price-list-rules/data.ts b/packages/modules/pricing/integration-tests/__fixtures__/price-list-rules/data.ts index 7c7cb9ecd8..3d33dbfca4 100644 --- a/packages/modules/pricing/integration-tests/__fixtures__/price-list-rules/data.ts +++ b/packages/modules/pricing/integration-tests/__fixtures__/price-list-rules/data.ts @@ -2,11 +2,13 @@ export const defaultPriceListRuleData = [ { id: "price-list-rule-1", price_list_id: "price-list-1", - rule_type_id: "rule-type-1", + attribute: "currency_code", + value: [], }, { id: "price-list-rule-2", price_list_id: "price-list-1", - rule_type_id: "rule-type-2", + attribute: "region_id", + value: [], }, ] diff --git a/packages/modules/pricing/integration-tests/__fixtures__/price-rule/data.ts b/packages/modules/pricing/integration-tests/__fixtures__/price-rule/data.ts index 506e560743..83394428ac 100644 --- a/packages/modules/pricing/integration-tests/__fixtures__/price-rule/data.ts +++ b/packages/modules/pricing/integration-tests/__fixtures__/price-rule/data.ts @@ -6,7 +6,7 @@ export const defaultPriceRuleData = [ { id: "price-rule-1", price_set_id: "price-set-1", - rule_type_id: "rule-type-1", + attribute: "currency_code", value: "USD", price_list_id: "test", price_id: "price-set-money-amount-USD", @@ -14,7 +14,7 @@ export const defaultPriceRuleData = [ { id: "price-rule-2", price_set_id: "price-set-2", - rule_type_id: "rule-type-2", + attribute: "region_id", value: "region_1", price_list_id: "test", price_id: "price-set-money-amount-EUR", diff --git a/packages/modules/pricing/integration-tests/__fixtures__/price-rule/index.ts b/packages/modules/pricing/integration-tests/__fixtures__/price-rule/index.ts index e38b4768b4..1df682da38 100644 --- a/packages/modules/pricing/integration-tests/__fixtures__/price-rule/index.ts +++ b/packages/modules/pricing/integration-tests/__fixtures__/price-rule/index.ts @@ -16,7 +16,7 @@ export async function createPriceRules( const priceRuleDataClone: CreatePriceRuleDTO = { ...priceRuleData } priceRuleDataClone.price_set_id = priceRuleDataClone.price_set_id - priceRuleDataClone.rule_type_id = priceRuleDataClone.rule_type_id + priceRuleDataClone.attribute = priceRuleDataClone.attribute priceRuleDataClone.price_id = priceRuleDataClone.price_id const priceRule = manager.create(PriceRule, priceRuleDataClone) diff --git a/packages/modules/pricing/integration-tests/__fixtures__/rule-type/data.ts b/packages/modules/pricing/integration-tests/__fixtures__/rule-type/data.ts deleted file mode 100644 index fbfe6b62d4..0000000000 --- a/packages/modules/pricing/integration-tests/__fixtures__/rule-type/data.ts +++ /dev/null @@ -1,12 +0,0 @@ -export const defaultRuleTypesData = [ - { - id: "rule-type-1", - name: "rule 1", - rule_attribute: "currency_code", - }, - { - id: "rule-type-2", - name: "rule 2", - rule_attribute: "region_id", - }, -] diff --git a/packages/modules/pricing/integration-tests/__fixtures__/rule-type/index.ts b/packages/modules/pricing/integration-tests/__fixtures__/rule-type/index.ts deleted file mode 100644 index 837ae8f3e0..0000000000 --- a/packages/modules/pricing/integration-tests/__fixtures__/rule-type/index.ts +++ /dev/null @@ -1,22 +0,0 @@ -import { SqlEntityManager } from "@mikro-orm/postgresql" -import { RuleType } from "@models" -import { defaultRuleTypesData } from "./data" - -export * from "./data" - -export async function createRuleTypes( - manager: SqlEntityManager, - ruletypesData: any[] = defaultRuleTypesData -): Promise { - const ruleTypes: RuleType[] = [] - - for (let ruleTypeData of ruletypesData) { - const ruleType = manager.create(RuleType, ruleTypeData) - - ruleTypes.push(ruleType) - } - - await manager.persistAndFlush(ruleTypes) - - return ruleTypes -} diff --git a/packages/modules/pricing/integration-tests/__fixtures__/seed-price-data.ts b/packages/modules/pricing/integration-tests/__fixtures__/seed-price-data.ts index 514fe1685e..5574c64018 100644 --- a/packages/modules/pricing/integration-tests/__fixtures__/seed-price-data.ts +++ b/packages/modules/pricing/integration-tests/__fixtures__/seed-price-data.ts @@ -2,7 +2,6 @@ import { SqlEntityManager } from "@mikro-orm/postgresql" import { createPrices, defaultPricesData } from "./price" import { createPriceRules, defaultPriceRuleData } from "./price-rule" import { createPriceSets, defaultPriceSetsData } from "./price-set" -import { createRuleTypes, defaultRuleTypesData } from "./rule-type" jest.setTimeout(30000) @@ -12,11 +11,9 @@ export async function seedPriceData( priceSetsData = defaultPriceSetsData, priceRuleData = defaultPriceRuleData, pricesData = defaultPricesData, - ruleTypesData = defaultRuleTypesData, } = {} ) { await createPriceSets(testManager, priceSetsData) await createPrices(testManager, pricesData) - await createRuleTypes(testManager, ruleTypesData) await createPriceRules(testManager, priceRuleData) } diff --git a/packages/modules/pricing/integration-tests/__tests__/services/pricing-module/calculate-price.spec.ts b/packages/modules/pricing/integration-tests/__tests__/services/pricing-module/calculate-price.spec.ts index 11a5a5f3f2..9bcfbd3ab3 100644 --- a/packages/modules/pricing/integration-tests/__tests__/services/pricing-module/calculate-price.spec.ts +++ b/packages/modules/pricing/integration-tests/__tests__/services/pricing-module/calculate-price.spec.ts @@ -154,32 +154,11 @@ moduleIntegrationTestRunner({ }, ] - const ruleTypesData = [ - { - id: "rule-type-company_id", - name: "rule type company id", - rule_attribute: "company_id", - default_priority: 2, - }, - { - id: "rule-type-region_id", - name: "rule type region id", - rule_attribute: "region_id", - default_priority: 1, - }, - { - id: "rule-type-customer_group_id", - name: "rule type customer group id", - rule_attribute: "customer_group_id", - default_priority: 3, - }, - ] - const priceRuleData = [ { id: "price-rule-company_id-EUR", price_set_id: "price-set-EUR", - rule_type_id: "rule-type-company_id", + attribute: "company_id", value: "EUR", price_list_id: "test", price_id: "price-company_id-EUR", @@ -187,7 +166,7 @@ moduleIntegrationTestRunner({ { id: "price-rule-company_id-PLN", price_set_id: "price-set-PLN", - rule_type_id: "rule-type-company_id", + attribute: "company_id", value: "medusa-company-id", price_list_id: "test", price_id: "price-company_id-PLN", @@ -195,7 +174,7 @@ moduleIntegrationTestRunner({ { id: "price-rule-region_id-PLN", price_set_id: "price-set-PLN", - rule_type_id: "rule-type-region_id", + attribute: "region_id", value: "PL", price_list_id: "test", price_id: "price-region_id-PLN", @@ -203,7 +182,7 @@ moduleIntegrationTestRunner({ { id: "price-rule-region_id+company_id-PL", price_set_id: "price-set-PLN", - rule_type_id: "rule-type-region_id", + attribute: "region_id", value: "PL", price_list_id: "test", price_id: "price-region_id+company_id-PLN", @@ -211,7 +190,7 @@ moduleIntegrationTestRunner({ { id: "price-rule-region_id+company_id-medusa-company-id", price_set_id: "price-set-PLN", - rule_type_id: "rule-type-company_id", + attribute: "company_id", value: "medusa-company-id", price_list_id: "test", price_id: "price-region_id+company_id-PLN", @@ -219,7 +198,7 @@ moduleIntegrationTestRunner({ { id: "price-rule-region_id-PLN-5-qty", price_set_id: "price-set-PLN", - rule_type_id: "rule-type-region_id", + attribute: "region_id", value: "PL", price_list_id: "test", price_id: "price-region_id-PLN-5-qty", @@ -227,7 +206,7 @@ moduleIntegrationTestRunner({ { id: "price-rule-region_id-company_id-PL", price_set_id: "price-set-PLN", - rule_type_id: "rule-type-region_id", + attribute: "region_id", value: "PL", price_list_id: "test", price_id: "price-region_id_company_id-PL-EUR", @@ -235,7 +214,7 @@ moduleIntegrationTestRunner({ { id: "price-rule-region_id-company_id-PLN", price_set_id: "price-set-PLN", - rule_type_id: "rule-type-company_id", + attribute: "company_id", value: "medusa-company-id", price_list_id: "test", price_id: "price-region_id_company_id-PL-EUR", @@ -243,7 +222,7 @@ moduleIntegrationTestRunner({ { id: "price-rule-region_id-company_id-PL-4-qty", price_set_id: "price-set-PLN", - rule_type_id: "rule-type-region_id", + attribute: "region_id", value: "PL", price_list_id: "test", price_id: "price-region_id_company_id-PL-EUR-4-qty", @@ -251,7 +230,7 @@ moduleIntegrationTestRunner({ { id: "price-rule-region_id-company_id-PLN-4-qty", price_set_id: "price-set-PLN", - rule_type_id: "rule-type-company_id", + attribute: "company_id", value: "medusa-company-id", price_list_id: "test", price_id: "price-region_id_company_id-PL-EUR-4-qty", @@ -259,7 +238,7 @@ moduleIntegrationTestRunner({ { id: "price-rule-region_id-currency_customer_group_code-PL", price_set_id: "price-set-PLN", - rule_type_id: "rule-type-region_id", + attribute: "region_id", value: "PL", price_list_id: "test", price_id: "price-region_id_company_id-PL-EUR-customer-group", @@ -267,7 +246,7 @@ moduleIntegrationTestRunner({ { id: "price-rule-region_id-currency_customer_group_code-PLN", price_set_id: "price-set-PLN", - rule_type_id: "rule-type-company_id", + attribute: "company_id", value: "medusa-company-id", price_list_id: "test", price_id: "price-region_id_company_id-PL-EUR-customer-group", @@ -275,7 +254,7 @@ moduleIntegrationTestRunner({ { id: "price-rule-region_id-currency_customer_group_code-test_customer_group", price_set_id: "price-set-PLN", - rule_type_id: "rule-type-customer_group_id", + attribute: "customer_group_id", value: "test-customer-group", price_list_id: "test", price_id: "price-region_id_company_id-PL-EUR-customer-group", @@ -286,7 +265,6 @@ moduleIntegrationTestRunner({ priceSetsData, pricesData, priceRuleData, - ruleTypesData, }) }) diff --git a/packages/modules/pricing/integration-tests/__tests__/services/pricing-module/price-list-rule.spec.ts b/packages/modules/pricing/integration-tests/__tests__/services/pricing-module/price-list-rule.spec.ts index 5f7163aa20..114d5d2966 100644 --- a/packages/modules/pricing/integration-tests/__tests__/services/pricing-module/price-list-rule.spec.ts +++ b/packages/modules/pricing/integration-tests/__tests__/services/pricing-module/price-list-rule.spec.ts @@ -3,7 +3,6 @@ import { SqlEntityManager } from "@mikro-orm/postgresql" import { moduleIntegrationTestRunner } from "medusa-test-utils" import { createPriceLists } from "../../../__fixtures__/price-list" import { createPriceListRules } from "../../../__fixtures__/price-list-rules" -import { createRuleTypes } from "../../../__fixtures__/rule-type" import { Modules } from "@medusajs/utils" jest.setTimeout(30000) @@ -15,7 +14,6 @@ moduleIntegrationTestRunner({ let testManager: SqlEntityManager beforeEach(async () => { testManager = await MikroOrmWrapper.forkManager() - await createRuleTypes(testManager) await createPriceLists(testManager) await createPriceListRules(testManager) }) @@ -166,14 +164,6 @@ moduleIntegrationTestRunner({ describe("setPriceListRules", () => { it("should add a price list rule to a price list", async () => { - await createRuleTypes(testManager, [ - { - id: "rule-type-3", - name: "test", - rule_attribute: "sales_channel", - }, - ]) - await service.setPriceListRules({ price_list_id: "price-list-1", rules: { @@ -184,34 +174,21 @@ moduleIntegrationTestRunner({ const [priceList] = await service.listPriceLists( { id: ["price-list-1"] }, { - relations: [ - "price_list_rules", - "price_list_rules.price_list_rule_values", - ], + relations: ["price_list_rules"], } ) expect(priceList.price_list_rules).toEqual( expect.arrayContaining([ expect.objectContaining({ - rule_type: { id: "rule-type-3" }, - price_list_rule_values: [ - expect.objectContaining({ value: "sc-1" }), - ], + attribute: "sales_channel", + value: "sc-1", }), ]) ) }) it("should multiple priceListRules to a priceList", async () => { - await createRuleTypes(testManager, [ - { - id: "rule-type-3", - name: "test", - rule_attribute: "sales_channel", - }, - ]) - await service.setPriceListRules({ price_list_id: "price-list-1", rules: { @@ -224,21 +201,15 @@ moduleIntegrationTestRunner({ id: ["price-list-1"], }, { - relations: [ - "price_list_rules", - "price_list_rules.price_list_rule_values", - ], + relations: ["price_list_rules"], } ) expect(priceList.price_list_rules).toEqual( expect.arrayContaining([ expect.objectContaining({ - rule_type: { id: "rule-type-3" }, - price_list_rule_values: expect.arrayContaining([ - expect.objectContaining({ value: "sc-1" }), - expect.objectContaining({ value: "sc-2" }), - ]), + attribute: "sales_channel", + value: ["sc-1", "sc-2"], }), ]) ) @@ -262,7 +233,7 @@ moduleIntegrationTestRunner({ ) expect(priceList.price_list_rules).toEqual([ - expect.objectContaining({ rule_type: { id: "rule-type-2" } }), + expect.objectContaining({ attribute: "region_id" }), ]) }) }) diff --git a/packages/modules/pricing/integration-tests/__tests__/services/pricing-module/price-list.spec.ts b/packages/modules/pricing/integration-tests/__tests__/services/pricing-module/price-list.spec.ts index 860479210d..8e37791c8f 100644 --- a/packages/modules/pricing/integration-tests/__tests__/services/pricing-module/price-list.spec.ts +++ b/packages/modules/pricing/integration-tests/__tests__/services/pricing-module/price-list.spec.ts @@ -32,16 +32,6 @@ moduleIntegrationTestRunner({ const testManager = await MikroOrmWrapper.forkManager() await createPriceSets(testManager) await createPriceLists(testManager) - await service.createRuleTypes([ - { - name: "Region ID", - rule_attribute: "region_id", - }, - { - name: "Customer Group ID", - rule_attribute: "customer_group_id", - }, - ]) }) describe("list", () => { @@ -287,20 +277,15 @@ moduleIntegrationTestRunner({ const [priceList] = await service.listPriceLists( { id: [createdId] }, { - relations: [ - "prices", - "prices.price_set", - "price_list_rules.price_list_rule_values", - "price_list_rules.rule_type", - ], + relations: ["prices", "price_list_rules"], select: [ "id", "starts_at", "prices.amount", "prices.currency_code", "prices.price_list_id", - "price_list_rules.price_list_rule_values.value", - "price_list_rules.rule_type.rule_attribute", + "price_list_rules.value", + "price_list_rules.attribute", ], } ) @@ -317,17 +302,8 @@ moduleIntegrationTestRunner({ ]), price_list_rules: expect.arrayContaining([ expect.objectContaining({ - id: expect.any(String), - rule_type: expect.objectContaining({ - id: expect.any(String), - rule_attribute: "new_rule", - }), - price_list_rule_values: [ - expect.objectContaining({ - id: expect.any(String), - value: "new-rule-value", - }), - ], + attribute: "new_rule", + value: ["new-rule-value"], }), ]), }) @@ -349,7 +325,7 @@ moduleIntegrationTestRunner({ } expect(error.message).toEqual( - 'PriceList with id "does-not-exist" not found' + "Price lists with ids: 'does-not-exist' not found" ) }) }) @@ -439,19 +415,14 @@ moduleIntegrationTestRunner({ id: [created.id], }, { - relations: [ - "prices", - "prices.price_set", - "price_list_rules.price_list_rule_values", - "price_list_rules.rule_type", - ], + relations: ["prices", "prices.price_set", "price_list_rules"], select: [ "id", "prices.amount", "prices.currency_code", "prices.price_list_id", - "price_list_rules.price_list_rule_values.value", - "price_list_rules.rule_type.rule_attribute", + "price_list_rules.value", + "price_list_rules.attribute", ], } ) @@ -468,37 +439,16 @@ moduleIntegrationTestRunner({ price_list_rules: expect.arrayContaining([ expect.objectContaining({ id: expect.any(String), - rule_type: expect.objectContaining({ - id: expect.any(String), - rule_attribute: "customer_group_id", - }), - price_list_rule_values: expect.arrayContaining([ - expect.objectContaining({ - id: expect.any(String), - value: "vip-customer-group-id", - }), - expect.objectContaining({ - id: expect.any(String), - value: "another-vip-customer-group-id", - }), - ]), + attribute: "customer_group_id", + value: [ + "vip-customer-group-id", + "another-vip-customer-group-id", + ], }), expect.objectContaining({ id: expect.any(String), - rule_type: expect.objectContaining({ - id: expect.any(String), - rule_attribute: "region_id", - }), - price_list_rule_values: expect.arrayContaining([ - expect.objectContaining({ - id: expect.any(String), - value: "DE", - }), - expect.objectContaining({ - id: expect.any(String), - value: "DK", - }), - ]), + attribute: "region_id", + value: ["DE", "DK"], }), ]), }) @@ -581,8 +531,7 @@ moduleIntegrationTestRunner({ "prices", "prices.price_set", "prices.price_rules", - "price_list_rules.price_list_rule_values", - "price_list_rules.rule_type", + "price_list_rules", ], select: [ "id", @@ -591,8 +540,8 @@ moduleIntegrationTestRunner({ "prices.amount", "prices.currency_code", "prices.price_list_id", - "price_list_rules.price_list_rule_values.value", - "price_list_rules.rule_type.rule_attribute", + "price_list_rules.value", + "price_list_rules.attribute", ], } ) @@ -622,72 +571,21 @@ moduleIntegrationTestRunner({ price_list_rules: expect.arrayContaining([ expect.objectContaining({ id: expect.any(String), - rule_type: expect.objectContaining({ - id: expect.any(String), - rule_attribute: "customer_group_id", - }), - price_list_rule_values: expect.arrayContaining([ - expect.objectContaining({ - id: expect.any(String), - value: "vip-customer-group-id", - }), - expect.objectContaining({ - id: expect.any(String), - value: "another-vip-customer-group-id", - }), - ]), + attribute: "customer_group_id", + value: [ + "vip-customer-group-id", + "another-vip-customer-group-id", + ], }), expect.objectContaining({ id: expect.any(String), - rule_type: expect.objectContaining({ - id: expect.any(String), - rule_attribute: "region_id", - }), - price_list_rule_values: expect.arrayContaining([ - expect.objectContaining({ - id: expect.any(String), - value: "DE", - }), - expect.objectContaining({ - id: expect.any(String), - value: "DK", - }), - ]), + attribute: "region_id", + value: ["DE", "DK"], }), ]), }) ) }) - - it("should throw error when rule type does not exist", async () => { - const error = await service - .createPriceLists([ - { - title: "test", - description: "test", - rules: { - region_id: ["DE", "DK"], - missing_1: ["test-missing-1"], - }, - prices: [ - { - amount: 400, - currency_code: "EUR", - price_set_id: "price-set-1", - rules: { - region_id: "DE", - missing_2: "test-missing-2", - }, - }, - ], - }, - ]) - .catch((e) => e) - - expect(error.message).toEqual( - "Cannot find RuleTypes with rule_attribute - missing_1, missing_2" - ) - }) }) describe("addPriceListPrices", () => { @@ -714,8 +612,7 @@ moduleIntegrationTestRunner({ "prices", "prices.price_set", "prices.price_rules", - "price_list_rules.price_list_rule_values", - "price_list_rules.rule_type", + "price_list_rules", ], select: [ "id", @@ -724,8 +621,8 @@ moduleIntegrationTestRunner({ "prices.amount", "prices.currency_code", "prices.price_list_id", - "price_list_rules.price_list_rule_values.value", - "price_list_rules.rule_type.rule_attribute", + "price_list_rules.value", + "price_list_rules.attribute", ], } ) @@ -746,13 +643,6 @@ moduleIntegrationTestRunner({ }) it("should add a price with rules to a priceList successfully", async () => { - await service.createRuleTypes([ - { - name: "region_id", - rule_attribute: "region_id", - }, - ]) - await service.addPriceListPrices([ { price_list_id: "price-list-1", @@ -778,20 +668,18 @@ moduleIntegrationTestRunner({ "prices", "prices.price_set", "prices.price_rules", - "prices.price_rules.rule_type", - "price_list_rules.price_list_rule_values", - "price_list_rules.rule_type", + "price_list_rules", ], select: [ "id", "prices.price_rules.value", - "prices.price_rules.rule_type.rule_attribute", + "prices.price_rules.attribute", "prices.rules_count", "prices.amount", "prices.currency_code", "prices.price_list_id", - "price_list_rules.price_list_rule_values.value", - "price_list_rules.rule_type.rule_attribute", + "price_list_rules.value", + "price_list_rules.attribute", ], } ) @@ -805,9 +693,7 @@ moduleIntegrationTestRunner({ price_rules: [ expect.objectContaining({ value: "EU", - rule_type: expect.objectContaining({ - rule_attribute: "region_id", - }), + attribute: "region_id", }), ], amount: 123, @@ -884,20 +770,18 @@ moduleIntegrationTestRunner({ "prices", "prices.price_set", "prices.price_rules", - "prices.price_rules.rule_type", - "price_list_rules.price_list_rule_values", - "price_list_rules.rule_type", + "price_list_rules", ], select: [ "id", "prices.price_rules.value", - "prices.price_rules.rule_type.rule_attribute", + "prices.price_rules.attribute", "prices.rules_count", "prices.amount", "prices.currency_code", "prices.price_list_id", - "price_list_rules.price_list_rule_values.value", - "price_list_rules.rule_type.rule_attribute", + "price_list_rules.value", + "price_list_rules.attribute", ], } ) @@ -911,15 +795,11 @@ moduleIntegrationTestRunner({ price_rules: expect.arrayContaining([ expect.objectContaining({ value: "new test", - rule_type: expect.objectContaining({ - rule_attribute: "region_id", - }), + attribute: "region_id", }), expect.objectContaining({ value: "new test", - rule_type: expect.objectContaining({ - rule_attribute: "customer_group_id", - }), + attribute: "customer_group_id", }), ]), amount: 123, @@ -934,9 +814,7 @@ moduleIntegrationTestRunner({ describe("removePrices", () => { it("should remove prices from a priceList successfully", async () => { - const [priceSet] = await service.createPriceSets([ - { rules: [{ rule_attribute: "region_id" }] }, - ]) + const [priceSet] = await service.createPriceSets([{}]) await service.addPriceListPrices([ { diff --git a/packages/modules/pricing/integration-tests/__tests__/services/pricing-module/price-rule.spec.ts b/packages/modules/pricing/integration-tests/__tests__/services/pricing-module/price-rule.spec.ts index 4117bc74b1..24c95fdb46 100644 --- a/packages/modules/pricing/integration-tests/__tests__/services/pricing-module/price-rule.spec.ts +++ b/packages/modules/pricing/integration-tests/__tests__/services/pricing-module/price-rule.spec.ts @@ -5,7 +5,6 @@ import { Price } from "../../../../src/models" import { createPrices } from "../../../__fixtures__/price" import { createPriceRules } from "../../../__fixtures__/price-rule" import { createPriceSets } from "../../../__fixtures__/price-set" -import { createRuleTypes } from "../../../__fixtures__/rule-type" import { Modules } from "@medusajs/utils" jest.setTimeout(30000) @@ -19,7 +18,6 @@ moduleIntegrationTestRunner({ testManager = await MikroOrmWrapper.forkManager() await createPriceSets(testManager) - await createRuleTypes(testManager) await createPrices(testManager) await createPriceRules(testManager) }) @@ -57,8 +55,8 @@ moduleIntegrationTestRunner({ id: ["price-rule-1"], }, { - select: ["id", "price_set.id"], - relations: ["price_set"], + select: ["id", "price.id"], + relations: ["price"], } ) @@ -67,10 +65,10 @@ moduleIntegrationTestRunner({ expect(serialized).toEqual([ { id: "price-rule-1", - price_set: { - id: "price-set-1", + price: { + id: "price-set-money-amount-USD", }, - price_set_id: "price-set-1", + price_id: "price-set-money-amount-USD", }, ]) }) @@ -112,8 +110,8 @@ moduleIntegrationTestRunner({ id: ["price-rule-1"], }, { - select: ["id", "price_set.id"], - relations: ["price_set"], + select: ["id", "price.id"], + relations: ["price"], } ) @@ -123,10 +121,10 @@ moduleIntegrationTestRunner({ expect(serialized).toEqual([ { id: "price-rule-1", - price_set: { - id: "price-set-1", + price: { + id: "price-set-money-amount-USD", }, - price_set_id: "price-set-1", + price_id: "price-set-money-amount-USD", }, ]) }) @@ -283,7 +281,7 @@ moduleIntegrationTestRunner({ await service.createPriceRules({ id: "price-rule-new", price_set_id: "price-set-1", - rule_type_id: "rule-type-1", + attribute: "region_id", value: "region_1", price_list_id: "test", price_id: price.id, diff --git a/packages/modules/pricing/integration-tests/__tests__/services/pricing-module/price-set.spec.ts b/packages/modules/pricing/integration-tests/__tests__/services/pricing-module/price-set.spec.ts index 8230a3a4ef..dd23de31be 100644 --- a/packages/modules/pricing/integration-tests/__tests__/services/pricing-module/price-set.spec.ts +++ b/packages/modules/pricing/integration-tests/__tests__/services/pricing-module/price-set.spec.ts @@ -1,39 +1,18 @@ -import { - CreatePriceSetDTO, - CreatePriceSetRuleTypeDTO, - IPricingModuleService, -} from "@medusajs/types" +import { CreatePriceSetDTO, IPricingModuleService } from "@medusajs/types" import { CommonEvents, composeMessage, Modules, PricingEvents, } from "@medusajs/utils" -import { SqlEntityManager } from "@mikro-orm/postgresql" import { MockEventBusService, moduleIntegrationTestRunner, } from "medusa-test-utils" -import { PriceSetRuleType } from "../../../../src/models" import { seedPriceData } from "../../../__fixtures__/seed-price-data" jest.setTimeout(30000) -async function createPriceSetPriceRules( - manager: SqlEntityManager, - priceSetRulesData: CreatePriceSetRuleTypeDTO[] -): Promise { - const priceSetRules: PriceSetRuleType[] = [] - - for (let priceSetRuleData of priceSetRulesData) { - const priceRule = manager.create(PriceSetRuleType, priceSetRuleData as any) - - priceSetRules.push(priceRule) - } - - await manager.persistAndFlush(priceSetRules) -} - moduleIntegrationTestRunner({ moduleName: Modules.PRICING, testSuite: ({ MikroOrmWrapper, service }) => { @@ -51,16 +30,6 @@ moduleIntegrationTestRunner({ beforeEach(async () => { const testManager = await MikroOrmWrapper.forkManager() await seedPriceData(testManager) - await createPriceSetPriceRules(testManager, [ - { - price_set_id: "price-set-1", - rule_type_id: "rule-type-1", - }, - { - price_set_id: "price-set-2", - rule_type_id: "rule-type-2", - }, - ]) }) describe("list", () => { @@ -346,32 +315,6 @@ moduleIntegrationTestRunner({ }) describe("create", () => { - it("should fail to create a price set with rule types and money amounts with rule types that don't exits", async () => { - let error - - try { - await service.createPriceSets([ - { - rules: [{ rule_attribute: "region_id" }], - prices: [ - { - amount: 100, - currency_code: "USD", - rules: { - city: "Berlin", - }, - }, - ], - }, - ]) - } catch (e) { - error = e - } - expect(error.message).toEqual( - "Rule types don't exist for prices with rule attribute: city" - ) - }) - it("should create a price set with rule types and money amounts", async () => { const [priceSet] = await service.createPriceSets([ { @@ -399,10 +342,6 @@ moduleIntegrationTestRunner({ }) ) - const [priceRules] = await service.listPriceRules({ - price_set_id: [priceSet.id], - }) - const events = eventBusEmitSpy.mock.calls[0][0] expect(events).toHaveLength(3) expect(events[0]).toEqual( @@ -429,7 +368,7 @@ moduleIntegrationTestRunner({ action: CommonEvents.CREATED, object: "price_rule", data: { - id: priceRules.id, + id: (priceSet as any).prices![0].price_rules[0].id, }, }) ) @@ -591,26 +530,6 @@ moduleIntegrationTestRunner({ }), ]) }) - - it("should fail with an appropriate error when trying to add a price with rule that doesn't exist", async () => { - let error - try { - await service.addPrices({ - priceSetId: "price-set-1", - prices: [ - { - amount: 100, - currency_code: "USD", - rules: { city: "Paris" }, - }, - ], - }) - } catch (e) { - error = e - } - - expect(error.message).toEqual("Rule types don't exist for: city") - }) }) }) }, diff --git a/packages/modules/pricing/integration-tests/__tests__/services/pricing-module/rule-type.spec.ts b/packages/modules/pricing/integration-tests/__tests__/services/pricing-module/rule-type.spec.ts deleted file mode 100644 index 99b84c3430..0000000000 --- a/packages/modules/pricing/integration-tests/__tests__/services/pricing-module/rule-type.spec.ts +++ /dev/null @@ -1,239 +0,0 @@ -import { createRuleTypes } from "../../../__fixtures__/rule-type" -import { moduleIntegrationTestRunner } from "medusa-test-utils" -import { IPricingModuleService } from "@medusajs/types" -import { Modules } from "@medusajs/utils" - -jest.setTimeout(30000) - -moduleIntegrationTestRunner({ - moduleName: Modules.PRICING, - testSuite: ({ MikroOrmWrapper, service }) => { - describe("PricingModuleService ruleType", () => { - beforeEach(async () => { - const testManager = MikroOrmWrapper.forkManager() - await createRuleTypes(testManager) - }) - - describe("listRuleTypes", () => { - it("should list rule types", async () => { - const ruleTypeResult = await service.listRuleTypes() - - expect(ruleTypeResult).toEqual([ - expect.objectContaining({ - id: "rule-type-1", - name: "rule 1", - }), - expect.objectContaining({ - id: "rule-type-2", - name: "rule 2", - }), - ]) - }) - - it("should list rule types by id", async () => { - const ruleTypeResult = await service.listRuleTypes({ - id: ["rule-type-1"], - }) - - expect(ruleTypeResult).toEqual([ - expect.objectContaining({ - id: "rule-type-1", - name: "rule 1", - }), - ]) - }) - }) - - describe("listAndCountRuleTypes", () => { - it("should return rule types and count", async () => { - const [ruleTypeResult, count] = await service.listAndCountRuleTypes() - - expect(count).toEqual(2) - expect(ruleTypeResult).toEqual([ - expect.objectContaining({ - id: "rule-type-1", - name: "rule 1", - }), - expect.objectContaining({ - id: "rule-type-2", - name: "rule 2", - }), - ]) - }) - - it("should return rule types and count when filtered", async () => { - const [ruleTypeResult, count] = await service.listAndCountRuleTypes({ - id: ["rule-type-1"], - }) - - expect(count).toEqual(1) - expect(ruleTypeResult).toEqual([ - expect.objectContaining({ - id: "rule-type-1", - name: "rule 1", - }), - ]) - }) - - it("should return rule types and count when using skip and take", async () => { - const [ruleTypeResult, count] = await service.listAndCountRuleTypes( - {}, - { skip: 1, take: 1 } - ) - - expect(count).toEqual(2) - expect(ruleTypeResult).toEqual([ - expect.objectContaining({ - id: "rule-type-2", - name: "rule 2", - }), - ]) - }) - - it("should return requested fields", async () => { - const [ruleTypeResult, count] = await service.listAndCountRuleTypes( - {}, - { - take: 1, - select: ["name"], - } - ) - - const serialized = JSON.parse(JSON.stringify(ruleTypeResult)) - - expect(count).toEqual(2) - expect(serialized).toEqual([ - { - id: "rule-type-1", - name: "rule 1", - }, - ]) - }) - }) - - describe("retrieveRuleType", () => { - it("should return ruleType for the given id", async () => { - const ruleType = await service.retrieveRuleType("rule-type-1") - - expect(ruleType).toEqual( - expect.objectContaining({ - id: "rule-type-1", - name: "rule 1", - }) - ) - }) - - it("should throw an error when ruleType with id does not exist", async () => { - let error - - try { - await service.retrieveRuleType("does-not-exist") - } catch (e) { - error = e - } - - expect(error.message).toEqual( - "RuleType 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.retrieveRuleType(undefined as unknown as string) - } catch (e) { - error = e - } - - expect(error.message).toEqual("ruleType - id must be defined") - }) - - it("should return ruleType based on config select param", async () => { - const ruleTypeResult = await service.retrieveRuleType("rule-type-1", { - select: ["name"], - }) - - const serialized = JSON.parse(JSON.stringify(ruleTypeResult)) - - expect(serialized).toEqual({ - name: "rule 1", - id: "rule-type-1", - }) - }) - }) - - describe("deleteRuleTypes", () => { - const id = "rule-type-1" - - it("should delete the ruleTypes given an id successfully", async () => { - await service.deleteRuleTypes([id]) - - const currencies = await service.listRuleTypes({ - id: [id], - }) - - expect(currencies).toHaveLength(0) - }) - }) - - describe("updateRuleTypes", () => { - const id = "rule-type-1" - - it("should update the name of the ruleType successfully", async () => { - await service.updateRuleTypes([ - { - id, - name: "rule 3", - }, - ]) - - const ruletype = await service.retrieveRuleType(id) - - expect(ruletype.name).toEqual("rule 3") - }) - - it("should throw an error when a id does not exist", async () => { - let error - - try { - await service.updateRuleTypes([ - { - id: "does-not-exist", - name: "rule 3", - }, - ]) - } catch (e) { - error = e - } - - expect(error.message).toEqual( - 'RuleType with id "does-not-exist" not found' - ) - }) - }) - - describe("createRuleTypes", () => { - it("should create a ruleType successfully", async () => { - await service.createRuleTypes([ - { - name: "Test Rule", - rule_attribute: "region_id", - }, - ]) - - const [ruleType] = await service.listRuleTypes({ - name: ["Test Rule"], - }) - - expect(ruleType).toEqual( - expect.objectContaining({ - name: "Test Rule", - rule_attribute: "region_id", - }) - ) - }) - }) - }) - }, -}) diff --git a/packages/modules/pricing/src/joiner-config.ts b/packages/modules/pricing/src/joiner-config.ts index 5ff9fb071c..296d01de18 100644 --- a/packages/modules/pricing/src/joiner-config.ts +++ b/packages/modules/pricing/src/joiner-config.ts @@ -4,15 +4,14 @@ import { MapToConfig, Modules, } from "@medusajs/utils" -import { Price, PriceList, PriceSet, RuleType } from "@models" +import { Price, PriceList, PriceSet } from "@models" export const joinerConfig = defineJoinerConfig(Modules.PRICING, { - entityQueryingConfig: [PriceSet, PriceList, Price, RuleType], + entityQueryingConfig: [PriceSet, PriceList, Price], linkableKeys: { price_set_id: PriceSet.name, price_list_id: PriceList.name, price_id: Price.name, - rule_type_id: RuleType.name, }, }) diff --git a/packages/modules/pricing/src/migrations/.snapshot-medusa-pricing.json b/packages/modules/pricing/src/migrations/.snapshot-medusa-pricing.json index a849810aa9..548347e3e7 100644 --- a/packages/modules/pricing/src/migrations/.snapshot-medusa-pricing.json +++ b/packages/modules/pricing/src/migrations/.snapshot-medusa-pricing.json @@ -150,6 +150,127 @@ "checks": [], "foreignKeys": {} }, + { + "columns": { + "id": { + "name": "id", + "type": "text", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": false, + "mappedType": "text" + }, + "attribute": { + "name": "attribute", + "type": "text", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": false, + "mappedType": "text" + }, + "value": { + "name": "value", + "type": "jsonb", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": true, + "mappedType": "json" + }, + "price_list_id": { + "name": "price_list_id", + "type": "text", + "unsigned": false, + "autoincrement": false, + "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", + "schema": "public", + "indexes": [ + { + "keyName": "IDX_price_list_rule_price_list_id", + "columnNames": [ + "price_list_id" + ], + "composite": false, + "primary": 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_deleted_at", + "columnNames": [ + "deleted_at" + ], + "composite": false, + "primary": false, + "unique": false, + "expression": "CREATE INDEX IF NOT EXISTS \"IDX_price_list_rule_deleted_at\" ON \"price_list_rule\" (deleted_at) WHERE deleted_at IS NOT NULL" + }, + { + "keyName": "price_list_rule_pkey", + "columnNames": [ + "id" + ], + "composite": false, + "primary": true, + "unique": true + } + ], + "checks": [], + "foreignKeys": { + "price_list_rule_price_list_id_foreign": { + "constraintName": "price_list_rule_price_list_id_foreign", + "columnNames": [ + "price_list_id" + ], + "localTableName": "public.price_list_rule", + "referencedColumnNames": [ + "id" + ], + "referencedTableName": "public.price_list", + "deleteRule": "cascade", + "updateRule": "cascade" + } + } + }, { "columns": { "id": { @@ -440,260 +561,8 @@ "nullable": false, "mappedType": "text" }, - "name": { - "name": "name", - "type": "text", - "unsigned": false, - "autoincrement": false, - "primary": false, - "nullable": false, - "mappedType": "text" - }, - "rule_attribute": { - "name": "rule_attribute", - "type": "text", - "unsigned": false, - "autoincrement": false, - "primary": false, - "nullable": false, - "mappedType": "text" - }, - "default_priority": { - "name": "default_priority", - "type": "integer", - "unsigned": false, - "autoincrement": false, - "primary": false, - "nullable": false, - "default": "0", - "mappedType": "integer" - }, - "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": "rule_type", - "schema": "public", - "indexes": [ - { - "keyName": "IDX_rule_type_rule_attribute", - "columnNames": [ - "rule_attribute" - ], - "composite": false, - "primary": 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" - ], - "composite": false, - "primary": true, - "unique": true - } - ], - "checks": [], - "foreignKeys": {} - }, - { - "columns": { - "id": { - "name": "id", - "type": "text", - "unsigned": false, - "autoincrement": false, - "primary": false, - "nullable": false, - "mappedType": "text" - }, - "price_set_id": { - "name": "price_set_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" - }, - "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": [ - { - "keyName": "IDX_price_set_rule_type_price_set_id", - "columnNames": [ - "price_set_id" - ], - "composite": false, - "primary": 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" - }, - { - "keyName": "IDX_price_set_rule_type_rule_type_id", - "columnNames": [ - "rule_type_id" - ], - "composite": false, - "primary": 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" - ], - "composite": false, - "primary": true, - "unique": true - } - ], - "checks": [], - "foreignKeys": { - "price_set_rule_type_price_set_id_foreign": { - "constraintName": "price_set_rule_type_price_set_id_foreign", - "columnNames": [ - "price_set_id" - ], - "localTableName": "public.price_set_rule_type", - "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" - ], - "localTableName": "public.price_set_rule_type", - "referencedColumnNames": [ - "id" - ], - "referencedTableName": "public.rule_type", - "deleteRule": "cascade", - "updateRule": "cascade" - } - } - }, - { - "columns": { - "id": { - "name": "id", - "type": "text", - "unsigned": false, - "autoincrement": false, - "primary": false, - "nullable": false, - "mappedType": "text" - }, - "price_set_id": { - "name": "price_set_id", - "type": "text", - "unsigned": false, - "autoincrement": false, - "primary": false, - "nullable": false, - "mappedType": "text" - }, - "rule_type_id": { - "name": "rule_type_id", + "attribute": { + "name": "attribute", "type": "text", "unsigned": false, "autoincrement": false, @@ -766,34 +635,14 @@ "schema": "public", "indexes": [ { - "keyName": "IDX_price_rule_price_set_id", - "columnNames": [ - "price_set_id" - ], - "composite": false, - "primary": 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" - }, - { - "keyName": "IDX_price_rule_rule_type_id", - "columnNames": [ - "rule_type_id" - ], - "composite": false, - "primary": 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" - }, - { - "keyName": "IDX_price_rule_price_id_rule_type_id_unique", + "keyName": "IDX_price_rule_price_id_attribute_unique", "columnNames": [ "price_id" ], "composite": false, "primary": false, "unique": false, - "expression": "CREATE UNIQUE INDEX IF NOT EXISTS \"IDX_price_rule_price_id_rule_type_id_unique\" ON \"price_rule\" (price_id, rule_type_id) WHERE deleted_at IS NULL" + "expression": "CREATE UNIQUE INDEX IF NOT EXISTS \"IDX_price_rule_price_id_attribute_unique\" ON \"price_rule\" (price_id, attribute) WHERE deleted_at IS NULL" }, { "keyName": "IDX_price_rule_deleted_at", @@ -817,31 +666,6 @@ ], "checks": [], "foreignKeys": { - "price_rule_price_set_id_foreign": { - "constraintName": "price_rule_price_set_id_foreign", - "columnNames": [ - "price_set_id" - ], - "localTableName": "public.price_rule", - "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" - ], - "localTableName": "public.price_rule", - "referencedColumnNames": [ - "id" - ], - "referencedTableName": "public.rule_type", - "updateRule": "cascade" - }, "price_rule_price_id_foreign": { "constraintName": "price_rule_price_id_foreign", "columnNames": [ @@ -856,252 +680,6 @@ "updateRule": "cascade" } } - }, - { - "columns": { - "id": { - "name": "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" - }, - "price_list_id": { - "name": "price_list_id", - "type": "text", - "unsigned": false, - "autoincrement": false, - "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", - "schema": "public", - "indexes": [ - { - "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", - "columnNames": [ - "price_list_id" - ], - "composite": false, - "primary": 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_deleted_at", - "columnNames": [ - "deleted_at" - ], - "composite": false, - "primary": false, - "unique": false, - "expression": "CREATE INDEX IF NOT EXISTS \"IDX_price_list_rule_deleted_at\" ON \"price_list_rule\" (deleted_at) WHERE deleted_at IS NOT NULL" - }, - { - "keyName": "price_list_rule_pkey", - "columnNames": [ - "id" - ], - "composite": false, - "primary": true, - "unique": true - } - ], - "checks": [], - "foreignKeys": { - "price_list_rule_rule_type_id_foreign": { - "constraintName": "price_list_rule_rule_type_id_foreign", - "columnNames": [ - "rule_type_id" - ], - "localTableName": "public.price_list_rule", - "referencedColumnNames": [ - "id" - ], - "referencedTableName": "public.rule_type", - "updateRule": "cascade" - }, - "price_list_rule_price_list_id_foreign": { - "constraintName": "price_list_rule_price_list_id_foreign", - "columnNames": [ - "price_list_id" - ], - "localTableName": "public.price_list_rule", - "referencedColumnNames": [ - "id" - ], - "referencedTableName": "public.price_list", - "deleteRule": "cascade", - "updateRule": "cascade" - } - } - }, - { - "columns": { - "id": { - "name": "id", - "type": "text", - "unsigned": false, - "autoincrement": false, - "primary": false, - "nullable": false, - "mappedType": "text" - }, - "price_list_rule_id": { - "name": "price_list_rule_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" - }, - "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": [ - { - "keyName": "IDX_price_list_rule_value_price_list_rule_id", - "columnNames": [ - "price_list_rule_id" - ], - "composite": false, - "primary": 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" - ], - "composite": false, - "primary": true, - "unique": true - } - ], - "checks": [], - "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" - ], - "localTableName": "public.price_list_rule_value", - "referencedColumnNames": [ - "id" - ], - "referencedTableName": "public.price_list_rule", - "deleteRule": "cascade", - "updateRule": "cascade" - } - } } ] } diff --git a/packages/modules/pricing/src/migrations/Migration20240626133555.ts b/packages/modules/pricing/src/migrations/Migration20240626133555.ts new file mode 100644 index 0000000000..7e688f1fb7 --- /dev/null +++ b/packages/modules/pricing/src/migrations/Migration20240626133555.ts @@ -0,0 +1,72 @@ +import { Migration } from "@mikro-orm/migrations" + +export class Migration20240626133555 extends Migration { + async up(): Promise { + this.addSql( + 'alter table if exists "price_list_rule" add column if not exists "value" jsonb;' + ) + + // TODO: Added on 28.06.2024, Drop defaults after a while. + this.addSql( + 'alter table if exists "price_list_rule" add column if not exists "attribute" text not null DEFAULT \'\';' + ) + + this.addSql( + 'alter table if exists "price_rule" add column if not exists "attribute" text not null DEFAULT \'\';' + ) + + /* DATA MIGRATION */ + this.addSql( + "update price_rule set attribute = (SELECT rule_attribute FROM rule_type WHERE rule_type.id = price_rule.rule_type_id);" + ) + this.addSql( + "update price_list_rule set value = (SELECT array_to_json(ARRAY(SELECT value FROM price_list_rule_value WHERE price_list_rule_value.price_list_rule_id = price_list_rule.id))::jsonb);" + ) + this.addSql( + "update price_list_rule set attribute = (SELECT rule_attribute FROM rule_type WHERE rule_type.id = price_list_rule.rule_type_id);" + ) + /* DATA MIGRATION END */ + + 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_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_rule_type_id_foreign";' + ) + + this.addSql( + 'alter table if exists "price_rule" drop constraint if exists "price_rule_price_set_id_foreign";' + ) + + this.addSql( + 'drop index if exists "IDX_price_list_rule_rule_type_id_unique";' + ) + this.addSql('drop index if exists "IDX_price_rule_price_set_id";') + this.addSql('drop index if exists "IDX_price_rule_rule_type_id";') + this.addSql('drop index if exists "IDX_price_rule_price_id_unique";') + this.addSql( + 'CREATE UNIQUE INDEX IF NOT EXISTS "IDX_price_rule_price_id_attribute_unique" ON "price_rule" (price_id, attribute) WHERE deleted_at IS NULL;' + ) + + this.addSql( + 'alter table if exists "price_rule" drop column if exists "price_set_id";' + ) + this.addSql( + 'alter table if exists "price_rule" drop column if exists "rule_type_id";' + ) + this.addSql( + 'alter table if exists "price_list_rule" drop column if exists "rule_type_id";' + ) + + this.addSql('drop table if exists "rule_type" cascade;') + + this.addSql('drop table if exists "price_set_rule_type" cascade;') + + this.addSql('drop table if exists "price_list_rule_value" cascade;') + } +} diff --git a/packages/modules/pricing/src/models/index.ts b/packages/modules/pricing/src/models/index.ts index c440086e0f..729f870b6b 100644 --- a/packages/modules/pricing/src/models/index.ts +++ b/packages/modules/pricing/src/models/index.ts @@ -1,8 +1,5 @@ export { default as Price } from "./price" export { default as PriceList } from "./price-list" export { default as PriceListRule } from "./price-list-rule" -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 PriceSetRuleType } from "./price-set-rule-type" -export { default as RuleType } from "./rule-type" diff --git a/packages/modules/pricing/src/models/price-list-rule-value.ts b/packages/modules/pricing/src/models/price-list-rule-value.ts deleted file mode 100644 index e46a7c53e3..0000000000 --- a/packages/modules/pricing/src/models/price-list-rule-value.ts +++ /dev/null @@ -1,88 +0,0 @@ -import { DAL } from "@medusajs/types" -import { - createPsqlIndexStatementHelper, - DALUtils, - generateEntityId, -} from "@medusajs/utils" -import { - BeforeCreate, - Entity, - Filter, - ManyToOne, - OnInit, - OptionalProps, - PrimaryKey, - Property, - Rel, -} from "@mikro-orm/core" -import PriceListRule from "./price-list-rule" - -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, { - columnType: "text", - mapToPk: true, - fieldName: "price_list_rule_id", - onDelete: "cascade", - }) - price_list_rule_id: string - - @ManyToOne(() => PriceListRule, { persist: false }) - price_list_rule: Rel - - @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") - this.price_list_rule_id ??= this.price_list_rule?.id - } - - @OnInit() - onInit() { - this.id = generateEntityId(this.id, "plrv") - this.price_list_rule_id ??= this.price_list_rule?.id - } -} diff --git a/packages/modules/pricing/src/models/price-list-rule.ts b/packages/modules/pricing/src/models/price-list-rule.ts index dd89f5ce57..5aa144922c 100644 --- a/packages/modules/pricing/src/models/price-list-rule.ts +++ b/packages/modules/pricing/src/models/price-list-rule.ts @@ -6,12 +6,9 @@ import { } from "@medusajs/utils" import { BeforeCreate, - Cascade, - Collection, Entity, Filter, ManyToOne, - OneToMany, OnInit, OptionalProps, PrimaryKey, @@ -19,8 +16,6 @@ import { Rel, } from "@mikro-orm/core" import PriceList from "./price-list" -import PriceListRuleValue from "./price-list-rule-value" -import RuleType from "./rule-type" type OptionalFields = DAL.SoftDeletableEntityDateColumns @@ -31,13 +26,6 @@ const PriceListRuleDeletedAtIndex = createPsqlIndexStatementHelper({ 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", @@ -52,21 +40,11 @@ export default class PriceListRule { @PrimaryKey({ columnType: "text" }) id!: string - @PriceListRuleRuleTypeIdIndex.MikroORMIndex() - @ManyToOne(() => RuleType, { - columnType: "text", - mapToPk: true, - fieldName: "rule_type_id", - }) - rule_type_id: string + @Property({ columnType: "text" }) + attribute: string - @ManyToOne(() => RuleType, { persist: false }) - rule_type: Rel - - @OneToMany(() => PriceListRuleValue, (plrv) => plrv.price_list_rule, { - cascade: [Cascade.PERSIST, "soft-remove" as Cascade], - }) - price_list_rule_values = new Collection>(this) + @Property({ columnType: "jsonb", nullable: true }) + value: string | string[] | null = null @PriceListRulePriceListIdIndex.MikroORMIndex() @ManyToOne(() => PriceList, { @@ -103,13 +81,11 @@ export default class PriceListRule { beforeCreate() { this.id = generateEntityId(this.id, "plrule") this.price_list_id ??= this.price_list?.id! - this.rule_type_id ??= this.rule_type?.id! } @OnInit() onInit() { this.id = generateEntityId(this.id, "plrule") this.price_list_id ??= this.price_list?.id! - this.rule_type_id ??= this.rule_type?.id! } } diff --git a/packages/modules/pricing/src/models/price-list.ts b/packages/modules/pricing/src/models/price-list.ts index d9abc01ae9..b2f5606605 100644 --- a/packages/modules/pricing/src/models/price-list.ts +++ b/packages/modules/pricing/src/models/price-list.ts @@ -14,7 +14,6 @@ import { Entity, Enum, Filter, - ManyToMany, OneToMany, OnInit, OptionalProps, @@ -24,7 +23,6 @@ import { } from "@mikro-orm/core" import Price from "./price" import PriceListRule from "./price-list-rule" -import RuleType from "./rule-type" type OptionalFields = | "starts_at" @@ -84,12 +82,6 @@ export default class PriceList { }) price_list_rules = new Collection>(this) - @ManyToMany({ - entity: () => RuleType, - pivotEntity: () => PriceListRule, - }) - rule_types = new Collection>(this) - @Property({ columnType: "integer", default: 0 }) rules_count: number = 0 diff --git a/packages/modules/pricing/src/models/price-rule.ts b/packages/modules/pricing/src/models/price-rule.ts index 951b60a4c0..ffd63a1545 100644 --- a/packages/modules/pricing/src/models/price-rule.ts +++ b/packages/modules/pricing/src/models/price-rule.ts @@ -16,8 +16,6 @@ import { Rel, } from "@mikro-orm/core" import Price from "./price" -import PriceSet from "./price-set" -import RuleType from "./rule-type" type OptionalFields = DAL.SoftDeletableEntityDateColumns @@ -28,21 +26,9 @@ const PriceRuleDeletedAtIndex = createPsqlIndexStatementHelper({ 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 PriceRulePriceIdIndex = createPsqlIndexStatementHelper({ tableName: tableName, - columns: ["price_id", "rule_type_id"], + columns: ["price_id", "attribute"], where: "deleted_at IS NULL", unique: true, }) @@ -55,28 +41,8 @@ export default class PriceRule { @PrimaryKey({ columnType: "text" }) id!: string - @PriceRulePriceSetIdIndex.MikroORMIndex() - @ManyToOne(() => PriceSet, { - columnType: "text", - mapToPk: true, - fieldName: "price_set_id", - onDelete: "cascade", - }) - price_set_id: string - - @ManyToOne(() => PriceSet, { persist: false }) - price_set: Rel - - @PriceRuleRuleTypeIdIndex.MikroORMIndex() - @ManyToOne(() => RuleType, { - columnType: "text", - mapToPk: true, - fieldName: "rule_type_id", - }) - rule_type_id: string - - @ManyToOne(() => RuleType, { persist: false }) - rule_type: Rel + @Property({ columnType: "text" }) + attribute: string @Property({ columnType: "text" }) value: string @@ -118,16 +84,12 @@ export default class PriceRule { @BeforeCreate() beforeCreate() { this.id = generateEntityId(this.id, "prule") - this.rule_type_id ??= this.rule_type?.id! - this.price_set_id ??= this.price_set?.id! this.price_id ??= this.price?.id! } @OnInit() onInit() { this.id = generateEntityId(this.id, "prule") - this.rule_type_id ??= this.rule_type?.id! - this.price_set_id ??= this.price_set?.id! this.price_id ??= this.price?.id! } } diff --git a/packages/modules/pricing/src/models/price-set-rule-type.ts b/packages/modules/pricing/src/models/price-set-rule-type.ts deleted file mode 100644 index f731d505f1..0000000000 --- a/packages/modules/pricing/src/models/price-set-rule-type.ts +++ /dev/null @@ -1,89 +0,0 @@ -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" - -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 - - @PriceSetRuleTypePriceSetIdIndex.MikroORMIndex() - @ManyToOne(() => PriceSet, { - columnType: "text", - mapToPk: true, - fieldName: "price_set_id", - onDelete: "cascade", - }) - price_set_id: string - - @PriceSetRuleTypeRuleTypeIdIndex.MikroORMIndex() - @ManyToOne(() => RuleType, { - columnType: "text", - mapToPk: true, - fieldName: "rule_type_id", - onDelete: "cascade", - }) - rule_type_id: 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 - - @PriceSetRuleTypeDeletedAtIndex.MikroORMIndex() - @Property({ columnType: "timestamptz", nullable: true }) - deleted_at: Date | null = null - - @BeforeCreate() - onCreate() { - this.id = generateEntityId(this.id, "psrt") - } - - @OnInit() - onInit() { - this.id = generateEntityId(this.id, "psrt") - } -} diff --git a/packages/modules/pricing/src/models/price-set.ts b/packages/modules/pricing/src/models/price-set.ts index 42e368eb52..8c1e4a5cdd 100644 --- a/packages/modules/pricing/src/models/price-set.ts +++ b/packages/modules/pricing/src/models/price-set.ts @@ -9,7 +9,6 @@ import { Collection, Entity, Filter, - ManyToMany, OneToMany, OnInit, PrimaryKey, @@ -17,9 +16,6 @@ import { Rel, } from "@mikro-orm/core" import Price from "./price" -import PriceRule from "./price-rule" -import PriceSetRuleType from "./price-set-rule-type" -import RuleType from "./rule-type" const tableName = "price_set" const PriceSetDeletedAtIndex = createPsqlIndexStatementHelper({ @@ -41,18 +37,6 @@ export default class PriceSet { }) prices = new Collection>(this) - @OneToMany(() => PriceRule, (pr) => pr.price_set, { - cascade: [Cascade.PERSIST, "soft-remove" as Cascade], - }) - price_rules = new Collection>(this) - - @ManyToMany({ - entity: () => RuleType, - pivotEntity: () => PriceSetRuleType, - cascade: ["soft-remove" as Cascade], - }) - rule_types = new Collection>(this) - @Property({ onCreate: () => new Date(), columnType: "timestamptz", diff --git a/packages/modules/pricing/src/models/rule-type.ts b/packages/modules/pricing/src/models/rule-type.ts deleted file mode 100644 index f4cdd43f9b..0000000000 --- a/packages/modules/pricing/src/models/rule-type.ts +++ /dev/null @@ -1,86 +0,0 @@ -import { - DALUtils, - createPsqlIndexStatementHelper, - generateEntityId, -} from "@medusajs/utils" -import { - BeforeCreate, - Collection, - Entity, - Filter, - ManyToMany, - OnInit, - OptionalProps, - PrimaryKey, - Property, - Rel, -} from "@mikro-orm/core" -import PriceSet from "./price-set" - -type OptionalFields = "default_priority" - -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 - - @PrimaryKey({ columnType: "text" }) - id!: string - - @Property({ columnType: "text" }) - name: string - - @RuleTypeRuleAttributeIndex.MikroORMIndex() - @Property({ columnType: "text" }) - rule_attribute: string - - @Property({ columnType: "integer", default: 0 }) - default_priority: number - - @ManyToMany(() => PriceSet, (priceSet) => priceSet.rule_types) - price_sets = 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 - - @RuleTypeDeletedAtIndex.MikroORMIndex() - @Property({ columnType: "timestamptz", nullable: true }) - deleted_at: Date | null = null - - @BeforeCreate() - onCreate() { - this.id = generateEntityId(this.id, "rul-typ") - } - - @OnInit() - onInit() { - this.id = generateEntityId(this.id, "rul-typ") - } -} - -export default RuleType diff --git a/packages/modules/pricing/src/repositories/pricing.ts b/packages/modules/pricing/src/repositories/pricing.ts index 6b9f6c487a..f4b606125f 100644 --- a/packages/modules/pricing/src/repositories/pricing.ts +++ b/packages/modules/pricing/src/repositories/pricing.ts @@ -61,65 +61,57 @@ export class PricingRepository price: "price", }) .select({ - id: "price1.id", - amount: "price1.amount", - min_quantity: "price1.min_quantity", - max_quantity: "price1.max_quantity", - currency_code: "price1.currency_code", - price_set_id: "price1.price_set_id", - rules_count: "price1.rules_count", - price_list_id: "price1.price_list_id", + id: "price.id", + amount: "price.amount", + min_quantity: "price.min_quantity", + max_quantity: "price.max_quantity", + currency_code: "price.currency_code", + price_set_id: "price.price_set_id", + rules_count: "price.rules_count", + price_list_id: "price.price_list_id", pl_rules_count: "pl.rules_count", pl_type: "pl.type", has_price_list: knex.raw( - "case when price1.price_list_id IS NULL then False else True end" + "case when price.price_list_id IS NULL then False else True end" ), }) - .leftJoin("price as price1", "price1.id", "price1.id") - .leftJoin("price_rule as pr", "pr.price_id", "price1.id") + .leftJoin("price_rule as pr", "pr.price_id", "price.id") .leftJoin("price_list as pl", function () { - this.on("pl.id", "price1.price_list_id").andOn( + this.on("pl.id", "price.price_list_id").andOn( "pl.status", knex.raw("?", [PriceListStatus.ACTIVE]) ) }) .leftJoin("price_list_rule as plr", "plr.price_list_id", "pl.id") - .leftJoin( - "price_list_rule_value as plrv", - "plrv.price_list_rule_id", - "plr.id" - ) - .leftJoin("rule_type as plrt", "plrt.id", "plr.rule_type_id") - .leftJoin("rule_type as rt", "rt.id", "pr.rule_type_id") .orderBy([ { column: "rules_count", order: "desc" }, { column: "pl.rules_count", order: "desc" }, ]) - .groupBy("price1.id", "pl.id") + .groupBy("price.id", "pl.id") .having( knex.raw( - "count(DISTINCT rt.rule_attribute) = price1.rules_count AND price1.price_list_id IS NULL" + "count(DISTINCT pr.attribute) = price.rules_count AND price.price_list_id IS NULL" ) ) .orHaving( knex.raw( - "count(DISTINCT plrt.rule_attribute) = pl.rules_count AND price1.price_list_id IS NOT NULL" + "count(DISTINCT plr.attribute) = pl.rules_count AND price.price_list_id IS NOT NULL" ) ) priceSubQueryKnex.orWhere((q) => { for (const [key, value] of Object.entries(context)) { q.orWhere({ - "rt.rule_attribute": key, + "pr.attribute": key, "pr.value": value, }) } - q.orWhere("price1.rules_count", "=", 0) - q.whereNull("price1.price_list_id") + q.orWhere("price.rules_count", "=", 0) + q.whereNull("price.price_list_id") }) priceSubQueryKnex.orWhere((q) => { - q.whereNotNull("price1.price_list_id") + q.whereNotNull("price.price_list_id") .andWhere(function () { this.whereNull("pl.starts_at").orWhere("pl.starts_at", "<=", date) }) @@ -130,9 +122,13 @@ export class PricingRepository this.andWhere(function () { for (const [key, value] of Object.entries(context)) { this.orWhere({ - "plrt.rule_attribute": key, + "plr.attribute": key, }) - this.whereIn("plrv.value", [value]) + this.where( + "plr.value", + "@>", + JSON.stringify(Array.isArray(value) ? value : [value]) + ) } this.orWhere("pl.rules_count", "=", 0) @@ -142,13 +138,13 @@ export class PricingRepository this.andWhere(function () { for (const [key, value] of Object.entries(context)) { this.orWhere({ - "rt.rule_attribute": key, + "pr.attribute": key, "pr.value": value, }) } - this.andWhere("price1.rules_count", ">", 0) + this.andWhere("price.rules_count", ">", 0) }) - this.orWhere("price1.rules_count", "=", 0) + this.orWhere("price.rules_count", "=", 0) }) }) }) @@ -163,7 +159,6 @@ export class PricingRepository min_quantity: "price.min_quantity", max_quantity: "price.max_quantity", currency_code: "price.currency_code", - default_priority: "rt.default_priority", rules_count: "price.rules_count", pl_rules_count: "price.pl_rules_count", price_list_type: "price.pl_type", @@ -171,7 +166,6 @@ export class PricingRepository }) .join(priceSubQueryKnex.as("price"), "price.price_set_id", "ps.id") .leftJoin("price_rule as pr", "pr.price_id", "price.id") - .leftJoin("rule_type as rt", "rt.id", "pr.rule_type_id") .whereIn("ps.id", pricingFilters.id) .andWhere("price.currency_code", "=", currencyCode) @@ -179,7 +173,6 @@ export class PricingRepository { column: "price.has_price_list", order: "asc" }, { column: "amount", order: "asc" }, { column: "rules_count", order: "desc" }, - { column: "default_priority", order: "desc" }, ]) if (quantity) { diff --git a/packages/modules/pricing/src/services/index.ts b/packages/modules/pricing/src/services/index.ts index 6538f16d7d..e1546127c0 100644 --- a/packages/modules/pricing/src/services/index.ts +++ b/packages/modules/pricing/src/services/index.ts @@ -1,3 +1 @@ -export { default as PriceListService } from "./price-list" export { default as PricingModuleService } from "./pricing-module" -export { default as RuleTypeService } from "./rule-type" diff --git a/packages/modules/pricing/src/services/price-list.ts b/packages/modules/pricing/src/services/price-list.ts deleted file mode 100644 index d8680f23c3..0000000000 --- a/packages/modules/pricing/src/services/price-list.ts +++ /dev/null @@ -1,67 +0,0 @@ -import { Context, DAL } from "@medusajs/types" -import { GetIsoStringFromDate, ModulesSdkUtils } from "@medusajs/utils" -import { PriceList } from "@models" -import { ServiceTypes } from "@types" - -type InjectedDependencies = { - priceListRepository: DAL.RepositoryService -} - -export default class PriceListService extends ModulesSdkUtils.MedusaInternalService( - PriceList -) { - constructor(container: InjectedDependencies) { - // @ts-ignore - super(...arguments) - } - - create( - data: ServiceTypes.CreatePriceListDTO[], - sharedContext?: Context - ): Promise - create( - data: ServiceTypes.CreatePriceListDTO, - sharedContext?: Context - ): Promise - - async create( - data: ServiceTypes.CreatePriceListDTO | ServiceTypes.CreatePriceListDTO[], - sharedContext?: Context - ): Promise { - const data_ = Array.isArray(data) ? data : [data] - const priceLists = this.normalizePriceListDate(data_) - return await super.create(priceLists, sharedContext) - } - - // @ts-ignore - update(data: any[], sharedContext?: Context): Promise - // @ts-ignore - update(data: any, sharedContext?: Context): Promise - - // TODO: Add support for selector? and then rm ts ignore - // @ts-ignore - async update( - data: ServiceTypes.UpdatePriceListDTO | ServiceTypes.UpdatePriceListDTO[], - sharedContext?: Context - ): Promise { - const data_ = Array.isArray(data) ? data : [data] - const priceLists = this.normalizePriceListDate(data_) - return await super.update(priceLists, sharedContext) - } - - protected normalizePriceListDate( - data: (ServiceTypes.UpdatePriceListDTO | ServiceTypes.CreatePriceListDTO)[] - ) { - return data.map((priceListData: any) => { - if (!!priceListData.starts_at) { - priceListData.starts_at = GetIsoStringFromDate(priceListData.starts_at) - } - - if (!!priceListData.ends_at) { - priceListData.ends_at = GetIsoStringFromDate(priceListData.ends_at) - } - - return priceListData - }) - } -} diff --git a/packages/modules/pricing/src/services/pricing-module.ts b/packages/modules/pricing/src/services/pricing-module.ts index 6bd22451b8..e14eb2f9ab 100644 --- a/packages/modules/pricing/src/services/pricing-module.ts +++ b/packages/modules/pricing/src/services/pricing-module.ts @@ -1,7 +1,6 @@ import { AddPricesDTO, Context, - CreatePriceListRuleDTO, CreatePriceRuleDTO, CreatePricesDTO, CreatePriceSetDTO, @@ -10,19 +9,18 @@ import { InternalModuleDeclaration, ModuleJoinerConfig, ModulesSdkTypes, + PriceDTO, PriceSetDTO, PricingContext, PricingFilters, PricingRepositoryService, PricingTypes, - RuleTypeDTO, UpsertPriceSetDTO, } from "@medusajs/types" import { arrayDifference, - deduplicate, EmitEvents, - generateEntityId, + GetIsoStringFromDate, groupBy, InjectManager, InjectTransactionManager, @@ -31,49 +29,35 @@ import { MedusaContext, MedusaError, ModulesSdkUtils, + PriceListStatus, PriceListType, promiseAll, removeNullish, } from "@medusajs/utils" -import { - Price, - PriceList, - PriceListRule, - PriceListRuleValue, - PriceRule, - PriceSet, - RuleType, -} from "@models" +import { Price, PriceList, PriceListRule, PriceRule, PriceSet } from "@models" -import { PriceListService, RuleTypeService } from "@services" import { ServiceTypes } from "@types" import { eventBuilders, validatePriceListDates } from "@utils" import { entityNameToLinkableKeysMap, joinerConfig } from "../joiner-config" -import { PriceListIdPrefix } from "../models/price-list" -import { PriceSetIdPrefix } from "../models/price-set" +import { CreatePriceListDTO } from "src/types/services" type InjectedDependencies = { baseRepository: DAL.RepositoryService pricingRepository: PricingRepositoryService priceSetService: ModulesSdkTypes.IMedusaInternalService - ruleTypeService: RuleTypeService priceRuleService: ModulesSdkTypes.IMedusaInternalService - priceSetRuleTypeService: ModulesSdkTypes.IMedusaInternalService priceService: ModulesSdkTypes.IMedusaInternalService - priceListService: PriceListService + priceListService: ModulesSdkTypes.IMedusaInternalService priceListRuleService: ModulesSdkTypes.IMedusaInternalService - priceListRuleValueService: ModulesSdkTypes.IMedusaInternalService } const generateMethodForModels = { PriceSet, PriceList, PriceListRule, - PriceListRuleValue, PriceRule, Price, - RuleType, } export default class PricingModuleService @@ -85,11 +69,6 @@ export default class PricingModuleService create: PricingTypes.CreatePriceRuleDTO update: PricingTypes.UpdatePriceRuleDTO } - RuleType: { - dto: PricingTypes.RuleTypeDTO - create: PricingTypes.CreateRuleTypeDTO - update: PricingTypes.UpdateRuleTypeDTO - } PriceList: { dto: PricingTypes.PriceListDTO } PriceListRule: { dto: PricingTypes.PriceListRuleDTO } }>(generateMethodForModels, entityNameToLinkableKeysMap) @@ -97,25 +76,21 @@ export default class PricingModuleService { protected baseRepository_: DAL.RepositoryService protected readonly pricingRepository_: PricingRepositoryService - protected readonly ruleTypeService_: RuleTypeService protected readonly priceSetService_: ModulesSdkTypes.IMedusaInternalService protected readonly priceRuleService_: ModulesSdkTypes.IMedusaInternalService protected readonly priceService_: ModulesSdkTypes.IMedusaInternalService - protected readonly priceListService_: PriceListService + protected readonly priceListService_: ModulesSdkTypes.IMedusaInternalService protected readonly priceListRuleService_: ModulesSdkTypes.IMedusaInternalService - protected readonly priceListRuleValueService_: ModulesSdkTypes.IMedusaInternalService constructor( { baseRepository, pricingRepository, - ruleTypeService, priceSetService, priceRuleService, priceService, priceListService, priceListRuleService, - priceListRuleValueService, }: InjectedDependencies, protected readonly moduleDeclaration: InternalModuleDeclaration ) { @@ -124,14 +99,11 @@ export default class PricingModuleService this.baseRepository_ = baseRepository this.pricingRepository_ = pricingRepository - this.ruleTypeService_ = ruleTypeService this.priceSetService_ = priceSetService - this.ruleTypeService_ = ruleTypeService this.priceRuleService_ = priceRuleService this.priceService_ = priceService this.priceListService_ = priceListService this.priceListRuleService_ = priceListRuleService - this.priceListRuleValueService_ = priceListRuleValueService } __joinerConfig(): ModuleJoinerConfig { @@ -168,25 +140,25 @@ export default class PricingModuleService const pricingContext = this.setupCalculatedPriceConfig_(filters, config) const priceSets = await super.listPriceSets(filters, config, sharedContext) + if (!pricingContext || !priceSets.length) { + return priceSets + } - if (pricingContext && priceSets.length) { - const priceSetIds: string[] = [] - const priceSetMap = new Map() - for (const priceSet of priceSets) { - priceSetIds.push(priceSet.id) - priceSetMap.set(priceSet.id, priceSet) - } + const priceSetIds: string[] = [] + const priceSetMap = new Map() + for (const priceSet of priceSets) { + priceSetIds.push(priceSet.id) + priceSetMap.set(priceSet.id, priceSet) + } - const calculatedPrices = await this.calculatePrices( - { id: priceSets.map((p) => p.id) }, - { context: pricingContext }, - sharedContext - ) - - for (const calculatedPrice of calculatedPrices) { - const priceSet = priceSetMap.get(calculatedPrice.id) - priceSet.calculated_price = calculatedPrice - } + const calculatedPrices = await this.calculatePrices( + { id: priceSetIds }, + { context: pricingContext }, + sharedContext + ) + for (const calculatedPrice of calculatedPrices) { + const priceSet = priceSetMap.get(calculatedPrice.id) + priceSet.calculated_price = calculatedPrice } return priceSets @@ -206,25 +178,25 @@ export default class PricingModuleService config, sharedContext ) + if (!pricingContext || !priceSets.length) { + return [priceSets, count] + } - if (pricingContext && priceSets.length) { - const priceSetIds: string[] = [] - const priceSetMap = new Map() - for (const priceSet of priceSets) { - priceSetIds.push(priceSet.id) - priceSetMap.set(priceSet.id, priceSet) - } + const priceSetIds: string[] = [] + const priceSetMap = new Map() + for (const priceSet of priceSets) { + priceSetIds.push(priceSet.id) + priceSetMap.set(priceSet.id, priceSet) + } - const calculatedPrices = await this.calculatePrices( - { id: priceSets.map((p) => p.id) }, - { context: pricingContext }, - sharedContext - ) - - for (const calculatedPrice of calculatedPrices) { - const priceSet = priceSetMap.get(calculatedPrice.id) - priceSet.calculated_price = calculatedPrice - } + const calculatedPrices = await this.calculatePrices( + { id: priceSetIds }, + { context: pricingContext }, + sharedContext + ) + for (const calculatedPrice of calculatedPrices) { + const priceSet = priceSetMap.get(calculatedPrice.id) + priceSet.calculated_price = calculatedPrice } return [priceSets, count] @@ -326,12 +298,7 @@ export default class PricingModuleService const dbPriceSets = await this.listPriceSets( { id: priceSets.map((p) => p.id) }, { - relations: [ - "rule_types", - "prices", - "price_rules", - "prices.price_rules", - ], + relations: ["prices", "prices.price_rules"], }, sharedContext ) @@ -430,37 +397,18 @@ export default class PricingModuleService return isString(idOrSelector) ? priceSets[0] : priceSets } - private async normalizeUpdateData( - data: ServiceTypes.UpdatePriceSetInput[], - sharedContext - ) { - const ruleAttributes = data - .map((d) => d.prices?.map((p) => Object.keys(p.rules ?? [])) ?? []) - .flat(Infinity) - .filter(Boolean) - - const ruleTypes = await this.ruleTypeService_.list( - { rule_attribute: ruleAttributes }, - { take: null }, - sharedContext - ) - - const ruleTypeMap = ruleTypes.reduce((acc, curr) => { - acc.set(curr.rule_attribute, curr) - return acc - }, new Map()) - + private async normalizeUpdateData(data: ServiceTypes.UpdatePriceSetInput[]) { return data.map((priceSet) => { const prices = priceSet.prices?.map((price) => { const rules = Object.entries(price.rules ?? {}).map( ([attribute, value]) => { return { - price_set_id: priceSet.id, - rule_type_id: ruleTypeMap.get(attribute)!.id, + attribute, value, } } ) + const hasRulesInput = isPresent(price.rules) delete price.rules return { @@ -483,10 +431,9 @@ export default class PricingModuleService data: ServiceTypes.UpdatePriceSetInput[], @MedusaContext() sharedContext: Context = {} ): Promise { - // TODO: We are not handling rule types, rules, etc. here, add support after data models are finalized // TODO: Since money IDs are rarely passed, this will delete all previous data and insert new entries. // We can make the `insert` inside upsertWithReplace do an `upsert` instead to avoid this - const normalizedData = await this.normalizeUpdateData(data, sharedContext) + const normalizedData = await this.normalizeUpdateData(data) const prices = normalizedData.flatMap((priceSet) => priceSet.prices || []) const { entities: upsertedPrices } = @@ -640,79 +587,39 @@ export default class PricingModuleService ) { const input = Array.isArray(data) ? data : [data] - const ruleAttributes = deduplicate( - data - .map( - (d) => - d.prices?.map((ma) => Object.keys(ma?.rules ?? {})).flat() ?? [] - ) - .flat() - ) - - const ruleTypes = await this.ruleTypeService_.list( - { rule_attribute: ruleAttributes }, - { take: null }, - sharedContext - ) - - const ruleTypeMap = ruleTypes.reduce((acc, curr) => { - acc.set(curr.rule_attribute, curr) - return acc - }, new Map()) - - const invalidRuleAttributes = ruleAttributes.filter( - (r) => !ruleTypeMap.has(r) - ) - - if (invalidRuleAttributes.length > 0) { - throw new MedusaError( - MedusaError.Types.NOT_FOUND, - `Rule types don't exist for prices with rule attribute: ${invalidRuleAttributes.join( - ", " - )}` - ) - } - const toCreate = input.map((inputData) => { - const id = generateEntityId( - (inputData as unknown as PriceSet).id, - PriceSetIdPrefix - ) + const entry = { + ...inputData, + } - const { prices, ...rest } = inputData + if (!inputData.prices) { + return entry + } - let pricesData: CreatePricesDTO[] = [] + const pricesData: CreatePricesDTO[] = inputData.prices.map((price) => { + let { rules: priceRules = {}, ...rest } = price + const cleanRules = priceRules ? removeNullish(priceRules) : {} + const rules = Object.entries(cleanRules) + const numberOfRules = rules.length - if (inputData.prices) { - pricesData = inputData.prices.map((price) => { - let { rules: priceRules = {}, ...rest } = price - const cleanRules = priceRules ? removeNullish(priceRules) : {} - const numberOfRules = Object.keys(cleanRules).length - const rulesDataMap = new Map() - - Object.entries(priceRules).map(([attribute, value]) => { - const rule = { - price_set_id: id, - rule_type_id: ruleTypeMap.get(attribute).id, - value, - } - rulesDataMap.set(JSON.stringify(rule), rule) - }) - - return { - ...rest, - title: "", // TODO: accept title - rules_count: numberOfRules, - price_rules: Array.from(rulesDataMap.values()), + const rulesDataMap = new Map() + rules.map(([attribute, value]) => { + const rule = { + attribute, + value, } + rulesDataMap.set(JSON.stringify(rule), rule) }) - } - return { - ...rest, - id, - prices: pricesData, - } + return { + ...rest, + rules_count: numberOfRules, + price_rules: Array.from(rulesDataMap.values()), + } + }) + + entry.prices = pricesData + return entry }) // Bulk create price sets @@ -774,22 +681,12 @@ export default class PricingModuleService ) { const priceSets = await this.listPriceSets( { id: input.map((d) => d.priceSetId) }, - { relations: ["rule_types"] }, + {}, sharedContext ) const priceSetMap = new Map(priceSets.map((p) => [p.id, p])) - - const ruleTypeMap: Map> = new Map( - priceSets.map((priceSet) => [ - priceSet.id, - new Map( - priceSet.rule_types?.map((rt) => [rt.rule_attribute, rt]) ?? [] - ), - ]) - ) - - input.forEach(({ priceSetId, prices }) => { + input.forEach(({ priceSetId }) => { const priceSet = priceSetMap.get(priceSetId) if (!priceSet) { @@ -798,21 +695,6 @@ export default class PricingModuleService `Price set with id: ${priceSetId} not found` ) } - - const ruleAttributes = prices - .map((d) => (d.rules ? Object.keys(d.rules) : [])) - .flat() - - const invalidRuleAttributes = ruleAttributes.filter( - (r) => !ruleTypeMap.get(priceSetId)!.has(r) - ) - - if (invalidRuleAttributes.length > 0) { - throw new MedusaError( - MedusaError.Types.NOT_FOUND, - `Rule types don't exist for: ${invalidRuleAttributes.join(", ")}` - ) - } }) const pricesToCreate: PricingTypes.CreatePriceDTO[] = input.flatMap( @@ -822,8 +704,8 @@ export default class PricingModuleService const priceRules = Object.entries(price.rules ?? {}).map( ([attribute, value]) => ({ - rule_type_id: ruleTypeMap.get(priceSetId)!.get(attribute)!.id, price_set_id: priceSetId, + attribute: attribute, value, }) ) @@ -831,7 +713,6 @@ export default class PricingModuleService return { ...price, price_set_id: priceSetId, - title: "test", // TODO: accept title rules_count: numberOfRules, price_rules: priceRules, } @@ -887,93 +768,62 @@ export default class PricingModuleService data: PricingTypes.CreatePriceListDTO[], @MedusaContext() sharedContext: Context = {} ) { - const ruleTypeAttributes: string[] = [] + const normalized = this.normalizePriceListDate(data) - for (const priceListData of data) { - const { prices = [], rules: priceListRules = {} } = priceListData - - ruleTypeAttributes.push(...Object.keys(priceListRules)) - - for (const price of prices) { - const { rules: priceListPriceRules = {} } = price - - ruleTypeAttributes.push(...Object.keys(priceListPriceRules)) - } - } - - const ruleTypes = await this.listRuleTypes( - { rule_attribute: ruleTypeAttributes }, - { take: null }, - sharedContext - ) - - const invalidRuleTypes = arrayDifference( - deduplicate(ruleTypeAttributes), - ruleTypes.map((ruleType) => ruleType.rule_attribute) - ) - - if (invalidRuleTypes.length) { - throw new MedusaError( - MedusaError.Types.INVALID_DATA, - `Cannot find RuleTypes with rule_attribute - ${invalidRuleTypes.join( - ", " - )}` - ) - } - - const ruleTypeMap: Map = new Map( - ruleTypes.map((rt) => [rt.rule_attribute, rt]) - ) - - const priceListsToCreate: PricingTypes.CreatePriceListDTO[] = data.map( + const priceListsToCreate: CreatePriceListDTO[] = normalized.map( (priceListData) => { - const id = generateEntityId( - (priceListData as unknown as PriceList).id, - PriceListIdPrefix - ) + const entry = { + ...priceListData, + rules: undefined, + } as CreatePriceListDTO - const { prices = [], rules = {}, ...rest } = priceListData + if (priceListData.prices) { + const pricesData = priceListData.prices.map((price) => { + let { rules: priceRules = {}, ...rest } = price + const cleanRules = priceRules ? removeNullish(priceRules) : {} + const rules = Object.entries(cleanRules) + const numberOfRules = rules.length - validatePriceListDates(priceListData) - - const priceListRules = Object.entries(rules).map( - ([attribute, value]) => { - const ruleType = ruleTypeMap.get(attribute)! - return { - price_list_id: id, - rule_type_id: ruleType.id, - price_list_rule_values: value.map((v) => ({ value: v })), - } - } - ) - - const pricesData = prices.map((price) => { - const priceRules = Object.entries(price.rules ?? {}).map( - ([ruleAttribute, ruleValue]) => { - return { - price_set_id: price.price_set_id, - rule_type_id: ruleTypeMap.get(ruleAttribute)!?.id, - value: ruleValue, + const rulesDataMap = new Map() + rules.map(([attribute, value]) => { + const rule = { + attribute, + value, } + rulesDataMap.set(JSON.stringify(rule), rule) + }) + + return { + ...rest, + rules_count: numberOfRules, + price_rules: Array.from(rulesDataMap.values()), } - ) + }) - return { - price_list_id: id, - title: "test", - rules_count: Object.keys(price.rules ?? {}).length, - price_rules: priceRules, - ...price, - } - }) - - return { - id, - ...rest, - rules_count: Object.keys(rules).length, - price_list_rules: priceListRules, - prices: pricesData, + entry.prices = pricesData } + + if (priceListData.rules) { + const cleanRules = priceListData.rules + ? removeNullish(priceListData.rules) + : {} + const rules = Object.entries(cleanRules) + const numberOfRules = rules.length + + const rulesDataMap = new Map() + rules.map(([attribute, value]) => { + const rule = { + attribute, + value, + } + rulesDataMap.set(JSON.stringify(rule), rule) + }) + + entry.price_list_rules = Array.from(rulesDataMap.values()) + entry.rules_count = numberOfRules + } + + return entry } ) @@ -1051,104 +901,63 @@ export default class PricingModuleService data: PricingTypes.UpdatePriceListDTO[], @MedusaContext() sharedContext: Context = {} ) { - const updatedPriceLists: PricingTypes.PriceListDTO[] = [] - const ruleAttributes: string[] = [] - const priceListIds: string[] = [] - - for (const priceListData of data) { - if (typeof priceListData.rules === "object") { - ruleAttributes.push(...Object.keys(priceListData.rules)) - priceListIds.push(priceListData.id) - } - } - - const existingPriceLists = await this.listPriceLists( - { id: priceListIds }, - { relations: ["price_list_rules"] }, - sharedContext - ) - - const priceListRuleIds = existingPriceLists - .map((pl) => (pl.price_list_rules || []).map((plr) => plr.id)) - .flat() - - const existingPriceListRules = await this.listPriceListRules( - { id: priceListRuleIds }, + const existingPriceLists = await this.priceListService_.list( + { id: data.map((d) => d.id) }, {}, sharedContext ) - if (existingPriceListRules.length) { - await this.deletePriceListRules( - existingPriceListRules.map((plr) => plr.id), - sharedContext + if (existingPriceLists.length !== data.length) { + const diff = arrayDifference( + data.map((d) => d.id), + existingPriceLists.map((p) => p.id) + ) + throw new MedusaError( + MedusaError.Types.INVALID_DATA, + `Price lists with ids: '${diff.join(", ")}' not found` ) } - const ruleTypes = await this.listRuleTypes( - { rule_attribute: ruleAttributes }, - { take: null }, - sharedContext - ) - - const ruleTypeMap: Map = new Map( - ruleTypes.map((rt) => [rt.rule_attribute, rt]) - ) - - for (const priceListData of data) { - const { rules, ...priceListOnlyData } = priceListData - - const updatePriceListData: any = { - ...priceListOnlyData, - } - - validatePriceListDates(updatePriceListData) - - if (typeof rules === "object") { - updatePriceListData.rules_count = Object.keys(rules).length - } - - const [updatedPriceList] = (await this.priceListService_.update( - [updatePriceListData], - sharedContext - )) as unknown as PricingTypes.PriceListDTO[] - - updatedPriceLists.push(updatedPriceList) - - for (const [ruleAttribute, ruleValues = []] of Object.entries( - rules || {} - )) { - let ruleType = ruleTypeMap.get(ruleAttribute) - - if (!ruleType) { - ;[ruleType] = await this.createRuleTypes( - [{ name: ruleAttribute, rule_attribute: ruleAttribute }], - sharedContext - ) - - ruleTypeMap.set(ruleAttribute, ruleType!) + const normalizedData = this.normalizePriceListDate(data).map( + (priceList) => { + const entry: Partial = { + ...priceList, + rules: undefined, + price_list_rules: undefined, } - const [priceListRule] = await this.priceListRuleService_.create( - [ - { - price_list_id: updatedPriceList.id, - rule_type_id: ruleType?.id, - }, - ], - sharedContext - ) + if (typeof priceList.rules === "object") { + const cleanRules = priceList.rules + ? removeNullish(priceList.rules) + : {} + const rules = Object.entries(cleanRules) + const numberOfRules = rules.length - for (const ruleValue of ruleValues) { - await this.priceListRuleValueService_.create( - [{ price_list_rule_id: priceListRule.id, value: ruleValue }], - sharedContext - ) + const rulesDataMap = new Map() + rules.map(([attribute, value]) => { + const rule = { + attribute, + value, + } + rulesDataMap.set(JSON.stringify(rule), rule) + }) + + entry.price_list_rules = Array.from(rulesDataMap.values()) + entry.rules_count = numberOfRules } - } - } - return updatedPriceLists + return entry + } + ) + + const { entities } = await this.priceListService_.upsertWithReplace( + normalizedData, + { + relations: ["price_list_rules"], + } + ) + + return entities } @InjectTransactionManager("baseRepository_") @@ -1156,7 +965,6 @@ export default class PricingModuleService data: PricingTypes.UpdatePriceListPricesDTO[], sharedContext: Context = {} ): Promise { - const ruleTypeAttributes: string[] = [] const priceListIds: string[] = [] const priceIds: string[] = [] @@ -1165,7 +973,6 @@ export default class PricingModuleService for (const price of priceListData.prices) { priceIds.push(price.id) - ruleTypeAttributes.push(...Object.keys(price.rules || {})) } } @@ -1179,16 +986,6 @@ export default class PricingModuleService prices.map((price) => [price.id, price]) ) - const ruleTypes = await this.listRuleTypes( - { rule_attribute: ruleTypeAttributes }, - { take: null }, - sharedContext - ) - - const ruleTypeMap: Map = new Map( - ruleTypes.map((rt) => [rt.rule_attribute, rt]) - ) - const priceLists = await this.listPriceLists( { id: priceListIds }, { take: null }, @@ -1219,7 +1016,7 @@ export default class PricingModuleService priceRulesToCreate.push( ...Object.entries(rules).map(([ruleAttribute, ruleValue]) => ({ price_set_id, - rule_type_id: ruleTypeMap.get(ruleAttribute)!.id, + attribute: ruleAttribute, value: ruleValue, price_id: price.id, })) @@ -1257,29 +1054,12 @@ export default class PricingModuleService data: PricingTypes.AddPriceListPricesDTO[], sharedContext: Context = {} ): Promise { - const ruleTypeAttributes: string[] = [] const priceListIds: string[] = [] - const priceSetIds: string[] = [] for (const priceListData of data) { priceListIds.push(priceListData.price_list_id) - - for (const price of priceListData.prices) { - ruleTypeAttributes.push(...Object.keys(price.rules || {})) - priceSetIds.push(price.price_set_id) - } } - const ruleTypes = await this.listRuleTypes( - { rule_attribute: ruleTypeAttributes }, - { take: null }, - sharedContext - ) - - const ruleTypeMap: Map = new Map( - ruleTypes.map((rt) => [rt.rule_attribute, rt]) - ) - const priceLists = await this.listPriceLists( { id: priceListIds }, {}, @@ -1307,8 +1087,8 @@ export default class PricingModuleService const priceRulesToCreate = Object.entries(priceRules).map( ([ruleAttribute, ruleValue]) => { return { - price_set_id: priceData.price_set_id, - rule_type_id: ruleTypeMap.get(ruleAttribute)!?.id, + price_list_id: priceData.price_set_id, + attribute: ruleAttribute, value: ruleValue, } } @@ -1373,113 +1153,60 @@ export default class PricingModuleService sharedContext: Context = {} ): Promise { // TODO: re think this method - const priceLists = await this.priceListService_.list( { id: data.map((d) => d.price_list_id) }, - { relations: ["price_list_rules", "price_list_rules.rule_type"] }, + { + relations: ["price_list_rules"], + }, sharedContext ) - const priceListMap = new Map(priceLists.map((p) => [p.id, p])) - const ruleTypes = await this.listRuleTypes( - { rule_attribute: data.map((d) => Object.keys(d.rules)).flat() }, - { take: null } + const rulesMap = new Map() + data.forEach((rule) => { + if (!rulesMap.has(rule.price_list_id)) { + rulesMap.set(rule.price_list_id, []) + } + + Object.entries(rule.rules).forEach(([key, value]) => { + rulesMap.get(rule.price_list_id).push([key, value]) + }) + }) + + const priceListsUpsert = priceLists + .map((priceList) => { + const allRules = new Map( + priceList.price_list_rules + .toArray() + .map((r) => [r.attribute, r.value]) + ) + + const rules = rulesMap.get(priceList.id) + if (!rules?.length) { + return + } + + rules.forEach(([key, value]) => { + allRules.set(key, value) + }) + + return { + ...priceList, + rules_count: allRules.size, + price_list_rules: Array.from(allRules).map(([attribute, value]) => ({ + attribute, + value, + })), + } + }) + .filter(Boolean) + + const { entities } = await this.priceListService_.upsertWithReplace( + priceListsUpsert, + { relations: ["price_list_rules"] }, + sharedContext ) - const ruleTypeMap = new Map(ruleTypes.map((rt) => [rt.rule_attribute, rt])) - - const ruleIdsToUpdate: string[] = [] - const rulesToCreate: CreatePriceListRuleDTO[] = [] - const priceRuleValues = new Map>() - - for (const { price_list_id: priceListId, rules } of data) { - const priceList = priceListMap.get(priceListId) - - if (!priceList) { - throw new MedusaError( - MedusaError.Types.INVALID_DATA, - `Price list with id: ${priceListId} not found` - ) - } - - const priceListRulesMap: Map = new Map( - priceList.price_list_rules.map((p) => [p.rule_type.rule_attribute, p]) - ) - - const priceListRuleValues = new Map() - - Object.entries(rules).map(async ([key, value]) => { - const ruleType = ruleTypeMap.get(key) - if (!ruleType) { - throw new MedusaError( - MedusaError.Types.INVALID_DATA, - `Rule type with attribute: ${key} not found` - ) - } - - const rule = priceListRulesMap.get(key) - - priceListRuleValues.set( - ruleType.id, - Array.isArray(value) ? value : [value] - ) - - if (!rule) { - rulesToCreate.push({ - rule_type_id: ruleType.id, - price_list_id: priceListId, - }) - } else { - ruleIdsToUpdate.push(rule.id) - } - }) - - priceRuleValues.set(priceListId, priceListRuleValues) - } - - const [createdRules, priceListValuesToDelete] = await promiseAll([ - this.priceListRuleService_.create(rulesToCreate), - this.priceListRuleValueService_.list( - { price_list_rule_id: ruleIdsToUpdate }, - { take: null } - ), - ]) - - const priceListRuleValuesToCreate: unknown[] = [] - - for (const { id, price_list_id, rule_type_id } of createdRules) { - const ruleValues = priceRuleValues.get(price_list_id) - - if (!ruleValues) { - continue - } - - const values = ruleValues.get(rule_type_id) - - if (!values) { - continue - } - - values.forEach((v) => { - priceListRuleValuesToCreate.push({ - price_list_rule_id: id, - value: v, - }) - }) - } - - await promiseAll([ - this.priceListRuleValueService_.delete( - priceListValuesToDelete.map((p) => p.id), - sharedContext - ), - this.priceListRuleValueService_.create( - priceListRuleValuesToCreate, - sharedContext - ), - ]) - - return priceLists + return entities } @InjectTransactionManager("baseRepository_") @@ -1487,40 +1214,84 @@ export default class PricingModuleService data: PricingTypes.RemovePriceListRulesDTO[], sharedContext: Context = {} ): Promise { + // TODO: re think this method const priceLists = await this.priceListService_.list( { id: data.map((d) => d.price_list_id) }, - { relations: ["price_list_rules", "price_list_rules.rule_type"] }, + { + relations: ["price_list_rules"], + }, sharedContext ) - const priceListMap = new Map(priceLists.map((p) => [p.id, p])) - const idsToDelete: string[] = [] - - for (const { price_list_id: priceListId, rules } of data) { - const priceList = priceListMap.get(priceListId) - - if (!priceList) { - throw new MedusaError( - MedusaError.Types.INVALID_DATA, - `Price list with id: ${priceListId} not found` - ) + const rulesMap = new Map() + data.forEach((rule) => { + if (!rulesMap.has(rule.price_list_id)) { + rulesMap.set(rule.price_list_id, []) } - const priceListRulesMap: Map = new Map( - priceList.price_list_rules.map((p) => [p.rule_type.rule_attribute, p]) - ) + rule.rules.forEach((key) => { + rulesMap.get(rule.price_list_id).push([key, undefined]) + }) + }) - rules.map(async (rule_attribute) => { - const rule = priceListRulesMap.get(rule_attribute) + const priceListsUpsert = priceLists + .map((priceList) => { + const allRules = new Map( + priceList.price_list_rules + .toArray() + .map((r) => [r.attribute, r.value]) + ) - if (rule) { - idsToDelete.push(rule.id) + const rules = rulesMap.get(priceList.id) + if (!rules?.length) { + return + } + + rules.forEach(([key, value]) => { + allRules.set(key, value) + }) + + return { + ...priceList, + rules_count: allRules.size, + price_list_rules: Array.from(allRules) + .map(([attribute, value]) => ({ + attribute, + value, + })) + .filter((r) => !!r.value), } }) - } + .filter(Boolean) - await this.priceListRuleService_.delete(idsToDelete) + const { entities } = await this.priceListService_.upsertWithReplace( + priceListsUpsert, + { relations: ["price_list_rules"] }, + sharedContext + ) - return priceLists + return entities + } + + protected normalizePriceListDate( + data: ( + | ServiceTypes.UpdatePriceListDTO + | ServiceTypes.CreatePriceListDTO + | CreatePriceListDTO + )[] + ) { + return data.map((priceListData: any) => { + validatePriceListDates(priceListData) + + if (!!priceListData.starts_at) { + priceListData.starts_at = GetIsoStringFromDate(priceListData.starts_at) + } + + if (!!priceListData.ends_at) { + priceListData.ends_at = GetIsoStringFromDate(priceListData.ends_at) + } + + return priceListData + }) } } diff --git a/packages/modules/pricing/src/services/rule-type.ts b/packages/modules/pricing/src/services/rule-type.ts deleted file mode 100644 index 26480f97b6..0000000000 --- a/packages/modules/pricing/src/services/rule-type.ts +++ /dev/null @@ -1,66 +0,0 @@ -import { Context, DAL, PricingTypes } from "@medusajs/types" -import { - InjectTransactionManager, - MedusaContext, - ModulesSdkUtils, - validateRuleAttributes, -} from "@medusajs/utils" -import { RuleType } from "@models" - -type InjectedDependencies = { - ruleTypeRepository: DAL.RepositoryService -} - -export default class RuleTypeService extends ModulesSdkUtils.MedusaInternalService( - RuleType -) { - protected readonly ruleTypeRepository_: DAL.RepositoryService - - constructor({ ruleTypeRepository }: InjectedDependencies) { - // @ts-ignore - super(...arguments) - this.ruleTypeRepository_ = ruleTypeRepository - } - - create( - data: PricingTypes.CreateRuleTypeDTO, - sharedContext: Context - ): Promise - create( - data: PricingTypes.CreateRuleTypeDTO[], - sharedContext: Context - ): Promise - - @InjectTransactionManager("ruleTypeRepository_") - async create( - data: PricingTypes.CreateRuleTypeDTO | PricingTypes.CreateRuleTypeDTO[], - @MedusaContext() sharedContext: Context = {} - ): Promise { - const data_ = Array.isArray(data) ? data : [data] - validateRuleAttributes(data_.map((d) => d.rule_attribute)) - return await super.create(data, sharedContext) - } - - // @ts-ignore - update( - data: PricingTypes.UpdateRuleTypeDTO[], - sharedContext: Context - ): Promise - // @ts-ignore - update( - data: PricingTypes.UpdateRuleTypeDTO, - sharedContext: Context - ): Promise - - @InjectTransactionManager("ruleTypeRepository_") - // TODO: add support for selector? and then rm ts ignore - // @ts-ignore - async update( - data: PricingTypes.UpdateRuleTypeDTO | PricingTypes.UpdateRuleTypeDTO[], - @MedusaContext() sharedContext: Context = {} - ): Promise { - const data_ = Array.isArray(data) ? data : [data] - validateRuleAttributes(data_.map((d) => d.rule_attribute)) - return await super.update(data, sharedContext) - } -} diff --git a/packages/modules/pricing/src/types/services/price-list.ts b/packages/modules/pricing/src/types/services/price-list.ts index a5f95ea891..ecf2b063ec 100644 --- a/packages/modules/pricing/src/types/services/price-list.ts +++ b/packages/modules/pricing/src/types/services/price-list.ts @@ -1,13 +1,12 @@ -import { PriceListStatus, PriceListType } from "@medusajs/types" +import { PriceListStatus, PricingTypes } from "@medusajs/types" -export interface CreatePriceListDTO { - title: string - description: string - starts_at?: string | null - ends_at?: string | null - status?: PriceListStatus - type?: PriceListType - number_rules?: number +export interface CreatePriceListDTO extends PricingTypes.CreatePriceListDTO { + rules_count?: number + price_list_rules?: { + attribute: string + value: string + }[] + prices?: PricingTypes.CreatePriceListPriceDTO[] } export interface UpdatePriceListDTO { diff --git a/packages/modules/pricing/src/utils/events.ts b/packages/modules/pricing/src/utils/events.ts index 47b40a3c2b..76389b159b 100644 --- a/packages/modules/pricing/src/utils/events.ts +++ b/packages/modules/pricing/src/utils/events.ts @@ -12,12 +12,6 @@ export const eventBuilders = { object: "price_set", eventsEnum: PricingEvents, }), - createdPriceSetRuleType: eventBuilderFactory({ - source: Modules.PRICING, - action: CommonEvents.CREATED, - object: "price_set_rule_type", - eventsEnum: PricingEvents, - }), createdPrice: eventBuilderFactory({ source: Modules.PRICING, action: CommonEvents.CREATED,