adminFront/src/api/tariffs.ts

102 lines
2.6 KiB
TypeScript
Raw Normal View History

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
import type { Privilege } from "@frontend/kitui";
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-09-01 13:17:24 +00:00
type CreateTariffBackendRequest = {
name: string;
2023-12-15 16:24:47 +00:00
description: string;
order: number;
2023-09-01 13:17:24 +00:00
price: number;
isCustom: boolean;
privileges: Omit<Privilege, "_id" | "updatedAt">[];
2023-09-01 13:17:24 +00:00
};
type GetTariffsResponse = {
totalPages: number;
tariffs: Tariff[];
};
2024-01-23 18:31:02 +00:00
const baseUrl =process.env.REACT_APP_DOMAIN + "/strator"
2023-08-31 14:01:29 +00:00
2023-09-01 13:17:24 +00:00
export const createTariff = async (
body: CreateTariffBackendRequest
): Promise<[unknown, string?]> => {
try {
const createdTariffResponse = await makeRequest<CreateTariffBackendRequest>(
{
url: baseUrl + "/tariff/",
method: "post",
body,
}
);
return [createdTariffResponse];
} catch (nativeError) {
const [error] = parseAxiosError(nativeError);
return [null, `Ошибка создания тарифа. ${error}`];
}
};
2023-08-31 14:01:29 +00:00
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,
order: tariff.order || 1,
description: tariff.description ?? "",
2023-09-16 18:02:01 +00:00
privileges: tariff.privileges,
2023-08-31 14:01:29 +00:00
},
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
2023-09-01 13:17:24 +00:00
export const requestTariffs = async (
page: number
): Promise<[GetTariffsResponse | null, string?]> => {
try {
const tariffsResponse = await makeRequest<never, GetTariffsResponse>({
url: baseUrl + `/tariff/?page=${page}&limit=${100}`,
method: "get",
});
return [tariffsResponse];
} catch (nativeError) {
const [error] = parseAxiosError(nativeError);
return [null, `Ошибка запроса тарифов. ${error}`];
}
};