Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

* [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.
* [WIDGET-WORKS] Save messages before Chatwoot sync in `/message/sendText` to avoid missing `chatwootMessageId` and log async sync failures.


### Features
Expand Down
32 changes: 22 additions & 10 deletions src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2195,14 +2195,6 @@ export class BaileysStartupService extends ChannelStartupService {
messageSent?.message?.ptvMessage ||
messageSent?.message?.audioMessage;

if (this.configService.get<Chatwoot>('CHATWOOT').ENABLED && this.localChatwoot?.enabled && !isIntegration) {
this.chatwootService.eventWhatsapp(
Events.SEND_MESSAGE,
{ instanceName: this.instance.name, instanceId: this.instanceId },
messageRaw,
);
}

if (this.configService.get<Openai>('OPENAI').ENABLED && messageRaw?.message?.audioMessage) {
const openAiDefaultSettings = await this.prismaRepository.openaiSetting.findFirst({
where: { instanceId: this.instanceId },
Expand All @@ -2214,9 +2206,29 @@ export class BaileysStartupService extends ChannelStartupService {
}
}

if (this.configService.get<Database>('DATABASE').SAVE_DATA.NEW_MESSAGE) {
const msg = await this.prismaRepository.message.create({ data: messageRaw });
const shouldSaveMessage = this.configService.get<Database>('DATABASE').SAVE_DATA.NEW_MESSAGE;
let msg;

if (shouldSaveMessage) {
msg = await this.prismaRepository.message.create({ data: messageRaw });
}

if (this.configService.get<Chatwoot>('CHATWOOT').ENABLED && this.localChatwoot?.enabled && !isIntegration) {
// [WIDGET-WORKS] Run after DB save to avoid chatwootMessageId race and surface async Chatwoot failures
this.chatwootService
.eventWhatsapp(
Events.SEND_MESSAGE,
{ instanceName: this.instance.name, instanceId: this.instanceId },
messageRaw,
)
.catch((error) => {
this.logger.error(
`[WIDGET-WORKS] Chatwoot sync failed for message ${messageRaw?.key?.id ?? 'unknown'}: ${error?.message}`,
);
});
}

if (shouldSaveMessage && msg) {
if (isMedia && this.configService.get<S3>('S3').ENABLE) {
try {
const message: any = messageRaw;
Expand Down