fix(medusa): List products end points (#3829)
* fix(medusa): List products end points * lint * Create fast-toes-know.md
This commit is contained in:
@@ -0,0 +1,5 @@
|
|||||||
|
---
|
||||||
|
"@medusajs/medusa": patch
|
||||||
|
---
|
||||||
|
|
||||||
|
fix(medusa): List products end points
|
||||||
@@ -231,32 +231,42 @@ export default async (req, res) => {
|
|||||||
|
|
||||||
const { skip, take, relations } = req.listConfig
|
const { skip, take, relations } = req.listConfig
|
||||||
|
|
||||||
const [rawProducts, count] = await productService.listAndCount(
|
const manager = req.scope.resolve("manager")
|
||||||
req.filterableFields,
|
|
||||||
req.listConfig
|
const [products, count] = await manager.transaction(
|
||||||
|
async (transactionManager) => {
|
||||||
|
const [rawProducts, count] = await productService
|
||||||
|
.withTransaction(transactionManager)
|
||||||
|
.listAndCount(req.filterableFields, req.listConfig)
|
||||||
|
|
||||||
|
let products: (Product | PricedProduct)[] = rawProducts
|
||||||
|
|
||||||
|
const includesPricing = ["variants", "variants.prices"].every(
|
||||||
|
(relation) => relations?.includes(relation)
|
||||||
|
)
|
||||||
|
if (includesPricing) {
|
||||||
|
products = await pricingService
|
||||||
|
.withTransaction(transactionManager)
|
||||||
|
.setProductPrices(rawProducts)
|
||||||
|
}
|
||||||
|
|
||||||
|
if (inventoryService) {
|
||||||
|
const [salesChannelsIds] = await salesChannelService
|
||||||
|
.withTransaction(transactionManager)
|
||||||
|
.listAndCount({}, { select: ["id"] })
|
||||||
|
|
||||||
|
products = await productVariantInventoryService
|
||||||
|
.withTransaction(transactionManager)
|
||||||
|
.setProductAvailability(
|
||||||
|
products,
|
||||||
|
salesChannelsIds.map((salesChannel) => salesChannel.id)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
return [products, count]
|
||||||
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
let products: (Product | PricedProduct)[] = rawProducts
|
|
||||||
|
|
||||||
const includesPricing = ["variants", "variants.prices"].every((relation) =>
|
|
||||||
relations?.includes(relation)
|
|
||||||
)
|
|
||||||
if (includesPricing) {
|
|
||||||
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({
|
res.json({
|
||||||
products,
|
products,
|
||||||
count,
|
count,
|
||||||
|
|||||||
@@ -20,7 +20,6 @@ import { FeatureFlagDecorators } from "../../../../utils/feature-flag-decorators
|
|||||||
import { optionalBooleanMapper } from "../../../../utils/validators/is-boolean"
|
import { optionalBooleanMapper } from "../../../../utils/validators/is-boolean"
|
||||||
import { IsType } from "../../../../utils/validators/is-type"
|
import { IsType } from "../../../../utils/validators/is-type"
|
||||||
import { cleanResponseData } from "../../../../utils/clean-response-data"
|
import { cleanResponseData } from "../../../../utils/clean-response-data"
|
||||||
import { Cart, Product } from "../../../../models"
|
|
||||||
import { defaultStoreCategoryScope } from "../product-categories"
|
import { defaultStoreCategoryScope } from "../product-categories"
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -204,7 +203,7 @@ export default async (req, res) => {
|
|||||||
filterableFields["categories"] = {
|
filterableFields["categories"] = {
|
||||||
...(filterableFields.categories || {}),
|
...(filterableFields.categories || {}),
|
||||||
// Store APIs are only allowed to query active and public categories
|
// Store APIs are only allowed to query active and public categories
|
||||||
...defaultStoreCategoryScope
|
...defaultStoreCategoryScope,
|
||||||
}
|
}
|
||||||
|
|
||||||
if (req.publishableApiKeyScopes?.sales_channel_ids.length) {
|
if (req.publishableApiKeyScopes?.sales_channel_ids.length) {
|
||||||
@@ -217,47 +216,62 @@ export default async (req, res) => {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const promises: Promise<any>[] = []
|
const manager = req.scope.resolve("manager")
|
||||||
|
|
||||||
promises.push(productService.listAndCount(filterableFields, listConfig))
|
const [computedProducts, count] = await manager.transaction(
|
||||||
|
async (transactionManager) => {
|
||||||
|
const promises: Promise<any>[] = []
|
||||||
|
|
||||||
if (validated.cart_id) {
|
promises.push(
|
||||||
promises.push(
|
productService
|
||||||
cartService.retrieve(validated.cart_id, {
|
.withTransaction(transactionManager)
|
||||||
select: ["id", "region_id"] as any,
|
.listAndCount(filterableFields, listConfig)
|
||||||
relations: ["region"],
|
)
|
||||||
})
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
const [[rawProducts, count], cart] = (await Promise.all(promises)) as [
|
if (validated.cart_id) {
|
||||||
[Product[], number],
|
promises.push(
|
||||||
Cart
|
cartService
|
||||||
]
|
.withTransaction(transactionManager)
|
||||||
|
.retrieve(validated.cart_id, {
|
||||||
|
select: ["id", "region_id"] as any,
|
||||||
|
relations: ["region"],
|
||||||
|
})
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
if (validated.cart_id) {
|
const [[rawProducts, count], cart] = await Promise.all(promises)
|
||||||
regionId = cart.region_id
|
|
||||||
currencyCode = cart.region.currency_code
|
|
||||||
}
|
|
||||||
|
|
||||||
// Create a new reference just for naming purpose
|
if (validated.cart_id) {
|
||||||
const computedProducts = rawProducts
|
regionId = cart.region_id
|
||||||
|
currencyCode = cart.region.currency_code
|
||||||
|
}
|
||||||
|
|
||||||
// We can run them concurrently as the new properties are assigned to the references
|
// Create a new reference just for naming purpose
|
||||||
// of the appropriate entity
|
const computedProducts = rawProducts
|
||||||
await Promise.all([
|
|
||||||
pricingService.setProductPrices(computedProducts, {
|
// We can run them concurrently as the new properties are assigned to the references
|
||||||
cart_id: cart_id,
|
// of the appropriate entity
|
||||||
region_id: regionId,
|
await Promise.all([
|
||||||
currency_code: currencyCode,
|
pricingService
|
||||||
customer_id: req.user?.customer_id,
|
.withTransaction(transactionManager)
|
||||||
include_discount_prices: true,
|
.setProductPrices(computedProducts, {
|
||||||
}),
|
cart_id: cart_id,
|
||||||
productVariantInventoryService.setProductAvailability(
|
region_id: regionId,
|
||||||
computedProducts,
|
currency_code: currencyCode,
|
||||||
filterableFields.sales_channel_id
|
customer_id: req.user?.customer_id,
|
||||||
),
|
include_discount_prices: true,
|
||||||
])
|
}),
|
||||||
|
productVariantInventoryService
|
||||||
|
.withTransaction(transactionManager)
|
||||||
|
.setProductAvailability(
|
||||||
|
computedProducts,
|
||||||
|
filterableFields.sales_channel_id
|
||||||
|
),
|
||||||
|
])
|
||||||
|
|
||||||
|
return [computedProducts, count]
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
res.json({
|
res.json({
|
||||||
products: cleanResponseData(computedProducts, req.allowedProperties || []),
|
products: cleanResponseData(computedProducts, req.allowedProperties || []),
|
||||||
|
|||||||
Reference in New Issue
Block a user