From f9726863ef947f80c2378e9439cde56ca5198ec0 Mon Sep 17 00:00:00 2001 From: Judson Cairo Date: Fri, 27 Sep 2024 09:40:18 -0300 Subject: [PATCH] feat: Update chatwoot source ID and status after sent --- .../chatbot/chatwoot/services/chatwoot.service.ts | 4 ++++ .../chatbot/chatwoot/utils/chatwoot-import-helper.ts | 8 ++++++++ 2 files changed, 12 insertions(+) diff --git a/src/api/integrations/chatbot/chatwoot/services/chatwoot.service.ts b/src/api/integrations/chatbot/chatwoot/services/chatwoot.service.ts index fe1c0301..1f370fb1 100644 --- a/src/api/integrations/chatbot/chatwoot/services/chatwoot.service.ts +++ b/src/api/integrations/chatbot/chatwoot/services/chatwoot.service.ts @@ -1453,6 +1453,10 @@ export class ChatwootService { chatwootIsRead: chatwootMessageIds.isRead, }, }); + + if (this.isImportHistoryAvailable()) { + chatwootImport.updateMessageSourceID(chatwootMessageIds.messageId, key.id); + } } private async getMessageByKeyId(instance: InstanceDto, keyId: string): Promise { diff --git a/src/api/integrations/chatbot/chatwoot/utils/chatwoot-import-helper.ts b/src/api/integrations/chatbot/chatwoot/utils/chatwoot-import-helper.ts index 8f1ae580..a486eab0 100644 --- a/src/api/integrations/chatbot/chatwoot/utils/chatwoot-import-helper.ts +++ b/src/api/integrations/chatbot/chatwoot/utils/chatwoot-import-helper.ts @@ -542,6 +542,14 @@ class ChatwootImport { public isIgnorePhoneNumber(remoteJid: string) { return this.isGroup(remoteJid) || remoteJid === 'status@broadcast' || remoteJid === '0@s.whatsapp.net'; } + + public updateMessageSourceID(messageId: string | number, sourceId: string) { + const pgClient = postgresClient.getChatwootConnection(); + + const sql = `UPDATE messages SET source_id = $1, status = 0 WHERE id = $2;`; + + return pgClient.query(sql, [`WAID:${sourceId}`, messageId]); + } } export const chatwootImport = new ChatwootImport();