128 lines
3.9 KiB
TypeScript
128 lines
3.9 KiB
TypeScript
import { Discount } from "../../model/discount";
|
|
|
|
|
|
export function findDiscountFactor(discount: Discount | null | undefined): number {
|
|
if (!discount) return 1;
|
|
|
|
if (discount.Layer === 1) return discount.Target.Products[0].Factor;
|
|
|
|
return discount.Target.Factor;
|
|
}
|
|
|
|
export function findNkoDiscount(discounts: Discount[]): Discount | null {
|
|
const applicableDiscounts = discounts.filter(discount => discount.Condition.UserType === "nko");
|
|
|
|
if (!applicableDiscounts.length) return null;
|
|
|
|
const maxValueDiscount = applicableDiscounts.reduce((prev, current) => {
|
|
return Number(current.Condition.CartPurchasesAmount) > Number(prev.Condition.CartPurchasesAmount) ? current : prev;
|
|
});
|
|
|
|
return maxValueDiscount;
|
|
}
|
|
|
|
export function findPrivilegeDiscount(
|
|
privilegeId: string,
|
|
privilegeAmount: number,
|
|
discounts: Discount[],
|
|
userId: string,
|
|
): Discount | null {
|
|
const applicableDiscounts = discounts.filter(discount => {
|
|
return (
|
|
discount.Layer === 1
|
|
&& privilegeId === discount.Condition.Product
|
|
&& privilegeAmount >= Number(discount.Condition.Term)
|
|
&& (discount.Condition.User === "" || discount.Condition.User === userId)
|
|
);
|
|
});
|
|
|
|
if (!applicableDiscounts.length) return null;
|
|
|
|
let maxValueDiscount: Discount = applicableDiscounts[0];
|
|
for (const discount of applicableDiscounts) {
|
|
if (discount.Condition.User !== "" && discount.Condition.User === userId) {
|
|
maxValueDiscount = discount;
|
|
break;
|
|
}
|
|
|
|
if (Number(discount.Condition.Term) > Number(maxValueDiscount.Condition.Term)) {
|
|
maxValueDiscount = discount;
|
|
}
|
|
}
|
|
|
|
return maxValueDiscount;
|
|
}
|
|
|
|
export function findServiceDiscount(
|
|
serviceKey: string,
|
|
currentPrice: number,
|
|
discounts: Discount[],
|
|
userId: string,
|
|
): Discount | null {
|
|
const applicableDiscounts = discounts.filter(discount => {
|
|
return (
|
|
discount.Layer === 2
|
|
&& serviceKey === discount.Condition.Group
|
|
&& currentPrice >= Number(discount.Condition.PriceFrom)
|
|
&& (discount.Condition.User === "" || discount.Condition.User === userId)
|
|
);
|
|
});
|
|
|
|
if (!applicableDiscounts.length) return null;
|
|
|
|
let maxValueDiscount: Discount = applicableDiscounts[0];
|
|
for (const discount of applicableDiscounts) {
|
|
if (discount.Condition.User !== "" && discount.Condition.User === userId) {
|
|
maxValueDiscount = discount;
|
|
break;
|
|
}
|
|
|
|
if (Number(discount.Condition.PriceFrom) > Number(maxValueDiscount.Condition.PriceFrom)) {
|
|
maxValueDiscount = discount;
|
|
}
|
|
}
|
|
|
|
return maxValueDiscount;
|
|
}
|
|
|
|
export function findCartDiscount(
|
|
cartPurchasesAmount: number,
|
|
discounts: Discount[],
|
|
): Discount | null {
|
|
const applicableDiscounts = discounts.filter(discount => {
|
|
return (
|
|
discount.Layer === 3
|
|
&& cartPurchasesAmount >= Number(discount.Condition.CartPurchasesAmount)
|
|
);
|
|
});
|
|
|
|
if (!applicableDiscounts.length) return null;
|
|
|
|
const maxValueDiscount = applicableDiscounts.reduce((prev, current) => {
|
|
return Number(current.Condition.CartPurchasesAmount) > Number(prev.Condition.CartPurchasesAmount) ? current : prev;
|
|
});
|
|
|
|
return maxValueDiscount;
|
|
}
|
|
|
|
export function findLoyaltyDiscount(
|
|
purchasesAmount: number,
|
|
discounts: Discount[],
|
|
): Discount | null {
|
|
const applicableDiscounts = discounts.filter(discount => {
|
|
return (
|
|
discount.Layer === 4
|
|
&& discount.Condition.UserType !== "nko"
|
|
&& purchasesAmount >= Number(discount.Condition.PurchasesAmount)
|
|
);
|
|
});
|
|
|
|
if (!applicableDiscounts.length) return null;
|
|
|
|
const maxValueDiscount = applicableDiscounts.reduce((prev, current) => {
|
|
return Number(current.Condition.PurchasesAmount) > Number(prev.Condition.PurchasesAmount) ? current : prev;
|
|
});
|
|
|
|
return maxValueDiscount;
|
|
}
|