a4a0b0d6142e379c57698c02eb208e2543d76031
[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 _0x2ff5=['path','to-csv','ejs','fs-extra','squel','crypto','jsforce','desk.js','querystring','papaparse','ioredis','../../components/auth/service','../../components/parsers/qs','../../components/parsers/advancedSearch','../../components/export/pdf','../../config/license/hardware','../../config/logger','api','redis','localhost','./whatsappInteraction.socket','register','status','end','undefined','limit','count','offset','set','Content-Range','json','reject','save','update','then','destroy','sendStatus','error','name','send','Users','fullname','Tags','TagIds','forEach','push','catch','talking','answered','unmanaged','total','closed','UserId','waiting','index','map','WhatsappInteraction','fieldName','key','model','query','keys','intersection','attributes','fields','hasOwnProperty','nolimit','order','sort','where','filters','field','color','from','tools_tags','u.id','u.fullname','ui.WhatsappInteractionId','user_has_whatsapp_interactions','left_join','users','select','whatsapp_interactions','c.id\x20=\x20i.ContactId\x20AND\x20c.deletedAt\x20IS\x20NULL','o.id\x20=\x20i.UserId','whatsapp_accounts','a.id\x20=\x20i.WhatsappAccountId','whatsapp_messages','me.WhatsappInteractionId\x20=\x20i.id','it.WhatsappInteractionId\x20=\x20i.id','t.id\x20=\x20it.TagId','expr','parseSearch','length','conditions','find','unreadMessages','value','having','`\x20>\x200','Contact','operator','$substring','split','MATCH\x20(c.firstName,\x20c.lastName,\x20c.phone,\x20c.mobile,\x20c.fax,\x20c.email)\x20AGAINST\x20(?\x20IN\x20BOOLEAN\x20MODE)','getFullTextValue','c.firstName\x20LIKE\x20?\x20OR\x20c.lastName\x20LIKE\x20?\x20OR\x20c.email\x20LIKE\x20?','ContactId','buildExpression','type','text','start','Tag','CONCAT(\x27,\x27,\x20`TagIds`,\x20\x27,\x27)\x20LIKE\x20?','union','User','CONCAT_WS(\x27\x20\x27,\x20o.name,\x20o.fullname)\x20LIKE\x20?','body','compact','isEmpty','WhatsappAccountId','i.WhatsappAccountId\x20IN\x20?','concat','createdAt','parse','$gte','null','\x20=\x20?','read','`unreadMessages`\x20=\x200','tag','isArray','filter','replace','isNumeric','i.phone\x20LIKE\x20?','CONCAT(c.firstName,\x20\x27\x20\x27,\x20c.lastName)\x20LIKE\x20?','group','i.id','Sequelize','SELECT','clone','SUM(me.read\x20=\x200\x20AND\x20me.direction\x20=\x20\x27in\x27)','DESC','t.id\x20IN\x20?','resolve','i.WhatsappAccountId\x20IS\x20NOT\x20NULL','role','agent','getWhatsappAccounts','i.WhatsappAccountId\x20IS\x20NULL','sequelize','toString','merge','a.key','Account.key','Account.mandatoryDisposition','CASE\x20WHEN\x20(c.firstName\x20IS\x20NOT\x20NULL\x20AND\x20c.firstName\x20<>\x20\x27\x27)\x20OR\x20(c.lastName\x20IS\x20NOT\x20NULL\x20AND\x20c.lastName\x20<>\x20\x27\x27)\x20THEN\x20CONCAT(IFNULL(c.firstName,\x20\x27\x27),\x20\x27\x20\x27,\x20IFNULL(c.lastName,\x20\x27\x27))\x20ELSE\x20\x27Unknown\x27\x20END','contactName','i.*','c.firstName','Contact.firstName','c.lastName','c.email','Contact.email','Contact.phone','c.mobile','Contact.mobile','Contact.fax','o.fullname','Owner.fullname','o.internal','Owner.internal','GROUP_CONCAT(DISTINCT\x20t.id)','ui.WhatsappInteractionId\x20IN\x20?','WhatsappInteractionId','all','rows','params','rawAttributes','include','options','create','describe','addMessage','omit','ids','getMessages','findOne','pick','format','YYYY-MM-DD\x20HH:mm:ss','includeAll','WhatsappMessage','addTags','setTags','spread','findAll','whatsappInteractionTags:save','removeTags','download','unix','join','server','files','tmp','CmContact','firstName','lastName','Attachment','Messages','secret','Account','Owner','get','Whatsapp','disposition','basename','createReadStream','createWriteStream','direction','out','System','renderFile','root','downloadInteraction.ejs','transcript-','.pdf','createPdfFromHTML','log','unlinkSync','rimraf','zip-dir','request-promise','moment','bluebird','mustache','util'];(function(_0x30c05b,_0x44cfcc){var _0x9b8cd3=function(_0x224a0c){while(--_0x224a0c){_0x30c05b['push'](_0x30c05b['shift']());}};_0x9b8cd3(++_0x44cfcc);}(_0x2ff5,0x1d5));var _0x52ff=function(_0x461380,_0x3c51d1){_0x461380=_0x461380-0x0;var _0x3b9036=_0x2ff5[_0x461380];return _0x3b9036;};'use strict';var emlformat=require('eml-format');var rimraf=require(_0x52ff('0x0'));var zipdir=require(_0x52ff('0x1'));var jsonpatch=require('fast-json-patch');var rp=require(_0x52ff('0x2'));var moment=require(_0x52ff('0x3'));var BPromise=require(_0x52ff('0x4'));var Mustache=require(_0x52ff('0x5'));var util=require(_0x52ff('0x6'));var path=require(_0x52ff('0x7'));var sox=require('sox');var csv=require(_0x52ff('0x8'));var ejs=require(_0x52ff('0x9'));var fs=require('fs');var fs_extra=require(_0x52ff('0xa'));var _=require('lodash');var squel=require(_0x52ff('0xb'));var crypto=require(_0x52ff('0xc'));var jsforce=require(_0x52ff('0xd'));var deskjs=require(_0x52ff('0xe'));var toCsv=require(_0x52ff('0x8'));var querystring=require(_0x52ff('0xf'));var Papa=require(_0x52ff('0x10'));var Redis=require(_0x52ff('0x11'));var authService=require(_0x52ff('0x12'));var qs=require(_0x52ff('0x13'));var as=require(_0x52ff('0x14'));var pdf=require(_0x52ff('0x15'));var hardwareService=require(_0x52ff('0x16'));var logger=require(_0x52ff('0x17'))(_0x52ff('0x18'));var utils=require('../../config/utils');var config=require('../../config/environment');var licenseUtil=require('../../config/license/util');var db=require('../../mysqldb')['db'];config[_0x52ff('0x19')]=_['defaults'](config[_0x52ff('0x19')],{'host':_0x52ff('0x1a'),'port':0x18eb});var socket=require('socket.io-emitter')(new Redis(config[_0x52ff('0x19')]));require(_0x52ff('0x1b'))[_0x52ff('0x1c')](socket);function respondWithStatusCode(_0x2023d3,_0x418202){_0x418202=_0x418202||0xcc;return function(_0x27c987){if(_0x27c987){return _0x2023d3['sendStatus'](_0x418202);}return _0x2023d3[_0x52ff('0x1d')](_0x418202)[_0x52ff('0x1e')]();};}function respondWithResult(_0x3154b7,_0x4c83f){_0x4c83f=_0x4c83f||0xc8;return function(_0xa7ce6c){if(_0xa7ce6c){return _0x3154b7[_0x52ff('0x1d')](_0x4c83f)['json'](_0xa7ce6c);}};}function respondWithFilteredResult(_0x426858,_0x154419){return function(_0x3cbe60){if(_0x3cbe60){var _0x36d544=typeof _0x154419['offset']===_0x52ff('0x1f')&&typeof _0x154419[_0x52ff('0x20')]===_0x52ff('0x1f');var _0x58efb4=_0x3cbe60[_0x52ff('0x21')];var _0x10ad04=_0x36d544?0x0:_0x154419[_0x52ff('0x22')];var _0x1d36c0=_0x36d544?_0x3cbe60[_0x52ff('0x21')]:_0x154419[_0x52ff('0x22')]+_0x154419[_0x52ff('0x20')];var _0x1f4bf3;if(_0x1d36c0>=_0x58efb4){_0x1d36c0=_0x58efb4;_0x1f4bf3=0xc8;}else{_0x1f4bf3=0xce;}_0x426858[_0x52ff('0x1d')](_0x1f4bf3);return _0x426858[_0x52ff('0x23')](_0x52ff('0x24'),_0x10ad04+'-'+_0x1d36c0+'/'+_0x58efb4)[_0x52ff('0x25')](_0x3cbe60);}return null;};}function patchUpdates(_0x1414c3){return function(_0x3e441b){try{jsonpatch['apply'](_0x3e441b,_0x1414c3,!![]);}catch(_0x376edf){return BPromise[_0x52ff('0x26')](_0x376edf);}return _0x3e441b[_0x52ff('0x27')]();};}function saveUpdates(_0x3accd4,_0x55f32d){return function(_0x4172ec){if(_0x4172ec){return _0x4172ec[_0x52ff('0x28')](_0x3accd4)[_0x52ff('0x29')](function(_0x5a6ebc){return _0x5a6ebc;});}return null;};}function removeEntity(_0x5b8905,_0x1ac770){return function(_0x3f5e22){if(_0x3f5e22){return _0x3f5e22[_0x52ff('0x2a')]()[_0x52ff('0x29')](function(){_0x5b8905[_0x52ff('0x1d')](0xcc)[_0x52ff('0x1e')]();});}};}function handleEntityNotFound(_0x25450b,_0x322260){return function(_0x1a706c){if(!_0x1a706c){_0x25450b[_0x52ff('0x2b')](0x194);}return _0x1a706c;};}function handleError(_0xd95637,_0x24094b){_0x24094b=_0x24094b||0x1f4;return function(_0x406343){logger[_0x52ff('0x2c')](_0x406343['stack']);if(_0x406343[_0x52ff('0x2d')]){delete _0x406343['name'];}_0xd95637[_0x52ff('0x1d')](_0x24094b)[_0x52ff('0x2e')](_0x406343);};}function getInteractionUsers(_0x530abb,_0x2d1cba){return new BPromise(function(_0x326d23,_0x264c7c){try{if(_0x2d1cba[_0x530abb['id']]){_0x530abb[_0x52ff('0x2f')]=_['map'](_0x2d1cba[_0x530abb['id']],function(_0x320c07){return{'id':_0x320c07['id'],'fullname':_0x320c07[_0x52ff('0x30')]};});}}catch(_0x526c35){_0x264c7c(_0x526c35);}_0x326d23(_0x530abb);});}function getInteractionTags(_0x603c97,_0x2f437d){return new BPromise(function(_0xef339d,_0x35f605){try{if(_0x603c97['TagIds']){_0x603c97[_0x52ff('0x31')]=[];_0x603c97[_0x52ff('0x32')]['split'](',')[_0x52ff('0x33')](function(_0x4757f6){_0x603c97[_0x52ff('0x31')][_0x52ff('0x34')](_0x2f437d[_0x4757f6]);});}delete _0x603c97['TagIds'];}catch(_0x14eded){_0x35f605(_0x14eded);}_0xef339d(_0x603c97);});}function updateWhatsappInteraction(_0x529415,_0x30f096,_0x23decc){return new BPromise(function(_0x56988b,_0x18fb46){return getInteractionUsers(_0x529415,_0x23decc)[_0x52ff('0x29')](function(_0x3c2617){return getInteractionTags(_0x3c2617,_0x30f096);})['then'](function(_0x4f3d97){return getStateInteraction(_0x4f3d97);})[_0x52ff('0x29')](function(_0x4b72cd){_0x56988b(_0x4b72cd);})[_0x52ff('0x35')](function(_0x4f5997){_0x18fb46(_0x4f5997);});});}function getStateInteraction(_0x14f909){return new BPromise(function(_0x2a5653){_0x14f909['waiting']=0x0;_0x14f909[_0x52ff('0x36')]=0x0;_0x14f909[_0x52ff('0x37')]=0x0;_0x14f909[_0x52ff('0x38')]=0x0;_0x14f909[_0x52ff('0x39')]=0x0;if(!_0x14f909[_0x52ff('0x3a')]&&!_0x14f909[_0x52ff('0x3b')]){_0x14f909[_0x52ff('0x3c')]+=0x1;}if(!_0x14f909['closed']&&_0x14f909[_0x52ff('0x3b')]){_0x14f909[_0x52ff('0x36')]+=0x1;if(_0x14f909[_0x52ff('0x3c')]){_0x14f909[_0x52ff('0x3c')]-=0x1;}}if(_0x14f909[_0x52ff('0x3a')]&&_0x14f909[_0x52ff('0x3b')]){_0x14f909[_0x52ff('0x37')]+=0x1;if(_0x14f909['talking']){_0x14f909[_0x52ff('0x36')]-=0x1;}}if(_0x14f909[_0x52ff('0x3a')]&&!_0x14f909[_0x52ff('0x3b')]){_0x14f909['unmanaged']+=0x1;if(_0x14f909['waiting']){_0x14f909[_0x52ff('0x3c')]-=0x1;}}_0x14f909['total']+=0x1;_0x2a5653(_0x14f909);});}exports[_0x52ff('0x3d')]=function(_0xbbfef8,_0x245d42){var _0x28c330={},_0x27d3f1={},_0x1eb769={'count':0x0,'rows':[]};var _0x4482d4=_[_0x52ff('0x3e')](db[_0x52ff('0x3f')]['rawAttributes'],function(_0x34b0dc){return{'name':_0x34b0dc[_0x52ff('0x40')],'type':_0x34b0dc['type'][_0x52ff('0x41')]};});_0x27d3f1[_0x52ff('0x42')]=_[_0x52ff('0x3e')](_0x4482d4,_0x52ff('0x2d'));_0x27d3f1[_0x52ff('0x43')]=_[_0x52ff('0x44')](_0xbbfef8[_0x52ff('0x43')]);_0x27d3f1['filters']=_[_0x52ff('0x45')](_0x27d3f1[_0x52ff('0x42')],_0x27d3f1[_0x52ff('0x43')]);_0x28c330[_0x52ff('0x46')]=_['intersection'](_0x27d3f1['model'],qs[_0x52ff('0x47')](_0xbbfef8[_0x52ff('0x43')][_0x52ff('0x47')]));_0x28c330[_0x52ff('0x46')]=_0x28c330[_0x52ff('0x46')]['length']?_0x28c330[_0x52ff('0x46')]:_0x27d3f1[_0x52ff('0x42')];if(!_0xbbfef8[_0x52ff('0x43')][_0x52ff('0x48')](_0x52ff('0x49'))){_0x28c330[_0x52ff('0x20')]=qs[_0x52ff('0x20')](_0xbbfef8[_0x52ff('0x43')][_0x52ff('0x20')]);_0x28c330[_0x52ff('0x22')]=qs['offset'](_0xbbfef8[_0x52ff('0x43')][_0x52ff('0x22')]);}_0x28c330[_0x52ff('0x4a')]=qs[_0x52ff('0x4b')](_0xbbfef8[_0x52ff('0x43')]['sort']);_0x28c330[_0x52ff('0x4c')]=qs[_0x52ff('0x4d')](_['pick'](_0xbbfef8[_0x52ff('0x43')],_0x27d3f1[_0x52ff('0x4d')]),_0x4482d4);var _0x6fa6a3=[];var _0x3e5e8d=squel['select']()['field']('id')[_0x52ff('0x4e')](_0x52ff('0x2d'))[_0x52ff('0x4e')](_0x52ff('0x4f'))[_0x52ff('0x50')](_0x52ff('0x51'));var _0x38283f=squel['select']()['field'](_0x52ff('0x52'))[_0x52ff('0x4e')](_0x52ff('0x53'))[_0x52ff('0x4e')](_0x52ff('0x54'))[_0x52ff('0x50')](_0x52ff('0x55'),'ui')[_0x52ff('0x56')](_0x52ff('0x57'),'u','u.id\x20=\x20ui.UserId');var _0x32d3d0=squel[_0x52ff('0x58')]()[_0x52ff('0x50')](_0x52ff('0x59'),'i')[_0x52ff('0x56')]('cm_contacts','c',_0x52ff('0x5a'))[_0x52ff('0x56')](_0x52ff('0x57'),'o',_0x52ff('0x5b'))['left_join'](_0x52ff('0x5c'),'a',_0x52ff('0x5d'))[_0x52ff('0x56')](_0x52ff('0x5e'),'me',_0x52ff('0x5f'))[_0x52ff('0x56')]('whatsapp_interaction_has_tags','it',_0x52ff('0x60'))['left_join'](_0x52ff('0x51'),'t',_0x52ff('0x61'));var _0x445cb2=squel[_0x52ff('0x62')]();var _0x4a1e3c=[];var _0x21c71a=squel['expr']();var _0x2207a0;if(_0xbbfef8['query']['search']){_0x2207a0=as[_0x52ff('0x63')](_0xbbfef8[_0x52ff('0x43')]['search']);var _0x474514=_0x2207a0['sqlOperator'];for(var _0x4141d5=0x0;_0x4141d5<_0x2207a0['conditions'][_0x52ff('0x64')];_0x4141d5++){var _0x319fc7=_0x2207a0[_0x52ff('0x65')][_0x4141d5];var _0xd3f42d='i';var _0x5aff4a=_[_0x52ff('0x66')](_0x4482d4,[_0x52ff('0x2d'),_0x319fc7[_0x52ff('0x4e')]]);if(!_0x5aff4a){switch(_0x319fc7[_0x52ff('0x4e')]){case _0x52ff('0x67'):if(_0x319fc7[_0x52ff('0x68')]==0x1){_0x32d3d0[_0x52ff('0x69')]('`'+_0x319fc7['field']+'`\x20=\x200');}else{_0x32d3d0['having']('`'+_0x319fc7['field']+_0x52ff('0x6a'));}break;case _0x52ff('0x6b'):if(_0x319fc7[_0x52ff('0x6c')]===_0x52ff('0x6d')){if(_0x319fc7[_0x52ff('0x68')][_0x52ff('0x6e')]('\x20')['length']>0x1){_0x445cb2[_0x474514](_0x52ff('0x6f'),qs[_0x52ff('0x70')](_0x319fc7[_0x52ff('0x68')]),null);}else{var _0x2c4ee6='%'+_0x319fc7[_0x52ff('0x68')]+'%';_0x445cb2[_0x474514](_0x52ff('0x71'),_0x2c4ee6,_0x2c4ee6,_0x2c4ee6);}}else{_0x5aff4a=_['find'](_0x4482d4,[_0x52ff('0x2d'),_0x52ff('0x72')]);_0x319fc7[_0x52ff('0x4e')]=_0x52ff('0x72');_0x565a82=as[_0x52ff('0x73')](_0xd3f42d,_0x5aff4a[_0x52ff('0x74')],_0x319fc7);_0x445cb2[_0x474514](_0x565a82[_0x52ff('0x75')],_0x565a82[_0x52ff('0x68')][_0x52ff('0x76')],_0x565a82[_0x52ff('0x68')][_0x52ff('0x1e')]);}break;case _0x52ff('0x77'):var _0x466c10=_0x319fc7[_0x52ff('0x68')]['split'](',')[_0x52ff('0x3e')](function(_0x33990d){return Number(_0x33990d);});_0x466c10[_0x52ff('0x33')](function(_0x31aa09){_0x21c71a['or'](_0x52ff('0x78'),'%,'+_0x31aa09+',%');});_0x4a1e3c=_[_0x52ff('0x79')](_0x4a1e3c,_0x466c10);break;case _0x52ff('0x7a'):if(_0x319fc7['operator']===_0x52ff('0x6d')){_0x445cb2[_0x474514](_0x52ff('0x7b'),'%'+_0x319fc7[_0x52ff('0x68')]+'%',null);}else{_0x5aff4a=_[_0x52ff('0x66')](_0x4482d4,['name',_0x52ff('0x3b')]);_0x319fc7[_0x52ff('0x4e')]=_0x52ff('0x3b');_0x565a82=as[_0x52ff('0x73')](_0xd3f42d,_0x5aff4a[_0x52ff('0x74')],_0x319fc7);_0x445cb2[_0x474514](_0x565a82[_0x52ff('0x75')],_0x565a82[_0x52ff('0x68')]['start'],_0x565a82[_0x52ff('0x68')][_0x52ff('0x1e')]);}break;case _0x52ff('0x7c'):_0x565a82=as[_0x52ff('0x73')]('me',null,_0x319fc7);_0x445cb2[_0x474514](_0x565a82[_0x52ff('0x75')],_0x565a82[_0x52ff('0x68')][_0x52ff('0x76')],_0x565a82[_0x52ff('0x68')][_0x52ff('0x1e')]);break;}}else{var _0x565a82=as['buildExpression'](_0xd3f42d,_0x5aff4a[_0x52ff('0x74')],_0x319fc7);_0x445cb2[_0x474514](_0x565a82[_0x52ff('0x75')],_0x565a82[_0x52ff('0x68')][_0x52ff('0x76')],_0x565a82[_0x52ff('0x68')][_0x52ff('0x1e')]);}}}else{var _0xdc235b=_(_0xbbfef8[_0x52ff('0x43')])[_0x52ff('0x44')]()[_0x52ff('0x3e')](function(_0x5b3fe0){return _['some'](_0x4482d4,[_0x52ff('0x2d'),_0x5b3fe0])?_0x5b3fe0:undefined;})[_0x52ff('0x7d')]()[_0x52ff('0x68')]();if(!_[_0x52ff('0x7e')](_0xdc235b)){_0xdc235b[_0x52ff('0x33')](function(_0x361025){if(_0x361025===_0x52ff('0x7f')){_0x32d3d0[_0x52ff('0x4c')](_0x52ff('0x80'),[][_0x52ff('0x81')](_0xbbfef8['query'][_0x361025]));}else if(_0x361025===_0x52ff('0x82')){var _0x18c357=JSON[_0x52ff('0x83')](_0xbbfef8[_0x52ff('0x43')][_0x361025])[_0x52ff('0x84')];var _0x1fa59c=JSON['parse'](_0xbbfef8[_0x52ff('0x43')][_0x361025])['$lte'];_0x32d3d0[_0x52ff('0x4c')]('i.createdAt\x20BETWEEN\x20?\x20AND\x20?',_0x18c357,_0x1fa59c);}else{if(_0xbbfef8['query'][_0x361025]===_0x52ff('0x85')){_0x32d3d0[_0x52ff('0x4c')]('i.'+_0x361025+'\x20IS\x20NULL');}else{_0x32d3d0[_0x52ff('0x4c')]('i.'+_0x361025+_0x52ff('0x86'),_0xbbfef8[_0x52ff('0x43')][_0x361025]);}}});}if(_0xbbfef8[_0x52ff('0x43')][_0x52ff('0x87')]){if(_0xbbfef8['query'][_0x52ff('0x87')]==0x1){_0x32d3d0[_0x52ff('0x69')](_0x52ff('0x88'));}else{_0x32d3d0[_0x52ff('0x69')]('`unreadMessages`\x20>\x200');}}if(_0xbbfef8[_0x52ff('0x43')][_0x52ff('0x89')]){_0x4a1e3c=_[_0x52ff('0x8a')](_0xbbfef8[_0x52ff('0x43')]['tag'])?_0xbbfef8['query'][_0x52ff('0x89')]:new Array(_0xbbfef8[_0x52ff('0x43')][_0x52ff('0x89')]);_0x4a1e3c[_0x52ff('0x33')](function(_0xb1839){_0x21c71a['or'](_0x52ff('0x78'),'%,'+_0xb1839+',%');});}if(_0xbbfef8['query'][_0x52ff('0x8b')]){var _0x8a4f23=_0xbbfef8[_0x52ff('0x43')][_0x52ff('0x8b')][_0x52ff('0x8c')]('\x5c','\x5c\x5c')[_0x52ff('0x8c')](/'/g,'\x27\x27');if(qs[_0x52ff('0x8d')](_0x8a4f23)){_0x445cb2['or']('i.id\x20LIKE\x20?',_0x8a4f23+'%')['or'](_0x52ff('0x8e'),_0x8a4f23+'%');}else{_0x445cb2['or']('MATCH\x20(me.body)\x20AGAINST\x20(?\x20IN\x20BOOLEAN\x20MODE)',qs[_0x52ff('0x70')](_0x8a4f23))['or'](_0x52ff('0x8f'),_0x8a4f23+'%');}}}_0x32d3d0[_0x52ff('0x4c')](_0x445cb2);_0x32d3d0[_0x52ff('0x90')](_0x52ff('0x91'));var _0x435c34={'type':db[_0x52ff('0x92')]['QueryTypes'][_0x52ff('0x93')],'raw':!![]};var _0x132537=_0x32d3d0[_0x52ff('0x94')]();_0x132537[_0x52ff('0x4e')]('i.id');_0x132537[_0x52ff('0x4e')](_0x52ff('0x95'),'unreadMessages');if(_0x28c330[_0x52ff('0x4a')]){_0x28c330[_0x52ff('0x4a')][_0x52ff('0x33')](function(_0x312165){var _0x41710e=_0x312165[0x0]==='contactName'?_0x312165[0x0]:'i.'+_0x312165[0x0];_0x32d3d0[_0x52ff('0x4a')](_0x41710e,_0x312165[0x1]===_0x52ff('0x96')?![]:!![]);});}if(!_[_0x52ff('0x7e')](_0x4a1e3c)){_0x32d3d0[_0x52ff('0x69')](_0x21c71a);_0x132537[_0x52ff('0x4c')](_0x52ff('0x97'),_0x4a1e3c);}BPromise[_0x52ff('0x98')]()[_0x52ff('0x29')](function(){if(!_0x2207a0){if(_0xbbfef8['query'][_0x52ff('0x7f')])return;_0x132537[_0x52ff('0x4c')](_0x52ff('0x99'));_0x32d3d0[_0x52ff('0x4c')](_0x52ff('0x99'));return;}if(_0xbbfef8['user'][_0x52ff('0x9a')]!==_0x52ff('0x9b'))return;return _0xbbfef8['user'][_0x52ff('0x9c')]({'attributes':['id'],'raw':!![]})['then'](function(_0x1ef0aa){if(_[_0x52ff('0x7e')](_0x1ef0aa)){_0x132537['where']('i.WhatsappAccountId\x20IS\x20NULL');_0x32d3d0[_0x52ff('0x4c')](_0x52ff('0x9d'));}else{_0x132537[_0x52ff('0x4c')](_0x52ff('0x80'),_[_0x52ff('0x3e')](_0x1ef0aa,'id'));_0x32d3d0[_0x52ff('0x4c')](_0x52ff('0x80'),_[_0x52ff('0x3e')](_0x1ef0aa,'id'));}});})[_0x52ff('0x29')](function(){if(_0xbbfef8['user'][_0x52ff('0x9a')]===_0x52ff('0x9b')&&!_0x2207a0&&!_0xbbfef8[_0x52ff('0x43')][_0x52ff('0x7f')])return[];return db[_0x52ff('0x9e')][_0x52ff('0x43')](_0x132537[_0x52ff('0x9f')](),_0x435c34);})[_0x52ff('0x29')](function(_0x58e9fa){_0x1eb769[_0x52ff('0x21')]=_0x58e9fa[_0x52ff('0x64')];if(_0x1eb769[_0x52ff('0x21')]===0x0)return[];return db[_0x52ff('0x9e')]['query'](_0x3e5e8d[_0x52ff('0x9f')](),_0x435c34)[_0x52ff('0x29')](function(_0x5a141c){_0x6fa6a3=_['keyBy'](_0x5a141c,'id');_0x435c34=_[_0x52ff('0xa0')](_0x435c34,{'model':db[_0x52ff('0x3f')],'mapToModel':!![],'raw':!![],'nest':!![]});if(_0xbbfef8[_0x52ff('0x43')][_0x52ff('0x47')]){_0x28c330['attributes'][_0x52ff('0x33')](function(_0xdd3215){_0x32d3d0[_0x52ff('0x4e')]('i.'+_0xdd3215);});}else{_0x32d3d0[_0x52ff('0x4e')](_0x52ff('0x95'),_0x52ff('0x67'));_0x32d3d0[_0x52ff('0x4e')](_0x52ff('0xa1'),_0x52ff('0xa2'));_0x32d3d0[_0x52ff('0x4e')]('a.mandatoryDisposition',_0x52ff('0xa3'));_0x32d3d0[_0x52ff('0x4e')](_0x52ff('0xa4'),_0x52ff('0xa5'));_0x32d3d0[_0x52ff('0x4e')](_0x52ff('0xa6'));_0x32d3d0[_0x52ff('0x4e')](_0x52ff('0xa7'),_0x52ff('0xa8'));_0x32d3d0[_0x52ff('0x4e')](_0x52ff('0xa9'),'Contact.lastName');_0x32d3d0[_0x52ff('0x4e')](_0x52ff('0xaa'),_0x52ff('0xab'));_0x32d3d0['field']('c.phone',_0x52ff('0xac'));_0x32d3d0[_0x52ff('0x4e')](_0x52ff('0xad'),_0x52ff('0xae'));_0x32d3d0[_0x52ff('0x4e')]('c.fax',_0x52ff('0xaf'));_0x32d3d0[_0x52ff('0x4e')](_0x52ff('0xb0'),_0x52ff('0xb1'));_0x32d3d0[_0x52ff('0x4e')](_0x52ff('0xb2'),_0x52ff('0xb3'));_0x32d3d0[_0x52ff('0x4e')](_0x52ff('0xb4'),_0x52ff('0x32'));}if(_0x28c330[_0x52ff('0x20')])_0x32d3d0[_0x52ff('0x20')](_0x28c330[_0x52ff('0x20')]);if(_0x28c330[_0x52ff('0x22')])_0x32d3d0[_0x52ff('0x22')](_0x28c330[_0x52ff('0x22')]);return db[_0x52ff('0x9e')][_0x52ff('0x43')](_0x32d3d0['toString'](),_0x435c34);})[_0x52ff('0x29')](function(_0x3abbf9){if(_0x3abbf9[_0x52ff('0x64')]>0x0)_0x38283f[_0x52ff('0x4c')](_0x52ff('0xb5'),_[_0x52ff('0x3e')](_0x3abbf9,'id'));return db['sequelize']['query'](_0x38283f[_0x52ff('0x9f')](),_0x435c34)[_0x52ff('0x29')](function(_0x2de329){var _0x3e10b2=_['groupBy'](_0x2de329,_0x52ff('0xb6'));var _0x241171=[];_0x3abbf9[_0x52ff('0x33')](function(_0x43b279){_0x241171[_0x52ff('0x34')](updateWhatsappInteraction(_0x43b279,_0x6fa6a3,_0x3e10b2));});return BPromise[_0x52ff('0xb7')](_0x241171);});});})[_0x52ff('0x29')](function(_0x1e468f){_0x1eb769[_0x52ff('0xb8')]=_0x1e468f;return _0x1eb769;})[_0x52ff('0x29')](respondWithFilteredResult(_0x245d42,_0x28c330))[_0x52ff('0x35')](handleError(_0x245d42,null));};exports['show']=function(_0x1bfcd4,_0xdc2520){var _0x5a4ee2={'raw':![],'where':{'id':_0x1bfcd4[_0x52ff('0xb9')]['id']}},_0x5c9940={};_0x5c9940[_0x52ff('0x42')]=_[_0x52ff('0x44')](db[_0x52ff('0x3f')][_0x52ff('0xba')]);_0x5c9940['query']=_[_0x52ff('0x44')](_0x1bfcd4['query']);_0x5c9940[_0x52ff('0x4d')]=_[_0x52ff('0x45')](_0x5c9940[_0x52ff('0x42')],_0x5c9940[_0x52ff('0x43')]);_0x5a4ee2[_0x52ff('0x46')]=_[_0x52ff('0x45')](_0x5c9940['model'],qs[_0x52ff('0x47')](_0x1bfcd4[_0x52ff('0x43')][_0x52ff('0x47')]));_0x5a4ee2[_0x52ff('0x46')]=_0x5a4ee2['attributes'][_0x52ff('0x64')]?_0x5a4ee2[_0x52ff('0x46')]:_0x5c9940[_0x52ff('0x42')];if(_0x1bfcd4[_0x52ff('0x43')]['includeAll']){_0x5a4ee2[_0x52ff('0xbb')]=[{'all':!![]}];}_0x5a4ee2=_[_0x52ff('0xa0')]({},_0x5a4ee2,_0x1bfcd4[_0x52ff('0xbc')]);return db[_0x52ff('0x3f')][_0x52ff('0x66')](_0x5a4ee2)[_0x52ff('0x29')](handleEntityNotFound(_0xdc2520,null))[_0x52ff('0x29')](respondWithResult(_0xdc2520,null))[_0x52ff('0x35')](handleError(_0xdc2520,null));};exports[_0x52ff('0xbd')]=function(_0x1ef4b5,_0xb607){return db['WhatsappInteraction'][_0x52ff('0xbd')](_0x1ef4b5[_0x52ff('0x7c')],{})['then'](respondWithResult(_0xb607,0xc9))[_0x52ff('0x35')](handleError(_0xb607,null));};exports[_0x52ff('0x28')]=function(_0x402b47,_0x11e530){if(_0x402b47[_0x52ff('0x7c')]['id']){delete _0x402b47[_0x52ff('0x7c')]['id'];}return db[_0x52ff('0x3f')][_0x52ff('0x66')]({'where':{'id':_0x402b47[_0x52ff('0xb9')]['id']}})[_0x52ff('0x29')](handleEntityNotFound(_0x11e530,null))['then'](saveUpdates(_0x402b47[_0x52ff('0x7c')],null))['then'](respondWithResult(_0x11e530,null))[_0x52ff('0x35')](handleError(_0x11e530,null));};exports[_0x52ff('0x2a')]=function(_0x1a5054,_0x18b714){return db[_0x52ff('0x3f')][_0x52ff('0x66')]({'where':{'id':_0x1a5054[_0x52ff('0xb9')]['id']}})[_0x52ff('0x29')](handleEntityNotFound(_0x18b714,null))[_0x52ff('0x29')](removeEntity(_0x18b714,null))['catch'](handleError(_0x18b714,null));};exports[_0x52ff('0xbe')]=function(_0x58f451,_0x981528){return db['WhatsappInteraction'][_0x52ff('0xbe')]()[_0x52ff('0x29')](respondWithResult(_0x981528,null))['catch'](handleError(_0x981528,null));};exports[_0x52ff('0xbf')]=function(_0x5b05bd,_0xd5bd41,_0x2d95da){return db['WhatsappMessage'][_0x52ff('0x66')]({'where':{'id':_0x5b05bd[_0x52ff('0xb9')]['id']}})[_0x52ff('0x29')](handleEntityNotFound(_0xd5bd41,null))[_0x52ff('0x29')](function(_0xa66646){if(_0xa66646){return _0xa66646[_0x52ff('0xbf')](_0x5b05bd[_0x52ff('0x7c')]['ids'],_[_0x52ff('0xc0')](_0x5b05bd[_0x52ff('0x7c')],[_0x52ff('0xc1'),'id'])||{});}})['then'](respondWithResult(_0xd5bd41,null))['catch'](handleError(_0xd5bd41,null));};exports[_0x52ff('0xc2')]=function(_0xe3913a,_0x3be917,_0x1d54eb){var _0x1567ab={'raw':![],'where':{}};var _0x5eb838={};var _0xf45ca5={'count':0x0,'rows':[]};return db['WhatsappInteraction'][_0x52ff('0xc3')]({'where':{'id':_0xe3913a[_0x52ff('0xb9')]['id']}})[_0x52ff('0x29')](handleEntityNotFound(_0x3be917,null))[_0x52ff('0x29')](function(_0x2ad891){if(_0x2ad891){_0x5eb838[_0x52ff('0x42')]=_[_0x52ff('0x44')](db['WhatsappMessage'][_0x52ff('0xba')]);_0x5eb838[_0x52ff('0x43')]=_[_0x52ff('0x44')](_0xe3913a[_0x52ff('0x43')]);_0x5eb838[_0x52ff('0x4d')]=_['intersection'](_0x5eb838[_0x52ff('0x42')],_0x5eb838[_0x52ff('0x43')]);_0x1567ab[_0x52ff('0x46')]=_[_0x52ff('0x45')](_0x5eb838[_0x52ff('0x42')],qs['fields'](_0xe3913a[_0x52ff('0x43')][_0x52ff('0x47')]));_0x1567ab[_0x52ff('0x46')]=_0x1567ab['attributes'][_0x52ff('0x64')]?_0x1567ab[_0x52ff('0x46')]:_0x5eb838[_0x52ff('0x42')];if(!_0xe3913a[_0x52ff('0x43')][_0x52ff('0x48')](_0x52ff('0x49'))){_0x1567ab[_0x52ff('0x20')]=qs[_0x52ff('0x20')](_0xe3913a[_0x52ff('0x43')]['limit']);_0x1567ab[_0x52ff('0x22')]=qs['offset'](_0xe3913a[_0x52ff('0x43')]['offset']);}_0x1567ab[_0x52ff('0x4a')]=qs[_0x52ff('0x4b')](_0xe3913a[_0x52ff('0x43')][_0x52ff('0x4b')]);_0x1567ab[_0x52ff('0x4c')]=qs[_0x52ff('0x4d')](_[_0x52ff('0xc4')](_0xe3913a['query'],_0x5eb838[_0x52ff('0x4d')]));_0x1567ab[_0x52ff('0x4c')]['WhatsappInteractionId']=_0x2ad891['id'];if(_0xe3913a[_0x52ff('0x43')][_0x52ff('0x8b')]){_0x1567ab[_0x52ff('0x4c')]=_[_0x52ff('0xa0')](_0x1567ab[_0x52ff('0x4c')],{'$or':_[_0x52ff('0x3e')](_0x1567ab[_0x52ff('0x46')],function(_0x1c5870){var _0x248f68={};_0x248f68[_0x1c5870]={'$like':'%'+_0xe3913a['query'][_0x52ff('0x8b')]+'%'};return _0x248f68;})});}if(_0xe3913a['query'][_0x52ff('0x84')]){var _0x5f29fd=_0xe3913a['query'][_0x52ff('0x84')][_0x52ff('0x6e')](',');var _0x19cfe3={};_0x19cfe3[_0x5f29fd[0x0]]={'$gte':moment(_0x5f29fd[0x1])[_0x52ff('0xc5')](_0x52ff('0xc6'))};_0x1567ab['where']=_[_0x52ff('0xa0')](_0x1567ab[_0x52ff('0x4c')],_0x19cfe3);}_0x1567ab=_[_0x52ff('0xa0')]({},_0x1567ab,_0xe3913a['options']);return db['WhatsappMessage'][_0x52ff('0x21')]({'where':_0x1567ab['where']})[_0x52ff('0x29')](function(_0x35ad42){_0xf45ca5[_0x52ff('0x21')]=_0x35ad42;if(_0xe3913a[_0x52ff('0x43')][_0x52ff('0xc7')]){_0x1567ab[_0x52ff('0xbb')]=[{'all':!![]}];}return db[_0x52ff('0xc8')]['findAll'](_0x1567ab);})[_0x52ff('0x29')](function(_0x4053c1){_0xf45ca5[_0x52ff('0xb8')]=_0x4053c1;return _0xf45ca5;});}})['then'](respondWithFilteredResult(_0x3be917,_0x1567ab))[_0x52ff('0x35')](handleError(_0x3be917,null));};exports[_0x52ff('0xc9')]=function(_0xebca29,_0x50654a,_0x530a2d){return db[_0x52ff('0x3f')][_0x52ff('0x66')]({'where':{'id':_0xebca29[_0x52ff('0xb9')]['id']}})[_0x52ff('0x29')](handleEntityNotFound(_0x50654a,null))[_0x52ff('0x29')](function(_0x42f1ce){if(_0x42f1ce){return _0x42f1ce[_0x52ff('0xca')](_0xebca29[_0x52ff('0x7c')][_0x52ff('0xc1')],_[_0x52ff('0xc0')](_0xebca29['body'],['ids','id'])||{})[_0x52ff('0xcb')](function(){return db[_0x52ff('0x77')][_0x52ff('0xcc')]({'attributes':['id',_0x52ff('0x2d'),_0x52ff('0x4f')],'where':{'id':_0xebca29[_0x52ff('0x7c')][_0x52ff('0xc1')]}});})[_0x52ff('0x29')](function(_0x345943){socket['emit'](_0x52ff('0xcd'),{'id':Number(_0xebca29[_0x52ff('0xb9')]['id']),'tags':_0x345943||[]});return{'id':Number(_0xebca29[_0x52ff('0xb9')]['id']),'tags':_0x345943||[]};});}})[_0x52ff('0x29')](respondWithResult(_0x50654a,null))[_0x52ff('0x35')](handleError(_0x50654a,null));};exports[_0x52ff('0xce')]=function(_0x3c6146,_0x26ce46,_0x4d0a47){return db[_0x52ff('0x3f')][_0x52ff('0x66')]({'where':{'id':_0x3c6146['params']['id']}})['then'](handleEntityNotFound(_0x26ce46,null))['then'](function(_0x4c5218){if(_0x4c5218){return _0x4c5218[_0x52ff('0xce')](_0x3c6146['query'][_0x52ff('0xc1')]);}})[_0x52ff('0x29')](respondWithStatusCode(_0x26ce46,null))['catch'](handleError(_0x26ce46,null));};exports[_0x52ff('0xcf')]=function(_0x47817e,_0xea4c45){var _0x1c2118=moment()[_0x52ff('0xd0')]()[_0x52ff('0x9f')]();var _0x47f78b=path[_0x52ff('0xd1')](config['root'],_0x52ff('0xd2'),_0x52ff('0xd3'),_0x52ff('0xd4'));var _0x3a93b3=path[_0x52ff('0xd1')](config['root'],_0x52ff('0xd2'),'files','attachments');var _0x400927=path[_0x52ff('0xd1')](_0x47f78b,_0x1c2118);var _0x2c10c9=util['format']('transcript-%d-%s.zip',_0x47817e[_0x52ff('0xb9')]['id'],_0x1c2118);var _0x5a9a88=path['join'](_0x47f78b,_0x2c10c9);var _0x5789a2=[];_0x5789a2[_0x52ff('0x34')]({'model':db[_0x52ff('0x7a')],'as':_0x52ff('0x7a'),'attributes':['id',_0x52ff('0x30')],'raw':!![]});_0x5789a2[_0x52ff('0x34')]({'model':db[_0x52ff('0xd5')],'as':_0x52ff('0x6b'),'attributes':['id',_0x52ff('0xd6'),_0x52ff('0xd7')],'raw':!![]});if(_0x47817e['query']['attachments']){_0x5789a2['push']({'model':db[_0x52ff('0xd8')],'as':_0x52ff('0xd8'),'raw':!![]});}var _0x1ed981=[{'model':db[_0x52ff('0xc8')],'as':_0x52ff('0xd9'),'attributes':['id',_0x52ff('0x7c'),_0x52ff('0x82'),'direction',_0x52ff('0xda'),_0x52ff('0x87')],'include':_0x5789a2}];_0x1ed981[_0x52ff('0x34')]({'model':db['WhatsappAccount'],'as':_0x52ff('0xdb'),'attributes':['id',_0x52ff('0x2d')]});_0x1ed981[_0x52ff('0x34')]({'model':db[_0x52ff('0x7a')],'as':_0x52ff('0xdc'),'attributes':['id',_0x52ff('0x30')]});return db[_0x52ff('0x3f')][_0x52ff('0x66')]({'where':{'id':_0x47817e[_0x52ff('0xb9')]['id']},'include':_0x1ed981})[_0x52ff('0x29')](handleEntityNotFound(_0xea4c45,null))['then'](function(_0x3de29f){if(_0x3de29f){var _0x44daad=_0x3de29f[_0x52ff('0xdd')]({'plain':!![]});fs['mkdirSync'](_0x400927);var _0x306225={'channel':_0x52ff('0xde'),'account':_0x44daad[_0x52ff('0xdb')][_0x52ff('0x2d')],'agent':_0x44daad['Owner']?_0x44daad[_0x52ff('0xdc')]['fullname']:'N.A.','createdAt':moment(_0x44daad[_0x52ff('0x82')])['format'](_0x52ff('0xc6'))['toString'](),'closedAt':_0x44daad['closed']?moment(_0x44daad['closedAt'])['format'](_0x52ff('0xc6'))[_0x52ff('0x9f')]():'','disposition':_0x44daad[_0x52ff('0xdf')],'messages':_(_0x44daad[_0x52ff('0xd9')])['filter']({'secret':![]})[_0x52ff('0x3e')](function(_0x303de5){if(_0x303de5['Attachment']){var _0x462f68=path[_0x52ff('0xd1')](_0x3a93b3,_0x303de5[_0x52ff('0xd8')][_0x52ff('0xe0')]);if(fs['existsSync'](_0x462f68)){fs[_0x52ff('0xe1')](_0x462f68)['pipe'](fs[_0x52ff('0xe2')](path[_0x52ff('0xd1')](_0x400927,_0x303de5[_0x52ff('0xd8')]['basename'])));}}return{'date':moment(_0x303de5[_0x52ff('0x82')])[_0x52ff('0xc5')](_0x52ff('0xc6'))[_0x52ff('0x9f')](),'sender':_0x303de5[_0x52ff('0xe3')]===_0x52ff('0xe4')?_0x303de5[_0x52ff('0x7a')]?_0x303de5[_0x52ff('0x7a')][_0x52ff('0x30')]:_0x52ff('0xe5'):_0x303de5[_0x52ff('0x6b')]['firstName']+(_0x303de5[_0x52ff('0x6b')][_0x52ff('0xd7')]?'\x20'+_0x303de5[_0x52ff('0x6b')][_0x52ff('0xd7')]:''),'body':_0x303de5[_0x52ff('0xd8')]?_0x303de5['Attachment']['basename']:_0x303de5[_0x52ff('0x7c')],'direction':_0x303de5[_0x52ff('0xe3')]==='out'?'A':'C','secret':_0x303de5['secret'],'read':_0x303de5[_0x52ff('0x87')],'attachment':_0x303de5['Attachment']};})[_0x52ff('0x68')]()};return ejs[_0x52ff('0xe6')](path[_0x52ff('0xd1')](config[_0x52ff('0xe7')],_0x52ff('0xd2'),'views',_0x52ff('0xe8')),{'interaction':_0x306225})[_0x52ff('0x29')](function(_0x500f23){var _0x4e887d=path[_0x52ff('0xd1')](_0x400927,_0x52ff('0xe9')+_0x44daad['id']+'-'+_0x1c2118+_0x52ff('0xea'));var _0x4c9afa={'path':_0x4e887d,'channel':_0x52ff('0xde'),'interactionId':_0x44daad['id']};return pdf[_0x52ff('0xeb')](_0x500f23,_0x4c9afa);})[_0x52ff('0x29')](function(){return new BPromise(function(_0x1d051e,_0xc1911a){zipdir(_0x400927,{'saveTo':_0x5a9a88},function(_0x19c7fd,_0x1acfdf){if(_0x19c7fd)return _0xc1911a(_0x19c7fd);return _0x1d051e(_0x1acfdf);});})[_0x52ff('0x29')](function(){return new BPromise(function(_0x56c3aa,_0x5a85aa){rimraf(_0x400927,function(_0x2ce8a5){if(_0x2ce8a5)_0x5a85aa(_0x2ce8a5);return _0x56c3aa();});});})[_0x52ff('0x29')](function(){return _0xea4c45[_0x52ff('0xcf')](_0x5a9a88,_0x2c10c9,function(_0x35d33d){if(_0x35d33d){console[_0x52ff('0xec')]('err',_0x35d33d);}else{fs[_0x52ff('0xed')](_0x5a9a88);}});});});}else{return _0xea4c45[_0x52ff('0x2b')](0xc8);}})[_0x52ff('0x35')](handleError(_0xea4c45,null));};