2024-04-22 08:37:17 +00:00
|
|
|
|
import {Tariff} from "@frontend/kitui"
|
|
|
|
|
import {parseAxiosError} from "@root/utils/parse-error"
|
2024-04-16 19:46:07 +00:00
|
|
|
|
import makeRequest from "@api/makeRequest"
|
2023-09-12 21:45:18 +00:00
|
|
|
|
|
|
|
|
|
export interface GetHistoryResponse {
|
2024-01-04 10:20:14 +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-01-04 10:20:14 +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
|
|
|
|
};
|
|
|
|
|
|
2024-03-12 17:07:56 +00:00
|
|
|
|
export interface GetHistoryResponse2 {
|
|
|
|
|
totalPages: number;
|
2024-04-22 08:37:17 +00:00
|
|
|
|
records: HistoryRecord2[];
|
2024-03-12 17:07:56 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export type HistoryRecord2 = {
|
|
|
|
|
comment: string;
|
|
|
|
|
createdAt: string;
|
|
|
|
|
id: string;
|
|
|
|
|
isDeleted: boolean;
|
|
|
|
|
key: string;
|
|
|
|
|
rawDetails: {
|
|
|
|
|
price: number;
|
|
|
|
|
tariffs: Tariff[];
|
|
|
|
|
};
|
|
|
|
|
updatedAt: string;
|
|
|
|
|
userId: string;
|
|
|
|
|
};
|
|
|
|
|
|
2023-09-16 12:24:19 +00:00
|
|
|
|
|
2024-04-22 08:37:17 +00:00
|
|
|
|
export type KeyValue = { Key: string; Value: string | number };
|
|
|
|
|
|
|
|
|
|
export type RawDetails = {
|
|
|
|
|
Key: "tariffs" | "price";
|
|
|
|
|
Value: string | number | KeyValue[][];
|
2024-01-04 10:20:14 +00:00
|
|
|
|
}
|
|
|
|
|
|
2024-03-12 17:07:56 +00:00
|
|
|
|
export async function getHistory(): Promise<[GetHistoryResponse | GetHistoryResponse2 | null, string?]> {
|
2023-11-05 23:33:40 +00:00
|
|
|
|
try {
|
2024-04-22 08:37:17 +00:00
|
|
|
|
const historyResponse = await makeRequest<never, GetHistoryResponse|GetHistoryResponse2>({
|
2024-01-17 20:04:39 +00:00
|
|
|
|
url: process.env.REACT_APP_DOMAIN + "/customer/history?page=1&limit=100&type=payCart",
|
2023-11-05 23:33:40 +00:00
|
|
|
|
method: "get",
|
|
|
|
|
useToken: true,
|
|
|
|
|
})
|
2023-09-12 21:45:18 +00:00
|
|
|
|
|
2024-03-12 17:07:56 +00:00
|
|
|
|
if (!Array.isArray(historyResponse.records[0]?.rawDetails)) {
|
|
|
|
|
return [historyResponse] as [GetHistoryResponse2]
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-04 10:20:14 +00:00
|
|
|
|
const checked = historyResponse.records.map((data) => {
|
2024-04-22 08:37:17 +00:00
|
|
|
|
//const buffer:RawDetails[] = [];
|
|
|
|
|
/*(data.rawDetails as HistoryRecord["rawDetails"]).forEach((slot) => {
|
|
|
|
|
const index = regList[slot.Key]
|
|
|
|
|
buffer[index] = { ...slot }
|
|
|
|
|
})*/
|
|
|
|
|
//Чистим дыры с помощью .filter(() => true)
|
2024-01-04 10:20:14 +00:00
|
|
|
|
//@ts-ignore
|
2024-04-22 08:37:17 +00:00
|
|
|
|
//data.rawDetails = buffer
|
|
|
|
|
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
|
|
|
|
|
]
|
|
|
|
|
return {...data, rawDetails: checkedRowDetails} as HistoryRecord
|
2024-01-04 10:20:14 +00:00
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
historyResponse.records = checked || []
|
2023-11-05 23:33:40 +00:00
|
|
|
|
return [historyResponse]
|
|
|
|
|
} catch (nativeError) {
|
|
|
|
|
const [error] = parseAxiosError(nativeError)
|
2023-09-12 21:45:18 +00:00
|
|
|
|
|
2023-11-05 23:33:40 +00:00
|
|
|
|
return [null, `Не удалось получить историю. ${error}`]
|
|
|
|
|
}
|
2023-09-12 21:45:18 +00:00
|
|
|
|
}
|