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
This commit is contained in:
Philip Korsholm authored and GitHub committed 2023-03-15 12:00:18 +00:00
1 parent 826d4bedfe
commit 3be6084dfd
1 file changed
+16 -3
@@ -362,18 +362,31 @@ class ProductVariantInventoryService extends TransactionBaseService {
let locationId = context.locationId let locationId = context.locationId
if (!isDefined(locationId) && context.salesChannelId) { if (!isDefined(locationId) && context.salesChannelId) {
const locations = await this.salesChannelLocationService_ const locationIds = await this.salesChannelLocationService_
.withTransaction(this.activeManager_) .withTransaction(this.activeManager_)
.listLocationIds(context.salesChannelId) .listLocationIds(context.salesChannelId)
if (!locations.length) { if (!locationIds.length) {
throw new MedusaError( throw new MedusaError(
MedusaError.Types.INVALID_DATA, MedusaError.Types.INVALID_DATA,
"Must provide location_id or sales_channel_id to a Sales Channel that has associated Stock Locations" "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( return await Promise.all(