fix custom tariff creation
This commit is contained in:
parent
9c7aaaef05
commit
2dfc60f355
@ -39,10 +39,9 @@ export default function CustomTariffCard({ serviceKey, privileges }: Props) {
|
||||
createAndSendTariff(serviceKey)
|
||||
.then((result) => {
|
||||
devlog(result);
|
||||
if (result.length === 0) return enqueueSnackbar("Нет тарифов");
|
||||
enqueueSnackbar(result.length > 1 ? "Тарифы созданы" : "Тариф создан");
|
||||
enqueueSnackbar("Тариф создан");
|
||||
|
||||
updateTariffs(result);
|
||||
updateTariffs([result]);
|
||||
})
|
||||
.catch((error) => {
|
||||
const message = getMessageFromFetchError(
|
||||
|
@ -1,10 +1,10 @@
|
||||
import { createTariff } from "@root/api/tariff";
|
||||
import { CreateTariffBody, CustomTariffUserValuesMap, ServiceKeyToPriceMap } from "@root/model/customTariffs";
|
||||
import { ServiceKeyToPrivilegesMap, PrivilegeWithoutPrice } from "@root/model/privilege";
|
||||
import { CustomTariffUserValuesMap, ServiceKeyToPriceMap } from "@root/model/customTariffs";
|
||||
import { ServiceKeyToPrivilegesMap } from "@root/model/privilege";
|
||||
import { produce } from "immer";
|
||||
import { create } from "zustand";
|
||||
import { devtools, persist } from "zustand/middleware";
|
||||
import { Discount, Tariff, findCartDiscount, findLoyaltyDiscount, findPrivilegeDiscount, findServiceDiscount } from "@frontend/kitui";
|
||||
import { Discount, PrivilegeWithAmount, findCartDiscount, findLoyaltyDiscount, findPrivilegeDiscount, findServiceDiscount } from "@frontend/kitui";
|
||||
|
||||
|
||||
interface CustomTariffsStore {
|
||||
@ -80,37 +80,35 @@ export const setCustomTariffsUserValue = (
|
||||
})
|
||||
);
|
||||
|
||||
export const createAndSendTariff = async (serviceKey: string) => {
|
||||
export const createAndSendTariff = (serviceKey: string) => {
|
||||
const state = useCustomTariffsStore.getState();
|
||||
|
||||
const tariffs: CreateTariffBody[] = [];
|
||||
const privilegies: PrivilegeWithAmount[] = [];
|
||||
|
||||
Object.entries(state.userValuesMap[serviceKey]).forEach(([privilegeId, userValue]) => {
|
||||
if (userValue === 0) return;
|
||||
|
||||
const privilege = state.privilegeByService[serviceKey].find(p => p._id === privilegeId);
|
||||
if (!privilege) throw new Error(`Privilege not found: ${privilegeId}`);
|
||||
const privilegeWithoutAmount = state.privilegeByService[serviceKey].find(p => p._id === privilegeId);
|
||||
if (!privilegeWithoutAmount) throw new Error(`Privilege not found: ${privilegeId}`);
|
||||
|
||||
const p2: PrivilegeWithoutPrice = {
|
||||
...privilege,
|
||||
privilegeId: privilege._id,
|
||||
const privilege: PrivilegeWithAmount = {
|
||||
...privilegeWithoutAmount,
|
||||
privilegeId: privilegeWithoutAmount._id,
|
||||
amount: userValue,
|
||||
};
|
||||
|
||||
tariffs.push({
|
||||
name: privilege.name,
|
||||
isCustom: true,
|
||||
privilegies: [p2],
|
||||
});
|
||||
privilegies.push(privilege);
|
||||
});
|
||||
|
||||
if (tariffs.length === 0) return [];
|
||||
const name = [...privilegies.map(p => p.name), new Date().toISOString()].join(", ");
|
||||
|
||||
const results = await Promise.allSettled(tariffs.map(tariff => createTariff(tariff)));
|
||||
const tariff = {
|
||||
name,
|
||||
isCustom: true,
|
||||
privilegies,
|
||||
};
|
||||
|
||||
const fulfilled = results.filter((p): p is PromiseFulfilledResult<Tariff> => p.status === "fulfilled");
|
||||
|
||||
return fulfilled.map(p => p.value);
|
||||
return createTariff(tariff);
|
||||
};
|
||||
|
||||
export const clearCustomTariffs = () => useCustomTariffsStore.setState({ ...initialState });
|
||||
|
Loading…
Reference in New Issue
Block a user