94 lines
2.5 KiB
TypeScript
94 lines
2.5 KiB
TypeScript
import makeRequest from "@root/api/makeRequest";
|
|
|
|
import { parseAxiosError } from "@root/utils/parse-error";
|
|
|
|
import type { Privilege } from "@frontend/kitui";
|
|
import type { Tariff } from "@frontend/kitui";
|
|
import type { EditTariffRequestBody } from "@root/model/tariff";
|
|
|
|
type CreateTariffBackendRequest = {
|
|
name: string;
|
|
description: string;
|
|
order: number;
|
|
price: number;
|
|
isCustom: boolean;
|
|
privileges: Omit<Privilege, "_id" | "updatedAt">[];
|
|
};
|
|
|
|
type GetTariffsResponse = {
|
|
totalPages: number;
|
|
tariffs: Tariff[];
|
|
};
|
|
|
|
const API_URL = `${process.env.REACT_APP_DOMAIN}/strator`;
|
|
|
|
export const createTariff = async (body: CreateTariffBackendRequest): Promise<[Tariff | null, string?]> => {
|
|
try {
|
|
const createdTariffResponse = await makeRequest<CreateTariffBackendRequest, Tariff>({
|
|
method: "POST",
|
|
url: `${API_URL}/tariff/`,
|
|
body,
|
|
});
|
|
|
|
return [createdTariffResponse];
|
|
} catch (nativeError) {
|
|
const [error] = parseAxiosError(nativeError);
|
|
|
|
return [null, `Ошибка создания тарифа. ${error}`];
|
|
}
|
|
};
|
|
|
|
export const putTariff = async (tariff: Tariff): Promise<[Tariff | null, string?]> => {
|
|
try {
|
|
const putedTariffResponse = await makeRequest<EditTariffRequestBody, Tariff>({
|
|
method: "PUT",
|
|
url: `${API_URL}/tariff/${tariff._id}`,
|
|
body: {
|
|
name: tariff.name,
|
|
price: tariff.price ?? 0,
|
|
isCustom: false,
|
|
order: tariff.order || 1,
|
|
description: tariff.description ?? "",
|
|
privileges: tariff.privileges,
|
|
},
|
|
});
|
|
|
|
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: `${API_URL}/tariff`,
|
|
body: { id: tariffId },
|
|
});
|
|
|
|
return [deletedTariffResponse];
|
|
} catch (nativeError) {
|
|
const [error] = parseAxiosError(nativeError);
|
|
|
|
return [null, `Ошибка удаления тарифа. ${error}`];
|
|
}
|
|
};
|
|
|
|
export const requestTariffs = async (page: number): Promise<[GetTariffsResponse | null, string?]> => {
|
|
try {
|
|
const tariffsResponse = await makeRequest<never, GetTariffsResponse>({
|
|
method: "GET",
|
|
url: `${API_URL}/tariff/getlist/?page=${page}&limit=${100}`,
|
|
});
|
|
|
|
return [tariffsResponse];
|
|
} catch (nativeError) {
|
|
const [error] = parseAxiosError(nativeError);
|
|
|
|
return [null, `Ошибка запроса тарифов. ${error}`];
|
|
}
|
|
};
|