Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -426,8 +426,19 @@ export class BaileysStartupService extends ChannelStartupService {
if (connection === 'close') {
const statusCode = (lastDisconnect?.error as Boom)?.output?.statusCode;
const codesToNotReconnect = [DisconnectReason.loggedOut, DisconnectReason.forbidden, 402, 406];

// FIX: Não reconectar se é primeira conexão (aguardando QR code)
// Isso evita loop infinito que impede geração do QR
const isInitialConnection = !this.instance.wuid && this.instance.qrcode.count === 0;

if (isInitialConnection) {
this.logger.info('Initial connection closed, waiting for QR code generation...');
return;
}

const shouldReconnect = !codesToNotReconnect.includes(statusCode);
if (shouldReconnect) {
this.logger.warn(`Connection lost (status: ${statusCode}), reconnecting...`);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion: Consider symmetric logging when opting not to reconnect

When shouldReconnect is false there’s no corresponding log explaining that reconnection is being skipped and why. Please add a log in the else branch (e.g. info/warn) that includes the statusCode and mentions that reconnect is skipped because it’s in codesToNotReconnect, to improve debugging and correlation with webhook events.

await this.connectToWhatsapp(this.phoneNumber);
} else {
this.sendDataWebhook(Events.STATUS_INSTANCE, {
Expand Down