fix: normalize path before consuming it while loading models (#8194)

* fix: normalize path before consuming it while loading models

* Make it so that models derived from the service if present

* fix linkable tests that now match what has been passed to the medusa service if there is no custom joiner config

* fix linkable tests that now match what has been passed to the medusa service if there is no custom joiner config

* fix linkable tests that now match what has been passed to the medusa service if there is no custom joiner config
This commit is contained in:
Adrien de Peretti authored and GitHub committed 2024-07-21 21:21:05 +02:00
1 parent 2f56030101
commit 566300d54b
7 files changed
+203 -248

No files matched your search

@@ -5,7 +5,7 @@ import {
PropertyType, PropertyType,
} from "@medusajs/types" } from "@medusajs/types"
import * as path from "path" import * as path from "path"
import { dirname, join } from "path" import { dirname, join, normalize } from "path"
import { import {
camelToSnakeCase, camelToSnakeCase,
deduplicate, deduplicate,
@@ -77,11 +77,16 @@ export function defineJoinerConfig(
break break
} }
fullPath = normalize(fullPath)
const integrationTestPotentialPath = normalize(
"integration-tests/__tests__"
)
/** /**
* Handle integration-tests/__tests__ path based on conventional naming * Handle integration-tests/__tests__ path based on conventional naming
*/ */
if (fullPath.includes("integration-tests/__tests__")) { if (fullPath.includes(integrationTestPotentialPath)) {
const sourcePath = fullPath.split("integration-tests/__tests__")[0] const sourcePath = fullPath.split(integrationTestPotentialPath)[0]
fullPath = path.join(sourcePath, "src") fullPath = path.join(sourcePath, "src")
} }
@@ -90,7 +95,8 @@ export function defineJoinerConfig(
let basePath = splitPath[0] + srcDir let basePath = splitPath[0] + srcDir
const isMedusaProject = fullPath.includes(`${srcDir}/modules/`) const potentialModulesDirPathSegment = normalize(`${srcDir}/modules/`)
const isMedusaProject = fullPath.includes(potentialModulesDirPathSegment)
if (isMedusaProject) { if (isMedusaProject) {
basePath = dirname(fullPath) basePath = dirname(fullPath)
} }
@@ -33,11 +33,15 @@ export function Module<
): ModuleExports<Service> & { ): ModuleExports<Service> & {
linkable: Linkable linkable: Linkable
} { } {
const defaultJoinerConfig = defineJoinerConfig(serviceName)
service.prototype.__joinerConfig ??= () => defaultJoinerConfig
const modelObjects = service[MedusaServiceModelObjectsSymbol] ?? {} 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 let linkable = {} as Linkable
if (Object.keys(modelObjects)?.length) { if (Object.keys(modelObjects)?.length) {
@@ -16,16 +16,14 @@ moduleIntegrationTestRunner<ICartModuleService>({
}).linkable }).linkable
expect(Object.keys(linkable)).toEqual([ expect(Object.keys(linkable)).toEqual([
"address",
"adjustmentLine",
"cart", "cart",
"address",
"lineItem",
"lineItemAdjustment", "lineItemAdjustment",
"lineItemTaxLine", "lineItemTaxLine",
"lineItem", "shippingMethod",
"shippingMethodAdjustment", "shippingMethodAdjustment",
"shippingMethodTaxLine", "shippingMethodTaxLine",
"shippingMethod",
"taxLine",
]) ])
Object.keys(linkable).forEach((key) => { Object.keys(linkable).forEach((key) => {
@@ -33,6 +31,14 @@ moduleIntegrationTestRunner<ICartModuleService>({
}) })
expect(linkable).toEqual({ expect(linkable).toEqual({
cart: {
id: {
linkable: "cart_id",
primaryKey: "id",
serviceName: "cart",
field: "cart",
},
},
address: { address: {
id: { id: {
linkable: "address_id", linkable: "address_id",
@@ -41,20 +47,12 @@ moduleIntegrationTestRunner<ICartModuleService>({
field: "address", field: "address",
}, },
}, },
adjustmentLine: { lineItem: {
id: { id: {
linkable: "adjustment_line_id", linkable: "line_item_id",
primaryKey: "id", primaryKey: "id",
serviceName: "cart", serviceName: "cart",
field: "adjustmentLine", field: "lineItem",
},
},
cart: {
id: {
linkable: "cart_id",
primaryKey: "id",
serviceName: "cart",
field: "cart",
}, },
}, },
lineItemAdjustment: { lineItemAdjustment: {
@@ -73,12 +71,12 @@ moduleIntegrationTestRunner<ICartModuleService>({
field: "lineItemTaxLine", field: "lineItemTaxLine",
}, },
}, },
lineItem: { shippingMethod: {
id: { id: {
linkable: "line_item_id", linkable: "shipping_method_id",
primaryKey: "id", primaryKey: "id",
serviceName: "cart", serviceName: "cart",
field: "lineItem", field: "shippingMethod",
}, },
}, },
shippingMethodAdjustment: { shippingMethodAdjustment: {
@@ -97,22 +95,6 @@ moduleIntegrationTestRunner<ICartModuleService>({
field: "shippingMethodTaxLine", 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",
},
},
}) })
}) })
@@ -13,30 +13,25 @@ moduleIntegrationTestRunner<IOrderModuleService>({
}).linkable }).linkable
expect(Object.keys(linkable)).toEqual([ expect(Object.keys(linkable)).toEqual([
"order",
"address", "address",
"adjustmentLine", "lineItem",
"claimItemImage",
"orderClaimItem",
"orderClaim",
"orderExchangeItem",
"orderExchange",
"lineItemAdjustment", "lineItemAdjustment",
"lineItemTaxLine", "lineItemTaxLine",
"lineItem", "shippingMethod",
"orderChangeAction",
"orderChange",
"orderItem",
"orderShippingMethod",
"orderSummary",
"order",
"returnItem",
"returnReason",
"return",
"shippingMethodAdjustment", "shippingMethodAdjustment",
"shippingMethodTaxLine", "shippingMethodTaxLine",
"shippingMethod",
"taxLine",
"transaction", "transaction",
"orderChange",
"orderChangeAction",
"orderItem",
"orderSummary",
"orderShippingMethod",
"returnReason",
"return",
"returnItem",
"orderClaim",
"orderExchange",
]) ])
Object.keys(linkable).forEach((key) => { Object.keys(linkable).forEach((key) => {
@@ -44,6 +39,14 @@ moduleIntegrationTestRunner<IOrderModuleService>({
}) })
expect(linkable).toEqual({ expect(linkable).toEqual({
order: {
id: {
linkable: "order_id",
primaryKey: "id",
serviceName: "order",
field: "order",
},
},
address: { address: {
id: { id: {
linkable: "address_id", linkable: "address_id",
@@ -52,52 +55,12 @@ moduleIntegrationTestRunner<IOrderModuleService>({
field: "address", field: "address",
}, },
}, },
adjustmentLine: { lineItem: {
id: { id: {
linkable: "adjustment_line_id", linkable: "line_item_id",
primaryKey: "id", primaryKey: "id",
serviceName: "order", serviceName: "order",
field: "adjustmentLine", field: "lineItem",
},
},
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",
}, },
}, },
lineItemAdjustment: { lineItemAdjustment: {
@@ -116,84 +79,12 @@ moduleIntegrationTestRunner<IOrderModuleService>({
field: "lineItemTaxLine", field: "lineItemTaxLine",
}, },
}, },
lineItem: { shippingMethod: {
id: { id: {
linkable: "line_item_id", linkable: "shipping_method_id",
primaryKey: "id", primaryKey: "id",
serviceName: "order", serviceName: "order",
field: "lineItem", field: "shippingMethod",
},
},
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",
}, },
}, },
shippingMethodAdjustment: { shippingMethodAdjustment: {
@@ -212,22 +103,6 @@ moduleIntegrationTestRunner<IOrderModuleService>({
field: "shippingMethodTaxLine", 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: { transaction: {
id: { id: {
linkable: "transaction_id", linkable: "transaction_id",
@@ -236,6 +111,86 @@ moduleIntegrationTestRunner<IOrderModuleService>({
field: "transaction", 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",
},
},
}) })
}) })
}) })
@@ -15,21 +15,13 @@ moduleIntegrationTestRunner<IStoreModuleService>({
service: StoreModuleService, service: StoreModuleService,
}).linkable }).linkable
expect(Object.keys(linkable)).toEqual(["storeCurrency", "store"]) expect(Object.keys(linkable)).toEqual(["store", "storeCurrency"])
Object.keys(linkable).forEach((key) => { Object.keys(linkable).forEach((key) => {
delete linkable[key].toJSON delete linkable[key].toJSON
}) })
expect(linkable).toEqual({ expect(linkable).toEqual({
storeCurrency: {
id: {
linkable: "store_currency_id",
primaryKey: "id",
serviceName: "store",
field: "storeCurrency",
},
},
store: { store: {
id: { id: {
linkable: "store_id", linkable: "store_id",
@@ -38,6 +30,14 @@ moduleIntegrationTestRunner<IStoreModuleService>({
field: "store", field: "store",
}, },
}, },
storeCurrency: {
id: {
linkable: "store_currency_id",
primaryKey: "id",
serviceName: "store",
field: "storeCurrency",
},
},
}) })
}) })
@@ -16,10 +16,10 @@ moduleIntegrationTestRunner<ITaxModuleService>({
}).linkable }).linkable
expect(Object.keys(linkable)).toEqual([ expect(Object.keys(linkable)).toEqual([
"taxProvider",
"taxRateRule",
"taxRate", "taxRate",
"taxRegion", "taxRegion",
"taxRateRule",
"taxProvider",
]) ])
Object.keys(linkable).forEach((key) => { Object.keys(linkable).forEach((key) => {
@@ -27,22 +27,6 @@ moduleIntegrationTestRunner<ITaxModuleService>({
}) })
expect(linkable).toEqual({ 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: { taxRate: {
id: { id: {
linkable: "tax_rate_id", linkable: "tax_rate_id",
@@ -59,6 +43,22 @@ moduleIntegrationTestRunner<ITaxModuleService>({
field: "taxRegion", 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<ITaxModuleService>({
}, },
}) })
const error = await service.createTaxRegions({ const error = await service
country_code: "US", .createTaxRegions({
default_tax_rate: { country_code: "US",
name: "Test Rate", default_tax_rate: {
rate: 0.2, name: "Test Rate",
}, rate: 0.2,
}).catch(e => e) },
})
.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 () => { 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<ITaxModuleService>({
}, },
}) })
const error = await service.createTaxRegions({ const error = await service
country_code: "US", .createTaxRegions({
province_code: "CA", country_code: "US",
default_tax_rate: { province_code: "CA",
name: "Test Rate", default_tax_rate: {
rate: 0.2, name: "Test Rate",
}, rate: 0.2,
}).catch(e => e) },
})
.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 () => { it("should create tax rates and update them", async () => {
const region = await service.createTaxRegions({ const region = await service.createTaxRegions({
country_code: "US", country_code: "US",
@@ -33,21 +33,13 @@ moduleIntegrationTestRunner<IUserModuleService>({
service: UserModuleService, service: UserModuleService,
}).linkable }).linkable
expect(Object.keys(linkable)).toEqual(["invite", "user"]) expect(Object.keys(linkable)).toEqual(["user", "invite"])
Object.keys(linkable).forEach((key) => { Object.keys(linkable).forEach((key) => {
delete linkable[key].toJSON delete linkable[key].toJSON
}) })
expect(linkable).toEqual({ expect(linkable).toEqual({
invite: {
id: {
linkable: "invite_id",
primaryKey: "id",
serviceName: "user",
field: "invite",
},
},
user: { user: {
id: { id: {
linkable: "user_id", linkable: "user_id",
@@ -56,6 +48,14 @@ moduleIntegrationTestRunner<IUserModuleService>({
field: "user", field: "user",
}, },
}, },
invite: {
id: {
linkable: "invite_id",
primaryKey: "id",
serviceName: "user",
field: "invite",
},
},
}) })
}) })