adminFront/src/api/tariffs.ts

103 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
2023-09-01 13:17:24 +00:00
import type { PrivilegeWithAmount } 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;
price: number;
isCustom: boolean;
privilegies: Omit<PrivilegeWithAmount, "_id" | "updatedAt">[];
};
type GetTariffsResponse = {
totalPages: number;
tariffs: Tariff[];
};
2023-08-31 14:01:29 +00:00
const baseUrl =
process.env.NODE_ENV === "production"
? "/strator"
: "https://admin.pena.digital/strator";
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,
}
);
debugger;
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,
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
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}`];
}
};