front-hub/src/api/cart.ts

86 lines
2.2 KiB
TypeScript
Raw Normal View History

2023-11-05 23:33:40 +00:00
import { UserAccount, makeRequest } from "@frontend/kitui"
import { AxiosError } from "axios"
2023-06-30 15:35:31 +00:00
2023-11-05 23:33:40 +00:00
import { parseAxiosError } from "@root/utils/parse-error"
2023-06-30 15:35:31 +00:00
2023-08-30 09:56:14 +00:00
const apiUrl =
process.env.NODE_ENV === "production"
2023-11-05 23:33:40 +00:00
? "/customer"
: "https://hub.pena.digital/customer"
2023-06-30 15:35:31 +00:00
2023-08-30 09:56:14 +00:00
export async function patchCart(
2023-11-05 23:33:40 +00:00
tariffId: string
2023-08-30 09:56:14 +00:00
): Promise<[string[], string?]> {
2023-11-05 23:33:40 +00:00
try {
const patchCartResponse = await makeRequest<never, UserAccount>({
url: apiUrl + `/cart?id=${tariffId}`,
method: "PATCH",
useToken: true,
})
2023-08-30 09:56:14 +00:00
2023-11-05 23:33:40 +00:00
return [patchCartResponse.cart]
} catch (nativeError) {
let [error, status] = parseAxiosError(nativeError)
if (status === 400 && error.indexOf("invalid id") !== -1) error = "Данный тариф более недоступен"
2023-08-30 09:56:14 +00:00
2023-11-05 23:33:40 +00:00
return [[], `Не удалось добавить товар в корзину. ${error}`]
}
2023-06-30 15:35:31 +00:00
}
2023-08-30 09:56:14 +00:00
export async function deleteCart(
2023-11-05 23:33:40 +00:00
tariffId: string
2023-08-30 09:56:14 +00:00
): Promise<[string[], string?]> {
2023-11-05 23:33:40 +00:00
try {
const deleteCartResponse = await makeRequest<never, UserAccount>({
url: apiUrl + `/cart?id=${tariffId}`,
method: "DELETE",
useToken: true,
})
2023-08-30 09:56:14 +00:00
2023-11-05 23:33:40 +00:00
return [deleteCartResponse.cart]
} catch (nativeError) {
const [error] = parseAxiosError(nativeError)
2023-08-30 09:56:14 +00:00
2023-11-05 23:33:40 +00:00
return [[], `Не удалось удалить товар из корзины. ${error}`]
}
2023-07-07 13:53:08 +00:00
}
2023-08-30 09:56:14 +00:00
export async function payCart(): Promise<[UserAccount | null, string?]> {
2023-11-05 23:33:40 +00:00
try {
const payCartResponse = await makeRequest<never, UserAccount>({
url: apiUrl + "/cart/pay",
method: "POST",
useToken: true,
})
2023-08-30 09:56:14 +00:00
2023-11-05 23:33:40 +00:00
return [payCartResponse]
} catch (nativeError) {
const [error] = parseAxiosError(nativeError)
2023-08-30 09:56:14 +00:00
2023-11-05 23:33:40 +00:00
return [null, `Не удалось оплатить товар из корзины. ${error}`]
}
2023-07-07 13:53:08 +00:00
}
2023-08-30 09:56:14 +00:00
export async function patchCurrency(
2023-11-05 23:33:40 +00:00
currency: string
2023-08-30 09:56:14 +00:00
): Promise<[UserAccount | null, string?]> {
2023-11-05 23:33:40 +00:00
try {
const patchCurrencyResponse = await makeRequest<
2023-08-30 09:56:14 +00:00
{ currency: string },
UserAccount
>({
2023-11-05 23:33:40 +00:00
url: apiUrl + "/wallet",
method: "PATCH",
useToken: true,
body: {
currency,
},
})
2023-08-30 09:56:14 +00:00
2023-11-05 23:33:40 +00:00
return [patchCurrencyResponse]
} catch (nativeError) {
const [error] = parseAxiosError(nativeError)
2023-08-30 09:56:14 +00:00
2023-11-05 23:33:40 +00:00
return [null, `Не удалось изменить валюту. ${error}`]
}
}