front-hub/src/api/ticket.ts

97 lines
2.6 KiB
TypeScript
Raw Normal View History

2024-05-27 15:43:38 +00:00
import makeRequest from "@api/makeRequest";
2024-03-11 16:02:37 +00:00
import { parseAxiosError } from "@root/utils/parse-error";
2024-05-29 11:58:54 +00:00
import { createTicket as createTicketRequest } from "@frontend/kitui";
import type { CreateTicketResponse } from "@frontend/kitui";
2023-08-31 10:02:11 +00:00
2024-03-11 16:02:37 +00:00
import { SendTicketMessageRequest } from "@frontend/kitui";
2023-08-31 10:02:11 +00:00
2024-05-27 15:43:38 +00:00
type SendFileResponse = {
message: string;
};
2024-05-29 11:58:54 +00:00
const API_URL = `${process.env.REACT_APP_DOMAIN}/heruvym/v1.0.0`;
2023-08-31 10:02:11 +00:00
2024-05-28 13:38:01 +00:00
export const sendTicketMessage = async (
2024-03-11 16:02:37 +00:00
ticketId: string,
message: string
2024-05-28 13:38:01 +00:00
): Promise<[null, string?]> => {
2024-03-11 16:02:37 +00:00
try {
const sendTicketMessageResponse = await makeRequest<
2023-08-31 10:02:11 +00:00
SendTicketMessageRequest,
null
>({
2024-03-11 16:02:37 +00:00
method: "POST",
2024-05-28 13:38:01 +00:00
url: `${API_URL}/send`,
2024-03-11 16:02:37 +00:00
body: { ticket: ticketId, message: message, lang: "ru", files: [] },
2024-05-28 13:38:01 +00:00
useToken: true,
2024-03-11 16:02:37 +00:00
});
return [sendTicketMessageResponse];
} catch (nativeError) {
const [error] = parseAxiosError(nativeError);
return [null, `Не удалось отправить сообщение. ${error}`];
}
2024-05-28 13:38:01 +00:00
};
2023-08-31 10:02:11 +00:00
2024-05-28 13:38:01 +00:00
export const shownMessage = async (id: string): Promise<[null, string?]> => {
2024-03-11 16:02:37 +00:00
try {
const shownMessageResponse = await makeRequest<{ id: string }, null>({
method: "POST",
2024-05-28 13:38:01 +00:00
url: `${API_URL}/shown`,
2024-03-11 16:02:37 +00:00
body: { id },
2024-05-28 13:38:01 +00:00
useToken: true,
2024-03-11 16:02:37 +00:00
});
return [shownMessageResponse];
} catch (nativeError) {
const [error] = parseAxiosError(nativeError);
return [null, `Не удалось прочесть сообщение. ${error}`];
}
2024-05-28 13:38:01 +00:00
};
2024-05-27 15:43:38 +00:00
export const sendFile = async (
ticketId: string,
file: File
): Promise<[SendFileResponse | null, string?]> => {
try {
const body = new FormData();
body.append(file.name, file);
body.append("ticket", ticketId);
const sendResponse = await makeRequest<FormData, SendFileResponse>({
method: "POST",
2024-05-29 11:58:54 +00:00
url: `${API_URL}/sendFiles`,
2024-05-27 15:43:38 +00:00
body,
});
return [sendResponse];
} catch (nativeError) {
const [error] = parseAxiosError(nativeError);
return [null, `Не удалось отправить файл. ${error}`];
}
};
2024-05-29 11:58:54 +00:00
export const createTicket = async (
ticketNameField: string,
2025-03-17 00:02:08 +00:00
ticketBodyField: string,
isToken?: boolean
2024-05-29 11:58:54 +00:00
): Promise<[CreateTicketResponse | null, string?]> => {
try {
const createTicketResponse = await createTicketRequest({
url: `${API_URL}/create`,
2025-03-06 21:43:51 +00:00
body: { Title: ticketNameField, Message: ticketBodyField, System: false },
2025-03-17 00:02:08 +00:00
useToken: isToken
2024-05-29 11:58:54 +00:00
});
return [createTicketResponse];
} catch (nativeError) {
const [error] = parseAxiosError(nativeError);
return [null, `Не удалось отправить файл. ${error}`];
}
};