import { logger } from '../logger'; import { whatsappQueue } from './queue'; export class WhatsAppService { /** * Unified entry point for incoming messages. * Delegates all business logic to the background worker via BullMQ. */ static async handleIncomingMessage( phone: string, text: string, audioUrl?: string, imageUrl?: string, timeTravelDayOverride?: number, organizationId: string = 'default-org-id' ) { const traceId = audioUrl ? `[STT-FLOW-${phone.slice(-4)}]` : imageUrl ? `[IMG-FLOW-${phone.slice(-4)}]` : `[TXT-FLOW-${phone.slice(-4)}]`; logger.info(`${traceId} Enqueueing message for worker: "${text.substring(0, 50)}..." (Org: ${organizationId})`); await whatsappQueue.add('handle-inbound', { phone, text, audioUrl, imageUrl, isTimeTravelMode: timeTravelDayOverride !== undefined, realCurrentDay: timeTravelDayOverride, organizationId }, { priority: 1, attempts: 3, backoff: { type: 'exponential', delay: 2000 } }); } }