From 68876d3c0a80c4458f15092792808507c3472bae Mon Sep 17 00:00:00 2001 From: skeris Date: Sun, 7 Jan 2024 02:24:20 +0300 Subject: [PATCH] feat: multiprivileges in custom tariffs --- src/stores/customTariffs.ts | 19 ++++++++++++++----- src/utils/calcCart/calcCart.ts | 8 ++++---- 2 files changed, 18 insertions(+), 9 deletions(-) diff --git a/src/stores/customTariffs.ts b/src/stores/customTariffs.ts index 9e3a489..066bef6 100644 --- a/src/stores/customTariffs.ts +++ b/src/stores/customTariffs.ts @@ -110,10 +110,19 @@ export const setCustomTariffsUserValue = ( if (!privilegeWithoutAmount) throw new Error(`Privilege not found: ${privilegeId}`) let priceWithoutDiscounts = 0 let priceAfterDiscounts = 0 - const privilege: PrivilegeWithAmount = { - ...privilegeWithoutAmount, - amount: value, - } + console.log(JSON.stringify(state.userValuesMap[serviceKey])) + const privileges = new Array() + + 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 = { _id: "", name: "", @@ -121,7 +130,7 @@ export const setCustomTariffsUserValue = ( description: "", isCustom: true, isDeleted: false, - privileges: [privilege], + privileges: privileges, } const cart = calcCart([...Object.values(useCartStore.getState().cartTariffMap as Record), tariff], discounts, purchasesAmount, isUserNko) diff --git a/src/utils/calcCart/calcCart.ts b/src/utils/calcCart/calcCart.ts index 93600f4..85f676d 100644 --- a/src/utils/calcCart/calcCart.ts +++ b/src/utils/calcCart/calcCart.ts @@ -60,7 +60,6 @@ function findCartDiscount( cartPurchasesAmount >= Number(discount.Condition.CartPurchasesAmount) ); }); - console.log('FCD', applicableDiscounts) if (!applicableDiscounts.length) return null; @@ -127,17 +126,18 @@ export function calcCart(tariffs: Tariff[], discounts: Discount[], purchasesAmou } tariffs.forEach(tariff => { + console.log(tariff) if ( - tariff.price !== undefined + (tariff.price || 0) > 0 && tariff.privileges.length !== 1 ) throw new Error("Price is defined for tariff with several") 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) { serviceData = { - serviceKey: tariff.isCustom ?"custom" :tariff.privileges[0].serviceKey, + serviceKey: tariff.isCustom ?"custom" :tariff.privileges[0]?.serviceKey, tariffs: [], price: 0, appliedServiceDiscount: null,