import { CustomTariffUserValues } from "../../model/customTariffs"; import { Discount } from "../../model/discount"; import { CustomPrivilegeWithAmount } from "../../model/privilege"; import { Tariff } from "../../model/tariff"; import { calcCart } from "./calcCart"; export function calcCustomTariffPrice( customTariffUserValues: CustomTariffUserValues, servicePrivileges: CustomPrivilegeWithAmount[], cartTariffs: Tariff[], discounts: Discount[], purchasesAmount: number, isUserNko: boolean, userId: string, ) { const privileges = new Array(); const priceBeforeDiscounts = servicePrivileges.reduce((price, privilege) => { const amount = customTariffUserValues?.[privilege._id] ?? 0; return price + privilege.price * amount; }, 0); Object.keys(customTariffUserValues).forEach(privilegeId => { const pwa = servicePrivileges.find(p => p._id === privilegeId); if (!pwa) return; if (customTariffUserValues[privilegeId] > 0) privileges.push({ ...pwa, amount: customTariffUserValues[privilegeId] }); }); const customTariff: Tariff = { _id: crypto.randomUUID(), name: "", price: 0, description: "", isCustom: true, isDeleted: false, privileges: privileges, }; const cart = calcCart([...cartTariffs, customTariff], discounts, purchasesAmount, userId, isUserNko); const customService = cart.services.flatMap(service => service.tariffs).find(tariff => tariff.id === customTariff._id); if (!customService) throw new Error("Custom service not found in cart"); return { priceBeforeDiscounts, priceAfterDiscounts: customService.price, }; }