import { Injectable, NotFoundException, ConflictException, BadRequestException, OnModuleDestroy, OnModuleInit, OnApplicationBootstrap, } from '@nestjs/common'; import { InjectRepository, InjectDataSource } from '@nestjs/typeorm'; import { Repository, In, Not, IsNull, DataSource } from 'typeorm'; import { Session, SessionStatus } from './entities/session.entity'; import { Message, MessageDirection, MessageStatus } from '../message/entities/message.entity'; import { CreateSessionDto } from './dto'; import { EngineFactory } from '../../engine/engine.factory'; import { IWhatsAppEngine, EngineStatus, ChatSummary, IncomingMessage, } from '../../engine/interfaces/whatsapp-engine.interface'; import { createLogger } from '../../common/services/logger.service'; import { EventsGateway } from '../events/events.gateway'; import { WebhookService } from '../webhook/webhook.service'; import { HookManager } from '../../core/hooks'; import { ackToMessageStatus, ackStatusTransitionFrom } from '../message/message-status.util'; interface ReconnectState { attempts: number; timer: NodeJS.Timeout | null; maxAttempts: number; baseDelay: number; } @Injectable() export class SessionService implements OnModuleDestroy, OnModuleInit, OnApplicationBootstrap { private readonly logger = createLogger('SessionService'); // In-memory map of active engine instances private engines: Map = new Map(); // Transient, human-readable reason for the most recent terminal engine failure, // keyed by session id. Surfaced on read so the dashboard can explain a FAILED // status; cleared when the session re-initializes or becomes ready. private sessionErrors: Map = new Map(); // Reconnection state per session private reconnectStates: Map = new Map(); constructor( @InjectRepository(Session, 'data') private readonly sessionRepository: Repository, @InjectRepository(Message, 'data') private readonly messageRepository: Repository, @InjectDataSource('data') private readonly dataSource: DataSource, private readonly engineFactory: EngineFactory, private readonly eventsGateway: EventsGateway, private readonly webhookService: WebhookService, private readonly hookManager: HookManager, ) {} /** * On backend startup, reset all active session statuses to disconnected * because the engines are not running yet after restart */ async onModuleInit(): Promise { const activeStatuses = [ SessionStatus.READY, SessionStatus.INITIALIZING, SessionStatus.QR_READY, SessionStatus.AUTHENTICATING, ]; const result = await this.sessionRepository.update( { status: In(activeStatuses) }, { status: SessionStatus.DISCONNECTED }, ); if (result.affected && result.affected > 0) { this.logger.log(`Reset ${result.affected} session(s) to disconnected on startup`, { action: 'startup_reset', affected: result.affected, }); } } async onApplicationBootstrap(): Promise { if (process.env.AUTO_START_SESSIONS !== 'true') return; const sessions = await this.sessionRepository.find({ where: { phone: Not(IsNull()), status: SessionStatus.DISCONNECTED }, }); if (sessions.length === 0) return; this.logger.log(`Auto-starting ${sessions.length} previously authenticated session(s)`, { action: 'auto_start', count: sessions.length, }); for (let i = 0; i < sessions.length; i++) { const session = sessions[i]; try { await this.start(session.id); this.logger.log(`Auto-started session: ${session.name}`, { sessionId: session.id, action: 'auto_start_success', }); } catch (error: unknown) { const errorMessage = error instanceof Error ? error.message : 'Unknown error'; this.logger.error(`Auto-start failed for session: ${session.name}`, errorMessage, { sessionId: session.id, action: 'auto_start_failed', }); } // Throttle between sequential Chromium launches; no need to wait after the last one. if (i < sessions.length - 1) { await this.delay(2000); } } } async onModuleDestroy(): Promise { // Clean up all engines on shutdown for (const [sessionId, engine] of this.engines) { this.logger.log(`Destroying engine for session ${sessionId}`, { sessionId, action: 'shutdown', }); await engine.destroy(); } this.engines.clear(); // Clear all reconnect timers for (const [, state] of this.reconnectStates) { if (state.timer) { clearTimeout(state.timer); } } this.reconnectStates.clear(); } async create(dto: CreateSessionDto): Promise { // Check if session with same name exists const existing = await this.sessionRepository.findOne({ where: { name: dto.name }, }); if (existing) { throw new ConflictException(`Session with name '${dto.name}' already exists`); } const session = this.sessionRepository.create({ name: dto.name, config: dto.config || {}, proxyUrl: dto.proxyUrl || null, proxyType: dto.proxyType || null, status: SessionStatus.CREATED, }); const saved = await this.dataSource.transaction(async manager => { return await manager.save(session); }); this.logger.log(`Session created: ${saved.name}`, { sessionId: saved.id, action: 'create', }); // Execute hook after session created (outside transaction since hooks do external I/O) await this.hookManager.execute('session:created', saved, { sessionId: saved.id, source: 'SessionService', }); return saved; } async findAll(): Promise { const sessions = await this.sessionRepository.find({ order: { createdAt: 'DESC' }, }); return sessions.map(session => this.attachLastError(session)); } async findOne(id: string): Promise { const session = await this.sessionRepository.findOne({ where: { id } }); if (!session) { throw new NotFoundException(`Session with id '${id}' not found`); } return this.attachLastError(session); } /** * Populate the transient `lastError` field from the in-memory error map. Only a * FAILED session carries an error; any other status clears it so a recovered * session never shows a stale failure reason. */ private attachLastError(session: Session): Session { session.lastError = session.status === SessionStatus.FAILED ? this.sessionErrors.get(session.id) : undefined; return session; } async findByName(name: string): Promise { const session = await this.sessionRepository.findOne({ where: { name } }); if (!session) { throw new NotFoundException(`Session with name '${name}' not found`); } return session; } async delete(id: string): Promise { const session = await this.findOne(id); // Cancel any reconnection attempts this.cancelReconnect(id); // Stop engine if running const engine = this.engines.get(id); if (engine) { await engine.destroy(); this.engines.delete(id); } // Execute hook BEFORE delete so plugins can access session data await this.hookManager.execute( 'session:deleted', { id: session.id, name: session.name, phone: session.phone, pushName: session.pushName, }, { sessionId: id, source: 'SessionService', }, ); await this.dataSource.transaction(async manager => { await manager.remove(session); }); this.logger.log(`Session deleted: ${session.name}`, { sessionId: id, action: 'delete', }); } async start(id: string): Promise { const session = await this.findOne(id); if (this.engines.has(id)) { // Allow restart when the engine is in a terminal FAILED state. if (session.status === SessionStatus.FAILED) { const stale = this.engines.get(id); await stale?.destroy().catch(() => undefined); this.engines.delete(id); } else { throw new BadRequestException('Session is already started'); } } // Execute hook before starting await this.hookManager.execute( 'session:starting', { sessionId: id }, { sessionId: id, source: 'SessionService', }, ); // Initialize reconnect state const config = session.config as { maxReconnectAttempts?: number; reconnectBaseDelay?: number; } | null; this.reconnectStates.set(id, { attempts: 0, timer: null, maxAttempts: config?.maxReconnectAttempts ?? 5, baseDelay: config?.reconnectBaseDelay ?? 5000, }); await this.initializeEngine(id, session); return this.findOne(id); } private async initializeEngine(id: string, session: Session): Promise { this.logger.log(`Initializing engine for session: ${session.name}`, { sessionId: id, action: 'engine_init', proxyEnabled: !!session.proxyUrl, }); const engine = this.engineFactory.create({ sessionId: session.name, proxyUrl: session.proxyUrl || undefined, proxyType: session.proxyType || undefined, }); this.engines.set(id, engine); // Clear any prior failure reason before a fresh start. this.sessionErrors.delete(id); // Mark INITIALIZING before engine.initialize(): the engine drives status forward // (QR_READY -> AUTHENTICATING -> READY) through the callbacks below while it // initializes, so writing INITIALIZING afterwards would clobber that progress. await this.updateStatus(id, SessionStatus.INITIALIZING); try { await engine.initialize({ onQRCode: (): void => { this.logger.log('QR code generated', { sessionId: id, action: 'qr_generated', }); // Execute hook for QR event void this.hookManager.execute( 'session:qr', { sessionId: id }, { sessionId: id, source: 'Engine', }, ); void this.updateStatus(id, SessionStatus.QR_READY); }, onReady: (phone: string, pushName: string): void => { this.logger.log(`Session ready: ${phone}`, { sessionId: id, phone, pushName, action: 'ready', }); // Execute hook for ready event void this.hookManager.execute( 'session:ready', { phone, pushName }, { sessionId: id, source: 'Engine', }, ); // Reset reconnect attempts and clear any stale failure reason on success const reconnectState = this.reconnectStates.get(id); if (reconnectState) { reconnectState.attempts = 0; } this.sessionErrors.delete(id); void this.sessionRepository.update(id, { status: SessionStatus.READY, phone, pushName, connectedAt: new Date(), lastActiveAt: new Date(), }); }, onMessage: (message): void => { this.logger.debug(`Message received from ${message.from}`, { sessionId: id, messageId: message.id, from: message.from, action: 'message_received', }); // Update last active timestamp void this.sessionRepository.update(id, { lastActiveAt: new Date() }); // Convert IncomingMessage to plain object for dispatch const messageData = { ...message }; // Execute hook for message received - plugins can modify or stop processing void this.hookManager .execute('message:received', messageData, { sessionId: id, source: 'Engine', }) .then(({ continue: shouldContinue, data: finalMessage }) => { if (!shouldContinue) { // Plugin stopped processing (e.g., auto-reply handled it) return; } // Persist the incoming message so the dashboard chats view can render history. const incoming: IncomingMessage = finalMessage; const metadata: Record = {}; if (incoming.media) { metadata.media = incoming.media; } if (incoming.quotedMessage) { metadata.quotedMessage = incoming.quotedMessage; } const dbMessage = this.messageRepository.create({ sessionId: id, waMessageId: incoming.id, chatId: incoming.chatId, from: incoming.from, to: incoming.to, body: incoming.body, type: incoming.type, direction: incoming.fromMe ? MessageDirection.OUTGOING : MessageDirection.INCOMING, timestamp: incoming.timestamp, status: MessageStatus.SENT, metadata: Object.keys(metadata).length > 0 ? metadata : undefined, }); void this.messageRepository.save(dbMessage).catch(err => { this.logger.error(`Failed to save incoming message ${incoming.id} to database`, String(err)); }); // Dispatch to webhooks with potentially modified message void this.webhookService.dispatch(id, 'message.received', finalMessage); // Emit real-time event to WebSocket clients this.eventsGateway.emitMessage(id, finalMessage); }); }, onMessageCreate: (message): void => { // `message_create` fires for every message the account creates, including sends composed on a // linked phone — which the `message`/`onMessage` event never delivers. Incoming messages are // already handled by `onMessage`, so only outgoing (`fromMe`) ones produce `message.sent` here. if (!message.fromMe) { return; } // Status/Story posts (`status@broadcast`) are account-created but not real conversations; // don't emit `message.sent` for them. if (message.to === 'status@broadcast' || message.chatId === 'status@broadcast') { return; } this.logger.debug(`Message sent to ${message.to}`, { sessionId: id, messageId: message.id, to: message.to, action: 'message_sent', }); // Update last active timestamp void this.sessionRepository.update(id, { lastActiveAt: new Date() }); const messageData = { ...message }; // Execute hook for message sent - plugins can modify or stop processing void this.hookManager .execute('message:sent', messageData, { sessionId: id, source: 'Engine', }) .then(({ continue: shouldContinue, data: finalMessage }) => { if (!shouldContinue) { return; } // Dispatch to webhooks with potentially modified message void this.webhookService.dispatch(id, 'message.sent', finalMessage); // Emit real-time event to WebSocket clients (as message.sent, not message.received) this.eventsGateway.emitMessageSent(id, finalMessage); }); }, onMessageAck: (messageId, ack): void => { this.logger.debug(`Message ack: ${messageId} -> ${ack}`, { sessionId: id, messageId, ack, action: 'message_ack', }); // Reflect real delivery state on the stored message (#220): ack=2 -> delivered, >=3 -> read, // <0 -> failed. A send that never reaches ack>=2 stays SENT — visibly "not delivered". // The UPDATE is guarded to the allowed prior statuses so delivery state only ADVANCES: an // out-of-order/late ack cannot downgrade a higher status, which also makes these // fire-and-forget writes race-safe at the DB level. const status = ackToMessageStatus(ack); if (status) { void this.messageRepository .update({ waMessageId: messageId, status: In(ackStatusTransitionFrom(status)) }, { status }) .then(result => { // affected:0 — the row was not advanced: either the send's 2nd save (which sets // waMessageId) hasn't committed yet, or the status is already at/above the target. if (result.affected === 0) { this.logger.debug(`Message ack ${messageId}: no status row advanced to ${status} (ack=${ack})`, { sessionId: id, messageId, ack, action: 'message_ack_noop', }); } }); } // Dispatch the delivery/read receipt to webhooks (#155). Outgoing `message.sent` is handled // solely by `onMessageCreate`, so the ack path deliberately does NOT emit `message.sent`. // `id` mirrors the field every other message.* webhook carries (and the idempotency key // resolver reads); `messageId` is kept for backward compatibility. void this.webhookService.dispatch(id, 'message.ack', { id: messageId, messageId, ack }); // Surface delivery failures actively so consumers don't have to poll for them (#220). if (ack < 0) { void this.webhookService.dispatch(id, 'message.failed', { id: messageId, messageId, ack }); } }, onMessageRevoked: (message): void => { this.logger.debug(`Message revoked: ${message.id}`, { sessionId: id, messageId: message.id, action: 'message_revoked', }); // Flag the stored message as revoked (best-effort; the message may not be in the // DB). The dashboard renders the localized "message deleted" text, so no display // string is persisted here. void this.messageRepository .update({ sessionId: id, waMessageId: message.id }, { body: '', type: 'revoked' }) .catch(err => { this.logger.error(`Failed to update revoked message: ${message.id}`, String(err)); }); // Notify consumers regardless of whether the row existed: webhook (message.revoked // is a declared event) + the real-time dashboard stream. const revokedPayload = message as unknown as Record; void this.webhookService.dispatch(id, 'message.revoked', revokedPayload); this.eventsGateway.emitMessageRevoked(id, revokedPayload); }, onMessageReaction: (event): void => { this.logger.debug(`Message reaction received: ${event.messageId} -> ${event.reaction}`, { sessionId: id, messageId: event.messageId, action: 'message_reaction_received', }); void this.messageRepository .findOne({ where: { sessionId: id, waMessageId: event.messageId } }) .then(async msg => { if (!msg) return; const metadata = msg.metadata || {}; const reactions = (metadata.reactions as Record) || {}; if (!event.reaction) { delete reactions[event.senderId]; } else { reactions[event.senderId] = event.reaction; } metadata.reactions = reactions; msg.metadata = metadata; await this.messageRepository.save(msg); this.eventsGateway.emitMessageReaction(id, { ...event, reactions }); }) .catch(err => { this.logger.error(`Failed to update message reaction: ${event.messageId}`, String(err)); }); }, onDisconnected: (reason: string): void => { this.logger.warn(`Session disconnected: ${reason}`, { sessionId: id, reason, action: 'disconnected', }); // Execute hook for disconnected event void this.hookManager.execute( 'session:disconnected', { reason }, { sessionId: id, source: 'Engine', }, ); void this.updateStatus(id, SessionStatus.DISCONNECTED); // Attempt to reconnect this.scheduleReconnect(id, session); }, onStateChanged: (engineState: EngineStatus): void => { const statusMap: Record = { [EngineStatus.DISCONNECTED]: SessionStatus.DISCONNECTED, [EngineStatus.INITIALIZING]: SessionStatus.INITIALIZING, [EngineStatus.QR_READY]: SessionStatus.QR_READY, [EngineStatus.AUTHENTICATING]: SessionStatus.AUTHENTICATING, [EngineStatus.READY]: SessionStatus.READY, [EngineStatus.FAILED]: SessionStatus.FAILED, }; const newStatus = statusMap[engineState]; if (newStatus) { void this.updateStatus(id, newStatus); } }, onError: (reason: string): void => { this.logger.error(`Session engine failed: ${reason}`, undefined, { sessionId: id, reason, action: 'engine_error', }); // Remember the reason so findOne/findAll can surface it to the dashboard, // then persist the FAILED status. This is terminal — no reconnect is // scheduled (unlike onDisconnected), since re-scanning is required. this.sessionErrors.set(id, reason); void this.hookManager.execute( 'session:error', { reason }, { sessionId: id, source: 'Engine', }, ); void this.updateStatus(id, SessionStatus.FAILED); }, }); } catch (error) { // engine.initialize() threw (e.g. network timeout). Remove it from the // map so a subsequent start() call is not blocked by "already started". this.engines.delete(id); throw error; } } private scheduleReconnect(id: string, session: Session): void { const state = this.reconnectStates.get(id); if (!state) return; if (state.attempts >= state.maxAttempts) { this.logger.error(`Max reconnect attempts reached for session: ${session.name}`, undefined, { sessionId: id, attempts: state.attempts, action: 'reconnect_failed', }); return; } // Exponential backoff: baseDelay * 2^attempts (with jitter) const delay = state.baseDelay * Math.pow(2, state.attempts) + Math.random() * 1000; state.attempts++; this.logger.log( `Scheduling reconnect attempt ${state.attempts}/${state.maxAttempts} in ${Math.round(delay / 1000)}s`, { sessionId: id, attempt: state.attempts, delayMs: delay, action: 'reconnect_scheduled', }, ); state.timer = setTimeout(() => { void this.executeReconnect(id, session, state); }, delay); } private async executeReconnect(id: string, session: Session, state: ReconnectState): Promise { try { // Clean up old engine const oldEngine = this.engines.get(id); if (oldEngine) { await oldEngine.destroy(); this.engines.delete(id); } // Re-initialize await this.initializeEngine(id, session); } catch (error: unknown) { const errorMessage = error instanceof Error ? error.message : 'Unknown error'; this.logger.error(`Reconnect attempt ${state.attempts} failed`, errorMessage, { sessionId: id, action: 'reconnect_error', }); // Schedule another attempt this.scheduleReconnect(id, session); } } private cancelReconnect(id: string): void { const state = this.reconnectStates.get(id); if (state?.timer) { clearTimeout(state.timer); state.timer = null; } this.reconnectStates.delete(id); } async stop(id: string): Promise { const session = await this.findOne(id); // Cancel any reconnection attempts this.cancelReconnect(id); const engine = this.engines.get(id); if (engine) { await engine.disconnect(); this.engines.delete(id); } this.logger.log(`Session stopped: ${session.name}`, { sessionId: id, action: 'stop', }); await this.updateStatus(id, SessionStatus.DISCONNECTED); return this.findOne(id); } async getQRCode(id: string): Promise<{ qrCode: string; status: SessionStatus }> { const session = await this.findOne(id); const engine = this.engines.get(id); if (!engine) { throw new BadRequestException('Session is not started. Call POST /sessions/:id/start first.'); } const qrCode = engine.getQRCode(); if (!qrCode) { if (session.status === SessionStatus.READY) { throw new BadRequestException('Session is already authenticated, no QR code needed'); } throw new BadRequestException('QR code is not ready yet. Please wait...'); } return { qrCode, status: session.status, }; } getEngine(id: string): IWhatsAppEngine | undefined { return this.engines.get(id); } async getGroups(id: string): Promise<{ id: string; name: string; linkedParentJID?: string | null }[]> { await this.findOne(id); // Verify session exists const engine = this.engines.get(id); if (!engine) { throw new BadRequestException('Session is not started'); } const groups = await engine.getGroups(); return groups.map(g => ({ id: g.id, name: g.name, linkedParentJID: g.linkedParentJID, })); } async getChats(id: string): Promise { await this.findOne(id); // Verify session exists const engine = this.engines.get(id); if (!engine) { throw new BadRequestException('Session is not started'); } return engine.getChats(); } async sendSeen(id: string, chatId: string): Promise { await this.findOne(id); // Verify session exists const engine = this.engines.get(id); if (!engine) { throw new BadRequestException('Session is not started'); } return engine.sendSeen(chatId); } private async updateStatus(id: string, status: SessionStatus): Promise { await this.sessionRepository.update(id, { status }); this.logger.debug(`Session status updated to ${status}`, { sessionId: id, status, action: 'status_update', }); // Emit real-time event to connected WebSocket clients this.eventsGateway.emitSessionStatus(id, status); } /** * Get overall session statistics for multi-session monitoring */ async getStats(): Promise<{ total: number; active: number; ready: number; disconnected: number; byStatus: Record; memoryUsage: { heapUsed: number; heapTotal: number; rss: number }; }> { const sessions = await this.findAll(); const byStatus: Record = {}; for (const session of sessions) { byStatus[session.status] = (byStatus[session.status] || 0) + 1; } const memory = process.memoryUsage(); return { total: sessions.length, active: this.engines.size, ready: byStatus[SessionStatus.READY] || 0, disconnected: byStatus[SessionStatus.DISCONNECTED] || 0, byStatus, memoryUsage: { heapUsed: Math.round(memory.heapUsed / 1024 / 1024), heapTotal: Math.round(memory.heapTotal / 1024 / 1024), rss: Math.round(memory.rss / 1024 / 1024), }, }; } /** * Get count of currently active (running) sessions */ getActiveCount(): number { return this.engines.size; } /** * Check if session is currently active (engine running) */ isActive(id: string): boolean { return this.engines.has(id); } private delay(ms: number): Promise { return new Promise(resolve => setTimeout(resolve, ms)); } }