Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
import { Repository } from 'typeorm';
import { Session, SessionStatus } from './entities/session.entity';
import { EventsGateway } from '../events/events.gateway';
import { WebhookService } from '../webhook/webhook.service';
import { type createLogger } from '../../common/services/logger.service';
/**
* The session-status fan-out extracted from SessionEngineLifecycle: persist the status, then
* mirror it to WS clients and subscribed webhooks, de-duped. Plain class (NOT a NestJS provider —
* the lifecycle's constructor signature is frozen by specs), built inside the lifecycle's
* constructor. The de-dup Map is OWNED here; the lifecycle exposes a `lastDispatchedStatus`
* getter alias returning it by reference (the liveCalls precedent) so the spec's `.set()` pokes
* land on this exact instance, and keeps `updateStatus` as a public delegate so its 9 call sites
* stay byte-identical. The method body below moved verbatim, which is why it reads
* `this.sessionRepository` / `this.eventsGateway` / `this.webhookService` / `this.logger` against
* the same-named fields assigned here.
*/
export class SessionStatusBroadcaster {
// Last session.status value broadcast per session. Some engines signal one transition via BOTH
// onStateChanged and a dedicated callback (onQRCode/onDisconnected), so this guards both the WS emit
// and the webhook POST against firing the same status twice. Cleared on delete(), and before a
// lapsed-row correction is announced (see SessionEngineLifecycle.announceStatus).
readonly lastDispatchedStatus = new Map<string, SessionStatus>();
private readonly sessionRepository: Repository<Session>;
private readonly eventsGateway: EventsGateway;
private readonly webhookService: WebhookService;
private readonly logger: ReturnType<typeof createLogger>;
constructor(deps: {
sessionRepository: Repository<Session>;
eventsGateway: EventsGateway;
webhookService: WebhookService;
logger: ReturnType<typeof createLogger>;
}) {
this.sessionRepository = deps.sessionRepository;
this.eventsGateway = deps.eventsGateway;
this.webhookService = deps.webhookService;
this.logger = deps.logger;
}
/**
* `canAnnounce`, when given, is asked once the write has settled: a caller whose engine can retire
* while the write is in flight keeps a late reply from announcing a status that is no longer current.
*/
async updateStatus(id: string, status: SessionStatus, canAnnounce?: () => boolean): Promise<void> {
await this.sessionRepository.update(id, { status });
this.logger.debug(`Session status updated to ${status}`, {
sessionId: id,
status,
action: 'status_update',
});
if (canAnnounce?.() === false) return;
this.announce(id, status);
}
/**
* The fan-out half of updateStatus, for a caller that has ALREADY written the row itself.
*
* One caller needs that split: a row whose owning node is gone has to be corrected under a
* predicate (still that node's, still lapsed, still claiming to run), never by id, because a peer
* can claim and start it at any moment. Writing by id afterwards to get the fan-out would undo the
* predicate. Everything else must keep using updateStatus.
*/
announce(id: string, status: SessionStatus): void {
// Mirror the status change to WS clients AND subscribed webhooks — both de-duped. Some engines signal
// one transition via both onStateChanged AND a dedicated callback (onQRCode/onDisconnected), which
// would otherwise emit/POST the same status twice; only act when it actually changed from the last one.
if (this.lastDispatchedStatus.get(id) !== status) {
this.lastDispatchedStatus.set(id, status);
this.eventsGateway.emitSessionStatus(id, status);
void this.webhookService.dispatch(id, 'session.status', { sessionId: id, status });
}
}
/**
* Drop the de-dup entry for a session — called from delete()'s committed-delete cleanup, and
* before a lapsed-status correction announces, since that write is a transition this process never saw.
*/
clear(id: string): void {
this.lastDispatchedStatus.delete(id);
}
}