From 6101c8d65140fe8a2716e0507bee700db23ac5f1 Mon Sep 17 00:00:00 2001 From: William Dumes Date: Fri, 25 Jul 2025 17:08:14 -0300 Subject: [PATCH 1/5] fix: corrigido disparo de eventos quando nao usa a opcao da ENV de salvar mensagens no banco --- .../whatsapp/whatsapp.baileys.service.ts | 91 +++++++++++-------- 1 file changed, 53 insertions(+), 38 deletions(-) diff --git a/src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts b/src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts index f454473a..7d7da2be 100644 --- a/src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts +++ b/src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts @@ -1443,16 +1443,7 @@ export class BaileysStartupService extends ChannelStartupService { } } - const findMessage = await this.prismaRepository.message.findFirst({ - where: { instanceId: this.instanceId, key: { path: ['id'], equals: key.id } }, - }); - - if (!findMessage) { - continue; - } - const message: any = { - messageId: findMessage.id, keyId: key.id, remoteJid: key?.remoteJid, fromMe: key.fromMe, @@ -1462,6 +1453,16 @@ export class BaileysStartupService extends ChannelStartupService { instanceId: this.instanceId, }; + let findMessage: any; + const configDatabaseData = this.configService.get('DATABASE').SAVE_DATA; + if (configDatabaseData.HISTORIC || configDatabaseData.NEW_MESSAGE) { + findMessage = await this.prismaRepository.message.findFirst({ + where: { instanceId: this.instanceId, key: { path: ['id'], equals: key.id } }, + }); + + if (findMessage) message.messageId = findMessage.id; + } + if (update.message === null && update.status === undefined) { this.sendDataWebhook(Events.MESSAGES_DELETE, key); @@ -1477,7 +1478,9 @@ export class BaileysStartupService extends ChannelStartupService { } continue; - } else if (update.status !== undefined && status[update.status] !== findMessage.status) { + } + + if (findMessage && update.status !== undefined && status[update.status] !== findMessage.status) { if (!key.fromMe && key.remoteJid) { readChatToUpdate[key.remoteJid] = true; @@ -3431,16 +3434,18 @@ export class BaileysStartupService extends ChannelStartupService { where: { id: message.id }, data: { key: { ...existingKey, deleted: true }, status: 'DELETED' }, }); - const messageUpdate: any = { - messageId: message.id, - keyId: messageId, - remoteJid: response.key.remoteJid, - fromMe: response.key.fromMe, - participant: response.key?.remoteJid, - status: 'DELETED', - instanceId: this.instanceId, - }; - await this.prismaRepository.messageUpdate.create({ data: messageUpdate }); + if (this.configService.get('DATABASE').SAVE_DATA.MESSAGE_UPDATE) { + const messageUpdate: any = { + messageId: message.id, + keyId: messageId, + remoteJid: response.key.remoteJid, + fromMe: response.key.fromMe, + participant: response.key?.remoteJid, + status: 'DELETED', + instanceId: this.instanceId, + }; + await this.prismaRepository.messageUpdate.create({ data: messageUpdate }); + } } else { await this.prismaRepository.message.deleteMany({ where: { id: message.id } }); } @@ -3771,6 +3776,10 @@ export class BaileysStartupService extends ChannelStartupService { private async formatUpdateMessage(data: UpdateMessageDto) { try { + if (!this.configService.get('DATABASE').SAVE_DATA.NEW_MESSAGE) { + return data; + } + const msg: any = await this.getMessage(data.key, true); if (msg?.messageType === 'conversation' || msg?.messageType === 'extendedTextMessage') { @@ -3804,13 +3813,15 @@ export class BaileysStartupService extends ChannelStartupService { try { const oldMessage: any = await this.getMessage(data.key, true); - if (!oldMessage) throw new NotFoundException('Message not found'); - if (oldMessage?.key?.remoteJid !== jid) { - throw new BadRequestException('RemoteJid does not match'); - } - if (oldMessage?.messageTimestamp > Date.now() + 900000) { - // 15 minutes in milliseconds - throw new BadRequestException('Message is older than 15 minutes'); + if (this.configService.get('DATABASE').SAVE_DATA.NEW_MESSAGE) { + if (!oldMessage) throw new NotFoundException('Message not found'); + if (oldMessage?.key?.remoteJid !== jid) { + throw new BadRequestException('RemoteJid does not match'); + } + if (oldMessage?.messageTimestamp > Date.now() + 900000) { + // 15 minutes in milliseconds + throw new BadRequestException('Message is older than 15 minutes'); + } } const messageSent = await this.client.sendMessage(jid, { ...(options as any), edit: data.key }); @@ -3828,7 +3839,7 @@ export class BaileysStartupService extends ChannelStartupService { ); const messageId = messageSent.message?.protocolMessage?.key?.id; - if (messageId) { + if (messageId && this.configService.get('DATABASE').SAVE_DATA.NEW_MESSAGE) { let message = await this.prismaRepository.message.findFirst({ where: { key: { path: ['id'], equals: messageId } }, }); @@ -3840,6 +3851,7 @@ export class BaileysStartupService extends ChannelStartupService { if ((message.key.valueOf() as any)?.deleted) { new BadRequestException('You cannot edit deleted messages'); } + if (oldMessage.messageType === 'conversation' || oldMessage.messageType === 'extendedTextMessage') { oldMessage.message.conversation = data.text; } else { @@ -3853,16 +3865,19 @@ export class BaileysStartupService extends ChannelStartupService { messageTimestamp: Math.floor(Date.now() / 1000), // Convert to int32 by dividing by 1000 to get seconds }, }); - const messageUpdate: any = { - messageId: message.id, - keyId: messageId, - remoteJid: messageSent.key.remoteJid, - fromMe: messageSent.key.fromMe, - participant: messageSent.key?.remoteJid, - status: 'EDITED', - instanceId: this.instanceId, - }; - await this.prismaRepository.messageUpdate.create({ data: messageUpdate }); + + if (this.configService.get('DATABASE').SAVE_DATA.MESSAGE_UPDATE) { + const messageUpdate: any = { + messageId: message.id, + keyId: messageId, + remoteJid: messageSent.key.remoteJid, + fromMe: messageSent.key.fromMe, + participant: messageSent.key?.remoteJid, + status: 'EDITED', + instanceId: this.instanceId, + }; + await this.prismaRepository.messageUpdate.create({ data: messageUpdate }); + } } } } From 69726f0dc2b77c6c017d08fd65491195059617c7 Mon Sep 17 00:00:00 2001 From: William Dumes Date: Mon, 28 Jul 2025 09:24:22 -0300 Subject: [PATCH 2/5] fix(evo): melhorado controle de recebimento do ack das msgs --- Dockerfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Dockerfile b/Dockerfile index 02130c43..261b734c 100644 --- a/Dockerfile +++ b/Dockerfile @@ -55,6 +55,6 @@ COPY --from=builder /evolution/tsup.config.ts ./tsup.config.ts ENV DOCKER_ENV=true -EXPOSE 8080 +EXPOSE 9000 ENTRYPOINT ["/bin/bash", "-c", ". ./Docker/scripts/deploy_database.sh && npm run start:prod" ] From a62f9ebe465fc6a933716b787063387435f1fe7e Mon Sep 17 00:00:00 2001 From: William Dumes Date: Mon, 28 Jul 2025 11:19:07 -0300 Subject: [PATCH 3/5] chore: voltar porta do dockerfile --- Dockerfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Dockerfile b/Dockerfile index 261b734c..02130c43 100644 --- a/Dockerfile +++ b/Dockerfile @@ -55,6 +55,6 @@ COPY --from=builder /evolution/tsup.config.ts ./tsup.config.ts ENV DOCKER_ENV=true -EXPOSE 9000 +EXPOSE 8080 ENTRYPOINT ["/bin/bash", "-c", ". ./Docker/scripts/deploy_database.sh && npm run start:prod" ] From bc11d0f751f68816db68a46788c6821d7328de2f Mon Sep 17 00:00:00 2001 From: William Dumes Date: Thu, 31 Jul 2025 17:27:06 -0300 Subject: [PATCH 4/5] fix: corrigido delete de mensagem quando nao salvo no banco de dados --- .../integrations/channel/whatsapp/whatsapp.baileys.service.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts b/src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts index 7d7da2be..8496a759 100644 --- a/src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts +++ b/src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts @@ -3447,6 +3447,7 @@ export class BaileysStartupService extends ChannelStartupService { await this.prismaRepository.messageUpdate.create({ data: messageUpdate }); } } else { + if (!message) return response; await this.prismaRepository.message.deleteMany({ where: { id: message.id } }); } this.sendDataWebhook(Events.MESSAGES_DELETE, { From 79f4a22217e6cb52e9ed41254c5f1037ed545aa4 Mon Sep 17 00:00:00 2001 From: William Dumes Date: Mon, 4 Aug 2025 13:56:16 -0300 Subject: [PATCH 5/5] refactor: lint check --- .../integrations/channel/whatsapp/whatsapp.baileys.service.ts | 2 +- 1 file changed, 1 insertion(+), 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 f9dbeed0..c036a281 100644 --- a/src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts +++ b/src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts @@ -1482,7 +1482,7 @@ export class BaileysStartupService extends ChannelStartupService { continue; } - + if (findMessage && update.status !== undefined && status[update.status] !== findMessage.status) { if (!key.fromMe && key.remoteJid) { readChatToUpdate[key.remoteJid] = true;