feat: multiprivileges in custom tariffs

This commit is contained in:
skeris 2024-01-07 02:24:20 +03:00
parent e2ab652944
commit 68876d3c0a
2 changed files with 18 additions and 9 deletions

@ -110,10 +110,19 @@ export const setCustomTariffsUserValue = (
if (!privilegeWithoutAmount) throw new Error(`Privilege not found: ${privilegeId}`) if (!privilegeWithoutAmount) throw new Error(`Privilege not found: ${privilegeId}`)
let priceWithoutDiscounts = 0 let priceWithoutDiscounts = 0
let priceAfterDiscounts = 0 let priceAfterDiscounts = 0
const privilege: PrivilegeWithAmount = { console.log(JSON.stringify(state.userValuesMap[serviceKey]))
...privilegeWithoutAmount, const privileges = new Array<PrivilegeWithAmount>()
amount: value,
} Object.keys(state.userValuesMap[serviceKey]).forEach(e => {
const pwa = state.privilegeByService[serviceKey].find(p => p._id === e)
if (!pwa) return
if (state.userValuesMap[serviceKey][e] > 0)
privileges.push({
...pwa,
amount: state.userValuesMap[serviceKey][e]
})
})
let tariff: Tariff = { let tariff: Tariff = {
_id: "", _id: "",
name: "", name: "",
@ -121,7 +130,7 @@ export const setCustomTariffsUserValue = (
description: "", description: "",
isCustom: true, isCustom: true,
isDeleted: false, isDeleted: false,
privileges: [privilege], privileges: privileges,
} }
const cart = calcCart([...Object.values(useCartStore.getState().cartTariffMap as Record<string, Tariff>), tariff], discounts, purchasesAmount, isUserNko) const cart = calcCart([...Object.values(useCartStore.getState().cartTariffMap as Record<string, Tariff>), tariff], discounts, purchasesAmount, isUserNko)

@ -60,7 +60,6 @@ function findCartDiscount(
cartPurchasesAmount >= Number(discount.Condition.CartPurchasesAmount) cartPurchasesAmount >= Number(discount.Condition.CartPurchasesAmount)
); );
}); });
console.log('FCD', applicableDiscounts)
if (!applicableDiscounts.length) return null; if (!applicableDiscounts.length) return null;
@ -127,17 +126,18 @@ export function calcCart(tariffs: Tariff[], discounts: Discount[], purchasesAmou
} }
tariffs.forEach(tariff => { tariffs.forEach(tariff => {
console.log(tariff)
if ( if (
tariff.price !== undefined (tariff.price || 0) > 0
&& tariff.privileges.length !== 1 && tariff.privileges.length !== 1
) throw new Error("Price is defined for tariff with several") ) throw new Error("Price is defined for tariff with several")
let serviceData =cartData.services.find(service => (service.serviceKey === "custom" && tariff.isCustom)) let serviceData =cartData.services.find(service => (service.serviceKey === "custom" && tariff.isCustom))
if (!serviceData && !tariff.isCustom) serviceData = cartData.services.find(service => service.serviceKey === tariff.privileges[0].serviceKey) if (!serviceData && !tariff.isCustom) serviceData = cartData.services.find(service => service.serviceKey === tariff.privileges[0]?.serviceKey)
if (!serviceData) { if (!serviceData) {
serviceData = { serviceData = {
serviceKey: tariff.isCustom ?"custom" :tariff.privileges[0].serviceKey, serviceKey: tariff.isCustom ?"custom" :tariff.privileges[0]?.serviceKey,
tariffs: [], tariffs: [],
price: 0, price: 0,
appliedServiceDiscount: null, appliedServiceDiscount: null,