add factor to find discount return values
This commit is contained in:
parent
197b9c247a
commit
7151dc25aa
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@frontend/kitui",
|
||||
"version": "1.0.10",
|
||||
"version": "1.0.11",
|
||||
"description": "test",
|
||||
"main": "index.js",
|
||||
"types": "index.d.ts",
|
||||
|
@ -6,13 +6,16 @@ export function applyCartDiscount(
|
||||
discounts: Discount[],
|
||||
) {
|
||||
const cartDiscount = findCartDiscount(cartData.priceAfterDiscounts, discounts);
|
||||
if (cartDiscount) cartData.priceAfterDiscounts *= cartDiscount.Target.Factor;
|
||||
if (cartDiscount) cartData.priceAfterDiscounts *= cartDiscount.factor;
|
||||
}
|
||||
|
||||
export function findCartDiscount(
|
||||
cartPurchasesAmount: number,
|
||||
discounts: Discount[],
|
||||
): Discount | null {
|
||||
): {
|
||||
discount: Discount | null,
|
||||
factor: number,
|
||||
} {
|
||||
const applicableDiscounts = discounts.filter(discount => {
|
||||
return (
|
||||
discount.Layer === 3 &&
|
||||
@ -20,11 +23,11 @@ export function findCartDiscount(
|
||||
);
|
||||
});
|
||||
|
||||
if (!applicableDiscounts.length) return null;
|
||||
if (!applicableDiscounts.length) return { discount: null, factor: 1 };
|
||||
|
||||
const maxValueDiscount = applicableDiscounts.reduce((prev, current) => {
|
||||
return current.Condition.CartPurchasesAmount > prev.Condition.CartPurchasesAmount ? current : prev;
|
||||
});
|
||||
|
||||
return maxValueDiscount;
|
||||
return { discount: maxValueDiscount, factor: maxValueDiscount.Target.Factor };
|
||||
}
|
||||
|
@ -5,15 +5,18 @@ export function applyLoyaltyDiscount(
|
||||
cartData: CartData,
|
||||
discounts: Discount[],
|
||||
purchasesAmount: number,
|
||||
){
|
||||
const loyalDiscount = findLoyaltyDiscount(purchasesAmount, discounts)
|
||||
if (loyalDiscount) cartData.priceAfterDiscounts *= loyalDiscount.Target.Factor
|
||||
) {
|
||||
const loyalDiscount = findLoyaltyDiscount(purchasesAmount, discounts);
|
||||
if (loyalDiscount) cartData.priceAfterDiscounts *= loyalDiscount.factor;
|
||||
}
|
||||
|
||||
export function findLoyaltyDiscount(
|
||||
purchasesAmount: number,
|
||||
discounts: Discount[],
|
||||
): Discount | null {
|
||||
): {
|
||||
discount: Discount | null,
|
||||
factor: number,
|
||||
} {
|
||||
const applicableDiscounts = discounts.filter(discount => {
|
||||
return (
|
||||
discount.Layer === 4 &&
|
||||
@ -21,11 +24,11 @@ export function findLoyaltyDiscount(
|
||||
);
|
||||
});
|
||||
|
||||
if (!applicableDiscounts.length) return null;
|
||||
if (!applicableDiscounts.length) return { discount: null, factor: 1 };
|
||||
|
||||
const maxValueDiscount = applicableDiscounts.reduce((prev, current) => {
|
||||
return current.Condition.PurchasesAmount > prev.Condition.PurchasesAmount ? current : prev;
|
||||
});
|
||||
|
||||
return maxValueDiscount;
|
||||
return { discount: maxValueDiscount, factor: maxValueDiscount.Target.Factor };
|
||||
}
|
||||
|
@ -8,7 +8,7 @@ export function applyPrivilegeDiscount(
|
||||
cartData.services.forEach(service => {
|
||||
service.privileges.forEach(privilege => {
|
||||
const privilegeDiscount = findPrivilegeDiscount(privilege.privilegeId, privilege.price, discounts);
|
||||
if (privilegeDiscount) privilege.price *= privilegeDiscount.Target.Products[0].Factor;
|
||||
if (privilegeDiscount) privilege.price *= privilegeDiscount.factor;
|
||||
});
|
||||
});
|
||||
}
|
||||
@ -17,7 +17,10 @@ export function findPrivilegeDiscount(
|
||||
privilegeId: string,
|
||||
privilegePrice: number,
|
||||
discounts: Discount[],
|
||||
): Discount | null {
|
||||
): {
|
||||
discount: Discount | null,
|
||||
factor: number,
|
||||
} {
|
||||
const applicableDiscounts = discounts.filter(discount => {
|
||||
const conditionMinPrice = parseFloat(discount.Condition.Term);
|
||||
if (!isFinite(conditionMinPrice)) throw new Error(`Couldn't parse Discount.Condition.Term: ${discount.Condition.Term}`);
|
||||
@ -29,11 +32,11 @@ export function findPrivilegeDiscount(
|
||||
);
|
||||
});
|
||||
|
||||
if (!applicableDiscounts.length) return null;
|
||||
if (!applicableDiscounts.length) return { discount: null, factor: 1 };
|
||||
|
||||
const maxValueDiscount = applicableDiscounts.reduce((prev, current) =>
|
||||
parseFloat(current.Condition.Term) > parseFloat(prev.Condition.Term) ? current : prev
|
||||
);
|
||||
|
||||
return maxValueDiscount;
|
||||
return { discount: maxValueDiscount, factor: maxValueDiscount.Target.Products[0].Factor };
|
||||
}
|
||||
|
@ -8,7 +8,7 @@ export function applyServiceDiscount(
|
||||
cartData.services.forEach(service => {
|
||||
service.privileges.forEach(privilege => {
|
||||
const privilegeDiscount = findServiceDiscount(privilege.serviceKey, privilege.price, discounts);
|
||||
if (privilegeDiscount) privilege.price *= privilegeDiscount.Target.Factor;
|
||||
if (privilegeDiscount) privilege.price *= privilegeDiscount.factor;
|
||||
});
|
||||
});
|
||||
}
|
||||
@ -17,7 +17,10 @@ export function findServiceDiscount(
|
||||
serviceKey: string,
|
||||
currentPrice: number,
|
||||
discounts: Discount[],
|
||||
): Discount | null {
|
||||
): {
|
||||
discount: Discount | null,
|
||||
factor: number,
|
||||
} {
|
||||
const applicableDiscounts = discounts.filter(discount => {
|
||||
return (
|
||||
discount.Layer === 2 &&
|
||||
@ -26,11 +29,11 @@ export function findServiceDiscount(
|
||||
);
|
||||
});
|
||||
|
||||
if (!applicableDiscounts.length) return null;
|
||||
if (!applicableDiscounts.length) return { discount: null, factor: 1 };
|
||||
|
||||
const maxValueDiscount = applicableDiscounts.reduce((prev, current) => {
|
||||
return current.Condition.PriceFrom > prev.Condition.PriceFrom ? current : prev;
|
||||
});
|
||||
|
||||
return maxValueDiscount;
|
||||
return { discount: maxValueDiscount, factor: maxValueDiscount.Target.Factor };
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user