1c255d122ccf10f32664c68ba916203531a3af7d
[motion2.git] / server / api / openchannelInteraction / openchannelInteraction.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 _0xf133=['where','select','field','color','from','tools_tags','u.id','u.fullname','ui.OpenchannelInteractionId','left_join','users','openchannel_interactions','c.id\x20=\x20i.ContactId\x20AND\x20c.deletedAt\x20IS\x20NULL','o.id\x20=\x20i.UserId','me.OpenchannelInteractionId\x20=\x20i.id','openchannel_interaction_has_tags','it.OpenchannelInteractionId\x20=\x20i.id','t.id\x20=\x20it.TagId','expr','search','parseSearch','sqlOperator','conditions','unreadMessages','value','having','`\x20=\x200','Contact','operator','$substring','MATCH\x20(c.firstName,\x20c.lastName,\x20c.phone,\x20c.mobile,\x20c.fax,\x20c.email)\x20AGAINST\x20(?\x20IN\x20BOOLEAN\x20MODE)','getFullTextValue','c.firstName\x20LIKE\x20?\x20OR\x20c.lastName\x20LIKE\x20?\x20OR\x20c.email\x20LIKE\x20?','find','ContactId','buildExpression','type','start','Tag','User','CONCAT_WS(\x27\x20\x27,\x20o.name,\x20o.fullname)\x20LIKE\x20?','UserId','text','compact','isEmpty','forEach','OpenchannelAccountId','concat','createdAt','$gte','parse','i.createdAt\x20BETWEEN\x20?\x20AND\x20?','null','\x20IS\x20NULL','\x20=\x20?','read','`unreadMessages`\x20=\x200','`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)','i.from\x20LIKE\x20?','group','Sequelize','QueryTypes','i.id','SUM(me.read\x20=\x200\x20AND\x20me.direction\x20=\x20\x27in\x27)','contactName','DESC','t.id\x20IN\x20?','resolve','i.OpenchannelAccountId\x20IS\x20NOT\x20NULL','role','agent','user','getOpenchannelAccounts','i.OpenchannelAccountId\x20IS\x20NULL','i.OpenchannelAccountId\x20IN\x20?','toString','length','sequelize','keyBy','merge','OpenchannelInteraction','Account.mandatoryDisposition','i.*','Contact.firstName','c.lastName','Contact.lastName','c.email','Contact.email','c.phone','Contact.phone','c.mobile','Contact.mobile','c.fax','Contact.fax','o.fullname','Owner.fullname','GROUP_CONCAT(DISTINCT\x20t.id)','groupBy','all','rows','show','params','includeAll','include','create','body','describe','addMessage','OpenchannelMessage','ids','omit','hasOwnProperty','pick','OpenchannelInteractionId','format','YYYY-MM-DD\x20HH:mm:ss','addTags','setTags','emit','removeTags','unix','join','root','server','files','tmp','transcript-%d-%s.zip','CmContact','firstName','lastName','attachments','Attachment','Messages','secret','OpenchannelAccount','Account','Owner','get','mkdirSync','Openchannel','N.A.','closed','closedAt','Public','Private','basename','createReadStream','pipe','createWriteStream','direction','out','System','views','downloadInteraction.ejs','download','err','unlinkSync','eml-format','rimraf','zip-dir','fast-json-patch','bluebird','mustache','path','to-csv','ejs','fs-extra','lodash','squel','crypto','desk.js','querystring','ioredis','../../components/auth/service','../../components/parsers/advancedSearch','../../components/export/pdf','../../config/license/hardware','../../config/logger','api','../../config/environment','../../config/license/util','../../mysqldb','redis','defaults','localhost','socket.io-emitter','register','status','end','json','offset','undefined','limit','count','set','reject','save','update','sendStatus','error','stack','name','Users','map','fullname','TagIds','split','Tags','push','then','catch','index','rawAttributes','fieldName','key','model','keys','query','filters','intersection','attributes','fields','nolimit','order','sort'];(function(_0x10fae2,_0x389018){var _0x7bf995=function(_0x3576f9){while(--_0x3576f9){_0x10fae2['push'](_0x10fae2['shift']());}};_0x7bf995(++_0x389018);}(_0xf133,0x18c));var _0x3f13=function(_0x2235bf,_0x3a06a7){_0x2235bf=_0x2235bf-0x0;var _0x4c3a64=_0xf133[_0x2235bf];return _0x4c3a64;};'use strict';var emlformat=require(_0x3f13('0x0'));var rimraf=require(_0x3f13('0x1'));var zipdir=require(_0x3f13('0x2'));var jsonpatch=require(_0x3f13('0x3'));var rp=require('request-promise');var moment=require('moment');var BPromise=require(_0x3f13('0x4'));var Mustache=require(_0x3f13('0x5'));var util=require('util');var path=require(_0x3f13('0x6'));var sox=require('sox');var csv=require(_0x3f13('0x7'));var ejs=require(_0x3f13('0x8'));var fs=require('fs');var fs_extra=require(_0x3f13('0x9'));var _=require(_0x3f13('0xa'));var squel=require(_0x3f13('0xb'));var crypto=require(_0x3f13('0xc'));var jsforce=require('jsforce');var deskjs=require(_0x3f13('0xd'));var toCsv=require(_0x3f13('0x7'));var querystring=require(_0x3f13('0xe'));var Papa=require('papaparse');var Redis=require(_0x3f13('0xf'));var authService=require(_0x3f13('0x10'));var qs=require('../../components/parsers/qs');var as=require(_0x3f13('0x11'));var pdf=require(_0x3f13('0x12'));var hardwareService=require(_0x3f13('0x13'));var logger=require(_0x3f13('0x14'))(_0x3f13('0x15'));var utils=require('../../config/utils');var config=require(_0x3f13('0x16'));var licenseUtil=require(_0x3f13('0x17'));var db=require(_0x3f13('0x18'))['db'];config[_0x3f13('0x19')]=_[_0x3f13('0x1a')](config['redis'],{'host':_0x3f13('0x1b'),'port':0x18eb});var socket=require(_0x3f13('0x1c'))(new Redis(config[_0x3f13('0x19')]));require('./openchannelInteraction.socket')[_0x3f13('0x1d')](socket);function respondWithStatusCode(_0x3e0af2,_0x1a87eb){_0x1a87eb=_0x1a87eb||0xcc;return function(_0x5d80d1){if(_0x5d80d1){return _0x3e0af2['sendStatus'](_0x1a87eb);}return _0x3e0af2[_0x3f13('0x1e')](_0x1a87eb)[_0x3f13('0x1f')]();};}function respondWithResult(_0x5612aa,_0x7cd02a){_0x7cd02a=_0x7cd02a||0xc8;return function(_0x237bc8){if(_0x237bc8){return _0x5612aa[_0x3f13('0x1e')](_0x7cd02a)[_0x3f13('0x20')](_0x237bc8);}};}function respondWithFilteredResult(_0x1806e6,_0x4daa9d){return function(_0x3cbe36){if(_0x3cbe36){var _0x14b9f1=typeof _0x4daa9d[_0x3f13('0x21')]===_0x3f13('0x22')&&typeof _0x4daa9d[_0x3f13('0x23')]===_0x3f13('0x22');var _0x2972b0=_0x3cbe36[_0x3f13('0x24')];var _0x19b1da=_0x14b9f1?0x0:_0x4daa9d[_0x3f13('0x21')];var _0x18b282=_0x14b9f1?_0x3cbe36[_0x3f13('0x24')]:_0x4daa9d[_0x3f13('0x21')]+_0x4daa9d[_0x3f13('0x23')];var _0x509ee5;if(_0x18b282>=_0x2972b0){_0x18b282=_0x2972b0;_0x509ee5=0xc8;}else{_0x509ee5=0xce;}_0x1806e6['status'](_0x509ee5);return _0x1806e6[_0x3f13('0x25')]('Content-Range',_0x19b1da+'-'+_0x18b282+'/'+_0x2972b0)[_0x3f13('0x20')](_0x3cbe36);}return null;};}function patchUpdates(_0x2b62b0){return function(_0x3afab6){try{jsonpatch['apply'](_0x3afab6,_0x2b62b0,!![]);}catch(_0x46fbaa){return BPromise[_0x3f13('0x26')](_0x46fbaa);}return _0x3afab6[_0x3f13('0x27')]();};}function saveUpdates(_0x412ea5,_0x1ac320){return function(_0x4ba7b2){if(_0x4ba7b2){return _0x4ba7b2[_0x3f13('0x28')](_0x412ea5)['then'](function(_0x5df9b1){return _0x5df9b1;});}return null;};}function removeEntity(_0x51fe40,_0x4638c5){return function(_0x4e42f9){if(_0x4e42f9){return _0x4e42f9['destroy']()['then'](function(){_0x51fe40[_0x3f13('0x1e')](0xcc)[_0x3f13('0x1f')]();});}};}function handleEntityNotFound(_0x5bb577,_0x10e707){return function(_0x1f335c){if(!_0x1f335c){_0x5bb577[_0x3f13('0x29')](0x194);}return _0x1f335c;};}function handleError(_0x55f9d5,_0x1ecb16){_0x1ecb16=_0x1ecb16||0x1f4;return function(_0x233932){logger[_0x3f13('0x2a')](_0x233932[_0x3f13('0x2b')]);if(_0x233932[_0x3f13('0x2c')]){delete _0x233932[_0x3f13('0x2c')];}_0x55f9d5[_0x3f13('0x1e')](_0x1ecb16)['send'](_0x233932);};}function getInteractionUsers(_0x1a1483,_0x247057){return new BPromise(function(_0x149550,_0x3bf14f){try{if(_0x247057[_0x1a1483['id']]){_0x1a1483[_0x3f13('0x2d')]=_[_0x3f13('0x2e')](_0x247057[_0x1a1483['id']],function(_0x5054f1){return{'id':_0x5054f1['id'],'fullname':_0x5054f1[_0x3f13('0x2f')]};});}}catch(_0x2272c1){_0x3bf14f(_0x2272c1);}_0x149550(_0x1a1483);});}function getInteractionTags(_0xf9dbc1,_0x199aba){return new BPromise(function(_0x4677e3,_0x4df664){try{if(_0xf9dbc1[_0x3f13('0x30')]){_0xf9dbc1['Tags']=[];_0xf9dbc1['TagIds'][_0x3f13('0x31')](',')['forEach'](function(_0x35c321){_0xf9dbc1[_0x3f13('0x32')][_0x3f13('0x33')](_0x199aba[_0x35c321]);});}delete _0xf9dbc1[_0x3f13('0x30')];}catch(_0x2ffa96){_0x4df664(_0x2ffa96);}_0x4677e3(_0xf9dbc1);});}function updateOpenchannelInteraction(_0x56b71b,_0x2c36c4,_0x489dc6){return new BPromise(function(_0x624ef7,_0x5c3457){return getInteractionUsers(_0x56b71b,_0x489dc6)[_0x3f13('0x34')](function(_0x21cecf){return getInteractionTags(_0x21cecf,_0x2c36c4);})[_0x3f13('0x34')](function(_0x2f0f0a){_0x624ef7(_0x2f0f0a);})[_0x3f13('0x35')](function(_0x37c5f8){_0x5c3457(_0x37c5f8);});});}exports[_0x3f13('0x36')]=function(_0x4bf18a,_0x141315){var _0x2bda31={},_0x393314={},_0x318679={'count':0x0,'rows':[]};var _0x58f424=_[_0x3f13('0x2e')](db['OpenchannelInteraction'][_0x3f13('0x37')],function(_0x3b2ed8){return{'name':_0x3b2ed8[_0x3f13('0x38')],'type':_0x3b2ed8['type'][_0x3f13('0x39')]};});_0x393314[_0x3f13('0x3a')]=_[_0x3f13('0x2e')](_0x58f424,_0x3f13('0x2c'));_0x393314['query']=_[_0x3f13('0x3b')](_0x4bf18a[_0x3f13('0x3c')]);_0x393314[_0x3f13('0x3d')]=_[_0x3f13('0x3e')](_0x393314[_0x3f13('0x3a')],_0x393314[_0x3f13('0x3c')]);_0x2bda31[_0x3f13('0x3f')]=_[_0x3f13('0x3e')](_0x393314[_0x3f13('0x3a')],qs['fields'](_0x4bf18a[_0x3f13('0x3c')][_0x3f13('0x40')]));_0x2bda31[_0x3f13('0x3f')]=_0x2bda31['attributes']['length']?_0x2bda31[_0x3f13('0x3f')]:_0x393314[_0x3f13('0x3a')];if(!_0x4bf18a[_0x3f13('0x3c')]['hasOwnProperty'](_0x3f13('0x41'))){_0x2bda31[_0x3f13('0x23')]=qs[_0x3f13('0x23')](_0x4bf18a['query'][_0x3f13('0x23')]);_0x2bda31['offset']=qs['offset'](_0x4bf18a[_0x3f13('0x3c')][_0x3f13('0x21')]);}_0x2bda31[_0x3f13('0x42')]=qs[_0x3f13('0x43')](_0x4bf18a[_0x3f13('0x3c')][_0x3f13('0x43')]);_0x2bda31[_0x3f13('0x44')]=qs[_0x3f13('0x3d')](_['pick'](_0x4bf18a[_0x3f13('0x3c')],_0x393314[_0x3f13('0x3d')]),_0x58f424);var _0x108d31=[];var _0x1cd28e=squel[_0x3f13('0x45')]()[_0x3f13('0x46')]('id')[_0x3f13('0x46')](_0x3f13('0x2c'))['field'](_0x3f13('0x47'))[_0x3f13('0x48')](_0x3f13('0x49'));var _0x1b183a=squel[_0x3f13('0x45')]()[_0x3f13('0x46')](_0x3f13('0x4a'))['field'](_0x3f13('0x4b'))[_0x3f13('0x46')](_0x3f13('0x4c'))[_0x3f13('0x48')]('user_has_openchannel_interactions','ui')[_0x3f13('0x4d')](_0x3f13('0x4e'),'u','u.id\x20=\x20ui.UserId');var _0x402eb6=squel[_0x3f13('0x45')]()[_0x3f13('0x48')](_0x3f13('0x4f'),'i')[_0x3f13('0x4d')]('cm_contacts','c',_0x3f13('0x50'))['left_join'](_0x3f13('0x4e'),'o',_0x3f13('0x51'))['left_join']('openchannel_accounts','a','a.id\x20=\x20i.OpenchannelAccountId')['left_join']('openchannel_messages','me',_0x3f13('0x52'))['left_join'](_0x3f13('0x53'),'it',_0x3f13('0x54'))[_0x3f13('0x4d')](_0x3f13('0x49'),'t',_0x3f13('0x55'));var _0x503f75=squel['expr']();var _0x465c10=[];var _0x3251b9=squel[_0x3f13('0x56')]();var _0x31f178;if(_0x4bf18a[_0x3f13('0x3c')][_0x3f13('0x57')]){_0x31f178=as[_0x3f13('0x58')](_0x4bf18a['query'][_0x3f13('0x57')]);var _0x1b63f7=_0x31f178[_0x3f13('0x59')];for(var _0x383c59=0x0;_0x383c59<_0x31f178[_0x3f13('0x5a')]['length'];_0x383c59++){var _0x2b5fd9=_0x31f178[_0x3f13('0x5a')][_0x383c59];var _0x37b508='i';var _0x172243=_['find'](_0x58f424,[_0x3f13('0x2c'),_0x2b5fd9['field']]);if(!_0x172243){switch(_0x2b5fd9[_0x3f13('0x46')]){case _0x3f13('0x5b'):if(_0x2b5fd9[_0x3f13('0x5c')]==0x1){_0x402eb6[_0x3f13('0x5d')]('`'+_0x2b5fd9[_0x3f13('0x46')]+_0x3f13('0x5e'));}else{_0x402eb6[_0x3f13('0x5d')]('`'+_0x2b5fd9[_0x3f13('0x46')]+'`\x20>\x200');}break;case _0x3f13('0x5f'):if(_0x2b5fd9[_0x3f13('0x60')]===_0x3f13('0x61')){if(_0x2b5fd9[_0x3f13('0x5c')][_0x3f13('0x31')]('\x20')['length']>0x1){_0x503f75[_0x1b63f7](_0x3f13('0x62'),qs[_0x3f13('0x63')](_0x2b5fd9[_0x3f13('0x5c')]),null);}else{var _0x206f13='%'+_0x2b5fd9['value']+'%';_0x503f75[_0x1b63f7](_0x3f13('0x64'),_0x206f13,_0x206f13,_0x206f13);}}else{_0x172243=_[_0x3f13('0x65')](_0x58f424,[_0x3f13('0x2c'),_0x3f13('0x66')]);_0x2b5fd9[_0x3f13('0x46')]=_0x3f13('0x66');_0xc73f63=as[_0x3f13('0x67')](_0x37b508,_0x172243[_0x3f13('0x68')],_0x2b5fd9);_0x503f75[_0x1b63f7](_0xc73f63['text'],_0xc73f63[_0x3f13('0x5c')][_0x3f13('0x69')],_0xc73f63['value'][_0x3f13('0x1f')]);}break;case _0x3f13('0x6a'):var _0x1594f3=_0x2b5fd9[_0x3f13('0x5c')][_0x3f13('0x31')](',')[_0x3f13('0x2e')](function(_0x22f311){return Number(_0x22f311);});_0x1594f3['forEach'](function(_0x464ea1){_0x3251b9['or']('CONCAT(\x27,\x27,\x20`TagIds`,\x20\x27,\x27)\x20LIKE\x20?','%,'+_0x464ea1+',%');});_0x465c10=_['union'](_0x465c10,_0x1594f3);break;case _0x3f13('0x6b'):if(_0x2b5fd9[_0x3f13('0x60')]==='$substring'){_0x503f75[_0x1b63f7](_0x3f13('0x6c'),'%'+_0x2b5fd9[_0x3f13('0x5c')]+'%',null);}else{_0x172243=_[_0x3f13('0x65')](_0x58f424,[_0x3f13('0x2c'),_0x3f13('0x6d')]);_0x2b5fd9[_0x3f13('0x46')]='UserId';_0xc73f63=as[_0x3f13('0x67')](_0x37b508,_0x172243[_0x3f13('0x68')],_0x2b5fd9);_0x503f75[_0x1b63f7](_0xc73f63[_0x3f13('0x6e')],_0xc73f63['value'][_0x3f13('0x69')],_0xc73f63[_0x3f13('0x5c')]['end']);}break;case'body':_0xc73f63=as[_0x3f13('0x67')]('me',null,_0x2b5fd9);_0x503f75[_0x1b63f7](_0xc73f63[_0x3f13('0x6e')],_0xc73f63['value'][_0x3f13('0x69')],_0xc73f63[_0x3f13('0x5c')][_0x3f13('0x1f')]);break;}}else{var _0xc73f63=as[_0x3f13('0x67')](_0x37b508,_0x172243[_0x3f13('0x68')],_0x2b5fd9);_0x503f75[_0x1b63f7](_0xc73f63['text'],_0xc73f63[_0x3f13('0x5c')]['start'],_0xc73f63[_0x3f13('0x5c')][_0x3f13('0x1f')]);}}}else{var _0x4d2e57=_(_0x4bf18a[_0x3f13('0x3c')])[_0x3f13('0x3b')]()['map'](function(_0x4ba588){return _['some'](_0x58f424,[_0x3f13('0x2c'),_0x4ba588])?_0x4ba588:undefined;})[_0x3f13('0x6f')]()['value']();if(!_[_0x3f13('0x70')](_0x4d2e57)){_0x4d2e57[_0x3f13('0x71')](function(_0x17f481){if(_0x17f481===_0x3f13('0x72')){_0x402eb6[_0x3f13('0x44')]('i.OpenchannelAccountId\x20IN\x20?',[][_0x3f13('0x73')](_0x4bf18a[_0x3f13('0x3c')][_0x17f481]));}else if(_0x17f481===_0x3f13('0x74')){var _0x1bbf02=JSON['parse'](_0x4bf18a['query'][_0x17f481])[_0x3f13('0x75')];var _0x320009=JSON[_0x3f13('0x76')](_0x4bf18a[_0x3f13('0x3c')][_0x17f481])['$lte'];_0x402eb6[_0x3f13('0x44')](_0x3f13('0x77'),_0x1bbf02,_0x320009);}else{if(_0x4bf18a[_0x3f13('0x3c')][_0x17f481]===_0x3f13('0x78')){_0x402eb6[_0x3f13('0x44')]('i.'+_0x17f481+_0x3f13('0x79'));}else{_0x402eb6[_0x3f13('0x44')]('i.'+_0x17f481+_0x3f13('0x7a'),_0x4bf18a[_0x3f13('0x3c')][_0x17f481]);}}});}if(_0x4bf18a[_0x3f13('0x3c')][_0x3f13('0x7b')]){if(_0x4bf18a[_0x3f13('0x3c')]['read']==0x1){_0x402eb6['having'](_0x3f13('0x7c'));}else{_0x402eb6[_0x3f13('0x5d')](_0x3f13('0x7d'));}}if(_0x4bf18a['query'][_0x3f13('0x7e')]){_0x465c10=_[_0x3f13('0x7f')](_0x4bf18a[_0x3f13('0x3c')][_0x3f13('0x7e')])?_0x4bf18a[_0x3f13('0x3c')][_0x3f13('0x7e')]:new Array(_0x4bf18a['query'][_0x3f13('0x7e')]);_0x465c10[_0x3f13('0x71')](function(_0xee746f){_0x3251b9['or'](_0x3f13('0x80'),'%,'+_0xee746f+',%');});}if(_0x4bf18a[_0x3f13('0x3c')][_0x3f13('0x81')]){var _0x41a645=_0x4bf18a[_0x3f13('0x3c')][_0x3f13('0x81')][_0x3f13('0x82')]('\x5c','\x5c\x5c')[_0x3f13('0x82')](/'/g,'\x27\x27');if(qs['isNumeric'](_0x41a645)){_0x503f75['or'](_0x3f13('0x83'),_0x41a645+'%');}else{_0x503f75['or'](_0x3f13('0x84'),qs[_0x3f13('0x63')](_0x41a645))['or'](_0x3f13('0x85'),_0x41a645+'%')['or']('CONCAT(c.firstName,\x20\x27\x20\x27,\x20c.lastName)\x20LIKE\x20?',_0x41a645+'%');}}}_0x402eb6[_0x3f13('0x44')](_0x503f75);_0x402eb6[_0x3f13('0x86')]('i.id');var _0x40643a={'type':db[_0x3f13('0x87')][_0x3f13('0x88')]['SELECT'],'raw':!![]};var _0x3c5fc4=_0x402eb6['clone']();_0x3c5fc4[_0x3f13('0x46')](_0x3f13('0x89'));_0x3c5fc4[_0x3f13('0x46')](_0x3f13('0x8a'),_0x3f13('0x5b'));if(_0x2bda31[_0x3f13('0x42')]){_0x2bda31[_0x3f13('0x42')][_0x3f13('0x71')](function(_0x2f2921){var _0x4590e8=_0x2f2921[0x0]===_0x3f13('0x8b')?_0x2f2921[0x0]:'i.'+_0x2f2921[0x0];_0x402eb6[_0x3f13('0x42')](_0x4590e8,_0x2f2921[0x1]===_0x3f13('0x8c')?![]:!![]);});}if(!_[_0x3f13('0x70')](_0x465c10)){_0x402eb6['having'](_0x3251b9);_0x3c5fc4[_0x3f13('0x44')](_0x3f13('0x8d'),_0x465c10);}BPromise[_0x3f13('0x8e')]()['then'](function(){if(!_0x31f178){if(_0x4bf18a['query'][_0x3f13('0x72')])return;_0x3c5fc4[_0x3f13('0x44')](_0x3f13('0x8f'));_0x402eb6['where'](_0x3f13('0x8f'));return;}if(_0x4bf18a['user'][_0x3f13('0x90')]!==_0x3f13('0x91'))return;return _0x4bf18a[_0x3f13('0x92')][_0x3f13('0x93')]({'attributes':['id'],'raw':!![]})[_0x3f13('0x34')](function(_0x3957d7){if(_[_0x3f13('0x70')](_0x3957d7)){_0x3c5fc4[_0x3f13('0x44')]('i.OpenchannelAccountId\x20IS\x20NULL');_0x402eb6[_0x3f13('0x44')](_0x3f13('0x94'));}else{_0x3c5fc4[_0x3f13('0x44')](_0x3f13('0x95'),_[_0x3f13('0x2e')](_0x3957d7,'id'));_0x402eb6[_0x3f13('0x44')](_0x3f13('0x95'),_[_0x3f13('0x2e')](_0x3957d7,'id'));}});})[_0x3f13('0x34')](function(){if(_0x4bf18a[_0x3f13('0x92')]['role']===_0x3f13('0x91')&&!_0x31f178&&!_0x4bf18a[_0x3f13('0x3c')][_0x3f13('0x72')])return[];return db['sequelize'][_0x3f13('0x3c')](_0x3c5fc4[_0x3f13('0x96')](),_0x40643a);})[_0x3f13('0x34')](function(_0xe1cb9f){_0x318679[_0x3f13('0x24')]=_0xe1cb9f[_0x3f13('0x97')];if(_0x318679['count']===0x0)return[];return db[_0x3f13('0x98')]['query'](_0x1cd28e[_0x3f13('0x96')](),_0x40643a)['then'](function(_0x32326b){_0x108d31=_[_0x3f13('0x99')](_0x32326b,'id');_0x40643a=_[_0x3f13('0x9a')](_0x40643a,{'model':db[_0x3f13('0x9b')],'mapToModel':!![],'raw':!![],'nest':!![]});if(_0x4bf18a[_0x3f13('0x3c')][_0x3f13('0x40')]){_0x2bda31[_0x3f13('0x3f')]['forEach'](function(_0x154abb){_0x402eb6[_0x3f13('0x46')]('i.'+_0x154abb);});}else{_0x402eb6[_0x3f13('0x46')](_0x3f13('0x8a'),_0x3f13('0x5b'));_0x402eb6['field']('a.key','Account.key');_0x402eb6[_0x3f13('0x46')]('a.mandatoryDisposition',_0x3f13('0x9c'));_0x402eb6[_0x3f13('0x46')]('CASE\x20WHEN\x20i.from\x20IS\x20NOT\x20NULL\x20AND\x20i.from\x20<>\x20\x27\x27\x20THEN\x20i.from\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',_0x3f13('0x8b'));_0x402eb6[_0x3f13('0x46')](_0x3f13('0x9d'));_0x402eb6[_0x3f13('0x46')]('c.firstName',_0x3f13('0x9e'));_0x402eb6[_0x3f13('0x46')](_0x3f13('0x9f'),_0x3f13('0xa0'));_0x402eb6['field'](_0x3f13('0xa1'),_0x3f13('0xa2'));_0x402eb6['field'](_0x3f13('0xa3'),_0x3f13('0xa4'));_0x402eb6['field'](_0x3f13('0xa5'),_0x3f13('0xa6'));_0x402eb6[_0x3f13('0x46')](_0x3f13('0xa7'),_0x3f13('0xa8'));_0x402eb6[_0x3f13('0x46')](_0x3f13('0xa9'),_0x3f13('0xaa'));_0x402eb6[_0x3f13('0x46')]('o.internal','Owner.internal');_0x402eb6['field'](_0x3f13('0xab'),_0x3f13('0x30'));}if(_0x2bda31[_0x3f13('0x23')])_0x402eb6['limit'](_0x2bda31[_0x3f13('0x23')]);if(_0x2bda31[_0x3f13('0x21')])_0x402eb6[_0x3f13('0x21')](_0x2bda31[_0x3f13('0x21')]);return db['sequelize']['query'](_0x402eb6['toString'](),_0x40643a);})[_0x3f13('0x34')](function(_0x4f1d13){if(_0x4f1d13[_0x3f13('0x97')]>0x0)_0x1b183a[_0x3f13('0x44')]('ui.OpenchannelInteractionId\x20IN\x20?',_[_0x3f13('0x2e')](_0x4f1d13,'id'));return db['sequelize'][_0x3f13('0x3c')](_0x1b183a[_0x3f13('0x96')](),_0x40643a)[_0x3f13('0x34')](function(_0x2e0965){var _0x5ad018=_[_0x3f13('0xac')](_0x2e0965,'OpenchannelInteractionId');var _0xcb4f64=[];_0x4f1d13[_0x3f13('0x71')](function(_0x8fcdef){_0xcb4f64[_0x3f13('0x33')](updateOpenchannelInteraction(_0x8fcdef,_0x108d31,_0x5ad018));});return BPromise[_0x3f13('0xad')](_0xcb4f64);});});})['then'](function(_0xfd9232){_0x318679[_0x3f13('0xae')]=_0xfd9232;return _0x318679;})[_0x3f13('0x34')](respondWithFilteredResult(_0x141315,_0x2bda31))['catch'](handleError(_0x141315,null));};exports[_0x3f13('0xaf')]=function(_0x3729af,_0x483eb6){var _0xc5164d={};if(_0x3729af[_0x3f13('0x3c')][_0x3f13('0x68')]&&_0x3729af[_0x3f13('0x3c')][_0x3f13('0x68')]===_0x3f13('0x48')){_0xc5164d['from']=_0x3729af[_0x3f13('0xb0')]['id'];}else{_0xc5164d['id']=_0x3729af[_0x3f13('0xb0')]['id'];}var _0x732be2={'raw':![],'where':_0xc5164d},_0x46da54={};_0x46da54[_0x3f13('0x3a')]=_[_0x3f13('0x3b')](db['OpenchannelInteraction']['rawAttributes']);_0x46da54['query']=_[_0x3f13('0x3b')](_0x3729af[_0x3f13('0x3c')]);_0x46da54[_0x3f13('0x3d')]=_[_0x3f13('0x3e')](_0x46da54[_0x3f13('0x3a')],_0x46da54[_0x3f13('0x3c')]);_0x732be2[_0x3f13('0x3f')]=_[_0x3f13('0x3e')](_0x46da54[_0x3f13('0x3a')],qs[_0x3f13('0x40')](_0x3729af[_0x3f13('0x3c')]['fields']));_0x732be2[_0x3f13('0x3f')]=_0x732be2[_0x3f13('0x3f')][_0x3f13('0x97')]?_0x732be2[_0x3f13('0x3f')]:_0x46da54[_0x3f13('0x3a')];if(_0x3729af[_0x3f13('0x3c')][_0x3f13('0xb1')]){_0x732be2[_0x3f13('0xb2')]=[{'all':!![]}];}_0x732be2=_[_0x3f13('0x9a')]({},_0x732be2,_0x3729af['options']);return db['OpenchannelInteraction'][_0x3f13('0x65')](_0x732be2)[_0x3f13('0x34')](handleEntityNotFound(_0x483eb6,null))[_0x3f13('0x34')](respondWithResult(_0x483eb6,null))[_0x3f13('0x35')](handleError(_0x483eb6,null));};exports[_0x3f13('0xb3')]=function(_0x773774,_0x5b97bb){return db[_0x3f13('0x9b')]['create'](_0x773774[_0x3f13('0xb4')],{})[_0x3f13('0x34')](respondWithResult(_0x5b97bb,0xc9))[_0x3f13('0x35')](handleError(_0x5b97bb,null));};exports['update']=function(_0x4da323,_0x3aafa3){if(_0x4da323['body']['id']){delete _0x4da323[_0x3f13('0xb4')]['id'];}return db[_0x3f13('0x9b')][_0x3f13('0x65')]({'where':{'id':_0x4da323[_0x3f13('0xb0')]['id']}})['then'](handleEntityNotFound(_0x3aafa3,null))[_0x3f13('0x34')](saveUpdates(_0x4da323[_0x3f13('0xb4')],null))['then'](respondWithResult(_0x3aafa3,null))['catch'](handleError(_0x3aafa3,null));};exports['destroy']=function(_0x2adcaa,_0xe62ebf){return db[_0x3f13('0x9b')][_0x3f13('0x65')]({'where':{'id':_0x2adcaa[_0x3f13('0xb0')]['id']}})['then'](handleEntityNotFound(_0xe62ebf,null))[_0x3f13('0x34')](removeEntity(_0xe62ebf,null))['catch'](handleError(_0xe62ebf,null));};exports[_0x3f13('0xb5')]=function(_0x3788ac,_0x538ccc){return db[_0x3f13('0x9b')]['describe']()[_0x3f13('0x34')](respondWithResult(_0x538ccc,null))[_0x3f13('0x35')](handleError(_0x538ccc,null));};exports[_0x3f13('0xb6')]=function(_0x2eadd7,_0x51c6c0,_0x2bcea1){return db[_0x3f13('0xb7')][_0x3f13('0x65')]({'where':{'id':_0x2eadd7['params']['id']}})[_0x3f13('0x34')](handleEntityNotFound(_0x51c6c0,null))[_0x3f13('0x34')](function(_0x3ff2db){if(_0x3ff2db){return _0x3ff2db[_0x3f13('0xb6')](_0x2eadd7[_0x3f13('0xb4')][_0x3f13('0xb8')],_[_0x3f13('0xb9')](_0x2eadd7[_0x3f13('0xb4')],[_0x3f13('0xb8'),'id'])||{});}})[_0x3f13('0x34')](respondWithResult(_0x51c6c0,null))['catch'](handleError(_0x51c6c0,null));};exports['getMessages']=function(_0x4d02b2,_0x5474ac,_0x25d7f9){var _0x2061b9={'raw':![],'where':{}};var _0x3c179a={};var _0x592e51={'count':0x0,'rows':[]};return db[_0x3f13('0x9b')]['findOne']({'where':{'id':_0x4d02b2['params']['id']}})['then'](handleEntityNotFound(_0x5474ac,null))['then'](function(_0xe58fcf){if(_0xe58fcf){_0x3c179a[_0x3f13('0x3a')]=_[_0x3f13('0x3b')](db[_0x3f13('0xb7')]['rawAttributes']);_0x3c179a[_0x3f13('0x3c')]=_[_0x3f13('0x3b')](_0x4d02b2[_0x3f13('0x3c')]);_0x3c179a['filters']=_[_0x3f13('0x3e')](_0x3c179a['model'],_0x3c179a[_0x3f13('0x3c')]);_0x2061b9[_0x3f13('0x3f')]=_[_0x3f13('0x3e')](_0x3c179a['model'],qs[_0x3f13('0x40')](_0x4d02b2[_0x3f13('0x3c')][_0x3f13('0x40')]));_0x2061b9[_0x3f13('0x3f')]=_0x2061b9['attributes'][_0x3f13('0x97')]?_0x2061b9[_0x3f13('0x3f')]:_0x3c179a[_0x3f13('0x3a')];if(!_0x4d02b2[_0x3f13('0x3c')][_0x3f13('0xba')](_0x3f13('0x41'))){_0x2061b9[_0x3f13('0x23')]=qs[_0x3f13('0x23')](_0x4d02b2['query'][_0x3f13('0x23')]);_0x2061b9['offset']=qs['offset'](_0x4d02b2[_0x3f13('0x3c')][_0x3f13('0x21')]);}_0x2061b9['order']=qs['sort'](_0x4d02b2[_0x3f13('0x3c')]['sort']);_0x2061b9['where']=qs[_0x3f13('0x3d')](_[_0x3f13('0xbb')](_0x4d02b2[_0x3f13('0x3c')],_0x3c179a['filters']));_0x2061b9[_0x3f13('0x44')][_0x3f13('0xbc')]=_0xe58fcf['id'];if(_0x4d02b2[_0x3f13('0x3c')]['filter']){_0x2061b9[_0x3f13('0x44')]=_['merge'](_0x2061b9[_0x3f13('0x44')],{'$or':_['map'](_0x2061b9[_0x3f13('0x3f')],function(_0x30d3a2){var _0x337e10={};_0x337e10[_0x30d3a2]={'$like':'%'+_0x4d02b2['query'][_0x3f13('0x81')]+'%'};return _0x337e10;})});}if(_0x4d02b2[_0x3f13('0x3c')][_0x3f13('0x75')]){var _0x2e3a8d=_0x4d02b2[_0x3f13('0x3c')][_0x3f13('0x75')][_0x3f13('0x31')](',');var _0x2b3611={};_0x2b3611[_0x2e3a8d[0x0]]={'$gte':moment(_0x2e3a8d[0x1])[_0x3f13('0xbd')](_0x3f13('0xbe'))};_0x2061b9[_0x3f13('0x44')]=_[_0x3f13('0x9a')](_0x2061b9[_0x3f13('0x44')],_0x2b3611);}_0x2061b9=_[_0x3f13('0x9a')]({},_0x2061b9,_0x4d02b2['options']);return db[_0x3f13('0xb7')][_0x3f13('0x24')]({'where':_0x2061b9['where']})['then'](function(_0x4d721b){_0x592e51[_0x3f13('0x24')]=_0x4d721b;if(_0x4d02b2[_0x3f13('0x3c')][_0x3f13('0xb1')]){_0x2061b9[_0x3f13('0xb2')]=[{'all':!![]}];}return db[_0x3f13('0xb7')]['findAll'](_0x2061b9);})[_0x3f13('0x34')](function(_0x1c08f9){_0x592e51['rows']=_0x1c08f9;return _0x592e51;});}})[_0x3f13('0x34')](respondWithFilteredResult(_0x5474ac,_0x2061b9))[_0x3f13('0x35')](handleError(_0x5474ac,null));};exports[_0x3f13('0xbf')]=function(_0x368f87,_0x1affc2,_0x134044){return db['OpenchannelInteraction'][_0x3f13('0x65')]({'where':{'id':_0x368f87[_0x3f13('0xb0')]['id']}})['then'](handleEntityNotFound(_0x1affc2,null))[_0x3f13('0x34')](function(_0x361390){if(_0x361390){return _0x361390[_0x3f13('0xc0')](_0x368f87[_0x3f13('0xb4')]['ids'],_[_0x3f13('0xb9')](_0x368f87[_0x3f13('0xb4')],[_0x3f13('0xb8'),'id'])||{})['spread'](function(){return db[_0x3f13('0x6a')]['findAll']({'attributes':['id',_0x3f13('0x2c'),_0x3f13('0x47')],'where':{'id':_0x368f87['body'][_0x3f13('0xb8')]}});})[_0x3f13('0x34')](function(_0x591561){socket[_0x3f13('0xc1')]('openchannelInteractionTags:save',{'id':Number(_0x368f87[_0x3f13('0xb0')]['id']),'tags':_0x591561||[]});return{'id':Number(_0x368f87[_0x3f13('0xb0')]['id']),'tags':_0x591561||[]};});}})[_0x3f13('0x34')](respondWithResult(_0x1affc2,null))[_0x3f13('0x35')](handleError(_0x1affc2,null));};exports[_0x3f13('0xc2')]=function(_0x1507a5,_0x14c389,_0x8197a){return db[_0x3f13('0x9b')][_0x3f13('0x65')]({'where':{'id':_0x1507a5[_0x3f13('0xb0')]['id']}})[_0x3f13('0x34')](handleEntityNotFound(_0x14c389,null))['then'](function(_0x3deb1d){if(_0x3deb1d){return _0x3deb1d[_0x3f13('0xc2')](_0x1507a5[_0x3f13('0x3c')][_0x3f13('0xb8')]);}})[_0x3f13('0x34')](respondWithStatusCode(_0x14c389,null))[_0x3f13('0x35')](handleError(_0x14c389,null));};exports['download']=function(_0x3c8684,_0xf73bba){var _0x194366=moment()[_0x3f13('0xc3')]()[_0x3f13('0x96')]();var _0x138903=path[_0x3f13('0xc4')](config[_0x3f13('0xc5')],_0x3f13('0xc6'),_0x3f13('0xc7'),_0x3f13('0xc8'));var _0x57d6df=path[_0x3f13('0xc4')](config['root'],'server','files','attachments');var _0x9ac79f=path[_0x3f13('0xc4')](_0x138903,_0x194366);var _0x2417bc=util[_0x3f13('0xbd')](_0x3f13('0xc9'),_0x3c8684[_0x3f13('0xb0')]['id'],_0x194366);var _0xfa3faa=path[_0x3f13('0xc4')](_0x138903,_0x2417bc);var _0x46bfcc=[];_0x46bfcc[_0x3f13('0x33')]({'model':db[_0x3f13('0x6b')],'as':'User','attributes':['id',_0x3f13('0x2f')],'raw':!![]});_0x46bfcc[_0x3f13('0x33')]({'model':db[_0x3f13('0xca')],'as':_0x3f13('0x5f'),'attributes':['id',_0x3f13('0xcb'),_0x3f13('0xcc')],'raw':!![]});if(_0x3c8684[_0x3f13('0x3c')][_0x3f13('0xcd')]){_0x46bfcc[_0x3f13('0x33')]({'model':db[_0x3f13('0xce')],'as':'Attachment','raw':!![]});}var _0x32db36=[{'model':db[_0x3f13('0xb7')],'as':_0x3f13('0xcf'),'attributes':['id','body','createdAt','direction',_0x3f13('0xd0'),_0x3f13('0x7b')],'include':_0x46bfcc}];_0x32db36[_0x3f13('0x33')]({'model':db[_0x3f13('0xd1')],'as':_0x3f13('0xd2'),'attributes':['id',_0x3f13('0x2c')]});_0x32db36[_0x3f13('0x33')]({'model':db[_0x3f13('0x6b')],'as':_0x3f13('0xd3'),'attributes':['id','fullname']});return db[_0x3f13('0x9b')][_0x3f13('0x65')]({'where':{'id':_0x3c8684[_0x3f13('0xb0')]['id']},'include':_0x32db36})['then'](handleEntityNotFound(_0xf73bba,null))['then'](function(_0x10cc6e){if(_0x10cc6e){var _0x43d479=_0x10cc6e[_0x3f13('0xd4')]({'plain':!![]});fs[_0x3f13('0xd5')](_0x9ac79f);var _0xbd9ad3={'channel':_0x3f13('0xd6'),'account':_0x43d479['Account'][_0x3f13('0x2c')],'agent':_0x43d479['Owner']?_0x43d479[_0x3f13('0xd3')][_0x3f13('0x2f')]:_0x3f13('0xd7'),'createdAt':moment(_0x43d479[_0x3f13('0x74')])['format']('YYYY-MM-DD\x20HH:mm:ss')[_0x3f13('0x96')](),'closedAt':_0x43d479[_0x3f13('0xd8')]?moment(_0x43d479[_0x3f13('0xd9')])[_0x3f13('0xbd')](_0x3f13('0xbe'))[_0x3f13('0x96')]():'','disposition':_0x43d479['disposition'],'visibility':_0x43d479['threadId']?_0x3f13('0xda'):_0x3f13('0xdb'),'messages':_(_0x43d479['Messages'])[_0x3f13('0x81')]({'secret':![]})[_0x3f13('0x2e')](function(_0x3165e1){if(_0x3165e1[_0x3f13('0xce')]){var _0x463051=path[_0x3f13('0xc4')](_0x57d6df,_0x3165e1[_0x3f13('0xce')][_0x3f13('0xdc')]);if(fs['existsSync'](_0x463051)){fs[_0x3f13('0xdd')](_0x463051)[_0x3f13('0xde')](fs[_0x3f13('0xdf')](path['join'](_0x9ac79f,_0x3165e1[_0x3f13('0xce')]['basename'])));}}return{'date':moment(_0x3165e1[_0x3f13('0x74')])['format'](_0x3f13('0xbe'))[_0x3f13('0x96')](),'sender':_0x3165e1[_0x3f13('0xe0')]===_0x3f13('0xe1')?_0x3165e1[_0x3f13('0x6b')]?_0x3165e1[_0x3f13('0x6b')][_0x3f13('0x2f')]:_0x3f13('0xe2'):_0x3165e1['Contact']['firstName']+(_0x3165e1[_0x3f13('0x5f')][_0x3f13('0xcc')]?'\x20'+_0x3165e1[_0x3f13('0x5f')]['lastName']:''),'body':_0x3165e1[_0x3f13('0xce')]?_0x3165e1[_0x3f13('0xce')]['basename']:_0x3165e1['body'],'direction':_0x3165e1[_0x3f13('0xe0')]===_0x3f13('0xe1')?'A':'C','secret':_0x3165e1[_0x3f13('0xd0')],'read':_0x3165e1[_0x3f13('0x7b')],'attachment':_0x3165e1[_0x3f13('0xce')]};})[_0x3f13('0x5c')]()};return ejs['renderFile'](path[_0x3f13('0xc4')](config[_0x3f13('0xc5')],_0x3f13('0xc6'),_0x3f13('0xe3'),_0x3f13('0xe4')),{'interaction':_0xbd9ad3})[_0x3f13('0x34')](function(_0xefdaf6){var _0x32f88a=path[_0x3f13('0xc4')](_0x9ac79f,'transcript-'+_0x43d479['id']+'-'+_0x194366+'.pdf');var _0x27610c={'path':_0x32f88a,'channel':'Openchannel','interactionId':_0x43d479['id']};return pdf['createPdfFromHTML'](_0xefdaf6,_0x27610c);})[_0x3f13('0x34')](function(){return new BPromise(function(_0x1bfbbf,_0x8147a4){zipdir(_0x9ac79f,{'saveTo':_0xfa3faa},function(_0x55febb,_0x38d5d3){if(_0x55febb)return _0x8147a4(_0x55febb);return _0x1bfbbf(_0x38d5d3);});})['then'](function(){return new BPromise(function(_0x1e59c7,_0x25046a){rimraf(_0x9ac79f,function(_0x378873){if(_0x378873)_0x25046a(_0x378873);return _0x1e59c7();});});})['then'](function(){return _0xf73bba[_0x3f13('0xe5')](_0xfa3faa,_0x2417bc,function(_0x11532b){if(_0x11532b){console['log'](_0x3f13('0xe6'),_0x11532b);}else{fs[_0x3f13('0xe7')](_0xfa3faa);}});});});}else{return _0xf73bba[_0x3f13('0x29')](0xc8);}})[_0x3f13('0x35')](handleError(_0xf73bba,null));};