fix(medusa): Include inventory quantity when listing products (#3586)

* initial inclusion of quantities when listing admin prices

* add changeset

* rename variable

* add inventory service check when listing products

* update changeset version bump
This commit is contained in:
Philip Korsholm
2023-03-28 12:42:26 +02:00
committed by GitHub
parent b6c08cbbfe
commit e359d3f85b
3 changed files with 58 additions and 15 deletions
@@ -1,10 +1,16 @@
import { IsNumber, IsOptional, IsString } from "class-validator"
import { PricingService, ProductService } from "../../../../services"
import {
PricingService,
ProductService,
ProductVariantInventoryService,
SalesChannelService,
} from "../../../../services"
import { Type } from "class-transformer"
import { Product } from "../../../../models"
import { PricedProduct } from "../../../../types/pricing"
import { FilterableProductProps } from "../../../../types/product"
import { PricedProduct } from "../../../../types/pricing"
import { Product } from "../../../../models"
import { Type } from "class-transformer"
import { IInventoryService } from "@medusajs/types"
/**
* @oas [get] /admin/products
@@ -214,6 +220,13 @@ import { FilterableProductProps } from "../../../../types/product"
*/
export default async (req, res) => {
const productService: ProductService = req.scope.resolve("productService")
const inventoryService: IInventoryService | undefined =
req.scope.resolve("inventoryService")
const productVariantInventoryService: ProductVariantInventoryService =
req.scope.resolve("productVariantInventoryService")
const salesChannelService: SalesChannelService = req.scope.resolve(
"salesChannelService"
)
const pricingService: PricingService = req.scope.resolve("pricingService")
const { skip, take, relations } = req.listConfig
@@ -232,6 +245,18 @@ export default async (req, res) => {
products = await pricingService.setProductPrices(rawProducts)
}
if (inventoryService) {
const [salesChannelsIds] = await salesChannelService.listAndCount(
{},
{ select: ["id"] }
)
products = await productVariantInventoryService.setProductAvailability(
products,
salesChannelsIds.map((salesChannel) => salesChannel.id)
)
}
res.json({
products,
count,