fix logic
This commit is contained in:
parent
8ccb95d45f
commit
93370f4173
@ -17,10 +17,9 @@ export function calcCartData(
|
|||||||
squiz: 0,
|
squiz: 0,
|
||||||
dwarfener: 0,
|
dwarfener: 0,
|
||||||
},
|
},
|
||||||
|
envolvedCartDiscounts: [],
|
||||||
};
|
};
|
||||||
|
|
||||||
debugger;
|
|
||||||
|
|
||||||
// TODO layer 0
|
// TODO layer 0
|
||||||
for (const discount of discounts) {
|
for (const discount of discounts) {
|
||||||
if (discount.conditionType !== "userType" || discount.condition.userType !== user.Type) continue;
|
if (discount.conditionType !== "userType" || discount.condition.userType !== user.Type) continue;
|
||||||
@ -53,7 +52,7 @@ export function calcCartData(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// layer 1-promo
|
// layer 1-privilege
|
||||||
for (const cartItem of cartItems) {
|
for (const cartItem of cartItems) {
|
||||||
const cartItemTotal: Cart.CartItemTotal = {
|
const cartItemTotal: Cart.CartItemTotal = {
|
||||||
item: cartItem.item,
|
item: cartItem.item,
|
||||||
@ -80,25 +79,38 @@ export function calcCartData(
|
|||||||
// layer 2
|
// layer 2
|
||||||
SERVICE_LIST.forEach(service => {
|
SERVICE_LIST.forEach(service => {
|
||||||
const serviceDiscount = findMaxServiceDiscount(service, discounts, cartTotal);
|
const serviceDiscount = findMaxServiceDiscount(service, discounts, cartTotal);
|
||||||
if (serviceDiscount) cartTotal.priceByService[service] *= serviceDiscount.target.factor;
|
if (serviceDiscount) {
|
||||||
|
cartTotal.priceByService[service] *= serviceDiscount.target.factor;
|
||||||
|
cartTotal.envolvedCartDiscounts.push(serviceDiscount._id);
|
||||||
|
}
|
||||||
|
|
||||||
cartTotal.totalPrice += cartTotal.priceByService[service];
|
cartTotal.totalPrice += cartTotal.priceByService[service];
|
||||||
});
|
});
|
||||||
|
|
||||||
// layer 3
|
// layer 3
|
||||||
const cartPurchasesAmountDiscount = findMaxCartPurchasesAmountDiscount(discounts, cartTotal);
|
const cartPurchasesAmountDiscount = findMaxCartPurchasesAmountDiscount(discounts, cartTotal);
|
||||||
if (cartPurchasesAmountDiscount) cartTotal.totalPrice *= cartPurchasesAmountDiscount.factor;
|
if (cartPurchasesAmountDiscount) {
|
||||||
|
cartTotal.totalPrice *= cartPurchasesAmountDiscount.factor;
|
||||||
|
cartTotal.envolvedCartDiscounts.push(cartPurchasesAmountDiscount._id);
|
||||||
|
}
|
||||||
|
|
||||||
// layer 4
|
// layer 4
|
||||||
const totalPurchasesAmountDiscount = findMaxTotalPurchasesAmountDiscount(discounts, user);
|
const totalPurchasesAmountDiscount = findMaxTotalPurchasesAmountDiscount(discounts, user);
|
||||||
if (totalPurchasesAmountDiscount) cartTotal.totalPrice *= totalPurchasesAmountDiscount.factor;
|
if (totalPurchasesAmountDiscount) {
|
||||||
|
cartTotal.totalPrice *= totalPurchasesAmountDiscount.factor;
|
||||||
|
cartTotal.envolvedCartDiscounts.push(totalPurchasesAmountDiscount._id);
|
||||||
|
}
|
||||||
|
|
||||||
return cartTotal;
|
return cartTotal;
|
||||||
}
|
}
|
||||||
|
|
||||||
function findMaxApplicablePrivilegeDiscount(discounts: Cart.AnyDiscount[], tariff: Tariffs.Tariff): Cart.PrivilegeDiscount | null {
|
function findMaxApplicablePrivilegeDiscount(discounts: Cart.AnyDiscount[], tariff: Tariffs.Tariff): Cart.PrivilegeDiscount | null {
|
||||||
const applicableDiscounts = discounts.filter((discount): discount is Cart.PrivilegeDiscount => {
|
const applicableDiscounts = discounts.filter((discount): discount is Cart.PrivilegeDiscount => {
|
||||||
return discount.conditionType === "privilege" && tariff.amount >= discount.condition.privilege.value;
|
return (
|
||||||
|
discount.conditionType === "privilege" &&
|
||||||
|
tariff.privilege.privilegeId === discount.condition.privilege.id &&
|
||||||
|
tariff.amount >= discount.condition.privilege.value
|
||||||
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
if (!applicableDiscounts.length) return null;
|
if (!applicableDiscounts.length) return null;
|
||||||
@ -112,7 +124,7 @@ function findMaxApplicablePrivilegeDiscount(discounts: Cart.AnyDiscount[], tarif
|
|||||||
|
|
||||||
function findMaxCartPurchasesAmountDiscount(discounts: Cart.AnyDiscount[], cartTotal: Cart.CartTotal): Cart.CartPurchasesAmountDiscount | null {
|
function findMaxCartPurchasesAmountDiscount(discounts: Cart.AnyDiscount[], cartTotal: Cart.CartTotal): Cart.CartPurchasesAmountDiscount | null {
|
||||||
const applicableDiscounts = discounts.filter((discount): discount is Cart.CartPurchasesAmountDiscount => {
|
const applicableDiscounts = discounts.filter((discount): discount is Cart.CartPurchasesAmountDiscount => {
|
||||||
return discount.conditionType === "cartPurchasesAmount" && discount.condition.cartPurchasesAmount >= cartTotal.totalPrice;
|
return discount.conditionType === "cartPurchasesAmount" && cartTotal.totalPrice >= discount.condition.cartPurchasesAmount;
|
||||||
});
|
});
|
||||||
|
|
||||||
if (!applicableDiscounts.length) return null;
|
if (!applicableDiscounts.length) return null;
|
||||||
@ -126,7 +138,7 @@ function findMaxCartPurchasesAmountDiscount(discounts: Cart.AnyDiscount[], cartT
|
|||||||
|
|
||||||
function findMaxTotalPurchasesAmountDiscount(discounts: Cart.AnyDiscount[], user: User): Cart.PurchasesAmountDiscount | null {
|
function findMaxTotalPurchasesAmountDiscount(discounts: Cart.AnyDiscount[], user: User): Cart.PurchasesAmountDiscount | null {
|
||||||
const applicableDiscounts = discounts.filter((discount): discount is Cart.PurchasesAmountDiscount => {
|
const applicableDiscounts = discounts.filter((discount): discount is Cart.PurchasesAmountDiscount => {
|
||||||
return discount.conditionType === "purchasesAmount" && discount.condition.purchasesAmount >= user.PurchasesAmount;
|
return discount.conditionType === "purchasesAmount" && user.PurchasesAmount >= discount.condition.purchasesAmount;
|
||||||
});
|
});
|
||||||
|
|
||||||
if (!applicableDiscounts.length) return null;
|
if (!applicableDiscounts.length) return null;
|
||||||
@ -147,7 +159,7 @@ function findMaxServiceDiscount(
|
|||||||
return (
|
return (
|
||||||
discount.conditionType === "service" &&
|
discount.conditionType === "service" &&
|
||||||
discount.condition.service.id === service &&
|
discount.condition.service.id === service &&
|
||||||
discount.condition.service.value >= cartTotal.priceByService[service]
|
cartTotal.priceByService[service] >= discount.condition.service.value
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user