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:
@@ -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,
|
||||
|
||||
Reference in New Issue
Block a user