2023-06-16 20:09:56 +00:00
|
|
|
import { Tariff } from "@root/model/tariff";
|
|
|
|
import { mockDiscounts } from "../__mocks__/discounts";
|
|
|
|
import { PrivilegeWithAmount } from "@root/model/privilege";
|
2023-06-30 15:35:31 +00:00
|
|
|
import { AnyDiscount, PrivilegeDiscount, ServiceDiscount } from "../model/discount";
|
2023-06-16 20:09:56 +00:00
|
|
|
|
|
|
|
|
2023-06-30 15:35:31 +00:00
|
|
|
export function calcTariffPrices(tariff: Tariff, discounts: AnyDiscount[] = mockDiscounts): {
|
2023-06-16 20:09:56 +00:00
|
|
|
price: number | undefined;
|
|
|
|
priceWithDiscounts: number | undefined;
|
|
|
|
} {
|
2023-06-30 15:35:31 +00:00
|
|
|
let price = tariff.price || tariff.privilegies.reduce((sum, privilege) => sum + privilege.amount * privilege.price, 0);
|
2023-06-16 20:09:56 +00:00
|
|
|
|
|
|
|
const priceWithDiscounts = tariff.privilegies.reduce((sum, privilege) => {
|
2023-06-20 10:11:55 +00:00
|
|
|
let privilegePrice = privilege.amount * privilege.price;
|
2023-06-16 20:09:56 +00:00
|
|
|
|
2023-06-30 15:35:31 +00:00
|
|
|
const privilegeDiscount = findPrivilegeDiscount(privilege, discounts);
|
2023-06-16 20:09:56 +00:00
|
|
|
if (privilegeDiscount) privilegePrice *= privilegeDiscount.target.products[0].factor;
|
|
|
|
|
2023-06-30 15:35:31 +00:00
|
|
|
const serviceDiscount = findServiceDiscount(privilege.serviceKey, privilegePrice, discounts);
|
2023-06-16 20:09:56 +00:00
|
|
|
if (serviceDiscount) privilegePrice *= serviceDiscount.target.factor;
|
|
|
|
|
|
|
|
return sum + privilegePrice;
|
|
|
|
}, 0);
|
|
|
|
|
|
|
|
return {
|
|
|
|
price,
|
|
|
|
priceWithDiscounts,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2023-06-30 15:35:31 +00:00
|
|
|
export function findPrivilegeDiscount(privilege: PrivilegeWithAmount, discounts: AnyDiscount[]): PrivilegeDiscount | null {
|
|
|
|
const applicableDiscounts = discounts.filter((discount): discount is PrivilegeDiscount => {
|
2023-06-16 20:09:56 +00:00
|
|
|
return (
|
|
|
|
discount.conditionType === "privilege" &&
|
|
|
|
privilege.privilegeId === discount.condition.privilege.id &&
|
|
|
|
privilege.amount >= discount.condition.privilege.value
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!applicableDiscounts.length) return null;
|
|
|
|
|
|
|
|
const maxValueDiscount = applicableDiscounts.reduce((prev, current) =>
|
|
|
|
current.condition.privilege.value > prev.condition.privilege.value ? current : prev
|
|
|
|
);
|
|
|
|
|
|
|
|
return maxValueDiscount;
|
|
|
|
}
|
|
|
|
|
2023-06-30 15:35:31 +00:00
|
|
|
export function findServiceDiscount(
|
2023-06-16 20:09:56 +00:00
|
|
|
serviceKey: string,
|
|
|
|
currentPrice: number,
|
2023-06-30 15:35:31 +00:00
|
|
|
discounts: AnyDiscount[],
|
2023-06-16 20:09:56 +00:00
|
|
|
): ServiceDiscount | null {
|
2023-06-30 15:35:31 +00:00
|
|
|
const discountsForTariffService = discounts.filter((discount): discount is ServiceDiscount => {
|
2023-06-16 20:09:56 +00:00
|
|
|
return (
|
|
|
|
discount.conditionType === "service" &&
|
|
|
|
discount.condition.service.id === serviceKey &&
|
|
|
|
currentPrice >= discount.condition.service.value
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!discountsForTariffService.length) return null;
|
|
|
|
|
|
|
|
const maxValueDiscount = discountsForTariffService.reduce((prev, current) => {
|
|
|
|
return current.condition.service.value > prev.condition.service.value ? current : prev;
|
|
|
|
});
|
|
|
|
|
|
|
|
return maxValueDiscount;
|
|
|
|
}
|