fix(meta): normalize execution order and fix chatwootIds in Cloud API

Two bugs in BusinessStartupService message processing:

1. Execution order: Chatwoot was processed AFTER the bot emit(), but
   Baileys channel processes Chatwoot FIRST. This inconsistency meant
   the bot could not access chatwootConversationId/chatwootInboxId
   when processing messages from the Cloud API.

2. chatwootIds assignment: chatwootInboxId and chatwootConversationId
   were both incorrectly set to chatwootSentMessage.id instead of
   .inbox_id and .conversation_id respectively.

Fix: reorder to Chatwoot-first (consistent with Baileys) and use the
correct property names from the Chatwoot response object.
This commit is contained in:
Milton Sosa
2026-02-16 04:30:40 -03:00
parent 42b46e0813
commit cb4a14d1ef
@@ -668,6 +668,21 @@ export class BusinessStartupService extends ChannelStartupService {
sendTelemetry(`received.message.${messageRaw.messageType ?? 'unknown'}`);
// Normalized order: Chatwoot first, then bot (consistent with Baileys channel)
if (this.configService.get<Chatwoot>('CHATWOOT').ENABLED && this.localChatwoot?.enabled) {
const chatwootSentMessage = await this.chatwootService.eventWhatsapp(
Events.MESSAGES_UPSERT,
{ instanceName: this.instance.name, instanceId: this.instanceId },
messageRaw,
);
if (chatwootSentMessage) {
messageRaw.chatwootMessageId = chatwootSentMessage.id;
messageRaw.chatwootInboxId = chatwootSentMessage.inbox_id;
messageRaw.chatwootConversationId = chatwootSentMessage.conversation_id;
}
}
this.sendDataWebhook(Events.MESSAGES_UPSERT, messageRaw);
await chatbotController.emit({
@@ -677,20 +692,6 @@ export class BusinessStartupService extends ChannelStartupService {
pushName: messageRaw.pushName,
});
if (this.configService.get<Chatwoot>('CHATWOOT').ENABLED && this.localChatwoot?.enabled) {
const chatwootSentMessage = await this.chatwootService.eventWhatsapp(
Events.MESSAGES_UPSERT,
{ instanceName: this.instance.name, instanceId: this.instanceId },
messageRaw,
);
if (chatwootSentMessage?.id) {
messageRaw.chatwootMessageId = chatwootSentMessage.id;
messageRaw.chatwootInboxId = chatwootSentMessage.id;
messageRaw.chatwootConversationId = chatwootSentMessage.id;
}
}
if (!this.isMediaMessage(message) && message.type !== 'sticker') {
await this.prismaRepository.message.create({
data: messageRaw,