adminFront/src/api/account.ts

67 lines
1.5 KiB
TypeScript
Raw Normal View History

import makeRequest from "@root/api/makeRequest";
2023-08-31 12:46:34 +00:00
import { parseAxiosError } from "@root/utils/parse-error";
type Name = {
2024-05-21 07:41:31 +00:00
firstname: string;
secondname: string;
middlename: string;
orgname: string;
};
type Wallet = {
2024-05-21 07:41:31 +00:00
currency: string;
cash: number;
purchasesAmount: number;
spent: number;
money: number;
};
export type Account = {
2024-05-21 07:41:31 +00:00
_id: string;
userId: string;
cart: string[];
status: string;
isDeleted: boolean;
createdAt: string;
updatedAt: string;
deletedAt: string;
name: Name;
wallet: Wallet;
};
2024-07-22 19:32:01 +00:00
const API_URL = `${process.env.REACT_APP_DOMAIN}/customer/v1.0.1`;
2023-08-31 12:46:34 +00:00
2024-05-21 07:41:31 +00:00
export const getAccountInfo = async (id: string): Promise<[Account | null, string?]> => {
try {
const accountInfoResponse = await makeRequest<never, Account>({
method: "GET",
2024-05-22 13:48:38 +00:00
url: `${API_URL}/account/${id}`,
2024-05-21 07:41:31 +00:00
useToken: true,
});
2023-08-31 12:46:34 +00:00
2024-05-21 07:41:31 +00:00
return [accountInfoResponse];
} catch (nativeError) {
const [error] = parseAxiosError(nativeError);
2023-08-31 12:46:34 +00:00
2024-05-21 07:41:31 +00:00
return [null, `Не удалось получить информацию об аккаунте. ${error}`];
}
2023-08-31 12:46:34 +00:00
};
2024-05-21 14:10:02 +00:00
2024-05-22 13:48:38 +00:00
export const editAccount = async (userId: string, status: "org" | "nko"): Promise<[Account | null, string?]> => {
2024-05-21 14:10:02 +00:00
try {
2024-05-22 13:48:38 +00:00
const editResponse = await makeRequest<{ status: "org" | "nko" }, Account>({
method: "PATCH",
2024-05-21 14:10:02 +00:00
url: `${API_URL}/account/${userId}`,
body: { status },
2024-05-22 13:48:38 +00:00
useToken: true,
2024-05-21 14:10:02 +00:00
});
return [editResponse];
} catch (nativeError) {
const [error] = parseAxiosError(nativeError);
return [null, `Не удалось отредактировать информацию. ${error}`];
}
};