diff --git a/src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts b/src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts index 908b3dff..02cd04e5 100644 --- a/src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts +++ b/src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts @@ -126,7 +126,6 @@ import { LabelAssociation } from 'baileys/lib/Types/LabelAssociation'; import { spawn } from 'child_process'; import { isArray, isBase64, isURL } from 'class-validator'; import { randomBytes } from 'crypto'; -import cuid from 'cuid'; import EventEmitter2 from 'eventemitter2'; import ffmpeg from 'fluent-ffmpeg'; import FormData from 'form-data'; @@ -1137,29 +1136,25 @@ export class BaileysStartupService extends ChannelStartupService { } const existingChat = await this.prismaRepository.chat.findFirst({ where: { instanceId: this.instanceId, remoteJid: received.key.remoteJid }, + select: { id: true, name: true }, }); - if (existingChat) { - const chatToInsert = { - remoteJid: received.key.remoteJid, - instanceId: this.instanceId, - name: received.pushName || '', - unreadMessages: 0, - }; - - this.sendDataWebhook(Events.CHATS_UPSERT, [chatToInsert]); + if ( + existingChat && + received.pushName && + existingChat.name !== received.pushName && + received.pushName.trim().length > 0 + ) { + this.sendDataWebhook(Events.CHATS_UPSERT, [{ ...existingChat, name: received.pushName }]); if (this.configService.get('DATABASE').SAVE_DATA.CHATS) { - try { - await this.prismaRepository.chat.update({ - where: { - id: existingChat.id, - }, - data: chatToInsert, - }); - } - catch(error){ - console.log(`Chat insert record ignored: ${chatToInsert.remoteJid} - ${chatToInsert.instanceId}`); - } + try { + await this.prismaRepository.chat.update({ + where: { id: existingChat.id }, + data: { name: received.pushName }, + }); + } catch (error) { + console.log(`Chat insert record ignored: ${received.key.remoteJid} - ${this.instanceId}`); + } } } @@ -1495,13 +1490,12 @@ export class BaileysStartupService extends ChannelStartupService { if (this.configService.get('DATABASE').SAVE_DATA.CHATS) { try { await this.prismaRepository.chat.update({ - where: { - id: existingChat.id, - }, + where: { + id: existingChat.id, + }, data: chatToInsert, }); - } - catch(error){ + } catch (error) { console.log(`Chat insert record ignored: ${chatToInsert.remoteJid} - ${chatToInsert.instanceId}`); } }