adminFront/src/api/account.ts
2024-07-22 22:32:01 +03:00

67 lines
1.5 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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