Built motion from commit (unavailable).|2.5.29
[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 _0xc7fb=['Messages','direction','OpenchannelAccount','get','mkdirSync','Owner','closed','closedAt','disposition','Public','Private','basename','existsSync','createReadStream','YYYY-MM-DD\x20HH:mm:ss','out','System','secret','renderFile','downloadInteraction.html','transcript-','Openchannel','createPdfFromHTML','download','log','err','eml-format','zip-dir','fast-json-patch','request-promise','moment','bluebird','util','path','sox','to-csv','lodash','squel','crypto','desk.js','querystring','ioredis','../../components/auth/service','../../components/parsers/qs','../../components/parsers/advancedSearch','../../components/export/pdf','../../config/utils','../../config/license/util','../../mysqldb','redis','defaults','localhost','socket.io-emitter','./openchannelInteraction.socket','register','status','offset','limit','undefined','count','set','Content-Range','json','reject','update','then','destroy','end','sendStatus','stack','name','send','Users','map','fullname','TagIds','Tags','split','forEach','push','catch','OpenchannelInteraction','rawAttributes','fieldName','model','query','filters','intersection','attributes','fields','length','order','sort','pick','select','field','from','tools_tags','u.id','u.fullname','ui.OpenchannelInteractionId','user_has_openchannel_interactions','users','u.id\x20=\x20ui.UserId','openchannel_interactions','left_join','cm_contacts','c.id\x20=\x20i.ContactId\x20AND\x20c.deletedAt\x20IS\x20NULL','o.id\x20=\x20i.UserId','openchannel_accounts','me.OpenchannelInteractionId\x20=\x20i.id','openchannel_interaction_has_tags','it.OpenchannelInteractionId\x20=\x20i.id','t.id\x20=\x20it.TagId','expr','search','sqlOperator','conditions','find','value','having','Contact','operator','$substring','getFullTextValue','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?','UserId','body','keys','compact','isEmpty','OpenchannelAccountId','where','concat','createdAt','parse','$gte','$lte','i.createdAt\x20BETWEEN\x20?\x20AND\x20?','\x20IS\x20NULL','\x20=\x20?','read','`unreadMessages`\x20=\x200','`unreadMessages`\x20>\x200','tag','isArray','filter','isNumeric','i.id\x20LIKE\x20?','MATCH\x20(me.body)\x20AGAINST\x20(?\x20IN\x20BOOLEAN\x20MODE)','i.from\x20LIKE\x20?','CONCAT(c.firstName,\x20\x27\x20\x27,\x20c.lastName)\x20LIKE\x20?','i.id','Sequelize','QueryTypes','SELECT','SUM(me.read\x20=\x200\x20AND\x20me.direction\x20=\x20\x27in\x27)','unreadMessages','contactName','DESC','resolve','i.OpenchannelAccountId\x20IS\x20NOT\x20NULL','user','role','agent','i.OpenchannelAccountId\x20IS\x20NULL','i.OpenchannelAccountId\x20IN\x20?','sequelize','toString','keyBy','merge','a.key','i.*','c.firstName','Contact.firstName','Contact.lastName','Contact.email','Contact.phone','c.mobile','Contact.mobile','c.fax','Contact.fax','o.fullname','Owner.fullname','o.internal','GROUP_CONCAT(DISTINCT\x20t.id)','ui.OpenchannelInteractionId\x20IN\x20?','groupBy','OpenchannelInteractionId','all','rows','show','params','includeAll','include','create','addMessage','ids','omit','getMessages','findOne','nolimit','format','OpenchannelMessage','findAll','addTags','setTags','spread','color','emit','openchannelInteractionTags:save','removeTags','unix','join','root','server','tmp','files','CmContact','firstName','lastName','attachments','Attachment'];(function(_0x53ccf2,_0x13cb67){var _0x5a2964=function(_0x3b0514){while(--_0x3b0514){_0x53ccf2['push'](_0x53ccf2['shift']());}};_0x5a2964(++_0x13cb67);}(_0xc7fb,0x1ea));var _0xbc7f=function(_0x53be90,_0x3a4967){_0x53be90=_0x53be90-0x0;var _0x5590d6=_0xc7fb[_0x53be90];return _0x5590d6;};'use strict';var emlformat=require(_0xbc7f('0x0'));var rimraf=require('rimraf');var zipdir=require(_0xbc7f('0x1'));var jsonpatch=require(_0xbc7f('0x2'));var rp=require(_0xbc7f('0x3'));var moment=require(_0xbc7f('0x4'));var BPromise=require(_0xbc7f('0x5'));var Mustache=require('mustache');var util=require(_0xbc7f('0x6'));var path=require(_0xbc7f('0x7'));var sox=require(_0xbc7f('0x8'));var csv=require(_0xbc7f('0x9'));var ejs=require('ejs');var fs=require('fs');var fs_extra=require('fs-extra');var _=require(_0xbc7f('0xa'));var squel=require(_0xbc7f('0xb'));var crypto=require(_0xbc7f('0xc'));var jsforce=require('jsforce');var deskjs=require(_0xbc7f('0xd'));var toCsv=require(_0xbc7f('0x9'));var querystring=require(_0xbc7f('0xe'));var Papa=require('papaparse');var Redis=require(_0xbc7f('0xf'));var authService=require(_0xbc7f('0x10'));var qs=require(_0xbc7f('0x11'));var as=require(_0xbc7f('0x12'));var pdf=require(_0xbc7f('0x13'));var hardwareService=require('../../config/license/hardware');var logger=require('../../config/logger')('api');var utils=require(_0xbc7f('0x14'));var config=require('../../config/environment');var licenseUtil=require(_0xbc7f('0x15'));var db=require(_0xbc7f('0x16'))['db'];config[_0xbc7f('0x17')]=_[_0xbc7f('0x18')](config['redis'],{'host':_0xbc7f('0x19'),'port':0x18eb});var socket=require(_0xbc7f('0x1a'))(new Redis(config['redis']));require(_0xbc7f('0x1b'))[_0xbc7f('0x1c')](socket);function respondWithStatusCode(_0x256bb5,_0x442e17){_0x442e17=_0x442e17||0xcc;return function(_0x3904f1){if(_0x3904f1){return _0x256bb5['sendStatus'](_0x442e17);}return _0x256bb5[_0xbc7f('0x1d')](_0x442e17)['end']();};}function respondWithResult(_0x57356e,_0x7bc6c2){_0x7bc6c2=_0x7bc6c2||0xc8;return function(_0x22d25d){if(_0x22d25d){return _0x57356e['status'](_0x7bc6c2)['json'](_0x22d25d);}};}function respondWithFilteredResult(_0x1f8f8e,_0x2f03e6){return function(_0x2e3db6){if(_0x2e3db6){var _0x55429b=typeof _0x2f03e6[_0xbc7f('0x1e')]==='undefined'&&typeof _0x2f03e6[_0xbc7f('0x1f')]===_0xbc7f('0x20');var _0x25250a=_0x2e3db6[_0xbc7f('0x21')];var _0x4c4aac=_0x55429b?0x0:_0x2f03e6[_0xbc7f('0x1e')];var _0x387405=_0x55429b?_0x2e3db6[_0xbc7f('0x21')]:_0x2f03e6[_0xbc7f('0x1e')]+_0x2f03e6[_0xbc7f('0x1f')];var _0x164412;if(_0x387405>=_0x25250a){_0x387405=_0x25250a;_0x164412=0xc8;}else{_0x164412=0xce;}_0x1f8f8e[_0xbc7f('0x1d')](_0x164412);return _0x1f8f8e[_0xbc7f('0x22')](_0xbc7f('0x23'),_0x4c4aac+'-'+_0x387405+'/'+_0x25250a)[_0xbc7f('0x24')](_0x2e3db6);}return null;};}function patchUpdates(_0x5754d1){return function(_0x4a7774){try{jsonpatch['apply'](_0x4a7774,_0x5754d1,!![]);}catch(_0x53b0d7){return BPromise[_0xbc7f('0x25')](_0x53b0d7);}return _0x4a7774['save']();};}function saveUpdates(_0x4a0737,_0x274bc2){return function(_0x5a866c){if(_0x5a866c){return _0x5a866c[_0xbc7f('0x26')](_0x4a0737)[_0xbc7f('0x27')](function(_0x453ae5){return _0x453ae5;});}return null;};}function removeEntity(_0x2d4cc5,_0x2d8760){return function(_0x5a3ba8){if(_0x5a3ba8){return _0x5a3ba8[_0xbc7f('0x28')]()['then'](function(){_0x2d4cc5['status'](0xcc)[_0xbc7f('0x29')]();});}};}function handleEntityNotFound(_0x4e5c56,_0x431d6b){return function(_0x2f7c3d){if(!_0x2f7c3d){_0x4e5c56[_0xbc7f('0x2a')](0x194);}return _0x2f7c3d;};}function handleError(_0x205c75,_0x26ce31){_0x26ce31=_0x26ce31||0x1f4;return function(_0x861a20){logger['error'](_0x861a20[_0xbc7f('0x2b')]);if(_0x861a20[_0xbc7f('0x2c')]){delete _0x861a20['name'];}_0x205c75[_0xbc7f('0x1d')](_0x26ce31)[_0xbc7f('0x2d')](_0x861a20);};}function getInteractionUsers(_0x231c59,_0x1ada85){return new BPromise(function(_0x2a1fed,_0x129488){try{if(_0x1ada85[_0x231c59['id']]){_0x231c59[_0xbc7f('0x2e')]=_[_0xbc7f('0x2f')](_0x1ada85[_0x231c59['id']],function(_0x465e40){return{'id':_0x465e40['id'],'fullname':_0x465e40[_0xbc7f('0x30')]};});}}catch(_0x214ae6){_0x129488(_0x214ae6);}_0x2a1fed(_0x231c59);});}function getInteractionTags(_0x5a8197,_0x4b273b){return new BPromise(function(_0x55ab3a,_0xbb6948){try{if(_0x5a8197[_0xbc7f('0x31')]){_0x5a8197[_0xbc7f('0x32')]=[];_0x5a8197['TagIds'][_0xbc7f('0x33')](',')[_0xbc7f('0x34')](function(_0x22398b){_0x5a8197['Tags'][_0xbc7f('0x35')](_0x4b273b[_0x22398b]);});}delete _0x5a8197[_0xbc7f('0x31')];}catch(_0xe69ba){_0xbb6948(_0xe69ba);}_0x55ab3a(_0x5a8197);});}function updateOpenchannelInteraction(_0x15804e,_0x1da687,_0x27314e){return new BPromise(function(_0x402402,_0x32db18){return getInteractionUsers(_0x15804e,_0x27314e)[_0xbc7f('0x27')](function(_0x56a671){return getInteractionTags(_0x56a671,_0x1da687);})[_0xbc7f('0x27')](function(_0x2da2e8){_0x402402(_0x2da2e8);})[_0xbc7f('0x36')](function(_0x57c1b7){_0x32db18(_0x57c1b7);});});}exports['index']=function(_0x263a68,_0x1bff51){var _0x21ec17={},_0x58cc1d={},_0x75a4b2={'count':0x0,'rows':[]};var _0x1aac9c=_[_0xbc7f('0x2f')](db[_0xbc7f('0x37')][_0xbc7f('0x38')],function(_0xe577e){return{'name':_0xe577e[_0xbc7f('0x39')],'type':_0xe577e['type']['key']};});_0x58cc1d[_0xbc7f('0x3a')]=_[_0xbc7f('0x2f')](_0x1aac9c,_0xbc7f('0x2c'));_0x58cc1d['query']=_['keys'](_0x263a68[_0xbc7f('0x3b')]);_0x58cc1d[_0xbc7f('0x3c')]=_[_0xbc7f('0x3d')](_0x58cc1d[_0xbc7f('0x3a')],_0x58cc1d['query']);_0x21ec17[_0xbc7f('0x3e')]=_['intersection'](_0x58cc1d[_0xbc7f('0x3a')],qs[_0xbc7f('0x3f')](_0x263a68[_0xbc7f('0x3b')]['fields']));_0x21ec17['attributes']=_0x21ec17['attributes'][_0xbc7f('0x40')]?_0x21ec17[_0xbc7f('0x3e')]:_0x58cc1d[_0xbc7f('0x3a')];if(!_0x263a68[_0xbc7f('0x3b')]['hasOwnProperty']('nolimit')){_0x21ec17[_0xbc7f('0x1f')]=qs[_0xbc7f('0x1f')](_0x263a68[_0xbc7f('0x3b')][_0xbc7f('0x1f')]);_0x21ec17['offset']=qs[_0xbc7f('0x1e')](_0x263a68[_0xbc7f('0x3b')][_0xbc7f('0x1e')]);}_0x21ec17[_0xbc7f('0x41')]=qs[_0xbc7f('0x42')](_0x263a68['query'][_0xbc7f('0x42')]);_0x21ec17['where']=qs[_0xbc7f('0x3c')](_[_0xbc7f('0x43')](_0x263a68['query'],_0x58cc1d['filters']),_0x1aac9c);var _0x51da5f=[];var _0x21cf7f=squel[_0xbc7f('0x44')]()['field']('id')[_0xbc7f('0x45')](_0xbc7f('0x2c'))[_0xbc7f('0x45')]('color')[_0xbc7f('0x46')](_0xbc7f('0x47'));var _0xefbe4e=squel[_0xbc7f('0x44')]()[_0xbc7f('0x45')](_0xbc7f('0x48'))[_0xbc7f('0x45')](_0xbc7f('0x49'))[_0xbc7f('0x45')](_0xbc7f('0x4a'))[_0xbc7f('0x46')](_0xbc7f('0x4b'),'ui')['left_join'](_0xbc7f('0x4c'),'u',_0xbc7f('0x4d'));var _0x606db1=squel['select']()[_0xbc7f('0x46')](_0xbc7f('0x4e'),'i')[_0xbc7f('0x4f')](_0xbc7f('0x50'),'c',_0xbc7f('0x51'))['left_join'](_0xbc7f('0x4c'),'o',_0xbc7f('0x52'))[_0xbc7f('0x4f')](_0xbc7f('0x53'),'a','a.id\x20=\x20i.OpenchannelAccountId')[_0xbc7f('0x4f')]('openchannel_messages','me',_0xbc7f('0x54'))['left_join'](_0xbc7f('0x55'),'it',_0xbc7f('0x56'))[_0xbc7f('0x4f')]('tools_tags','t',_0xbc7f('0x57'));var _0x20f8d7=squel[_0xbc7f('0x58')]();var _0x525e28=[];var _0xe09dcd=squel['expr']();var _0x3cd374;if(_0x263a68[_0xbc7f('0x3b')][_0xbc7f('0x59')]){_0x3cd374=as['parseSearch'](_0x263a68['query'][_0xbc7f('0x59')]);var _0x3d8623=_0x3cd374[_0xbc7f('0x5a')];for(var _0x33763c=0x0;_0x33763c<_0x3cd374[_0xbc7f('0x5b')][_0xbc7f('0x40')];_0x33763c++){var _0x33b864=_0x3cd374['conditions'][_0x33763c];var _0x35254c='i';var _0x32c674=_[_0xbc7f('0x5c')](_0x1aac9c,[_0xbc7f('0x2c'),_0x33b864[_0xbc7f('0x45')]]);if(!_0x32c674){switch(_0x33b864[_0xbc7f('0x45')]){case'unreadMessages':if(_0x33b864[_0xbc7f('0x5d')]==0x1){_0x606db1['having']('`'+_0x33b864['field']+'`\x20=\x200');}else{_0x606db1[_0xbc7f('0x5e')]('`'+_0x33b864['field']+'`\x20>\x200');}break;case _0xbc7f('0x5f'):if(_0x33b864[_0xbc7f('0x60')]===_0xbc7f('0x61')){if(_0x33b864[_0xbc7f('0x5d')][_0xbc7f('0x33')]('\x20')[_0xbc7f('0x40')]>0x1){_0x20f8d7[_0x3d8623]('MATCH\x20(c.firstName,\x20c.lastName,\x20c.phone,\x20c.mobile,\x20c.fax,\x20c.email)\x20AGAINST\x20(?\x20IN\x20BOOLEAN\x20MODE)',qs[_0xbc7f('0x62')](_0x33b864[_0xbc7f('0x5d')]),null);}else{var _0x24e703='%'+_0x33b864[_0xbc7f('0x5d')]+'%';_0x20f8d7[_0x3d8623]('c.firstName\x20LIKE\x20?\x20OR\x20c.lastName\x20LIKE\x20?\x20OR\x20c.email\x20LIKE\x20?',_0x24e703,_0x24e703,_0x24e703);}}else{_0x32c674=_[_0xbc7f('0x5c')](_0x1aac9c,[_0xbc7f('0x2c'),_0xbc7f('0x63')]);_0x33b864[_0xbc7f('0x45')]=_0xbc7f('0x63');_0x3a1f41=as[_0xbc7f('0x64')](_0x35254c,_0x32c674[_0xbc7f('0x65')],_0x33b864);_0x20f8d7[_0x3d8623](_0x3a1f41[_0xbc7f('0x66')],_0x3a1f41[_0xbc7f('0x5d')][_0xbc7f('0x67')],_0x3a1f41[_0xbc7f('0x5d')]['end']);}break;case _0xbc7f('0x68'):var _0x29eeb4=_0x33b864['value']['split'](',')['map'](function(_0x4cd077){return Number(_0x4cd077);});_0x29eeb4[_0xbc7f('0x34')](function(_0x2031ff){_0xe09dcd['or'](_0xbc7f('0x69'),'%,'+_0x2031ff+',%');});_0x525e28=_[_0xbc7f('0x6a')](_0x525e28,_0x29eeb4);break;case _0xbc7f('0x6b'):if(_0x33b864[_0xbc7f('0x60')]==='$substring'){_0x20f8d7[_0x3d8623](_0xbc7f('0x6c'),'%'+_0x33b864['value']+'%',null);}else{_0x32c674=_[_0xbc7f('0x5c')](_0x1aac9c,[_0xbc7f('0x2c'),_0xbc7f('0x6d')]);_0x33b864[_0xbc7f('0x45')]='UserId';_0x3a1f41=as['buildExpression'](_0x35254c,_0x32c674[_0xbc7f('0x65')],_0x33b864);_0x20f8d7[_0x3d8623](_0x3a1f41[_0xbc7f('0x66')],_0x3a1f41[_0xbc7f('0x5d')][_0xbc7f('0x67')],_0x3a1f41[_0xbc7f('0x5d')][_0xbc7f('0x29')]);}break;case _0xbc7f('0x6e'):_0x3a1f41=as['buildExpression']('me',null,_0x33b864);_0x20f8d7[_0x3d8623](_0x3a1f41[_0xbc7f('0x66')],_0x3a1f41['value'][_0xbc7f('0x67')],_0x3a1f41[_0xbc7f('0x5d')][_0xbc7f('0x29')]);break;}}else{var _0x3a1f41=as[_0xbc7f('0x64')](_0x35254c,_0x32c674[_0xbc7f('0x65')],_0x33b864);_0x20f8d7[_0x3d8623](_0x3a1f41[_0xbc7f('0x66')],_0x3a1f41[_0xbc7f('0x5d')][_0xbc7f('0x67')],_0x3a1f41[_0xbc7f('0x5d')]['end']);}}}else{var _0x24060d=_(_0x263a68['query'])[_0xbc7f('0x6f')]()['map'](function(_0x27796b){return _['some'](_0x1aac9c,['name',_0x27796b])?_0x27796b:undefined;})[_0xbc7f('0x70')]()[_0xbc7f('0x5d')]();if(!_[_0xbc7f('0x71')](_0x24060d)){_0x24060d[_0xbc7f('0x34')](function(_0x31fb3b){if(_0x31fb3b===_0xbc7f('0x72')){_0x606db1[_0xbc7f('0x73')]('i.OpenchannelAccountId\x20IN\x20?',[][_0xbc7f('0x74')](_0x263a68['query'][_0x31fb3b]));}else if(_0x31fb3b===_0xbc7f('0x75')){var _0x24186e=JSON[_0xbc7f('0x76')](_0x263a68[_0xbc7f('0x3b')][_0x31fb3b])[_0xbc7f('0x77')];var _0x197143=JSON[_0xbc7f('0x76')](_0x263a68['query'][_0x31fb3b])[_0xbc7f('0x78')];_0x606db1[_0xbc7f('0x73')](_0xbc7f('0x79'),_0x24186e,_0x197143);}else{if(_0x263a68['query'][_0x31fb3b]==='null'){_0x606db1[_0xbc7f('0x73')]('i.'+_0x31fb3b+_0xbc7f('0x7a'));}else{_0x606db1[_0xbc7f('0x73')]('i.'+_0x31fb3b+_0xbc7f('0x7b'),_0x263a68[_0xbc7f('0x3b')][_0x31fb3b]);}}});}if(_0x263a68[_0xbc7f('0x3b')][_0xbc7f('0x7c')]){if(_0x263a68[_0xbc7f('0x3b')][_0xbc7f('0x7c')]==0x1){_0x606db1['having'](_0xbc7f('0x7d'));}else{_0x606db1['having'](_0xbc7f('0x7e'));}}if(_0x263a68['query'][_0xbc7f('0x7f')]){_0x525e28=_[_0xbc7f('0x80')](_0x263a68[_0xbc7f('0x3b')][_0xbc7f('0x7f')])?_0x263a68[_0xbc7f('0x3b')][_0xbc7f('0x7f')]:new Array(_0x263a68[_0xbc7f('0x3b')][_0xbc7f('0x7f')]);_0x525e28[_0xbc7f('0x34')](function(_0x5c218b){_0xe09dcd['or'](_0xbc7f('0x69'),'%,'+_0x5c218b+',%');});}if(_0x263a68[_0xbc7f('0x3b')][_0xbc7f('0x81')]){var _0x3598a4=_0x263a68[_0xbc7f('0x3b')][_0xbc7f('0x81')]['replace']('\x5c','\x5c\x5c')['replace'](/'/g,'\x27\x27');if(qs[_0xbc7f('0x82')](_0x3598a4)){_0x20f8d7['or'](_0xbc7f('0x83'),_0x3598a4+'%');}else{_0x20f8d7['or'](_0xbc7f('0x84'),qs['getFullTextValue'](_0x3598a4))['or'](_0xbc7f('0x85'),_0x3598a4+'%')['or'](_0xbc7f('0x86'),_0x3598a4+'%');}}}_0x606db1[_0xbc7f('0x73')](_0x20f8d7);_0x606db1['group'](_0xbc7f('0x87'));var _0xdd2fb8={'type':db[_0xbc7f('0x88')][_0xbc7f('0x89')][_0xbc7f('0x8a')],'raw':!![]};var _0x421f21=_0x606db1['clone']();_0x421f21[_0xbc7f('0x45')]('i.id');_0x421f21[_0xbc7f('0x45')](_0xbc7f('0x8b'),_0xbc7f('0x8c'));if(_0x21ec17[_0xbc7f('0x41')]){_0x21ec17[_0xbc7f('0x41')][_0xbc7f('0x34')](function(_0x3e7825){var _0x1cd0d7=_0x3e7825[0x0]===_0xbc7f('0x8d')?_0x3e7825[0x0]:'i.'+_0x3e7825[0x0];_0x606db1['order'](_0x1cd0d7,_0x3e7825[0x1]===_0xbc7f('0x8e')?![]:!![]);});}if(!_[_0xbc7f('0x71')](_0x525e28)){_0x606db1['having'](_0xe09dcd);_0x421f21[_0xbc7f('0x73')]('t.id\x20IN\x20?',_0x525e28);}BPromise[_0xbc7f('0x8f')]()[_0xbc7f('0x27')](function(){if(!_0x3cd374){if(_0x263a68[_0xbc7f('0x3b')][_0xbc7f('0x72')])return;_0x421f21[_0xbc7f('0x73')](_0xbc7f('0x90'));_0x606db1[_0xbc7f('0x73')]('i.OpenchannelAccountId\x20IS\x20NOT\x20NULL');return;}if(_0x263a68[_0xbc7f('0x91')][_0xbc7f('0x92')]!==_0xbc7f('0x93'))return;return _0x263a68[_0xbc7f('0x91')]['getOpenchannelAccounts']({'attributes':['id'],'raw':!![]})[_0xbc7f('0x27')](function(_0xd1bad7){if(_[_0xbc7f('0x71')](_0xd1bad7)){_0x421f21[_0xbc7f('0x73')](_0xbc7f('0x94'));_0x606db1[_0xbc7f('0x73')]('i.OpenchannelAccountId\x20IS\x20NULL');}else{_0x421f21[_0xbc7f('0x73')](_0xbc7f('0x95'),_[_0xbc7f('0x2f')](_0xd1bad7,'id'));_0x606db1[_0xbc7f('0x73')](_0xbc7f('0x95'),_[_0xbc7f('0x2f')](_0xd1bad7,'id'));}});})[_0xbc7f('0x27')](function(){if(_0x263a68['user']['role']==='agent'&&!_0x3cd374&&!_0x263a68['query']['OpenchannelAccountId'])return[];return db[_0xbc7f('0x96')][_0xbc7f('0x3b')](_0x421f21['toString'](),_0xdd2fb8);})['then'](function(_0x60f7a6){_0x75a4b2[_0xbc7f('0x21')]=_0x60f7a6['length'];if(_0x75a4b2[_0xbc7f('0x21')]===0x0)return[];return db[_0xbc7f('0x96')][_0xbc7f('0x3b')](_0x21cf7f[_0xbc7f('0x97')](),_0xdd2fb8)[_0xbc7f('0x27')](function(_0x2ad232){_0x51da5f=_[_0xbc7f('0x98')](_0x2ad232,'id');_0xdd2fb8=_[_0xbc7f('0x99')](_0xdd2fb8,{'model':db[_0xbc7f('0x37')],'mapToModel':!![],'raw':!![],'nest':!![]});if(_0x263a68[_0xbc7f('0x3b')][_0xbc7f('0x3f')]){_0x21ec17[_0xbc7f('0x3e')]['forEach'](function(_0x3c5715){_0x606db1[_0xbc7f('0x45')]('i.'+_0x3c5715);});}else{_0x606db1['field'](_0xbc7f('0x8b'),_0xbc7f('0x8c'));_0x606db1[_0xbc7f('0x45')](_0xbc7f('0x9a'),'Account.key');_0x606db1[_0xbc7f('0x45')]('a.mandatoryDisposition','Account.mandatoryDisposition');_0x606db1[_0xbc7f('0x45')]('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',_0xbc7f('0x8d'));_0x606db1[_0xbc7f('0x45')](_0xbc7f('0x9b'));_0x606db1[_0xbc7f('0x45')](_0xbc7f('0x9c'),_0xbc7f('0x9d'));_0x606db1[_0xbc7f('0x45')]('c.lastName',_0xbc7f('0x9e'));_0x606db1[_0xbc7f('0x45')]('c.email',_0xbc7f('0x9f'));_0x606db1[_0xbc7f('0x45')]('c.phone',_0xbc7f('0xa0'));_0x606db1[_0xbc7f('0x45')](_0xbc7f('0xa1'),_0xbc7f('0xa2'));_0x606db1[_0xbc7f('0x45')](_0xbc7f('0xa3'),_0xbc7f('0xa4'));_0x606db1[_0xbc7f('0x45')](_0xbc7f('0xa5'),_0xbc7f('0xa6'));_0x606db1[_0xbc7f('0x45')](_0xbc7f('0xa7'),'Owner.internal');_0x606db1[_0xbc7f('0x45')](_0xbc7f('0xa8'),_0xbc7f('0x31'));}if(_0x21ec17['limit'])_0x606db1[_0xbc7f('0x1f')](_0x21ec17['limit']);if(_0x21ec17[_0xbc7f('0x1e')])_0x606db1[_0xbc7f('0x1e')](_0x21ec17[_0xbc7f('0x1e')]);return db[_0xbc7f('0x96')][_0xbc7f('0x3b')](_0x606db1['toString'](),_0xdd2fb8);})[_0xbc7f('0x27')](function(_0x195b10){if(_0x195b10[_0xbc7f('0x40')]>0x0)_0xefbe4e[_0xbc7f('0x73')](_0xbc7f('0xa9'),_[_0xbc7f('0x2f')](_0x195b10,'id'));return db['sequelize'][_0xbc7f('0x3b')](_0xefbe4e['toString'](),_0xdd2fb8)[_0xbc7f('0x27')](function(_0x4504c1){var _0x9b0919=_[_0xbc7f('0xaa')](_0x4504c1,_0xbc7f('0xab'));var _0x28e6eb=[];_0x195b10[_0xbc7f('0x34')](function(_0x72310d){_0x28e6eb[_0xbc7f('0x35')](updateOpenchannelInteraction(_0x72310d,_0x51da5f,_0x9b0919));});return BPromise[_0xbc7f('0xac')](_0x28e6eb);});});})['then'](function(_0x55e13b){_0x75a4b2[_0xbc7f('0xad')]=_0x55e13b;return _0x75a4b2;})[_0xbc7f('0x27')](respondWithFilteredResult(_0x1bff51,_0x21ec17))[_0xbc7f('0x36')](handleError(_0x1bff51,null));};exports[_0xbc7f('0xae')]=function(_0x4d59eb,_0x51e260){var _0x1ced40={};if(_0x4d59eb['query'][_0xbc7f('0x65')]&&_0x4d59eb[_0xbc7f('0x3b')][_0xbc7f('0x65')]===_0xbc7f('0x46')){_0x1ced40['from']=_0x4d59eb[_0xbc7f('0xaf')]['id'];}else{_0x1ced40['id']=_0x4d59eb['params']['id'];}var _0x3c41ce={'raw':![],'where':_0x1ced40},_0x5a31e3={};_0x5a31e3[_0xbc7f('0x3a')]=_[_0xbc7f('0x6f')](db[_0xbc7f('0x37')]['rawAttributes']);_0x5a31e3[_0xbc7f('0x3b')]=_[_0xbc7f('0x6f')](_0x4d59eb[_0xbc7f('0x3b')]);_0x5a31e3['filters']=_[_0xbc7f('0x3d')](_0x5a31e3[_0xbc7f('0x3a')],_0x5a31e3[_0xbc7f('0x3b')]);_0x3c41ce[_0xbc7f('0x3e')]=_[_0xbc7f('0x3d')](_0x5a31e3[_0xbc7f('0x3a')],qs[_0xbc7f('0x3f')](_0x4d59eb[_0xbc7f('0x3b')][_0xbc7f('0x3f')]));_0x3c41ce[_0xbc7f('0x3e')]=_0x3c41ce[_0xbc7f('0x3e')][_0xbc7f('0x40')]?_0x3c41ce['attributes']:_0x5a31e3['model'];if(_0x4d59eb[_0xbc7f('0x3b')][_0xbc7f('0xb0')]){_0x3c41ce[_0xbc7f('0xb1')]=[{'all':!![]}];}_0x3c41ce=_['merge']({},_0x3c41ce,_0x4d59eb['options']);return db[_0xbc7f('0x37')][_0xbc7f('0x5c')](_0x3c41ce)['then'](handleEntityNotFound(_0x51e260,null))[_0xbc7f('0x27')](respondWithResult(_0x51e260,null))[_0xbc7f('0x36')](handleError(_0x51e260,null));};exports['create']=function(_0x2fd1c0,_0x51f832){return db['OpenchannelInteraction'][_0xbc7f('0xb2')](_0x2fd1c0[_0xbc7f('0x6e')],{})[_0xbc7f('0x27')](respondWithResult(_0x51f832,0xc9))[_0xbc7f('0x36')](handleError(_0x51f832,null));};exports[_0xbc7f('0x26')]=function(_0x41fad3,_0x2eebeb){if(_0x41fad3[_0xbc7f('0x6e')]['id']){delete _0x41fad3['body']['id'];}return db[_0xbc7f('0x37')][_0xbc7f('0x5c')]({'where':{'id':_0x41fad3[_0xbc7f('0xaf')]['id']}})[_0xbc7f('0x27')](handleEntityNotFound(_0x2eebeb,null))['then'](saveUpdates(_0x41fad3[_0xbc7f('0x6e')],null))[_0xbc7f('0x27')](respondWithResult(_0x2eebeb,null))[_0xbc7f('0x36')](handleError(_0x2eebeb,null));};exports['destroy']=function(_0x17132b,_0x45b838){return db[_0xbc7f('0x37')][_0xbc7f('0x5c')]({'where':{'id':_0x17132b[_0xbc7f('0xaf')]['id']}})[_0xbc7f('0x27')](handleEntityNotFound(_0x45b838,null))[_0xbc7f('0x27')](removeEntity(_0x45b838,null))[_0xbc7f('0x36')](handleError(_0x45b838,null));};exports['describe']=function(_0x51b93e,_0x445149){return db[_0xbc7f('0x37')]['describe']()[_0xbc7f('0x27')](respondWithResult(_0x445149,null))[_0xbc7f('0x36')](handleError(_0x445149,null));};exports[_0xbc7f('0xb3')]=function(_0x37c864,_0x178f8e,_0x39c077){return db['OpenchannelMessage']['find']({'where':{'id':_0x37c864['params']['id']}})[_0xbc7f('0x27')](handleEntityNotFound(_0x178f8e,null))['then'](function(_0x308a47){if(_0x308a47){return _0x308a47['addMessage'](_0x37c864[_0xbc7f('0x6e')][_0xbc7f('0xb4')],_[_0xbc7f('0xb5')](_0x37c864['body'],[_0xbc7f('0xb4'),'id'])||{});}})['then'](respondWithResult(_0x178f8e,null))[_0xbc7f('0x36')](handleError(_0x178f8e,null));};exports[_0xbc7f('0xb6')]=function(_0x3865cf,_0x339faa,_0x34d295){var _0x2f3f80={'raw':![],'where':{}};var _0x1517ab={};var _0x5183b7={'count':0x0,'rows':[]};return db['OpenchannelInteraction'][_0xbc7f('0xb7')]({'where':{'id':_0x3865cf[_0xbc7f('0xaf')]['id']}})['then'](handleEntityNotFound(_0x339faa,null))[_0xbc7f('0x27')](function(_0x1f217d){if(_0x1f217d){_0x1517ab[_0xbc7f('0x3a')]=_[_0xbc7f('0x6f')](db['OpenchannelMessage'][_0xbc7f('0x38')]);_0x1517ab['query']=_[_0xbc7f('0x6f')](_0x3865cf[_0xbc7f('0x3b')]);_0x1517ab['filters']=_[_0xbc7f('0x3d')](_0x1517ab[_0xbc7f('0x3a')],_0x1517ab[_0xbc7f('0x3b')]);_0x2f3f80[_0xbc7f('0x3e')]=_[_0xbc7f('0x3d')](_0x1517ab[_0xbc7f('0x3a')],qs[_0xbc7f('0x3f')](_0x3865cf[_0xbc7f('0x3b')]['fields']));_0x2f3f80[_0xbc7f('0x3e')]=_0x2f3f80[_0xbc7f('0x3e')]['length']?_0x2f3f80[_0xbc7f('0x3e')]:_0x1517ab[_0xbc7f('0x3a')];if(!_0x3865cf[_0xbc7f('0x3b')]['hasOwnProperty'](_0xbc7f('0xb8'))){_0x2f3f80[_0xbc7f('0x1f')]=qs[_0xbc7f('0x1f')](_0x3865cf[_0xbc7f('0x3b')]['limit']);_0x2f3f80['offset']=qs['offset'](_0x3865cf[_0xbc7f('0x3b')][_0xbc7f('0x1e')]);}_0x2f3f80[_0xbc7f('0x41')]=qs[_0xbc7f('0x42')](_0x3865cf['query']['sort']);_0x2f3f80['where']=qs[_0xbc7f('0x3c')](_['pick'](_0x3865cf[_0xbc7f('0x3b')],_0x1517ab[_0xbc7f('0x3c')]));_0x2f3f80[_0xbc7f('0x73')][_0xbc7f('0xab')]=_0x1f217d['id'];if(_0x3865cf[_0xbc7f('0x3b')]['filter']){_0x2f3f80[_0xbc7f('0x73')]=_['merge'](_0x2f3f80[_0xbc7f('0x73')],{'$or':_[_0xbc7f('0x2f')](_0x2f3f80[_0xbc7f('0x3e')],function(_0x1bd122){var _0x57eea8={};_0x57eea8[_0x1bd122]={'$like':'%'+_0x3865cf[_0xbc7f('0x3b')][_0xbc7f('0x81')]+'%'};return _0x57eea8;})});}if(_0x3865cf[_0xbc7f('0x3b')]['$gte']){var _0x2be2c9=_0x3865cf[_0xbc7f('0x3b')][_0xbc7f('0x77')][_0xbc7f('0x33')](',');var _0xd5b515={};_0xd5b515[_0x2be2c9[0x0]]={'$gte':moment(_0x2be2c9[0x1])[_0xbc7f('0xb9')]('YYYY-MM-DD\x20HH:mm:ss')};_0x2f3f80['where']=_[_0xbc7f('0x99')](_0x2f3f80['where'],_0xd5b515);}_0x2f3f80=_[_0xbc7f('0x99')]({},_0x2f3f80,_0x3865cf['options']);return db[_0xbc7f('0xba')][_0xbc7f('0x21')]({'where':_0x2f3f80[_0xbc7f('0x73')]})[_0xbc7f('0x27')](function(_0x99ab5e){_0x5183b7[_0xbc7f('0x21')]=_0x99ab5e;if(_0x3865cf[_0xbc7f('0x3b')][_0xbc7f('0xb0')]){_0x2f3f80[_0xbc7f('0xb1')]=[{'all':!![]}];}return db['OpenchannelMessage'][_0xbc7f('0xbb')](_0x2f3f80);})[_0xbc7f('0x27')](function(_0x257336){_0x5183b7[_0xbc7f('0xad')]=_0x257336;return _0x5183b7;});}})[_0xbc7f('0x27')](respondWithFilteredResult(_0x339faa,_0x2f3f80))[_0xbc7f('0x36')](handleError(_0x339faa,null));};exports[_0xbc7f('0xbc')]=function(_0x1defc6,_0x274f5c,_0x495864){return db[_0xbc7f('0x37')]['find']({'where':{'id':_0x1defc6[_0xbc7f('0xaf')]['id']}})['then'](handleEntityNotFound(_0x274f5c,null))['then'](function(_0x3942c9){if(_0x3942c9){return _0x3942c9[_0xbc7f('0xbd')](_0x1defc6['body'][_0xbc7f('0xb4')],_[_0xbc7f('0xb5')](_0x1defc6[_0xbc7f('0x6e')],[_0xbc7f('0xb4'),'id'])||{})[_0xbc7f('0xbe')](function(){return db[_0xbc7f('0x68')][_0xbc7f('0xbb')]({'attributes':['id',_0xbc7f('0x2c'),_0xbc7f('0xbf')],'where':{'id':_0x1defc6[_0xbc7f('0x6e')][_0xbc7f('0xb4')]}});})[_0xbc7f('0x27')](function(_0x2a54f6){socket[_0xbc7f('0xc0')](_0xbc7f('0xc1'),{'id':Number(_0x1defc6['params']['id']),'tags':_0x2a54f6||[]});return{'id':Number(_0x1defc6[_0xbc7f('0xaf')]['id']),'tags':_0x2a54f6||[]};});}})[_0xbc7f('0x27')](respondWithResult(_0x274f5c,null))[_0xbc7f('0x36')](handleError(_0x274f5c,null));};exports[_0xbc7f('0xc2')]=function(_0x2691fe,_0x41eedf,_0x47eb1e){return db[_0xbc7f('0x37')][_0xbc7f('0x5c')]({'where':{'id':_0x2691fe['params']['id']}})['then'](handleEntityNotFound(_0x41eedf,null))['then'](function(_0x52b01f){if(_0x52b01f){return _0x52b01f['removeTags'](_0x2691fe[_0xbc7f('0x3b')][_0xbc7f('0xb4')]);}})[_0xbc7f('0x27')](respondWithStatusCode(_0x41eedf,null))[_0xbc7f('0x36')](handleError(_0x41eedf,null));};exports['download']=function(_0x47fc11,_0x526ff5){var _0x461652=moment()[_0xbc7f('0xc3')]()[_0xbc7f('0x97')]();var _0x2f1c84=path[_0xbc7f('0xc4')](config[_0xbc7f('0xc5')],_0xbc7f('0xc6'),'files',_0xbc7f('0xc7'));var _0x23f699=path[_0xbc7f('0xc4')](config[_0xbc7f('0xc5')],'server',_0xbc7f('0xc8'),'attachments');var _0x53a9b8=path[_0xbc7f('0xc4')](_0x2f1c84,_0x461652);var _0x5ab837=util[_0xbc7f('0xb9')]('transcript-%d-%s.zip',_0x47fc11['params']['id'],_0x461652);var _0x22e7f=path[_0xbc7f('0xc4')](_0x2f1c84,_0x5ab837);var _0xd0b85f=[];_0xd0b85f[_0xbc7f('0x35')]({'model':db[_0xbc7f('0x6b')],'as':'User','attributes':['id',_0xbc7f('0x30')],'raw':!![]});_0xd0b85f['push']({'model':db[_0xbc7f('0xc9')],'as':_0xbc7f('0x5f'),'attributes':['id',_0xbc7f('0xca'),_0xbc7f('0xcb')],'raw':!![]});if(_0x47fc11[_0xbc7f('0x3b')][_0xbc7f('0xcc')]){_0xd0b85f[_0xbc7f('0x35')]({'model':db[_0xbc7f('0xcd')],'as':_0xbc7f('0xcd'),'raw':!![]});}var _0x5709ab=[{'model':db[_0xbc7f('0xba')],'as':_0xbc7f('0xce'),'attributes':['id',_0xbc7f('0x6e'),_0xbc7f('0x75'),_0xbc7f('0xcf'),'secret',_0xbc7f('0x7c')],'include':_0xd0b85f}];_0x5709ab[_0xbc7f('0x35')]({'model':db[_0xbc7f('0xd0')],'as':'Account','attributes':['id',_0xbc7f('0x2c')]});_0x5709ab[_0xbc7f('0x35')]({'model':db[_0xbc7f('0x6b')],'as':'Owner','attributes':['id','fullname']});return db[_0xbc7f('0x37')][_0xbc7f('0x5c')]({'where':{'id':_0x47fc11[_0xbc7f('0xaf')]['id']},'include':_0x5709ab})[_0xbc7f('0x27')](handleEntityNotFound(_0x526ff5,null))[_0xbc7f('0x27')](function(_0x10cb0f){if(_0x10cb0f){var _0x5a6ff8=_0x10cb0f[_0xbc7f('0xd1')]({'plain':!![]});fs[_0xbc7f('0xd2')](_0x53a9b8);var _0x472c28={'channel':'Openchannel','account':_0x5a6ff8['Account'][_0xbc7f('0x2c')],'agent':_0x5a6ff8['Owner']?_0x5a6ff8[_0xbc7f('0xd3')]['fullname']:'N.A.','createdAt':moment(_0x5a6ff8[_0xbc7f('0x75')])[_0xbc7f('0xb9')]('YYYY-MM-DD\x20HH:mm:ss')[_0xbc7f('0x97')](),'closedAt':_0x5a6ff8[_0xbc7f('0xd4')]?moment(_0x5a6ff8[_0xbc7f('0xd5')])['format']('YYYY-MM-DD\x20HH:mm:ss')['toString']():'','disposition':_0x5a6ff8[_0xbc7f('0xd6')],'visibility':_0x5a6ff8['threadId']?_0xbc7f('0xd7'):_0xbc7f('0xd8'),'messages':_(_0x5a6ff8[_0xbc7f('0xce')])[_0xbc7f('0x81')]({'secret':![]})[_0xbc7f('0x2f')](function(_0x3b6491){if(_0x3b6491['Attachment']){var _0x204a34=path[_0xbc7f('0xc4')](_0x23f699,_0x3b6491[_0xbc7f('0xcd')][_0xbc7f('0xd9')]);if(fs[_0xbc7f('0xda')](_0x204a34)){fs[_0xbc7f('0xdb')](_0x204a34)['pipe'](fs['createWriteStream'](path[_0xbc7f('0xc4')](_0x53a9b8,_0x3b6491['Attachment'][_0xbc7f('0xd9')])));}}return{'date':moment(_0x3b6491[_0xbc7f('0x75')])[_0xbc7f('0xb9')](_0xbc7f('0xdc'))[_0xbc7f('0x97')](),'sender':_0x3b6491[_0xbc7f('0xcf')]===_0xbc7f('0xdd')?_0x3b6491[_0xbc7f('0x6b')]?_0x3b6491[_0xbc7f('0x6b')][_0xbc7f('0x30')]:_0xbc7f('0xde'):_0x3b6491[_0xbc7f('0x5f')][_0xbc7f('0xca')]+(_0x3b6491[_0xbc7f('0x5f')][_0xbc7f('0xcb')]?'\x20'+_0x3b6491[_0xbc7f('0x5f')]['lastName']:''),'body':_0x3b6491['Attachment']?_0x3b6491[_0xbc7f('0xcd')][_0xbc7f('0xd9')]:_0x3b6491[_0xbc7f('0x6e')],'direction':_0x3b6491[_0xbc7f('0xcf')]===_0xbc7f('0xdd')?'A':'C','secret':_0x3b6491[_0xbc7f('0xdf')],'read':_0x3b6491[_0xbc7f('0x7c')],'attachment':_0x3b6491[_0xbc7f('0xcd')]};})[_0xbc7f('0x5d')]()};return ejs[_0xbc7f('0xe0')](path[_0xbc7f('0xc4')](config[_0xbc7f('0xc5')],_0xbc7f('0xc6'),'views',_0xbc7f('0xe1')),{'interaction':_0x472c28})['then'](function(_0x25b728){var _0x5cfd5e=path[_0xbc7f('0xc4')](_0x53a9b8,_0xbc7f('0xe2')+_0x5a6ff8['id']+'-'+_0x461652+'.pdf');var _0x2fa356={'path':_0x5cfd5e,'channel':_0xbc7f('0xe3'),'interactionId':_0x5a6ff8['id']};return pdf[_0xbc7f('0xe4')](_0x25b728,_0x2fa356);})[_0xbc7f('0x27')](function(){return new BPromise(function(_0x559b75,_0x23cbff){zipdir(_0x53a9b8,{'saveTo':_0x22e7f},function(_0xe61ff7,_0x3a8a2f){if(_0xe61ff7)return _0x23cbff(_0xe61ff7);return _0x559b75(_0x3a8a2f);});})[_0xbc7f('0x27')](function(){return new BPromise(function(_0x24c3bf,_0x44c626){rimraf(_0x53a9b8,function(_0x5856b6){if(_0x5856b6)_0x44c626(_0x5856b6);return _0x24c3bf();});});})[_0xbc7f('0x27')](function(){return _0x526ff5[_0xbc7f('0xe5')](_0x22e7f,_0x5ab837,function(_0x3faf23){if(_0x3faf23){console[_0xbc7f('0xe6')](_0xbc7f('0xe7'),_0x3faf23);}else{fs['unlinkSync'](_0x22e7f);}});});});}else{return _0x526ff5['sendStatus'](0xc8);}})[_0xbc7f('0x36')](handleError(_0x526ff5,null));};