@@ -14,6 +14,7 @@ import { InstanceDto } from '../dto/instance.dto';
1414import { Options , Quoted , SendAudioDto , SendMediaDto , SendTextDto } from '../dto/sendMessage.dto' ;
1515import { MessageRaw } from '../models' ;
1616import { RepositoryBroker } from '../repository/repository.manager' ;
17+ import { Events } from '../types/wa.types' ;
1718import { WAMonitoringService } from './monitor.service' ;
1819
1920export class ChatwootService {
@@ -1036,7 +1037,9 @@ export class ChatwootService {
10361037 const message = await this . repository . message . find ( {
10371038 where : {
10381039 owner : instance . instanceName ,
1039- chatwootMessageId : body . id ,
1040+ chatwoot : {
1041+ messageId : body . id ,
1042+ } ,
10401043 } ,
10411044 limit : 1 ,
10421045 } ) ;
@@ -1160,7 +1163,11 @@ export class ChatwootService {
11601163 ...messageSent ,
11611164 owner : instance . instanceName ,
11621165 } ,
1163- body . id ,
1166+ {
1167+ messageId : body . id ,
1168+ inboxId : body . inbox ?. id ,
1169+ conversationId : body . conversation ?. id ,
1170+ } ,
11641171 instance ,
11651172 ) ;
11661173 }
@@ -1187,7 +1194,11 @@ export class ChatwootService {
11871194 ...messageSent ,
11881195 owner : instance . instanceName ,
11891196 } ,
1190- body . id ,
1197+ {
1198+ messageId : body . id ,
1199+ inboxId : body . inbox ?. id ,
1200+ conversationId : body . conversation ?. id ,
1201+ } ,
11911202 instance ,
11921203 ) ;
11931204 }
@@ -1221,15 +1232,33 @@ export class ChatwootService {
12211232 }
12221233 }
12231234
1224- private updateChatwootMessageId ( message : MessageRaw , chatwootMessageId : string , instance : InstanceDto ) {
1225- if ( ! chatwootMessageId || ! message ?. key ?. id ) {
1235+ private updateChatwootMessageId (
1236+ message : MessageRaw ,
1237+ chatwootMessageIds : MessageRaw [ 'chatwoot' ] ,
1238+ instance : InstanceDto ,
1239+ ) {
1240+ if ( ! chatwootMessageIds . messageId || ! message ?. key ?. id ) {
12261241 return ;
12271242 }
12281243
1229- message . chatwootMessageId = chatwootMessageId ;
1244+ message . chatwoot = chatwootMessageIds ;
12301245 this . repository . message . update ( [ message ] , instance . instanceName , true ) ;
12311246 }
12321247
1248+ private async getMessageByKeyId ( instance : InstanceDto , keyId : string ) : Promise < MessageRaw > {
1249+ const messages = await this . repository . message . find ( {
1250+ where : {
1251+ key : {
1252+ id : keyId ,
1253+ } ,
1254+ owner : instance . instanceName ,
1255+ } ,
1256+ limit : 1 ,
1257+ } ) ;
1258+
1259+ return messages . length ? messages [ 0 ] : null ;
1260+ }
1261+
12331262 private async getReplyToIds (
12341263 msg : any ,
12351264 instance : InstanceDto ,
@@ -1240,17 +1269,9 @@ export class ChatwootService {
12401269 if ( msg ) {
12411270 inReplyToExternalId = msg . message ?. extendedTextMessage ?. contextInfo ?. stanzaId ;
12421271 if ( inReplyToExternalId ) {
1243- const message = await this . repository . message . find ( {
1244- where : {
1245- key : {
1246- id : inReplyToExternalId ,
1247- } ,
1248- owner : instance . instanceName ,
1249- } ,
1250- limit : 1 ,
1251- } ) ;
1252- if ( message . length && message [ 0 ] ?. chatwootMessageId ) {
1253- inReplyTo = message [ 0 ] . chatwootMessageId ;
1272+ const message = await this . getMessageByKeyId ( instance , inReplyToExternalId ) ;
1273+ if ( message ?. chatwoot ?. messageId ) {
1274+ inReplyTo = message . chatwoot . messageId ;
12541275 }
12551276 }
12561277 }
@@ -1265,7 +1286,9 @@ export class ChatwootService {
12651286 if ( msg ?. content_attributes ?. in_reply_to ) {
12661287 const message = await this . repository . message . find ( {
12671288 where : {
1268- chatwootMessageId : msg ?. content_attributes ?. in_reply_to ,
1289+ chatwoot : {
1290+ messageId : msg ?. content_attributes ?. in_reply_to ,
1291+ } ,
12691292 owner : instance . instanceName ,
12701293 } ,
12711294 limit : 1 ,
@@ -1757,6 +1780,25 @@ export class ChatwootService {
17571780 }
17581781 }
17591782
1783+ if ( event === Events . MESSAGES_DELETE ) {
1784+ this . logger . verbose ( 'deleting message from instance: ' + instance . instanceName ) ;
1785+
1786+ if ( ! body ?. key ?. id ) {
1787+ this . logger . warn ( 'message id not found' ) ;
1788+ return ;
1789+ }
1790+
1791+ const message = await this . getMessageByKeyId ( instance , body . key . id ) ;
1792+ if ( message ?. chatwoot ?. messageId && message ?. chatwoot ?. conversationId ) {
1793+ this . logger . verbose ( 'deleting message in chatwoot. Message id: ' + body . key . id ) ;
1794+ return await client . messages . delete ( {
1795+ accountId : this . provider . account_id ,
1796+ conversationId : message . chatwoot . conversationId ,
1797+ messageId : message . chatwoot . messageId ,
1798+ } ) ;
1799+ }
1800+ }
1801+
17601802 if ( event === 'status.instance' ) {
17611803 this . logger . verbose ( 'event status.instance' ) ;
17621804 const data = body ;
0 commit comments