From 28581f88b2083ae09ee37cbaacaef6c64a82449c Mon Sep 17 00:00:00 2001 From: jaison-x Date: Fri, 12 Apr 2024 10:51:36 -0300 Subject: [PATCH] fix(chatwoot): fix conversation read on chatwoot version 3.7 if contact has more than one conversation, we could get the wrong conversation. --- src/whatsapp/services/chatwoot.service.ts | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/src/whatsapp/services/chatwoot.service.ts b/src/whatsapp/services/chatwoot.service.ts index 0fb6272e..d2c1f139 100644 --- a/src/whatsapp/services/chatwoot.service.ts +++ b/src/whatsapp/services/chatwoot.service.ts @@ -1,4 +1,12 @@ -import ChatwootClient, { ChatwootAPIConfig, contact, conversation, generic_id, inbox } from '@figuro/chatwoot-sdk'; +import ChatwootClient, { + ChatwootAPIConfig, + contact, + contact_inboxes, + conversation, + conversation_show, + generic_id, + inbox, +} from '@figuro/chatwoot-sdk'; import { request as chatwootRequest } from '@figuro/chatwoot-sdk/dist/core/request'; import axios from 'axios'; import FormData from 'form-data'; @@ -2130,12 +2138,13 @@ export class ChatwootService { }; if (!sourceId && inbox) { - const contact = (await this.findContact( - instance, - this.getNumberFromRemoteJid(body.key.remoteJid), - )) as contact; - const contactInbox = contact?.contact_inboxes?.find((contactInbox) => contactInbox?.inbox?.id === inbox.id); - sourceId = contactInbox?.source_id; + const conversation = (await client.conversations.get({ + accountId: this.provider.account_id, + conversationId: conversationId, + })) as conversation_show & { + last_non_activity_message: { conversation: { contact_inbox: contact_inboxes } }; + }; + sourceId = conversation.last_non_activity_message?.conversation?.contact_inbox?.source_id; } if (sourceId && inbox?.inbox_identifier) {