From fc321e96ceb142d080e5d1eceabd2126ff3a999e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Frane=20Poli=C4=87?= <16856471+fPolic@users.noreply.github.com> Date: Fri, 20 Dec 2024 09:25:19 +0100 Subject: [PATCH] fix(core-flows): select stock locations for reservation from correct SC (#10661) * fix: add stock location for reservations only if related to correct SC * fix: update spec * fix: wrong SC id get in OE flow * fix: ensure test case has multiple SC and SLs --- .../cart/store/cart.workflows.spec.ts | 1 + .../__tests__/cart/store/carts.spec.ts | 48 +++++++++++++++++++ .../utils/prepare-confirm-inventory-input.ts | 2 +- .../src/cart/workflows/complete-cart.ts | 2 +- .../order-edit/confirm-order-edit-request.ts | 2 +- 5 files changed, 52 insertions(+), 3 deletions(-) diff --git a/integration-tests/modules/__tests__/cart/store/cart.workflows.spec.ts b/integration-tests/modules/__tests__/cart/store/cart.workflows.spec.ts index 9500c804f73af..10c888c40efd5 100644 --- a/integration-tests/modules/__tests__/cart/store/cart.workflows.spec.ts +++ b/integration-tests/modules/__tests__/cart/store/cart.workflows.spec.ts @@ -2116,6 +2116,7 @@ medusaIntegrationTestRunner({ ]) let cart = await cartModuleService.createCarts({ + sales_channel_id: salesChannel.id, currency_code: "usd", items: [ { diff --git a/integration-tests/modules/__tests__/cart/store/carts.spec.ts b/integration-tests/modules/__tests__/cart/store/carts.spec.ts index b268a1ff80d47..a5b127746d5ca 100644 --- a/integration-tests/modules/__tests__/cart/store/carts.spec.ts +++ b/integration-tests/modules/__tests__/cart/store/carts.spec.ts @@ -1117,6 +1117,14 @@ medusaIntegrationTestRunner({ ) ).data.sales_channel + const salesChannel2 = ( + await api.post( + "/admin/sales-channels", + { name: "second channel", description: "channel" }, + adminHeaders + ) + ).data.sales_channel + stockLocation = ( await api.post( `/admin/stock-locations`, @@ -1125,6 +1133,14 @@ medusaIntegrationTestRunner({ ) ).data.stock_location + const stockLocation2 = ( + await api.post( + `/admin/stock-locations`, + { name: "test location 2" }, + adminHeaders + ) + ).data.stock_location + inventoryItem = ( await api.post( `/admin/inventory-items`, @@ -1144,12 +1160,27 @@ medusaIntegrationTestRunner({ adminHeaders ) + await api.post( + `/admin/inventory-items/${inventoryItem.id}/location-levels`, + { + location_id: stockLocation2.id, + stocked_quantity: 10, + }, + adminHeaders + ) + await api.post( `/admin/stock-locations/${stockLocation.id}/sales-channels`, { add: [salesChannel.id] }, adminHeaders ) + await api.post( + `/admin/stock-locations/${stockLocation2.id}/sales-channels`, + { add: [salesChannel2.id] }, + adminHeaders + ) + shippingProfile = await fulfillmentModule.createShippingProfiles({ name: "Test", type: "default", @@ -1210,6 +1241,23 @@ medusaIntegrationTestRunner({ fulfillment_set_id: fulfillmentSet.id, }, }, + { + [Modules.PRODUCT]: { + product_id: product.id, + }, + [Modules.SALES_CHANNEL]: { + sales_channel_id: salesChannel2.id, + }, + }, + // Add product to 2 sales channels to test that the right stock location is selected for reservations + { + [Modules.PRODUCT]: { + product_id: product.id, + }, + [Modules.SALES_CHANNEL]: { + sales_channel_id: salesChannel.id, + }, + }, ]) promotion = ( diff --git a/packages/core/core-flows/src/cart/utils/prepare-confirm-inventory-input.ts b/packages/core/core-flows/src/cart/utils/prepare-confirm-inventory-input.ts index 1cf02b21f56ea..23405a1223c6b 100644 --- a/packages/core/core-flows/src/cart/utils/prepare-confirm-inventory-input.ts +++ b/packages/core/core-flows/src/cart/utils/prepare-confirm-inventory-input.ts @@ -67,7 +67,7 @@ export const prepareConfirmInventoryInput = (data: { hasSalesChannelStockLocation = true } - if (stock_locations) { + if (stock_locations && sales_channels?.id === salesChannelId) { stockLocationIds.add(stock_locations.id) } diff --git a/packages/core/core-flows/src/cart/workflows/complete-cart.ts b/packages/core/core-flows/src/cart/workflows/complete-cart.ts index 0ae1264d86674..01096ed328364 100644 --- a/packages/core/core-flows/src/cart/workflows/complete-cart.ts +++ b/packages/core/core-flows/src/cart/workflows/complete-cart.ts @@ -5,7 +5,7 @@ import { import { Modules, OrderStatus, - OrderWorkflowEvents + OrderWorkflowEvents, } from "@medusajs/framework/utils" import { createWorkflow, diff --git a/packages/core/core-flows/src/order/workflows/order-edit/confirm-order-edit-request.ts b/packages/core/core-flows/src/order/workflows/order-edit/confirm-order-edit-request.ts index 0a6db89022ff4..20ac78cc7eb08 100644 --- a/packages/core/core-flows/src/order/workflows/order-edit/confirm-order-edit-request.ts +++ b/packages/core/core-flows/src/order/workflows/order-edit/confirm-order-edit-request.ts @@ -215,7 +215,7 @@ export const confirmOrderEditRequestWorkflow = createWorkflow( const formatedInventoryItems = transform( { input: { - sales_channel_id: (orderItems as any).order.sales_channel_id, + sales_channel_id: (orderItems as any).sales_channel_id, variants, items, },