chore(medusa): Improve store list products (#3252)
* chore(medusa): Improve list products by 46.5 percents * fix handler * todo's * Create tricky-terms-wash.md --------- Co-authored-by: Oliver Windall Juhl <59018053+olivermrbl@users.noreply.github.com>
This commit is contained in:
1 parent
13c200ad2f
commit
46547f29c7
4 files changed
+66
-48
No files matched your search
@@ -11,7 +11,6 @@ import {
|
||||
CartService,
|
||||
ProductService,
|
||||
ProductVariantInventoryService,
|
||||
RegionService,
|
||||
} from "../../../../services"
|
||||
import SalesChannelFeatureFlag from "../../../../loaders/feature-flags/sales-channels"
|
||||
import PricingService from "../../../../services/pricing"
|
||||
@@ -23,6 +22,7 @@ import { IsType } from "../../../../utils/validators/is-type"
|
||||
import { FlagRouter } from "../../../../utils/flag-router"
|
||||
import PublishableAPIKeysFeatureFlag from "../../../../loaders/feature-flags/publishable-api-keys"
|
||||
import { cleanResponseData } from "../../../../utils/clean-response-data"
|
||||
import { Cart, Product } from "../../../../models"
|
||||
|
||||
/**
|
||||
* @oas [get] /products
|
||||
@@ -187,15 +187,16 @@ export default async (req, res) => {
|
||||
req.scope.resolve("productVariantInventoryService")
|
||||
const pricingService: PricingService = req.scope.resolve("pricingService")
|
||||
const cartService: CartService = req.scope.resolve("cartService")
|
||||
const regionService: RegionService = req.scope.resolve("regionService")
|
||||
|
||||
const validated = req.validatedQuery as StoreGetProductsParams
|
||||
|
||||
let {
|
||||
cart_id,
|
||||
region_id: regionId,
|
||||
currency_code: currencyCode,
|
||||
...filterableFields
|
||||
} = req.filterableFields
|
||||
|
||||
const listConfig = req.listConfig
|
||||
|
||||
// get only published products for store endpoint
|
||||
@@ -212,37 +213,50 @@ export default async (req, res) => {
|
||||
}
|
||||
}
|
||||
|
||||
const [rawProducts, count] = await productService.listAndCount(
|
||||
filterableFields,
|
||||
listConfig
|
||||
)
|
||||
const promises: Promise<any>[] = []
|
||||
|
||||
promises.push(productService.listAndCount(filterableFields, listConfig))
|
||||
|
||||
if (validated.cart_id) {
|
||||
const cart = await cartService.retrieve(validated.cart_id, {
|
||||
select: ["id", "region_id"],
|
||||
})
|
||||
const region = await regionService.retrieve(cart.region_id, {
|
||||
select: ["id", "currency_code"],
|
||||
})
|
||||
regionId = region.id
|
||||
currencyCode = region.currency_code
|
||||
promises.push(
|
||||
cartService.retrieve(validated.cart_id, {
|
||||
select: ["id", "region_id"] as any,
|
||||
relations: ["region"],
|
||||
})
|
||||
)
|
||||
}
|
||||
|
||||
const pricedProducts = await pricingService.setProductPrices(rawProducts, {
|
||||
cart_id: cart_id,
|
||||
region_id: regionId,
|
||||
currency_code: currencyCode,
|
||||
customer_id: req.user?.customer_id,
|
||||
include_discount_prices: true,
|
||||
})
|
||||
const [[rawProducts, count], cart] = (await Promise.all(promises)) as [
|
||||
[Product[], number],
|
||||
Cart
|
||||
]
|
||||
|
||||
const products = await productVariantInventoryService.setProductAvailability(
|
||||
pricedProducts,
|
||||
filterableFields.sales_channel_id
|
||||
)
|
||||
if (validated.cart_id) {
|
||||
regionId = cart.region_id
|
||||
currencyCode = cart.region.currency_code
|
||||
}
|
||||
|
||||
// Create a new reference just for naming purpose
|
||||
const computedProducts = rawProducts
|
||||
|
||||
// We can run them concurrently as the new properties are assigned to the references
|
||||
// of the appropriate entity
|
||||
await Promise.all([
|
||||
pricingService.setProductPrices(computedProducts, {
|
||||
cart_id: cart_id,
|
||||
region_id: regionId,
|
||||
currency_code: currencyCode,
|
||||
customer_id: req.user?.customer_id,
|
||||
include_discount_prices: true,
|
||||
}),
|
||||
productVariantInventoryService.setProductAvailability(
|
||||
computedProducts,
|
||||
filterableFields.sales_channel_id
|
||||
),
|
||||
])
|
||||
|
||||
res.json({
|
||||
products: cleanResponseData(products, req.allowedProperties || []),
|
||||
products: cleanResponseData(computedProducts, req.allowedProperties || []),
|
||||
count,
|
||||
offset: validated.offset,
|
||||
limit: validated.limit,
|
||||
|
||||
Reference in new issue
Block a user