fix(medusa): Account for multiple inventory items in get-inventory (#3094)

* use pvi service method instead of inventory service method to get quantity for sales channel

* save a few invocations

* rename quantity

* omit || 0

* add changeset

* extract method for retrieving variant inventory quantity

* update pr with feedback
This commit is contained in:
Philip Korsholm
2023-02-28 17:09:20 +01:00
committed by GitHub
parent 5eb61fa0ef
commit d61d6c7b7f
3 changed files with 68 additions and 29 deletions
@@ -4,6 +4,7 @@ import {
} from "../../../../types/inventory"
import ProductVariantInventoryService from "../../../../services/product-variant-inventory"
import {
SalesChannelInventoryService,
SalesChannelLocationService,
SalesChannelService,
} from "../../../../services"
@@ -75,6 +76,8 @@ export default async (req, res) => {
const channelLocationService: SalesChannelLocationService = req.scope.resolve(
"salesChannelLocationService"
)
const salesChannelInventoryService: SalesChannelInventoryService =
req.scope.resolve("salesChannelInventoryService")
const channelService: SalesChannelService = req.scope.resolve(
"salesChannelService"
)
@@ -106,11 +109,13 @@ export default async (req, res) => {
})
)
const variantInventoryItems =
await productVariantInventoryService.listByVariant(variant.id)
const inventory =
await productVariantInventoryService.listInventoryItemsByVariant(variant.id)
responseVariant.inventory = await joinLevels(inventory, [], inventoryService)
// TODO: adjust for required quantity
if (inventory.length) {
responseVariant.sales_channel_availability = await Promise.all(
channels.map(async (channel) => {
@@ -122,10 +127,11 @@ export default async (req, res) => {
}
}
const quantity = await inventoryService.retrieveAvailableQuantity(
inventory[0].id,
channel.locations
)
const quantity =
await productVariantInventoryService.getVariantQuantityFromVariantInventoryItems(
variantInventoryItems,
channel.id
)
return {
channel_name: channel.name as string,