i have a question 3 days ago and received help, today i have a new question. I have a array promotion conditions based on product quantity, if user order product match with product id and >= quantity in promotion condition, order will discount for user. Promotion data structure is
const promotions = [
{
id: 1,
name: 'Promotion 1',
conditionQuantity: 3, // need product in order >= this quantity to discount order
discount: 10000,
conditionProducts: [
{ promotionId: 1, productId: 1 },
{ promotionId: 1, productId: 2 },
]
},
{
id: 2,
name: 'Promotion 1',
conditionQuantity: 5,
discount: 15000,
conditionProducts: [
{ promotionId: 2, productId: 1 },
{ promotionId: 2, productId: 2 },
{ promotionId: 2, productId: 3 },
]
},
{
id: 3,
name: 'Promotion 2',
conditionQuantity: 3,
discount: 12000,
conditionProducts: [
{ promotionId: 3, productId: 1 },
]
}
]
and order products structure is
const productList = [
{
id: 1, // product id
name: 'Product 1',
quantity: 3, // quantity of product in order
},
{
id: 2,
name: 'Product 2',
quantity: 6,
},
{
id: 3,
name: 'Product 3',
quantity: 7,
}
]
I expected result in my case is
const result = [
id: 1,
name: 'Promotion 1',
totalDiscount: 10000,
},
{
id: 2,
name: 'Promotion 1',
totalDiscount: 30000
},
{
id: 3,
name: 'Promotion 2',
totalDiscount: 12000
},
]
and total discount = 52000 (because productId = 1 is satisfy “Promotion 1” and “Promotion 2” (3 >= 3), productId = 2 and productId = 3 satisfy condition of “Promotion 2” (6, 7 > 5))
i try this code but not correct expected result
// get product condition in promotions
const getPromotionProducts = (promotions) => {
return promotions.reduce((acc, promotion) => {
if (promotion.conditionProducts.length > 0) {
acc = acc.concat(promotion.conditionProducts)
};
return acc;
}, []);
}
const getUserPromotions = (promotions, productList) => {
const promotionProducts = getPromotionProducts(promotions);
const mapPromotionConditionAndProductList = promotionProducts.reduce((acc, promotionProduct) => {
const findProduct = productList.find(product => product.id === promotionProduct.productId);
if (findProduct) acc = acc.concat(promotionProduct);
return acc;
}, []);
let result = [], max = 0;
for (const mappedProduct of mapPromotionConditionAndProductList) {
const findPromotion = promotions.find(promotion => promotion.id === mappedProduct.promotionId);
const findProduct = productList.find(product => product.id === mappedProduct.productId);
if (findPromotion.conditionQuantity <= findProduct.quantity && findPromotion.conditionQuantity >= max) {
if (findPromotion.conditionQuantity > max ) {
max = findPromotion.conditionQuantity;
result = [];
}
result.push({ ...findPromotion, productId: findProduct.id });
}
}
return Object.values(result.reduce((acc, item) => {
const key = `${item.id}-${item.promotionId}`;
if (!acc[key]) { acc[key] = { ...item, totalDiscount: 0 } }
acc[key].totalDiscount += item.discount;
return acc;
}, {}));
};
My code result uncorrect result, missing “Promotion 1” and “Promotion 2” with productId = 1.
[
{
id: 2,
name: 'Promotion 1',
totalDiscount: 30000,
productId: 2
}
]
How can i fix getUserPromotions function to response correct? I am looking forward to receive help from anyone.