import makeRequest from "@api/makeRequest"; import { parseAxiosError } from "@root/utils/parse-error"; import { SendTicketMessageRequest } from "@frontend/kitui"; type SendFileResponse = { message: string; }; const API_URL = `${process.env.REACT_APP_DOMAIN}/heruvym`; export async function sendTicketMessage( ticketId: string, message: string ): Promise<[null, string?]> { try { const sendTicketMessageResponse = await makeRequest< SendTicketMessageRequest, null >({ url: `${API_URL}/send`, method: "POST", useToken: true, body: { ticket: ticketId, message: message, lang: "ru", files: [] }, }); return [sendTicketMessageResponse]; } catch (nativeError) { const [error] = parseAxiosError(nativeError); return [null, `Не удалось отправить сообщение. ${error}`]; } } export async function shownMessage(id: string): Promise<[null, string?]> { try { const shownMessageResponse = await makeRequest<{ id: string }, null>({ url: `${API_URL}/shown`, method: "POST", useToken: true, body: { id }, }); return [shownMessageResponse]; } catch (nativeError) { const [error] = parseAxiosError(nativeError); return [null, `Не удалось прочесть сообщение. ${error}`]; } } 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({ method: "POST", url: `${process.env.REACT_APP_DOMAIN}/sendFiles`, body, }); return [sendResponse]; } catch (nativeError) { const [error] = parseAxiosError(nativeError); return [null, `Не удалось отправить файл. ${error}`]; } };