import { sql } from "drizzle-orm"; import { db } from "../db.js"; import { logger } from "../logger.js"; export type WebEvent = // QR PNG bytes live in `whatsapp_accounts.last_qr_png` so this NOTIFY // payload stays under Postgres' 8000-byte limit. Web fetches the PNG // from /api/qr/[accountId] when it sees this event. | { type: "session.qr"; accountId: string; ts: number } | { type: "session.connected"; accountId: string; phoneNumber: string | null } | { type: "session.disconnected"; accountId: string } | { type: "session.timeout"; accountId: string } | { type: "groups.synced"; accountId: string; count: number } | { type: "reminder.fired"; reminderId: string; runId: string; status: string } | { type: "reminder.failed"; reminderId: string; error: string } // The web action enqueues a send_test via pg_notify and shows // "Sending…" optimistically. This event closes the loop. | { type: "send_test.done"; groupId: string; ok: boolean; error: string | null; }; export async function pgNotifyWeb(event: WebEvent): Promise { const json = JSON.stringify(event); // pg_notify takes a literal channel name as 1st arg. await db.execute(sql`SELECT pg_notify('web.event', ${json})`); logger.debug({ event: event.type }, "ipc: web.event published"); }