From fc433c98f17a336e8dfecb69348ca1f9546f2711 Mon Sep 17 00:00:00 2001 From: Wahyu Saputra Date: Tue, 2 Dec 2025 19:54:02 +0700 Subject: [PATCH 1/4] fix(whatsapp): guard messageUpdate.create against missing parent message - Prevents PrismaClientValidationError (Argument Message is missing) when processing updates for messages not in the database. - Adds a check for message.messageId before calling messageUpdate.create. - Logs a warning if the parent message is missing. - [WIDGET-WORKS] fix/cache-delete-messages-update --- CHANGELOG.md | 12 ++++---- .../whatsapp/whatsapp.baileys.service.ts | 28 +++++++++++++++---- 2 files changed, 28 insertions(+), 12 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 711d6e9b7..83cc3628f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,10 +1,10 @@ -# Unreleased +# 2.3.2 (2025-09-02) -### Fixed +### Widget Works Modification +* [WIDGET-WORKS] Fix `PrismaClientValidationError` in `messages.update` (missing `Message` relation) by guarding `messageUpdate.create` calls. * [WIDGET-WORKS] Guard `messages.update` cache cleanup with a derived key and optional `MESSAGE_UPDATE_CACHE_DELETE_DISABLED` flag to prevent cache.delete crash-loops. -# 2.3.2 (2025-09-02) ### Features @@ -803,7 +803,7 @@ * Added messages.delete event * Added restart instance endpoint -* Created automation for creating instances in the chatwoot bot with the command '#inbox_whatsapp:{INSTANCE_NAME} +* Created automation for creating instances in the chatwoot bot with the command '#inbox_whatsapp:{INSTANCE_NAME}' * Change Baileys version to: 6.4.0 * Send contact in chatwoot * Send contact array in chatwoot @@ -814,7 +814,7 @@ * Fixed error to send message in large groups * Docker files adjusted -* Fixed in the postman collection the webhookByEvent parameter by webhook_by_events +* Fixed in the postman collection the webhookByEvent parameter by webhook_by__events * Added validations in create instance * Removed link preview endpoint, now it's done automatically from sending conventional text * Added group membership validation before sending message to groups @@ -999,4 +999,4 @@ * Sending the local webhook url as destination in the webhook data for webhook redirection * Startup modes, server or container * Server Mode works normally as everyone is used to -* Container mode made to use one instance per container, when starting the application an instance is already created and the qrcode is generated and it starts sending webhook without having to call it manually, it only allows one instance at a time. +* Container mode made to use one instance per container, when starting the application an instance is already created and the qrcode is generated and it starts sending webhook without having to call it manually, it only allows one instance at a time. \ No newline at end of file diff --git a/src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts b/src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts index 03be7876c..eea479af8 100644 --- a/src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts +++ b/src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts @@ -151,7 +151,8 @@ import { BaileysMessageProcessor } from './baileysMessage.processor'; import { useVoiceCallsBaileys } from './voiceCalls/useVoiceCallsBaileys'; // [WIDGET-WORKS] Local helper because isJidUser is absent from current Baileys typings. -const isJidUser = (jid?: string) => !!jid && jid.includes('@s.whatsapp.net') && !isJidGroup(jid) && !isJidBroadcast(jid); +const isJidUser = (jid?: string) => + !!jid && jid.includes('@s.whatsapp.net') && !isJidGroup(jid) && !isJidBroadcast(jid); // [WIDGET-WORKS] Gracefully access non-typed Baileys senderPn on keys. const getSenderPn = (key: WAMessageKey | proto.IMessageKey) => (key as any)?.senderPn; @@ -1478,9 +1479,16 @@ export class BaileysStartupService extends ChannelStartupService { if (update.message === null && update.status === undefined) { this.sendDataWebhook(Events.MESSAGES_DELETE, key); - if (this.configService.get('DATABASE').SAVE_DATA.MESSAGE_UPDATE) - await this.prismaRepository.messageUpdate.create({ data: message }); - + if (this.configService.get('DATABASE').SAVE_DATA.MESSAGE_UPDATE) { + // [WIDGET-WORKS] Guard against missing parent message to prevent PrismaClientValidationError + if (message.messageId) { + await this.prismaRepository.messageUpdate.create({ data: message }); + } else { + this.logger.warn( + `[WIDGET-WORKS] Skipping messageUpdate.create for key ${key.id}: Parent message not found.`, + ); + } + } if (this.configService.get('CHATWOOT').ENABLED && this.localChatwoot?.enabled) { this.chatwootService.eventWhatsapp( Events.MESSAGES_DELETE, @@ -1524,8 +1532,16 @@ export class BaileysStartupService extends ChannelStartupService { this.sendDataWebhook(Events.MESSAGES_UPDATE, message); - if (this.configService.get('DATABASE').SAVE_DATA.MESSAGE_UPDATE) - await this.prismaRepository.messageUpdate.create({ data: message }); + if (this.configService.get('DATABASE').SAVE_DATA.MESSAGE_UPDATE) { + // [WIDGET-WORKS] Guard against missing parent message to prevent PrismaClientValidationError + if (message.messageId) { + await this.prismaRepository.messageUpdate.create({ data: message }); + } else { + this.logger.warn( + `[WIDGET-WORKS] Skipping messageUpdate.create for key ${key.id}: Parent message not found.`, + ); + } + } if (!skipMessageUpdateCacheDelete) { const cacheDeleteKey = `${this.instanceId}:${message.remoteJid}:${message.keyId}`; From 2a6ab666b13be6377e9e863e117f973cf439fa8a Mon Sep 17 00:00:00 2001 From: Wahyu Saputra Date: Tue, 2 Dec 2025 21:53:12 +0700 Subject: [PATCH 2/4] chore: fix lint and clean changelog --- CHANGELOG.md | 6 +++--- .../channel/whatsapp/baileysMessage.processor.ts | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 83cc3628f..d884ba244 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -803,7 +803,7 @@ * Added messages.delete event * Added restart instance endpoint -* Created automation for creating instances in the chatwoot bot with the command '#inbox_whatsapp:{INSTANCE_NAME}' +* Created automation for creating instances in the chatwoot bot with the command #inbox_whatsapp:{INSTANCE_NAME} * Change Baileys version to: 6.4.0 * Send contact in chatwoot * Send contact array in chatwoot @@ -814,7 +814,7 @@ * Fixed error to send message in large groups * Docker files adjusted -* Fixed in the postman collection the webhookByEvent parameter by webhook_by__events +* Fixed in the postman collection the webhookByEvent parameter by webhook_by_events * Added validations in create instance * Removed link preview endpoint, now it's done automatically from sending conventional text * Added group membership validation before sending message to groups @@ -999,4 +999,4 @@ * Sending the local webhook url as destination in the webhook data for webhook redirection * Startup modes, server or container * Server Mode works normally as everyone is used to -* Container mode made to use one instance per container, when starting the application an instance is already created and the qrcode is generated and it starts sending webhook without having to call it manually, it only allows one instance at a time. \ No newline at end of file +* Container mode made to use one instance per container, when starting the application an instance is already created and the qrcode is generated and it starts sending webhook without having to call it manually, it only allows one instance at a time. diff --git a/src/api/integrations/channel/whatsapp/baileysMessage.processor.ts b/src/api/integrations/channel/whatsapp/baileysMessage.processor.ts index 1b0202fbf..d46c347ad 100644 --- a/src/api/integrations/channel/whatsapp/baileysMessage.processor.ts +++ b/src/api/integrations/channel/whatsapp/baileysMessage.processor.ts @@ -1,5 +1,5 @@ import { Logger } from '@config/logger.config'; -import { BaileysEventMap, MessageUpsertType, proto } from 'baileys'; +import { BaileysEventMap, MessageUpsertType } from 'baileys'; import { catchError, concatMap, delay, EMPTY, from, retryWhen, Subject, Subscription, take, tap } from 'rxjs'; type MessageUpsertPayload = BaileysEventMap['messages.upsert']; From 4dbff2bc6eb63cbc9f0715a6e3004fa7cea3789f Mon Sep 17 00:00:00 2001 From: Wahyu Saputra Date: Tue, 2 Dec 2025 21:55:36 +0700 Subject: [PATCH 3/4] chore: restore changelog wording --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d884ba244..f69f0ccec 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -803,7 +803,7 @@ * Added messages.delete event * Added restart instance endpoint -* Created automation for creating instances in the chatwoot bot with the command #inbox_whatsapp:{INSTANCE_NAME} +* Created automation for creating instances in the chatwoot bot with the command '#inbox_whatsapp:{INSTANCE_NAME}' * Change Baileys version to: 6.4.0 * Send contact in chatwoot * Send contact array in chatwoot From eccd5680d3e6d850ce23a71f845b38d065828513 Mon Sep 17 00:00:00 2001 From: Wahyu Saputra Date: Tue, 2 Dec 2025 21:58:09 +0700 Subject: [PATCH 4/4] chore: tweak changelog line --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f69f0ccec..329af4351 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -803,7 +803,7 @@ * Added messages.delete event * Added restart instance endpoint -* Created automation for creating instances in the chatwoot bot with the command '#inbox_whatsapp:{INSTANCE_NAME}' +* Created automation for creating instances in the chatwoot bot with the command '#inbox_whatsapp:{INSTANCE_NAME} * Change Baileys version to: 6.4.0 * Send contact in chatwoot * Send contact array in chatwoot