add factor to find discount return values
This commit is contained in:
parent
197b9c247a
commit
7151dc25aa
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@frontend/kitui",
|
"name": "@frontend/kitui",
|
||||||
"version": "1.0.10",
|
"version": "1.0.11",
|
||||||
"description": "test",
|
"description": "test",
|
||||||
"main": "index.js",
|
"main": "index.js",
|
||||||
"types": "index.d.ts",
|
"types": "index.d.ts",
|
||||||
|
@ -6,13 +6,16 @@ export function applyCartDiscount(
|
|||||||
discounts: Discount[],
|
discounts: Discount[],
|
||||||
) {
|
) {
|
||||||
const cartDiscount = findCartDiscount(cartData.priceAfterDiscounts, discounts);
|
const cartDiscount = findCartDiscount(cartData.priceAfterDiscounts, discounts);
|
||||||
if (cartDiscount) cartData.priceAfterDiscounts *= cartDiscount.Target.Factor;
|
if (cartDiscount) cartData.priceAfterDiscounts *= cartDiscount.factor;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function findCartDiscount(
|
export function findCartDiscount(
|
||||||
cartPurchasesAmount: number,
|
cartPurchasesAmount: number,
|
||||||
discounts: Discount[],
|
discounts: Discount[],
|
||||||
): Discount | null {
|
): {
|
||||||
|
discount: Discount | null,
|
||||||
|
factor: number,
|
||||||
|
} {
|
||||||
const applicableDiscounts = discounts.filter(discount => {
|
const applicableDiscounts = discounts.filter(discount => {
|
||||||
return (
|
return (
|
||||||
discount.Layer === 3 &&
|
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) => {
|
const maxValueDiscount = applicableDiscounts.reduce((prev, current) => {
|
||||||
return current.Condition.CartPurchasesAmount > prev.Condition.CartPurchasesAmount ? current : prev;
|
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,
|
cartData: CartData,
|
||||||
discounts: Discount[],
|
discounts: Discount[],
|
||||||
purchasesAmount: number,
|
purchasesAmount: number,
|
||||||
){
|
) {
|
||||||
const loyalDiscount = findLoyaltyDiscount(purchasesAmount, discounts)
|
const loyalDiscount = findLoyaltyDiscount(purchasesAmount, discounts);
|
||||||
if (loyalDiscount) cartData.priceAfterDiscounts *= loyalDiscount.Target.Factor
|
if (loyalDiscount) cartData.priceAfterDiscounts *= loyalDiscount.factor;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function findLoyaltyDiscount(
|
export function findLoyaltyDiscount(
|
||||||
purchasesAmount: number,
|
purchasesAmount: number,
|
||||||
discounts: Discount[],
|
discounts: Discount[],
|
||||||
): Discount | null {
|
): {
|
||||||
|
discount: Discount | null,
|
||||||
|
factor: number,
|
||||||
|
} {
|
||||||
const applicableDiscounts = discounts.filter(discount => {
|
const applicableDiscounts = discounts.filter(discount => {
|
||||||
return (
|
return (
|
||||||
discount.Layer === 4 &&
|
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) => {
|
const maxValueDiscount = applicableDiscounts.reduce((prev, current) => {
|
||||||
return current.Condition.PurchasesAmount > prev.Condition.PurchasesAmount ? current : prev;
|
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 => {
|
cartData.services.forEach(service => {
|
||||||
service.privileges.forEach(privilege => {
|
service.privileges.forEach(privilege => {
|
||||||
const privilegeDiscount = findPrivilegeDiscount(privilege.privilegeId, privilege.price, discounts);
|
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,
|
privilegeId: string,
|
||||||
privilegePrice: number,
|
privilegePrice: number,
|
||||||
discounts: Discount[],
|
discounts: Discount[],
|
||||||
): Discount | null {
|
): {
|
||||||
|
discount: Discount | null,
|
||||||
|
factor: number,
|
||||||
|
} {
|
||||||
const applicableDiscounts = discounts.filter(discount => {
|
const applicableDiscounts = discounts.filter(discount => {
|
||||||
const conditionMinPrice = parseFloat(discount.Condition.Term);
|
const conditionMinPrice = parseFloat(discount.Condition.Term);
|
||||||
if (!isFinite(conditionMinPrice)) throw new Error(`Couldn't parse Discount.Condition.Term: ${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) =>
|
const maxValueDiscount = applicableDiscounts.reduce((prev, current) =>
|
||||||
parseFloat(current.Condition.Term) > parseFloat(prev.Condition.Term) ? current : prev
|
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 => {
|
cartData.services.forEach(service => {
|
||||||
service.privileges.forEach(privilege => {
|
service.privileges.forEach(privilege => {
|
||||||
const privilegeDiscount = findServiceDiscount(privilege.serviceKey, privilege.price, discounts);
|
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,
|
serviceKey: string,
|
||||||
currentPrice: number,
|
currentPrice: number,
|
||||||
discounts: Discount[],
|
discounts: Discount[],
|
||||||
): Discount | null {
|
): {
|
||||||
|
discount: Discount | null,
|
||||||
|
factor: number,
|
||||||
|
} {
|
||||||
const applicableDiscounts = discounts.filter(discount => {
|
const applicableDiscounts = discounts.filter(discount => {
|
||||||
return (
|
return (
|
||||||
discount.Layer === 2 &&
|
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) => {
|
const maxValueDiscount = applicableDiscounts.reduce((prev, current) => {
|
||||||
return current.Condition.PriceFrom > prev.Condition.PriceFrom ? current : prev;
|
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