428901cbc040d94cd46dd331ca5e26540c3dd75b
[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 _0x6fc2=['removeTags','download','unix','join','root','server','files','tmp','attachments','CmContact','firstName','Attachment','Messages','createdAt','secret','WhatsappAccount','Account','User','Owner','mkdirSync','Whatsapp','N.A.','closed','closedAt','disposition','basename','existsSync','createReadStream','pipe','createWriteStream','out','System','lastName','direction','renderFile','views','downloadInteraction.html','.pdf','log','err','eml-format','rimraf','zip-dir','request-promise','moment','path','sox','ejs','fs-extra','lodash','squel','jsforce','querystring','papaparse','ioredis','../../components/parsers/qs','../../components/parsers/advancedSearch','../../components/export/pdf','../../config/license/hardware','../../config/logger','api','../../config/utils','../../config/environment','../../config/license/util','../../mysqldb','redis','defaults','localhost','socket.io-emitter','register','sendStatus','status','end','undefined','limit','count','set','Content-Range','apply','reject','save','update','then','destroy','error','stack','name','body','contact','message','agent','interaction','account','map','fullname','TagIds','split','forEach','Tags','index','WhatsappInteraction','rawAttributes','fieldName','type','key','keys','filters','intersection','model','query','attributes','fields','hasOwnProperty','nolimit','offset','order','sort','where','pick','select','field','color','tools_tags','u.id','u.fullname','ui.WhatsappInteractionId','user_has_whatsapp_interactions','left_join','u.id\x20=\x20ui.UserId','whatsapp_interactions','cm_contacts','users','o.id\x20=\x20i.UserId','whatsapp_accounts','a.id\x20=\x20i.WhatsappAccountId','me.WhatsappInteractionId\x20=\x20i.id','it.WhatsappInteractionId\x20=\x20i.id','t.id\x20=\x20it.TagId','expr','search','parseSearch','conditions','find','unreadMessages','value','having','`\x20>\x200','Contact','operator','$substring','length','getFullTextValue','c.firstName\x20LIKE\x20?\x20OR\x20c.lastName\x20LIKE\x20?\x20OR\x20c.email\x20LIKE\x20?','ContactId','text','start','Tag','union','buildExpression','some','isEmpty','WhatsappAccountId','concat','parse','$gte','\x20IS\x20NULL','read','`unreadMessages`\x20=\x200','`unreadMessages`\x20>\x200','tag','isArray','filter','replace','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)','contactName','t.id\x20IN\x20?','resolve','i.WhatsappAccountId\x20IS\x20NOT\x20NULL','user','role','getWhatsappAccounts','i.WhatsappAccountId\x20IS\x20NULL','i.WhatsappAccountId\x20IN\x20?','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','Contact.email','Contact.phone','c.mobile','c.fax','Contact.fax','o.fullname','Owner.fullname','Owner.internal','GROUP_CONCAT(DISTINCT\x20t.id)','ui.WhatsappInteractionId\x20IN\x20?','groupBy','WhatsappInteractionId','push','all','rows','show','params','include','options','create','catch','describe','addMessage','WhatsappMessage','omit','getMessages','findOne','format','YYYY-MM-DD\x20HH:mm:ss','includeAll','findAll','addTags','setTags','ids','emit'];(function(_0x2dadb9,_0x42cfe6){var _0x2f0097=function(_0x21163f){while(--_0x21163f){_0x2dadb9['push'](_0x2dadb9['shift']());}};_0x2f0097(++_0x42cfe6);}(_0x6fc2,0x117));var _0x26fc=function(_0x413feb,_0x10477f){_0x413feb=_0x413feb-0x0;var _0x40ee88=_0x6fc2[_0x413feb];return _0x40ee88;};'use strict';var emlformat=require(_0x26fc('0x0'));var rimraf=require(_0x26fc('0x1'));var zipdir=require(_0x26fc('0x2'));var jsonpatch=require('fast-json-patch');var rp=require(_0x26fc('0x3'));var moment=require(_0x26fc('0x4'));var BPromise=require('bluebird');var Mustache=require('mustache');var util=require('util');var path=require(_0x26fc('0x5'));var sox=require(_0x26fc('0x6'));var csv=require('to-csv');var ejs=require(_0x26fc('0x7'));var fs=require('fs');var fs_extra=require(_0x26fc('0x8'));var _=require(_0x26fc('0x9'));var squel=require(_0x26fc('0xa'));var crypto=require('crypto');var jsforce=require(_0x26fc('0xb'));var deskjs=require('desk.js');var toCsv=require('to-csv');var querystring=require(_0x26fc('0xc'));var Papa=require(_0x26fc('0xd'));var Redis=require(_0x26fc('0xe'));var authService=require('../../components/auth/service');var qs=require(_0x26fc('0xf'));var as=require(_0x26fc('0x10'));var pdf=require(_0x26fc('0x11'));var hardwareService=require(_0x26fc('0x12'));var logger=require(_0x26fc('0x13'))(_0x26fc('0x14'));var utils=require(_0x26fc('0x15'));var config=require(_0x26fc('0x16'));var licenseUtil=require(_0x26fc('0x17'));var db=require(_0x26fc('0x18'))['db'];config[_0x26fc('0x19')]=_[_0x26fc('0x1a')](config[_0x26fc('0x19')],{'host':_0x26fc('0x1b'),'port':0x18eb});var socket=require(_0x26fc('0x1c'))(new Redis(config[_0x26fc('0x19')]));require('./whatsappInteraction.socket')[_0x26fc('0x1d')](socket);function respondWithStatusCode(_0x2a2d09,_0x1f1903){_0x1f1903=_0x1f1903||0xcc;return function(_0x405139){if(_0x405139){return _0x2a2d09[_0x26fc('0x1e')](_0x1f1903);}return _0x2a2d09[_0x26fc('0x1f')](_0x1f1903)[_0x26fc('0x20')]();};}function respondWithResult(_0x383f2f,_0x351926){_0x351926=_0x351926||0xc8;return function(_0x54f0e5){if(_0x54f0e5){return _0x383f2f[_0x26fc('0x1f')](_0x351926)['json'](_0x54f0e5);}};}function respondWithFilteredResult(_0x2da252,_0x3507b1){return function(_0x2732a4){if(_0x2732a4){var _0x3b3a92=typeof _0x3507b1['offset']===_0x26fc('0x21')&&typeof _0x3507b1[_0x26fc('0x22')]===_0x26fc('0x21');var _0x9a9c2=_0x2732a4[_0x26fc('0x23')];var _0x5f454f=_0x3b3a92?0x0:_0x3507b1['offset'];var _0x5c10c6=_0x3b3a92?_0x2732a4['count']:_0x3507b1['offset']+_0x3507b1['limit'];var _0x2339ad;if(_0x5c10c6>=_0x9a9c2){_0x5c10c6=_0x9a9c2;_0x2339ad=0xc8;}else{_0x2339ad=0xce;}_0x2da252['status'](_0x2339ad);return _0x2da252[_0x26fc('0x24')](_0x26fc('0x25'),_0x5f454f+'-'+_0x5c10c6+'/'+_0x9a9c2)['json'](_0x2732a4);}return null;};}function patchUpdates(_0x4e62d8){return function(_0x288c8d){try{jsonpatch[_0x26fc('0x26')](_0x288c8d,_0x4e62d8,!![]);}catch(_0x1641f2){return BPromise[_0x26fc('0x27')](_0x1641f2);}return _0x288c8d[_0x26fc('0x28')]();};}function saveUpdates(_0x4b320b,_0x346fe0){return function(_0x435d72){if(_0x435d72){return _0x435d72[_0x26fc('0x29')](_0x4b320b)[_0x26fc('0x2a')](function(_0x281511){return _0x281511;});}return null;};}function removeEntity(_0x3be613,_0x430a98){return function(_0x5b9dba){if(_0x5b9dba){return _0x5b9dba[_0x26fc('0x2b')]()[_0x26fc('0x2a')](function(){_0x3be613[_0x26fc('0x1f')](0xcc)[_0x26fc('0x20')]();});}};}function handleEntityNotFound(_0x57851e,_0x12ecb7){return function(_0x4f968a){if(!_0x4f968a){_0x57851e[_0x26fc('0x1e')](0x194);}return _0x4f968a;};}function handleError(_0x4218a0,_0x3bff70){_0x3bff70=_0x3bff70||0x1f4;return function(_0x32088c){logger[_0x26fc('0x2c')](_0x32088c[_0x26fc('0x2d')]);if(_0x32088c[_0x26fc('0x2e')]){delete _0x32088c[_0x26fc('0x2e')];}_0x4218a0['status'](_0x3bff70)['send'](_0x32088c);};}function getMustacheRender(_0x47b642,_0x297e53){return Mustache['render'](_0x47b642,_['merge'](_0x297e53[_0x26fc('0x2f')],{'contact':_0x297e53[_0x26fc('0x30')],'message':_0x297e53[_0x26fc('0x31')],'agent':_0x297e53[_0x26fc('0x32')],'interaction':_0x297e53[_0x26fc('0x33')],'account':_0x297e53[_0x26fc('0x34')]}))||'';}function getInteractionUsers(_0x2b1385,_0x1661cd){return new BPromise(function(_0x260339,_0x2466ab){try{if(_0x1661cd[_0x2b1385['id']]){_0x2b1385['Users']=_[_0x26fc('0x35')](_0x1661cd[_0x2b1385['id']],function(_0x3e51b0){return{'id':_0x3e51b0['id'],'fullname':_0x3e51b0[_0x26fc('0x36')]};});}}catch(_0x115985){_0x2466ab(_0x115985);}_0x260339(_0x2b1385);});}function getInteractionTags(_0x2fde81,_0x190092){return new BPromise(function(_0x143a56,_0x5201fa){try{if(_0x2fde81[_0x26fc('0x37')]){_0x2fde81['Tags']=[];_0x2fde81[_0x26fc('0x37')][_0x26fc('0x38')](',')[_0x26fc('0x39')](function(_0x324e43){_0x2fde81[_0x26fc('0x3a')]['push'](_0x190092[_0x324e43]);});}delete _0x2fde81[_0x26fc('0x37')];}catch(_0x4664ec){_0x5201fa(_0x4664ec);}_0x143a56(_0x2fde81);});}function updateWhatsappInteraction(_0x47c4df,_0x1cf6f0,_0x56be15){return new BPromise(function(_0x440451,_0x4264a0){return getInteractionUsers(_0x47c4df,_0x56be15)[_0x26fc('0x2a')](function(_0x1973bd){return getInteractionTags(_0x1973bd,_0x1cf6f0);})[_0x26fc('0x2a')](function(_0x31bb59){_0x440451(_0x31bb59);})['catch'](function(_0x5b34c9){_0x4264a0(_0x5b34c9);});});}exports[_0x26fc('0x3b')]=function(_0xdc8ef3,_0x31a2fe){var _0x1205e8={},_0x4c69e4={},_0x235861={'count':0x0,'rows':[]};var _0x3909fc=_['map'](db[_0x26fc('0x3c')][_0x26fc('0x3d')],function(_0x5e4c3d){return{'name':_0x5e4c3d[_0x26fc('0x3e')],'type':_0x5e4c3d[_0x26fc('0x3f')][_0x26fc('0x40')]};});_0x4c69e4['model']=_[_0x26fc('0x35')](_0x3909fc,_0x26fc('0x2e'));_0x4c69e4['query']=_[_0x26fc('0x41')](_0xdc8ef3['query']);_0x4c69e4[_0x26fc('0x42')]=_[_0x26fc('0x43')](_0x4c69e4[_0x26fc('0x44')],_0x4c69e4[_0x26fc('0x45')]);_0x1205e8[_0x26fc('0x46')]=_[_0x26fc('0x43')](_0x4c69e4[_0x26fc('0x44')],qs[_0x26fc('0x47')](_0xdc8ef3[_0x26fc('0x45')][_0x26fc('0x47')]));_0x1205e8[_0x26fc('0x46')]=_0x1205e8[_0x26fc('0x46')]['length']?_0x1205e8[_0x26fc('0x46')]:_0x4c69e4[_0x26fc('0x44')];if(!_0xdc8ef3[_0x26fc('0x45')][_0x26fc('0x48')](_0x26fc('0x49'))){_0x1205e8[_0x26fc('0x22')]=qs[_0x26fc('0x22')](_0xdc8ef3['query']['limit']);_0x1205e8[_0x26fc('0x4a')]=qs[_0x26fc('0x4a')](_0xdc8ef3['query'][_0x26fc('0x4a')]);}_0x1205e8[_0x26fc('0x4b')]=qs[_0x26fc('0x4c')](_0xdc8ef3['query'][_0x26fc('0x4c')]);_0x1205e8[_0x26fc('0x4d')]=qs[_0x26fc('0x42')](_[_0x26fc('0x4e')](_0xdc8ef3[_0x26fc('0x45')],_0x4c69e4[_0x26fc('0x42')]),_0x3909fc);var _0x1395a8=[];var _0x44b16e=squel[_0x26fc('0x4f')]()[_0x26fc('0x50')]('id')[_0x26fc('0x50')]('name')[_0x26fc('0x50')](_0x26fc('0x51'))['from'](_0x26fc('0x52'));var _0x354ec0=squel[_0x26fc('0x4f')]()['field'](_0x26fc('0x53'))[_0x26fc('0x50')](_0x26fc('0x54'))[_0x26fc('0x50')](_0x26fc('0x55'))['from'](_0x26fc('0x56'),'ui')[_0x26fc('0x57')]('users','u',_0x26fc('0x58'));var _0x399cb8=squel[_0x26fc('0x4f')]()['from'](_0x26fc('0x59'),'i')[_0x26fc('0x57')](_0x26fc('0x5a'),'c','c.id\x20=\x20i.ContactId\x20AND\x20c.deletedAt\x20IS\x20NULL')['left_join'](_0x26fc('0x5b'),'o',_0x26fc('0x5c'))[_0x26fc('0x57')](_0x26fc('0x5d'),'a',_0x26fc('0x5e'))['left_join']('whatsapp_messages','me',_0x26fc('0x5f'))['left_join']('whatsapp_interaction_has_tags','it',_0x26fc('0x60'))[_0x26fc('0x57')](_0x26fc('0x52'),'t',_0x26fc('0x61'));var _0x4671db=squel[_0x26fc('0x62')]();var _0x6c753=[];var _0x21915b=squel[_0x26fc('0x62')]();var _0x1a8ede;if(_0xdc8ef3['query'][_0x26fc('0x63')]){_0x1a8ede=as[_0x26fc('0x64')](_0xdc8ef3[_0x26fc('0x45')]['search']);var _0x79fed0=_0x1a8ede['sqlOperator'];for(var _0x5ddde5=0x0;_0x5ddde5<_0x1a8ede[_0x26fc('0x65')]['length'];_0x5ddde5++){var _0x52194c=_0x1a8ede[_0x26fc('0x65')][_0x5ddde5];var _0x4fc2e6='i';var _0xdcdd06=_[_0x26fc('0x66')](_0x3909fc,['name',_0x52194c[_0x26fc('0x50')]]);if(!_0xdcdd06){switch(_0x52194c[_0x26fc('0x50')]){case _0x26fc('0x67'):if(_0x52194c[_0x26fc('0x68')]==0x1){_0x399cb8[_0x26fc('0x69')]('`'+_0x52194c[_0x26fc('0x50')]+'`\x20=\x200');}else{_0x399cb8[_0x26fc('0x69')]('`'+_0x52194c['field']+_0x26fc('0x6a'));}break;case _0x26fc('0x6b'):if(_0x52194c[_0x26fc('0x6c')]===_0x26fc('0x6d')){if(_0x52194c[_0x26fc('0x68')][_0x26fc('0x38')]('\x20')[_0x26fc('0x6e')]>0x1){_0x4671db[_0x79fed0]('MATCH\x20(c.firstName,\x20c.lastName,\x20c.phone,\x20c.mobile,\x20c.fax,\x20c.email)\x20AGAINST\x20(?\x20IN\x20BOOLEAN\x20MODE)',qs[_0x26fc('0x6f')](_0x52194c[_0x26fc('0x68')]),null);}else{var _0x3626f2='%'+_0x52194c[_0x26fc('0x68')]+'%';_0x4671db[_0x79fed0](_0x26fc('0x70'),_0x3626f2,_0x3626f2,_0x3626f2);}}else{_0xdcdd06=_[_0x26fc('0x66')](_0x3909fc,['name',_0x26fc('0x71')]);_0x52194c[_0x26fc('0x50')]=_0x26fc('0x71');_0x2539ec=as['buildExpression'](_0x4fc2e6,_0xdcdd06[_0x26fc('0x3f')],_0x52194c);_0x4671db[_0x79fed0](_0x2539ec[_0x26fc('0x72')],_0x2539ec['value'][_0x26fc('0x73')],_0x2539ec[_0x26fc('0x68')][_0x26fc('0x20')]);}break;case _0x26fc('0x74'):var _0x1773f0=_0x52194c[_0x26fc('0x68')][_0x26fc('0x38')](',')['map'](function(_0x36cee3){return Number(_0x36cee3);});_0x1773f0['forEach'](function(_0x13e2ac){_0x21915b['or']('CONCAT(\x27,\x27,\x20`TagIds`,\x20\x27,\x27)\x20LIKE\x20?','%,'+_0x13e2ac+',%');});_0x6c753=_[_0x26fc('0x75')](_0x6c753,_0x1773f0);break;case'User':if(_0x52194c[_0x26fc('0x6c')]===_0x26fc('0x6d')){_0x4671db[_0x79fed0]('CONCAT_WS(\x27\x20\x27,\x20o.name,\x20o.fullname)\x20LIKE\x20?','%'+_0x52194c[_0x26fc('0x68')]+'%',null);}else{_0xdcdd06=_[_0x26fc('0x66')](_0x3909fc,[_0x26fc('0x2e'),'UserId']);_0x52194c['field']='UserId';_0x2539ec=as[_0x26fc('0x76')](_0x4fc2e6,_0xdcdd06['type'],_0x52194c);_0x4671db[_0x79fed0](_0x2539ec['text'],_0x2539ec[_0x26fc('0x68')][_0x26fc('0x73')],_0x2539ec['value']['end']);}break;case _0x26fc('0x2f'):_0x2539ec=as[_0x26fc('0x76')]('me',null,_0x52194c);_0x4671db[_0x79fed0](_0x2539ec[_0x26fc('0x72')],_0x2539ec[_0x26fc('0x68')]['start'],_0x2539ec[_0x26fc('0x68')]['end']);break;}}else{var _0x2539ec=as['buildExpression'](_0x4fc2e6,_0xdcdd06['type'],_0x52194c);_0x4671db[_0x79fed0](_0x2539ec[_0x26fc('0x72')],_0x2539ec[_0x26fc('0x68')][_0x26fc('0x73')],_0x2539ec[_0x26fc('0x68')]['end']);}}}else{var _0x3e9c3e=_(_0xdc8ef3[_0x26fc('0x45')])[_0x26fc('0x41')]()[_0x26fc('0x35')](function(_0x298310){return _[_0x26fc('0x77')](_0x3909fc,[_0x26fc('0x2e'),_0x298310])?_0x298310:undefined;})['compact']()[_0x26fc('0x68')]();if(!_[_0x26fc('0x78')](_0x3e9c3e)){_0x3e9c3e[_0x26fc('0x39')](function(_0x3cecf3){if(_0x3cecf3===_0x26fc('0x79')){_0x399cb8[_0x26fc('0x4d')]('i.WhatsappAccountId\x20IN\x20?',[][_0x26fc('0x7a')](_0xdc8ef3[_0x26fc('0x45')][_0x3cecf3]));}else if(_0x3cecf3==='createdAt'){var _0x156d84=JSON[_0x26fc('0x7b')](_0xdc8ef3[_0x26fc('0x45')][_0x3cecf3])[_0x26fc('0x7c')];var _0x8aa523=JSON[_0x26fc('0x7b')](_0xdc8ef3[_0x26fc('0x45')][_0x3cecf3])['$lte'];_0x399cb8[_0x26fc('0x4d')]('i.createdAt\x20BETWEEN\x20?\x20AND\x20?',_0x156d84,_0x8aa523);}else{if(_0xdc8ef3[_0x26fc('0x45')][_0x3cecf3]==='null'){_0x399cb8[_0x26fc('0x4d')]('i.'+_0x3cecf3+_0x26fc('0x7d'));}else{_0x399cb8[_0x26fc('0x4d')]('i.'+_0x3cecf3+'\x20=\x20?',_0xdc8ef3[_0x26fc('0x45')][_0x3cecf3]);}}});}if(_0xdc8ef3[_0x26fc('0x45')][_0x26fc('0x7e')]){if(_0xdc8ef3[_0x26fc('0x45')]['read']==0x1){_0x399cb8[_0x26fc('0x69')](_0x26fc('0x7f'));}else{_0x399cb8[_0x26fc('0x69')](_0x26fc('0x80'));}}if(_0xdc8ef3['query'][_0x26fc('0x81')]){_0x6c753=_[_0x26fc('0x82')](_0xdc8ef3[_0x26fc('0x45')][_0x26fc('0x81')])?_0xdc8ef3[_0x26fc('0x45')]['tag']:new Array(_0xdc8ef3[_0x26fc('0x45')][_0x26fc('0x81')]);_0x6c753[_0x26fc('0x39')](function(_0x5e7754){_0x21915b['or']('CONCAT(\x27,\x27,\x20`TagIds`,\x20\x27,\x27)\x20LIKE\x20?','%,'+_0x5e7754+',%');});}if(_0xdc8ef3[_0x26fc('0x45')][_0x26fc('0x83')]){var _0x285f0d=_0xdc8ef3[_0x26fc('0x45')]['filter']['replace']('\x5c','\x5c\x5c')[_0x26fc('0x84')](/'/g,'\x27\x27');if(qs[_0x26fc('0x85')](_0x285f0d)){_0x4671db['or'](_0x26fc('0x86'),_0x285f0d+'%')['or'](_0x26fc('0x87'),_0x285f0d+'%');}else{_0x4671db['or']('MATCH\x20(me.body)\x20AGAINST\x20(?\x20IN\x20BOOLEAN\x20MODE)',qs[_0x26fc('0x6f')](_0x285f0d))['or'](_0x26fc('0x88'),_0x285f0d+'%');}}}_0x399cb8[_0x26fc('0x4d')](_0x4671db);_0x399cb8[_0x26fc('0x89')](_0x26fc('0x8a'));var _0x377f88={'type':db[_0x26fc('0x8b')][_0x26fc('0x8c')]['SELECT'],'raw':!![]};var _0xeb7d53=_0x399cb8[_0x26fc('0x8d')]();_0xeb7d53[_0x26fc('0x50')](_0x26fc('0x8a'));_0xeb7d53[_0x26fc('0x50')](_0x26fc('0x8e'),'unreadMessages');if(_0x1205e8[_0x26fc('0x4b')]){_0x1205e8['order'][_0x26fc('0x39')](function(_0x6ab76){var _0x37ca33=_0x6ab76[0x0]===_0x26fc('0x8f')?_0x6ab76[0x0]:'i.'+_0x6ab76[0x0];_0x399cb8[_0x26fc('0x4b')](_0x37ca33,_0x6ab76[0x1]==='DESC'?![]:!![]);});}if(!_[_0x26fc('0x78')](_0x6c753)){_0x399cb8[_0x26fc('0x69')](_0x21915b);_0xeb7d53[_0x26fc('0x4d')](_0x26fc('0x90'),_0x6c753);}BPromise[_0x26fc('0x91')]()[_0x26fc('0x2a')](function(){if(!_0x1a8ede){if(_0xdc8ef3[_0x26fc('0x45')]['WhatsappAccountId'])return;_0xeb7d53[_0x26fc('0x4d')]('i.WhatsappAccountId\x20IS\x20NOT\x20NULL');_0x399cb8['where'](_0x26fc('0x92'));return;}if(_0xdc8ef3[_0x26fc('0x93')][_0x26fc('0x94')]!=='agent')return;return _0xdc8ef3['user'][_0x26fc('0x95')]({'attributes':['id'],'raw':!![]})['then'](function(_0x5162c7){if(_[_0x26fc('0x78')](_0x5162c7)){_0xeb7d53[_0x26fc('0x4d')]('i.WhatsappAccountId\x20IS\x20NULL');_0x399cb8[_0x26fc('0x4d')](_0x26fc('0x96'));}else{_0xeb7d53[_0x26fc('0x4d')](_0x26fc('0x97'),_['map'](_0x5162c7,'id'));_0x399cb8[_0x26fc('0x4d')](_0x26fc('0x97'),_['map'](_0x5162c7,'id'));}});})['then'](function(){if(_0xdc8ef3[_0x26fc('0x93')][_0x26fc('0x94')]===_0x26fc('0x32')&&!_0x1a8ede&&!_0xdc8ef3['query'][_0x26fc('0x79')])return[];return db['sequelize'][_0x26fc('0x45')](_0xeb7d53[_0x26fc('0x98')](),_0x377f88);})['then'](function(_0x2e5bf6){_0x235861[_0x26fc('0x23')]=_0x2e5bf6[_0x26fc('0x6e')];if(_0x235861['count']===0x0)return[];return db[_0x26fc('0x99')][_0x26fc('0x45')](_0x44b16e['toString'](),_0x377f88)[_0x26fc('0x2a')](function(_0x160871){_0x1395a8=_[_0x26fc('0x9a')](_0x160871,'id');_0x377f88=_[_0x26fc('0x9b')](_0x377f88,{'model':db[_0x26fc('0x3c')],'mapToModel':!![],'raw':!![],'nest':!![]});if(_0xdc8ef3[_0x26fc('0x45')]['fields']){_0x1205e8[_0x26fc('0x46')]['forEach'](function(_0x42a8eb){_0x399cb8[_0x26fc('0x50')]('i.'+_0x42a8eb);});}else{_0x399cb8[_0x26fc('0x50')]('SUM(me.read\x20=\x200\x20AND\x20me.direction\x20=\x20\x27in\x27)',_0x26fc('0x67'));_0x399cb8[_0x26fc('0x50')](_0x26fc('0x9c'),_0x26fc('0x9d'));_0x399cb8[_0x26fc('0x50')](_0x26fc('0x9e'),_0x26fc('0x9f'));_0x399cb8[_0x26fc('0x50')](_0x26fc('0xa0'),_0x26fc('0x8f'));_0x399cb8['field']('i.*');_0x399cb8[_0x26fc('0x50')](_0x26fc('0xa1'),'Contact.firstName');_0x399cb8[_0x26fc('0x50')](_0x26fc('0xa2'),_0x26fc('0xa3'));_0x399cb8[_0x26fc('0x50')]('c.email',_0x26fc('0xa4'));_0x399cb8[_0x26fc('0x50')]('c.phone',_0x26fc('0xa5'));_0x399cb8[_0x26fc('0x50')](_0x26fc('0xa6'),'Contact.mobile');_0x399cb8['field'](_0x26fc('0xa7'),_0x26fc('0xa8'));_0x399cb8['field'](_0x26fc('0xa9'),_0x26fc('0xaa'));_0x399cb8[_0x26fc('0x50')]('o.internal',_0x26fc('0xab'));_0x399cb8[_0x26fc('0x50')](_0x26fc('0xac'),_0x26fc('0x37'));}if(_0x1205e8['limit'])_0x399cb8['limit'](_0x1205e8[_0x26fc('0x22')]);if(_0x1205e8[_0x26fc('0x4a')])_0x399cb8[_0x26fc('0x4a')](_0x1205e8[_0x26fc('0x4a')]);return db['sequelize'][_0x26fc('0x45')](_0x399cb8['toString'](),_0x377f88);})[_0x26fc('0x2a')](function(_0x210827){if(_0x210827['length']>0x0)_0x354ec0[_0x26fc('0x4d')](_0x26fc('0xad'),_[_0x26fc('0x35')](_0x210827,'id'));return db['sequelize'][_0x26fc('0x45')](_0x354ec0[_0x26fc('0x98')](),_0x377f88)[_0x26fc('0x2a')](function(_0x1e3ddb){var _0x327518=_[_0x26fc('0xae')](_0x1e3ddb,_0x26fc('0xaf'));var _0x324481=[];_0x210827[_0x26fc('0x39')](function(_0x483711){_0x324481[_0x26fc('0xb0')](updateWhatsappInteraction(_0x483711,_0x1395a8,_0x327518));});return BPromise[_0x26fc('0xb1')](_0x324481);});});})['then'](function(_0x2860df){_0x235861[_0x26fc('0xb2')]=_0x2860df;return _0x235861;})[_0x26fc('0x2a')](respondWithFilteredResult(_0x31a2fe,_0x1205e8))['catch'](handleError(_0x31a2fe,null));};exports[_0x26fc('0xb3')]=function(_0x3c2db5,_0x50a3e6){var _0x5ad139={'raw':![],'where':{'id':_0x3c2db5[_0x26fc('0xb4')]['id']}},_0x48c954={};_0x48c954[_0x26fc('0x44')]=_['keys'](db[_0x26fc('0x3c')]['rawAttributes']);_0x48c954[_0x26fc('0x45')]=_[_0x26fc('0x41')](_0x3c2db5[_0x26fc('0x45')]);_0x48c954[_0x26fc('0x42')]=_[_0x26fc('0x43')](_0x48c954[_0x26fc('0x44')],_0x48c954[_0x26fc('0x45')]);_0x5ad139[_0x26fc('0x46')]=_['intersection'](_0x48c954[_0x26fc('0x44')],qs[_0x26fc('0x47')](_0x3c2db5[_0x26fc('0x45')][_0x26fc('0x47')]));_0x5ad139['attributes']=_0x5ad139['attributes'][_0x26fc('0x6e')]?_0x5ad139[_0x26fc('0x46')]:_0x48c954[_0x26fc('0x44')];if(_0x3c2db5[_0x26fc('0x45')]['includeAll']){_0x5ad139[_0x26fc('0xb5')]=[{'all':!![]}];}_0x5ad139=_[_0x26fc('0x9b')]({},_0x5ad139,_0x3c2db5[_0x26fc('0xb6')]);return db['WhatsappInteraction'][_0x26fc('0x66')](_0x5ad139)[_0x26fc('0x2a')](handleEntityNotFound(_0x50a3e6,null))[_0x26fc('0x2a')](respondWithResult(_0x50a3e6,null))['catch'](handleError(_0x50a3e6,null));};exports[_0x26fc('0xb7')]=function(_0x5e3176,_0x4909e5){return db[_0x26fc('0x3c')][_0x26fc('0xb7')](_0x5e3176[_0x26fc('0x2f')],{})['then'](respondWithResult(_0x4909e5,0xc9))[_0x26fc('0xb8')](handleError(_0x4909e5,null));};exports[_0x26fc('0x29')]=function(_0x36f407,_0x524535){if(_0x36f407[_0x26fc('0x2f')]['id']){delete _0x36f407[_0x26fc('0x2f')]['id'];}return db['WhatsappInteraction']['find']({'where':{'id':_0x36f407[_0x26fc('0xb4')]['id']}})['then'](handleEntityNotFound(_0x524535,null))['then'](saveUpdates(_0x36f407['body'],null))[_0x26fc('0x2a')](respondWithResult(_0x524535,null))[_0x26fc('0xb8')](handleError(_0x524535,null));};exports[_0x26fc('0x2b')]=function(_0x2190dc,_0x29d842){return db['WhatsappInteraction'][_0x26fc('0x66')]({'where':{'id':_0x2190dc[_0x26fc('0xb4')]['id']}})[_0x26fc('0x2a')](handleEntityNotFound(_0x29d842,null))[_0x26fc('0x2a')](removeEntity(_0x29d842,null))[_0x26fc('0xb8')](handleError(_0x29d842,null));};exports[_0x26fc('0xb9')]=function(_0x527cda,_0x4a55b5){return db['WhatsappInteraction'][_0x26fc('0xb9')]()[_0x26fc('0x2a')](respondWithResult(_0x4a55b5,null))[_0x26fc('0xb8')](handleError(_0x4a55b5,null));};exports[_0x26fc('0xba')]=function(_0x2e7f83,_0x1bd7b4,_0x272bd8){return db[_0x26fc('0xbb')][_0x26fc('0x66')]({'where':{'id':_0x2e7f83[_0x26fc('0xb4')]['id']}})[_0x26fc('0x2a')](handleEntityNotFound(_0x1bd7b4,null))[_0x26fc('0x2a')](function(_0x338087){if(_0x338087){return _0x338087['addMessage'](_0x2e7f83[_0x26fc('0x2f')]['ids'],_[_0x26fc('0xbc')](_0x2e7f83[_0x26fc('0x2f')],['ids','id'])||{});}})[_0x26fc('0x2a')](respondWithResult(_0x1bd7b4,null))[_0x26fc('0xb8')](handleError(_0x1bd7b4,null));};exports[_0x26fc('0xbd')]=function(_0x3af93c,_0x5762cb,_0x108eab){var _0x33dfb8={'raw':![],'where':{}};var _0x220afa={};var _0xab3347={'count':0x0,'rows':[]};return db[_0x26fc('0x3c')][_0x26fc('0xbe')]({'where':{'id':_0x3af93c[_0x26fc('0xb4')]['id']}})[_0x26fc('0x2a')](handleEntityNotFound(_0x5762cb,null))[_0x26fc('0x2a')](function(_0x5e42bd){if(_0x5e42bd){_0x220afa[_0x26fc('0x44')]=_[_0x26fc('0x41')](db[_0x26fc('0xbb')][_0x26fc('0x3d')]);_0x220afa[_0x26fc('0x45')]=_[_0x26fc('0x41')](_0x3af93c[_0x26fc('0x45')]);_0x220afa[_0x26fc('0x42')]=_['intersection'](_0x220afa['model'],_0x220afa[_0x26fc('0x45')]);_0x33dfb8['attributes']=_[_0x26fc('0x43')](_0x220afa[_0x26fc('0x44')],qs[_0x26fc('0x47')](_0x3af93c[_0x26fc('0x45')][_0x26fc('0x47')]));_0x33dfb8[_0x26fc('0x46')]=_0x33dfb8['attributes'][_0x26fc('0x6e')]?_0x33dfb8[_0x26fc('0x46')]:_0x220afa[_0x26fc('0x44')];if(!_0x3af93c[_0x26fc('0x45')]['hasOwnProperty'](_0x26fc('0x49'))){_0x33dfb8[_0x26fc('0x22')]=qs[_0x26fc('0x22')](_0x3af93c[_0x26fc('0x45')]['limit']);_0x33dfb8[_0x26fc('0x4a')]=qs[_0x26fc('0x4a')](_0x3af93c[_0x26fc('0x45')][_0x26fc('0x4a')]);}_0x33dfb8['order']=qs[_0x26fc('0x4c')](_0x3af93c[_0x26fc('0x45')][_0x26fc('0x4c')]);_0x33dfb8[_0x26fc('0x4d')]=qs[_0x26fc('0x42')](_[_0x26fc('0x4e')](_0x3af93c['query'],_0x220afa[_0x26fc('0x42')]));_0x33dfb8[_0x26fc('0x4d')][_0x26fc('0xaf')]=_0x5e42bd['id'];if(_0x3af93c[_0x26fc('0x45')]['filter']){_0x33dfb8['where']=_[_0x26fc('0x9b')](_0x33dfb8[_0x26fc('0x4d')],{'$or':_[_0x26fc('0x35')](_0x33dfb8[_0x26fc('0x46')],function(_0x13054d){var _0x10a1cf={};_0x10a1cf[_0x13054d]={'$like':'%'+_0x3af93c['query'][_0x26fc('0x83')]+'%'};return _0x10a1cf;})});}if(_0x3af93c[_0x26fc('0x45')][_0x26fc('0x7c')]){var _0x278205=_0x3af93c[_0x26fc('0x45')][_0x26fc('0x7c')][_0x26fc('0x38')](',');var _0x49cbc5={};_0x49cbc5[_0x278205[0x0]]={'$gte':moment(_0x278205[0x1])[_0x26fc('0xbf')](_0x26fc('0xc0'))};_0x33dfb8[_0x26fc('0x4d')]=_[_0x26fc('0x9b')](_0x33dfb8[_0x26fc('0x4d')],_0x49cbc5);}_0x33dfb8=_['merge']({},_0x33dfb8,_0x3af93c[_0x26fc('0xb6')]);return db[_0x26fc('0xbb')][_0x26fc('0x23')]({'where':_0x33dfb8[_0x26fc('0x4d')]})[_0x26fc('0x2a')](function(_0x31d056){_0xab3347[_0x26fc('0x23')]=_0x31d056;if(_0x3af93c[_0x26fc('0x45')][_0x26fc('0xc1')]){_0x33dfb8[_0x26fc('0xb5')]=[{'all':!![]}];}return db[_0x26fc('0xbb')][_0x26fc('0xc2')](_0x33dfb8);})[_0x26fc('0x2a')](function(_0x24dd10){_0xab3347[_0x26fc('0xb2')]=_0x24dd10;return _0xab3347;});}})[_0x26fc('0x2a')](respondWithFilteredResult(_0x5762cb,_0x33dfb8))[_0x26fc('0xb8')](handleError(_0x5762cb,null));};exports[_0x26fc('0xc3')]=function(_0x47c555,_0x18e6a6,_0x4a7a2e){return db[_0x26fc('0x3c')][_0x26fc('0x66')]({'where':{'id':_0x47c555[_0x26fc('0xb4')]['id']}})[_0x26fc('0x2a')](handleEntityNotFound(_0x18e6a6,null))[_0x26fc('0x2a')](function(_0x8a2476){if(_0x8a2476){return _0x8a2476[_0x26fc('0xc4')](_0x47c555[_0x26fc('0x2f')][_0x26fc('0xc5')],_['omit'](_0x47c555[_0x26fc('0x2f')],[_0x26fc('0xc5'),'id'])||{})['spread'](function(){return db['Tag'][_0x26fc('0xc2')]({'attributes':['id','name',_0x26fc('0x51')],'where':{'id':_0x47c555[_0x26fc('0x2f')]['ids']}});})[_0x26fc('0x2a')](function(_0x20bf92){socket[_0x26fc('0xc6')]('whatsappInteractionTags:save',{'id':Number(_0x47c555[_0x26fc('0xb4')]['id']),'tags':_0x20bf92||[]});return{'id':Number(_0x47c555[_0x26fc('0xb4')]['id']),'tags':_0x20bf92||[]};});}})['then'](respondWithResult(_0x18e6a6,null))[_0x26fc('0xb8')](handleError(_0x18e6a6,null));};exports[_0x26fc('0xc7')]=function(_0x3f662d,_0x7c4f25,_0x51e881){return db[_0x26fc('0x3c')][_0x26fc('0x66')]({'where':{'id':_0x3f662d[_0x26fc('0xb4')]['id']}})['then'](handleEntityNotFound(_0x7c4f25,null))['then'](function(_0x4910de){if(_0x4910de){return _0x4910de[_0x26fc('0xc7')](_0x3f662d[_0x26fc('0x45')][_0x26fc('0xc5')]);}})[_0x26fc('0x2a')](respondWithStatusCode(_0x7c4f25,null))['catch'](handleError(_0x7c4f25,null));};exports[_0x26fc('0xc8')]=function(_0x37c58a,_0x5a0b08){var _0x5ae8b0=moment()[_0x26fc('0xc9')]()[_0x26fc('0x98')]();var _0x214472=path[_0x26fc('0xca')](config[_0x26fc('0xcb')],_0x26fc('0xcc'),_0x26fc('0xcd'),_0x26fc('0xce'));var _0x20bb90=path[_0x26fc('0xca')](config[_0x26fc('0xcb')],_0x26fc('0xcc'),_0x26fc('0xcd'),_0x26fc('0xcf'));var _0x17241d=path[_0x26fc('0xca')](_0x214472,_0x5ae8b0);var _0x27647c=util[_0x26fc('0xbf')]('transcript-%d-%s.zip',_0x37c58a['params']['id'],_0x5ae8b0);var _0x5a16b7=path[_0x26fc('0xca')](_0x214472,_0x27647c);var _0x1c7a95=[];_0x1c7a95[_0x26fc('0xb0')]({'model':db['User'],'as':'User','attributes':['id',_0x26fc('0x36')],'raw':!![]});_0x1c7a95[_0x26fc('0xb0')]({'model':db[_0x26fc('0xd0')],'as':'Contact','attributes':['id',_0x26fc('0xd1'),'lastName'],'raw':!![]});if(_0x37c58a[_0x26fc('0x45')][_0x26fc('0xcf')]){_0x1c7a95[_0x26fc('0xb0')]({'model':db[_0x26fc('0xd2')],'as':_0x26fc('0xd2'),'raw':!![]});}var _0x5725f1=[{'model':db['WhatsappMessage'],'as':_0x26fc('0xd3'),'attributes':['id','body',_0x26fc('0xd4'),'direction',_0x26fc('0xd5'),_0x26fc('0x7e')],'include':_0x1c7a95}];_0x5725f1[_0x26fc('0xb0')]({'model':db[_0x26fc('0xd6')],'as':_0x26fc('0xd7'),'attributes':['id',_0x26fc('0x2e')]});_0x5725f1[_0x26fc('0xb0')]({'model':db[_0x26fc('0xd8')],'as':_0x26fc('0xd9'),'attributes':['id',_0x26fc('0x36')]});return db[_0x26fc('0x3c')][_0x26fc('0x66')]({'where':{'id':_0x37c58a[_0x26fc('0xb4')]['id']},'include':_0x5725f1})[_0x26fc('0x2a')](handleEntityNotFound(_0x5a0b08,null))[_0x26fc('0x2a')](function(_0x38290b){if(_0x38290b){var _0x127db8=_0x38290b['get']({'plain':!![]});fs[_0x26fc('0xda')](_0x17241d);var _0x242934={'channel':_0x26fc('0xdb'),'account':_0x127db8[_0x26fc('0xd7')][_0x26fc('0x2e')],'agent':_0x127db8[_0x26fc('0xd9')]?_0x127db8[_0x26fc('0xd9')][_0x26fc('0x36')]:_0x26fc('0xdc'),'createdAt':moment(_0x127db8[_0x26fc('0xd4')])['format']('YYYY-MM-DD\x20HH:mm:ss')[_0x26fc('0x98')](),'closedAt':_0x127db8[_0x26fc('0xdd')]?moment(_0x127db8[_0x26fc('0xde')])[_0x26fc('0xbf')]('YYYY-MM-DD\x20HH:mm:ss')[_0x26fc('0x98')]():'','disposition':_0x127db8[_0x26fc('0xdf')],'messages':_(_0x127db8[_0x26fc('0xd3')])[_0x26fc('0x83')]({'secret':![]})[_0x26fc('0x35')](function(_0x1d11f0){if(_0x1d11f0[_0x26fc('0xd2')]){var _0x17291f=path[_0x26fc('0xca')](_0x20bb90,_0x1d11f0['Attachment'][_0x26fc('0xe0')]);if(fs[_0x26fc('0xe1')](_0x17291f)){fs[_0x26fc('0xe2')](_0x17291f)[_0x26fc('0xe3')](fs[_0x26fc('0xe4')](path['join'](_0x17241d,_0x1d11f0[_0x26fc('0xd2')][_0x26fc('0xe0')])));}}return{'date':moment(_0x1d11f0['createdAt'])[_0x26fc('0xbf')](_0x26fc('0xc0'))['toString'](),'sender':_0x1d11f0['direction']===_0x26fc('0xe5')?_0x1d11f0[_0x26fc('0xd8')]?_0x1d11f0[_0x26fc('0xd8')][_0x26fc('0x36')]:_0x26fc('0xe6'):_0x1d11f0[_0x26fc('0x6b')][_0x26fc('0xd1')]+(_0x1d11f0[_0x26fc('0x6b')]['lastName']?'\x20'+_0x1d11f0[_0x26fc('0x6b')][_0x26fc('0xe7')]:''),'body':_0x1d11f0[_0x26fc('0xd2')]?_0x1d11f0[_0x26fc('0xd2')][_0x26fc('0xe0')]:_0x1d11f0['body'],'direction':_0x1d11f0[_0x26fc('0xe8')]===_0x26fc('0xe5')?'A':'C','secret':_0x1d11f0[_0x26fc('0xd5')],'read':_0x1d11f0[_0x26fc('0x7e')],'attachment':_0x1d11f0[_0x26fc('0xd2')]};})['value']()};return ejs[_0x26fc('0xe9')](path[_0x26fc('0xca')](config[_0x26fc('0xcb')],'server',_0x26fc('0xea'),_0x26fc('0xeb')),{'interaction':_0x242934})[_0x26fc('0x2a')](function(_0x159108){var _0x4caa20=path[_0x26fc('0xca')](_0x17241d,'transcript-'+_0x127db8['id']+'-'+_0x5ae8b0+_0x26fc('0xec'));var _0x2fdc3c={'path':_0x4caa20,'channel':_0x26fc('0xdb'),'interactionId':_0x127db8['id']};return pdf['createPdfFromHTML'](_0x159108,_0x2fdc3c);})[_0x26fc('0x2a')](function(){return new BPromise(function(_0x267628,_0x521f39){zipdir(_0x17241d,{'saveTo':_0x5a16b7},function(_0xde348e,_0x52b79f){if(_0xde348e)return _0x521f39(_0xde348e);return _0x267628(_0x52b79f);});})[_0x26fc('0x2a')](function(){return new BPromise(function(_0x1eab0e,_0xe44e85){rimraf(_0x17241d,function(_0x361c69){if(_0x361c69)_0xe44e85(_0x361c69);return _0x1eab0e();});});})[_0x26fc('0x2a')](function(){return _0x5a0b08['download'](_0x5a16b7,_0x27647c,function(_0x738c3e){if(_0x738c3e){console[_0x26fc('0xed')](_0x26fc('0xee'),_0x738c3e);}else{fs['unlinkSync'](_0x5a16b7);}});});});}else{return _0x5a0b08['sendStatus'](0xc8);}})[_0x26fc('0xb8')](handleError(_0x5a0b08,null));};