2023-08-31 14:01:29 +00:00
|
|
|
import { makeRequest } from "@frontend/kitui";
|
2023-08-02 11:36:50 +00:00
|
|
|
|
2023-08-31 14:01:29 +00:00
|
|
|
import { parseAxiosError } from "@root/utils/parse-error";
|
2023-08-02 11:36:50 +00:00
|
|
|
|
2023-08-31 14:01:29 +00:00
|
|
|
import type { Tariff } from "@frontend/kitui";
|
2023-08-31 14:30:48 +00:00
|
|
|
import type { EditTariffRequestBody } from "@root/model/tariff";
|
2023-08-02 11:36:50 +00:00
|
|
|
|
2023-08-31 14:01:29 +00:00
|
|
|
const baseUrl =
|
|
|
|
process.env.NODE_ENV === "production"
|
|
|
|
? "/strator"
|
|
|
|
: "https://admin.pena.digital/strator";
|
|
|
|
|
|
|
|
export const putTariff = async (tariff: Tariff): Promise<[null, string?]> => {
|
|
|
|
try {
|
|
|
|
const putedTariffResponse = await makeRequest<EditTariffRequestBody, null>({
|
|
|
|
method: "put",
|
|
|
|
url: baseUrl + `/tariff/${tariff._id}`,
|
|
|
|
body: {
|
|
|
|
name: tariff.name,
|
|
|
|
price: tariff.price ?? 0,
|
|
|
|
isCustom: false,
|
|
|
|
privilegies: tariff.privilegies,
|
|
|
|
},
|
2023-08-02 11:36:50 +00:00
|
|
|
});
|
|
|
|
|
2023-08-31 14:01:29 +00:00
|
|
|
return [putedTariffResponse];
|
|
|
|
} catch (nativeError) {
|
|
|
|
const [error] = parseAxiosError(nativeError);
|
|
|
|
|
|
|
|
return [null, `Ошибка редактирования тарифа. ${error}`];
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
export const deleteTariff = async (
|
|
|
|
tariffId: string
|
|
|
|
): Promise<[Tariff | null, string?]> => {
|
|
|
|
try {
|
|
|
|
const deletedTariffResponse = await makeRequest<{ id: string }, Tariff>({
|
|
|
|
method: "delete",
|
|
|
|
url: baseUrl + "/tariff",
|
|
|
|
body: { id: tariffId },
|
2023-08-02 11:36:50 +00:00
|
|
|
});
|
2023-08-31 14:01:29 +00:00
|
|
|
|
|
|
|
return [deletedTariffResponse];
|
|
|
|
} catch (nativeError) {
|
|
|
|
const [error] = parseAxiosError(nativeError);
|
|
|
|
|
|
|
|
return [null, `Ошибка удаления тарифа. ${error}`];
|
|
|
|
}
|
|
|
|
};
|
2023-08-02 11:36:50 +00:00
|
|
|
|
|
|
|
export async function deleteManyTariffs(tariffIds: string[]) {
|
2023-08-31 14:01:29 +00:00
|
|
|
const results = await Promise.allSettled(
|
|
|
|
tariffIds.map((tariffId) => deleteTariff(tariffId))
|
|
|
|
);
|
|
|
|
|
|
|
|
let deletedCount = 0;
|
|
|
|
let errorCount = 0;
|
|
|
|
const errors: unknown[] = [];
|
|
|
|
|
|
|
|
results.forEach((result) => {
|
|
|
|
if (result.status === "fulfilled") deletedCount++;
|
|
|
|
else {
|
|
|
|
errorCount++;
|
|
|
|
errors.push(result.reason);
|
|
|
|
}
|
|
|
|
});
|
2023-08-02 11:36:50 +00:00
|
|
|
|
2023-08-31 14:01:29 +00:00
|
|
|
return { deletedCount, errorCount, errors };
|
2023-08-02 11:36:50 +00:00
|
|
|
}
|