Files
co-lite/solvway-admin-portal/app/lib/services/adminEventLeads.ts
T

56 lines
2.7 KiB
TypeScript

import { apiDownload, apiGet, apiPost, apiPut } from "@/app/lib/clientApi";
import type {
EventLeadCapturePayload,
EventLeadEvent,
EventLeadRow,
EventLeadUpdatePayload,
InterestOption,
LeadMatchResult,
SalespersonOption,
} from "@/app/lib/types";
const BASE = "/api/event-leads";
const FILTERS = ["salespersonId", "temperature", "status", "interestId", "leadType"] as const;
export type EventLeadFilters = Partial<Record<(typeof FILTERS)[number], string>>;
export interface EventLeadListParams extends EventLeadFilters {
/** 1-based. */
page: number;
pageSize: number;
companyId?: number;
}
export const listEventLeadEvents = (companyId?: number) =>
apiGet<EventLeadEvent[]>(`${BASE}/events${companyId ? `?companyId=${companyId}` : ""}`);
export const listEventLeadInterests = () => apiGet<InterestOption[]>(`${BASE}/interest-options`);
export const listEventSalespeople = (eventUuid: string) =>
apiGet<SalespersonOption[]>(`${BASE}/${eventUuid}/salesperson-options`);
export async function listEventLeads(eventUuid: string, params: EventLeadListParams): Promise<{ items: EventLeadRow[]; total: number }> {
const qs = new URLSearchParams();
for (const key of FILTERS) if (params[key]) qs.set(key, params[key]);
if (params.companyId) qs.set("companyId", String(params.companyId));
qs.set("page", String(Math.max(0, params.page - 1)));
qs.set("size", String(params.pageSize));
const raw = await apiGet<{ content?: EventLeadRow[]; totalElements?: number } | null>(`${BASE}/${eventUuid}/leads?${qs}`);
const items = Array.isArray(raw?.content) ? raw.content : [];
return { items, total: typeof raw?.totalElements === "number" ? raw.totalElements : items.length };
}
export const matchLead = (eventUuid: string, contact: { mobile: string | null; email: string | null }, companyId?: number) =>
apiPost<LeadMatchResult>(`${BASE}/${eventUuid}/match`, companyId ? { ...contact, companyId } : contact);
export const captureEventLead = (eventUuid: string, payload: EventLeadCapturePayload) =>
apiPost<EventLeadRow>(`${BASE}/${eventUuid}`, payload);
export const getEventLead = (eventUuid: string, eventLeadUuid: string) =>
apiGet<EventLeadRow>(`${BASE}/${eventUuid}/leads/${eventLeadUuid}`);
export const updateEventLead = (eventUuid: string, eventLeadUuid: string, payload: EventLeadUpdatePayload) =>
apiPut<EventLeadRow>(`${BASE}/${eventUuid}/leads/${eventLeadUuid}`, payload);
export function exportEventLeads(eventUuid: string, filters: EventLeadFilters, companyId?: number) {
const qs = new URLSearchParams();
for (const key of FILTERS) if (filters[key]) qs.set(key, filters[key]);
if (companyId) qs.set("companyId", String(companyId));
return apiDownload(`${BASE}/${eventUuid}/export${qs.size ? `?${qs}` : ""}`, "event-leads.xlsx");
}