Initial commit
This commit is contained in:
26
api/hooks/product/helper.js
Normal file
26
api/hooks/product/helper.js
Normal file
@@ -0,0 +1,26 @@
|
||||
/**
|
||||
* @param {LocalProduct[]} products
|
||||
* @returns {LocalProduct[]}
|
||||
*/
|
||||
function attachRatingObjsToProduct(products) {
|
||||
let productIds = products.map((product) => product.bigCommerceId)
|
||||
/**@type {ProductRating[]} */
|
||||
//@ts-ignore
|
||||
let allRatings = context.db.find("rating", {
|
||||
filter: {
|
||||
status: "approved",
|
||||
bigCommerceProductId: { $in: productIds },
|
||||
},
|
||||
})
|
||||
|
||||
products.forEach((product, i) => {
|
||||
let ratings = allRatings.filter((rating) => rating.bigCommerceProductId === product.bigCommerceId).reverse()
|
||||
products[i].ratings = ratings
|
||||
})
|
||||
|
||||
return products
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
attachRatingObjsToProduct,
|
||||
}
|
||||
Reference in New Issue
Block a user