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
2023-03-15 13:00:18 +01:00
committed by GitHub
parent 826d4bedfe
commit 3be6084dfd

View File

@@ -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(