diff --git a/packages/core/utils/src/modules-sdk/joiner-config-builder.ts b/packages/core/utils/src/modules-sdk/joiner-config-builder.ts index ba9c09643d..31236abc74 100644 --- a/packages/core/utils/src/modules-sdk/joiner-config-builder.ts +++ b/packages/core/utils/src/modules-sdk/joiner-config-builder.ts @@ -5,7 +5,7 @@ import { PropertyType, } from "@medusajs/types" import * as path from "path" -import { dirname, join } from "path" +import { dirname, join, normalize } from "path" import { camelToSnakeCase, deduplicate, @@ -77,11 +77,16 @@ export function defineJoinerConfig( break } + fullPath = normalize(fullPath) + const integrationTestPotentialPath = normalize( + "integration-tests/__tests__" + ) + /** * Handle integration-tests/__tests__ path based on conventional naming */ - if (fullPath.includes("integration-tests/__tests__")) { - const sourcePath = fullPath.split("integration-tests/__tests__")[0] + if (fullPath.includes(integrationTestPotentialPath)) { + const sourcePath = fullPath.split(integrationTestPotentialPath)[0] fullPath = path.join(sourcePath, "src") } @@ -90,7 +95,8 @@ export function defineJoinerConfig( let basePath = splitPath[0] + srcDir - const isMedusaProject = fullPath.includes(`${srcDir}/modules/`) + const potentialModulesDirPathSegment = normalize(`${srcDir}/modules/`) + const isMedusaProject = fullPath.includes(potentialModulesDirPathSegment) if (isMedusaProject) { basePath = dirname(fullPath) } diff --git a/packages/core/utils/src/modules-sdk/module.ts b/packages/core/utils/src/modules-sdk/module.ts index 610ea76064..16ca901a5a 100644 --- a/packages/core/utils/src/modules-sdk/module.ts +++ b/packages/core/utils/src/modules-sdk/module.ts @@ -33,11 +33,15 @@ export function Module< ): ModuleExports & { linkable: Linkable } { - const defaultJoinerConfig = defineJoinerConfig(serviceName) - service.prototype.__joinerConfig ??= () => defaultJoinerConfig - const modelObjects = service[MedusaServiceModelObjectsSymbol] ?? {} + const defaultJoinerConfig = defineJoinerConfig(serviceName, { + models: Object.keys(modelObjects).length + ? Object.values(modelObjects) + : undefined, + }) + service.prototype.__joinerConfig ??= () => defaultJoinerConfig + let linkable = {} as Linkable if (Object.keys(modelObjects)?.length) { diff --git a/packages/modules/cart/integration-tests/__tests__/services/cart-module/index.spec.ts b/packages/modules/cart/integration-tests/__tests__/services/cart-module/index.spec.ts index f7168129dc..1297a1f71b 100644 --- a/packages/modules/cart/integration-tests/__tests__/services/cart-module/index.spec.ts +++ b/packages/modules/cart/integration-tests/__tests__/services/cart-module/index.spec.ts @@ -16,16 +16,14 @@ moduleIntegrationTestRunner({ }).linkable expect(Object.keys(linkable)).toEqual([ - "address", - "adjustmentLine", "cart", + "address", + "lineItem", "lineItemAdjustment", "lineItemTaxLine", - "lineItem", + "shippingMethod", "shippingMethodAdjustment", "shippingMethodTaxLine", - "shippingMethod", - "taxLine", ]) Object.keys(linkable).forEach((key) => { @@ -33,6 +31,14 @@ moduleIntegrationTestRunner({ }) expect(linkable).toEqual({ + cart: { + id: { + linkable: "cart_id", + primaryKey: "id", + serviceName: "cart", + field: "cart", + }, + }, address: { id: { linkable: "address_id", @@ -41,20 +47,12 @@ moduleIntegrationTestRunner({ field: "address", }, }, - adjustmentLine: { + lineItem: { id: { - linkable: "adjustment_line_id", + linkable: "line_item_id", primaryKey: "id", serviceName: "cart", - field: "adjustmentLine", - }, - }, - cart: { - id: { - linkable: "cart_id", - primaryKey: "id", - serviceName: "cart", - field: "cart", + field: "lineItem", }, }, lineItemAdjustment: { @@ -73,12 +71,12 @@ moduleIntegrationTestRunner({ field: "lineItemTaxLine", }, }, - lineItem: { + shippingMethod: { id: { - linkable: "line_item_id", + linkable: "shipping_method_id", primaryKey: "id", serviceName: "cart", - field: "lineItem", + field: "shippingMethod", }, }, shippingMethodAdjustment: { @@ -97,22 +95,6 @@ moduleIntegrationTestRunner({ field: "shippingMethodTaxLine", }, }, - shippingMethod: { - id: { - linkable: "shipping_method_id", - primaryKey: "id", - serviceName: "cart", - field: "shippingMethod", - }, - }, - taxLine: { - id: { - linkable: "tax_line_id", - primaryKey: "id", - serviceName: "cart", - field: "taxLine", - }, - }, }) }) diff --git a/packages/modules/order/integration-tests/__tests__/index.spec.ts b/packages/modules/order/integration-tests/__tests__/index.spec.ts index eda63ff131..223a5076ed 100644 --- a/packages/modules/order/integration-tests/__tests__/index.spec.ts +++ b/packages/modules/order/integration-tests/__tests__/index.spec.ts @@ -13,30 +13,25 @@ moduleIntegrationTestRunner({ }).linkable expect(Object.keys(linkable)).toEqual([ + "order", "address", - "adjustmentLine", - "claimItemImage", - "orderClaimItem", - "orderClaim", - "orderExchangeItem", - "orderExchange", + "lineItem", "lineItemAdjustment", "lineItemTaxLine", - "lineItem", - "orderChangeAction", - "orderChange", - "orderItem", - "orderShippingMethod", - "orderSummary", - "order", - "returnItem", - "returnReason", - "return", + "shippingMethod", "shippingMethodAdjustment", "shippingMethodTaxLine", - "shippingMethod", - "taxLine", "transaction", + "orderChange", + "orderChangeAction", + "orderItem", + "orderSummary", + "orderShippingMethod", + "returnReason", + "return", + "returnItem", + "orderClaim", + "orderExchange", ]) Object.keys(linkable).forEach((key) => { @@ -44,6 +39,14 @@ moduleIntegrationTestRunner({ }) expect(linkable).toEqual({ + order: { + id: { + linkable: "order_id", + primaryKey: "id", + serviceName: "order", + field: "order", + }, + }, address: { id: { linkable: "address_id", @@ -52,52 +55,12 @@ moduleIntegrationTestRunner({ field: "address", }, }, - adjustmentLine: { + lineItem: { id: { - linkable: "adjustment_line_id", + linkable: "line_item_id", primaryKey: "id", serviceName: "order", - field: "adjustmentLine", - }, - }, - claimItemImage: { - id: { - linkable: "claim_item_image_id", - primaryKey: "id", - serviceName: "order", - field: "claimItemImage", - }, - }, - orderClaimItem: { - id: { - linkable: "order_claim_item_id", - primaryKey: "id", - serviceName: "order", - field: "orderClaimItem", - }, - }, - orderClaim: { - id: { - linkable: "order_claim_id", - primaryKey: "id", - serviceName: "order", - field: "orderClaim", - }, - }, - orderExchangeItem: { - id: { - linkable: "order_exchange_item_id", - primaryKey: "id", - serviceName: "order", - field: "orderExchangeItem", - }, - }, - orderExchange: { - id: { - linkable: "order_exchange_id", - primaryKey: "id", - serviceName: "order", - field: "orderExchange", + field: "lineItem", }, }, lineItemAdjustment: { @@ -116,84 +79,12 @@ moduleIntegrationTestRunner({ field: "lineItemTaxLine", }, }, - lineItem: { + shippingMethod: { id: { - linkable: "line_item_id", + linkable: "shipping_method_id", primaryKey: "id", serviceName: "order", - field: "lineItem", - }, - }, - orderChangeAction: { - id: { - linkable: "order_change_action_id", - primaryKey: "id", - serviceName: "order", - field: "orderChangeAction", - }, - }, - orderChange: { - id: { - linkable: "order_change_id", - primaryKey: "id", - serviceName: "order", - field: "orderChange", - }, - }, - orderItem: { - id: { - linkable: "order_item_id", - primaryKey: "id", - serviceName: "order", - field: "orderItem", - }, - }, - orderShippingMethod: { - id: { - linkable: "order_shipping_method_id", - primaryKey: "id", - serviceName: "order", - field: "orderShippingMethod", - }, - }, - orderSummary: { - id: { - linkable: "order_summary_id", - primaryKey: "id", - serviceName: "order", - field: "orderSummary", - }, - }, - order: { - id: { - linkable: "order_id", - primaryKey: "id", - serviceName: "order", - field: "order", - }, - }, - returnItem: { - id: { - linkable: "return_item_id", - primaryKey: "id", - serviceName: "order", - field: "returnItem", - }, - }, - returnReason: { - id: { - linkable: "return_reason_id", - primaryKey: "id", - serviceName: "order", - field: "returnReason", - }, - }, - return: { - id: { - linkable: "return_id", - primaryKey: "id", - serviceName: "order", - field: "return", + field: "shippingMethod", }, }, shippingMethodAdjustment: { @@ -212,22 +103,6 @@ moduleIntegrationTestRunner({ field: "shippingMethodTaxLine", }, }, - shippingMethod: { - id: { - linkable: "shipping_method_id", - primaryKey: "id", - serviceName: "order", - field: "shippingMethod", - }, - }, - taxLine: { - id: { - linkable: "tax_line_id", - primaryKey: "id", - serviceName: "order", - field: "taxLine", - }, - }, transaction: { id: { linkable: "transaction_id", @@ -236,6 +111,86 @@ moduleIntegrationTestRunner({ field: "transaction", }, }, + orderChange: { + id: { + linkable: "order_change_id", + primaryKey: "id", + serviceName: "order", + field: "orderChange", + }, + }, + orderChangeAction: { + id: { + linkable: "order_change_action_id", + primaryKey: "id", + serviceName: "order", + field: "orderChangeAction", + }, + }, + orderItem: { + id: { + linkable: "order_item_id", + primaryKey: "id", + serviceName: "order", + field: "orderItem", + }, + }, + orderSummary: { + id: { + linkable: "order_summary_id", + primaryKey: "id", + serviceName: "order", + field: "orderSummary", + }, + }, + orderShippingMethod: { + id: { + linkable: "order_shipping_method_id", + primaryKey: "id", + serviceName: "order", + field: "orderShippingMethod", + }, + }, + returnReason: { + id: { + linkable: "return_reason_id", + primaryKey: "id", + serviceName: "order", + field: "returnReason", + }, + }, + return: { + id: { + linkable: "return_id", + primaryKey: "id", + serviceName: "order", + field: "return", + }, + }, + returnItem: { + id: { + linkable: "return_item_id", + primaryKey: "id", + serviceName: "order", + field: "returnItem", + }, + }, + orderClaim: { + id: { + linkable: "order_claim_id", + primaryKey: "id", + serviceName: "order", + field: "orderClaim", + }, + }, + orderExchange: { + id: { + linkable: "order_exchange_id", + primaryKey: "id", + serviceName: "order", + field: "orderExchange", + }, + }, }) }) }) diff --git a/packages/modules/store/integration-tests/__tests__/store-module-service.spec.ts b/packages/modules/store/integration-tests/__tests__/store-module-service.spec.ts index 67fcd1b9a2..7674c2eda7 100644 --- a/packages/modules/store/integration-tests/__tests__/store-module-service.spec.ts +++ b/packages/modules/store/integration-tests/__tests__/store-module-service.spec.ts @@ -15,21 +15,13 @@ moduleIntegrationTestRunner({ service: StoreModuleService, }).linkable - expect(Object.keys(linkable)).toEqual(["storeCurrency", "store"]) + expect(Object.keys(linkable)).toEqual(["store", "storeCurrency"]) Object.keys(linkable).forEach((key) => { delete linkable[key].toJSON }) expect(linkable).toEqual({ - storeCurrency: { - id: { - linkable: "store_currency_id", - primaryKey: "id", - serviceName: "store", - field: "storeCurrency", - }, - }, store: { id: { linkable: "store_id", @@ -38,6 +30,14 @@ moduleIntegrationTestRunner({ field: "store", }, }, + storeCurrency: { + id: { + linkable: "store_currency_id", + primaryKey: "id", + serviceName: "store", + field: "storeCurrency", + }, + }, }) }) diff --git a/packages/modules/tax/integration-tests/__tests__/index.spec.ts b/packages/modules/tax/integration-tests/__tests__/index.spec.ts index a10ddd5c86..86a5171542 100644 --- a/packages/modules/tax/integration-tests/__tests__/index.spec.ts +++ b/packages/modules/tax/integration-tests/__tests__/index.spec.ts @@ -16,10 +16,10 @@ moduleIntegrationTestRunner({ }).linkable expect(Object.keys(linkable)).toEqual([ - "taxProvider", - "taxRateRule", "taxRate", "taxRegion", + "taxRateRule", + "taxProvider", ]) Object.keys(linkable).forEach((key) => { @@ -27,22 +27,6 @@ moduleIntegrationTestRunner({ }) expect(linkable).toEqual({ - taxProvider: { - id: { - linkable: "tax_provider_id", - primaryKey: "id", - serviceName: "tax", - field: "taxProvider", - }, - }, - taxRateRule: { - id: { - linkable: "tax_rate_rule_id", - primaryKey: "id", - serviceName: "tax", - field: "taxRateRule", - }, - }, taxRate: { id: { linkable: "tax_rate_id", @@ -59,6 +43,22 @@ moduleIntegrationTestRunner({ field: "taxRegion", }, }, + taxRateRule: { + id: { + linkable: "tax_rate_rule_id", + primaryKey: "id", + serviceName: "tax", + field: "taxRateRule", + }, + }, + taxProvider: { + id: { + linkable: "tax_provider_id", + primaryKey: "id", + serviceName: "tax", + field: "taxProvider", + }, + }, }) }) @@ -214,15 +214,19 @@ moduleIntegrationTestRunner({ }, }) - const error = await service.createTaxRegions({ - country_code: "US", - default_tax_rate: { - name: "Test Rate", - rate: 0.2, - }, - }).catch(e => e) + const error = await service + .createTaxRegions({ + country_code: "US", + default_tax_rate: { + name: "Test Rate", + rate: 0.2, + }, + }) + .catch((e) => e) - expect(error.message).toEqual("Tax region with country_code: us, already exists.") + expect(error.message).toEqual( + "Tax region with country_code: us, already exists." + ) }) it("should throw when creating a tax region with a country code and province code of an existing region", async () => { @@ -235,18 +239,22 @@ moduleIntegrationTestRunner({ }, }) - const error = await service.createTaxRegions({ - country_code: "US", - province_code: "CA", - default_tax_rate: { - name: "Test Rate", - rate: 0.2, - }, - }).catch(e => e) + const error = await service + .createTaxRegions({ + country_code: "US", + province_code: "CA", + default_tax_rate: { + name: "Test Rate", + rate: 0.2, + }, + }) + .catch((e) => e) - expect(error.message).toEqual("Tax region with country_code: us, province_code: ca, already exists.") + expect(error.message).toEqual( + "Tax region with country_code: us, province_code: ca, already exists." + ) }) - + it("should create tax rates and update them", async () => { const region = await service.createTaxRegions({ country_code: "US", diff --git a/packages/modules/user/integration-tests/__tests__/user.spec.ts b/packages/modules/user/integration-tests/__tests__/user.spec.ts index 7be2c41d2c..07eddfc09b 100644 --- a/packages/modules/user/integration-tests/__tests__/user.spec.ts +++ b/packages/modules/user/integration-tests/__tests__/user.spec.ts @@ -33,21 +33,13 @@ moduleIntegrationTestRunner({ service: UserModuleService, }).linkable - expect(Object.keys(linkable)).toEqual(["invite", "user"]) + expect(Object.keys(linkable)).toEqual(["user", "invite"]) Object.keys(linkable).forEach((key) => { delete linkable[key].toJSON }) expect(linkable).toEqual({ - invite: { - id: { - linkable: "invite_id", - primaryKey: "id", - serviceName: "user", - field: "invite", - }, - }, user: { id: { linkable: "user_id", @@ -56,6 +48,14 @@ moduleIntegrationTestRunner({ field: "user", }, }, + invite: { + id: { + linkable: "invite_id", + primaryKey: "id", + serviceName: "user", + field: "invite", + }, + }, }) })