bafc9e54cdf2383176bfa2e1bcb1429f10c07510
[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 _0xd56e=['type','key','query','filters','intersection','attributes','model','fields','length','nolimit','order','sort','where','pick','select','field','color','tools_tags','u.fullname','ui.WhatsappInteractionId','from','left_join','whatsapp_interactions','cm_contacts','c.id\x20=\x20i.ContactId\x20AND\x20c.deletedAt\x20IS\x20NULL','users','o.id\x20=\x20i.UserId','whatsapp_accounts','a.id\x20=\x20i.WhatsappAccountId','whatsapp_messages','whatsapp_interaction_has_tags','it.WhatsappInteractionId\x20=\x20i.id','t.id\x20=\x20it.TagId','search','sqlOperator','conditions','unreadMessages','value','having','`\x20=\x200','`\x20>\x200','Contact','operator','$substring','MATCH\x20(c.firstName,\x20c.lastName,\x20c.phone,\x20c.mobile,\x20c.fax,\x20c.email)\x20AGAINST\x20(?\x20IN\x20BOOLEAN\x20MODE)','getFullTextValue','c.firstName\x20LIKE\x20?\x20OR\x20c.lastName\x20LIKE\x20?\x20OR\x20c.email\x20LIKE\x20?','find','ContactId','text','start','end','union','User','CONCAT_WS(\x27\x20\x27,\x20o.name,\x20o.fullname)\x20LIKE\x20?','UserId','buildExpression','body','keys','compact','WhatsappAccountId','i.WhatsappAccountId\x20IN\x20?','concat','createdAt','parse','i.createdAt\x20BETWEEN\x20?\x20AND\x20?','null','\x20=\x20?','read','`unreadMessages`\x20=\x200','`unreadMessages`\x20>\x200','isArray','tag','CONCAT(\x27,\x27,\x20`TagIds`,\x20\x27,\x27)\x20LIKE\x20?','filter','replace','isNumeric','i.id\x20LIKE\x20?','MATCH\x20(me.body)\x20AGAINST\x20(?\x20IN\x20BOOLEAN\x20MODE)','CONCAT(c.firstName,\x20\x27\x20\x27,\x20c.lastName)\x20LIKE\x20?','group','i.id','Sequelize','QueryTypes','SELECT','clone','SUM(me.read\x20=\x200\x20AND\x20me.direction\x20=\x20\x27in\x27)','contactName','DESC','isEmpty','t.id\x20IN\x20?','resolve','i.WhatsappAccountId\x20IS\x20NOT\x20NULL','user','role','agent','getWhatsappAccounts','i.WhatsappAccountId\x20IS\x20NULL','sequelize','toString','keyBy','merge','a.key','Account.key','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','Contact.firstName','Contact.lastName','c.email','Contact.email','c.phone','c.mobile','c.fax','Contact.fax','o.fullname','Owner.internal','ui.WhatsappInteractionId\x20IN\x20?','groupBy','WhatsappInteractionId','all','rows','includeAll','include','options','create','params','WhatsappMessage','addMessage','ids','omit','findOne','hasOwnProperty','$gte','format','YYYY-MM-DD\x20HH:mm:ss','findAll','spread','Tag','emit','removeTags','download','join','server','files','tmp','root','attachments','transcript-%d-%s.zip','CmContact','firstName','lastName','Attachment','Messages','direction','secret','WhatsappAccount','Account','Owner','mkdirSync','Whatsapp','N.A.','closed','closedAt','basename','createReadStream','createWriteStream','out','System','renderFile','views','downloadInteraction.html','createPdfFromHTML','log','unlinkSync','eml-format','rimraf','zip-dir','request-promise','bluebird','mustache','path','sox','to-csv','fs-extra','lodash','squel','crypto','jsforce','desk.js','querystring','papaparse','ioredis','../../components/auth/service','../../components/export/pdf','../../config/logger','api','../../config/utils','../../config/environment','../../config/license/util','redis','defaults','localhost','socket.io-emitter','status','offset','limit','count','Content-Range','apply','reject','then','destroy','sendStatus','name','map','fullname','TagIds','split','forEach','Tags','push','catch','WhatsappInteraction','rawAttributes'];(function(_0x426711,_0x3451b3){var _0x309fc3=function(_0x2156b5){while(--_0x2156b5){_0x426711['push'](_0x426711['shift']());}};_0x309fc3(++_0x3451b3);}(_0xd56e,0xae));var _0xed56=function(_0x53e36c,_0x5adedd){_0x53e36c=_0x53e36c-0x0;var _0x1fc47d=_0xd56e[_0x53e36c];return _0x1fc47d;};'use strict';var emlformat=require(_0xed56('0x0'));var rimraf=require(_0xed56('0x1'));var zipdir=require(_0xed56('0x2'));var jsonpatch=require('fast-json-patch');var rp=require(_0xed56('0x3'));var moment=require('moment');var BPromise=require(_0xed56('0x4'));var Mustache=require(_0xed56('0x5'));var util=require('util');var path=require(_0xed56('0x6'));var sox=require(_0xed56('0x7'));var csv=require(_0xed56('0x8'));var ejs=require('ejs');var fs=require('fs');var fs_extra=require(_0xed56('0x9'));var _=require(_0xed56('0xa'));var squel=require(_0xed56('0xb'));var crypto=require(_0xed56('0xc'));var jsforce=require(_0xed56('0xd'));var deskjs=require(_0xed56('0xe'));var toCsv=require(_0xed56('0x8'));var querystring=require(_0xed56('0xf'));var Papa=require(_0xed56('0x10'));var Redis=require(_0xed56('0x11'));var authService=require(_0xed56('0x12'));var qs=require('../../components/parsers/qs');var as=require('../../components/parsers/advancedSearch');var pdf=require(_0xed56('0x13'));var hardwareService=require('../../config/license/hardware');var logger=require(_0xed56('0x14'))(_0xed56('0x15'));var utils=require(_0xed56('0x16'));var config=require(_0xed56('0x17'));var licenseUtil=require(_0xed56('0x18'));var db=require('../../mysqldb')['db'];config[_0xed56('0x19')]=_[_0xed56('0x1a')](config[_0xed56('0x19')],{'host':_0xed56('0x1b'),'port':0x18eb});var socket=require(_0xed56('0x1c'))(new Redis(config[_0xed56('0x19')]));require('./whatsappInteraction.socket')['register'](socket);function respondWithStatusCode(_0x4d108b,_0x4fb5bf){_0x4fb5bf=_0x4fb5bf||0xcc;return function(_0xf2fdc7){if(_0xf2fdc7){return _0x4d108b['sendStatus'](_0x4fb5bf);}return _0x4d108b['status'](_0x4fb5bf)['end']();};}function respondWithResult(_0x183685,_0x33a1de){_0x33a1de=_0x33a1de||0xc8;return function(_0x14f4ef){if(_0x14f4ef){return _0x183685[_0xed56('0x1d')](_0x33a1de)['json'](_0x14f4ef);}};}function respondWithFilteredResult(_0x1f7862,_0x454c76){return function(_0x2420c9){if(_0x2420c9){var _0x4ef537=typeof _0x454c76[_0xed56('0x1e')]==='undefined'&&typeof _0x454c76[_0xed56('0x1f')]==='undefined';var _0x3a15bf=_0x2420c9['count'];var _0x51d95c=_0x4ef537?0x0:_0x454c76[_0xed56('0x1e')];var _0x4aca71=_0x4ef537?_0x2420c9[_0xed56('0x20')]:_0x454c76['offset']+_0x454c76[_0xed56('0x1f')];var _0x4926fd;if(_0x4aca71>=_0x3a15bf){_0x4aca71=_0x3a15bf;_0x4926fd=0xc8;}else{_0x4926fd=0xce;}_0x1f7862[_0xed56('0x1d')](_0x4926fd);return _0x1f7862['set'](_0xed56('0x21'),_0x51d95c+'-'+_0x4aca71+'/'+_0x3a15bf)['json'](_0x2420c9);}return null;};}function patchUpdates(_0x472e09){return function(_0xa560d4){try{jsonpatch[_0xed56('0x22')](_0xa560d4,_0x472e09,!![]);}catch(_0x164650){return BPromise[_0xed56('0x23')](_0x164650);}return _0xa560d4['save']();};}function saveUpdates(_0x57a395,_0xd135b2){return function(_0x3ebb4b){if(_0x3ebb4b){return _0x3ebb4b['update'](_0x57a395)[_0xed56('0x24')](function(_0x604c52){return _0x604c52;});}return null;};}function removeEntity(_0x55fdae,_0x232b88){return function(_0x1cf3df){if(_0x1cf3df){return _0x1cf3df[_0xed56('0x25')]()[_0xed56('0x24')](function(){_0x55fdae[_0xed56('0x1d')](0xcc)['end']();});}};}function handleEntityNotFound(_0x5463e6,_0x4f4751){return function(_0x490aba){if(!_0x490aba){_0x5463e6[_0xed56('0x26')](0x194);}return _0x490aba;};}function handleError(_0x3d9f52,_0x122e8d){_0x122e8d=_0x122e8d||0x1f4;return function(_0x28abf1){logger['error'](_0x28abf1['stack']);if(_0x28abf1[_0xed56('0x27')]){delete _0x28abf1[_0xed56('0x27')];}_0x3d9f52[_0xed56('0x1d')](_0x122e8d)['send'](_0x28abf1);};}function getInteractionUsers(_0x2e62fd,_0x48255b){return new BPromise(function(_0x2fbbbe,_0x34c94a){try{if(_0x48255b[_0x2e62fd['id']]){_0x2e62fd['Users']=_[_0xed56('0x28')](_0x48255b[_0x2e62fd['id']],function(_0x3fd7eb){return{'id':_0x3fd7eb['id'],'fullname':_0x3fd7eb[_0xed56('0x29')]};});}}catch(_0x1dd7a7){_0x34c94a(_0x1dd7a7);}_0x2fbbbe(_0x2e62fd);});}function getInteractionTags(_0x184813,_0x55efda){return new BPromise(function(_0x2cdd25,_0x1757e2){try{if(_0x184813[_0xed56('0x2a')]){_0x184813['Tags']=[];_0x184813['TagIds'][_0xed56('0x2b')](',')[_0xed56('0x2c')](function(_0x55f33f){_0x184813[_0xed56('0x2d')][_0xed56('0x2e')](_0x55efda[_0x55f33f]);});}delete _0x184813[_0xed56('0x2a')];}catch(_0x5a7824){_0x1757e2(_0x5a7824);}_0x2cdd25(_0x184813);});}function updateWhatsappInteraction(_0x5f0a1d,_0x1d7935,_0x55480b){return new BPromise(function(_0xbdf722,_0x26d8f8){return getInteractionUsers(_0x5f0a1d,_0x55480b)['then'](function(_0x1a159d){return getInteractionTags(_0x1a159d,_0x1d7935);})[_0xed56('0x24')](function(_0xe26b13){_0xbdf722(_0xe26b13);})[_0xed56('0x2f')](function(_0x555bae){_0x26d8f8(_0x555bae);});});}exports['index']=function(_0x4ecbda,_0x48b649){var _0x1bc32e={},_0x2f8486={},_0x204860={'count':0x0,'rows':[]};var _0x2c2bb3=_[_0xed56('0x28')](db[_0xed56('0x30')][_0xed56('0x31')],function(_0x37d245){return{'name':_0x37d245['fieldName'],'type':_0x37d245[_0xed56('0x32')][_0xed56('0x33')]};});_0x2f8486['model']=_[_0xed56('0x28')](_0x2c2bb3,_0xed56('0x27'));_0x2f8486[_0xed56('0x34')]=_['keys'](_0x4ecbda[_0xed56('0x34')]);_0x2f8486[_0xed56('0x35')]=_[_0xed56('0x36')](_0x2f8486['model'],_0x2f8486[_0xed56('0x34')]);_0x1bc32e[_0xed56('0x37')]=_['intersection'](_0x2f8486[_0xed56('0x38')],qs[_0xed56('0x39')](_0x4ecbda[_0xed56('0x34')][_0xed56('0x39')]));_0x1bc32e[_0xed56('0x37')]=_0x1bc32e[_0xed56('0x37')][_0xed56('0x3a')]?_0x1bc32e[_0xed56('0x37')]:_0x2f8486[_0xed56('0x38')];if(!_0x4ecbda[_0xed56('0x34')]['hasOwnProperty'](_0xed56('0x3b'))){_0x1bc32e[_0xed56('0x1f')]=qs[_0xed56('0x1f')](_0x4ecbda[_0xed56('0x34')][_0xed56('0x1f')]);_0x1bc32e['offset']=qs['offset'](_0x4ecbda[_0xed56('0x34')][_0xed56('0x1e')]);}_0x1bc32e[_0xed56('0x3c')]=qs[_0xed56('0x3d')](_0x4ecbda[_0xed56('0x34')]['sort']);_0x1bc32e[_0xed56('0x3e')]=qs[_0xed56('0x35')](_[_0xed56('0x3f')](_0x4ecbda[_0xed56('0x34')],_0x2f8486[_0xed56('0x35')]),_0x2c2bb3);var _0x26f8a9=[];var _0x492a80=squel[_0xed56('0x40')]()['field']('id')[_0xed56('0x41')]('name')[_0xed56('0x41')](_0xed56('0x42'))['from'](_0xed56('0x43'));var _0xa2ca50=squel[_0xed56('0x40')]()[_0xed56('0x41')]('u.id')[_0xed56('0x41')](_0xed56('0x44'))[_0xed56('0x41')](_0xed56('0x45'))[_0xed56('0x46')]('user_has_whatsapp_interactions','ui')[_0xed56('0x47')]('users','u','u.id\x20=\x20ui.UserId');var _0x345e35=squel[_0xed56('0x40')]()[_0xed56('0x46')](_0xed56('0x48'),'i')[_0xed56('0x47')](_0xed56('0x49'),'c',_0xed56('0x4a'))['left_join'](_0xed56('0x4b'),'o',_0xed56('0x4c'))[_0xed56('0x47')](_0xed56('0x4d'),'a',_0xed56('0x4e'))[_0xed56('0x47')](_0xed56('0x4f'),'me','me.WhatsappInteractionId\x20=\x20i.id')[_0xed56('0x47')](_0xed56('0x50'),'it',_0xed56('0x51'))[_0xed56('0x47')](_0xed56('0x43'),'t',_0xed56('0x52'));var _0x189461=squel['expr']();var _0x29b58d=[];var _0x5b555e=squel['expr']();var _0x4a6a5b;if(_0x4ecbda[_0xed56('0x34')][_0xed56('0x53')]){_0x4a6a5b=as['parseSearch'](_0x4ecbda[_0xed56('0x34')][_0xed56('0x53')]);var _0x37c827=_0x4a6a5b[_0xed56('0x54')];for(var _0x3c3d18=0x0;_0x3c3d18<_0x4a6a5b[_0xed56('0x55')]['length'];_0x3c3d18++){var _0x46bf39=_0x4a6a5b[_0xed56('0x55')][_0x3c3d18];var _0x35d86a='i';var _0x36f6f8=_['find'](_0x2c2bb3,[_0xed56('0x27'),_0x46bf39['field']]);if(!_0x36f6f8){switch(_0x46bf39[_0xed56('0x41')]){case _0xed56('0x56'):if(_0x46bf39[_0xed56('0x57')]==0x1){_0x345e35[_0xed56('0x58')]('`'+_0x46bf39[_0xed56('0x41')]+_0xed56('0x59'));}else{_0x345e35[_0xed56('0x58')]('`'+_0x46bf39[_0xed56('0x41')]+_0xed56('0x5a'));}break;case _0xed56('0x5b'):if(_0x46bf39[_0xed56('0x5c')]===_0xed56('0x5d')){if(_0x46bf39[_0xed56('0x57')][_0xed56('0x2b')]('\x20')['length']>0x1){_0x189461[_0x37c827](_0xed56('0x5e'),qs[_0xed56('0x5f')](_0x46bf39['value']),null);}else{var _0x25377b='%'+_0x46bf39[_0xed56('0x57')]+'%';_0x189461[_0x37c827](_0xed56('0x60'),_0x25377b,_0x25377b,_0x25377b);}}else{_0x36f6f8=_[_0xed56('0x61')](_0x2c2bb3,[_0xed56('0x27'),'ContactId']);_0x46bf39[_0xed56('0x41')]=_0xed56('0x62');_0x860514=as['buildExpression'](_0x35d86a,_0x36f6f8[_0xed56('0x32')],_0x46bf39);_0x189461[_0x37c827](_0x860514[_0xed56('0x63')],_0x860514['value'][_0xed56('0x64')],_0x860514['value'][_0xed56('0x65')]);}break;case'Tag':var _0x71018b=_0x46bf39[_0xed56('0x57')]['split'](',')['map'](function(_0x28ee18){return Number(_0x28ee18);});_0x71018b[_0xed56('0x2c')](function(_0x48dd42){_0x5b555e['or']('CONCAT(\x27,\x27,\x20`TagIds`,\x20\x27,\x27)\x20LIKE\x20?','%,'+_0x48dd42+',%');});_0x29b58d=_[_0xed56('0x66')](_0x29b58d,_0x71018b);break;case _0xed56('0x67'):if(_0x46bf39[_0xed56('0x5c')]===_0xed56('0x5d')){_0x189461[_0x37c827](_0xed56('0x68'),'%'+_0x46bf39[_0xed56('0x57')]+'%',null);}else{_0x36f6f8=_[_0xed56('0x61')](_0x2c2bb3,[_0xed56('0x27'),'UserId']);_0x46bf39[_0xed56('0x41')]=_0xed56('0x69');_0x860514=as[_0xed56('0x6a')](_0x35d86a,_0x36f6f8[_0xed56('0x32')],_0x46bf39);_0x189461[_0x37c827](_0x860514['text'],_0x860514['value'][_0xed56('0x64')],_0x860514[_0xed56('0x57')]['end']);}break;case _0xed56('0x6b'):_0x860514=as[_0xed56('0x6a')]('me',null,_0x46bf39);_0x189461[_0x37c827](_0x860514[_0xed56('0x63')],_0x860514[_0xed56('0x57')][_0xed56('0x64')],_0x860514[_0xed56('0x57')]['end']);break;}}else{var _0x860514=as[_0xed56('0x6a')](_0x35d86a,_0x36f6f8[_0xed56('0x32')],_0x46bf39);_0x189461[_0x37c827](_0x860514['text'],_0x860514[_0xed56('0x57')][_0xed56('0x64')],_0x860514[_0xed56('0x57')][_0xed56('0x65')]);}}}else{var _0xa4eceb=_(_0x4ecbda['query'])[_0xed56('0x6c')]()[_0xed56('0x28')](function(_0x26e5f6){return _['some'](_0x2c2bb3,[_0xed56('0x27'),_0x26e5f6])?_0x26e5f6:undefined;})[_0xed56('0x6d')]()[_0xed56('0x57')]();if(!_['isEmpty'](_0xa4eceb)){_0xa4eceb[_0xed56('0x2c')](function(_0x26a787){if(_0x26a787===_0xed56('0x6e')){_0x345e35[_0xed56('0x3e')](_0xed56('0x6f'),[][_0xed56('0x70')](_0x4ecbda['query'][_0x26a787]));}else if(_0x26a787===_0xed56('0x71')){var _0x1acef4=JSON[_0xed56('0x72')](_0x4ecbda[_0xed56('0x34')][_0x26a787])['$gte'];var _0x4aeac3=JSON[_0xed56('0x72')](_0x4ecbda[_0xed56('0x34')][_0x26a787])['$lte'];_0x345e35['where'](_0xed56('0x73'),_0x1acef4,_0x4aeac3);}else{if(_0x4ecbda[_0xed56('0x34')][_0x26a787]===_0xed56('0x74')){_0x345e35[_0xed56('0x3e')]('i.'+_0x26a787+'\x20IS\x20NULL');}else{_0x345e35['where']('i.'+_0x26a787+_0xed56('0x75'),_0x4ecbda['query'][_0x26a787]);}}});}if(_0x4ecbda[_0xed56('0x34')][_0xed56('0x76')]){if(_0x4ecbda[_0xed56('0x34')]['read']==0x1){_0x345e35[_0xed56('0x58')](_0xed56('0x77'));}else{_0x345e35[_0xed56('0x58')](_0xed56('0x78'));}}if(_0x4ecbda[_0xed56('0x34')]['tag']){_0x29b58d=_[_0xed56('0x79')](_0x4ecbda[_0xed56('0x34')]['tag'])?_0x4ecbda[_0xed56('0x34')][_0xed56('0x7a')]:new Array(_0x4ecbda['query'][_0xed56('0x7a')]);_0x29b58d['forEach'](function(_0x2b1fda){_0x5b555e['or'](_0xed56('0x7b'),'%,'+_0x2b1fda+',%');});}if(_0x4ecbda['query'][_0xed56('0x7c')]){var _0x2a6476=_0x4ecbda[_0xed56('0x34')][_0xed56('0x7c')][_0xed56('0x7d')]('\x5c','\x5c\x5c')[_0xed56('0x7d')](/'/g,'\x27\x27');if(qs[_0xed56('0x7e')](_0x2a6476)){_0x189461['or'](_0xed56('0x7f'),_0x2a6476+'%')['or']('i.phone\x20LIKE\x20?',_0x2a6476+'%');}else{_0x189461['or'](_0xed56('0x80'),qs[_0xed56('0x5f')](_0x2a6476))['or'](_0xed56('0x81'),_0x2a6476+'%');}}}_0x345e35[_0xed56('0x3e')](_0x189461);_0x345e35[_0xed56('0x82')](_0xed56('0x83'));var _0x5c3b0f={'type':db[_0xed56('0x84')][_0xed56('0x85')][_0xed56('0x86')],'raw':!![]};var _0x11525a=_0x345e35[_0xed56('0x87')]();_0x11525a[_0xed56('0x41')](_0xed56('0x83'));_0x11525a[_0xed56('0x41')](_0xed56('0x88'),'unreadMessages');if(_0x1bc32e[_0xed56('0x3c')]){_0x1bc32e[_0xed56('0x3c')]['forEach'](function(_0x69b0e2){var _0x1d2790=_0x69b0e2[0x0]===_0xed56('0x89')?_0x69b0e2[0x0]:'i.'+_0x69b0e2[0x0];_0x345e35[_0xed56('0x3c')](_0x1d2790,_0x69b0e2[0x1]===_0xed56('0x8a')?![]:!![]);});}if(!_[_0xed56('0x8b')](_0x29b58d)){_0x345e35[_0xed56('0x58')](_0x5b555e);_0x11525a['where'](_0xed56('0x8c'),_0x29b58d);}BPromise[_0xed56('0x8d')]()[_0xed56('0x24')](function(){if(!_0x4a6a5b){if(_0x4ecbda[_0xed56('0x34')][_0xed56('0x6e')])return;_0x11525a[_0xed56('0x3e')](_0xed56('0x8e'));_0x345e35[_0xed56('0x3e')](_0xed56('0x8e'));return;}if(_0x4ecbda[_0xed56('0x8f')][_0xed56('0x90')]!==_0xed56('0x91'))return;return _0x4ecbda['user'][_0xed56('0x92')]({'attributes':['id'],'raw':!![]})[_0xed56('0x24')](function(_0xaf9bfb){if(_[_0xed56('0x8b')](_0xaf9bfb)){_0x11525a['where'](_0xed56('0x93'));_0x345e35['where'](_0xed56('0x93'));}else{_0x11525a[_0xed56('0x3e')]('i.WhatsappAccountId\x20IN\x20?',_['map'](_0xaf9bfb,'id'));_0x345e35['where']('i.WhatsappAccountId\x20IN\x20?',_['map'](_0xaf9bfb,'id'));}});})[_0xed56('0x24')](function(){if(_0x4ecbda[_0xed56('0x8f')][_0xed56('0x90')]==='agent'&&!_0x4a6a5b&&!_0x4ecbda[_0xed56('0x34')][_0xed56('0x6e')])return[];return db[_0xed56('0x94')][_0xed56('0x34')](_0x11525a['toString'](),_0x5c3b0f);})['then'](function(_0x2a9ac7){_0x204860[_0xed56('0x20')]=_0x2a9ac7[_0xed56('0x3a')];if(_0x204860[_0xed56('0x20')]===0x0)return[];return db[_0xed56('0x94')][_0xed56('0x34')](_0x492a80[_0xed56('0x95')](),_0x5c3b0f)['then'](function(_0x42e459){_0x26f8a9=_[_0xed56('0x96')](_0x42e459,'id');_0x5c3b0f=_[_0xed56('0x97')](_0x5c3b0f,{'model':db[_0xed56('0x30')],'mapToModel':!![],'raw':!![],'nest':!![]});if(_0x4ecbda[_0xed56('0x34')][_0xed56('0x39')]){_0x1bc32e[_0xed56('0x37')][_0xed56('0x2c')](function(_0x268bea){_0x345e35[_0xed56('0x41')]('i.'+_0x268bea);});}else{_0x345e35[_0xed56('0x41')](_0xed56('0x88'),_0xed56('0x56'));_0x345e35[_0xed56('0x41')](_0xed56('0x98'),_0xed56('0x99'));_0x345e35['field']('a.mandatoryDisposition',_0xed56('0x9a'));_0x345e35[_0xed56('0x41')](_0xed56('0x9b'),_0xed56('0x89'));_0x345e35['field']('i.*');_0x345e35[_0xed56('0x41')]('c.firstName',_0xed56('0x9c'));_0x345e35[_0xed56('0x41')]('c.lastName',_0xed56('0x9d'));_0x345e35[_0xed56('0x41')](_0xed56('0x9e'),_0xed56('0x9f'));_0x345e35[_0xed56('0x41')](_0xed56('0xa0'),'Contact.phone');_0x345e35['field'](_0xed56('0xa1'),'Contact.mobile');_0x345e35['field'](_0xed56('0xa2'),_0xed56('0xa3'));_0x345e35['field'](_0xed56('0xa4'),'Owner.fullname');_0x345e35[_0xed56('0x41')]('o.internal',_0xed56('0xa5'));_0x345e35[_0xed56('0x41')]('GROUP_CONCAT(DISTINCT\x20t.id)',_0xed56('0x2a'));}if(_0x1bc32e['limit'])_0x345e35['limit'](_0x1bc32e['limit']);if(_0x1bc32e[_0xed56('0x1e')])_0x345e35[_0xed56('0x1e')](_0x1bc32e['offset']);return db[_0xed56('0x94')][_0xed56('0x34')](_0x345e35[_0xed56('0x95')](),_0x5c3b0f);})['then'](function(_0x51ff35){if(_0x51ff35[_0xed56('0x3a')]>0x0)_0xa2ca50[_0xed56('0x3e')](_0xed56('0xa6'),_['map'](_0x51ff35,'id'));return db[_0xed56('0x94')][_0xed56('0x34')](_0xa2ca50['toString'](),_0x5c3b0f)[_0xed56('0x24')](function(_0x3506aa){var _0x535d9a=_[_0xed56('0xa7')](_0x3506aa,_0xed56('0xa8'));var _0x4bca55=[];_0x51ff35['forEach'](function(_0x1bd211){_0x4bca55[_0xed56('0x2e')](updateWhatsappInteraction(_0x1bd211,_0x26f8a9,_0x535d9a));});return BPromise[_0xed56('0xa9')](_0x4bca55);});});})[_0xed56('0x24')](function(_0x4fc2f4){_0x204860[_0xed56('0xaa')]=_0x4fc2f4;return _0x204860;})[_0xed56('0x24')](respondWithFilteredResult(_0x48b649,_0x1bc32e))[_0xed56('0x2f')](handleError(_0x48b649,null));};exports['show']=function(_0x51cf5f,_0x3a41ce){var _0x49391c={'raw':![],'where':{'id':_0x51cf5f['params']['id']}},_0x548067={};_0x548067[_0xed56('0x38')]=_['keys'](db[_0xed56('0x30')][_0xed56('0x31')]);_0x548067['query']=_['keys'](_0x51cf5f['query']);_0x548067[_0xed56('0x35')]=_[_0xed56('0x36')](_0x548067[_0xed56('0x38')],_0x548067[_0xed56('0x34')]);_0x49391c['attributes']=_[_0xed56('0x36')](_0x548067['model'],qs[_0xed56('0x39')](_0x51cf5f['query'][_0xed56('0x39')]));_0x49391c[_0xed56('0x37')]=_0x49391c[_0xed56('0x37')][_0xed56('0x3a')]?_0x49391c[_0xed56('0x37')]:_0x548067[_0xed56('0x38')];if(_0x51cf5f[_0xed56('0x34')][_0xed56('0xab')]){_0x49391c[_0xed56('0xac')]=[{'all':!![]}];}_0x49391c=_[_0xed56('0x97')]({},_0x49391c,_0x51cf5f[_0xed56('0xad')]);return db[_0xed56('0x30')][_0xed56('0x61')](_0x49391c)['then'](handleEntityNotFound(_0x3a41ce,null))[_0xed56('0x24')](respondWithResult(_0x3a41ce,null))['catch'](handleError(_0x3a41ce,null));};exports[_0xed56('0xae')]=function(_0x14b661,_0x1584d5){return db[_0xed56('0x30')][_0xed56('0xae')](_0x14b661[_0xed56('0x6b')],{})[_0xed56('0x24')](respondWithResult(_0x1584d5,0xc9))[_0xed56('0x2f')](handleError(_0x1584d5,null));};exports['update']=function(_0x17a482,_0xf1c5){if(_0x17a482[_0xed56('0x6b')]['id']){delete _0x17a482[_0xed56('0x6b')]['id'];}return db['WhatsappInteraction']['find']({'where':{'id':_0x17a482[_0xed56('0xaf')]['id']}})['then'](handleEntityNotFound(_0xf1c5,null))[_0xed56('0x24')](saveUpdates(_0x17a482[_0xed56('0x6b')],null))[_0xed56('0x24')](respondWithResult(_0xf1c5,null))[_0xed56('0x2f')](handleError(_0xf1c5,null));};exports[_0xed56('0x25')]=function(_0x1e5bd4,_0x398dc7){return db[_0xed56('0x30')][_0xed56('0x61')]({'where':{'id':_0x1e5bd4[_0xed56('0xaf')]['id']}})['then'](handleEntityNotFound(_0x398dc7,null))['then'](removeEntity(_0x398dc7,null))['catch'](handleError(_0x398dc7,null));};exports['describe']=function(_0x2c70f3,_0x373b66){return db[_0xed56('0x30')]['describe']()[_0xed56('0x24')](respondWithResult(_0x373b66,null))[_0xed56('0x2f')](handleError(_0x373b66,null));};exports['addMessage']=function(_0x319bee,_0x3fd507,_0x3e3dbc){return db[_0xed56('0xb0')]['find']({'where':{'id':_0x319bee['params']['id']}})[_0xed56('0x24')](handleEntityNotFound(_0x3fd507,null))[_0xed56('0x24')](function(_0xd0d04c){if(_0xd0d04c){return _0xd0d04c[_0xed56('0xb1')](_0x319bee['body'][_0xed56('0xb2')],_[_0xed56('0xb3')](_0x319bee[_0xed56('0x6b')],[_0xed56('0xb2'),'id'])||{});}})[_0xed56('0x24')](respondWithResult(_0x3fd507,null))[_0xed56('0x2f')](handleError(_0x3fd507,null));};exports['getMessages']=function(_0xd43c37,_0x1bcac7,_0x47f7cc){var _0x3ce6dc={'raw':![],'where':{}};var _0x25ade9={};var _0x319c09={'count':0x0,'rows':[]};return db[_0xed56('0x30')][_0xed56('0xb4')]({'where':{'id':_0xd43c37[_0xed56('0xaf')]['id']}})['then'](handleEntityNotFound(_0x1bcac7,null))[_0xed56('0x24')](function(_0x4901aa){if(_0x4901aa){_0x25ade9['model']=_[_0xed56('0x6c')](db[_0xed56('0xb0')]['rawAttributes']);_0x25ade9['query']=_['keys'](_0xd43c37[_0xed56('0x34')]);_0x25ade9[_0xed56('0x35')]=_['intersection'](_0x25ade9['model'],_0x25ade9[_0xed56('0x34')]);_0x3ce6dc[_0xed56('0x37')]=_['intersection'](_0x25ade9[_0xed56('0x38')],qs[_0xed56('0x39')](_0xd43c37[_0xed56('0x34')][_0xed56('0x39')]));_0x3ce6dc['attributes']=_0x3ce6dc['attributes'][_0xed56('0x3a')]?_0x3ce6dc[_0xed56('0x37')]:_0x25ade9[_0xed56('0x38')];if(!_0xd43c37[_0xed56('0x34')][_0xed56('0xb5')](_0xed56('0x3b'))){_0x3ce6dc['limit']=qs[_0xed56('0x1f')](_0xd43c37[_0xed56('0x34')][_0xed56('0x1f')]);_0x3ce6dc[_0xed56('0x1e')]=qs[_0xed56('0x1e')](_0xd43c37[_0xed56('0x34')][_0xed56('0x1e')]);}_0x3ce6dc['order']=qs[_0xed56('0x3d')](_0xd43c37[_0xed56('0x34')]['sort']);_0x3ce6dc['where']=qs['filters'](_['pick'](_0xd43c37['query'],_0x25ade9['filters']));_0x3ce6dc['where'][_0xed56('0xa8')]=_0x4901aa['id'];if(_0xd43c37['query']['filter']){_0x3ce6dc[_0xed56('0x3e')]=_[_0xed56('0x97')](_0x3ce6dc[_0xed56('0x3e')],{'$or':_[_0xed56('0x28')](_0x3ce6dc[_0xed56('0x37')],function(_0x15081e){var _0x432b77={};_0x432b77[_0x15081e]={'$like':'%'+_0xd43c37['query'][_0xed56('0x7c')]+'%'};return _0x432b77;})});}if(_0xd43c37[_0xed56('0x34')][_0xed56('0xb6')]){var _0x265819=_0xd43c37[_0xed56('0x34')][_0xed56('0xb6')]['split'](',');var _0x385dff={};_0x385dff[_0x265819[0x0]]={'$gte':moment(_0x265819[0x1])[_0xed56('0xb7')](_0xed56('0xb8'))};_0x3ce6dc[_0xed56('0x3e')]=_['merge'](_0x3ce6dc[_0xed56('0x3e')],_0x385dff);}_0x3ce6dc=_[_0xed56('0x97')]({},_0x3ce6dc,_0xd43c37[_0xed56('0xad')]);return db[_0xed56('0xb0')][_0xed56('0x20')]({'where':_0x3ce6dc[_0xed56('0x3e')]})[_0xed56('0x24')](function(_0x1e72b2){_0x319c09[_0xed56('0x20')]=_0x1e72b2;if(_0xd43c37[_0xed56('0x34')][_0xed56('0xab')]){_0x3ce6dc[_0xed56('0xac')]=[{'all':!![]}];}return db[_0xed56('0xb0')][_0xed56('0xb9')](_0x3ce6dc);})['then'](function(_0x2b8b89){_0x319c09['rows']=_0x2b8b89;return _0x319c09;});}})[_0xed56('0x24')](respondWithFilteredResult(_0x1bcac7,_0x3ce6dc))[_0xed56('0x2f')](handleError(_0x1bcac7,null));};exports['addTags']=function(_0x527653,_0x5b3117,_0x86e86c){return db[_0xed56('0x30')]['find']({'where':{'id':_0x527653[_0xed56('0xaf')]['id']}})[_0xed56('0x24')](handleEntityNotFound(_0x5b3117,null))[_0xed56('0x24')](function(_0x1ebad3){if(_0x1ebad3){return _0x1ebad3['setTags'](_0x527653[_0xed56('0x6b')][_0xed56('0xb2')],_[_0xed56('0xb3')](_0x527653[_0xed56('0x6b')],[_0xed56('0xb2'),'id'])||{})[_0xed56('0xba')](function(){return db[_0xed56('0xbb')][_0xed56('0xb9')]({'attributes':['id',_0xed56('0x27'),_0xed56('0x42')],'where':{'id':_0x527653['body'][_0xed56('0xb2')]}});})['then'](function(_0x4f9b01){socket[_0xed56('0xbc')]('whatsappInteractionTags:save',{'id':Number(_0x527653[_0xed56('0xaf')]['id']),'tags':_0x4f9b01||[]});return{'id':Number(_0x527653[_0xed56('0xaf')]['id']),'tags':_0x4f9b01||[]};});}})['then'](respondWithResult(_0x5b3117,null))[_0xed56('0x2f')](handleError(_0x5b3117,null));};exports['removeTags']=function(_0x39a932,_0x47e7bd,_0x1ef3f6){return db[_0xed56('0x30')]['find']({'where':{'id':_0x39a932[_0xed56('0xaf')]['id']}})['then'](handleEntityNotFound(_0x47e7bd,null))[_0xed56('0x24')](function(_0x58d908){if(_0x58d908){return _0x58d908[_0xed56('0xbd')](_0x39a932['query'][_0xed56('0xb2')]);}})['then'](respondWithStatusCode(_0x47e7bd,null))[_0xed56('0x2f')](handleError(_0x47e7bd,null));};exports[_0xed56('0xbe')]=function(_0x300365,_0x1ac199){var _0x414a47=moment()['unix']()[_0xed56('0x95')]();var _0x37d075=path[_0xed56('0xbf')](config['root'],_0xed56('0xc0'),_0xed56('0xc1'),_0xed56('0xc2'));var _0x26b703=path[_0xed56('0xbf')](config[_0xed56('0xc3')],_0xed56('0xc0'),'files',_0xed56('0xc4'));var _0x463789=path['join'](_0x37d075,_0x414a47);var _0xc94d7b=util[_0xed56('0xb7')](_0xed56('0xc5'),_0x300365[_0xed56('0xaf')]['id'],_0x414a47);var _0x58196e=path['join'](_0x37d075,_0xc94d7b);var _0x17bd9a=[];_0x17bd9a[_0xed56('0x2e')]({'model':db[_0xed56('0x67')],'as':'User','attributes':['id',_0xed56('0x29')],'raw':!![]});_0x17bd9a[_0xed56('0x2e')]({'model':db[_0xed56('0xc6')],'as':_0xed56('0x5b'),'attributes':['id',_0xed56('0xc7'),_0xed56('0xc8')],'raw':!![]});if(_0x300365[_0xed56('0x34')][_0xed56('0xc4')]){_0x17bd9a['push']({'model':db[_0xed56('0xc9')],'as':_0xed56('0xc9'),'raw':!![]});}var _0x107816=[{'model':db[_0xed56('0xb0')],'as':_0xed56('0xca'),'attributes':['id',_0xed56('0x6b'),_0xed56('0x71'),_0xed56('0xcb'),_0xed56('0xcc'),_0xed56('0x76')],'include':_0x17bd9a}];_0x107816['push']({'model':db[_0xed56('0xcd')],'as':_0xed56('0xce'),'attributes':['id',_0xed56('0x27')]});_0x107816[_0xed56('0x2e')]({'model':db[_0xed56('0x67')],'as':_0xed56('0xcf'),'attributes':['id',_0xed56('0x29')]});return db[_0xed56('0x30')][_0xed56('0x61')]({'where':{'id':_0x300365[_0xed56('0xaf')]['id']},'include':_0x107816})[_0xed56('0x24')](handleEntityNotFound(_0x1ac199,null))[_0xed56('0x24')](function(_0x6ffff3){if(_0x6ffff3){var _0x5dc39b=_0x6ffff3['get']({'plain':!![]});fs[_0xed56('0xd0')](_0x463789);var _0x1ef8e1={'channel':_0xed56('0xd1'),'account':_0x5dc39b[_0xed56('0xce')][_0xed56('0x27')],'agent':_0x5dc39b['Owner']?_0x5dc39b['Owner'][_0xed56('0x29')]:_0xed56('0xd2'),'createdAt':moment(_0x5dc39b[_0xed56('0x71')])['format']('YYYY-MM-DD\x20HH:mm:ss')[_0xed56('0x95')](),'closedAt':_0x5dc39b[_0xed56('0xd3')]?moment(_0x5dc39b[_0xed56('0xd4')])['format'](_0xed56('0xb8'))[_0xed56('0x95')]():'','disposition':_0x5dc39b['disposition'],'messages':_(_0x5dc39b[_0xed56('0xca')])[_0xed56('0x7c')]({'secret':![]})['map'](function(_0x2c2cb8){if(_0x2c2cb8[_0xed56('0xc9')]){var _0x39ea5d=path[_0xed56('0xbf')](_0x26b703,_0x2c2cb8[_0xed56('0xc9')][_0xed56('0xd5')]);if(fs['existsSync'](_0x39ea5d)){fs[_0xed56('0xd6')](_0x39ea5d)['pipe'](fs[_0xed56('0xd7')](path[_0xed56('0xbf')](_0x463789,_0x2c2cb8['Attachment'][_0xed56('0xd5')])));}}return{'date':moment(_0x2c2cb8[_0xed56('0x71')])[_0xed56('0xb7')](_0xed56('0xb8'))[_0xed56('0x95')](),'sender':_0x2c2cb8[_0xed56('0xcb')]===_0xed56('0xd8')?_0x2c2cb8[_0xed56('0x67')]?_0x2c2cb8[_0xed56('0x67')][_0xed56('0x29')]:_0xed56('0xd9'):_0x2c2cb8[_0xed56('0x5b')][_0xed56('0xc7')]+(_0x2c2cb8[_0xed56('0x5b')]['lastName']?'\x20'+_0x2c2cb8[_0xed56('0x5b')]['lastName']:''),'body':_0x2c2cb8[_0xed56('0xc9')]?_0x2c2cb8[_0xed56('0xc9')][_0xed56('0xd5')]:_0x2c2cb8['body'],'direction':_0x2c2cb8[_0xed56('0xcb')]===_0xed56('0xd8')?'A':'C','secret':_0x2c2cb8[_0xed56('0xcc')],'read':_0x2c2cb8[_0xed56('0x76')],'attachment':_0x2c2cb8[_0xed56('0xc9')]};})['value']()};return ejs[_0xed56('0xda')](path[_0xed56('0xbf')](config['root'],_0xed56('0xc0'),_0xed56('0xdb'),_0xed56('0xdc')),{'interaction':_0x1ef8e1})[_0xed56('0x24')](function(_0x2cac12){var _0x3141f9=path[_0xed56('0xbf')](_0x463789,'transcript-'+_0x5dc39b['id']+'-'+_0x414a47+'.pdf');var _0x38d4ba={'path':_0x3141f9,'channel':_0xed56('0xd1'),'interactionId':_0x5dc39b['id']};return pdf[_0xed56('0xdd')](_0x2cac12,_0x38d4ba);})[_0xed56('0x24')](function(){return new BPromise(function(_0x36765a,_0x1023dc){zipdir(_0x463789,{'saveTo':_0x58196e},function(_0x49dcd7,_0x375426){if(_0x49dcd7)return _0x1023dc(_0x49dcd7);return _0x36765a(_0x375426);});})[_0xed56('0x24')](function(){return new BPromise(function(_0x5486a5,_0x4e526f){rimraf(_0x463789,function(_0x26dc15){if(_0x26dc15)_0x4e526f(_0x26dc15);return _0x5486a5();});});})[_0xed56('0x24')](function(){return _0x1ac199[_0xed56('0xbe')](_0x58196e,_0xc94d7b,function(_0xd06f01){if(_0xd06f01){console[_0xed56('0xde')]('err',_0xd06f01);}else{fs[_0xed56('0xdf')](_0x58196e);}});});});}else{return _0x1ac199[_0xed56('0x26')](0xc8);}})[_0xed56('0x2f')](handleError(_0x1ac199,null));};