1ec91e1212ddf6b44d6e9725c462f09effffec01
[motion2.git] / server / api / whatsappInteraction / whatsappInteraction.controller.js
1 // *************************************************************************
2 // *                                                                       *
3 // * xCALLY Motion -  The Omnichannel Contact Center                       *
4 // * Copyright (c) Xenialab s.r.l. All Rights Reserved                     *
5 // *                                                                       *
6 // *************************************************************************
7 // *                                                                       *
8 // * Email: info@xcally.com                                                *
9 // * Website: https://www.xcally.com                                       *
10 // *                                                                       *
11 // *************************************************************************
12 // *                                                                       *
13 // * The SOFTWARE PRODUCT is protected by copyright laws and international *
14 // * copyright treaties, as well as other intellectual property laws and   *
15 // * treaties. The SOFTWARE PRODUCT is licensed, not sold.                         *
16 // *                                                                       *
17 // *************************************************************************
18 var _0x1f13=['isNumeric','i.id\x20LIKE\x20?','i.phone\x20LIKE\x20?','CONCAT(c.firstName,\x20\x27\x20\x27,\x20c.lastName)\x20LIKE\x20?','group','i.id','Sequelize','QueryTypes','clone','SUM(me.read\x20=\x200\x20AND\x20me.direction\x20=\x20\x27in\x27)','unreadMessages','contactName','DESC','t.id\x20IN\x20?','resolve','user','role','agent','getWhatsappAccounts','i.WhatsappAccountId\x20IS\x20NULL','toString','sequelize','keyBy','merge','a.key','Account.key','a.mandatoryDisposition','Account.mandatoryDisposition','CASE\x20WHEN\x20(c.firstName\x20IS\x20NOT\x20NULL\x20AND\x20c.firstName\x20<>\x20\x27\x27)\x20OR\x20(c.lastName\x20IS\x20NOT\x20NULL\x20AND\x20c.lastName\x20<>\x20\x27\x27)\x20THEN\x20CONCAT(IFNULL(c.firstName,\x20\x27\x27),\x20\x27\x20\x27,\x20IFNULL(c.lastName,\x20\x27\x27))\x20ELSE\x20\x27Unknown\x27\x20END','c.firstName','c.lastName','Contact.lastName','c.email','Contact.email','c.phone','Contact.phone','c.mobile','Contact.mobile','c.fax','Contact.fax','o.fullname','Owner.fullname','o.internal','Owner.internal','ui.WhatsappInteractionId\x20IN\x20?','groupBy','WhatsappInteractionId','all','rows','show','params','rawAttributes','includeAll','options','catch','create','destroy','describe','addMessage','WhatsappMessage','getMessages','findOne','hasOwnProperty','pick','include','addTags','ids','omit','spread','findAll','emit','whatsappInteractionTags:save','removeTags','join','server','files','tmp','root','attachments','format','firstName','lastName','Attachment','direction','secret','WhatsappAccount','Account','Owner','get','mkdirSync','Whatsapp','N.A.','YYYY-MM-DD\x20HH:mm:ss','closedAt','Messages','basename','existsSync','createReadStream','pipe','createWriteStream','out','System','renderFile','views','downloadInteraction.html','transcript-','.pdf','createPdfFromHTML','download','log','err','unlinkSync','eml-format','rimraf','zip-dir','fast-json-patch','request-promise','moment','bluebird','mustache','util','path','sox','to-csv','ejs','fs-extra','squel','crypto','jsforce','desk.js','querystring','ioredis','../../components/auth/service','../../components/parsers/qs','../../components/parsers/advancedSearch','../../config/license/hardware','../../config/logger','api','../../config/environment','../../config/license/util','../../mysqldb','redis','defaults','localhost','socket.io-emitter','register','sendStatus','end','status','json','count','offset','limit','set','reject','update','then','error','stack','name','send','Users','fullname','TagIds','Tags','split','forEach','push','map','WhatsappInteraction','fieldName','key','model','query','filters','intersection','attributes','fields','length','nolimit','order','sort','field','color','from','tools_tags','select','u.id','ui.WhatsappInteractionId','left_join','users','u.id\x20=\x20ui.UserId','whatsapp_interactions','cm_contacts','c.id\x20=\x20i.ContactId\x20AND\x20c.deletedAt\x20IS\x20NULL','o.id\x20=\x20i.UserId','whatsapp_accounts','a.id\x20=\x20i.WhatsappAccountId','whatsapp_messages','me.WhatsappInteractionId\x20=\x20i.id','whatsapp_interaction_has_tags','it.WhatsappInteractionId\x20=\x20i.id','expr','search','parseSearch','conditions','value','`\x20=\x200','having','`\x20>\x200','Contact','operator','$substring','MATCH\x20(c.firstName,\x20c.lastName,\x20c.phone,\x20c.mobile,\x20c.fax,\x20c.email)\x20AGAINST\x20(?\x20IN\x20BOOLEAN\x20MODE)','c.firstName\x20LIKE\x20?\x20OR\x20c.lastName\x20LIKE\x20?\x20OR\x20c.email\x20LIKE\x20?','find','ContactId','text','start','Tag','User','CONCAT_WS(\x27\x20\x27,\x20o.name,\x20o.fullname)\x20LIKE\x20?','UserId','buildExpression','type','body','keys','some','compact','isEmpty','WhatsappAccountId','where','i.WhatsappAccountId\x20IN\x20?','concat','createdAt','parse','$gte','$lte','\x20IS\x20NULL','read','`unreadMessages`\x20=\x200','tag','CONCAT(\x27,\x27,\x20`TagIds`,\x20\x27,\x27)\x20LIKE\x20?','filter','replace'];(function(_0x5b30ff,_0x5303eb){var _0x40b976=function(_0x24199c){while(--_0x24199c){_0x5b30ff['push'](_0x5b30ff['shift']());}};_0x40b976(++_0x5303eb);}(_0x1f13,0x70));var _0x31f1=function(_0x51cee2,_0x25a976){_0x51cee2=_0x51cee2-0x0;var _0x12cb54=_0x1f13[_0x51cee2];return _0x12cb54;};'use strict';var emlformat=require(_0x31f1('0x0'));var rimraf=require(_0x31f1('0x1'));var zipdir=require(_0x31f1('0x2'));var jsonpatch=require(_0x31f1('0x3'));var rp=require(_0x31f1('0x4'));var moment=require(_0x31f1('0x5'));var BPromise=require(_0x31f1('0x6'));var Mustache=require(_0x31f1('0x7'));var util=require(_0x31f1('0x8'));var path=require(_0x31f1('0x9'));var sox=require(_0x31f1('0xa'));var csv=require(_0x31f1('0xb'));var ejs=require(_0x31f1('0xc'));var fs=require('fs');var fs_extra=require(_0x31f1('0xd'));var _=require('lodash');var squel=require(_0x31f1('0xe'));var crypto=require(_0x31f1('0xf'));var jsforce=require(_0x31f1('0x10'));var deskjs=require(_0x31f1('0x11'));var toCsv=require(_0x31f1('0xb'));var querystring=require(_0x31f1('0x12'));var Papa=require('papaparse');var Redis=require(_0x31f1('0x13'));var authService=require(_0x31f1('0x14'));var qs=require(_0x31f1('0x15'));var as=require(_0x31f1('0x16'));var pdf=require('../../components/export/pdf');var hardwareService=require(_0x31f1('0x17'));var logger=require(_0x31f1('0x18'))(_0x31f1('0x19'));var utils=require('../../config/utils');var config=require(_0x31f1('0x1a'));var licenseUtil=require(_0x31f1('0x1b'));var db=require(_0x31f1('0x1c'))['db'];config[_0x31f1('0x1d')]=_[_0x31f1('0x1e')](config[_0x31f1('0x1d')],{'host':_0x31f1('0x1f'),'port':0x18eb});var socket=require(_0x31f1('0x20'))(new Redis(config['redis']));require('./whatsappInteraction.socket')[_0x31f1('0x21')](socket);function respondWithStatusCode(_0x4aef29,_0x3a53fe){_0x3a53fe=_0x3a53fe||0xcc;return function(_0x59ba94){if(_0x59ba94){return _0x4aef29[_0x31f1('0x22')](_0x3a53fe);}return _0x4aef29['status'](_0x3a53fe)[_0x31f1('0x23')]();};}function respondWithResult(_0x16fb84,_0x482c26){_0x482c26=_0x482c26||0xc8;return function(_0x4853d8){if(_0x4853d8){return _0x16fb84[_0x31f1('0x24')](_0x482c26)[_0x31f1('0x25')](_0x4853d8);}};}function respondWithFilteredResult(_0xb5dba2,_0x4a8180){return function(_0xbd4a19){if(_0xbd4a19){var _0x5e536c=_0xbd4a19[_0x31f1('0x26')],_0x440bb3=_0x4a8180[_0x31f1('0x27')],_0x32ed53=_0x4a8180[_0x31f1('0x27')]+_0x4a8180[_0x31f1('0x28')],_0x7a3a73;if(_0x32ed53>=_0x5e536c){_0x32ed53=_0x5e536c;_0x7a3a73=0xc8;}else{_0x7a3a73=0xce;}_0xb5dba2[_0x31f1('0x24')](_0x7a3a73);return _0xb5dba2[_0x31f1('0x29')]('Content-Range',_0x440bb3+'-'+_0x32ed53+'/'+_0x5e536c)[_0x31f1('0x25')](_0xbd4a19);}return null;};}function patchUpdates(_0x1d78e0){return function(_0x375e6f){try{jsonpatch['apply'](_0x375e6f,_0x1d78e0,!![]);}catch(_0x258584){return BPromise[_0x31f1('0x2a')](_0x258584);}return _0x375e6f['save']();};}function saveUpdates(_0x9ea9f2,_0x16662d){return function(_0x2f8e31){if(_0x2f8e31){return _0x2f8e31[_0x31f1('0x2b')](_0x9ea9f2)[_0x31f1('0x2c')](function(_0x5ac727){return _0x5ac727;});}return null;};}function removeEntity(_0x19a362,_0x175a47){return function(_0x76935a){if(_0x76935a){return _0x76935a['destroy']()[_0x31f1('0x2c')](function(){_0x19a362['status'](0xcc)['end']();});}};}function handleEntityNotFound(_0xcafa5d,_0x40f5ab){return function(_0x308198){if(!_0x308198){_0xcafa5d['sendStatus'](0x194);}return _0x308198;};}function handleError(_0x287965,_0x4020ae){_0x4020ae=_0x4020ae||0x1f4;return function(_0x511986){logger[_0x31f1('0x2d')](_0x511986[_0x31f1('0x2e')]);if(_0x511986['name']){delete _0x511986[_0x31f1('0x2f')];}_0x287965[_0x31f1('0x24')](_0x4020ae)[_0x31f1('0x30')](_0x511986);};}function getInteractionUsers(_0x24e15d,_0x3b67f8){return new BPromise(function(_0x3c127e,_0x33816f){try{if(_0x3b67f8[_0x24e15d['id']]){_0x24e15d[_0x31f1('0x31')]=_['map'](_0x3b67f8[_0x24e15d['id']],function(_0x554e24){return{'id':_0x554e24['id'],'fullname':_0x554e24[_0x31f1('0x32')]};});}}catch(_0x253a58){_0x33816f(_0x253a58);}_0x3c127e(_0x24e15d);});}function getInteractionTags(_0x2de8c2,_0x3574ce){return new BPromise(function(_0x2ee559,_0x3c7168){try{if(_0x2de8c2[_0x31f1('0x33')]){_0x2de8c2[_0x31f1('0x34')]=[];_0x2de8c2[_0x31f1('0x33')][_0x31f1('0x35')](',')[_0x31f1('0x36')](function(_0x3409c9){_0x2de8c2[_0x31f1('0x34')][_0x31f1('0x37')](_0x3574ce[_0x3409c9]);});}delete _0x2de8c2['TagIds'];}catch(_0xd054d9){_0x3c7168(_0xd054d9);}_0x2ee559(_0x2de8c2);});}function updateWhatsappInteraction(_0x5d8cd9,_0x210777,_0x57fd76){return new BPromise(function(_0x399c9b,_0xf0b25a){return getInteractionUsers(_0x5d8cd9,_0x57fd76)['then'](function(_0x162a29){return getInteractionTags(_0x162a29,_0x210777);})['then'](function(_0x15e721){_0x399c9b(_0x15e721);})['catch'](function(_0x2502b0){_0xf0b25a(_0x2502b0);});});}exports['index']=function(_0x3ac074,_0x53cd37){var _0x12f71f={},_0x1bd2bb={},_0x325857={'count':0x0,'rows':[]};var _0x55a59a=_[_0x31f1('0x38')](db[_0x31f1('0x39')]['rawAttributes'],function(_0x24b857){return{'name':_0x24b857[_0x31f1('0x3a')],'type':_0x24b857['type'][_0x31f1('0x3b')]};});_0x1bd2bb[_0x31f1('0x3c')]=_[_0x31f1('0x38')](_0x55a59a,_0x31f1('0x2f'));_0x1bd2bb[_0x31f1('0x3d')]=_['keys'](_0x3ac074[_0x31f1('0x3d')]);_0x1bd2bb[_0x31f1('0x3e')]=_[_0x31f1('0x3f')](_0x1bd2bb[_0x31f1('0x3c')],_0x1bd2bb['query']);_0x12f71f[_0x31f1('0x40')]=_[_0x31f1('0x3f')](_0x1bd2bb[_0x31f1('0x3c')],qs[_0x31f1('0x41')](_0x3ac074[_0x31f1('0x3d')]['fields']));_0x12f71f[_0x31f1('0x40')]=_0x12f71f[_0x31f1('0x40')][_0x31f1('0x42')]?_0x12f71f[_0x31f1('0x40')]:_0x1bd2bb[_0x31f1('0x3c')];if(!_0x3ac074[_0x31f1('0x3d')]['hasOwnProperty'](_0x31f1('0x43'))){_0x12f71f['limit']=qs[_0x31f1('0x28')](_0x3ac074[_0x31f1('0x3d')][_0x31f1('0x28')]);_0x12f71f[_0x31f1('0x27')]=qs[_0x31f1('0x27')](_0x3ac074[_0x31f1('0x3d')][_0x31f1('0x27')]);}_0x12f71f[_0x31f1('0x44')]=qs[_0x31f1('0x45')](_0x3ac074['query'][_0x31f1('0x45')]);_0x12f71f['where']=qs[_0x31f1('0x3e')](_['pick'](_0x3ac074['query'],_0x1bd2bb['filters']),_0x55a59a);var _0x57194a=[];var _0x7bd528=squel['select']()['field']('id')[_0x31f1('0x46')](_0x31f1('0x2f'))[_0x31f1('0x46')](_0x31f1('0x47'))[_0x31f1('0x48')](_0x31f1('0x49'));var _0x53fc4e=squel[_0x31f1('0x4a')]()[_0x31f1('0x46')](_0x31f1('0x4b'))['field']('u.fullname')[_0x31f1('0x46')](_0x31f1('0x4c'))[_0x31f1('0x48')]('user_has_whatsapp_interactions','ui')[_0x31f1('0x4d')](_0x31f1('0x4e'),'u',_0x31f1('0x4f'));var _0x39f9ac=squel['select']()[_0x31f1('0x48')](_0x31f1('0x50'),'i')[_0x31f1('0x4d')](_0x31f1('0x51'),'c',_0x31f1('0x52'))[_0x31f1('0x4d')](_0x31f1('0x4e'),'o',_0x31f1('0x53'))['left_join'](_0x31f1('0x54'),'a',_0x31f1('0x55'))[_0x31f1('0x4d')](_0x31f1('0x56'),'me',_0x31f1('0x57'))[_0x31f1('0x4d')](_0x31f1('0x58'),'it',_0x31f1('0x59'))[_0x31f1('0x4d')](_0x31f1('0x49'),'t','t.id\x20=\x20it.TagId');var _0x136bd0=squel[_0x31f1('0x5a')]();var _0x2d1a43=[];var _0x14fc8d=squel['expr']();var _0x327050;if(_0x3ac074['query'][_0x31f1('0x5b')]){_0x327050=as[_0x31f1('0x5c')](_0x3ac074[_0x31f1('0x3d')][_0x31f1('0x5b')]);var _0xedb3c=_0x327050['sqlOperator'];for(var _0x37c3dc=0x0;_0x37c3dc<_0x327050[_0x31f1('0x5d')][_0x31f1('0x42')];_0x37c3dc++){var _0x617245=_0x327050[_0x31f1('0x5d')][_0x37c3dc];var _0x45b919='i';var _0x2fe619=_['find'](_0x55a59a,[_0x31f1('0x2f'),_0x617245['field']]);if(!_0x2fe619){switch(_0x617245[_0x31f1('0x46')]){case'unreadMessages':if(_0x617245[_0x31f1('0x5e')]==0x1){_0x39f9ac['having']('`'+_0x617245[_0x31f1('0x46')]+_0x31f1('0x5f'));}else{_0x39f9ac[_0x31f1('0x60')]('`'+_0x617245[_0x31f1('0x46')]+_0x31f1('0x61'));}break;case _0x31f1('0x62'):if(_0x617245[_0x31f1('0x63')]===_0x31f1('0x64')){if(_0x617245[_0x31f1('0x5e')][_0x31f1('0x35')]('\x20')[_0x31f1('0x42')]>0x1){_0x136bd0[_0xedb3c](_0x31f1('0x65'),qs['getFullTextValue'](_0x617245['value']),null);}else{var _0x23cca0='%'+_0x617245[_0x31f1('0x5e')]+'%';_0x136bd0[_0xedb3c](_0x31f1('0x66'),_0x23cca0,_0x23cca0,_0x23cca0);}}else{_0x2fe619=_[_0x31f1('0x67')](_0x55a59a,[_0x31f1('0x2f'),_0x31f1('0x68')]);_0x617245[_0x31f1('0x46')]='ContactId';_0x118b7f=as['buildExpression'](_0x45b919,_0x2fe619['type'],_0x617245);_0x136bd0[_0xedb3c](_0x118b7f[_0x31f1('0x69')],_0x118b7f['value'][_0x31f1('0x6a')],_0x118b7f[_0x31f1('0x5e')][_0x31f1('0x23')]);}break;case _0x31f1('0x6b'):var _0x370b70=_0x617245[_0x31f1('0x5e')]['split'](',')[_0x31f1('0x38')](function(_0x2bd01e){return Number(_0x2bd01e);});_0x370b70[_0x31f1('0x36')](function(_0x4538df){_0x14fc8d['or']('CONCAT(\x27,\x27,\x20`TagIds`,\x20\x27,\x27)\x20LIKE\x20?','%,'+_0x4538df+',%');});_0x2d1a43=_['union'](_0x2d1a43,_0x370b70);break;case _0x31f1('0x6c'):if(_0x617245[_0x31f1('0x63')]==='$substring'){_0x136bd0[_0xedb3c](_0x31f1('0x6d'),'%'+_0x617245[_0x31f1('0x5e')]+'%',null);}else{_0x2fe619=_[_0x31f1('0x67')](_0x55a59a,[_0x31f1('0x2f'),_0x31f1('0x6e')]);_0x617245[_0x31f1('0x46')]=_0x31f1('0x6e');_0x118b7f=as[_0x31f1('0x6f')](_0x45b919,_0x2fe619[_0x31f1('0x70')],_0x617245);_0x136bd0[_0xedb3c](_0x118b7f['text'],_0x118b7f[_0x31f1('0x5e')][_0x31f1('0x6a')],_0x118b7f[_0x31f1('0x5e')][_0x31f1('0x23')]);}break;case _0x31f1('0x71'):_0x118b7f=as[_0x31f1('0x6f')]('me',null,_0x617245);_0x136bd0[_0xedb3c](_0x118b7f[_0x31f1('0x69')],_0x118b7f['value'][_0x31f1('0x6a')],_0x118b7f[_0x31f1('0x5e')][_0x31f1('0x23')]);break;}}else{var _0x118b7f=as[_0x31f1('0x6f')](_0x45b919,_0x2fe619[_0x31f1('0x70')],_0x617245);_0x136bd0[_0xedb3c](_0x118b7f[_0x31f1('0x69')],_0x118b7f['value']['start'],_0x118b7f['value'][_0x31f1('0x23')]);}}}else{var _0x3852b1=_(_0x3ac074['query'])[_0x31f1('0x72')]()[_0x31f1('0x38')](function(_0x17db64){return _[_0x31f1('0x73')](_0x55a59a,[_0x31f1('0x2f'),_0x17db64])?_0x17db64:undefined;})[_0x31f1('0x74')]()[_0x31f1('0x5e')]();if(!_[_0x31f1('0x75')](_0x3852b1)){_0x3852b1[_0x31f1('0x36')](function(_0x111556){if(_0x111556===_0x31f1('0x76')){_0x39f9ac[_0x31f1('0x77')](_0x31f1('0x78'),[][_0x31f1('0x79')](_0x3ac074[_0x31f1('0x3d')][_0x111556]));}else if(_0x111556===_0x31f1('0x7a')){var _0x595652=JSON[_0x31f1('0x7b')](_0x3ac074['query'][_0x111556])[_0x31f1('0x7c')];var _0x4e6669=JSON['parse'](_0x3ac074[_0x31f1('0x3d')][_0x111556])[_0x31f1('0x7d')];_0x39f9ac[_0x31f1('0x77')]('i.createdAt\x20BETWEEN\x20?\x20AND\x20?',_0x595652,_0x4e6669);}else{if(_0x3ac074[_0x31f1('0x3d')][_0x111556]==='null'){_0x39f9ac[_0x31f1('0x77')]('i.'+_0x111556+_0x31f1('0x7e'));}else{_0x39f9ac[_0x31f1('0x77')]('i.'+_0x111556+'\x20=\x20?',_0x3ac074[_0x31f1('0x3d')][_0x111556]);}}});}if(_0x3ac074[_0x31f1('0x3d')]['read']){if(_0x3ac074[_0x31f1('0x3d')][_0x31f1('0x7f')]==0x1){_0x39f9ac[_0x31f1('0x60')](_0x31f1('0x80'));}else{_0x39f9ac['having']('`unreadMessages`\x20>\x200');}}if(_0x3ac074[_0x31f1('0x3d')][_0x31f1('0x81')]){_0x2d1a43=_['isArray'](_0x3ac074[_0x31f1('0x3d')]['tag'])?_0x3ac074[_0x31f1('0x3d')][_0x31f1('0x81')]:new Array(_0x3ac074['query'][_0x31f1('0x81')]);_0x2d1a43[_0x31f1('0x36')](function(_0x4aa393){_0x14fc8d['or'](_0x31f1('0x82'),'%,'+_0x4aa393+',%');});}if(_0x3ac074[_0x31f1('0x3d')][_0x31f1('0x83')]){var _0x6bbe3a=_0x3ac074['query'][_0x31f1('0x83')][_0x31f1('0x84')]('\x5c','\x5c\x5c')[_0x31f1('0x84')](/'/g,'\x27\x27');if(qs[_0x31f1('0x85')](_0x6bbe3a)){_0x136bd0['or'](_0x31f1('0x86'),_0x6bbe3a+'%')['or'](_0x31f1('0x87'),_0x6bbe3a+'%');}else{_0x136bd0['or']('MATCH\x20(me.body)\x20AGAINST\x20(?\x20IN\x20BOOLEAN\x20MODE)',qs['getFullTextValue'](_0x6bbe3a))['or'](_0x31f1('0x88'),_0x6bbe3a+'%');}}}_0x39f9ac[_0x31f1('0x77')](_0x136bd0);_0x39f9ac[_0x31f1('0x89')](_0x31f1('0x8a'));var _0x18a907={'type':db[_0x31f1('0x8b')][_0x31f1('0x8c')]['SELECT'],'raw':!![]};var _0x42f4a4=_0x39f9ac[_0x31f1('0x8d')]();_0x42f4a4[_0x31f1('0x46')](_0x31f1('0x8a'));_0x42f4a4[_0x31f1('0x46')](_0x31f1('0x8e'),_0x31f1('0x8f'));if(_0x12f71f['order']){_0x12f71f['order'][_0x31f1('0x36')](function(_0xf56691){var _0x4606ba=_0xf56691[0x0]===_0x31f1('0x90')?_0xf56691[0x0]:'i.'+_0xf56691[0x0];_0x39f9ac[_0x31f1('0x44')](_0x4606ba,_0xf56691[0x1]===_0x31f1('0x91')?![]:!![]);});}if(!_['isEmpty'](_0x2d1a43)){_0x39f9ac[_0x31f1('0x60')](_0x14fc8d);_0x42f4a4[_0x31f1('0x77')](_0x31f1('0x92'),_0x2d1a43);}BPromise[_0x31f1('0x93')]()[_0x31f1('0x2c')](function(){if(!_0x327050){if(_0x3ac074['query'][_0x31f1('0x76')])return;_0x42f4a4[_0x31f1('0x77')]('i.WhatsappAccountId\x20IS\x20NOT\x20NULL');_0x39f9ac['where']('i.WhatsappAccountId\x20IS\x20NOT\x20NULL');return;}if(_0x3ac074[_0x31f1('0x94')][_0x31f1('0x95')]!==_0x31f1('0x96'))return;return _0x3ac074[_0x31f1('0x94')][_0x31f1('0x97')]({'attributes':['id'],'raw':!![]})['then'](function(_0x545d7b){if(_[_0x31f1('0x75')](_0x545d7b)){_0x42f4a4[_0x31f1('0x77')](_0x31f1('0x98'));_0x39f9ac[_0x31f1('0x77')](_0x31f1('0x98'));}else{_0x42f4a4[_0x31f1('0x77')]('i.WhatsappAccountId\x20IN\x20?',_[_0x31f1('0x38')](_0x545d7b,'id'));_0x39f9ac[_0x31f1('0x77')](_0x31f1('0x78'),_[_0x31f1('0x38')](_0x545d7b,'id'));}});})[_0x31f1('0x2c')](function(){if(_0x3ac074['user'][_0x31f1('0x95')]===_0x31f1('0x96')&&!_0x327050&&!_0x3ac074[_0x31f1('0x3d')]['WhatsappAccountId'])return[];return db['sequelize'][_0x31f1('0x3d')](_0x42f4a4[_0x31f1('0x99')](),_0x18a907);})[_0x31f1('0x2c')](function(_0x41ca8f){_0x325857[_0x31f1('0x26')]=_0x41ca8f['length'];if(_0x325857[_0x31f1('0x26')]===0x0)return[];return db[_0x31f1('0x9a')][_0x31f1('0x3d')](_0x7bd528[_0x31f1('0x99')](),_0x18a907)[_0x31f1('0x2c')](function(_0x3e531f){_0x57194a=_[_0x31f1('0x9b')](_0x3e531f,'id');_0x18a907=_[_0x31f1('0x9c')](_0x18a907,{'model':db['WhatsappInteraction'],'mapToModel':!![],'raw':!![],'nest':!![]});if(_0x3ac074[_0x31f1('0x3d')][_0x31f1('0x41')]){_0x12f71f[_0x31f1('0x40')][_0x31f1('0x36')](function(_0x473135){_0x39f9ac['field']('i.'+_0x473135);});}else{_0x39f9ac[_0x31f1('0x46')](_0x31f1('0x8e'),_0x31f1('0x8f'));_0x39f9ac[_0x31f1('0x46')](_0x31f1('0x9d'),_0x31f1('0x9e'));_0x39f9ac[_0x31f1('0x46')](_0x31f1('0x9f'),_0x31f1('0xa0'));_0x39f9ac[_0x31f1('0x46')](_0x31f1('0xa1'),_0x31f1('0x90'));_0x39f9ac['field']('i.*');_0x39f9ac[_0x31f1('0x46')](_0x31f1('0xa2'),'Contact.firstName');_0x39f9ac[_0x31f1('0x46')](_0x31f1('0xa3'),_0x31f1('0xa4'));_0x39f9ac[_0x31f1('0x46')](_0x31f1('0xa5'),_0x31f1('0xa6'));_0x39f9ac[_0x31f1('0x46')](_0x31f1('0xa7'),_0x31f1('0xa8'));_0x39f9ac[_0x31f1('0x46')](_0x31f1('0xa9'),_0x31f1('0xaa'));_0x39f9ac['field'](_0x31f1('0xab'),_0x31f1('0xac'));_0x39f9ac[_0x31f1('0x46')](_0x31f1('0xad'),_0x31f1('0xae'));_0x39f9ac[_0x31f1('0x46')](_0x31f1('0xaf'),_0x31f1('0xb0'));_0x39f9ac[_0x31f1('0x46')]('GROUP_CONCAT(DISTINCT\x20t.id)',_0x31f1('0x33'));}if(_0x12f71f[_0x31f1('0x28')])_0x39f9ac[_0x31f1('0x28')](_0x12f71f[_0x31f1('0x28')]);if(_0x12f71f[_0x31f1('0x27')])_0x39f9ac[_0x31f1('0x27')](_0x12f71f['offset']);return db['sequelize'][_0x31f1('0x3d')](_0x39f9ac[_0x31f1('0x99')](),_0x18a907);})[_0x31f1('0x2c')](function(_0x342d8f){_0x53fc4e[_0x31f1('0x77')](_0x31f1('0xb1'),_[_0x31f1('0x38')](_0x342d8f,'id'));return db[_0x31f1('0x9a')][_0x31f1('0x3d')](_0x53fc4e[_0x31f1('0x99')](),_0x18a907)['then'](function(_0x53214d){var _0x10b4c1=_[_0x31f1('0xb2')](_0x53214d,_0x31f1('0xb3'));var _0x2d863d=[];_0x342d8f[_0x31f1('0x36')](function(_0xe2379a){_0x2d863d[_0x31f1('0x37')](updateWhatsappInteraction(_0xe2379a,_0x57194a,_0x10b4c1));});return BPromise[_0x31f1('0xb4')](_0x2d863d);});});})[_0x31f1('0x2c')](function(_0x45dce7){_0x325857[_0x31f1('0xb5')]=_0x45dce7;return _0x325857;})[_0x31f1('0x2c')](respondWithFilteredResult(_0x53cd37,_0x12f71f))['catch'](handleError(_0x53cd37,null));};exports[_0x31f1('0xb6')]=function(_0x5d3d91,_0x92c4b8){var _0x105959={'raw':![],'where':{'id':_0x5d3d91[_0x31f1('0xb7')]['id']}},_0x5dd7f4={};_0x5dd7f4[_0x31f1('0x3c')]=_[_0x31f1('0x72')](db['WhatsappInteraction'][_0x31f1('0xb8')]);_0x5dd7f4[_0x31f1('0x3d')]=_[_0x31f1('0x72')](_0x5d3d91[_0x31f1('0x3d')]);_0x5dd7f4[_0x31f1('0x3e')]=_[_0x31f1('0x3f')](_0x5dd7f4[_0x31f1('0x3c')],_0x5dd7f4['query']);_0x105959[_0x31f1('0x40')]=_[_0x31f1('0x3f')](_0x5dd7f4[_0x31f1('0x3c')],qs[_0x31f1('0x41')](_0x5d3d91[_0x31f1('0x3d')][_0x31f1('0x41')]));_0x105959[_0x31f1('0x40')]=_0x105959[_0x31f1('0x40')][_0x31f1('0x42')]?_0x105959[_0x31f1('0x40')]:_0x5dd7f4['model'];if(_0x5d3d91['query'][_0x31f1('0xb9')]){_0x105959['include']=[{'all':!![]}];}_0x105959=_[_0x31f1('0x9c')]({},_0x105959,_0x5d3d91[_0x31f1('0xba')]);return db[_0x31f1('0x39')][_0x31f1('0x67')](_0x105959)['then'](handleEntityNotFound(_0x92c4b8,null))['then'](respondWithResult(_0x92c4b8,null))[_0x31f1('0xbb')](handleError(_0x92c4b8,null));};exports[_0x31f1('0xbc')]=function(_0x4ab9cc,_0x4511bb){return db[_0x31f1('0x39')][_0x31f1('0xbc')](_0x4ab9cc[_0x31f1('0x71')],{})['then'](respondWithResult(_0x4511bb,0xc9))[_0x31f1('0xbb')](handleError(_0x4511bb,null));};exports[_0x31f1('0x2b')]=function(_0x433319,_0x26085c){if(_0x433319[_0x31f1('0x71')]['id']){delete _0x433319[_0x31f1('0x71')]['id'];}return db[_0x31f1('0x39')]['find']({'where':{'id':_0x433319[_0x31f1('0xb7')]['id']}})[_0x31f1('0x2c')](handleEntityNotFound(_0x26085c,null))[_0x31f1('0x2c')](saveUpdates(_0x433319[_0x31f1('0x71')],null))[_0x31f1('0x2c')](respondWithResult(_0x26085c,null))[_0x31f1('0xbb')](handleError(_0x26085c,null));};exports[_0x31f1('0xbd')]=function(_0x3289c3,_0x9cac93){return db[_0x31f1('0x39')][_0x31f1('0x67')]({'where':{'id':_0x3289c3['params']['id']}})[_0x31f1('0x2c')](handleEntityNotFound(_0x9cac93,null))[_0x31f1('0x2c')](removeEntity(_0x9cac93,null))[_0x31f1('0xbb')](handleError(_0x9cac93,null));};exports['describe']=function(_0x28635d,_0x3d835c){return db['WhatsappInteraction'][_0x31f1('0xbe')]()[_0x31f1('0x2c')](respondWithResult(_0x3d835c,null))[_0x31f1('0xbb')](handleError(_0x3d835c,null));};exports[_0x31f1('0xbf')]=function(_0x50967d,_0xd0633b,_0x1d22f2){return db[_0x31f1('0xc0')][_0x31f1('0x67')]({'where':{'id':_0x50967d['params']['id']}})[_0x31f1('0x2c')](handleEntityNotFound(_0xd0633b,null))['then'](function(_0x26fa3a){if(_0x26fa3a){return _0x26fa3a[_0x31f1('0xbf')](_0x50967d[_0x31f1('0x71')]['ids'],_['omit'](_0x50967d[_0x31f1('0x71')],['ids','id'])||{});}})[_0x31f1('0x2c')](respondWithResult(_0xd0633b,null))[_0x31f1('0xbb')](handleError(_0xd0633b,null));};exports[_0x31f1('0xc1')]=function(_0x36a0d4,_0x27f158,_0x5b80d5){var _0x5bd95f={'raw':![],'where':{}};var _0x1f16fe={};var _0x34e25d={'count':0x0,'rows':[]};return db[_0x31f1('0x39')][_0x31f1('0xc2')]({'where':{'id':_0x36a0d4['params']['id']}})[_0x31f1('0x2c')](handleEntityNotFound(_0x27f158,null))[_0x31f1('0x2c')](function(_0x53840e){if(_0x53840e){_0x1f16fe['model']=_['keys'](db[_0x31f1('0xc0')][_0x31f1('0xb8')]);_0x1f16fe[_0x31f1('0x3d')]=_[_0x31f1('0x72')](_0x36a0d4['query']);_0x1f16fe['filters']=_[_0x31f1('0x3f')](_0x1f16fe[_0x31f1('0x3c')],_0x1f16fe[_0x31f1('0x3d')]);_0x5bd95f[_0x31f1('0x40')]=_[_0x31f1('0x3f')](_0x1f16fe[_0x31f1('0x3c')],qs[_0x31f1('0x41')](_0x36a0d4[_0x31f1('0x3d')][_0x31f1('0x41')]));_0x5bd95f['attributes']=_0x5bd95f[_0x31f1('0x40')][_0x31f1('0x42')]?_0x5bd95f[_0x31f1('0x40')]:_0x1f16fe['model'];if(!_0x36a0d4[_0x31f1('0x3d')][_0x31f1('0xc3')]('nolimit')){_0x5bd95f[_0x31f1('0x28')]=qs['limit'](_0x36a0d4[_0x31f1('0x3d')][_0x31f1('0x28')]);_0x5bd95f[_0x31f1('0x27')]=qs[_0x31f1('0x27')](_0x36a0d4[_0x31f1('0x3d')]['offset']);}_0x5bd95f[_0x31f1('0x44')]=qs[_0x31f1('0x45')](_0x36a0d4[_0x31f1('0x3d')][_0x31f1('0x45')]);_0x5bd95f[_0x31f1('0x77')]=qs[_0x31f1('0x3e')](_[_0x31f1('0xc4')](_0x36a0d4[_0x31f1('0x3d')],_0x1f16fe[_0x31f1('0x3e')]));_0x5bd95f[_0x31f1('0x77')][_0x31f1('0xb3')]=_0x53840e['id'];if(_0x36a0d4[_0x31f1('0x3d')]['filter']){_0x5bd95f[_0x31f1('0x77')]=_['merge'](_0x5bd95f['where'],{'$or':_[_0x31f1('0x38')](_0x5bd95f[_0x31f1('0x40')],function(_0x459a26){var _0x3355a7={};_0x3355a7[_0x459a26]={'$like':'%'+_0x36a0d4[_0x31f1('0x3d')][_0x31f1('0x83')]+'%'};return _0x3355a7;})});}if(_0x36a0d4[_0x31f1('0x3d')][_0x31f1('0x7c')]){var _0x5c4f26=_0x36a0d4[_0x31f1('0x3d')][_0x31f1('0x7c')][_0x31f1('0x35')](',');var _0x1a989e={};_0x1a989e[_0x5c4f26[0x0]]={'$gte':moment(_0x5c4f26[0x1])['format']('YYYY-MM-DD\x20HH:mm:ss')};_0x5bd95f[_0x31f1('0x77')]=_['merge'](_0x5bd95f[_0x31f1('0x77')],_0x1a989e);}_0x5bd95f=_['merge']({},_0x5bd95f,_0x36a0d4[_0x31f1('0xba')]);return db['WhatsappMessage'][_0x31f1('0x26')]({'where':_0x5bd95f[_0x31f1('0x77')]})['then'](function(_0x4468e8){_0x34e25d[_0x31f1('0x26')]=_0x4468e8;if(_0x36a0d4[_0x31f1('0x3d')][_0x31f1('0xb9')]){_0x5bd95f[_0x31f1('0xc5')]=[{'all':!![]}];}return db[_0x31f1('0xc0')]['findAll'](_0x5bd95f);})[_0x31f1('0x2c')](function(_0xa61b8){_0x34e25d[_0x31f1('0xb5')]=_0xa61b8;return _0x34e25d;});}})[_0x31f1('0x2c')](respondWithFilteredResult(_0x27f158,_0x5bd95f))[_0x31f1('0xbb')](handleError(_0x27f158,null));};exports[_0x31f1('0xc6')]=function(_0x5b51b3,_0x1a5449,_0xc07a8a){return db[_0x31f1('0x39')][_0x31f1('0x67')]({'where':{'id':_0x5b51b3['params']['id']}})[_0x31f1('0x2c')](handleEntityNotFound(_0x1a5449,null))[_0x31f1('0x2c')](function(_0x323ba2){if(_0x323ba2){return _0x323ba2['setTags'](_0x5b51b3[_0x31f1('0x71')][_0x31f1('0xc7')],_[_0x31f1('0xc8')](_0x5b51b3[_0x31f1('0x71')],[_0x31f1('0xc7'),'id'])||{})[_0x31f1('0xc9')](function(){return db[_0x31f1('0x6b')][_0x31f1('0xca')]({'attributes':['id','name','color'],'where':{'id':_0x5b51b3[_0x31f1('0x71')][_0x31f1('0xc7')]}});})[_0x31f1('0x2c')](function(_0x322386){socket[_0x31f1('0xcb')](_0x31f1('0xcc'),{'id':Number(_0x5b51b3[_0x31f1('0xb7')]['id']),'tags':_0x322386||[]});return{'id':Number(_0x5b51b3['params']['id']),'tags':_0x322386||[]};});}})['then'](respondWithResult(_0x1a5449,null))[_0x31f1('0xbb')](handleError(_0x1a5449,null));};exports[_0x31f1('0xcd')]=function(_0x5e4587,_0x12304c,_0x25d397){return db[_0x31f1('0x39')][_0x31f1('0x67')]({'where':{'id':_0x5e4587[_0x31f1('0xb7')]['id']}})[_0x31f1('0x2c')](handleEntityNotFound(_0x12304c,null))[_0x31f1('0x2c')](function(_0x5408b7){if(_0x5408b7){return _0x5408b7[_0x31f1('0xcd')](_0x5e4587[_0x31f1('0x3d')]['ids']);}})[_0x31f1('0x2c')](respondWithStatusCode(_0x12304c,null))[_0x31f1('0xbb')](handleError(_0x12304c,null));};exports['download']=function(_0x70c3ab,_0x4fec7f){var _0x1599b8=moment()['unix']()[_0x31f1('0x99')]();var _0x588634=path[_0x31f1('0xce')](config['root'],_0x31f1('0xcf'),_0x31f1('0xd0'),_0x31f1('0xd1'));var _0x476e94=path[_0x31f1('0xce')](config[_0x31f1('0xd2')],_0x31f1('0xcf'),_0x31f1('0xd0'),_0x31f1('0xd3'));var _0x230088=path['join'](_0x588634,_0x1599b8);var _0xaf07a7=util[_0x31f1('0xd4')]('transcript-%d-%s.zip',_0x70c3ab[_0x31f1('0xb7')]['id'],_0x1599b8);var _0x2a4a1d=path[_0x31f1('0xce')](_0x588634,_0xaf07a7);var _0x236623=[];_0x236623[_0x31f1('0x37')]({'model':db[_0x31f1('0x6c')],'as':_0x31f1('0x6c'),'attributes':['id','fullname'],'raw':!![]});_0x236623['push']({'model':db['CmContact'],'as':_0x31f1('0x62'),'attributes':['id',_0x31f1('0xd5'),_0x31f1('0xd6')],'raw':!![]});if(_0x70c3ab[_0x31f1('0x3d')][_0x31f1('0xd3')]){_0x236623[_0x31f1('0x37')]({'model':db[_0x31f1('0xd7')],'as':_0x31f1('0xd7'),'raw':!![]});}var _0x469865=[{'model':db[_0x31f1('0xc0')],'as':'Messages','attributes':['id','body','createdAt',_0x31f1('0xd8'),_0x31f1('0xd9'),_0x31f1('0x7f')],'include':_0x236623}];_0x469865[_0x31f1('0x37')]({'model':db[_0x31f1('0xda')],'as':_0x31f1('0xdb'),'attributes':['id',_0x31f1('0x2f')]});_0x469865[_0x31f1('0x37')]({'model':db[_0x31f1('0x6c')],'as':_0x31f1('0xdc'),'attributes':['id',_0x31f1('0x32')]});return db[_0x31f1('0x39')]['find']({'where':{'id':_0x70c3ab[_0x31f1('0xb7')]['id']},'include':_0x469865})[_0x31f1('0x2c')](handleEntityNotFound(_0x4fec7f,null))['then'](function(_0x4c4ade){if(_0x4c4ade){var _0x1323cc=_0x4c4ade[_0x31f1('0xdd')]({'plain':!![]});fs[_0x31f1('0xde')](_0x230088);var _0x28bc13={'channel':_0x31f1('0xdf'),'account':_0x1323cc['Account'][_0x31f1('0x2f')],'agent':_0x1323cc[_0x31f1('0xdc')]?_0x1323cc[_0x31f1('0xdc')][_0x31f1('0x32')]:_0x31f1('0xe0'),'createdAt':moment(_0x1323cc[_0x31f1('0x7a')])[_0x31f1('0xd4')](_0x31f1('0xe1'))[_0x31f1('0x99')](),'closedAt':_0x1323cc['closed']?moment(_0x1323cc[_0x31f1('0xe2')])[_0x31f1('0xd4')](_0x31f1('0xe1'))[_0x31f1('0x99')]():'','disposition':_0x1323cc['disposition'],'messages':_(_0x1323cc[_0x31f1('0xe3')])['filter']({'secret':![]})[_0x31f1('0x38')](function(_0x5c55f8){if(_0x5c55f8[_0x31f1('0xd7')]){var _0x340de8=path['join'](_0x476e94,_0x5c55f8['Attachment'][_0x31f1('0xe4')]);if(fs[_0x31f1('0xe5')](_0x340de8)){fs[_0x31f1('0xe6')](_0x340de8)[_0x31f1('0xe7')](fs[_0x31f1('0xe8')](path[_0x31f1('0xce')](_0x230088,_0x5c55f8[_0x31f1('0xd7')]['basename'])));}}return{'date':moment(_0x5c55f8[_0x31f1('0x7a')])[_0x31f1('0xd4')]('YYYY-MM-DD\x20HH:mm:ss')[_0x31f1('0x99')](),'sender':_0x5c55f8['direction']===_0x31f1('0xe9')?_0x5c55f8['User']?_0x5c55f8[_0x31f1('0x6c')][_0x31f1('0x32')]:_0x31f1('0xea'):_0x5c55f8[_0x31f1('0x62')]['firstName']+(_0x5c55f8[_0x31f1('0x62')][_0x31f1('0xd6')]?'\x20'+_0x5c55f8['Contact']['lastName']:''),'body':_0x5c55f8['Attachment']?_0x5c55f8[_0x31f1('0xd7')][_0x31f1('0xe4')]:_0x5c55f8[_0x31f1('0x71')],'direction':_0x5c55f8[_0x31f1('0xd8')]===_0x31f1('0xe9')?'A':'C','secret':_0x5c55f8['secret'],'read':_0x5c55f8['read'],'attachment':_0x5c55f8[_0x31f1('0xd7')]};})[_0x31f1('0x5e')]()};return ejs[_0x31f1('0xeb')](path['join'](config['root'],'server',_0x31f1('0xec'),_0x31f1('0xed')),{'interaction':_0x28bc13})[_0x31f1('0x2c')](function(_0x4d0ef4){var _0x35d3b3=path[_0x31f1('0xce')](_0x230088,_0x31f1('0xee')+_0x1323cc['id']+'-'+_0x1599b8+_0x31f1('0xef'));var _0x3b8a9d={'path':_0x35d3b3,'channel':_0x31f1('0xdf'),'interactionId':_0x1323cc['id']};return pdf[_0x31f1('0xf0')](_0x4d0ef4,_0x3b8a9d);})[_0x31f1('0x2c')](function(){return new BPromise(function(_0x16013e,_0x5d7e9d){zipdir(_0x230088,{'saveTo':_0x2a4a1d},function(_0x2ef1e9,_0x56564c){if(_0x2ef1e9)return _0x5d7e9d(_0x2ef1e9);return _0x16013e(_0x56564c);});})[_0x31f1('0x2c')](function(){return new BPromise(function(_0x1fad4c,_0xdf0f9e){rimraf(_0x230088,function(_0x1b86c2){if(_0x1b86c2)_0xdf0f9e(_0x1b86c2);return _0x1fad4c();});});})['then'](function(){return _0x4fec7f[_0x31f1('0xf1')](_0x2a4a1d,_0xaf07a7,function(_0x304420){if(_0x304420){console[_0x31f1('0xf2')](_0x31f1('0xf3'),_0x304420);}else{fs[_0x31f1('0xf4')](_0x2a4a1d);}});});});}else{return _0x4fec7f[_0x31f1('0x22')](0xc8);}})[_0x31f1('0xbb')](handleError(_0x4fec7f,null));};