From 3be6084dfd35d18505e4a1047414a40547c351e4 Mon Sep 17 00:00:00 2001 From: Philip Korsholm <88927411+pKorsholm@users.noreply.github.com> Date: Wed, 15 Mar 2023 13:00:18 +0100 Subject: [PATCH] filter possible locations by inventory id (#3476) **What** - Ensure that an inventory level exists for a sales channel (which it will since this is invoked after `confirmInventory` that will validate that stock exists in the sales channel) Fixes CORE-1225 --- .../src/services/product-variant-inventory.ts | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/packages/medusa/src/services/product-variant-inventory.ts b/packages/medusa/src/services/product-variant-inventory.ts index 44e3438f4b..4d3f997dde 100644 --- a/packages/medusa/src/services/product-variant-inventory.ts +++ b/packages/medusa/src/services/product-variant-inventory.ts @@ -362,18 +362,31 @@ class ProductVariantInventoryService extends TransactionBaseService { let locationId = context.locationId if (!isDefined(locationId) && context.salesChannelId) { - const locations = await this.salesChannelLocationService_ + const locationIds = await this.salesChannelLocationService_ .withTransaction(this.activeManager_) .listLocationIds(context.salesChannelId) - if (!locations.length) { + if (!locationIds.length) { throw new MedusaError( MedusaError.Types.INVALID_DATA, "Must provide location_id or sales_channel_id to a Sales Channel that has associated Stock Locations" ) } - locationId = locations[0] + const [locations, count] = + await this.inventoryService_.listInventoryLevels({ + location_id: locationIds, + inventory_item_id: variantInventory[0].inventory_item_id, + }) + + if (count === 0) { + throw new MedusaError( + MedusaError.Types.INVALID_DATA, + "Must provide location_id or sales_channel_id to a Sales Channel that has associated locations with inventory levels" + ) + } + + locationId = locations[0].location_id } return await Promise.all(