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 @@ -5,6 +5,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.
* [WIDGET-WORKS] Skip Chatwoot sync in `messages.upsert` when a message key already has `chatwootMessageId` (handles Chatwoot automation echoes/duplicates).


### Features
Expand Down
37 changes: 32 additions & 5 deletions src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1187,6 +1187,36 @@ export class BaileysStartupService extends ChannelStartupService {

const messageRaw = this.prepareMessage(received);

const shouldSyncChatwoot =
this.configService.get<Chatwoot>('CHATWOOT').ENABLED &&
this.localChatwoot?.enabled &&
!received.key.id.includes('@broadcast');

if (shouldSyncChatwoot) {
const existingChatwootMessage = await this.prismaRepository.message.findFirst({
where: {
instanceId: this.instanceId, // [WIDGET-WORKS] Scope Chatwoot dedupe to current instance
key: { path: ['id'], equals: received.key.id },
chatwootMessageId: { not: null },
},
select: {
chatwootMessageId: true,
chatwootInboxId: true,
chatwootConversationId: true,
},
});

if (existingChatwootMessage) {
this.logger.verbose(
`[WIDGET-WORKS] Message ${received.key.id} already synced to Chatwoot (ID: ${existingChatwootMessage.chatwootMessageId}), skipping duplicate sync`,
);

messageRaw.chatwootMessageId = existingChatwootMessage.chatwootMessageId;
messageRaw.chatwootInboxId = existingChatwootMessage.chatwootInboxId;
messageRaw.chatwootConversationId = existingChatwootMessage.chatwootConversationId;
}
}

const isMedia =
received?.message?.imageMessage ||
received?.message?.videoMessage ||
Expand All @@ -1204,11 +1234,8 @@ export class BaileysStartupService extends ChannelStartupService {
await this.client.readMessages([received.key]);
}

if (
this.configService.get<Chatwoot>('CHATWOOT').ENABLED &&
this.localChatwoot?.enabled &&
!received.key.id.includes('@broadcast')
) {
if (shouldSyncChatwoot && !messageRaw.chatwootMessageId) {
// [WIDGET-WORKS] Skip Chatwoot sync if this message was already synced (DB fallback after cache miss)
const chatwootSentMessage = await this.chatwootService.eventWhatsapp(
Events.MESSAGES_UPSERT,
{ instanceName: this.instance.name, instanceId: this.instanceId },
Expand Down