From 11d31123ac2ea4f202852c10aaddf1d83180ee66 Mon Sep 17 00:00:00 2001 From: yousseefs Date: Mon, 28 Oct 2024 20:23:20 +0000 Subject: [PATCH] fix: received messages but chat doesnt exists --- src/api/controllers/instance.controller.ts | 2 +- .../whatsapp/whatsapp.baileys.service.ts | 39 +++++++++++++++++++ 2 files changed, 40 insertions(+), 1 deletion(-) diff --git a/src/api/controllers/instance.controller.ts b/src/api/controllers/instance.controller.ts index 074d2a49..a61fbe35 100644 --- a/src/api/controllers/instance.controller.ts +++ b/src/api/controllers/instance.controller.ts @@ -382,7 +382,7 @@ export class InstanceController { return this.waMonitor.instanceInfoById(instanceId, number); } - return this.waMonitor.instanceInfo(instanceName); + return this.waMonitor.instanceInfo([instanceName]); } public async setPresence({ instanceName }: InstanceDto, data: SetPresenceDto) { diff --git a/src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts b/src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts index 0105a14c..185910bd 100644 --- a/src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts +++ b/src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts @@ -1071,6 +1071,25 @@ export class BaileysStartupService extends ChannelStartupService { if (settings?.groupsIgnore && received.key.remoteJid.includes('@g.us')) { continue; } + const existingChat = await this.prismaRepository.chat.findFirst({ + where: { instanceId: this.instanceId, remoteJid: received.key.remoteJid }, + }); + + if (!!existingChat) { + const chatToInsert = { + remoteJid: received.key.remoteJid, + instanceId: this.instanceId, + name: received.pushName || '', + unreadMessages: 0, + }; + + this.sendDataWebhook(Events.CHATS_UPSERT, [chatToInsert]); + if (this.configService.get('DATABASE').SAVE_DATA.CHATS) { + await this.prismaRepository.chat.create({ + data: chatToInsert, + }); + } + } const messageRaw = this.prepareMessage(received); @@ -1386,6 +1405,26 @@ export class BaileysStartupService extends ChannelStartupService { await this.prismaRepository.messageUpdate.create({ data: message, }); + + const existingChat = await this.prismaRepository.chat.findFirst({ + where: { instanceId: this.instanceId, remoteJid: message.key.remoteJid }, + }); + + if (!!existingChat) { + const chatToInsert = { + remoteJid: message.key.remoteJid, + instanceId: this.instanceId, + name: message.pushName || '', + unreadMessages: 0, + }; + + this.sendDataWebhook(Events.CHATS_UPSERT, [chatToInsert]); + if (this.configService.get('DATABASE').SAVE_DATA.CHATS) { + await this.prismaRepository.chat.create({ + data: chatToInsert, + }); + } + } } }