import { makeRequest } from "@frontend/kitui"; import { parseAxiosError } from "@utils/parse-error"; export type LeadTargetType = "mail" | "telegram" | "whatsapp" | "webhook"; export interface LeadTargetModel { ID: number; AccountID: string; Type: LeadTargetType; QuizID: number; Target: string; InviteLink?: string; Deleted?: boolean; CreatedAt?: string; } const API_URL = `${process.env.REACT_APP_DOMAIN}/squiz`; export const getLeadTargetsByQuiz = async ( quizId: number, ): Promise<[LeadTargetModel[] | null, string?]> => { try { const items = await makeRequest({ method: "GET", url: `${API_URL}/account/leadtarget/${quizId}`, }); return [items]; } catch (nativeError) { const [error] = parseAxiosError(nativeError); return [null, `Не удалось получить цели лида. ${error}`]; } }; export const createLeadTarget = async ( body: { type: LeadTargetType; quizID: number; target: string; name?: string; }, ): Promise<[LeadTargetModel | true | null, string?]> => { try { const response = await makeRequest({ method: "POST", url: `${API_URL}/account/leadtarget`, body, }); return [response]; } catch (nativeError) { const [error] = parseAxiosError(nativeError); return [null, `Не удалось создать цель лида. ${error}`]; } }; export const updateLeadTarget = async ( body: { id: number; target: string; }, ): Promise<[LeadTargetModel | null, string?]> => { try { const updated = await makeRequest({ method: "PUT", url: `${API_URL}/account/leadtarget`, body, }); return [updated]; } catch (nativeError) { const [error] = parseAxiosError(nativeError); return [null, `Не удалось обновить цель лида. ${error}`]; } }; export const deleteLeadTarget = async ( id: number, ): Promise<[true | null, string?]> => { try { await makeRequest({ method: "DELETE", url: `${API_URL}/account/leadtarget/${id}`, }); return [true]; } catch (nativeError) { const [error] = parseAxiosError(nativeError); return [null, `Не удалось удалить цель лида. ${error}`]; } }; export const leadTargetApi = { getByQuiz: getLeadTargetsByQuiz, create: createLeadTarget, update: updateLeadTarget, delete: deleteLeadTarget, };