front-hub/src/api/history.ts

124 lines
3.0 KiB
TypeScript
Raw Normal View History

2024-05-27 15:43:38 +00:00
import { Tariff } from "@frontend/kitui";
import { parseAxiosError } from "@root/utils/parse-error";
import makeRequest from "@api/makeRequest";
2023-09-12 21:45:18 +00:00
export interface GetHistoryResponse {
2024-05-27 15:43:38 +00:00
totalPages: number;
records: HistoryRecord[];
2023-09-12 21:45:18 +00:00
}
2023-11-10 14:51:50 +00:00
export type HistoryRecord = {
2024-05-27 15:43:38 +00:00
comment: string;
createdAt: string;
id: string;
isDeleted: boolean;
key: string;
rawDetails: [RawDetails, KeyValue];
updatedAt: string;
userId: string;
2023-09-15 21:00:02 +00:00
};
export interface GetHistoryResponse2 {
2024-05-27 15:43:38 +00:00
totalPages: number;
records: HistoryRecord2[];
}
export type HistoryRecord2 = {
2024-05-27 15:43:38 +00:00
comment: string;
createdAt: string;
id: string;
isDeleted: boolean;
key: string;
rawDetails: {
price: number;
tariffs: Tariff[];
};
updatedAt: string;
userId: string;
};
2024-04-22 08:37:17 +00:00
export type KeyValue = { Key: string; Value: string | number };
export type RawDetails = {
2024-05-27 15:43:38 +00:00
Key: "tariffs" | "price";
Value: string | number | KeyValue[][];
};
2024-06-07 16:59:44 +00:00
const API_URL = `${process.env.REACT_APP_DOMAIN}/customer/v1.0.0`;
2024-05-28 13:38:01 +00:00
export const getHistory = async (): Promise<
2024-05-27 15:43:38 +00:00
[GetHistoryResponse | GetHistoryResponse2 | null, string?]
2024-05-28 13:38:01 +00:00
> => {
2024-05-27 15:43:38 +00:00
try {
const historyResponse = await makeRequest<
never,
GetHistoryResponse | GetHistoryResponse2
>({
method: "GET",
2024-05-28 13:38:01 +00:00
url: `${API_URL}/history?page=1&limit=100&type=payCart`,
2024-05-27 15:43:38 +00:00
useToken: true,
});
if (!Array.isArray(historyResponse.records[0]?.rawDetails)) {
return [historyResponse] as [GetHistoryResponse2];
}
const checked = historyResponse.records.map((data) => {
const checkedRowDetails = [
(data.rawDetails as HistoryRecord["rawDetails"]).find(
(details) => details.Key === "tariffs"
) as RawDetails,
(data.rawDetails as HistoryRecord["rawDetails"]).find(
(details) => details.Key === "price"
) as KeyValue,
];
2024-05-28 13:38:01 +00:00
2024-05-27 15:43:38 +00:00
return { ...data, rawDetails: checkedRowDetails } as HistoryRecord;
});
historyResponse.records = checked || [];
return [historyResponse];
} catch (nativeError) {
const [error] = parseAxiosError(nativeError);
return [null, `Не удалось получить историю. ${error}`];
}
2024-05-28 13:38:01 +00:00
};
2024-05-27 15:43:38 +00:00
export const sendReport = async (
id: string
2024-05-29 11:58:54 +00:00
): Promise<[void | null, string?]> => {
debugger;
2024-05-27 15:43:38 +00:00
try {
2024-05-29 11:58:54 +00:00
const sendReportResponse = await makeRequest<{ id: string }, void>({
2024-05-27 15:43:38 +00:00
method: "POST",
2024-05-28 13:38:01 +00:00
url: `${API_URL}/sendReport`,
2024-05-27 15:43:38 +00:00
body: { id },
});
return [sendReportResponse];
} catch (nativeError) {
const [error] = parseAxiosError(nativeError);
2024-05-29 11:58:54 +00:00
return [null, `Не удалось отправить отчёт. ${error}`];
2024-05-27 15:43:38 +00:00
}
};
export const sendReportById = async (
tariffId: string
2024-05-29 11:58:54 +00:00
): Promise<[void | null, string?]> => {
debugger;
2024-05-27 15:43:38 +00:00
try {
2024-05-29 11:58:54 +00:00
const sendReportResponse = await makeRequest<never, void>({
2024-05-27 15:43:38 +00:00
method: "POST",
2024-05-28 13:38:01 +00:00
url: `${API_URL}/sendReport/${tariffId}`,
2024-05-27 15:43:38 +00:00
});
return [sendReportResponse];
} catch (nativeError) {
const [error] = parseAxiosError(nativeError);
2024-05-29 11:58:54 +00:00
return [null, `Не удалось отправить отчёт. ${error}`];
2024-05-27 15:43:38 +00:00
}
};