Built motion from commit 43538d75.|2.5.35
[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 _0x32cd=['filters','intersection','attributes','fields','length','hasOwnProperty','order','sort','where','pick','select','field','color','from','tools_tags','u.fullname','ui.OpenchannelInteractionId','user_has_openchannel_interactions','left_join','u.id\x20=\x20ui.UserId','openchannel_interactions','cm_contacts','c.id\x20=\x20i.ContactId\x20AND\x20c.deletedAt\x20IS\x20NULL','users','o.id\x20=\x20i.UserId','openchannel_accounts','me.OpenchannelInteractionId\x20=\x20i.id','openchannel_interaction_has_tags','expr','search','parseSearch','sqlOperator','conditions','find','unreadMessages','`\x20=\x200','`\x20>\x200','Contact','$substring','getFullTextValue','value','c.firstName\x20LIKE\x20?\x20OR\x20c.lastName\x20LIKE\x20?\x20OR\x20c.email\x20LIKE\x20?','ContactId','text','start','Tag','CONCAT(\x27,\x27,\x20`TagIds`,\x20\x27,\x27)\x20LIKE\x20?','union','User','operator','CONCAT_WS(\x27\x20\x27,\x20o.name,\x20o.fullname)\x20LIKE\x20?','UserId','buildExpression','body','some','compact','isEmpty','OpenchannelAccountId','i.OpenchannelAccountId\x20IN\x20?','concat','createdAt','parse','$gte','$lte','i.createdAt\x20BETWEEN\x20?\x20AND\x20?','\x20IS\x20NULL','\x20=\x20?','having','`unreadMessages`\x20=\x200','`unreadMessages`\x20>\x200','isArray','tag','filter','isNumeric','i.id\x20LIKE\x20?','CONCAT(c.firstName,\x20\x27\x20\x27,\x20c.lastName)\x20LIKE\x20?','group','Sequelize','QueryTypes','clone','SUM(me.read\x20=\x200\x20AND\x20me.direction\x20=\x20\x27in\x27)','contactName','DESC','t.id\x20IN\x20?','resolve','i.OpenchannelAccountId\x20IS\x20NOT\x20NULL','user','agent','getOpenchannelAccounts','i.OpenchannelAccountId\x20IS\x20NULL','role','sequelize','a.key','Account.key','Account.mandatoryDisposition','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','i.*','c.firstName','Contact.firstName','Contact.lastName','c.email','Contact.email','c.phone','c.mobile','Contact.fax','o.fullname','Owner.fullname','Owner.internal','GROUP_CONCAT(DISTINCT\x20t.id)','toString','ui.OpenchannelInteractionId\x20IN\x20?','groupBy','OpenchannelInteractionId','all','params','includeAll','include','merge','options','create','describe','addMessage','OpenchannelMessage','ids','omit','getMessages','findOne','nolimit','format','YYYY-MM-DD\x20HH:mm:ss','findAll','rows','addTags','setTags','spread','emit','removeTags','download','join','root','server','files','attachments','transcript-%d-%s.zip','firstName','Attachment','direction','secret','read','Account','Owner','get','mkdirSync','closed','closedAt','disposition','threadId','Public','Private','Messages','basename','existsSync','createWriteStream','out','lastName','renderFile','views','downloadInteraction.ejs','transcript-','.pdf','Openchannel','log','err','unlinkSync','eml-format','rimraf','moment','bluebird','mustache','path','sox','to-csv','ejs','fs-extra','squel','crypto','desk.js','querystring','../../components/auth/service','../../components/parsers/qs','../../components/parsers/advancedSearch','../../components/export/pdf','../../config/license/hardware','../../config/logger','api','../../config/utils','../../config/environment','../../config/license/util','../../mysqldb','redis','localhost','./openchannelInteraction.socket','register','sendStatus','end','status','count','offset','limit','set','apply','reject','destroy','then','stack','name','send','Users','map','fullname','TagIds','Tags','split','forEach','push','catch','index','OpenchannelInteraction','rawAttributes','fieldName','type','key','model','query','keys'];(function(_0x341e0f,_0x4a09d0){var _0x59555a=function(_0x4343a8){while(--_0x4343a8){_0x341e0f['push'](_0x341e0f['shift']());}};_0x59555a(++_0x4a09d0);}(_0x32cd,0x199));var _0xd32c=function(_0x76bb0f,_0x3eeac6){_0x76bb0f=_0x76bb0f-0x0;var _0x47abc1=_0x32cd[_0x76bb0f];return _0x47abc1;};'use strict';var emlformat=require(_0xd32c('0x0'));var rimraf=require(_0xd32c('0x1'));var zipdir=require('zip-dir');var jsonpatch=require('fast-json-patch');var rp=require('request-promise');var moment=require(_0xd32c('0x2'));var BPromise=require(_0xd32c('0x3'));var Mustache=require(_0xd32c('0x4'));var util=require('util');var path=require(_0xd32c('0x5'));var sox=require(_0xd32c('0x6'));var csv=require(_0xd32c('0x7'));var ejs=require(_0xd32c('0x8'));var fs=require('fs');var fs_extra=require(_0xd32c('0x9'));var _=require('lodash');var squel=require(_0xd32c('0xa'));var crypto=require(_0xd32c('0xb'));var jsforce=require('jsforce');var deskjs=require(_0xd32c('0xc'));var toCsv=require(_0xd32c('0x7'));var querystring=require(_0xd32c('0xd'));var Papa=require('papaparse');var Redis=require('ioredis');var authService=require(_0xd32c('0xe'));var qs=require(_0xd32c('0xf'));var as=require(_0xd32c('0x10'));var pdf=require(_0xd32c('0x11'));var hardwareService=require(_0xd32c('0x12'));var logger=require(_0xd32c('0x13'))(_0xd32c('0x14'));var utils=require(_0xd32c('0x15'));var config=require(_0xd32c('0x16'));var licenseUtil=require(_0xd32c('0x17'));var db=require(_0xd32c('0x18'))['db'];config[_0xd32c('0x19')]=_['defaults'](config[_0xd32c('0x19')],{'host':_0xd32c('0x1a'),'port':0x18eb});var socket=require('socket.io-emitter')(new Redis(config[_0xd32c('0x19')]));require(_0xd32c('0x1b'))[_0xd32c('0x1c')](socket);function respondWithStatusCode(_0x458f50,_0x42d068){_0x42d068=_0x42d068||0xcc;return function(_0x3083dc){if(_0x3083dc){return _0x458f50[_0xd32c('0x1d')](_0x42d068);}return _0x458f50['status'](_0x42d068)[_0xd32c('0x1e')]();};}function respondWithResult(_0x3fe3a2,_0x12c5db){_0x12c5db=_0x12c5db||0xc8;return function(_0x1bb8fa){if(_0x1bb8fa){return _0x3fe3a2[_0xd32c('0x1f')](_0x12c5db)['json'](_0x1bb8fa);}};}function respondWithFilteredResult(_0x2895f9,_0x159c17){return function(_0x2480e7){if(_0x2480e7){var _0x3dd986=typeof _0x159c17['offset']==='undefined'&&typeof _0x159c17['limit']==='undefined';var _0x61bde3=_0x2480e7[_0xd32c('0x20')];var _0x5da4de=_0x3dd986?0x0:_0x159c17[_0xd32c('0x21')];var _0xf6fd=_0x3dd986?_0x2480e7[_0xd32c('0x20')]:_0x159c17[_0xd32c('0x21')]+_0x159c17[_0xd32c('0x22')];var _0x5b961c;if(_0xf6fd>=_0x61bde3){_0xf6fd=_0x61bde3;_0x5b961c=0xc8;}else{_0x5b961c=0xce;}_0x2895f9[_0xd32c('0x1f')](_0x5b961c);return _0x2895f9[_0xd32c('0x23')]('Content-Range',_0x5da4de+'-'+_0xf6fd+'/'+_0x61bde3)['json'](_0x2480e7);}return null;};}function patchUpdates(_0x553d5a){return function(_0xb78e6f){try{jsonpatch[_0xd32c('0x24')](_0xb78e6f,_0x553d5a,!![]);}catch(_0x5d5646){return BPromise[_0xd32c('0x25')](_0x5d5646);}return _0xb78e6f['save']();};}function saveUpdates(_0x1054b8,_0x3550bd){return function(_0xbe21b8){if(_0xbe21b8){return _0xbe21b8['update'](_0x1054b8)['then'](function(_0x410c37){return _0x410c37;});}return null;};}function removeEntity(_0x4652f8,_0x499cf4){return function(_0x311dab){if(_0x311dab){return _0x311dab[_0xd32c('0x26')]()[_0xd32c('0x27')](function(){_0x4652f8[_0xd32c('0x1f')](0xcc)[_0xd32c('0x1e')]();});}};}function handleEntityNotFound(_0x422da4,_0x3dc7fc){return function(_0x5ae5c6){if(!_0x5ae5c6){_0x422da4['sendStatus'](0x194);}return _0x5ae5c6;};}function handleError(_0x1018bf,_0x3e01cc){_0x3e01cc=_0x3e01cc||0x1f4;return function(_0x48bdcd){logger['error'](_0x48bdcd[_0xd32c('0x28')]);if(_0x48bdcd[_0xd32c('0x29')]){delete _0x48bdcd[_0xd32c('0x29')];}_0x1018bf['status'](_0x3e01cc)[_0xd32c('0x2a')](_0x48bdcd);};}function getInteractionUsers(_0x2a7fb2,_0x15e1ca){return new BPromise(function(_0xa0447a,_0x5a7279){try{if(_0x15e1ca[_0x2a7fb2['id']]){_0x2a7fb2[_0xd32c('0x2b')]=_[_0xd32c('0x2c')](_0x15e1ca[_0x2a7fb2['id']],function(_0x3c1980){return{'id':_0x3c1980['id'],'fullname':_0x3c1980[_0xd32c('0x2d')]};});}}catch(_0x1cda82){_0x5a7279(_0x1cda82);}_0xa0447a(_0x2a7fb2);});}function getInteractionTags(_0x4bc5be,_0xc0021){return new BPromise(function(_0x162aa5,_0xea1321){try{if(_0x4bc5be[_0xd32c('0x2e')]){_0x4bc5be[_0xd32c('0x2f')]=[];_0x4bc5be[_0xd32c('0x2e')][_0xd32c('0x30')](',')[_0xd32c('0x31')](function(_0x3a49fd){_0x4bc5be['Tags'][_0xd32c('0x32')](_0xc0021[_0x3a49fd]);});}delete _0x4bc5be[_0xd32c('0x2e')];}catch(_0x2db176){_0xea1321(_0x2db176);}_0x162aa5(_0x4bc5be);});}function updateOpenchannelInteraction(_0x59c6b8,_0x259457,_0xadeb34){return new BPromise(function(_0x168ac9,_0x2951b7){return getInteractionUsers(_0x59c6b8,_0xadeb34)[_0xd32c('0x27')](function(_0x1b8f3c){return getInteractionTags(_0x1b8f3c,_0x259457);})['then'](function(_0x1560c3){_0x168ac9(_0x1560c3);})[_0xd32c('0x33')](function(_0x1fff05){_0x2951b7(_0x1fff05);});});}exports[_0xd32c('0x34')]=function(_0x5ef5a2,_0x45cf9f){var _0x212818={},_0x9644a={},_0xdb9ce1={'count':0x0,'rows':[]};var _0x480686=_[_0xd32c('0x2c')](db[_0xd32c('0x35')][_0xd32c('0x36')],function(_0x52d896){return{'name':_0x52d896[_0xd32c('0x37')],'type':_0x52d896[_0xd32c('0x38')][_0xd32c('0x39')]};});_0x9644a[_0xd32c('0x3a')]=_[_0xd32c('0x2c')](_0x480686,_0xd32c('0x29'));_0x9644a[_0xd32c('0x3b')]=_[_0xd32c('0x3c')](_0x5ef5a2[_0xd32c('0x3b')]);_0x9644a[_0xd32c('0x3d')]=_[_0xd32c('0x3e')](_0x9644a[_0xd32c('0x3a')],_0x9644a[_0xd32c('0x3b')]);_0x212818[_0xd32c('0x3f')]=_[_0xd32c('0x3e')](_0x9644a['model'],qs['fields'](_0x5ef5a2[_0xd32c('0x3b')][_0xd32c('0x40')]));_0x212818[_0xd32c('0x3f')]=_0x212818[_0xd32c('0x3f')][_0xd32c('0x41')]?_0x212818[_0xd32c('0x3f')]:_0x9644a[_0xd32c('0x3a')];if(!_0x5ef5a2[_0xd32c('0x3b')][_0xd32c('0x42')]('nolimit')){_0x212818[_0xd32c('0x22')]=qs[_0xd32c('0x22')](_0x5ef5a2[_0xd32c('0x3b')][_0xd32c('0x22')]);_0x212818['offset']=qs[_0xd32c('0x21')](_0x5ef5a2[_0xd32c('0x3b')][_0xd32c('0x21')]);}_0x212818[_0xd32c('0x43')]=qs[_0xd32c('0x44')](_0x5ef5a2[_0xd32c('0x3b')][_0xd32c('0x44')]);_0x212818[_0xd32c('0x45')]=qs[_0xd32c('0x3d')](_[_0xd32c('0x46')](_0x5ef5a2[_0xd32c('0x3b')],_0x9644a[_0xd32c('0x3d')]),_0x480686);var _0x24c2ea=[];var _0x188a79=squel[_0xd32c('0x47')]()[_0xd32c('0x48')]('id')[_0xd32c('0x48')]('name')[_0xd32c('0x48')](_0xd32c('0x49'))[_0xd32c('0x4a')](_0xd32c('0x4b'));var _0x4fae22=squel[_0xd32c('0x47')]()[_0xd32c('0x48')]('u.id')[_0xd32c('0x48')](_0xd32c('0x4c'))[_0xd32c('0x48')](_0xd32c('0x4d'))[_0xd32c('0x4a')](_0xd32c('0x4e'),'ui')[_0xd32c('0x4f')]('users','u',_0xd32c('0x50'));var _0x5cee76=squel['select']()[_0xd32c('0x4a')](_0xd32c('0x51'),'i')[_0xd32c('0x4f')](_0xd32c('0x52'),'c',_0xd32c('0x53'))[_0xd32c('0x4f')](_0xd32c('0x54'),'o',_0xd32c('0x55'))[_0xd32c('0x4f')](_0xd32c('0x56'),'a','a.id\x20=\x20i.OpenchannelAccountId')[_0xd32c('0x4f')]('openchannel_messages','me',_0xd32c('0x57'))[_0xd32c('0x4f')](_0xd32c('0x58'),'it','it.OpenchannelInteractionId\x20=\x20i.id')[_0xd32c('0x4f')](_0xd32c('0x4b'),'t','t.id\x20=\x20it.TagId');var _0x43cd08=squel['expr']();var _0x4e2a3f=[];var _0x4520b0=squel[_0xd32c('0x59')]();var _0x25b806;if(_0x5ef5a2[_0xd32c('0x3b')][_0xd32c('0x5a')]){_0x25b806=as[_0xd32c('0x5b')](_0x5ef5a2['query'][_0xd32c('0x5a')]);var _0x202572=_0x25b806[_0xd32c('0x5c')];for(var _0x65d419=0x0;_0x65d419<_0x25b806['conditions'][_0xd32c('0x41')];_0x65d419++){var _0x818202=_0x25b806[_0xd32c('0x5d')][_0x65d419];var _0x3d4eba='i';var _0x371b3c=_[_0xd32c('0x5e')](_0x480686,['name',_0x818202[_0xd32c('0x48')]]);if(!_0x371b3c){switch(_0x818202['field']){case _0xd32c('0x5f'):if(_0x818202['value']==0x1){_0x5cee76['having']('`'+_0x818202['field']+_0xd32c('0x60'));}else{_0x5cee76['having']('`'+_0x818202[_0xd32c('0x48')]+_0xd32c('0x61'));}break;case _0xd32c('0x62'):if(_0x818202['operator']===_0xd32c('0x63')){if(_0x818202['value']['split']('\x20')['length']>0x1){_0x43cd08[_0x202572]('MATCH\x20(c.firstName,\x20c.lastName,\x20c.phone,\x20c.mobile,\x20c.fax,\x20c.email)\x20AGAINST\x20(?\x20IN\x20BOOLEAN\x20MODE)',qs[_0xd32c('0x64')](_0x818202[_0xd32c('0x65')]),null);}else{var _0x3a9f8c='%'+_0x818202['value']+'%';_0x43cd08[_0x202572](_0xd32c('0x66'),_0x3a9f8c,_0x3a9f8c,_0x3a9f8c);}}else{_0x371b3c=_['find'](_0x480686,[_0xd32c('0x29'),'ContactId']);_0x818202[_0xd32c('0x48')]=_0xd32c('0x67');_0x55bff2=as['buildExpression'](_0x3d4eba,_0x371b3c[_0xd32c('0x38')],_0x818202);_0x43cd08[_0x202572](_0x55bff2[_0xd32c('0x68')],_0x55bff2[_0xd32c('0x65')][_0xd32c('0x69')],_0x55bff2[_0xd32c('0x65')][_0xd32c('0x1e')]);}break;case _0xd32c('0x6a'):var _0x56b1c9=_0x818202[_0xd32c('0x65')]['split'](',')[_0xd32c('0x2c')](function(_0x3ee6b4){return Number(_0x3ee6b4);});_0x56b1c9[_0xd32c('0x31')](function(_0x30144f){_0x4520b0['or'](_0xd32c('0x6b'),'%,'+_0x30144f+',%');});_0x4e2a3f=_[_0xd32c('0x6c')](_0x4e2a3f,_0x56b1c9);break;case _0xd32c('0x6d'):if(_0x818202[_0xd32c('0x6e')]==='$substring'){_0x43cd08[_0x202572](_0xd32c('0x6f'),'%'+_0x818202['value']+'%',null);}else{_0x371b3c=_[_0xd32c('0x5e')](_0x480686,['name',_0xd32c('0x70')]);_0x818202[_0xd32c('0x48')]=_0xd32c('0x70');_0x55bff2=as[_0xd32c('0x71')](_0x3d4eba,_0x371b3c[_0xd32c('0x38')],_0x818202);_0x43cd08[_0x202572](_0x55bff2[_0xd32c('0x68')],_0x55bff2[_0xd32c('0x65')]['start'],_0x55bff2[_0xd32c('0x65')][_0xd32c('0x1e')]);}break;case _0xd32c('0x72'):_0x55bff2=as[_0xd32c('0x71')]('me',null,_0x818202);_0x43cd08[_0x202572](_0x55bff2[_0xd32c('0x68')],_0x55bff2['value'][_0xd32c('0x69')],_0x55bff2[_0xd32c('0x65')][_0xd32c('0x1e')]);break;}}else{var _0x55bff2=as[_0xd32c('0x71')](_0x3d4eba,_0x371b3c['type'],_0x818202);_0x43cd08[_0x202572](_0x55bff2[_0xd32c('0x68')],_0x55bff2[_0xd32c('0x65')][_0xd32c('0x69')],_0x55bff2[_0xd32c('0x65')][_0xd32c('0x1e')]);}}}else{var _0x76ae16=_(_0x5ef5a2[_0xd32c('0x3b')])[_0xd32c('0x3c')]()[_0xd32c('0x2c')](function(_0x5ebfc8){return _[_0xd32c('0x73')](_0x480686,['name',_0x5ebfc8])?_0x5ebfc8:undefined;})[_0xd32c('0x74')]()[_0xd32c('0x65')]();if(!_[_0xd32c('0x75')](_0x76ae16)){_0x76ae16[_0xd32c('0x31')](function(_0x15ac68){if(_0x15ac68===_0xd32c('0x76')){_0x5cee76[_0xd32c('0x45')](_0xd32c('0x77'),[][_0xd32c('0x78')](_0x5ef5a2['query'][_0x15ac68]));}else if(_0x15ac68===_0xd32c('0x79')){var _0x40edef=JSON[_0xd32c('0x7a')](_0x5ef5a2[_0xd32c('0x3b')][_0x15ac68])[_0xd32c('0x7b')];var _0x4fbb7a=JSON[_0xd32c('0x7a')](_0x5ef5a2[_0xd32c('0x3b')][_0x15ac68])[_0xd32c('0x7c')];_0x5cee76[_0xd32c('0x45')](_0xd32c('0x7d'),_0x40edef,_0x4fbb7a);}else{if(_0x5ef5a2[_0xd32c('0x3b')][_0x15ac68]==='null'){_0x5cee76[_0xd32c('0x45')]('i.'+_0x15ac68+_0xd32c('0x7e'));}else{_0x5cee76[_0xd32c('0x45')]('i.'+_0x15ac68+_0xd32c('0x7f'),_0x5ef5a2[_0xd32c('0x3b')][_0x15ac68]);}}});}if(_0x5ef5a2[_0xd32c('0x3b')]['read']){if(_0x5ef5a2[_0xd32c('0x3b')]['read']==0x1){_0x5cee76[_0xd32c('0x80')](_0xd32c('0x81'));}else{_0x5cee76[_0xd32c('0x80')](_0xd32c('0x82'));}}if(_0x5ef5a2[_0xd32c('0x3b')]['tag']){_0x4e2a3f=_[_0xd32c('0x83')](_0x5ef5a2[_0xd32c('0x3b')][_0xd32c('0x84')])?_0x5ef5a2['query'][_0xd32c('0x84')]:new Array(_0x5ef5a2[_0xd32c('0x3b')][_0xd32c('0x84')]);_0x4e2a3f['forEach'](function(_0x20b63a){_0x4520b0['or'](_0xd32c('0x6b'),'%,'+_0x20b63a+',%');});}if(_0x5ef5a2[_0xd32c('0x3b')][_0xd32c('0x85')]){var _0x1004c4=_0x5ef5a2[_0xd32c('0x3b')][_0xd32c('0x85')]['replace']('\x5c','\x5c\x5c')['replace'](/'/g,'\x27\x27');if(qs[_0xd32c('0x86')](_0x1004c4)){_0x43cd08['or'](_0xd32c('0x87'),_0x1004c4+'%');}else{_0x43cd08['or']('MATCH\x20(me.body)\x20AGAINST\x20(?\x20IN\x20BOOLEAN\x20MODE)',qs[_0xd32c('0x64')](_0x1004c4))['or']('i.from\x20LIKE\x20?',_0x1004c4+'%')['or'](_0xd32c('0x88'),_0x1004c4+'%');}}}_0x5cee76[_0xd32c('0x45')](_0x43cd08);_0x5cee76[_0xd32c('0x89')]('i.id');var _0x416977={'type':db[_0xd32c('0x8a')][_0xd32c('0x8b')]['SELECT'],'raw':!![]};var _0x2a8c8e=_0x5cee76[_0xd32c('0x8c')]();_0x2a8c8e['field']('i.id');_0x2a8c8e[_0xd32c('0x48')](_0xd32c('0x8d'),'unreadMessages');if(_0x212818['order']){_0x212818[_0xd32c('0x43')][_0xd32c('0x31')](function(_0x1d1da8){var _0x113e91=_0x1d1da8[0x0]===_0xd32c('0x8e')?_0x1d1da8[0x0]:'i.'+_0x1d1da8[0x0];_0x5cee76[_0xd32c('0x43')](_0x113e91,_0x1d1da8[0x1]===_0xd32c('0x8f')?![]:!![]);});}if(!_[_0xd32c('0x75')](_0x4e2a3f)){_0x5cee76['having'](_0x4520b0);_0x2a8c8e[_0xd32c('0x45')](_0xd32c('0x90'),_0x4e2a3f);}BPromise[_0xd32c('0x91')]()[_0xd32c('0x27')](function(){if(!_0x25b806){if(_0x5ef5a2[_0xd32c('0x3b')][_0xd32c('0x76')])return;_0x2a8c8e[_0xd32c('0x45')](_0xd32c('0x92'));_0x5cee76[_0xd32c('0x45')](_0xd32c('0x92'));return;}if(_0x5ef5a2[_0xd32c('0x93')]['role']!==_0xd32c('0x94'))return;return _0x5ef5a2[_0xd32c('0x93')][_0xd32c('0x95')]({'attributes':['id'],'raw':!![]})['then'](function(_0x49cd96){if(_[_0xd32c('0x75')](_0x49cd96)){_0x2a8c8e[_0xd32c('0x45')](_0xd32c('0x96'));_0x5cee76[_0xd32c('0x45')](_0xd32c('0x96'));}else{_0x2a8c8e[_0xd32c('0x45')](_0xd32c('0x77'),_[_0xd32c('0x2c')](_0x49cd96,'id'));_0x5cee76[_0xd32c('0x45')]('i.OpenchannelAccountId\x20IN\x20?',_['map'](_0x49cd96,'id'));}});})[_0xd32c('0x27')](function(){if(_0x5ef5a2['user'][_0xd32c('0x97')]===_0xd32c('0x94')&&!_0x25b806&&!_0x5ef5a2['query'][_0xd32c('0x76')])return[];return db[_0xd32c('0x98')][_0xd32c('0x3b')](_0x2a8c8e['toString'](),_0x416977);})[_0xd32c('0x27')](function(_0x48caca){_0xdb9ce1[_0xd32c('0x20')]=_0x48caca[_0xd32c('0x41')];if(_0xdb9ce1[_0xd32c('0x20')]===0x0)return[];return db['sequelize'][_0xd32c('0x3b')](_0x188a79['toString'](),_0x416977)[_0xd32c('0x27')](function(_0x1c87e0){_0x24c2ea=_['keyBy'](_0x1c87e0,'id');_0x416977=_['merge'](_0x416977,{'model':db[_0xd32c('0x35')],'mapToModel':!![],'raw':!![],'nest':!![]});if(_0x5ef5a2[_0xd32c('0x3b')][_0xd32c('0x40')]){_0x212818[_0xd32c('0x3f')][_0xd32c('0x31')](function(_0x2dc690){_0x5cee76[_0xd32c('0x48')]('i.'+_0x2dc690);});}else{_0x5cee76[_0xd32c('0x48')](_0xd32c('0x8d'),_0xd32c('0x5f'));_0x5cee76[_0xd32c('0x48')](_0xd32c('0x99'),_0xd32c('0x9a'));_0x5cee76[_0xd32c('0x48')]('a.mandatoryDisposition',_0xd32c('0x9b'));_0x5cee76[_0xd32c('0x48')](_0xd32c('0x9c'),_0xd32c('0x8e'));_0x5cee76[_0xd32c('0x48')](_0xd32c('0x9d'));_0x5cee76[_0xd32c('0x48')](_0xd32c('0x9e'),_0xd32c('0x9f'));_0x5cee76['field']('c.lastName',_0xd32c('0xa0'));_0x5cee76[_0xd32c('0x48')](_0xd32c('0xa1'),_0xd32c('0xa2'));_0x5cee76['field'](_0xd32c('0xa3'),'Contact.phone');_0x5cee76[_0xd32c('0x48')](_0xd32c('0xa4'),'Contact.mobile');_0x5cee76[_0xd32c('0x48')]('c.fax',_0xd32c('0xa5'));_0x5cee76[_0xd32c('0x48')](_0xd32c('0xa6'),_0xd32c('0xa7'));_0x5cee76[_0xd32c('0x48')]('o.internal',_0xd32c('0xa8'));_0x5cee76['field'](_0xd32c('0xa9'),_0xd32c('0x2e'));}if(_0x212818[_0xd32c('0x22')])_0x5cee76['limit'](_0x212818[_0xd32c('0x22')]);if(_0x212818[_0xd32c('0x21')])_0x5cee76[_0xd32c('0x21')](_0x212818['offset']);return db[_0xd32c('0x98')][_0xd32c('0x3b')](_0x5cee76[_0xd32c('0xaa')](),_0x416977);})[_0xd32c('0x27')](function(_0x8dc082){if(_0x8dc082['length']>0x0)_0x4fae22[_0xd32c('0x45')](_0xd32c('0xab'),_[_0xd32c('0x2c')](_0x8dc082,'id'));return db[_0xd32c('0x98')][_0xd32c('0x3b')](_0x4fae22['toString'](),_0x416977)['then'](function(_0x369d4a){var _0x302eea=_[_0xd32c('0xac')](_0x369d4a,_0xd32c('0xad'));var _0x473aa6=[];_0x8dc082[_0xd32c('0x31')](function(_0x3c7b4c){_0x473aa6[_0xd32c('0x32')](updateOpenchannelInteraction(_0x3c7b4c,_0x24c2ea,_0x302eea));});return BPromise[_0xd32c('0xae')](_0x473aa6);});});})[_0xd32c('0x27')](function(_0x35f741){_0xdb9ce1['rows']=_0x35f741;return _0xdb9ce1;})[_0xd32c('0x27')](respondWithFilteredResult(_0x45cf9f,_0x212818))[_0xd32c('0x33')](handleError(_0x45cf9f,null));};exports['show']=function(_0x5902db,_0x43fb0a){var _0x51770c={};if(_0x5902db[_0xd32c('0x3b')][_0xd32c('0x38')]&&_0x5902db[_0xd32c('0x3b')][_0xd32c('0x38')]===_0xd32c('0x4a')){_0x51770c[_0xd32c('0x4a')]=_0x5902db[_0xd32c('0xaf')]['id'];}else{_0x51770c['id']=_0x5902db['params']['id'];}var _0x5f5b19={'raw':![],'where':_0x51770c},_0x2c1c47={};_0x2c1c47['model']=_[_0xd32c('0x3c')](db['OpenchannelInteraction'][_0xd32c('0x36')]);_0x2c1c47[_0xd32c('0x3b')]=_[_0xd32c('0x3c')](_0x5902db['query']);_0x2c1c47[_0xd32c('0x3d')]=_['intersection'](_0x2c1c47[_0xd32c('0x3a')],_0x2c1c47[_0xd32c('0x3b')]);_0x5f5b19[_0xd32c('0x3f')]=_[_0xd32c('0x3e')](_0x2c1c47[_0xd32c('0x3a')],qs[_0xd32c('0x40')](_0x5902db[_0xd32c('0x3b')]['fields']));_0x5f5b19['attributes']=_0x5f5b19[_0xd32c('0x3f')][_0xd32c('0x41')]?_0x5f5b19[_0xd32c('0x3f')]:_0x2c1c47[_0xd32c('0x3a')];if(_0x5902db[_0xd32c('0x3b')][_0xd32c('0xb0')]){_0x5f5b19[_0xd32c('0xb1')]=[{'all':!![]}];}_0x5f5b19=_[_0xd32c('0xb2')]({},_0x5f5b19,_0x5902db[_0xd32c('0xb3')]);return db['OpenchannelInteraction'][_0xd32c('0x5e')](_0x5f5b19)[_0xd32c('0x27')](handleEntityNotFound(_0x43fb0a,null))['then'](respondWithResult(_0x43fb0a,null))['catch'](handleError(_0x43fb0a,null));};exports[_0xd32c('0xb4')]=function(_0xaee2fc,_0x7088e9){return db['OpenchannelInteraction'][_0xd32c('0xb4')](_0xaee2fc[_0xd32c('0x72')],{})['then'](respondWithResult(_0x7088e9,0xc9))[_0xd32c('0x33')](handleError(_0x7088e9,null));};exports['update']=function(_0xff493d,_0x243e61){if(_0xff493d[_0xd32c('0x72')]['id']){delete _0xff493d[_0xd32c('0x72')]['id'];}return db[_0xd32c('0x35')]['find']({'where':{'id':_0xff493d[_0xd32c('0xaf')]['id']}})[_0xd32c('0x27')](handleEntityNotFound(_0x243e61,null))[_0xd32c('0x27')](saveUpdates(_0xff493d[_0xd32c('0x72')],null))[_0xd32c('0x27')](respondWithResult(_0x243e61,null))['catch'](handleError(_0x243e61,null));};exports[_0xd32c('0x26')]=function(_0x4d80b4,_0x2484e3){return db[_0xd32c('0x35')][_0xd32c('0x5e')]({'where':{'id':_0x4d80b4['params']['id']}})[_0xd32c('0x27')](handleEntityNotFound(_0x2484e3,null))[_0xd32c('0x27')](removeEntity(_0x2484e3,null))['catch'](handleError(_0x2484e3,null));};exports[_0xd32c('0xb5')]=function(_0x59c2e6,_0x288685){return db[_0xd32c('0x35')][_0xd32c('0xb5')]()['then'](respondWithResult(_0x288685,null))[_0xd32c('0x33')](handleError(_0x288685,null));};exports[_0xd32c('0xb6')]=function(_0x422d13,_0x578f49,_0x3e2fe1){return db[_0xd32c('0xb7')]['find']({'where':{'id':_0x422d13[_0xd32c('0xaf')]['id']}})['then'](handleEntityNotFound(_0x578f49,null))[_0xd32c('0x27')](function(_0x464502){if(_0x464502){return _0x464502[_0xd32c('0xb6')](_0x422d13[_0xd32c('0x72')][_0xd32c('0xb8')],_[_0xd32c('0xb9')](_0x422d13[_0xd32c('0x72')],[_0xd32c('0xb8'),'id'])||{});}})[_0xd32c('0x27')](respondWithResult(_0x578f49,null))['catch'](handleError(_0x578f49,null));};exports[_0xd32c('0xba')]=function(_0x26a3f9,_0x3c29c4,_0xa2f9c9){var _0x5a865f={'raw':![],'where':{}};var _0x58d507={};var _0x1ca0c4={'count':0x0,'rows':[]};return db['OpenchannelInteraction'][_0xd32c('0xbb')]({'where':{'id':_0x26a3f9[_0xd32c('0xaf')]['id']}})[_0xd32c('0x27')](handleEntityNotFound(_0x3c29c4,null))[_0xd32c('0x27')](function(_0x36e082){if(_0x36e082){_0x58d507['model']=_[_0xd32c('0x3c')](db['OpenchannelMessage'][_0xd32c('0x36')]);_0x58d507['query']=_[_0xd32c('0x3c')](_0x26a3f9['query']);_0x58d507[_0xd32c('0x3d')]=_[_0xd32c('0x3e')](_0x58d507[_0xd32c('0x3a')],_0x58d507[_0xd32c('0x3b')]);_0x5a865f[_0xd32c('0x3f')]=_[_0xd32c('0x3e')](_0x58d507[_0xd32c('0x3a')],qs[_0xd32c('0x40')](_0x26a3f9['query'][_0xd32c('0x40')]));_0x5a865f[_0xd32c('0x3f')]=_0x5a865f[_0xd32c('0x3f')][_0xd32c('0x41')]?_0x5a865f[_0xd32c('0x3f')]:_0x58d507[_0xd32c('0x3a')];if(!_0x26a3f9[_0xd32c('0x3b')][_0xd32c('0x42')](_0xd32c('0xbc'))){_0x5a865f['limit']=qs[_0xd32c('0x22')](_0x26a3f9[_0xd32c('0x3b')][_0xd32c('0x22')]);_0x5a865f[_0xd32c('0x21')]=qs[_0xd32c('0x21')](_0x26a3f9[_0xd32c('0x3b')]['offset']);}_0x5a865f['order']=qs['sort'](_0x26a3f9[_0xd32c('0x3b')][_0xd32c('0x44')]);_0x5a865f[_0xd32c('0x45')]=qs['filters'](_['pick'](_0x26a3f9[_0xd32c('0x3b')],_0x58d507[_0xd32c('0x3d')]));_0x5a865f[_0xd32c('0x45')][_0xd32c('0xad')]=_0x36e082['id'];if(_0x26a3f9[_0xd32c('0x3b')][_0xd32c('0x85')]){_0x5a865f[_0xd32c('0x45')]=_['merge'](_0x5a865f[_0xd32c('0x45')],{'$or':_[_0xd32c('0x2c')](_0x5a865f[_0xd32c('0x3f')],function(_0x1ca946){var _0x7f2b30={};_0x7f2b30[_0x1ca946]={'$like':'%'+_0x26a3f9[_0xd32c('0x3b')]['filter']+'%'};return _0x7f2b30;})});}if(_0x26a3f9['query'][_0xd32c('0x7b')]){var _0x3dfd1f=_0x26a3f9[_0xd32c('0x3b')][_0xd32c('0x7b')][_0xd32c('0x30')](',');var _0x5e7741={};_0x5e7741[_0x3dfd1f[0x0]]={'$gte':moment(_0x3dfd1f[0x1])[_0xd32c('0xbd')](_0xd32c('0xbe'))};_0x5a865f['where']=_[_0xd32c('0xb2')](_0x5a865f[_0xd32c('0x45')],_0x5e7741);}_0x5a865f=_[_0xd32c('0xb2')]({},_0x5a865f,_0x26a3f9[_0xd32c('0xb3')]);return db['OpenchannelMessage'][_0xd32c('0x20')]({'where':_0x5a865f[_0xd32c('0x45')]})[_0xd32c('0x27')](function(_0x2971a9){_0x1ca0c4['count']=_0x2971a9;if(_0x26a3f9['query'][_0xd32c('0xb0')]){_0x5a865f['include']=[{'all':!![]}];}return db[_0xd32c('0xb7')][_0xd32c('0xbf')](_0x5a865f);})['then'](function(_0x535829){_0x1ca0c4[_0xd32c('0xc0')]=_0x535829;return _0x1ca0c4;});}})['then'](respondWithFilteredResult(_0x3c29c4,_0x5a865f))[_0xd32c('0x33')](handleError(_0x3c29c4,null));};exports[_0xd32c('0xc1')]=function(_0x6390a,_0x5476fb,_0x2fcfc0){return db[_0xd32c('0x35')][_0xd32c('0x5e')]({'where':{'id':_0x6390a['params']['id']}})[_0xd32c('0x27')](handleEntityNotFound(_0x5476fb,null))[_0xd32c('0x27')](function(_0x39ba45){if(_0x39ba45){return _0x39ba45[_0xd32c('0xc2')](_0x6390a[_0xd32c('0x72')][_0xd32c('0xb8')],_[_0xd32c('0xb9')](_0x6390a[_0xd32c('0x72')],[_0xd32c('0xb8'),'id'])||{})[_0xd32c('0xc3')](function(){return db[_0xd32c('0x6a')][_0xd32c('0xbf')]({'attributes':['id',_0xd32c('0x29'),_0xd32c('0x49')],'where':{'id':_0x6390a['body']['ids']}});})[_0xd32c('0x27')](function(_0xd10ada){socket[_0xd32c('0xc4')]('openchannelInteractionTags:save',{'id':Number(_0x6390a[_0xd32c('0xaf')]['id']),'tags':_0xd10ada||[]});return{'id':Number(_0x6390a[_0xd32c('0xaf')]['id']),'tags':_0xd10ada||[]};});}})[_0xd32c('0x27')](respondWithResult(_0x5476fb,null))['catch'](handleError(_0x5476fb,null));};exports[_0xd32c('0xc5')]=function(_0x2a7d45,_0x97c1d3,_0x501bcf){return db['OpenchannelInteraction'][_0xd32c('0x5e')]({'where':{'id':_0x2a7d45['params']['id']}})[_0xd32c('0x27')](handleEntityNotFound(_0x97c1d3,null))[_0xd32c('0x27')](function(_0x4767ad){if(_0x4767ad){return _0x4767ad[_0xd32c('0xc5')](_0x2a7d45[_0xd32c('0x3b')][_0xd32c('0xb8')]);}})[_0xd32c('0x27')](respondWithStatusCode(_0x97c1d3,null))[_0xd32c('0x33')](handleError(_0x97c1d3,null));};exports[_0xd32c('0xc6')]=function(_0x5dbb83,_0x31fa84){var _0x2ab948=moment()['unix']()['toString']();var _0x47ac05=path[_0xd32c('0xc7')](config[_0xd32c('0xc8')],_0xd32c('0xc9'),_0xd32c('0xca'),'tmp');var _0x53f415=path[_0xd32c('0xc7')](config[_0xd32c('0xc8')],_0xd32c('0xc9'),_0xd32c('0xca'),_0xd32c('0xcb'));var _0x176abd=path['join'](_0x47ac05,_0x2ab948);var _0x335ec1=util[_0xd32c('0xbd')](_0xd32c('0xcc'),_0x5dbb83[_0xd32c('0xaf')]['id'],_0x2ab948);var _0x4e51e7=path[_0xd32c('0xc7')](_0x47ac05,_0x335ec1);var _0xb0eefe=[];_0xb0eefe[_0xd32c('0x32')]({'model':db[_0xd32c('0x6d')],'as':'User','attributes':['id',_0xd32c('0x2d')],'raw':!![]});_0xb0eefe[_0xd32c('0x32')]({'model':db['CmContact'],'as':_0xd32c('0x62'),'attributes':['id',_0xd32c('0xcd'),'lastName'],'raw':!![]});if(_0x5dbb83[_0xd32c('0x3b')][_0xd32c('0xcb')]){_0xb0eefe[_0xd32c('0x32')]({'model':db[_0xd32c('0xce')],'as':'Attachment','raw':!![]});}var _0x4b79a6=[{'model':db[_0xd32c('0xb7')],'as':'Messages','attributes':['id','body','createdAt',_0xd32c('0xcf'),_0xd32c('0xd0'),_0xd32c('0xd1')],'include':_0xb0eefe}];_0x4b79a6[_0xd32c('0x32')]({'model':db['OpenchannelAccount'],'as':_0xd32c('0xd2'),'attributes':['id',_0xd32c('0x29')]});_0x4b79a6[_0xd32c('0x32')]({'model':db[_0xd32c('0x6d')],'as':_0xd32c('0xd3'),'attributes':['id',_0xd32c('0x2d')]});return db[_0xd32c('0x35')]['find']({'where':{'id':_0x5dbb83[_0xd32c('0xaf')]['id']},'include':_0x4b79a6})[_0xd32c('0x27')](handleEntityNotFound(_0x31fa84,null))[_0xd32c('0x27')](function(_0xc613a6){if(_0xc613a6){var _0x596d2e=_0xc613a6[_0xd32c('0xd4')]({'plain':!![]});fs[_0xd32c('0xd5')](_0x176abd);var _0x575ff7={'channel':'Openchannel','account':_0x596d2e[_0xd32c('0xd2')]['name'],'agent':_0x596d2e[_0xd32c('0xd3')]?_0x596d2e['Owner'][_0xd32c('0x2d')]:'N.A.','createdAt':moment(_0x596d2e[_0xd32c('0x79')])[_0xd32c('0xbd')](_0xd32c('0xbe'))['toString'](),'closedAt':_0x596d2e[_0xd32c('0xd6')]?moment(_0x596d2e[_0xd32c('0xd7')])[_0xd32c('0xbd')]('YYYY-MM-DD\x20HH:mm:ss')[_0xd32c('0xaa')]():'','disposition':_0x596d2e[_0xd32c('0xd8')],'visibility':_0x596d2e[_0xd32c('0xd9')]?_0xd32c('0xda'):_0xd32c('0xdb'),'messages':_(_0x596d2e[_0xd32c('0xdc')])[_0xd32c('0x85')]({'secret':![]})[_0xd32c('0x2c')](function(_0x11ee68){if(_0x11ee68[_0xd32c('0xce')]){var _0xf4d70=path[_0xd32c('0xc7')](_0x53f415,_0x11ee68['Attachment'][_0xd32c('0xdd')]);if(fs[_0xd32c('0xde')](_0xf4d70)){fs['createReadStream'](_0xf4d70)['pipe'](fs[_0xd32c('0xdf')](path['join'](_0x176abd,_0x11ee68[_0xd32c('0xce')][_0xd32c('0xdd')])));}}return{'date':moment(_0x11ee68[_0xd32c('0x79')])[_0xd32c('0xbd')](_0xd32c('0xbe'))[_0xd32c('0xaa')](),'sender':_0x11ee68[_0xd32c('0xcf')]===_0xd32c('0xe0')?_0x11ee68[_0xd32c('0x6d')]?_0x11ee68['User'][_0xd32c('0x2d')]:'System':_0x11ee68[_0xd32c('0x62')][_0xd32c('0xcd')]+(_0x11ee68[_0xd32c('0x62')][_0xd32c('0xe1')]?'\x20'+_0x11ee68[_0xd32c('0x62')]['lastName']:''),'body':_0x11ee68['Attachment']?_0x11ee68[_0xd32c('0xce')]['basename']:_0x11ee68[_0xd32c('0x72')],'direction':_0x11ee68[_0xd32c('0xcf')]===_0xd32c('0xe0')?'A':'C','secret':_0x11ee68['secret'],'read':_0x11ee68[_0xd32c('0xd1')],'attachment':_0x11ee68[_0xd32c('0xce')]};})[_0xd32c('0x65')]()};return ejs[_0xd32c('0xe2')](path[_0xd32c('0xc7')](config[_0xd32c('0xc8')],_0xd32c('0xc9'),_0xd32c('0xe3'),_0xd32c('0xe4')),{'interaction':_0x575ff7})[_0xd32c('0x27')](function(_0x486ba8){var _0x435dbf=path[_0xd32c('0xc7')](_0x176abd,_0xd32c('0xe5')+_0x596d2e['id']+'-'+_0x2ab948+_0xd32c('0xe6'));var _0x2fa0c1={'path':_0x435dbf,'channel':_0xd32c('0xe7'),'interactionId':_0x596d2e['id']};return pdf['createPdfFromHTML'](_0x486ba8,_0x2fa0c1);})[_0xd32c('0x27')](function(){return new BPromise(function(_0x1c16df,_0x1c63fc){zipdir(_0x176abd,{'saveTo':_0x4e51e7},function(_0x1e590e,_0x35c25c){if(_0x1e590e)return _0x1c63fc(_0x1e590e);return _0x1c16df(_0x35c25c);});})[_0xd32c('0x27')](function(){return new BPromise(function(_0x5abd25,_0x5d4368){rimraf(_0x176abd,function(_0x3777a7){if(_0x3777a7)_0x5d4368(_0x3777a7);return _0x5abd25();});});})['then'](function(){return _0x31fa84[_0xd32c('0xc6')](_0x4e51e7,_0x335ec1,function(_0x2b63cf){if(_0x2b63cf){console[_0xd32c('0xe8')](_0xd32c('0xe9'),_0x2b63cf);}else{fs[_0xd32c('0xea')](_0x4e51e7);}});});});}else{return _0x31fa84[_0xd32c('0x1d')](0xc8);}})[_0xd32c('0x33')](handleError(_0x31fa84,null));};