diff --git a/src/api/account.ts b/src/api/account.ts index e9f4ba3..3548446 100644 --- a/src/api/account.ts +++ b/src/api/account.ts @@ -30,12 +30,12 @@ export type Account = { wallet: Wallet; }; -const baseUrl = process.env.REACT_APP_DOMAIN + "/customer"; +const API_URL = process.env.REACT_APP_DOMAIN + "/customer"; export const getAccountInfo = async (id: string): Promise<[Account | null, string?]> => { try { const accountInfoResponse = await makeRequest({ - url: `${baseUrl}/account/${id}`, + url: `${API_URL}/account/${id}`, method: "GET", useToken: true, }); @@ -47,3 +47,20 @@ export const getAccountInfo = async (id: string): Promise<[Account | null, strin return [null, `Не удалось получить информацию об аккаунте. ${error}`]; } }; + +export const editAccount = async (userId: string, status: "org" | "nko"): Promise<[unknown | null, string?]> => { + try { + const editResponse = await makeRequest<{ status: "org" | "nko" }, unknown>({ + method: "patch", + useToken: true, + url: `${API_URL}/account/${userId}`, + body: { status }, + }); + + return [editResponse]; + } catch (nativeError) { + const [error] = parseAxiosError(nativeError); + + return [null, `Не удалось отредактировать информацию. ${error}`]; + } +}; diff --git a/src/api/quiz.ts b/src/api/quiz.ts new file mode 100644 index 0000000..b4064a3 --- /dev/null +++ b/src/api/quiz.ts @@ -0,0 +1,26 @@ +import makeRequest from "@root/api/makeRequest"; + +import { parseAxiosError } from "@root/utils/parse-error"; + +const API_URL = `${process.env.REACT_APP_DOMAIN}/squiz/quiz`; + +type MoveQuizBody = { + Qid: string; + AccountID: string; +}; + +export const moveQuiz = async (quizId: string, userId: string): Promise<[unknown | null, string?]> => { + try { + const moveResponse = await makeRequest({ + method: "post", + url: `${API_URL}/move`, + body: { Qid: quizId, AccountID: userId }, + }); + + return [moveResponse]; + } catch (nativeError) { + const [error] = parseAxiosError(nativeError); + + return [null, `Ошибка перемещения квиза. ${error}`]; + } +}; diff --git a/src/api/roles.ts b/src/api/roles.ts index d0470f5..a99c242 100644 --- a/src/api/roles.ts +++ b/src/api/roles.ts @@ -47,7 +47,7 @@ export const getRoles_mock = (): Promise => { export const deleteRole = async (id: string): Promise<[unknown, string?]> => { try { - const deleteRoleResponse = await makeRequest({ + const deleteRoleResponse = await makeRequest({ url: `${baseUrl}/${id}`, method: "delete", }); diff --git a/src/api/tickets.ts b/src/api/tickets.ts index d0befdc..d7ca441 100644 --- a/src/api/tickets.ts +++ b/src/api/tickets.ts @@ -4,12 +4,12 @@ import { parseAxiosError } from "@root/utils/parse-error"; import type { SendTicketMessageRequest } from "@root/model/ticket"; -const baseUrl = process.env.REACT_APP_DOMAIN + "/heruvym"; +const API_URL = process.env.REACT_APP_DOMAIN + "/heruvym"; export const sendTicketMessage = async (body: SendTicketMessageRequest): Promise<[unknown, string?]> => { try { const sendTicketMessageResponse = await makeRequest({ - url: `${baseUrl}/send`, + url: `${API_URL}/send`, method: "POST", useToken: true, body, @@ -22,3 +22,40 @@ export const sendTicketMessage = async (body: SendTicketMessageRequest): Promise return [null, `Ошибка отправки сообщения. ${error}`]; } }; + +export const closeTicket = async (ticketId: string): Promise<[unknown, string?]> => { + try { + const ticketCloseResponse = await makeRequest({ + url: `${API_URL}/close`, + method: "post", + useToken: true, + body: { ticket: ticketId }, + }); + + return [ticketCloseResponse]; + } catch (nativeError) { + const [error] = parseAxiosError(nativeError); + + return [null, `Не удалось закрыть тикет. ${error}`]; + } +}; + +export const sendFile = async (file: File, ticketId: string): Promise<[unknown | null, string?]> => { + try { + const body = new FormData(); + body.append(file.name, file); + body.append("ticket", ticketId); + + const sendFileRespose = await makeRequest({ + method: "POST", + url: `${API_URL}/sendFiles`, + body, + }); + + return [sendFileRespose]; + } catch (nativeError) { + const [error] = parseAxiosError(nativeError); + + return [null, `Не удалось отправить файл. ${error}`]; + } +}; diff --git a/src/pages/dashboard/Content/Support/Chat/Chat.tsx b/src/pages/dashboard/Content/Support/Chat/Chat.tsx index 0585ca9..9f124d5 100644 --- a/src/pages/dashboard/Content/Support/Chat/Chat.tsx +++ b/src/pages/dashboard/Content/Support/Chat/Chat.tsx @@ -30,6 +30,7 @@ import ChatDocument from "./ChatDocument"; import ChatVideo from "./ChatVideo"; import ChatMessage from "./ChatMessage"; import { ACCEPT_SEND_MEDIA_TYPES_MAP, MAX_FILE_SIZE, MAX_PHOTO_SIZE, MAX_VIDEO_SIZE } from "./fileUpload"; +import { sendFile as sendFileRequest } from "@root/api/tickets"; const tooLarge = "Файл слишком большой"; const checkAcceptableMediaType = (file: File) => { @@ -164,24 +165,18 @@ export default function Chat() { const sendFile = async (file: File) => { if (file === undefined) return true; - let data; - const ticketId = ticket?.id; if (ticketId !== undefined) { - try { - const body = new FormData(); + const [_, sendFileError] = await sendFileRequest(file, ticketId); - body.append(file.name, file); - body.append("ticket", ticketId); - await makeRequest({ - url: process.env.REACT_APP_DOMAIN + "/heruvym/sendFiles", - body: body, - method: "POST", - }); - } catch (error: any) { - const errorMessage = getMessageFromFetchError(error); - if (errorMessage) enqueueSnackbar(errorMessage); + if (sendFileError) { + const errorMessage = getMessageFromFetchError(sendFileError); + + if (errorMessage) { + enqueueSnackbar(errorMessage); + } } + return true; } }; diff --git a/src/pages/dashboard/Content/Support/TicketList/CloseTicketModal.tsx b/src/pages/dashboard/Content/Support/TicketList/CloseTicketModal.tsx index daafdcc..f258885 100644 --- a/src/pages/dashboard/Content/Support/TicketList/CloseTicketModal.tsx +++ b/src/pages/dashboard/Content/Support/TicketList/CloseTicketModal.tsx @@ -5,6 +5,7 @@ import Typography from "@mui/material/Typography"; import Button from "@mui/material/Button"; import makeRequest from "@root/api/makeRequest"; import { parseAxiosError } from "@root/utils/parse-error"; +import { closeTicket } from "@root/api/tickets"; interface Props { ticketId: string | undefined; openModal: boolean; @@ -12,22 +13,10 @@ interface Props { } export default function CloseTicketModal({ ticketId, openModal, setOpenModal }: Props) { - const CloseTicket = async () => { - try { - const ticketCloseResponse = await makeRequest({ - url: process.env.REACT_APP_DOMAIN + "/heruvym/close", - method: "post", - useToken: true, - body: { - ticket: ticketId, - }, - }); - - return [ticketCloseResponse]; - } catch (nativeError) { - const [error] = parseAxiosError(nativeError); - - return [null, `Не удалось закрыть тикет. ${error}`]; + const onCloseTicket = async () => { + if (ticketId) { + await closeTicket(ticketId); + setOpenModal(false); } }; @@ -64,13 +53,7 @@ export default function CloseTicketModal({ ticketId, openModal, setOpenModal }: alignItems: "center", }} > - diff --git a/src/pages/dashboard/ModalUser/VerificationTab.tsx b/src/pages/dashboard/ModalUser/VerificationTab.tsx index 1779153..d8b6b55 100644 --- a/src/pages/dashboard/ModalUser/VerificationTab.tsx +++ b/src/pages/dashboard/ModalUser/VerificationTab.tsx @@ -6,7 +6,7 @@ import { useDebouncedCallback } from "use-debounce"; import type { ChangeEvent } from "react"; import type { Verification } from "@root/api/verification"; -import makeRequest from "@root/api/makeRequest"; +import { editAccount } from "@root/api/account"; type VerificationTabProps = { userId: string; @@ -66,13 +66,9 @@ export const VerificationTab = ({ userId }: VerificationTabProps) => { status: verificationInfo.status, }); - if (accepted && _ === "OK") - await makeRequest({ - method: "patch", - useToken: true, - url: process.env.REACT_APP_DOMAIN + `/customer/account/${userId}`, - body: { status: verificationInfo.status }, - }); + if (accepted && _ === "OK") { + await editAccount(userId, verificationInfo.status); + } if (patchVerificationError) { return console.error("Error verifying:", patchVerificationError);