Built motion from commit 616d3211.|2.6.4
[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 _0x8861=['read','`unreadMessages`\x20>\x200','tag','isArray','CONCAT(\x27,\x27,\x20`TagIds`,\x20\x27,\x27)\x20LIKE\x20?','filter','replace','i.id\x20LIKE\x20?','MATCH\x20(me.body)\x20AGAINST\x20(?\x20IN\x20BOOLEAN\x20MODE)','getFullTextValue','i.id','Sequelize','SELECT','SUM(me.read\x20=\x200\x20AND\x20me.direction\x20=\x20\x27in\x27)','order','DESC','isEmpty','t.id\x20IN\x20?','resolve','i.WhatsappAccountId\x20IS\x20NOT\x20NULL','user','role','i.WhatsappAccountId\x20IS\x20NULL','agent','sequelize','toString','keyBy','merge','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','contactName','i.*','c.firstName','Contact.firstName','c.lastName','Contact.lastName','c.email','Contact.email','c.phone','Contact.phone','Contact.mobile','Contact.fax','o.fullname','Owner.fullname','o.internal','Owner.internal','GROUP_CONCAT(DISTINCT\x20t.id)','ui.WhatsappInteractionId\x20IN\x20?','WhatsappInteractionId','all','show','params','includeAll','include','options','create','body','update','describe','addMessage','WhatsappMessage','omit','ids','getMessages','findOne','hasOwnProperty','format','YYYY-MM-DD\x20HH:mm:ss','setTags','spread','findAll','emit','whatsappInteractionTags:save','removeTags','download','join','root','server','files','attachments','transcript-%d-%s.zip','CmContact','firstName','lastName','Attachment','Messages','direction','secret','WhatsappAccount','Account','Owner','get','mkdirSync','Whatsapp','N.A.','closedAt','disposition','basename','existsSync','createReadStream','pipe','createWriteStream','out','System','transcript-','.pdf','createPdfFromHTML','log','zip-dir','fast-json-patch','request-promise','moment','bluebird','mustache','util','path','sox','to-csv','ejs','fs-extra','lodash','squel','crypto','desk.js','querystring','ioredis','../../components/parsers/qs','../../components/parsers/advancedSearch','../../components/export/pdf','../../config/license/hardware','../../config/logger','api','../../config/environment','../../config/license/util','../../mysqldb','redis','defaults','./whatsappInteraction.socket','register','sendStatus','end','json','offset','undefined','limit','count','status','set','Content-Range','reject','save','then','destroy','stack','name','send','map','fullname','TagIds','Tags','push','catch','waiting','answered','unmanaged','total','closed','UserId','talking','index','WhatsappInteraction','rawAttributes','fieldName','type','key','model','query','keys','filters','intersection','attributes','fields','nolimit','sort','where','pick','select','field','color','from','u.fullname','ui.WhatsappInteractionId','user_has_whatsapp_interactions','left_join','users','whatsapp_interactions','cm_contacts','whatsapp_messages','tools_tags','expr','search','length','find','unreadMessages','value','having','`\x20=\x200','Contact','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?','ContactId','text','start','Tag','split','forEach','union','User','operator','$substring','buildExpression','some','WhatsappAccountId','i.WhatsappAccountId\x20IN\x20?','concat','createdAt','parse','null','\x20IS\x20NULL'];(function(_0x2ac3b9,_0xce831d){var _0x3e6c19=function(_0x27bdd4){while(--_0x27bdd4){_0x2ac3b9['push'](_0x2ac3b9['shift']());}};_0x3e6c19(++_0xce831d);}(_0x8861,0x6d));var _0x1886=function(_0x10d8bb,_0x1b9a0a){_0x10d8bb=_0x10d8bb-0x0;var _0x1fb518=_0x8861[_0x10d8bb];return _0x1fb518;};'use strict';var emlformat=require('eml-format');var rimraf=require('rimraf');var zipdir=require(_0x1886('0x0'));var jsonpatch=require(_0x1886('0x1'));var rp=require(_0x1886('0x2'));var moment=require(_0x1886('0x3'));var BPromise=require(_0x1886('0x4'));var Mustache=require(_0x1886('0x5'));var util=require(_0x1886('0x6'));var path=require(_0x1886('0x7'));var sox=require(_0x1886('0x8'));var csv=require(_0x1886('0x9'));var ejs=require(_0x1886('0xa'));var fs=require('fs');var fs_extra=require(_0x1886('0xb'));var _=require(_0x1886('0xc'));var squel=require(_0x1886('0xd'));var crypto=require(_0x1886('0xe'));var jsforce=require('jsforce');var deskjs=require(_0x1886('0xf'));var toCsv=require('to-csv');var querystring=require(_0x1886('0x10'));var Papa=require('papaparse');var Redis=require(_0x1886('0x11'));var authService=require('../../components/auth/service');var qs=require(_0x1886('0x12'));var as=require(_0x1886('0x13'));var pdf=require(_0x1886('0x14'));var hardwareService=require(_0x1886('0x15'));var logger=require(_0x1886('0x16'))(_0x1886('0x17'));var utils=require('../../config/utils');var config=require(_0x1886('0x18'));var licenseUtil=require(_0x1886('0x19'));var db=require(_0x1886('0x1a'))['db'];config[_0x1886('0x1b')]=_[_0x1886('0x1c')](config[_0x1886('0x1b')],{'host':'localhost','port':0x18eb});var socket=require('socket.io-emitter')(new Redis(config[_0x1886('0x1b')]));require(_0x1886('0x1d'))[_0x1886('0x1e')](socket);function respondWithStatusCode(_0x4e856e,_0xb22f22){_0xb22f22=_0xb22f22||0xcc;return function(_0x43f020){if(_0x43f020){return _0x4e856e[_0x1886('0x1f')](_0xb22f22);}return _0x4e856e['status'](_0xb22f22)[_0x1886('0x20')]();};}function respondWithResult(_0x4083d5,_0x5afc6b){_0x5afc6b=_0x5afc6b||0xc8;return function(_0x18f7a6){if(_0x18f7a6){return _0x4083d5['status'](_0x5afc6b)[_0x1886('0x21')](_0x18f7a6);}};}function respondWithFilteredResult(_0x4cb628,_0x59e422){return function(_0x2faf4e){if(_0x2faf4e){var _0x33765e=typeof _0x59e422[_0x1886('0x22')]===_0x1886('0x23')&&typeof _0x59e422[_0x1886('0x24')]===_0x1886('0x23');var _0x433507=_0x2faf4e['count'];var _0x2a4f0a=_0x33765e?0x0:_0x59e422[_0x1886('0x22')];var _0x49dc6c=_0x33765e?_0x2faf4e[_0x1886('0x25')]:_0x59e422[_0x1886('0x22')]+_0x59e422[_0x1886('0x24')];var _0x5dc431;if(_0x49dc6c>=_0x433507){_0x49dc6c=_0x433507;_0x5dc431=0xc8;}else{_0x5dc431=0xce;}_0x4cb628[_0x1886('0x26')](_0x5dc431);return _0x4cb628[_0x1886('0x27')](_0x1886('0x28'),_0x2a4f0a+'-'+_0x49dc6c+'/'+_0x433507)[_0x1886('0x21')](_0x2faf4e);}return null;};}function patchUpdates(_0x1e34ac){return function(_0x5e65eb){try{jsonpatch['apply'](_0x5e65eb,_0x1e34ac,!![]);}catch(_0x3f7bb1){return BPromise[_0x1886('0x29')](_0x3f7bb1);}return _0x5e65eb[_0x1886('0x2a')]();};}function saveUpdates(_0x3f7caa,_0x2bf330){return function(_0x5081b0){if(_0x5081b0){return _0x5081b0['update'](_0x3f7caa)[_0x1886('0x2b')](function(_0x3c4d84){return _0x3c4d84;});}return null;};}function removeEntity(_0x195846,_0x4b7265){return function(_0x2d2dff){if(_0x2d2dff){return _0x2d2dff[_0x1886('0x2c')]()[_0x1886('0x2b')](function(){_0x195846[_0x1886('0x26')](0xcc)[_0x1886('0x20')]();});}};}function handleEntityNotFound(_0x1a9ce3,_0xcd0ac5){return function(_0x148cf5){if(!_0x148cf5){_0x1a9ce3[_0x1886('0x1f')](0x194);}return _0x148cf5;};}function handleError(_0x1cd80d,_0x13fe64){_0x13fe64=_0x13fe64||0x1f4;return function(_0x177307){logger['error'](_0x177307[_0x1886('0x2d')]);if(_0x177307[_0x1886('0x2e')]){delete _0x177307[_0x1886('0x2e')];}_0x1cd80d['status'](_0x13fe64)[_0x1886('0x2f')](_0x177307);};}function getInteractionUsers(_0x2539c6,_0x3d1142){return new BPromise(function(_0x4b9985,_0x553634){try{if(_0x3d1142[_0x2539c6['id']]){_0x2539c6['Users']=_[_0x1886('0x30')](_0x3d1142[_0x2539c6['id']],function(_0x290950){return{'id':_0x290950['id'],'fullname':_0x290950[_0x1886('0x31')]};});}}catch(_0x33db06){_0x553634(_0x33db06);}_0x4b9985(_0x2539c6);});}function getInteractionTags(_0x25a905,_0x33c2fd){return new BPromise(function(_0x4880ea,_0x1510dc){try{if(_0x25a905[_0x1886('0x32')]){_0x25a905['Tags']=[];_0x25a905[_0x1886('0x32')]['split'](',')['forEach'](function(_0x2ea6d8){_0x25a905[_0x1886('0x33')][_0x1886('0x34')](_0x33c2fd[_0x2ea6d8]);});}delete _0x25a905[_0x1886('0x32')];}catch(_0x1b47c4){_0x1510dc(_0x1b47c4);}_0x4880ea(_0x25a905);});}function updateWhatsappInteraction(_0x3241d2,_0x326bab,_0x242e88){return new BPromise(function(_0x17d3b9,_0x279e85){return getInteractionUsers(_0x3241d2,_0x242e88)['then'](function(_0x4adcc5){return getInteractionTags(_0x4adcc5,_0x326bab);})[_0x1886('0x2b')](function(_0x504ac1){return getStateInteraction(_0x504ac1);})[_0x1886('0x2b')](function(_0x117505){_0x17d3b9(_0x117505);})[_0x1886('0x35')](function(_0x25be39){_0x279e85(_0x25be39);});});}function getStateInteraction(_0x55637d){return new BPromise(function(_0x8d84cc){_0x55637d[_0x1886('0x36')]=0x0;_0x55637d['talking']=0x0;_0x55637d[_0x1886('0x37')]=0x0;_0x55637d[_0x1886('0x38')]=0x0;_0x55637d[_0x1886('0x39')]=0x0;if(!_0x55637d[_0x1886('0x3a')]&&!_0x55637d[_0x1886('0x3b')]){_0x55637d[_0x1886('0x36')]+=0x1;}if(!_0x55637d[_0x1886('0x3a')]&&_0x55637d[_0x1886('0x3b')]){_0x55637d['talking']+=0x1;if(_0x55637d[_0x1886('0x36')]){_0x55637d[_0x1886('0x36')]-=0x1;}}if(_0x55637d[_0x1886('0x3a')]&&_0x55637d[_0x1886('0x3b')]){_0x55637d[_0x1886('0x37')]+=0x1;if(_0x55637d['talking']){_0x55637d[_0x1886('0x3c')]-=0x1;}}if(_0x55637d['closed']&&!_0x55637d['UserId']){_0x55637d['unmanaged']+=0x1;if(_0x55637d[_0x1886('0x36')]){_0x55637d[_0x1886('0x36')]-=0x1;}}_0x55637d['total']+=0x1;_0x8d84cc(_0x55637d);});}exports[_0x1886('0x3d')]=function(_0x4854dd,_0x52ae24){var _0x99a93={},_0xe85aad={},_0x446552={'count':0x0,'rows':[]};var _0x3a3e88=_[_0x1886('0x30')](db[_0x1886('0x3e')][_0x1886('0x3f')],function(_0x2cfcba){return{'name':_0x2cfcba[_0x1886('0x40')],'type':_0x2cfcba[_0x1886('0x41')][_0x1886('0x42')]};});_0xe85aad[_0x1886('0x43')]=_[_0x1886('0x30')](_0x3a3e88,_0x1886('0x2e'));_0xe85aad[_0x1886('0x44')]=_[_0x1886('0x45')](_0x4854dd[_0x1886('0x44')]);_0xe85aad[_0x1886('0x46')]=_[_0x1886('0x47')](_0xe85aad[_0x1886('0x43')],_0xe85aad[_0x1886('0x44')]);_0x99a93[_0x1886('0x48')]=_[_0x1886('0x47')](_0xe85aad[_0x1886('0x43')],qs[_0x1886('0x49')](_0x4854dd['query'][_0x1886('0x49')]));_0x99a93['attributes']=_0x99a93[_0x1886('0x48')]['length']?_0x99a93[_0x1886('0x48')]:_0xe85aad['model'];if(!_0x4854dd[_0x1886('0x44')]['hasOwnProperty'](_0x1886('0x4a'))){_0x99a93['limit']=qs[_0x1886('0x24')](_0x4854dd['query'][_0x1886('0x24')]);_0x99a93['offset']=qs[_0x1886('0x22')](_0x4854dd['query'][_0x1886('0x22')]);}_0x99a93['order']=qs[_0x1886('0x4b')](_0x4854dd[_0x1886('0x44')]['sort']);_0x99a93[_0x1886('0x4c')]=qs[_0x1886('0x46')](_[_0x1886('0x4d')](_0x4854dd['query'],_0xe85aad[_0x1886('0x46')]),_0x3a3e88);var _0x22b229=[];var _0x55f03d=squel[_0x1886('0x4e')]()[_0x1886('0x4f')]('id')[_0x1886('0x4f')](_0x1886('0x2e'))[_0x1886('0x4f')](_0x1886('0x50'))[_0x1886('0x51')]('tools_tags');var _0xeeeab3=squel['select']()['field']('u.id')[_0x1886('0x4f')](_0x1886('0x52'))['field'](_0x1886('0x53'))[_0x1886('0x51')](_0x1886('0x54'),'ui')[_0x1886('0x55')](_0x1886('0x56'),'u','u.id\x20=\x20ui.UserId');var _0x198fe2=squel['select']()[_0x1886('0x51')](_0x1886('0x57'),'i')[_0x1886('0x55')](_0x1886('0x58'),'c','c.id\x20=\x20i.ContactId\x20AND\x20c.deletedAt\x20IS\x20NULL')[_0x1886('0x55')](_0x1886('0x56'),'o','o.id\x20=\x20i.UserId')['left_join']('whatsapp_accounts','a','a.id\x20=\x20i.WhatsappAccountId')['left_join'](_0x1886('0x59'),'me','me.WhatsappInteractionId\x20=\x20i.id')[_0x1886('0x55')]('whatsapp_interaction_has_tags','it','it.WhatsappInteractionId\x20=\x20i.id')[_0x1886('0x55')](_0x1886('0x5a'),'t','t.id\x20=\x20it.TagId');var _0x3f32c1=squel[_0x1886('0x5b')]();var _0x3e90c6=[];var _0x2dcf25=squel[_0x1886('0x5b')]();var _0x368e1b;if(_0x4854dd[_0x1886('0x44')][_0x1886('0x5c')]){_0x368e1b=as['parseSearch'](_0x4854dd[_0x1886('0x44')]['search']);var _0x4ec571=_0x368e1b['sqlOperator'];for(var _0xd235ed=0x0;_0xd235ed<_0x368e1b['conditions'][_0x1886('0x5d')];_0xd235ed++){var _0x57254b=_0x368e1b['conditions'][_0xd235ed];var _0x3755af='i';var _0x4edd14=_[_0x1886('0x5e')](_0x3a3e88,['name',_0x57254b[_0x1886('0x4f')]]);if(!_0x4edd14){switch(_0x57254b['field']){case _0x1886('0x5f'):if(_0x57254b[_0x1886('0x60')]==0x1){_0x198fe2[_0x1886('0x61')]('`'+_0x57254b[_0x1886('0x4f')]+_0x1886('0x62'));}else{_0x198fe2['having']('`'+_0x57254b[_0x1886('0x4f')]+'`\x20>\x200');}break;case _0x1886('0x63'):if(_0x57254b['operator']==='$substring'){if(_0x57254b['value']['split']('\x20')[_0x1886('0x5d')]>0x1){_0x3f32c1[_0x4ec571](_0x1886('0x64'),qs['getFullTextValue'](_0x57254b[_0x1886('0x60')]),null);}else{var _0x4b4c27='%'+_0x57254b['value']+'%';_0x3f32c1[_0x4ec571](_0x1886('0x65'),_0x4b4c27,_0x4b4c27,_0x4b4c27);}}else{_0x4edd14=_[_0x1886('0x5e')](_0x3a3e88,[_0x1886('0x2e'),'ContactId']);_0x57254b[_0x1886('0x4f')]=_0x1886('0x66');_0x3af56c=as['buildExpression'](_0x3755af,_0x4edd14['type'],_0x57254b);_0x3f32c1[_0x4ec571](_0x3af56c[_0x1886('0x67')],_0x3af56c['value'][_0x1886('0x68')],_0x3af56c[_0x1886('0x60')][_0x1886('0x20')]);}break;case _0x1886('0x69'):var _0x20ad7f=_0x57254b['value'][_0x1886('0x6a')](',')['map'](function(_0x162bc8){return Number(_0x162bc8);});_0x20ad7f[_0x1886('0x6b')](function(_0x361133){_0x2dcf25['or']('CONCAT(\x27,\x27,\x20`TagIds`,\x20\x27,\x27)\x20LIKE\x20?','%,'+_0x361133+',%');});_0x3e90c6=_[_0x1886('0x6c')](_0x3e90c6,_0x20ad7f);break;case _0x1886('0x6d'):if(_0x57254b[_0x1886('0x6e')]===_0x1886('0x6f')){_0x3f32c1[_0x4ec571]('CONCAT_WS(\x27\x20\x27,\x20o.name,\x20o.fullname)\x20LIKE\x20?','%'+_0x57254b[_0x1886('0x60')]+'%',null);}else{_0x4edd14=_['find'](_0x3a3e88,[_0x1886('0x2e'),_0x1886('0x3b')]);_0x57254b['field']='UserId';_0x3af56c=as[_0x1886('0x70')](_0x3755af,_0x4edd14[_0x1886('0x41')],_0x57254b);_0x3f32c1[_0x4ec571](_0x3af56c[_0x1886('0x67')],_0x3af56c[_0x1886('0x60')][_0x1886('0x68')],_0x3af56c[_0x1886('0x60')][_0x1886('0x20')]);}break;case'body':_0x3af56c=as['buildExpression']('me',null,_0x57254b);_0x3f32c1[_0x4ec571](_0x3af56c[_0x1886('0x67')],_0x3af56c[_0x1886('0x60')][_0x1886('0x68')],_0x3af56c[_0x1886('0x60')]['end']);break;}}else{var _0x3af56c=as[_0x1886('0x70')](_0x3755af,_0x4edd14['type'],_0x57254b);_0x3f32c1[_0x4ec571](_0x3af56c['text'],_0x3af56c[_0x1886('0x60')][_0x1886('0x68')],_0x3af56c[_0x1886('0x60')][_0x1886('0x20')]);}}}else{var _0x2cf969=_(_0x4854dd[_0x1886('0x44')])['keys']()[_0x1886('0x30')](function(_0x58ecc7){return _[_0x1886('0x71')](_0x3a3e88,[_0x1886('0x2e'),_0x58ecc7])?_0x58ecc7:undefined;})['compact']()[_0x1886('0x60')]();if(!_['isEmpty'](_0x2cf969)){_0x2cf969[_0x1886('0x6b')](function(_0xb78b2c){if(_0xb78b2c===_0x1886('0x72')){_0x198fe2[_0x1886('0x4c')](_0x1886('0x73'),[][_0x1886('0x74')](_0x4854dd['query'][_0xb78b2c]));}else if(_0xb78b2c===_0x1886('0x75')){var _0xf34668=JSON[_0x1886('0x76')](_0x4854dd[_0x1886('0x44')][_0xb78b2c])['$gte'];var _0x13e4e4=JSON['parse'](_0x4854dd[_0x1886('0x44')][_0xb78b2c])['$lte'];_0x198fe2[_0x1886('0x4c')]('i.createdAt\x20BETWEEN\x20?\x20AND\x20?',_0xf34668,_0x13e4e4);}else{if(_0x4854dd['query'][_0xb78b2c]===_0x1886('0x77')){_0x198fe2[_0x1886('0x4c')]('i.'+_0xb78b2c+_0x1886('0x78'));}else{_0x198fe2[_0x1886('0x4c')]('i.'+_0xb78b2c+'\x20=\x20?',_0x4854dd[_0x1886('0x44')][_0xb78b2c]);}}});}if(_0x4854dd[_0x1886('0x44')]['read']){if(_0x4854dd[_0x1886('0x44')][_0x1886('0x79')]==0x1){_0x198fe2[_0x1886('0x61')]('`unreadMessages`\x20=\x200');}else{_0x198fe2[_0x1886('0x61')](_0x1886('0x7a'));}}if(_0x4854dd['query'][_0x1886('0x7b')]){_0x3e90c6=_[_0x1886('0x7c')](_0x4854dd[_0x1886('0x44')][_0x1886('0x7b')])?_0x4854dd[_0x1886('0x44')][_0x1886('0x7b')]:new Array(_0x4854dd[_0x1886('0x44')][_0x1886('0x7b')]);_0x3e90c6[_0x1886('0x6b')](function(_0xb8c1d4){_0x2dcf25['or'](_0x1886('0x7d'),'%,'+_0xb8c1d4+',%');});}if(_0x4854dd[_0x1886('0x44')]['filter']){var _0x28975f=_0x4854dd[_0x1886('0x44')][_0x1886('0x7e')][_0x1886('0x7f')]('\x5c','\x5c\x5c')['replace'](/'/g,'\x27\x27');if(qs['isNumeric'](_0x28975f)){_0x3f32c1['or'](_0x1886('0x80'),_0x28975f+'%')['or']('i.phone\x20LIKE\x20?',_0x28975f+'%');}else{_0x3f32c1['or'](_0x1886('0x81'),qs[_0x1886('0x82')](_0x28975f))['or']('CONCAT(c.firstName,\x20\x27\x20\x27,\x20c.lastName)\x20LIKE\x20?',_0x28975f+'%');}}}_0x198fe2['where'](_0x3f32c1);_0x198fe2['group'](_0x1886('0x83'));var _0x4a53b9={'type':db[_0x1886('0x84')]['QueryTypes'][_0x1886('0x85')],'raw':!![]};var _0x2f1e31=_0x198fe2['clone']();_0x2f1e31[_0x1886('0x4f')](_0x1886('0x83'));_0x2f1e31[_0x1886('0x4f')](_0x1886('0x86'),_0x1886('0x5f'));if(_0x99a93[_0x1886('0x87')]){_0x99a93['order'][_0x1886('0x6b')](function(_0x58c476){var _0x16343a=_0x58c476[0x0]==='contactName'?_0x58c476[0x0]:'i.'+_0x58c476[0x0];_0x198fe2['order'](_0x16343a,_0x58c476[0x1]===_0x1886('0x88')?![]:!![]);});}if(!_[_0x1886('0x89')](_0x3e90c6)){_0x198fe2[_0x1886('0x61')](_0x2dcf25);_0x2f1e31[_0x1886('0x4c')](_0x1886('0x8a'),_0x3e90c6);}BPromise[_0x1886('0x8b')]()[_0x1886('0x2b')](function(){if(!_0x368e1b){if(_0x4854dd[_0x1886('0x44')]['WhatsappAccountId'])return;_0x2f1e31[_0x1886('0x4c')](_0x1886('0x8c'));_0x198fe2[_0x1886('0x4c')](_0x1886('0x8c'));return;}if(_0x4854dd[_0x1886('0x8d')][_0x1886('0x8e')]!=='agent')return;return _0x4854dd[_0x1886('0x8d')]['getWhatsappAccounts']({'attributes':['id'],'raw':!![]})[_0x1886('0x2b')](function(_0x12617e){if(_['isEmpty'](_0x12617e)){_0x2f1e31[_0x1886('0x4c')](_0x1886('0x8f'));_0x198fe2[_0x1886('0x4c')](_0x1886('0x8f'));}else{_0x2f1e31[_0x1886('0x4c')](_0x1886('0x73'),_[_0x1886('0x30')](_0x12617e,'id'));_0x198fe2[_0x1886('0x4c')](_0x1886('0x73'),_['map'](_0x12617e,'id'));}});})['then'](function(){if(_0x4854dd[_0x1886('0x8d')][_0x1886('0x8e')]===_0x1886('0x90')&&!_0x368e1b&&!_0x4854dd[_0x1886('0x44')][_0x1886('0x72')])return[];return db[_0x1886('0x91')][_0x1886('0x44')](_0x2f1e31[_0x1886('0x92')](),_0x4a53b9);})['then'](function(_0x37e86b){_0x446552['count']=_0x37e86b[_0x1886('0x5d')];if(_0x446552[_0x1886('0x25')]===0x0)return[];return db[_0x1886('0x91')]['query'](_0x55f03d[_0x1886('0x92')](),_0x4a53b9)[_0x1886('0x2b')](function(_0x53d500){_0x22b229=_[_0x1886('0x93')](_0x53d500,'id');_0x4a53b9=_[_0x1886('0x94')](_0x4a53b9,{'model':db[_0x1886('0x3e')],'mapToModel':!![],'raw':!![],'nest':!![]});if(_0x4854dd['query'][_0x1886('0x49')]){_0x99a93[_0x1886('0x48')][_0x1886('0x6b')](function(_0xd1594){_0x198fe2['field']('i.'+_0xd1594);});}else{_0x198fe2[_0x1886('0x4f')](_0x1886('0x86'),_0x1886('0x5f'));_0x198fe2[_0x1886('0x4f')]('a.key',_0x1886('0x95'));_0x198fe2[_0x1886('0x4f')]('a.mandatoryDisposition',_0x1886('0x96'));_0x198fe2[_0x1886('0x4f')](_0x1886('0x97'),_0x1886('0x98'));_0x198fe2[_0x1886('0x4f')](_0x1886('0x99'));_0x198fe2[_0x1886('0x4f')](_0x1886('0x9a'),_0x1886('0x9b'));_0x198fe2[_0x1886('0x4f')](_0x1886('0x9c'),_0x1886('0x9d'));_0x198fe2[_0x1886('0x4f')](_0x1886('0x9e'),_0x1886('0x9f'));_0x198fe2['field'](_0x1886('0xa0'),_0x1886('0xa1'));_0x198fe2[_0x1886('0x4f')]('c.mobile',_0x1886('0xa2'));_0x198fe2[_0x1886('0x4f')]('c.fax',_0x1886('0xa3'));_0x198fe2[_0x1886('0x4f')](_0x1886('0xa4'),_0x1886('0xa5'));_0x198fe2['field'](_0x1886('0xa6'),_0x1886('0xa7'));_0x198fe2[_0x1886('0x4f')](_0x1886('0xa8'),_0x1886('0x32'));}if(_0x99a93['limit'])_0x198fe2[_0x1886('0x24')](_0x99a93[_0x1886('0x24')]);if(_0x99a93[_0x1886('0x22')])_0x198fe2['offset'](_0x99a93['offset']);return db[_0x1886('0x91')][_0x1886('0x44')](_0x198fe2[_0x1886('0x92')](),_0x4a53b9);})[_0x1886('0x2b')](function(_0x41d43b){if(_0x41d43b[_0x1886('0x5d')]>0x0)_0xeeeab3[_0x1886('0x4c')](_0x1886('0xa9'),_['map'](_0x41d43b,'id'));return db[_0x1886('0x91')][_0x1886('0x44')](_0xeeeab3[_0x1886('0x92')](),_0x4a53b9)[_0x1886('0x2b')](function(_0x1eeb99){var _0x583380=_['groupBy'](_0x1eeb99,_0x1886('0xaa'));var _0x301b0c=[];_0x41d43b[_0x1886('0x6b')](function(_0x1dd1f4){_0x301b0c[_0x1886('0x34')](updateWhatsappInteraction(_0x1dd1f4,_0x22b229,_0x583380));});return BPromise[_0x1886('0xab')](_0x301b0c);});});})[_0x1886('0x2b')](function(_0x165ffd){_0x446552['rows']=_0x165ffd;return _0x446552;})['then'](respondWithFilteredResult(_0x52ae24,_0x99a93))[_0x1886('0x35')](handleError(_0x52ae24,null));};exports[_0x1886('0xac')]=function(_0x119658,_0x35c548){var _0x14bf41={'raw':![],'where':{'id':_0x119658[_0x1886('0xad')]['id']}},_0x15103f={};_0x15103f[_0x1886('0x43')]=_[_0x1886('0x45')](db[_0x1886('0x3e')][_0x1886('0x3f')]);_0x15103f[_0x1886('0x44')]=_[_0x1886('0x45')](_0x119658[_0x1886('0x44')]);_0x15103f[_0x1886('0x46')]=_[_0x1886('0x47')](_0x15103f[_0x1886('0x43')],_0x15103f[_0x1886('0x44')]);_0x14bf41['attributes']=_[_0x1886('0x47')](_0x15103f[_0x1886('0x43')],qs[_0x1886('0x49')](_0x119658['query'][_0x1886('0x49')]));_0x14bf41[_0x1886('0x48')]=_0x14bf41[_0x1886('0x48')][_0x1886('0x5d')]?_0x14bf41[_0x1886('0x48')]:_0x15103f[_0x1886('0x43')];if(_0x119658[_0x1886('0x44')][_0x1886('0xae')]){_0x14bf41[_0x1886('0xaf')]=[{'all':!![]}];}_0x14bf41=_[_0x1886('0x94')]({},_0x14bf41,_0x119658[_0x1886('0xb0')]);return db[_0x1886('0x3e')][_0x1886('0x5e')](_0x14bf41)['then'](handleEntityNotFound(_0x35c548,null))[_0x1886('0x2b')](respondWithResult(_0x35c548,null))[_0x1886('0x35')](handleError(_0x35c548,null));};exports[_0x1886('0xb1')]=function(_0x30cb52,_0x2c45e6){return db[_0x1886('0x3e')][_0x1886('0xb1')](_0x30cb52[_0x1886('0xb2')],{})[_0x1886('0x2b')](respondWithResult(_0x2c45e6,0xc9))[_0x1886('0x35')](handleError(_0x2c45e6,null));};exports[_0x1886('0xb3')]=function(_0x2759bd,_0x68a6de){if(_0x2759bd[_0x1886('0xb2')]['id']){delete _0x2759bd[_0x1886('0xb2')]['id'];}return db['WhatsappInteraction']['find']({'where':{'id':_0x2759bd[_0x1886('0xad')]['id']}})['then'](handleEntityNotFound(_0x68a6de,null))[_0x1886('0x2b')](saveUpdates(_0x2759bd[_0x1886('0xb2')],null))[_0x1886('0x2b')](respondWithResult(_0x68a6de,null))[_0x1886('0x35')](handleError(_0x68a6de,null));};exports[_0x1886('0x2c')]=function(_0x3921bd,_0x4af6aa){return db['WhatsappInteraction'][_0x1886('0x5e')]({'where':{'id':_0x3921bd[_0x1886('0xad')]['id']}})['then'](handleEntityNotFound(_0x4af6aa,null))[_0x1886('0x2b')](removeEntity(_0x4af6aa,null))[_0x1886('0x35')](handleError(_0x4af6aa,null));};exports['describe']=function(_0x13aa23,_0x484d07){return db['WhatsappInteraction'][_0x1886('0xb4')]()[_0x1886('0x2b')](respondWithResult(_0x484d07,null))[_0x1886('0x35')](handleError(_0x484d07,null));};exports[_0x1886('0xb5')]=function(_0x170679,_0x154898,_0x3e6150){return db[_0x1886('0xb6')]['find']({'where':{'id':_0x170679['params']['id']}})[_0x1886('0x2b')](handleEntityNotFound(_0x154898,null))['then'](function(_0x573044){if(_0x573044){return _0x573044['addMessage'](_0x170679[_0x1886('0xb2')]['ids'],_[_0x1886('0xb7')](_0x170679[_0x1886('0xb2')],[_0x1886('0xb8'),'id'])||{});}})[_0x1886('0x2b')](respondWithResult(_0x154898,null))['catch'](handleError(_0x154898,null));};exports[_0x1886('0xb9')]=function(_0x528e64,_0x5bf8b8,_0x26f13c){var _0x5b3dcf={'raw':![],'where':{}};var _0x4c9309={};var _0x42d879={'count':0x0,'rows':[]};return db[_0x1886('0x3e')][_0x1886('0xba')]({'where':{'id':_0x528e64[_0x1886('0xad')]['id']}})[_0x1886('0x2b')](handleEntityNotFound(_0x5bf8b8,null))[_0x1886('0x2b')](function(_0x45e206){if(_0x45e206){_0x4c9309[_0x1886('0x43')]=_['keys'](db[_0x1886('0xb6')][_0x1886('0x3f')]);_0x4c9309[_0x1886('0x44')]=_['keys'](_0x528e64[_0x1886('0x44')]);_0x4c9309[_0x1886('0x46')]=_[_0x1886('0x47')](_0x4c9309[_0x1886('0x43')],_0x4c9309[_0x1886('0x44')]);_0x5b3dcf[_0x1886('0x48')]=_[_0x1886('0x47')](_0x4c9309['model'],qs[_0x1886('0x49')](_0x528e64[_0x1886('0x44')][_0x1886('0x49')]));_0x5b3dcf['attributes']=_0x5b3dcf['attributes'][_0x1886('0x5d')]?_0x5b3dcf[_0x1886('0x48')]:_0x4c9309[_0x1886('0x43')];if(!_0x528e64[_0x1886('0x44')][_0x1886('0xbb')](_0x1886('0x4a'))){_0x5b3dcf[_0x1886('0x24')]=qs[_0x1886('0x24')](_0x528e64[_0x1886('0x44')]['limit']);_0x5b3dcf[_0x1886('0x22')]=qs[_0x1886('0x22')](_0x528e64[_0x1886('0x44')]['offset']);}_0x5b3dcf[_0x1886('0x87')]=qs['sort'](_0x528e64['query']['sort']);_0x5b3dcf[_0x1886('0x4c')]=qs['filters'](_[_0x1886('0x4d')](_0x528e64['query'],_0x4c9309['filters']));_0x5b3dcf[_0x1886('0x4c')][_0x1886('0xaa')]=_0x45e206['id'];if(_0x528e64[_0x1886('0x44')][_0x1886('0x7e')]){_0x5b3dcf[_0x1886('0x4c')]=_[_0x1886('0x94')](_0x5b3dcf[_0x1886('0x4c')],{'$or':_[_0x1886('0x30')](_0x5b3dcf[_0x1886('0x48')],function(_0x3bee72){var _0x3f308c={};_0x3f308c[_0x3bee72]={'$like':'%'+_0x528e64[_0x1886('0x44')]['filter']+'%'};return _0x3f308c;})});}if(_0x528e64[_0x1886('0x44')]['$gte']){var _0x366740=_0x528e64['query']['$gte'][_0x1886('0x6a')](',');var _0xf50ddd={};_0xf50ddd[_0x366740[0x0]]={'$gte':moment(_0x366740[0x1])[_0x1886('0xbc')](_0x1886('0xbd'))};_0x5b3dcf[_0x1886('0x4c')]=_[_0x1886('0x94')](_0x5b3dcf['where'],_0xf50ddd);}_0x5b3dcf=_[_0x1886('0x94')]({},_0x5b3dcf,_0x528e64['options']);return db[_0x1886('0xb6')][_0x1886('0x25')]({'where':_0x5b3dcf[_0x1886('0x4c')]})[_0x1886('0x2b')](function(_0x1d2057){_0x42d879[_0x1886('0x25')]=_0x1d2057;if(_0x528e64[_0x1886('0x44')]['includeAll']){_0x5b3dcf['include']=[{'all':!![]}];}return db[_0x1886('0xb6')]['findAll'](_0x5b3dcf);})[_0x1886('0x2b')](function(_0x2e7735){_0x42d879['rows']=_0x2e7735;return _0x42d879;});}})[_0x1886('0x2b')](respondWithFilteredResult(_0x5bf8b8,_0x5b3dcf))[_0x1886('0x35')](handleError(_0x5bf8b8,null));};exports['addTags']=function(_0x597b40,_0x24ee1e,_0x1fea36){return db[_0x1886('0x3e')][_0x1886('0x5e')]({'where':{'id':_0x597b40[_0x1886('0xad')]['id']}})[_0x1886('0x2b')](handleEntityNotFound(_0x24ee1e,null))[_0x1886('0x2b')](function(_0x134865){if(_0x134865){return _0x134865[_0x1886('0xbe')](_0x597b40['body'][_0x1886('0xb8')],_[_0x1886('0xb7')](_0x597b40['body'],['ids','id'])||{})[_0x1886('0xbf')](function(){return db['Tag'][_0x1886('0xc0')]({'attributes':['id','name',_0x1886('0x50')],'where':{'id':_0x597b40[_0x1886('0xb2')][_0x1886('0xb8')]}});})[_0x1886('0x2b')](function(_0x1d27f8){socket[_0x1886('0xc1')](_0x1886('0xc2'),{'id':Number(_0x597b40[_0x1886('0xad')]['id']),'tags':_0x1d27f8||[]});return{'id':Number(_0x597b40['params']['id']),'tags':_0x1d27f8||[]};});}})[_0x1886('0x2b')](respondWithResult(_0x24ee1e,null))[_0x1886('0x35')](handleError(_0x24ee1e,null));};exports[_0x1886('0xc3')]=function(_0xe1f4f3,_0x4dd4ff,_0x192519){return db['WhatsappInteraction'][_0x1886('0x5e')]({'where':{'id':_0xe1f4f3[_0x1886('0xad')]['id']}})['then'](handleEntityNotFound(_0x4dd4ff,null))[_0x1886('0x2b')](function(_0x35e079){if(_0x35e079){return _0x35e079['removeTags'](_0xe1f4f3[_0x1886('0x44')]['ids']);}})[_0x1886('0x2b')](respondWithStatusCode(_0x4dd4ff,null))[_0x1886('0x35')](handleError(_0x4dd4ff,null));};exports[_0x1886('0xc4')]=function(_0x5eca5f,_0x16a373){var _0x57e1fa=moment()['unix']()[_0x1886('0x92')]();var _0x14ac3e=path[_0x1886('0xc5')](config[_0x1886('0xc6')],_0x1886('0xc7'),_0x1886('0xc8'),'tmp');var _0x1cb93f=path['join'](config[_0x1886('0xc6')],_0x1886('0xc7'),_0x1886('0xc8'),_0x1886('0xc9'));var _0x177549=path['join'](_0x14ac3e,_0x57e1fa);var _0x21ee77=util[_0x1886('0xbc')](_0x1886('0xca'),_0x5eca5f[_0x1886('0xad')]['id'],_0x57e1fa);var _0xcb92c=path[_0x1886('0xc5')](_0x14ac3e,_0x21ee77);var _0x8fce7d=[];_0x8fce7d['push']({'model':db['User'],'as':'User','attributes':['id',_0x1886('0x31')],'raw':!![]});_0x8fce7d[_0x1886('0x34')]({'model':db[_0x1886('0xcb')],'as':_0x1886('0x63'),'attributes':['id',_0x1886('0xcc'),_0x1886('0xcd')],'raw':!![]});if(_0x5eca5f['query'][_0x1886('0xc9')]){_0x8fce7d[_0x1886('0x34')]({'model':db[_0x1886('0xce')],'as':_0x1886('0xce'),'raw':!![]});}var _0x4e502b=[{'model':db[_0x1886('0xb6')],'as':_0x1886('0xcf'),'attributes':['id',_0x1886('0xb2'),'createdAt',_0x1886('0xd0'),_0x1886('0xd1'),_0x1886('0x79')],'include':_0x8fce7d}];_0x4e502b[_0x1886('0x34')]({'model':db[_0x1886('0xd2')],'as':_0x1886('0xd3'),'attributes':['id',_0x1886('0x2e')]});_0x4e502b[_0x1886('0x34')]({'model':db[_0x1886('0x6d')],'as':_0x1886('0xd4'),'attributes':['id',_0x1886('0x31')]});return db['WhatsappInteraction']['find']({'where':{'id':_0x5eca5f[_0x1886('0xad')]['id']},'include':_0x4e502b})[_0x1886('0x2b')](handleEntityNotFound(_0x16a373,null))['then'](function(_0xafadfd){if(_0xafadfd){var _0x3ce57b=_0xafadfd[_0x1886('0xd5')]({'plain':!![]});fs[_0x1886('0xd6')](_0x177549);var _0x335d7e={'channel':_0x1886('0xd7'),'account':_0x3ce57b[_0x1886('0xd3')][_0x1886('0x2e')],'agent':_0x3ce57b[_0x1886('0xd4')]?_0x3ce57b[_0x1886('0xd4')]['fullname']:_0x1886('0xd8'),'createdAt':moment(_0x3ce57b[_0x1886('0x75')])[_0x1886('0xbc')](_0x1886('0xbd'))[_0x1886('0x92')](),'closedAt':_0x3ce57b[_0x1886('0x3a')]?moment(_0x3ce57b[_0x1886('0xd9')])[_0x1886('0xbc')](_0x1886('0xbd'))[_0x1886('0x92')]():'','disposition':_0x3ce57b[_0x1886('0xda')],'messages':_(_0x3ce57b[_0x1886('0xcf')])['filter']({'secret':![]})[_0x1886('0x30')](function(_0x1175c8){if(_0x1175c8[_0x1886('0xce')]){var _0xfc7bd4=path[_0x1886('0xc5')](_0x1cb93f,_0x1175c8[_0x1886('0xce')][_0x1886('0xdb')]);if(fs[_0x1886('0xdc')](_0xfc7bd4)){fs[_0x1886('0xdd')](_0xfc7bd4)[_0x1886('0xde')](fs[_0x1886('0xdf')](path['join'](_0x177549,_0x1175c8['Attachment'][_0x1886('0xdb')])));}}return{'date':moment(_0x1175c8[_0x1886('0x75')])[_0x1886('0xbc')]('YYYY-MM-DD\x20HH:mm:ss')[_0x1886('0x92')](),'sender':_0x1175c8[_0x1886('0xd0')]===_0x1886('0xe0')?_0x1175c8['User']?_0x1175c8['User'][_0x1886('0x31')]:_0x1886('0xe1'):_0x1175c8[_0x1886('0x63')]['firstName']+(_0x1175c8[_0x1886('0x63')][_0x1886('0xcd')]?'\x20'+_0x1175c8[_0x1886('0x63')][_0x1886('0xcd')]:''),'body':_0x1175c8['Attachment']?_0x1175c8[_0x1886('0xce')]['basename']:_0x1175c8['body'],'direction':_0x1175c8['direction']===_0x1886('0xe0')?'A':'C','secret':_0x1175c8[_0x1886('0xd1')],'read':_0x1175c8[_0x1886('0x79')],'attachment':_0x1175c8['Attachment']};})['value']()};return ejs['renderFile'](path['join'](config[_0x1886('0xc6')],_0x1886('0xc7'),'views','downloadInteraction.ejs'),{'interaction':_0x335d7e})['then'](function(_0x6c0b48){var _0x3aaee3=path[_0x1886('0xc5')](_0x177549,_0x1886('0xe2')+_0x3ce57b['id']+'-'+_0x57e1fa+_0x1886('0xe3'));var _0x3d0c41={'path':_0x3aaee3,'channel':_0x1886('0xd7'),'interactionId':_0x3ce57b['id']};return pdf[_0x1886('0xe4')](_0x6c0b48,_0x3d0c41);})[_0x1886('0x2b')](function(){return new BPromise(function(_0x29e80b,_0x29daa8){zipdir(_0x177549,{'saveTo':_0xcb92c},function(_0x1b1151,_0x7b3fc1){if(_0x1b1151)return _0x29daa8(_0x1b1151);return _0x29e80b(_0x7b3fc1);});})['then'](function(){return new BPromise(function(_0x310b90,_0x596211){rimraf(_0x177549,function(_0x669de9){if(_0x669de9)_0x596211(_0x669de9);return _0x310b90();});});})[_0x1886('0x2b')](function(){return _0x16a373[_0x1886('0xc4')](_0xcb92c,_0x21ee77,function(_0x3e58bb){if(_0x3e58bb){console[_0x1886('0xe5')]('err',_0x3e58bb);}else{fs['unlinkSync'](_0xcb92c);}});});});}else{return _0x16a373[_0x1886('0x1f')](0xc8);}})[_0x1886('0x35')](handleError(_0x16a373,null));};