// Per-user conversation state for menu-driven flows. // Currently tracks: "operator clicked Pair New, waiting for them to type the label". // In-memory only — fine for a single-instance bot. If we ever scale horizontally, // move this to Postgres. const PENDING_TTL_MS = 5 * 60 * 1000; // 5 minutes const pendingPairLabel = new Map(); // userId → expires_at_ms export function setPendingPairLabel(userId: number): void { pendingPairLabel.set(userId, Date.now() + PENDING_TTL_MS); } export function clearPendingPairLabel(userId: number): void { pendingPairLabel.delete(userId); } export function consumePendingPairLabel(userId: number): boolean { const expiresAt = pendingPairLabel.get(userId); if (!expiresAt) return false; pendingPairLabel.delete(userId); return Date.now() < expiresAt; } // "Send a test message to this WhatsApp group" pending state. type PendingSend = { groupId: string; expiresAt: number }; const pendingSendToGroup = new Map(); export function setPendingSendToGroup(userId: number, groupId: string): void { pendingSendToGroup.set(userId, { groupId, expiresAt: Date.now() + PENDING_TTL_MS }); } export function clearPendingSendToGroup(userId: number): void { pendingSendToGroup.delete(userId); } export function consumePendingSendToGroup(userId: number): string | null { const pending = pendingSendToGroup.get(userId); if (!pending) return null; pendingSendToGroup.delete(userId); if (Date.now() >= pending.expiresAt) return null; return pending.groupId; }