From 70d334b0b604b8b08170bc7a88e46057051231b0 Mon Sep 17 00:00:00 2001 From: Jeferson Ramos Date: Tue, 20 Jan 2026 10:45:58 -0300 Subject: [PATCH 1/6] fix(whatsapp): enhance contact and chat handling with improved JID mapping and debug logging --- .../whatsapp/whatsapp.baileys.service.ts | 47 +++++++++++++++++-- 1 file changed, 44 insertions(+), 3 deletions(-) diff --git a/src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts b/src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts index 60e857fcc..9b8f31f7a 100644 --- a/src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts +++ b/src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts @@ -939,6 +939,14 @@ export class BaileysStartupService extends ChannelStartupService { progress?: number; syncType?: proto.HistorySync.HistorySyncType; }) => { + //These logs are crucial; when something changes in Baileys/WhatsApp, we can more easily understand what changed! + this.logger.debug('Messages abaixo'); + this.logger.debug(messages); + this.logger.debug('Chats abaixo'); + this.logger.debug(chats); + this.logger.debug('Contatos abaixo'); + this.logger.debug(contacts); + try { if (syncType === proto.HistorySync.HistorySyncType.ON_DEMAND) { console.log('received on-demand history sync, messages=', messages); @@ -967,14 +975,30 @@ export class BaileysStartupService extends ChannelStartupService { } const contactsMap = new Map(); + const contactsMapLidJid = new Map(); for (const contact of contacts) { + + let jid = null; + + if (contact?.id?.search('@lid') !== -1) { + if (contact.phoneNumber) { + jid = contact.phoneNumber; + } + } + + if (!jid) { + jid = contact?.id; + } + if (contact.id && (contact.notify || contact.name)) { - contactsMap.set(contact.id, { name: contact.name ?? contact.notify, jid: contact.id }); + contactsMap.set(contact.id, { name: contact.name ?? contact.notify, jid }); } + + contactsMapLidJid.set(contact.id, { jid }); } - const chatsRaw: { remoteJid: string; instanceId: string; name?: string }[] = []; + const chatsRaw: { remoteJid: string; remoteLid: string; instanceId: string; name?: string }[] = []; const chatsRepository = new Set( (await this.prismaRepository.chat.findMany({ where: { instanceId: this.instanceId } })).map( (chat) => chat.remoteJid, @@ -986,7 +1010,24 @@ export class BaileysStartupService extends ChannelStartupService { continue; } - chatsRaw.push({ remoteJid: chat.id, instanceId: this.instanceId, name: chat.name }); + let remoteJid = null; + let remoteLid = null; + + if (chat.id.search('@lid') !== -1) { + const contact = contactsMapLidJid.get(chat.id); + + remoteLid = chat.id; + + if (contact && contact.jid) { + remoteJid = contact.jid; + } + } + + if (!remoteJid) { + remoteJid = chat.id; + } + + chatsRaw.push({ remoteJid, remoteLid, instanceId: this.instanceId, name: chat.name }); } this.sendDataWebhook(Events.CHATS_SET, chatsRaw); From 3979a9e08ca1eb6e53c66abee41a1de118a1a120 Mon Sep 17 00:00:00 2001 From: Jeferson Ramos Date: Tue, 20 Jan 2026 10:53:06 -0300 Subject: [PATCH 2/6] fix(whatsapp): remove unnecessary blank line in contacts mapping loop --- .../integrations/channel/whatsapp/whatsapp.baileys.service.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts b/src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts index 9b8f31f7a..7ed2750ca 100644 --- a/src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts +++ b/src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts @@ -978,7 +978,6 @@ export class BaileysStartupService extends ChannelStartupService { const contactsMapLidJid = new Map(); for (const contact of contacts) { - let jid = null; if (contact?.id?.search('@lid') !== -1) { From 06b0ff0e27903d3811c725488ddb8c4b8e691292 Mon Sep 17 00:00:00 2001 From: Jeferson Ramos Date: Tue, 20 Jan 2026 13:14:46 -0300 Subject: [PATCH 3/6] fix(whatsapp): update chatsRaw handling to remove remoteLid and optimize variable declaration --- .../channel/whatsapp/whatsapp.baileys.service.ts | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts b/src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts index 7ed2750ca..eb23a7a44 100644 --- a/src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts +++ b/src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts @@ -997,7 +997,7 @@ export class BaileysStartupService extends ChannelStartupService { contactsMapLidJid.set(contact.id, { jid }); } - const chatsRaw: { remoteJid: string; remoteLid: string; instanceId: string; name?: string }[] = []; + let chatsRaw: { remoteJid: string; remoteLid: string; instanceId: string; name?: string }[] = []; const chatsRepository = new Set( (await this.prismaRepository.chat.findMany({ where: { instanceId: this.instanceId } })).map( (chat) => chat.remoteJid, @@ -1032,6 +1032,12 @@ export class BaileysStartupService extends ChannelStartupService { this.sendDataWebhook(Events.CHATS_SET, chatsRaw); if (this.configService.get('DATABASE').SAVE_DATA.HISTORIC) { + chatsRaw = chatsRaw.map((chat) => { + delete chat.remoteLid; + + return chat; + }); + await this.prismaRepository.chat.createMany({ data: chatsRaw, skipDuplicates: true }); } From d131e83d08cbb366bb710a1f0a1eba0f802f78fb Mon Sep 17 00:00:00 2001 From: Jeferson Ramos Date: Wed, 21 Jan 2026 09:53:28 -0300 Subject: [PATCH 4/6] fix(whatsapp): handle remoteLid assignment for chats with accountLid containing '@lid' --- .../integrations/channel/whatsapp/whatsapp.baileys.service.ts | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts b/src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts index eb23a7a44..589719dbe 100644 --- a/src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts +++ b/src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts @@ -1022,6 +1022,10 @@ export class BaileysStartupService extends ChannelStartupService { } } + if (!remoteLid && chat.accountLid.search('@lid') !== -1) { + remoteLid = chat.accountLid; + } + if (!remoteJid) { remoteJid = chat.id; } From b8d6c87360f189c41d6f8223d43a56c2e1f146d3 Mon Sep 17 00:00:00 2001 From: Jeferson Ramos Date: Wed, 21 Jan 2026 17:39:19 -0300 Subject: [PATCH 5/6] fix(whatsapp): correct remoteJid handling and optimize chat creation logic --- .../whatsapp/whatsapp.baileys.service.ts | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts b/src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts index 589719dbe..f8384c7b1 100644 --- a/src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts +++ b/src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts @@ -1028,7 +1028,7 @@ export class BaileysStartupService extends ChannelStartupService { if (!remoteJid) { remoteJid = chat.id; - } + } chatsRaw.push({ remoteJid, remoteLid, instanceId: this.instanceId, name: chat.name }); } @@ -1036,13 +1036,13 @@ export class BaileysStartupService extends ChannelStartupService { this.sendDataWebhook(Events.CHATS_SET, chatsRaw); if (this.configService.get('DATABASE').SAVE_DATA.HISTORIC) { - chatsRaw = chatsRaw.map((chat) => { - delete chat.remoteLid; + const chatsToCreateMany = JSON.parse(JSON.stringify(chatsRaw)).map((chat) => { + delete chat.remoteLid; return chat; - }); + }) - await this.prismaRepository.chat.createMany({ data: chatsRaw, skipDuplicates: true }); + await this.prismaRepository.chat.createMany({ data: chatsToCreateMany, skipDuplicates: true }); } const messagesRaw: any[] = []; @@ -1525,8 +1525,15 @@ export class BaileysStartupService extends ChannelStartupService { this.logger.verbose(messageRaw); sendTelemetry(`received.message.${messageRaw.messageType ?? 'unknown'}`); + if (messageRaw.key.remoteJid?.includes('@lid') && messageRaw.key.remoteJidAlt) { + + const lid = messageRaw.key.remoteJid + messageRaw.key.remoteJid = messageRaw.key.remoteJidAlt; + messageRaw.key.remoteJidAlt = lid + + messageRaw.key.addressingMode = 'pn' } console.log(messageRaw); From 9401216927910fc439558e4586f9def9e4d4a308 Mon Sep 17 00:00:00 2001 From: Jeferson Ramos Date: Wed, 21 Jan 2026 17:42:50 -0300 Subject: [PATCH 6/6] fix(whatsapp): lint --- .../channel/whatsapp/whatsapp.baileys.service.ts | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts b/src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts index f8384c7b1..9db4bfcb3 100644 --- a/src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts +++ b/src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts @@ -997,7 +997,7 @@ export class BaileysStartupService extends ChannelStartupService { contactsMapLidJid.set(contact.id, { jid }); } - let chatsRaw: { remoteJid: string; remoteLid: string; instanceId: string; name?: string }[] = []; + const chatsRaw: { remoteJid: string; remoteLid: string; instanceId: string; name?: string }[] = []; const chatsRepository = new Set( (await this.prismaRepository.chat.findMany({ where: { instanceId: this.instanceId } })).map( (chat) => chat.remoteJid, @@ -1028,7 +1028,7 @@ export class BaileysStartupService extends ChannelStartupService { if (!remoteJid) { remoteJid = chat.id; - } + } chatsRaw.push({ remoteJid, remoteLid, instanceId: this.instanceId, name: chat.name }); } @@ -1036,11 +1036,10 @@ export class BaileysStartupService extends ChannelStartupService { this.sendDataWebhook(Events.CHATS_SET, chatsRaw); if (this.configService.get('DATABASE').SAVE_DATA.HISTORIC) { - const chatsToCreateMany = JSON.parse(JSON.stringify(chatsRaw)).map((chat) => { delete chat.remoteLid; return chat; - }) + }); await this.prismaRepository.chat.createMany({ data: chatsToCreateMany, skipDuplicates: true }); } @@ -1527,13 +1526,12 @@ export class BaileysStartupService extends ChannelStartupService { sendTelemetry(`received.message.${messageRaw.messageType ?? 'unknown'}`); if (messageRaw.key.remoteJid?.includes('@lid') && messageRaw.key.remoteJidAlt) { - - const lid = messageRaw.key.remoteJid + const lid = messageRaw.key.remoteJid; messageRaw.key.remoteJid = messageRaw.key.remoteJidAlt; - messageRaw.key.remoteJidAlt = lid + messageRaw.key.remoteJidAlt = lid; - messageRaw.key.addressingMode = 'pn' + messageRaw.key.addressingMode = 'pn'; } console.log(messageRaw);