diff --git a/integration-tests/modules/__tests__/link-modules/sales-channel-location.spec.ts b/integration-tests/modules/__tests__/link-modules/sales-channel-location.spec.ts new file mode 100644 index 0000000000..3361fffe63 --- /dev/null +++ b/integration-tests/modules/__tests__/link-modules/sales-channel-location.spec.ts @@ -0,0 +1,118 @@ +import { ModuleRegistrationName, Modules } from "@medusajs/modules-sdk" +import { + ISalesChannelModuleService, + IStockLocationService, +} from "@medusajs/types" +import { remoteQueryObjectFromString } from "@medusajs/utils" +import { medusaIntegrationTestRunner } from "medusa-test-utils" + +jest.setTimeout(50000) + +const env = { MEDUSA_FF_MEDUSA_V2: true } + +medusaIntegrationTestRunner({ + env, + testSuite: ({ getContainer }) => { + describe("Cart links", () => { + let appContainer + let scService: ISalesChannelModuleService + let locationService: IStockLocationService + let remoteQuery, remoteLink + + beforeAll(async () => { + appContainer = getContainer() + scService = appContainer.resolve(ModuleRegistrationName.SALES_CHANNEL) + locationService = appContainer.resolve( + ModuleRegistrationName.STOCK_LOCATION + ) + remoteQuery = appContainer.resolve("remoteQuery") + remoteLink = appContainer.resolve("remoteLink") + }) + + it("should query carts, sales channels, customers, regions with remote query", async () => { + const scWebshop = await scService.create({ + name: "Webshop", + }) + + const scCphStore = await scService.create({ + name: "CPH store", + }) + + const scNycStore = await scService.create({ + name: "NYC store", + }) + + const euWarehouse = await locationService.create({ + name: "EU Warehouse", + }) + + const usWarehouse = await locationService.create({ + name: "US Warehouse", + }) + + await remoteLink.create([ + { + [Modules.SALES_CHANNEL]: { + sales_channel_id: scWebshop.id, + }, + [Modules.STOCK_LOCATION]: { + location_id: euWarehouse.id, + }, + }, + { + [Modules.SALES_CHANNEL]: { + sales_channel_id: scCphStore.id, + }, + [Modules.STOCK_LOCATION]: { + location_id: euWarehouse.id, + }, + }, + { + [Modules.SALES_CHANNEL]: { + sales_channel_id: scNycStore.id, + }, + [Modules.STOCK_LOCATION]: { + location_id: usWarehouse.id, + }, + }, + ]) + + const euStockLocationChannelQuery = remoteQueryObjectFromString({ + entryPoint: "stock_locations", + fields: ["id", "name", "sales_channels.id", "sales_channels.name"], + variables: { id: euWarehouse.id }, + }) + + const usStockLocationChannelQuery = remoteQueryObjectFromString({ + entryPoint: "stock_locations", + fields: ["id", "name", "sales_channels.id", "sales_channels.name"], + variables: { id: usWarehouse.id }, + }) + + const euLocations = await remoteQuery(euStockLocationChannelQuery) + const usLocations = await remoteQuery(usStockLocationChannelQuery) + + expect(euLocations.length).toBe(1) + expect(euLocations).toEqual([ + expect.objectContaining({ + id: euWarehouse.id, + sales_channels: [ + expect.objectContaining({ id: scWebshop.id }), + expect.objectContaining({ id: scCphStore.id }), + ], + name: "EU Warehouse", + }), + ]) + + expect(usLocations.length).toBe(1) + expect(usLocations).toEqual([ + expect.objectContaining({ + id: usWarehouse.id, + sales_channels: [expect.objectContaining({ id: scNycStore.id })], + name: "US Warehouse", + }), + ]) + }) + }) + }, +}) diff --git a/integration-tests/modules/medusa-config.js b/integration-tests/modules/medusa-config.js index 0aa1798f22..73341ed119 100644 --- a/integration-tests/modules/medusa-config.js +++ b/integration-tests/modules/medusa-config.js @@ -49,8 +49,14 @@ module.exports = { resolve: "@medusajs/cache-inmemory", options: { ttl: 0 }, // Cache disabled }, - [Modules.STOCK_LOCATION]: true, - [Modules.INVENTORY]: true, + [Modules.STOCK_LOCATION]: { + resolve: "@medusajs/stock-location-next", + options: {}, + }, + [Modules.INVENTORY]: { + resolve: "@medusajs/inventory-next", + options: {}, + }, [Modules.PRODUCT]: true, [Modules.PRICING]: true, [Modules.PROMOTION]: true, diff --git a/integration-tests/modules/package.json b/integration-tests/modules/package.json index 71f9a588f8..8304f68507 100644 --- a/integration-tests/modules/package.json +++ b/integration-tests/modules/package.json @@ -14,13 +14,14 @@ "@medusajs/cache-inmemory": "workspace:*", "@medusajs/customer": "workspace:^", "@medusajs/event-bus-local": "workspace:*", - "@medusajs/inventory": "workspace:^", + "@medusajs/inventory-next": "workspace:^", "@medusajs/medusa": "workspace:*", "@medusajs/modules-sdk": "workspace:^", "@medusajs/pricing": "workspace:^", "@medusajs/product": "workspace:^", "@medusajs/promotion": "workspace:^", "@medusajs/region": "workspace:^", + "@medusajs/stock-location-next": "workspace:^", "@medusajs/store": "workspace:^", "@medusajs/tax": "workspace:^", "@medusajs/user": "workspace:^", diff --git a/packages/link-modules/src/definitions/index.ts b/packages/link-modules/src/definitions/index.ts index af88028497..cd4feac8bd 100644 --- a/packages/link-modules/src/definitions/index.ts +++ b/packages/link-modules/src/definitions/index.ts @@ -11,4 +11,5 @@ export * from "./product-variant-inventory-item" export * from "./product-variant-price-set" export * from "./publishable-api-key-sales-channel" export * from "./region-payment-provider" +export * from "./sales-channel-location" diff --git a/packages/link-modules/src/definitions/sales-channel-location.ts b/packages/link-modules/src/definitions/sales-channel-location.ts new file mode 100644 index 0000000000..89d1dc094d --- /dev/null +++ b/packages/link-modules/src/definitions/sales-channel-location.ts @@ -0,0 +1,63 @@ +import { Modules } from "@medusajs/modules-sdk" +import { ModuleJoinerConfig } from "@medusajs/types" +import { LINKS } from "../links" + +export const SalesChannelLocation: ModuleJoinerConfig = { + serviceName: LINKS.SalesChannelLocation, + isLink: true, + databaseConfig: { + tableName: "sales_channel_locations", + idPrefix: "scloc", + }, + alias: [ + { + name: ["sales_channel_location", "sales_channel_locations"], + args: { + entity: "LinkSalesChannelLocation", + }, + }, + ], + primaryKeys: ["id", "sales_channel_id", "location_id"], + relationships: [ + { + serviceName: Modules.SALES_CHANNEL, + primaryKey: "id", + foreignKey: "sales_channel_id", + alias: "sales_channel", + }, + { + serviceName: Modules.STOCK_LOCATION, + primaryKey: "id", + foreignKey: "location_id", + alias: "location", + }, + ], + extends: [ + { + serviceName: Modules.SALES_CHANNEL, + fieldAlias: { + locations: "locations_link.location", + }, + relationship: { + serviceName: LINKS.SalesChannelLocation, + primaryKey: "sales_channel_id", + foreignKey: "id", + alias: "locations_link", + isList: true, + }, + }, + { + serviceName: Modules.STOCK_LOCATION, + fieldAlias: { + sales_channels: "sales_channels_link.sales_channel", + }, + relationship: { + serviceName: LINKS.SalesChannelLocation, + primaryKey: "location_id", + foreignKey: "id", + alias: "sales_channels_link", + isList: true, + }, + }, + ], +} diff --git a/packages/link-modules/src/links.ts b/packages/link-modules/src/links.ts index 04a57d9f2f..e8699e3db8 100644 --- a/packages/link-modules/src/links.ts +++ b/packages/link-modules/src/links.ts @@ -32,6 +32,12 @@ export const LINKS = { Modules.PROMOTION, "promotion_id" ), + SalesChannelLocation: composeLinkName( + Modules.SALES_CHANNEL, + "sales_channel_id", + Modules.STOCK_LOCATION, + "location_id" + ), // Internal services ProductShippingProfile: composeLinkName( diff --git a/yarn.lock b/yarn.lock index 0a345a92d3..5ee725b72b 100644 --- a/yarn.lock +++ b/yarn.lock @@ -8311,7 +8311,7 @@ __metadata: languageName: unknown linkType: soft -"@medusajs/inventory-next@workspace:packages/inventory-next": +"@medusajs/inventory-next@workspace:^, @medusajs/inventory-next@workspace:packages/inventory-next": version: 0.0.0-use.local resolution: "@medusajs/inventory-next@workspace:packages/inventory-next" dependencies: @@ -8843,7 +8843,7 @@ __metadata: languageName: unknown linkType: soft -"@medusajs/stock-location-next@workspace:packages/stock-location-next": +"@medusajs/stock-location-next@workspace:^, @medusajs/stock-location-next@workspace:packages/stock-location-next": version: 0.0.0-use.local resolution: "@medusajs/stock-location-next@workspace:packages/stock-location-next" dependencies: @@ -31857,13 +31857,14 @@ __metadata: "@medusajs/cache-inmemory": "workspace:*" "@medusajs/customer": "workspace:^" "@medusajs/event-bus-local": "workspace:*" - "@medusajs/inventory": "workspace:^" + "@medusajs/inventory-next": "workspace:^" "@medusajs/medusa": "workspace:*" "@medusajs/modules-sdk": "workspace:^" "@medusajs/pricing": "workspace:^" "@medusajs/product": "workspace:^" "@medusajs/promotion": "workspace:^" "@medusajs/region": "workspace:^" + "@medusajs/stock-location-next": "workspace:^" "@medusajs/store": "workspace:^" "@medusajs/tax": "workspace:^" "@medusajs/types": "workspace:^"