33bacd0b7a5f834b49670efca95f5564b0990d1f
[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 _0xb5fa=['lastName','renderFile','views','downloadInteraction.ejs','transcript-','createPdfFromHTML','log','err','unlinkSync','eml-format','rimraf','zip-dir','fast-json-patch','request-promise','moment','bluebird','mustache','util','path','sox','to-csv','squel','crypto','desk.js','querystring','papaparse','ioredis','../../components/parsers/qs','../../components/export/pdf','../../config/logger','api','../../mysqldb','redis','localhost','socket.io-emitter','./whatsappInteraction.socket','register','sendStatus','status','end','json','offset','undefined','limit','count','set','apply','reject','update','then','error','stack','name','Users','TagIds','Tags','split','push','catch','waiting','talking','answered','unmanaged','total','UserId','closed','index','map','rawAttributes','fieldName','type','model','query','keys','filters','intersection','attributes','fields','hasOwnProperty','sort','pick','select','field','color','from','tools_tags','u.id','u.fullname','user_has_whatsapp_interactions','users','u.id\x20=\x20ui.UserId','left_join','cm_contacts','whatsapp_accounts','whatsapp_messages','me.WhatsappInteractionId\x20=\x20i.id','whatsapp_interaction_has_tags','it.WhatsappInteractionId\x20=\x20i.id','t.id\x20=\x20it.TagId','search','parseSearch','sqlOperator','conditions','length','find','unreadMessages','value','having','`\x20=\x200','`\x20>\x200','Contact','operator','$substring','getFullTextValue','c.firstName\x20LIKE\x20?\x20OR\x20c.lastName\x20LIKE\x20?\x20OR\x20c.email\x20LIKE\x20?','ContactId','text','start','Tag','CONCAT(\x27,\x27,\x20`TagIds`,\x20\x27,\x27)\x20LIKE\x20?','User','CONCAT_WS(\x27\x20\x27,\x20o.name,\x20o.fullname)\x20LIKE\x20?','buildExpression','body','some','compact','isEmpty','forEach','i.WhatsappAccountId\x20IN\x20?','createdAt','$gte','$lte','where','i.createdAt\x20BETWEEN\x20?\x20AND\x20?','null','\x20IS\x20NULL','\x20=\x20?','read','`unreadMessages`\x20=\x200','tag','isArray','filter','replace','i.id\x20LIKE\x20?','i.phone\x20LIKE\x20?','CONCAT(c.firstName,\x20\x27\x20\x27,\x20c.lastName)\x20LIKE\x20?','group','i.id','Sequelize','QueryTypes','SUM(me.read\x20=\x200\x20AND\x20me.direction\x20=\x20\x27in\x27)','order','contactName','DESC','t.id\x20IN\x20?','resolve','WhatsappAccountId','i.WhatsappAccountId\x20IS\x20NOT\x20NULL','role','user','i.WhatsappAccountId\x20IS\x20NULL','agent','sequelize','keyBy','merge','WhatsappInteraction','a.key','Account.key','Account.mandatoryDisposition','c.firstName','Contact.firstName','Contact.lastName','c.email','Contact.email','c.phone','c.mobile','Contact.mobile','c.fax','Contact.fax','o.fullname','Owner.fullname','o.internal','Owner.internal','GROUP_CONCAT(DISTINCT\x20t.id)','toString','ui.WhatsappInteractionId\x20IN\x20?','groupBy','WhatsappInteractionId','all','rows','params','includeAll','options','create','describe','WhatsappMessage','addMessage','ids','omit','getMessages','nolimit','format','findAll','setTags','emit','whatsappInteractionTags:save','removeTags','download','root','server','files','tmp','join','attachments','transcript-%d-%s.zip','fullname','CmContact','firstName','Attachment','Messages','direction','secret','WhatsappAccount','Account','Owner','get','mkdirSync','Whatsapp','N.A.','YYYY-MM-DD\x20HH:mm:ss','closedAt','disposition','existsSync','createReadStream','pipe','createWriteStream','basename','out','System'];(function(_0x37c096,_0x3abf0f){var _0x29c4f3=function(_0x436e67){while(--_0x436e67){_0x37c096['push'](_0x37c096['shift']());}};_0x29c4f3(++_0x3abf0f);}(_0xb5fa,0xf8));var _0xab5f=function(_0x52326e,_0x18f7b5){_0x52326e=_0x52326e-0x0;var _0x39b11e=_0xb5fa[_0x52326e];return _0x39b11e;};'use strict';var emlformat=require(_0xab5f('0x0'));var rimraf=require(_0xab5f('0x1'));var zipdir=require(_0xab5f('0x2'));var jsonpatch=require(_0xab5f('0x3'));var rp=require(_0xab5f('0x4'));var moment=require(_0xab5f('0x5'));var BPromise=require(_0xab5f('0x6'));var Mustache=require(_0xab5f('0x7'));var util=require(_0xab5f('0x8'));var path=require(_0xab5f('0x9'));var sox=require(_0xab5f('0xa'));var csv=require(_0xab5f('0xb'));var ejs=require('ejs');var fs=require('fs');var fs_extra=require('fs-extra');var _=require('lodash');var squel=require(_0xab5f('0xc'));var crypto=require(_0xab5f('0xd'));var jsforce=require('jsforce');var deskjs=require(_0xab5f('0xe'));var toCsv=require(_0xab5f('0xb'));var querystring=require(_0xab5f('0xf'));var Papa=require(_0xab5f('0x10'));var Redis=require(_0xab5f('0x11'));var authService=require('../../components/auth/service');var qs=require(_0xab5f('0x12'));var as=require('../../components/parsers/advancedSearch');var pdf=require(_0xab5f('0x13'));var hardwareService=require('../../config/license/hardware');var logger=require(_0xab5f('0x14'))(_0xab5f('0x15'));var utils=require('../../config/utils');var config=require('../../config/environment');var licenseUtil=require('../../config/license/util');var db=require(_0xab5f('0x16'))['db'];config[_0xab5f('0x17')]=_['defaults'](config[_0xab5f('0x17')],{'host':_0xab5f('0x18'),'port':0x18eb});var socket=require(_0xab5f('0x19'))(new Redis(config[_0xab5f('0x17')]));require(_0xab5f('0x1a'))[_0xab5f('0x1b')](socket);function respondWithStatusCode(_0x4319c7,_0x425053){_0x425053=_0x425053||0xcc;return function(_0x32def2){if(_0x32def2){return _0x4319c7[_0xab5f('0x1c')](_0x425053);}return _0x4319c7[_0xab5f('0x1d')](_0x425053)[_0xab5f('0x1e')]();};}function respondWithResult(_0x177e6d,_0x22588b){_0x22588b=_0x22588b||0xc8;return function(_0x3d5485){if(_0x3d5485){return _0x177e6d[_0xab5f('0x1d')](_0x22588b)[_0xab5f('0x1f')](_0x3d5485);}};}function respondWithFilteredResult(_0x340190,_0x4aca7d){return function(_0x2caadb){if(_0x2caadb){var _0x4da25e=typeof _0x4aca7d[_0xab5f('0x20')]===_0xab5f('0x21')&&typeof _0x4aca7d[_0xab5f('0x22')]===_0xab5f('0x21');var _0x4c344f=_0x2caadb[_0xab5f('0x23')];var _0x413412=_0x4da25e?0x0:_0x4aca7d[_0xab5f('0x20')];var _0x1bc5ae=_0x4da25e?_0x2caadb[_0xab5f('0x23')]:_0x4aca7d[_0xab5f('0x20')]+_0x4aca7d['limit'];var _0x3fca4c;if(_0x1bc5ae>=_0x4c344f){_0x1bc5ae=_0x4c344f;_0x3fca4c=0xc8;}else{_0x3fca4c=0xce;}_0x340190[_0xab5f('0x1d')](_0x3fca4c);return _0x340190[_0xab5f('0x24')]('Content-Range',_0x413412+'-'+_0x1bc5ae+'/'+_0x4c344f)[_0xab5f('0x1f')](_0x2caadb);}return null;};}function patchUpdates(_0x134a23){return function(_0x811e76){try{jsonpatch[_0xab5f('0x25')](_0x811e76,_0x134a23,!![]);}catch(_0x297dff){return BPromise[_0xab5f('0x26')](_0x297dff);}return _0x811e76['save']();};}function saveUpdates(_0x55daaf,_0x3c5847){return function(_0x3f3bae){if(_0x3f3bae){return _0x3f3bae[_0xab5f('0x27')](_0x55daaf)[_0xab5f('0x28')](function(_0x3fb403){return _0x3fb403;});}return null;};}function removeEntity(_0x49972a,_0x3db1a6){return function(_0x38d793){if(_0x38d793){return _0x38d793['destroy']()[_0xab5f('0x28')](function(){_0x49972a[_0xab5f('0x1d')](0xcc)[_0xab5f('0x1e')]();});}};}function handleEntityNotFound(_0x391fec,_0x4fee82){return function(_0x144281){if(!_0x144281){_0x391fec[_0xab5f('0x1c')](0x194);}return _0x144281;};}function handleError(_0x1e1fa9,_0x239229){_0x239229=_0x239229||0x1f4;return function(_0x5e43a5){logger[_0xab5f('0x29')](_0x5e43a5[_0xab5f('0x2a')]);if(_0x5e43a5[_0xab5f('0x2b')]){delete _0x5e43a5[_0xab5f('0x2b')];}_0x1e1fa9[_0xab5f('0x1d')](_0x239229)['send'](_0x5e43a5);};}function getInteractionUsers(_0x5c6229,_0x7f5308){return new BPromise(function(_0x464287,_0x290df8){try{if(_0x7f5308[_0x5c6229['id']]){_0x5c6229[_0xab5f('0x2c')]=_['map'](_0x7f5308[_0x5c6229['id']],function(_0x56a8e9){return{'id':_0x56a8e9['id'],'fullname':_0x56a8e9['fullname']};});}}catch(_0x5f2c8e){_0x290df8(_0x5f2c8e);}_0x464287(_0x5c6229);});}function getInteractionTags(_0x494c4b,_0xcfaa90){return new BPromise(function(_0x312140,_0x18ca34){try{if(_0x494c4b[_0xab5f('0x2d')]){_0x494c4b[_0xab5f('0x2e')]=[];_0x494c4b[_0xab5f('0x2d')][_0xab5f('0x2f')](',')['forEach'](function(_0x2acdfc){_0x494c4b[_0xab5f('0x2e')][_0xab5f('0x30')](_0xcfaa90[_0x2acdfc]);});}delete _0x494c4b[_0xab5f('0x2d')];}catch(_0x2ba2e6){_0x18ca34(_0x2ba2e6);}_0x312140(_0x494c4b);});}function updateWhatsappInteraction(_0x42af45,_0x4e86d2,_0x39c74d){return new BPromise(function(_0x529c2d,_0x3e02f6){return getInteractionUsers(_0x42af45,_0x39c74d)['then'](function(_0x55c8ab){return getInteractionTags(_0x55c8ab,_0x4e86d2);})['then'](function(_0x4e7fc1){return getStateInteraction(_0x4e7fc1);})[_0xab5f('0x28')](function(_0x56e0f3){_0x529c2d(_0x56e0f3);})[_0xab5f('0x31')](function(_0x137502){_0x3e02f6(_0x137502);});});}function getStateInteraction(_0x41023a){return new BPromise(function(_0x6fb3fc){_0x41023a[_0xab5f('0x32')]=0x0;_0x41023a[_0xab5f('0x33')]=0x0;_0x41023a[_0xab5f('0x34')]=0x0;_0x41023a[_0xab5f('0x35')]=0x0;_0x41023a[_0xab5f('0x36')]=0x0;if(!_0x41023a['closed']&&!_0x41023a[_0xab5f('0x37')]){_0x41023a['waiting']+=0x1;}if(!_0x41023a[_0xab5f('0x38')]&&_0x41023a[_0xab5f('0x37')]){_0x41023a[_0xab5f('0x33')]+=0x1;if(_0x41023a['waiting']){_0x41023a[_0xab5f('0x32')]-=0x1;}}if(_0x41023a[_0xab5f('0x38')]&&_0x41023a[_0xab5f('0x37')]){_0x41023a[_0xab5f('0x34')]+=0x1;if(_0x41023a[_0xab5f('0x33')]){_0x41023a[_0xab5f('0x33')]-=0x1;}}if(_0x41023a[_0xab5f('0x38')]&&!_0x41023a['UserId']){_0x41023a[_0xab5f('0x35')]+=0x1;if(_0x41023a[_0xab5f('0x32')]){_0x41023a[_0xab5f('0x32')]-=0x1;}}_0x41023a[_0xab5f('0x36')]+=0x1;_0x6fb3fc(_0x41023a);});}exports[_0xab5f('0x39')]=function(_0x141a34,_0x41184c){var _0x1d1f7d={},_0x4aaaa3={},_0x484fde={'count':0x0,'rows':[]};var _0x24878b=_[_0xab5f('0x3a')](db['WhatsappInteraction'][_0xab5f('0x3b')],function(_0x579946){return{'name':_0x579946[_0xab5f('0x3c')],'type':_0x579946[_0xab5f('0x3d')]['key']};});_0x4aaaa3[_0xab5f('0x3e')]=_['map'](_0x24878b,_0xab5f('0x2b'));_0x4aaaa3[_0xab5f('0x3f')]=_[_0xab5f('0x40')](_0x141a34[_0xab5f('0x3f')]);_0x4aaaa3[_0xab5f('0x41')]=_[_0xab5f('0x42')](_0x4aaaa3['model'],_0x4aaaa3[_0xab5f('0x3f')]);_0x1d1f7d[_0xab5f('0x43')]=_[_0xab5f('0x42')](_0x4aaaa3[_0xab5f('0x3e')],qs[_0xab5f('0x44')](_0x141a34[_0xab5f('0x3f')]['fields']));_0x1d1f7d[_0xab5f('0x43')]=_0x1d1f7d[_0xab5f('0x43')]['length']?_0x1d1f7d[_0xab5f('0x43')]:_0x4aaaa3['model'];if(!_0x141a34[_0xab5f('0x3f')][_0xab5f('0x45')]('nolimit')){_0x1d1f7d[_0xab5f('0x22')]=qs[_0xab5f('0x22')](_0x141a34[_0xab5f('0x3f')][_0xab5f('0x22')]);_0x1d1f7d[_0xab5f('0x20')]=qs[_0xab5f('0x20')](_0x141a34[_0xab5f('0x3f')][_0xab5f('0x20')]);}_0x1d1f7d['order']=qs[_0xab5f('0x46')](_0x141a34[_0xab5f('0x3f')][_0xab5f('0x46')]);_0x1d1f7d['where']=qs[_0xab5f('0x41')](_[_0xab5f('0x47')](_0x141a34[_0xab5f('0x3f')],_0x4aaaa3[_0xab5f('0x41')]),_0x24878b);var _0x3e699e=[];var _0x14e815=squel[_0xab5f('0x48')]()[_0xab5f('0x49')]('id')[_0xab5f('0x49')](_0xab5f('0x2b'))[_0xab5f('0x49')](_0xab5f('0x4a'))[_0xab5f('0x4b')](_0xab5f('0x4c'));var _0x5f1018=squel[_0xab5f('0x48')]()[_0xab5f('0x49')](_0xab5f('0x4d'))[_0xab5f('0x49')](_0xab5f('0x4e'))['field']('ui.WhatsappInteractionId')[_0xab5f('0x4b')](_0xab5f('0x4f'),'ui')['left_join'](_0xab5f('0x50'),'u',_0xab5f('0x51'));var _0x85fd96=squel[_0xab5f('0x48')]()['from']('whatsapp_interactions','i')[_0xab5f('0x52')](_0xab5f('0x53'),'c','c.id\x20=\x20i.ContactId\x20AND\x20c.deletedAt\x20IS\x20NULL')[_0xab5f('0x52')](_0xab5f('0x50'),'o','o.id\x20=\x20i.UserId')['left_join'](_0xab5f('0x54'),'a','a.id\x20=\x20i.WhatsappAccountId')[_0xab5f('0x52')](_0xab5f('0x55'),'me',_0xab5f('0x56'))[_0xab5f('0x52')](_0xab5f('0x57'),'it',_0xab5f('0x58'))[_0xab5f('0x52')](_0xab5f('0x4c'),'t',_0xab5f('0x59'));var _0x454932=squel['expr']();var _0x36db0a=[];var _0x493235=squel['expr']();var _0x43a51b;if(_0x141a34['query'][_0xab5f('0x5a')]){_0x43a51b=as[_0xab5f('0x5b')](_0x141a34[_0xab5f('0x3f')][_0xab5f('0x5a')]);var _0x1b9cbd=_0x43a51b[_0xab5f('0x5c')];for(var _0x269eb8=0x0;_0x269eb8<_0x43a51b[_0xab5f('0x5d')][_0xab5f('0x5e')];_0x269eb8++){var _0x51b5d5=_0x43a51b[_0xab5f('0x5d')][_0x269eb8];var _0x201896='i';var _0x1d8952=_[_0xab5f('0x5f')](_0x24878b,['name',_0x51b5d5[_0xab5f('0x49')]]);if(!_0x1d8952){switch(_0x51b5d5[_0xab5f('0x49')]){case _0xab5f('0x60'):if(_0x51b5d5[_0xab5f('0x61')]==0x1){_0x85fd96[_0xab5f('0x62')]('`'+_0x51b5d5['field']+_0xab5f('0x63'));}else{_0x85fd96[_0xab5f('0x62')]('`'+_0x51b5d5['field']+_0xab5f('0x64'));}break;case _0xab5f('0x65'):if(_0x51b5d5[_0xab5f('0x66')]===_0xab5f('0x67')){if(_0x51b5d5[_0xab5f('0x61')][_0xab5f('0x2f')]('\x20')[_0xab5f('0x5e')]>0x1){_0x454932[_0x1b9cbd]('MATCH\x20(c.firstName,\x20c.lastName,\x20c.phone,\x20c.mobile,\x20c.fax,\x20c.email)\x20AGAINST\x20(?\x20IN\x20BOOLEAN\x20MODE)',qs[_0xab5f('0x68')](_0x51b5d5[_0xab5f('0x61')]),null);}else{var _0x2d10e6='%'+_0x51b5d5[_0xab5f('0x61')]+'%';_0x454932[_0x1b9cbd](_0xab5f('0x69'),_0x2d10e6,_0x2d10e6,_0x2d10e6);}}else{_0x1d8952=_[_0xab5f('0x5f')](_0x24878b,['name','ContactId']);_0x51b5d5[_0xab5f('0x49')]=_0xab5f('0x6a');_0x15b9b1=as['buildExpression'](_0x201896,_0x1d8952[_0xab5f('0x3d')],_0x51b5d5);_0x454932[_0x1b9cbd](_0x15b9b1[_0xab5f('0x6b')],_0x15b9b1['value'][_0xab5f('0x6c')],_0x15b9b1[_0xab5f('0x61')][_0xab5f('0x1e')]);}break;case _0xab5f('0x6d'):var _0x5dde1b=_0x51b5d5['value'][_0xab5f('0x2f')](',')[_0xab5f('0x3a')](function(_0x1e4cd6){return Number(_0x1e4cd6);});_0x5dde1b['forEach'](function(_0x41fab8){_0x493235['or'](_0xab5f('0x6e'),'%,'+_0x41fab8+',%');});_0x36db0a=_['union'](_0x36db0a,_0x5dde1b);break;case _0xab5f('0x6f'):if(_0x51b5d5[_0xab5f('0x66')]===_0xab5f('0x67')){_0x454932[_0x1b9cbd](_0xab5f('0x70'),'%'+_0x51b5d5[_0xab5f('0x61')]+'%',null);}else{_0x1d8952=_[_0xab5f('0x5f')](_0x24878b,[_0xab5f('0x2b'),_0xab5f('0x37')]);_0x51b5d5[_0xab5f('0x49')]=_0xab5f('0x37');_0x15b9b1=as[_0xab5f('0x71')](_0x201896,_0x1d8952['type'],_0x51b5d5);_0x454932[_0x1b9cbd](_0x15b9b1[_0xab5f('0x6b')],_0x15b9b1[_0xab5f('0x61')][_0xab5f('0x6c')],_0x15b9b1[_0xab5f('0x61')]['end']);}break;case _0xab5f('0x72'):_0x15b9b1=as[_0xab5f('0x71')]('me',null,_0x51b5d5);_0x454932[_0x1b9cbd](_0x15b9b1['text'],_0x15b9b1[_0xab5f('0x61')][_0xab5f('0x6c')],_0x15b9b1['value']['end']);break;}}else{var _0x15b9b1=as[_0xab5f('0x71')](_0x201896,_0x1d8952[_0xab5f('0x3d')],_0x51b5d5);_0x454932[_0x1b9cbd](_0x15b9b1[_0xab5f('0x6b')],_0x15b9b1[_0xab5f('0x61')][_0xab5f('0x6c')],_0x15b9b1[_0xab5f('0x61')][_0xab5f('0x1e')]);}}}else{var _0x58442f=_(_0x141a34[_0xab5f('0x3f')])[_0xab5f('0x40')]()[_0xab5f('0x3a')](function(_0x610c3e){return _[_0xab5f('0x73')](_0x24878b,[_0xab5f('0x2b'),_0x610c3e])?_0x610c3e:undefined;})[_0xab5f('0x74')]()[_0xab5f('0x61')]();if(!_[_0xab5f('0x75')](_0x58442f)){_0x58442f[_0xab5f('0x76')](function(_0x5ca3b1){if(_0x5ca3b1==='WhatsappAccountId'){_0x85fd96['where'](_0xab5f('0x77'),[]['concat'](_0x141a34[_0xab5f('0x3f')][_0x5ca3b1]));}else if(_0x5ca3b1===_0xab5f('0x78')){var _0x5ec60c=JSON['parse'](_0x141a34[_0xab5f('0x3f')][_0x5ca3b1])[_0xab5f('0x79')];var _0x14e83f=JSON['parse'](_0x141a34[_0xab5f('0x3f')][_0x5ca3b1])[_0xab5f('0x7a')];_0x85fd96[_0xab5f('0x7b')](_0xab5f('0x7c'),_0x5ec60c,_0x14e83f);}else{if(_0x141a34[_0xab5f('0x3f')][_0x5ca3b1]===_0xab5f('0x7d')){_0x85fd96[_0xab5f('0x7b')]('i.'+_0x5ca3b1+_0xab5f('0x7e'));}else{_0x85fd96[_0xab5f('0x7b')]('i.'+_0x5ca3b1+_0xab5f('0x7f'),_0x141a34['query'][_0x5ca3b1]);}}});}if(_0x141a34['query'][_0xab5f('0x80')]){if(_0x141a34['query'][_0xab5f('0x80')]==0x1){_0x85fd96['having'](_0xab5f('0x81'));}else{_0x85fd96[_0xab5f('0x62')]('`unreadMessages`\x20>\x200');}}if(_0x141a34['query'][_0xab5f('0x82')]){_0x36db0a=_[_0xab5f('0x83')](_0x141a34[_0xab5f('0x3f')][_0xab5f('0x82')])?_0x141a34[_0xab5f('0x3f')][_0xab5f('0x82')]:new Array(_0x141a34[_0xab5f('0x3f')]['tag']);_0x36db0a['forEach'](function(_0x2c29e7){_0x493235['or']('CONCAT(\x27,\x27,\x20`TagIds`,\x20\x27,\x27)\x20LIKE\x20?','%,'+_0x2c29e7+',%');});}if(_0x141a34[_0xab5f('0x3f')][_0xab5f('0x84')]){var _0x5d4ed8=_0x141a34[_0xab5f('0x3f')][_0xab5f('0x84')][_0xab5f('0x85')]('\x5c','\x5c\x5c')['replace'](/'/g,'\x27\x27');if(qs['isNumeric'](_0x5d4ed8)){_0x454932['or'](_0xab5f('0x86'),_0x5d4ed8+'%')['or'](_0xab5f('0x87'),_0x5d4ed8+'%');}else{_0x454932['or']('MATCH\x20(me.body)\x20AGAINST\x20(?\x20IN\x20BOOLEAN\x20MODE)',qs['getFullTextValue'](_0x5d4ed8))['or'](_0xab5f('0x88'),_0x5d4ed8+'%');}}}_0x85fd96[_0xab5f('0x7b')](_0x454932);_0x85fd96[_0xab5f('0x89')](_0xab5f('0x8a'));var _0x12430f={'type':db[_0xab5f('0x8b')][_0xab5f('0x8c')]['SELECT'],'raw':!![]};var _0x23acfb=_0x85fd96['clone']();_0x23acfb[_0xab5f('0x49')](_0xab5f('0x8a'));_0x23acfb[_0xab5f('0x49')](_0xab5f('0x8d'),_0xab5f('0x60'));if(_0x1d1f7d[_0xab5f('0x8e')]){_0x1d1f7d['order'][_0xab5f('0x76')](function(_0x12f94e){var _0x12756c=_0x12f94e[0x0]===_0xab5f('0x8f')?_0x12f94e[0x0]:'i.'+_0x12f94e[0x0];_0x85fd96[_0xab5f('0x8e')](_0x12756c,_0x12f94e[0x1]===_0xab5f('0x90')?![]:!![]);});}if(!_['isEmpty'](_0x36db0a)){_0x85fd96[_0xab5f('0x62')](_0x493235);_0x23acfb[_0xab5f('0x7b')](_0xab5f('0x91'),_0x36db0a);}BPromise[_0xab5f('0x92')]()['then'](function(){if(!_0x43a51b){if(_0x141a34['query'][_0xab5f('0x93')])return;_0x23acfb[_0xab5f('0x7b')](_0xab5f('0x94'));_0x85fd96[_0xab5f('0x7b')](_0xab5f('0x94'));return;}if(_0x141a34['user'][_0xab5f('0x95')]!=='agent')return;return _0x141a34[_0xab5f('0x96')]['getWhatsappAccounts']({'attributes':['id'],'raw':!![]})[_0xab5f('0x28')](function(_0x502538){if(_[_0xab5f('0x75')](_0x502538)){_0x23acfb[_0xab5f('0x7b')](_0xab5f('0x97'));_0x85fd96[_0xab5f('0x7b')](_0xab5f('0x97'));}else{_0x23acfb[_0xab5f('0x7b')](_0xab5f('0x77'),_[_0xab5f('0x3a')](_0x502538,'id'));_0x85fd96[_0xab5f('0x7b')](_0xab5f('0x77'),_[_0xab5f('0x3a')](_0x502538,'id'));}});})[_0xab5f('0x28')](function(){if(_0x141a34[_0xab5f('0x96')][_0xab5f('0x95')]===_0xab5f('0x98')&&!_0x43a51b&&!_0x141a34['query'][_0xab5f('0x93')])return[];return db['sequelize'][_0xab5f('0x3f')](_0x23acfb['toString'](),_0x12430f);})[_0xab5f('0x28')](function(_0x81d183){_0x484fde[_0xab5f('0x23')]=_0x81d183[_0xab5f('0x5e')];if(_0x484fde['count']===0x0)return[];return db[_0xab5f('0x99')]['query'](_0x14e815['toString'](),_0x12430f)['then'](function(_0x5d349e){_0x3e699e=_[_0xab5f('0x9a')](_0x5d349e,'id');_0x12430f=_[_0xab5f('0x9b')](_0x12430f,{'model':db[_0xab5f('0x9c')],'mapToModel':!![],'raw':!![],'nest':!![]});if(_0x141a34[_0xab5f('0x3f')][_0xab5f('0x44')]){_0x1d1f7d[_0xab5f('0x43')]['forEach'](function(_0x32c470){_0x85fd96[_0xab5f('0x49')]('i.'+_0x32c470);});}else{_0x85fd96[_0xab5f('0x49')]('SUM(me.read\x20=\x200\x20AND\x20me.direction\x20=\x20\x27in\x27)',_0xab5f('0x60'));_0x85fd96[_0xab5f('0x49')](_0xab5f('0x9d'),_0xab5f('0x9e'));_0x85fd96[_0xab5f('0x49')]('a.mandatoryDisposition',_0xab5f('0x9f'));_0x85fd96[_0xab5f('0x49')]('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',_0xab5f('0x8f'));_0x85fd96[_0xab5f('0x49')]('i.*');_0x85fd96[_0xab5f('0x49')](_0xab5f('0xa0'),_0xab5f('0xa1'));_0x85fd96['field']('c.lastName',_0xab5f('0xa2'));_0x85fd96[_0xab5f('0x49')](_0xab5f('0xa3'),_0xab5f('0xa4'));_0x85fd96['field'](_0xab5f('0xa5'),'Contact.phone');_0x85fd96[_0xab5f('0x49')](_0xab5f('0xa6'),_0xab5f('0xa7'));_0x85fd96[_0xab5f('0x49')](_0xab5f('0xa8'),_0xab5f('0xa9'));_0x85fd96[_0xab5f('0x49')](_0xab5f('0xaa'),_0xab5f('0xab'));_0x85fd96[_0xab5f('0x49')](_0xab5f('0xac'),_0xab5f('0xad'));_0x85fd96[_0xab5f('0x49')](_0xab5f('0xae'),_0xab5f('0x2d'));}if(_0x1d1f7d[_0xab5f('0x22')])_0x85fd96[_0xab5f('0x22')](_0x1d1f7d['limit']);if(_0x1d1f7d[_0xab5f('0x20')])_0x85fd96['offset'](_0x1d1f7d[_0xab5f('0x20')]);return db[_0xab5f('0x99')]['query'](_0x85fd96[_0xab5f('0xaf')](),_0x12430f);})[_0xab5f('0x28')](function(_0x22b3d9){if(_0x22b3d9[_0xab5f('0x5e')]>0x0)_0x5f1018[_0xab5f('0x7b')](_0xab5f('0xb0'),_[_0xab5f('0x3a')](_0x22b3d9,'id'));return db[_0xab5f('0x99')][_0xab5f('0x3f')](_0x5f1018[_0xab5f('0xaf')](),_0x12430f)[_0xab5f('0x28')](function(_0x2fbc0f){var _0x323cd8=_[_0xab5f('0xb1')](_0x2fbc0f,_0xab5f('0xb2'));var _0x3f54f7=[];_0x22b3d9[_0xab5f('0x76')](function(_0x43e7f8){_0x3f54f7[_0xab5f('0x30')](updateWhatsappInteraction(_0x43e7f8,_0x3e699e,_0x323cd8));});return BPromise[_0xab5f('0xb3')](_0x3f54f7);});});})['then'](function(_0x5b2035){_0x484fde[_0xab5f('0xb4')]=_0x5b2035;return _0x484fde;})[_0xab5f('0x28')](respondWithFilteredResult(_0x41184c,_0x1d1f7d))[_0xab5f('0x31')](handleError(_0x41184c,null));};exports['show']=function(_0x24cd3c,_0x3dab9a){var _0xe05cfd={'raw':![],'where':{'id':_0x24cd3c[_0xab5f('0xb5')]['id']}},_0x12e073={};_0x12e073['model']=_['keys'](db[_0xab5f('0x9c')][_0xab5f('0x3b')]);_0x12e073['query']=_['keys'](_0x24cd3c[_0xab5f('0x3f')]);_0x12e073[_0xab5f('0x41')]=_[_0xab5f('0x42')](_0x12e073[_0xab5f('0x3e')],_0x12e073['query']);_0xe05cfd[_0xab5f('0x43')]=_['intersection'](_0x12e073[_0xab5f('0x3e')],qs[_0xab5f('0x44')](_0x24cd3c[_0xab5f('0x3f')][_0xab5f('0x44')]));_0xe05cfd[_0xab5f('0x43')]=_0xe05cfd[_0xab5f('0x43')][_0xab5f('0x5e')]?_0xe05cfd['attributes']:_0x12e073[_0xab5f('0x3e')];if(_0x24cd3c[_0xab5f('0x3f')][_0xab5f('0xb6')]){_0xe05cfd['include']=[{'all':!![]}];}_0xe05cfd=_[_0xab5f('0x9b')]({},_0xe05cfd,_0x24cd3c[_0xab5f('0xb7')]);return db[_0xab5f('0x9c')][_0xab5f('0x5f')](_0xe05cfd)[_0xab5f('0x28')](handleEntityNotFound(_0x3dab9a,null))[_0xab5f('0x28')](respondWithResult(_0x3dab9a,null))[_0xab5f('0x31')](handleError(_0x3dab9a,null));};exports['create']=function(_0x3e57c6,_0x18ff4e){return db[_0xab5f('0x9c')][_0xab5f('0xb8')](_0x3e57c6[_0xab5f('0x72')],{})[_0xab5f('0x28')](respondWithResult(_0x18ff4e,0xc9))['catch'](handleError(_0x18ff4e,null));};exports['update']=function(_0x17fd76,_0x1b419c){if(_0x17fd76[_0xab5f('0x72')]['id']){delete _0x17fd76[_0xab5f('0x72')]['id'];}return db['WhatsappInteraction'][_0xab5f('0x5f')]({'where':{'id':_0x17fd76['params']['id']}})[_0xab5f('0x28')](handleEntityNotFound(_0x1b419c,null))[_0xab5f('0x28')](saveUpdates(_0x17fd76[_0xab5f('0x72')],null))[_0xab5f('0x28')](respondWithResult(_0x1b419c,null))[_0xab5f('0x31')](handleError(_0x1b419c,null));};exports['destroy']=function(_0x52a486,_0x595b26){return db['WhatsappInteraction'][_0xab5f('0x5f')]({'where':{'id':_0x52a486['params']['id']}})[_0xab5f('0x28')](handleEntityNotFound(_0x595b26,null))[_0xab5f('0x28')](removeEntity(_0x595b26,null))[_0xab5f('0x31')](handleError(_0x595b26,null));};exports[_0xab5f('0xb9')]=function(_0x217a50,_0xfa5759){return db['WhatsappInteraction']['describe']()[_0xab5f('0x28')](respondWithResult(_0xfa5759,null))[_0xab5f('0x31')](handleError(_0xfa5759,null));};exports['addMessage']=function(_0x303f60,_0x46e581,_0x176553){return db[_0xab5f('0xba')][_0xab5f('0x5f')]({'where':{'id':_0x303f60[_0xab5f('0xb5')]['id']}})[_0xab5f('0x28')](handleEntityNotFound(_0x46e581,null))[_0xab5f('0x28')](function(_0x1cce17){if(_0x1cce17){return _0x1cce17[_0xab5f('0xbb')](_0x303f60[_0xab5f('0x72')][_0xab5f('0xbc')],_[_0xab5f('0xbd')](_0x303f60[_0xab5f('0x72')],[_0xab5f('0xbc'),'id'])||{});}})[_0xab5f('0x28')](respondWithResult(_0x46e581,null))['catch'](handleError(_0x46e581,null));};exports[_0xab5f('0xbe')]=function(_0x1a5a12,_0x2a7196,_0x34d741){var _0x5ce2a2={'raw':![],'where':{}};var _0xe88324={};var _0x2d0cee={'count':0x0,'rows':[]};return db[_0xab5f('0x9c')]['findOne']({'where':{'id':_0x1a5a12[_0xab5f('0xb5')]['id']}})['then'](handleEntityNotFound(_0x2a7196,null))[_0xab5f('0x28')](function(_0x23a906){if(_0x23a906){_0xe88324['model']=_[_0xab5f('0x40')](db['WhatsappMessage'][_0xab5f('0x3b')]);_0xe88324[_0xab5f('0x3f')]=_[_0xab5f('0x40')](_0x1a5a12[_0xab5f('0x3f')]);_0xe88324[_0xab5f('0x41')]=_[_0xab5f('0x42')](_0xe88324[_0xab5f('0x3e')],_0xe88324[_0xab5f('0x3f')]);_0x5ce2a2[_0xab5f('0x43')]=_[_0xab5f('0x42')](_0xe88324[_0xab5f('0x3e')],qs['fields'](_0x1a5a12['query'][_0xab5f('0x44')]));_0x5ce2a2['attributes']=_0x5ce2a2[_0xab5f('0x43')][_0xab5f('0x5e')]?_0x5ce2a2['attributes']:_0xe88324[_0xab5f('0x3e')];if(!_0x1a5a12[_0xab5f('0x3f')]['hasOwnProperty'](_0xab5f('0xbf'))){_0x5ce2a2['limit']=qs[_0xab5f('0x22')](_0x1a5a12[_0xab5f('0x3f')]['limit']);_0x5ce2a2[_0xab5f('0x20')]=qs[_0xab5f('0x20')](_0x1a5a12[_0xab5f('0x3f')]['offset']);}_0x5ce2a2[_0xab5f('0x8e')]=qs[_0xab5f('0x46')](_0x1a5a12['query'][_0xab5f('0x46')]);_0x5ce2a2[_0xab5f('0x7b')]=qs[_0xab5f('0x41')](_[_0xab5f('0x47')](_0x1a5a12[_0xab5f('0x3f')],_0xe88324[_0xab5f('0x41')]));_0x5ce2a2[_0xab5f('0x7b')][_0xab5f('0xb2')]=_0x23a906['id'];if(_0x1a5a12['query'][_0xab5f('0x84')]){_0x5ce2a2[_0xab5f('0x7b')]=_[_0xab5f('0x9b')](_0x5ce2a2[_0xab5f('0x7b')],{'$or':_[_0xab5f('0x3a')](_0x5ce2a2[_0xab5f('0x43')],function(_0xae2ddb){var _0x2924f9={};_0x2924f9[_0xae2ddb]={'$like':'%'+_0x1a5a12[_0xab5f('0x3f')]['filter']+'%'};return _0x2924f9;})});}if(_0x1a5a12['query']['$gte']){var _0x3efd43=_0x1a5a12['query'][_0xab5f('0x79')][_0xab5f('0x2f')](',');var _0x3ed2a8={};_0x3ed2a8[_0x3efd43[0x0]]={'$gte':moment(_0x3efd43[0x1])[_0xab5f('0xc0')]('YYYY-MM-DD\x20HH:mm:ss')};_0x5ce2a2[_0xab5f('0x7b')]=_['merge'](_0x5ce2a2[_0xab5f('0x7b')],_0x3ed2a8);}_0x5ce2a2=_[_0xab5f('0x9b')]({},_0x5ce2a2,_0x1a5a12['options']);return db[_0xab5f('0xba')]['count']({'where':_0x5ce2a2[_0xab5f('0x7b')]})[_0xab5f('0x28')](function(_0x505047){_0x2d0cee[_0xab5f('0x23')]=_0x505047;if(_0x1a5a12['query']['includeAll']){_0x5ce2a2['include']=[{'all':!![]}];}return db[_0xab5f('0xba')][_0xab5f('0xc1')](_0x5ce2a2);})[_0xab5f('0x28')](function(_0x1f9abf){_0x2d0cee[_0xab5f('0xb4')]=_0x1f9abf;return _0x2d0cee;});}})[_0xab5f('0x28')](respondWithFilteredResult(_0x2a7196,_0x5ce2a2))[_0xab5f('0x31')](handleError(_0x2a7196,null));};exports['addTags']=function(_0x10c60d,_0x4be3c4,_0x4da443){return db[_0xab5f('0x9c')]['find']({'where':{'id':_0x10c60d[_0xab5f('0xb5')]['id']}})['then'](handleEntityNotFound(_0x4be3c4,null))[_0xab5f('0x28')](function(_0x2eb968){if(_0x2eb968){return _0x2eb968[_0xab5f('0xc2')](_0x10c60d[_0xab5f('0x72')][_0xab5f('0xbc')],_[_0xab5f('0xbd')](_0x10c60d[_0xab5f('0x72')],[_0xab5f('0xbc'),'id'])||{})['spread'](function(){return db[_0xab5f('0x6d')][_0xab5f('0xc1')]({'attributes':['id','name','color'],'where':{'id':_0x10c60d[_0xab5f('0x72')][_0xab5f('0xbc')]}});})[_0xab5f('0x28')](function(_0x11ab73){socket[_0xab5f('0xc3')](_0xab5f('0xc4'),{'id':Number(_0x10c60d[_0xab5f('0xb5')]['id']),'tags':_0x11ab73||[]});return{'id':Number(_0x10c60d[_0xab5f('0xb5')]['id']),'tags':_0x11ab73||[]};});}})[_0xab5f('0x28')](respondWithResult(_0x4be3c4,null))[_0xab5f('0x31')](handleError(_0x4be3c4,null));};exports[_0xab5f('0xc5')]=function(_0x5dc376,_0x332556,_0x1f4e9b){return db[_0xab5f('0x9c')][_0xab5f('0x5f')]({'where':{'id':_0x5dc376[_0xab5f('0xb5')]['id']}})['then'](handleEntityNotFound(_0x332556,null))[_0xab5f('0x28')](function(_0x3b1a93){if(_0x3b1a93){return _0x3b1a93[_0xab5f('0xc5')](_0x5dc376[_0xab5f('0x3f')][_0xab5f('0xbc')]);}})[_0xab5f('0x28')](respondWithStatusCode(_0x332556,null))[_0xab5f('0x31')](handleError(_0x332556,null));};exports[_0xab5f('0xc6')]=function(_0x562b48,_0xb29308){var _0x544cfb=moment()['unix']()[_0xab5f('0xaf')]();var _0x46e175=path['join'](config[_0xab5f('0xc7')],_0xab5f('0xc8'),_0xab5f('0xc9'),_0xab5f('0xca'));var _0x1d1c17=path[_0xab5f('0xcb')](config[_0xab5f('0xc7')],'server',_0xab5f('0xc9'),_0xab5f('0xcc'));var _0x172b67=path[_0xab5f('0xcb')](_0x46e175,_0x544cfb);var _0x56ddfa=util[_0xab5f('0xc0')](_0xab5f('0xcd'),_0x562b48[_0xab5f('0xb5')]['id'],_0x544cfb);var _0x5898d0=path[_0xab5f('0xcb')](_0x46e175,_0x56ddfa);var _0x4a09b4=[];_0x4a09b4['push']({'model':db['User'],'as':_0xab5f('0x6f'),'attributes':['id',_0xab5f('0xce')],'raw':!![]});_0x4a09b4[_0xab5f('0x30')]({'model':db[_0xab5f('0xcf')],'as':'Contact','attributes':['id',_0xab5f('0xd0'),'lastName'],'raw':!![]});if(_0x562b48[_0xab5f('0x3f')][_0xab5f('0xcc')]){_0x4a09b4[_0xab5f('0x30')]({'model':db[_0xab5f('0xd1')],'as':_0xab5f('0xd1'),'raw':!![]});}var _0x20ffaa=[{'model':db[_0xab5f('0xba')],'as':_0xab5f('0xd2'),'attributes':['id',_0xab5f('0x72'),_0xab5f('0x78'),_0xab5f('0xd3'),_0xab5f('0xd4'),_0xab5f('0x80')],'include':_0x4a09b4}];_0x20ffaa[_0xab5f('0x30')]({'model':db[_0xab5f('0xd5')],'as':_0xab5f('0xd6'),'attributes':['id',_0xab5f('0x2b')]});_0x20ffaa[_0xab5f('0x30')]({'model':db[_0xab5f('0x6f')],'as':_0xab5f('0xd7'),'attributes':['id',_0xab5f('0xce')]});return db[_0xab5f('0x9c')]['find']({'where':{'id':_0x562b48[_0xab5f('0xb5')]['id']},'include':_0x20ffaa})[_0xab5f('0x28')](handleEntityNotFound(_0xb29308,null))['then'](function(_0x115ef9){if(_0x115ef9){var _0x8ff3ac=_0x115ef9[_0xab5f('0xd8')]({'plain':!![]});fs[_0xab5f('0xd9')](_0x172b67);var _0x490af7={'channel':_0xab5f('0xda'),'account':_0x8ff3ac[_0xab5f('0xd6')][_0xab5f('0x2b')],'agent':_0x8ff3ac['Owner']?_0x8ff3ac[_0xab5f('0xd7')][_0xab5f('0xce')]:_0xab5f('0xdb'),'createdAt':moment(_0x8ff3ac[_0xab5f('0x78')])[_0xab5f('0xc0')](_0xab5f('0xdc'))[_0xab5f('0xaf')](),'closedAt':_0x8ff3ac[_0xab5f('0x38')]?moment(_0x8ff3ac[_0xab5f('0xdd')])[_0xab5f('0xc0')]('YYYY-MM-DD\x20HH:mm:ss')[_0xab5f('0xaf')]():'','disposition':_0x8ff3ac[_0xab5f('0xde')],'messages':_(_0x8ff3ac[_0xab5f('0xd2')])[_0xab5f('0x84')]({'secret':![]})['map'](function(_0xa331c){if(_0xa331c[_0xab5f('0xd1')]){var _0x3fb6fd=path[_0xab5f('0xcb')](_0x1d1c17,_0xa331c['Attachment']['basename']);if(fs[_0xab5f('0xdf')](_0x3fb6fd)){fs[_0xab5f('0xe0')](_0x3fb6fd)[_0xab5f('0xe1')](fs[_0xab5f('0xe2')](path[_0xab5f('0xcb')](_0x172b67,_0xa331c['Attachment'][_0xab5f('0xe3')])));}}return{'date':moment(_0xa331c[_0xab5f('0x78')])[_0xab5f('0xc0')](_0xab5f('0xdc'))[_0xab5f('0xaf')](),'sender':_0xa331c[_0xab5f('0xd3')]===_0xab5f('0xe4')?_0xa331c[_0xab5f('0x6f')]?_0xa331c[_0xab5f('0x6f')]['fullname']:_0xab5f('0xe5'):_0xa331c[_0xab5f('0x65')]['firstName']+(_0xa331c[_0xab5f('0x65')][_0xab5f('0xe6')]?'\x20'+_0xa331c[_0xab5f('0x65')][_0xab5f('0xe6')]:''),'body':_0xa331c[_0xab5f('0xd1')]?_0xa331c['Attachment'][_0xab5f('0xe3')]:_0xa331c[_0xab5f('0x72')],'direction':_0xa331c['direction']===_0xab5f('0xe4')?'A':'C','secret':_0xa331c[_0xab5f('0xd4')],'read':_0xa331c['read'],'attachment':_0xa331c[_0xab5f('0xd1')]};})['value']()};return ejs[_0xab5f('0xe7')](path[_0xab5f('0xcb')](config[_0xab5f('0xc7')],_0xab5f('0xc8'),_0xab5f('0xe8'),_0xab5f('0xe9')),{'interaction':_0x490af7})[_0xab5f('0x28')](function(_0xe3a487){var _0x2f61d8=path[_0xab5f('0xcb')](_0x172b67,_0xab5f('0xea')+_0x8ff3ac['id']+'-'+_0x544cfb+'.pdf');var _0x408566={'path':_0x2f61d8,'channel':_0xab5f('0xda'),'interactionId':_0x8ff3ac['id']};return pdf[_0xab5f('0xeb')](_0xe3a487,_0x408566);})[_0xab5f('0x28')](function(){return new BPromise(function(_0x3eb0ae,_0x195bcf){zipdir(_0x172b67,{'saveTo':_0x5898d0},function(_0x186440,_0x55e0df){if(_0x186440)return _0x195bcf(_0x186440);return _0x3eb0ae(_0x55e0df);});})[_0xab5f('0x28')](function(){return new BPromise(function(_0x32058f,_0x3d1ef7){rimraf(_0x172b67,function(_0x257dcb){if(_0x257dcb)_0x3d1ef7(_0x257dcb);return _0x32058f();});});})[_0xab5f('0x28')](function(){return _0xb29308[_0xab5f('0xc6')](_0x5898d0,_0x56ddfa,function(_0x580990){if(_0x580990){console[_0xab5f('0xec')](_0xab5f('0xed'),_0x580990);}else{fs[_0xab5f('0xee')](_0x5898d0);}});});});}else{return _0xb29308[_0xab5f('0x1c')](0xc8);}})[_0xab5f('0x31')](handleError(_0xb29308,null));};