fix(medusa): fetching a product without a category with categories filed passed (#13020)
**What** - adding `categories` to `fields` param would return 404 for an existing product if the product was not associated with a category --- CLOSES CMRC-1046
This commit is contained in:
@@ -2,6 +2,7 @@ import { isPresent, MedusaError } from "@medusajs/framework/utils"
|
||||
import { MedusaResponse } from "@medusajs/framework/http"
|
||||
import { wrapVariantsWithInventoryQuantityForSalesChannel } from "../../../utils/middlewares"
|
||||
import {
|
||||
filterOutInternalProductCategories,
|
||||
refetchProduct,
|
||||
RequestWithContext,
|
||||
wrapProductsWithTaxPrices,
|
||||
@@ -33,6 +34,14 @@ export const GET = async (
|
||||
}
|
||||
}
|
||||
|
||||
const includesCategoriesField = req.queryConfig.fields.some((field) =>
|
||||
field.startsWith("categories")
|
||||
)
|
||||
|
||||
if (!req.queryConfig.fields.includes("categories.is_internal")) {
|
||||
req.queryConfig.fields.push("categories.is_internal")
|
||||
}
|
||||
|
||||
const product = await refetchProduct(
|
||||
filters,
|
||||
req.scope,
|
||||
@@ -53,6 +62,10 @@ export const GET = async (
|
||||
)
|
||||
}
|
||||
|
||||
if (includesCategoriesField) {
|
||||
filterOutInternalProductCategories([product])
|
||||
}
|
||||
|
||||
await wrapProductsWithTaxPrices(req, [product])
|
||||
res.json({ product })
|
||||
}
|
||||
|
||||
@@ -28,6 +28,22 @@ export const refetchProduct = async (
|
||||
return await refetchEntity("product", idOrFilter, scope, fields)
|
||||
}
|
||||
|
||||
export const filterOutInternalProductCategories = (
|
||||
products: HttpTypes.StoreProduct[]
|
||||
) => {
|
||||
return products.forEach((product: HttpTypes.StoreProduct) => {
|
||||
if (!product.categories) {
|
||||
return
|
||||
}
|
||||
|
||||
product.categories = product.categories.filter(
|
||||
(category) =>
|
||||
!(category as HttpTypes.StoreProductCategory & { is_internal: boolean })
|
||||
.is_internal
|
||||
)
|
||||
})
|
||||
}
|
||||
|
||||
export const wrapProductsWithTaxPrices = async <T>(
|
||||
req: RequestWithContext<T>,
|
||||
products: HttpTypes.StoreProduct[]
|
||||
|
||||
@@ -93,13 +93,6 @@ export const storeProductRoutesMiddlewares: MiddlewareRoute[] = [
|
||||
}),
|
||||
applyDefaultFilters({
|
||||
status: ProductStatus.PUBLISHED,
|
||||
categories: (_filters, fields: string[]) => {
|
||||
if (!fields.some((field) => field.startsWith("categories"))) {
|
||||
return
|
||||
}
|
||||
|
||||
return { is_internal: false, is_active: true }
|
||||
},
|
||||
}),
|
||||
normalizeDataForContext(),
|
||||
setPricingContext(),
|
||||
|
||||
Reference in New Issue
Block a user