Part of tasks 5.1/5.2 (not ticked): one catch-all proxy at /api/whatsapp (media served with nosniff and a sandbox CSP), typed client calls, and usePolling that pauses while the tab is hidden. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01EFUzfj5akPxyikzGc2iAHM
114 lines
3.9 KiB
TypeScript
114 lines
3.9 KiB
TypeScript
import { apiGet, apiPost } from "@/app/lib/clientApi";
|
|
|
|
export type WhatsAppTab = "mine" | "unassigned" | "all";
|
|
|
|
export interface WhatsAppConversation {
|
|
uuid: string;
|
|
companyId: number;
|
|
contactWaId: string;
|
|
contactName: string | null;
|
|
leadUuid: string | null;
|
|
leadName: string | null;
|
|
ownerUserId: number | null;
|
|
ownerName: string | null;
|
|
ownerManual: boolean;
|
|
lastInboundAt: string | null;
|
|
lastMessageAt: string | null;
|
|
lastMessagePreview: string | null;
|
|
windowOpen: boolean;
|
|
unreadCount: number;
|
|
}
|
|
|
|
export interface WhatsAppMessage {
|
|
uuid: string;
|
|
direction: "IN" | "OUT";
|
|
type: string;
|
|
body: string | null;
|
|
templateName: string | null;
|
|
mediaMime: string | null;
|
|
mediaFilename: string | null;
|
|
locationLat: number | null;
|
|
locationLng: number | null;
|
|
locationName: string | null;
|
|
status: "pending" | "sent" | "delivered" | "read" | "failed" | null;
|
|
errorCode: string | null;
|
|
errorTitle: string | null;
|
|
sentByUserId: number | null;
|
|
createdAt: string;
|
|
statusAt: string | null;
|
|
}
|
|
|
|
export interface WhatsAppTemplate {
|
|
name: string;
|
|
language: string;
|
|
category: string;
|
|
bodyText: string;
|
|
paramCount: number;
|
|
supported: boolean;
|
|
}
|
|
|
|
export interface WhatsAppPage {
|
|
items: WhatsAppConversation[];
|
|
total: number;
|
|
page: number;
|
|
size: number;
|
|
}
|
|
|
|
export interface WhatsAppThread {
|
|
conversation: WhatsAppConversation;
|
|
messages: WhatsAppMessage[];
|
|
hasOlder: boolean;
|
|
}
|
|
|
|
const q = (params: Record<string, string | number | undefined | null>) => {
|
|
const s = new URLSearchParams();
|
|
for (const [k, v] of Object.entries(params)) if (v !== undefined && v !== null && v !== "") s.set(k, String(v));
|
|
const out = s.toString();
|
|
return out ? `?${out}` : "";
|
|
};
|
|
|
|
const c = (uuid: string) => encodeURIComponent(uuid);
|
|
|
|
export const listConversations = (tab: WhatsAppTab, search: string, page: number, companyId?: number | null) =>
|
|
apiGet<WhatsAppPage>(`/api/whatsapp/conversations${q({ tab, q: search, page, companyId })}`);
|
|
|
|
export const getThread = (uuid: string, before?: string, companyId?: number | null) =>
|
|
apiGet<WhatsAppThread>(`/api/whatsapp/conversations/${c(uuid)}${q({ before, companyId })}`);
|
|
|
|
export const sendText = (uuid: string, text: string, companyId?: number | null) =>
|
|
apiPost<WhatsAppMessage>(`/api/whatsapp/conversations/${c(uuid)}/messages${q({ companyId })}`, { text });
|
|
|
|
export const sendTemplate = (uuid: string, template: WhatsAppTemplate, params: string[], companyId?: number | null) =>
|
|
apiPost<WhatsAppMessage>(`/api/whatsapp/conversations/${c(uuid)}/messages${q({ companyId })}`, {
|
|
template: template.name,
|
|
language: template.language,
|
|
params,
|
|
});
|
|
|
|
export const markRead = (uuid: string, companyId?: number | null) =>
|
|
apiPost<null>(`/api/whatsapp/conversations/${c(uuid)}/read${q({ companyId })}`);
|
|
|
|
export const assignConversation = (uuid: string, userId: number | null, companyId?: number | null) =>
|
|
apiPost<WhatsAppConversation>(
|
|
`/api/whatsapp/conversations/${c(uuid)}/assign${q({ companyId })}`,
|
|
userId === null ? { automatic: true } : { userId },
|
|
);
|
|
|
|
/** Exactly one of leadUuid or eventLeadUuid. */
|
|
export const startChat = (target: { leadUuid: string } | { eventLeadUuid: string }) =>
|
|
apiPost<WhatsAppConversation>("/api/whatsapp/start", target);
|
|
|
|
export const listTemplates = (companyId?: number | null) =>
|
|
apiGet<WhatsAppTemplate[]>(`/api/whatsapp/templates${q({ companyId })}`);
|
|
|
|
export const syncTemplates = (companyId?: number | null) =>
|
|
apiPost<WhatsAppTemplate[]>(`/api/whatsapp/templates/sync${q({ companyId })}`);
|
|
|
|
export const unreadCount = () => apiGet<{ count: number }>("/api/whatsapp/unread-count");
|
|
|
|
export const listAssignees = (companyId?: number | null) =>
|
|
apiGet<{ id: number; name: string | null }[]>(`/api/whatsapp/assignees${q({ companyId })}`);
|
|
|
|
export const mediaUrl = (messageUuid: string, companyId?: number | null) =>
|
|
`/api/whatsapp/media/${c(messageUuid)}${q({ companyId })}`;
|