2d2e25be8b908666a1237f2cf3b1edac924a0d4b
[motion2.git] / server / api / chatInteraction / chatInteraction.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 _0x645f=['find','create','describe','createVidaooSession','findOne','ChatWebsite','ChatWebsiteId','vidaooApiKey','vidaooNote','vidaooEscalation','fullname','email','POST','https://api-vidaoo.xcally.com/api/v1/meetings','vidaooTopic','isNull','follow','vidaooMetadata','parse','metadata','Sequelize','Error\x20in\x20conversion\x20vidaooMetadata','ValidationError','Error\x20in\x20Vidaoo\x20configuration','join_url','out','ChatMessage','Unable\x20to\x20create\x20Vidaoo\x20session','customUpdate','Contact','abandon','abandonInteraction,\x20%s,\x20%s,\x20%s','entity\x20not\x20found','UserId','Interaction\x20already\x20assigned','closed','disposition','AbandonInteraction','attachmentUpload','file','originalname','filename','mimetype','Attachment','attachmentDownload','json','Unauthorized','attachId','join','server/files/attachments/','offline-chat/','basename','download','close','mailTranscript','server/files/chat/upload/','transcript-%s.txt','now','pathTranscript','Messages','User','ContactId','format','%s%d','agentAlias','customer','Visitor','agentIdentifier','website_alias','agent_alias','alias','agent_fullname','System','[%s]\x20%s:\x20%s\x0a','createdAt','appendFileSync','MailAccount','Smtp','CloudProvider','getSmtpOptions','SendMail','sendMail','\x22%s\x22\x20<%s>','Mail\x20Transcript','Hi\x20%s,\x20attached\x20you\x20can\x20find\x20your\x20transcript\x20file.\x20\x0a\x0aRegards.\x20\x0a\x0a','addMessage','ids','omit','getMessages','hasOwnProperty','nolimit','order','sort','where','pick','filter','$gte','split','YYYY-MM-DD\x20HH:mm:ss','count','ChatInteractionId','includeAgent','push','addTags','setTags','spread','Tag','color','emit','chatInteractionTags:save','removeTags','unix','toString','root','files','tmp','server','attachments','transcript-%d-%s.zip','CmContact','lastName','direction','secret','read','Account','get','mkdirSync','Chat','N.A.','browserName','osName','referer','customerIp','customerPort','tail','existsSync','createReadStream','pipe','createWriteStream','firstName','value','views','transcript-','rimraf','zip-dir','moment','bluebird','mustache','path','ejs','lodash','../../components/parsers/qs','../../components/export/pdf','../../config/logger','../../config/environment','../../mysqldb','redis','localhost','socket.io-emitter','./chatInteraction.socket','jayson/promise','client','http','request','then','info','request\x20sent','debug','ChatInteraction,\x20%s,\x20%s,\x20%s','stringify','error','code','ChatInteraction,\x20%s,\x20%s','message','result','catch','sendStatus','status','offset','undefined','limit','Content-Range','update','destroy','stack','name','send','render','merge','body','contact','agent','interaction','account','index','map','fieldName','type','getOptions','findAll','chat','query','user','show','params','model','keys','ChatInteraction','rawAttributes','filters','intersection','attributes','fields','length','includeAll','include','options'];(function(_0x2db667,_0x193cf5){var _0x83ff9=function(_0x5da022){while(--_0x5da022){_0x2db667['push'](_0x2db667['shift']());}};_0x83ff9(++_0x193cf5);}(_0x645f,0x8f));var _0xf645=function(_0x122ef9,_0x451d2a){_0x122ef9=_0x122ef9-0x0;var _0x42fba0=_0x645f[_0x122ef9];return _0x42fba0;};'use strict';var rimraf=require(_0xf645('0x0'));var zipdir=require(_0xf645('0x1'));var rp=require('request-promise');var moment=require(_0xf645('0x2'));var BPromise=require(_0xf645('0x3'));var Mustache=require(_0xf645('0x4'));var util=require('util');var path=require(_0xf645('0x5'));var ejs=require(_0xf645('0x6'));var fs=require('fs');var _=require(_0xf645('0x7'));var Redis=require('ioredis');var interaction=require('../../components/interaction/service');var qs=require(_0xf645('0x8'));var pdf=require(_0xf645('0x9'));var logger=require(_0xf645('0xa'))('api');var config=require(_0xf645('0xb'));var db=require(_0xf645('0xc'))['db'];config[_0xf645('0xd')]=_['defaults'](config[_0xf645('0xd')],{'host':_0xf645('0xe'),'port':0x18eb});var socket=require(_0xf645('0xf'))(new Redis(config[_0xf645('0xd')]));require(_0xf645('0x10'))['register'](socket);var jayson=require(_0xf645('0x11'));var client=jayson[_0xf645('0x12')]['http']({'port':0x232b});var client9004=jayson['client'][_0xf645('0x13')]({'port':0x232c});function respondWithRpcPromise(_0x2eea3d,_0x3555c5,_0x43313b,_0x19ade0){return new BPromise(function(_0x1ae5c6,_0x16a810){var _0x5f31af=_0x19ade0||client;return _0x5f31af[_0xf645('0x14')](_0x2eea3d,_0x43313b)[_0xf645('0x15')](function(_0x129414){logger[_0xf645('0x16')]('ChatInteraction,\x20%s,\x20%s',_0x3555c5,_0xf645('0x17'));logger[_0xf645('0x18')](_0xf645('0x19'),_0x3555c5,_0xf645('0x17'),JSON[_0xf645('0x1a')](_0x129414));if(_0x129414['error']){if(_0x129414[_0xf645('0x1b')][_0xf645('0x1c')]===0x1f4){logger['error'](_0xf645('0x1d'),_0x3555c5,_0x129414[_0xf645('0x1b')][_0xf645('0x1e')]);return _0x16a810(_0x129414[_0xf645('0x1b')][_0xf645('0x1e')]);}logger['error'](_0xf645('0x1d'),_0x3555c5,_0x129414[_0xf645('0x1b')][_0xf645('0x1e')]);return _0x1ae5c6(_0x129414[_0xf645('0x1b')]['message']);}else{logger[_0xf645('0x16')](_0xf645('0x1d'),_0x3555c5,_0xf645('0x17'));_0x1ae5c6(_0x129414[_0xf645('0x1f')][_0xf645('0x1e')]);}})[_0xf645('0x20')](function(_0x238c8b){logger[_0xf645('0x1b')](_0xf645('0x1d'),_0x3555c5,_0x238c8b);_0x16a810(_0x238c8b);});});}function respondWithStatusCode(_0xaebc3d,_0x2c6810){_0x2c6810=_0x2c6810||0xcc;return function(_0x48929){if(_0x48929){return _0xaebc3d[_0xf645('0x21')](_0x2c6810);}return _0xaebc3d['status'](_0x2c6810)['end']();};}function respondWithResult(_0x1520b5,_0x12298c){_0x12298c=_0x12298c||0xc8;return function(_0x3b7c2d){if(_0x3b7c2d){return _0x1520b5[_0xf645('0x22')](_0x12298c)['json'](_0x3b7c2d);}};}function respondWithFilteredResult(_0x258425,_0x65c09b){return function(_0x55fa31){if(_0x55fa31){var _0x4c54f6=typeof _0x65c09b[_0xf645('0x23')]===_0xf645('0x24')&&typeof _0x65c09b[_0xf645('0x25')]===_0xf645('0x24');var _0x42654e=_0x55fa31['count'];var _0xa4e6a9=_0x4c54f6?0x0:_0x65c09b[_0xf645('0x23')];var _0x348d9e=_0x4c54f6?_0x55fa31['count']:_0x65c09b[_0xf645('0x23')]+_0x65c09b[_0xf645('0x25')];var _0x4f7854;if(_0x348d9e>=_0x42654e){_0x348d9e=_0x42654e;_0x4f7854=0xc8;}else{_0x4f7854=0xce;}_0x258425[_0xf645('0x22')](_0x4f7854);return _0x258425['set'](_0xf645('0x26'),_0xa4e6a9+'-'+_0x348d9e+'/'+_0x42654e)['json'](_0x55fa31);}return null;};}function saveUpdates(_0x438d2f){return function(_0x304831){if(_0x304831){return _0x304831[_0xf645('0x27')](_0x438d2f)[_0xf645('0x15')](function(_0x54efa4){return _0x54efa4;});}return null;};}function removeEntity(_0x551faa){return function(_0x36cb6a){if(_0x36cb6a){return _0x36cb6a[_0xf645('0x28')]()[_0xf645('0x15')](function(){_0x551faa[_0xf645('0x22')](0xcc)['end']();});}};}function handleEntityNotFound(_0x1086d2){return function(_0x46522e){if(!_0x46522e){_0x1086d2[_0xf645('0x21')](0x194);}return _0x46522e;};}function handleError(_0x423564,_0x4a92d0){_0x4a92d0=_0x4a92d0||0x1f4;return function(_0x2855c9){logger[_0xf645('0x1b')](_0x2855c9[_0xf645('0x29')]);if(_0x2855c9[_0xf645('0x2a')]){delete _0x2855c9[_0xf645('0x2a')];}_0x423564[_0xf645('0x22')](_0x4a92d0)[_0xf645('0x2b')](_0x2855c9);};}function getMustacheRender(_0x49313b,_0x4ab0a0){return Mustache[_0xf645('0x2c')](_0x49313b,_[_0xf645('0x2d')](_0x4ab0a0[_0xf645('0x2e')],{'contact':_0x4ab0a0[_0xf645('0x2f')],'message':_0x4ab0a0[_0xf645('0x1e')],'agent':_0x4ab0a0[_0xf645('0x30')],'interaction':_0x4ab0a0[_0xf645('0x31')],'account':_0x4ab0a0[_0xf645('0x32')]}))||'';}exports[_0xf645('0x33')]=function(_0x270a53,_0x3582e2){var _0x51925d={};var _0x107c5e=_[_0xf645('0x34')](db['ChatInteraction']['rawAttributes'],function(_0x50b3c5){return{'name':_0x50b3c5[_0xf645('0x35')],'type':_0x50b3c5[_0xf645('0x36')]['key']};});_0x51925d=qs[_0xf645('0x37')](_0x107c5e,_0x270a53);return interaction[_0xf645('0x38')](_0xf645('0x39'),_0x270a53[_0xf645('0x3a')],_0x270a53[_0xf645('0x3b')],_0x51925d,_0x107c5e)[_0xf645('0x15')](respondWithFilteredResult(_0x3582e2,_0x51925d))[_0xf645('0x20')](handleError(_0x3582e2,null));};exports[_0xf645('0x3c')]=function(_0xec563e,_0x6f290c){var _0x6ce810={'raw':![],'where':{'id':_0xec563e[_0xf645('0x3d')]['id']}},_0x173d30={};_0x173d30[_0xf645('0x3e')]=_[_0xf645('0x3f')](db[_0xf645('0x40')][_0xf645('0x41')]);_0x173d30[_0xf645('0x3a')]=_[_0xf645('0x3f')](_0xec563e[_0xf645('0x3a')]);_0x173d30[_0xf645('0x42')]=_[_0xf645('0x43')](_0x173d30[_0xf645('0x3e')],_0x173d30[_0xf645('0x3a')]);_0x6ce810[_0xf645('0x44')]=_[_0xf645('0x43')](_0x173d30[_0xf645('0x3e')],qs[_0xf645('0x45')](_0xec563e[_0xf645('0x3a')]['fields']));_0x6ce810[_0xf645('0x44')]=_0x6ce810[_0xf645('0x44')][_0xf645('0x46')]?_0x6ce810['attributes']:_0x173d30[_0xf645('0x3e')];if(_0xec563e[_0xf645('0x3a')][_0xf645('0x47')]){_0x6ce810[_0xf645('0x48')]=[{'all':!![]}];}_0x6ce810=_[_0xf645('0x2d')]({},_0x6ce810,_0xec563e[_0xf645('0x49')]);return db['ChatInteraction'][_0xf645('0x4a')](_0x6ce810)['then'](handleEntityNotFound(_0x6f290c,null))[_0xf645('0x15')](respondWithResult(_0x6f290c,null))[_0xf645('0x20')](handleError(_0x6f290c,null));};exports[_0xf645('0x4b')]=function(_0x14a771,_0x5e2190){return db[_0xf645('0x40')][_0xf645('0x4b')](_0x14a771['body'],{})['then'](respondWithResult(_0x5e2190,0xc9))[_0xf645('0x20')](handleError(_0x5e2190,null));};exports[_0xf645('0x27')]=function(_0x511b51,_0x5008b2){if(_0x511b51[_0xf645('0x2e')]['id']){delete _0x511b51[_0xf645('0x2e')]['id'];}return db['ChatInteraction'][_0xf645('0x4a')]({'where':{'id':_0x511b51[_0xf645('0x3d')]['id']}})[_0xf645('0x15')](handleEntityNotFound(_0x5008b2,null))['then'](saveUpdates(_0x511b51[_0xf645('0x2e')],null))[_0xf645('0x15')](respondWithResult(_0x5008b2,null))['catch'](handleError(_0x5008b2,null));};exports[_0xf645('0x28')]=function(_0x4280d4,_0x4a4b48){return db['ChatInteraction'][_0xf645('0x4a')]({'where':{'id':_0x4280d4[_0xf645('0x3d')]['id']}})[_0xf645('0x15')](handleEntityNotFound(_0x4a4b48,null))[_0xf645('0x15')](removeEntity(_0x4a4b48,null))[_0xf645('0x20')](handleError(_0x4a4b48,null));};exports[_0xf645('0x4c')]=function(_0x142518,_0x4fb3dd){return db[_0xf645('0x40')]['describe']()[_0xf645('0x15')](respondWithResult(_0x4fb3dd,null))['catch'](handleError(_0x4fb3dd,null));};exports[_0xf645('0x4d')]=function(_0x4063fe,_0x37c949){var _0x2e62c4=_0x4063fe[_0xf645('0x3d')]['id'];var _0x1bd333;var _0xb02d4c;var _0xdac084;return db[_0xf645('0x40')][_0xf645('0x4e')]({'where':{'id':_0x2e62c4}})[_0xf645('0x15')](function(_0x26b9ff){_0x1bd333=_0x26b9ff;return db[_0xf645('0x4f')][_0xf645('0x4e')]({'where':{'id':_0x26b9ff[_0xf645('0x50')]},'attributes':['id',_0xf645('0x51'),'vidaooEscalation','vidaooTopic',_0xf645('0x52'),'vidaooMetadata'],'raw':!![]});})[_0xf645('0x15')](function(_0xd691ed){if(_0xd691ed&&_0xd691ed['vidaooApiKey']&&_0xd691ed[_0xf645('0x53')]){_0xb02d4c=_0xd691ed;var _0x484db6={'interaction':{'id':_0x2e62c4},'agent':{'id':_0x4063fe[_0xf645('0x3b')]['id'],'name':_0x4063fe[_0xf645('0x3b')]['name'],'fullname':_0x4063fe[_0xf645('0x3b')][_0xf645('0x54')],'email':_0x4063fe[_0xf645('0x3b')][_0xf645('0x55')]},'contact':_0x4063fe['body'][_0xf645('0x2f')],'account':_0x4063fe[_0xf645('0x2e')][_0xf645('0x32')]};var _0x441a79={'rejectUnauthorized':![],'method':_0xf645('0x56'),'url':_0xf645('0x57'),'headers':{'X-APIKEY':_0xd691ed[_0xf645('0x51')]},'body':{'duration':null,'scheduledAt':null,'participants':[_0x4063fe['user'][_0xf645('0x55')]],'buttons':null,'topic':_0xb02d4c[_0xf645('0x58')],'note':_[_0xf645('0x59')](_0xb02d4c[_0xf645('0x52')])?null:getMustacheRender(_0xb02d4c[_0xf645('0x52')],_0x484db6),'metadata':null},'redirect':_0xf645('0x5a'),'json':!![]};try{if(!_['isNull'](_0xb02d4c[_0xf645('0x5b')])){var _0x3d43b4=getMustacheRender(_0xb02d4c[_0xf645('0x5b')],_0x484db6);var _0x5ae207=JSON[_0xf645('0x5c')](_0x3d43b4);_0x441a79['body'][_0xf645('0x5d')]=_0x5ae207;}}catch(_0xa5a78d){throw new db[(_0xf645('0x5e'))]['ValidationError'](_0xf645('0x5f'));}return rp(_0x441a79);}else{throw new db['Sequelize'][(_0xf645('0x60'))](_0xf645('0x61'));}})[_0xf645('0x15')](function(_0x2c018a){_0xdac084=_0x2c018a;if(_0xdac084['id']){var _0x221e1b={'body':_0xdac084[_0xf645('0x62')],'ChatWebsiteId':_0xb02d4c['id'],'ChatInteractionId':_0x1bd333['id'],'direction':_0xf645('0x63'),'UserId':_0x4063fe[_0xf645('0x3b')]['id']};return db[_0xf645('0x64')][_0xf645('0x4b')](_0x221e1b,{});}else{throw new db[(_0xf645('0x5e'))][(_0xf645('0x60'))](_0xf645('0x65'));}})['then'](function(){return _0x1bd333['updateAttributes']({'vidaooSessionId':_0xdac084['id']});})[_0xf645('0x15')](respondWithResult(_0x37c949,null))[_0xf645('0x20')](handleError(_0x37c949,null));};exports[_0xf645('0x66')]=function(_0x15b4b5,_0x5a4247){if(_0x15b4b5[_0xf645('0x2e')]['id']){delete _0x15b4b5[_0xf645('0x2e')]['id'];}return db[_0xf645('0x40')][_0xf645('0x4a')]({'where':{'id':_0x15b4b5[_0xf645('0x3d')]['id']},'include':[{'model':db['CmContact'],'as':_0xf645('0x67')}]})['then'](handleEntityNotFound(_0x5a4247,null))[_0xf645('0x15')](saveUpdates(_0x15b4b5['body'],null))['then'](respondWithResult(_0x5a4247,null))['catch'](handleError(_0x5a4247,null));};exports[_0xf645('0x68')]=function(_0x3efb83,_0x50e28a){if(_0x3efb83[_0xf645('0x2e')]['id']){delete _0x3efb83['body']['id'];}return db[_0xf645('0x40')][_0xf645('0x4a')]({'where':{'id':_0x3efb83[_0xf645('0x3d')]['id']}})[_0xf645('0x15')](function(_0x2b4a69){if(!_0x2b4a69){logger[_0xf645('0x1b')](_0xf645('0x69'),_0xf645('0x68'),0x194,_0xf645('0x6a'));_0x50e28a[_0xf645('0x21')](0x194);}else{if(_0x2b4a69[_0xf645('0x6b')]){throw new db['Sequelize'][(_0xf645('0x60'))](_0xf645('0x6c'));}}return _0x2b4a69;})[_0xf645('0x15')](function(_0x5d607b){if(_0x5d607b){return _0x5d607b[_0xf645('0x27')]({'closed':_0x3efb83['body'][_0xf645('0x6d')],'disposition':_0x3efb83[_0xf645('0x2e')][_0xf645('0x6e')]})[_0xf645('0x15')](function(_0x4b2b2a){return _0x4b2b2a;});}})[_0xf645('0x15')](function(){return respondWithRpcPromise(_0xf645('0x6f'),'abandon',_0x3efb83[_0xf645('0x2e')],client9004);})[_0xf645('0x15')](respondWithResult(_0x50e28a,null))[_0xf645('0x20')](handleError(_0x50e28a,null));};exports[_0xf645('0x70')]=function(_0xde7024,_0x112ba9){_0xde7024['body'][_0xf645('0x2a')]=_0xde7024[_0xf645('0x71')][_0xf645('0x72')];_0xde7024[_0xf645('0x2e')]['basename']=_0xde7024['file'][_0xf645('0x73')];_0xde7024['body'][_0xf645('0x36')]=_0xde7024['file'][_0xf645('0x74')];return db[_0xf645('0x75')][_0xf645('0x4b')](_0xde7024['body'])[_0xf645('0x15')](respondWithResult(_0x112ba9,0xc9))[_0xf645('0x20')](handleError(_0x112ba9,null));};exports[_0xf645('0x76')]=function(_0x200a0e,_0x21fc34){return db['ChatInteraction'][_0xf645('0x4e')]({'where':{'id':_0x200a0e[_0xf645('0x3d')]['id']},'raw':!![]})['then'](handleEntityNotFound(_0x21fc34,null))['then'](function(_0x4e9bd1){if(_0x4e9bd1){if(_0x4e9bd1[_0xf645('0x6d')]){return _0x21fc34['status'](0x193)[_0xf645('0x77')]({'status':0x193,'message':_0xf645('0x78')});}return db[_0xf645('0x75')][_0xf645('0x4e')]({'where':{'id':_0x200a0e[_0xf645('0x3a')][_0xf645('0x79')]},'raw':!![]})[_0xf645('0x15')](handleEntityNotFound(_0x21fc34,null));}})[_0xf645('0x15')](function(_0x574537){if(_0x574537){var _0x43d912=_0x574537['ChatOfflineMessageId']!==null;var _0xcd0df4=path[_0xf645('0x7a')](config['root'],_0xf645('0x7b'));if(_0x43d912){_0xcd0df4=path[_0xf645('0x7a')](_0xcd0df4,_0xf645('0x7c'));}var _0x44bc61=path['join'](_0xcd0df4,_0x574537[_0xf645('0x7d')]);return _0x21fc34[_0xf645('0x7e')](_0x44bc61);}})['catch'](handleError(_0x21fc34,null));};exports[_0xf645('0x7f')]=function(_0x7a472c,_0x18bb0a){var _0x5818c0=null;var _0x1d7ac7=null;var _0x4f9aa9=null;if(_0x7a472c[_0xf645('0x2e')]['id']){delete _0x7a472c[_0xf645('0x2e')]['id'];}if(_0x7a472c[_0xf645('0x2e')][_0xf645('0x80')]){_0x4f9aa9=path[_0xf645('0x7a')](config['root'],_0xf645('0x81'),util['format'](_0xf645('0x82'),Date[_0xf645('0x83')]()));_0x7a472c[_0xf645('0x2e')][_0xf645('0x84')]=_0x4f9aa9;}return db['ChatInteraction'][_0xf645('0x4a')]({'where':{'id':_0x7a472c[_0xf645('0x3d')]['id']},'include':[{'model':db[_0xf645('0x64')],'as':_0xf645('0x85'),'include':[{'model':db[_0xf645('0x86')],'as':'User'}]}]})['then'](handleEntityNotFound(_0x18bb0a,null))['then'](saveUpdates(_0x7a472c[_0xf645('0x2e')],null))[_0xf645('0x15')](function(_0x2c7f53){if(_0x2c7f53){_0x5818c0=_0x2c7f53;return db[_0xf645('0x4f')]['find']({'where':{'id':_0x5818c0[_0xf645('0x50')]}});}})[_0xf645('0x15')](function(_0x37653c){if(_0x37653c){_0x1d7ac7=_0x37653c;return db['CmContact'][_0xf645('0x4a')]({'where':{'id':_0x5818c0[_0xf645('0x87')]}});}})['then'](function(_0x515cef){if(_0x515cef){if(_0x4f9aa9){var _0x12b369=null;for(var _0x2cd961=0x1;_0x2cd961<_0x5818c0['Messages'][_0xf645('0x46')];++_0x2cd961){_0x12b369=_0x5818c0['Messages'][_0x2cd961];var _0x1f8811=_0x12b369[_0xf645('0x6b')]?util[_0xf645('0x88')](_0xf645('0x89'),_0x1d7ac7[_0xf645('0x8a')],_0x12b369[_0xf645('0x6b')]):'System';var _0x27b53a=_0x7a472c['body'][_0xf645('0x8b')]||util[_0xf645('0x88')]('%s%d',_0xf645('0x8c'),_0x12b369[_0xf645('0x87')]);switch(_0x1d7ac7[_0xf645('0x8d')]){case _0xf645('0x8e'):_0x1f8811=_0x12b369['UserId']?util[_0xf645('0x88')](_0xf645('0x89'),_0x1d7ac7[_0xf645('0x8a')],_0x12b369[_0xf645('0x6b')]):'System';break;case _0xf645('0x8f'):_0x1f8811=_0x12b369[_0xf645('0x6b')]?_0x12b369[_0xf645('0x86')][_0xf645('0x90')]||_0x12b369[_0xf645('0x86')][_0xf645('0x54')]:'System';break;case _0xf645('0x91'):_0x1f8811=_0x12b369[_0xf645('0x6b')]?_0x12b369['User'][_0xf645('0x54')]:_0xf645('0x92');break;default:_0x1f8811=_0x12b369[_0xf645('0x6b')]?util[_0xf645('0x88')](_0xf645('0x89'),_0x1d7ac7[_0xf645('0x8a')],_0x12b369[_0xf645('0x6b')]):_0xf645('0x92');}var _0x50d808=util[_0xf645('0x88')](_0xf645('0x93'),moment(_0x12b369[_0xf645('0x94')])[_0xf645('0x88')]('MM/DD/YYYY\x20HH:mm:ss'),_0x12b369['direction']==='in'?_0x27b53a:_0x1f8811,_0x12b369[_0xf645('0x2e')]);if(!_0x12b369['secret']){fs[_0xf645('0x95')](_0x4f9aa9,_0x50d808);}}return db[_0xf645('0x96')][_0xf645('0x4e')]({'where':{'service':!![]},'include':[{'model':db['MailServerOut'],'as':_0xf645('0x97'),'raw':!![],'include':[{'model':db['CloudProvider'],'as':_0xf645('0x98'),'raw':!![]}]}]});}}})[_0xf645('0x15')](function(_0x2cb5d9){if(!_0x2cb5d9||!_0x2cb5d9[_0xf645('0x97')]||!_0x7a472c[_0xf645('0x2e')])return _0x5818c0;var _0x2298ae=_0x2cb5d9[_0xf645('0x99')]();return respondWithRpcPromise(_0xf645('0x9a'),_0xf645('0x9b'),{'account':_0x2298ae,'message':{'from':util[_0xf645('0x88')](_0xf645('0x9c'),_0x2cb5d9['name'],_0x2cb5d9['email']||_0x2cb5d9['Smtp'][_0xf645('0x3b')]),'to':_0x7a472c[_0xf645('0x2e')][_0xf645('0x80')],'subject':_0xf645('0x9d'),'text':util[_0xf645('0x88')](_0xf645('0x9e'),_0x7a472c[_0xf645('0x2e')][_0xf645('0x80')]),'attachments':[{'path':_0x4f9aa9}]}})['then'](function(){return _0x5818c0;})[_0xf645('0x20')](function(_0x36e053){logger[_0xf645('0x1b')](_0x36e053);});})[_0xf645('0x15')](respondWithStatusCode(_0x18bb0a,0xc8))[_0xf645('0x20')](handleError(_0x18bb0a,null));};exports[_0xf645('0x9f')]=function(_0xe4e6f7,_0x394c11){return db[_0xf645('0x64')][_0xf645('0x4a')]({'where':{'id':_0xe4e6f7['params']['id']}})[_0xf645('0x15')](handleEntityNotFound(_0x394c11,null))['then'](function(_0x1a0ba5){if(_0x1a0ba5){return _0x1a0ba5[_0xf645('0x9f')](_0xe4e6f7[_0xf645('0x2e')][_0xf645('0xa0')],_[_0xf645('0xa1')](_0xe4e6f7[_0xf645('0x2e')],[_0xf645('0xa0'),'id'])||{});}})[_0xf645('0x15')](respondWithResult(_0x394c11,null))[_0xf645('0x20')](handleError(_0x394c11,null));};exports[_0xf645('0xa2')]=function(_0x39ade1,_0x1c034b){var _0x410dba={'raw':![],'where':{}};var _0x24e6f={};var _0x5a6cb3={'count':0x0,'rows':[]};return db[_0xf645('0x40')][_0xf645('0x4e')]({'where':{'id':_0x39ade1[_0xf645('0x3d')]['id']}})[_0xf645('0x15')](handleEntityNotFound(_0x1c034b,null))[_0xf645('0x15')](function(_0x188057){if(_0x188057){_0x24e6f[_0xf645('0x3e')]=_[_0xf645('0x3f')](db[_0xf645('0x64')]['rawAttributes']);_0x24e6f[_0xf645('0x3a')]=_[_0xf645('0x3f')](_0x39ade1['query']);_0x24e6f[_0xf645('0x42')]=_[_0xf645('0x43')](_0x24e6f[_0xf645('0x3e')],_0x24e6f[_0xf645('0x3a')]);_0x410dba['attributes']=_['intersection'](_0x24e6f['model'],qs['fields'](_0x39ade1[_0xf645('0x3a')]['fields']));_0x410dba['attributes']=_0x410dba[_0xf645('0x44')][_0xf645('0x46')]?_0x410dba['attributes']:_0x24e6f[_0xf645('0x3e')];if(!_0x39ade1['query'][_0xf645('0xa3')](_0xf645('0xa4'))){_0x410dba[_0xf645('0x25')]=qs['limit'](_0x39ade1[_0xf645('0x3a')][_0xf645('0x25')]);_0x410dba[_0xf645('0x23')]=qs[_0xf645('0x23')](_0x39ade1[_0xf645('0x3a')]['offset']);}_0x410dba[_0xf645('0xa5')]=qs['sort'](_0x39ade1[_0xf645('0x3a')][_0xf645('0xa6')]);_0x410dba[_0xf645('0xa7')]=qs['filters'](_[_0xf645('0xa8')](_0x39ade1['query'],_0x24e6f[_0xf645('0x42')]));_0x410dba['where']['ChatInteractionId']=_0x188057['id'];if(_0x39ade1[_0xf645('0x3a')][_0xf645('0xa9')]){_0x410dba[_0xf645('0xa7')]=_['merge'](_0x410dba[_0xf645('0xa7')],{'$or':_['map'](_0x410dba[_0xf645('0x44')],function(_0x553a36){var _0x254e38={};_0x254e38[_0x553a36]={'$like':'%'+_0x39ade1[_0xf645('0x3a')][_0xf645('0xa9')]+'%'};return _0x254e38;})});}if(_0x39ade1[_0xf645('0x3a')][_0xf645('0xaa')]){var _0x2873ac=_0x39ade1[_0xf645('0x3a')]['$gte'][_0xf645('0xab')](',');var _0x1b34be={};_0x1b34be[_0x2873ac[0x0]]={'$gte':moment(_0x2873ac[0x1])[_0xf645('0x88')](_0xf645('0xac'))};_0x410dba['where']=_[_0xf645('0x2d')](_0x410dba[_0xf645('0xa7')],_0x1b34be);}_0x410dba=_['merge']({},_0x410dba,_0x39ade1[_0xf645('0x49')]);return db['ChatMessage'][_0xf645('0xad')]({'where':_0x410dba[_0xf645('0xa7')]})[_0xf645('0x15')](function(_0xeab5e1){_0x5a6cb3[_0xf645('0xad')]=_0xeab5e1;if(_0x39ade1[_0xf645('0x3a')]['includeAll']){_0x410dba['include']=[{'all':!![]}];}return db[_0xf645('0x64')][_0xf645('0x38')](_0x410dba);})[_0xf645('0x15')](function(_0x4f3707){_0x5a6cb3['rows']=_0x4f3707;return _0x5a6cb3;});}})[_0xf645('0x15')](respondWithFilteredResult(_0x1c034b,_0x410dba))[_0xf645('0x20')](handleError(_0x1c034b,null));};exports['getMyMessages']=function(_0x6339c4,_0x2f38a){var _0xfcd360={'where':{}},_0x13d53c={},_0x29b982={'count':0x0,'rows':[]};return db[_0xf645('0x40')][_0xf645('0x4e')]({'where':{'id':_0x6339c4['params']['id']}})[_0xf645('0x15')](handleEntityNotFound(_0x2f38a,null))['then'](function(_0xe440e5){if(_0xe440e5){_0x13d53c[_0xf645('0x3e')]=_['keys'](db['ChatMessage']['rawAttributes']);_0x13d53c[_0xf645('0x3a')]=_[_0xf645('0x3f')](_0x6339c4[_0xf645('0x3a')]);_0x13d53c[_0xf645('0x42')]=_[_0xf645('0x43')](_0x13d53c[_0xf645('0x3e')],_0x13d53c[_0xf645('0x3a')]);_0xfcd360[_0xf645('0x44')]=_[_0xf645('0x43')](_0x13d53c[_0xf645('0x3e')],qs[_0xf645('0x45')](_0x6339c4[_0xf645('0x3a')][_0xf645('0x45')]));_0xfcd360[_0xf645('0x44')]=_0xfcd360[_0xf645('0x44')][_0xf645('0x46')]?_0xfcd360[_0xf645('0x44')]:_0x13d53c['model'];if(!_0x6339c4['query'][_0xf645('0xa3')](_0xf645('0xa4'))){_0xfcd360[_0xf645('0x25')]=qs['limit'](_0x6339c4['query']['limit']);_0xfcd360[_0xf645('0x23')]=qs['offset'](_0x6339c4[_0xf645('0x3a')][_0xf645('0x23')]);}_0xfcd360[_0xf645('0xa5')]=qs['sort'](_0x6339c4[_0xf645('0x3a')][_0xf645('0xa6')]);_0xfcd360[_0xf645('0xa7')]=qs[_0xf645('0x42')](_[_0xf645('0xa8')](_0x6339c4[_0xf645('0x3a')],_0x13d53c[_0xf645('0x42')]));_0xfcd360[_0xf645('0xa7')][_0xf645('0xae')]=_0xe440e5['id'];if(_0x6339c4[_0xf645('0x3a')][_0xf645('0xa9')]){_0xfcd360['where']=_[_0xf645('0x2d')](_0xfcd360[_0xf645('0xa7')],{'$or':_[_0xf645('0x34')](_0xfcd360['attributes'],function(_0x582fb7){var _0x3ba899={};_0x3ba899[_0x582fb7]={'$like':'%'+_0x6339c4['query'][_0xf645('0xa9')]+'%'};return _0x3ba899;})});}_0xfcd360[_0xf645('0xa7')]['secret']=![];if(_0x6339c4['query']['id']){_0xfcd360[_0xf645('0xa7')]['id']={'$gt':_0x6339c4[_0xf645('0x3a')]['id']};}_0xfcd360=_['merge']({},_0xfcd360,_0x6339c4[_0xf645('0x49')]);return db[_0xf645('0x64')][_0xf645('0xad')]({'where':_0xfcd360[_0xf645('0xa7')]})[_0xf645('0x15')](function(_0x4ac6d7){_0x29b982[_0xf645('0xad')]=_0x4ac6d7;_0xfcd360[_0xf645('0x48')]=[];if(_0x6339c4[_0xf645('0x3a')][_0xf645('0xaf')]){_0xfcd360[_0xf645('0x48')][_0xf645('0xb0')]({'model':db['User'],'as':_0xf645('0x86'),'attributes':['id','fullname',_0xf645('0x90')]});}if(_0x6339c4[_0xf645('0x3a')][_0xf645('0x47')]){_0xfcd360[_0xf645('0x48')]=[{'all':!![]}];}return db[_0xf645('0x64')]['findAll'](_0xfcd360);})[_0xf645('0x15')](function(_0x3b263c){_0x29b982['rows']=_0x3b263c;return _0x29b982;});}})[_0xf645('0x15')](respondWithFilteredResult(_0x2f38a,_0xfcd360))[_0xf645('0x20')](handleError(_0x2f38a,null));};exports[_0xf645('0xb1')]=function(_0x2e4714,_0x4e7d1c){return db[_0xf645('0x40')][_0xf645('0x4a')]({'where':{'id':_0x2e4714[_0xf645('0x3d')]['id']}})['then'](handleEntityNotFound(_0x4e7d1c,null))[_0xf645('0x15')](function(_0x3301bc){if(_0x3301bc){return _0x3301bc[_0xf645('0xb2')](_0x2e4714[_0xf645('0x2e')][_0xf645('0xa0')],_[_0xf645('0xa1')](_0x2e4714[_0xf645('0x2e')],[_0xf645('0xa0'),'id'])||{})[_0xf645('0xb3')](function(){return db[_0xf645('0xb4')][_0xf645('0x38')]({'attributes':['id','name',_0xf645('0xb5')],'where':{'id':_0x2e4714[_0xf645('0x2e')][_0xf645('0xa0')]}});})['then'](function(_0xd3c69d){socket[_0xf645('0xb6')](_0xf645('0xb7'),{'id':Number(_0x2e4714['params']['id']),'tags':_0xd3c69d||[]});return{'id':Number(_0x2e4714[_0xf645('0x3d')]['id']),'tags':_0xd3c69d||[]};});}})[_0xf645('0x15')](respondWithResult(_0x4e7d1c,null))[_0xf645('0x20')](handleError(_0x4e7d1c,null));};exports[_0xf645('0xb8')]=function(_0x28937c,_0x5b4a46){return db[_0xf645('0x40')][_0xf645('0x4a')]({'where':{'id':_0x28937c[_0xf645('0x3d')]['id']}})['then'](handleEntityNotFound(_0x5b4a46,null))[_0xf645('0x15')](function(_0x212a60){if(_0x212a60){return _0x212a60[_0xf645('0xb8')](_0x28937c[_0xf645('0x3a')][_0xf645('0xa0')]);}})[_0xf645('0x15')](respondWithStatusCode(_0x5b4a46,null))[_0xf645('0x20')](handleError(_0x5b4a46,null));};exports[_0xf645('0x7e')]=function(_0x43eaea,_0x1ebf31){var _0x56720f=moment()[_0xf645('0xb9')]()[_0xf645('0xba')]();var _0x26c0d5=path[_0xf645('0x7a')](config[_0xf645('0xbb')],'server',_0xf645('0xbc'),_0xf645('0xbd'));var _0x5e1a42=path['join'](config[_0xf645('0xbb')],_0xf645('0xbe'),_0xf645('0xbc'),_0xf645('0xbf'));var _0x20910c=path['join'](_0x26c0d5,_0x56720f);var _0x483b07=util[_0xf645('0x88')](_0xf645('0xc0'),_0x43eaea['params']['id'],_0x56720f);var _0x53ed3a=path[_0xf645('0x7a')](_0x26c0d5,_0x483b07);var _0x280a9c=[];_0x280a9c[_0xf645('0xb0')]({'model':db[_0xf645('0x86')],'as':_0xf645('0x86'),'attributes':['id',_0xf645('0x54')],'raw':!![]});_0x280a9c['push']({'model':db[_0xf645('0xc1')],'as':_0xf645('0x67'),'attributes':['id','firstName',_0xf645('0xc2')],'raw':!![]});if(_0x43eaea[_0xf645('0x3a')][_0xf645('0xbf')]){_0x280a9c['push']({'model':db[_0xf645('0x75')],'as':_0xf645('0x75'),'raw':!![]});}var _0xa36008=[{'model':db[_0xf645('0x64')],'as':'Messages','attributes':['id',_0xf645('0x2e'),_0xf645('0x94'),_0xf645('0xc3'),_0xf645('0xc4'),_0xf645('0xc5')],'include':_0x280a9c}];_0xa36008[_0xf645('0xb0')]({'model':db[_0xf645('0x4f')],'as':_0xf645('0xc6'),'attributes':['id',_0xf645('0x2a')]});_0xa36008['push']({'model':db['User'],'as':'Owner','attributes':['id',_0xf645('0x54')]});return db['ChatInteraction'][_0xf645('0x4a')]({'where':{'id':_0x43eaea[_0xf645('0x3d')]['id']},'include':_0xa36008})[_0xf645('0x15')](handleEntityNotFound(_0x1ebf31,null))[_0xf645('0x15')](function(_0x327e88){if(_0x327e88){var _0x2bd3a8=_0x327e88[_0xf645('0xc7')]({'plain':!![]});fs[_0xf645('0xc8')](_0x20910c);var _0x1e31ff={'channel':_0xf645('0xc9'),'account':_0x2bd3a8['Account'][_0xf645('0x2a')],'agent':_0x2bd3a8['Owner']?_0x2bd3a8['Owner'][_0xf645('0x54')]:_0xf645('0xca'),'createdAt':moment(_0x2bd3a8[_0xf645('0x94')])['format'](_0xf645('0xac'))['toString'](),'closedAt':_0x2bd3a8[_0xf645('0x6d')]?moment(_0x2bd3a8['closedAt'])[_0xf645('0x88')](_0xf645('0xac'))[_0xf645('0xba')]():'','disposition':_0x2bd3a8[_0xf645('0x6e')],'chatInfo':{'browser':_0x2bd3a8[_0xf645('0xcb')],'os':_0x2bd3a8[_0xf645('0xcc')],'referer':_0x2bd3a8[_0xf645('0xcd')],'customerIp':_0x2bd3a8[_0xf645('0xce')],'customerPort':_0x2bd3a8['customerPort']?_0x2bd3a8[_0xf645('0xcf')]:_0xf645('0xca')},'messages':_(_0x2bd3a8['Messages'])[_0xf645('0xd0')]()['filter']({'secret':![]})[_0xf645('0x34')](function(_0x381f16){if(_0x381f16['Attachment']){var _0x507eaf=path[_0xf645('0x7a')](_0x5e1a42,_0x381f16[_0xf645('0x75')][_0xf645('0x7d')]);if(fs[_0xf645('0xd1')](_0x507eaf)){fs[_0xf645('0xd2')](_0x507eaf)[_0xf645('0xd3')](fs[_0xf645('0xd4')](path['join'](_0x20910c,_0x381f16[_0xf645('0x75')][_0xf645('0x7d')])));}}return{'date':moment(_0x381f16[_0xf645('0x94')])[_0xf645('0x88')](_0xf645('0xac'))[_0xf645('0xba')](),'sender':_0x381f16[_0xf645('0xc3')]===_0xf645('0x63')?_0x381f16[_0xf645('0x86')]?_0x381f16[_0xf645('0x86')][_0xf645('0x54')]:_0xf645('0x92'):_0x381f16[_0xf645('0x67')][_0xf645('0xd5')]+(_0x381f16['Contact'][_0xf645('0xc2')]?'\x20'+_0x381f16['Contact']['lastName']:''),'body':_0x381f16[_0xf645('0x75')]?_0x381f16['Attachment'][_0xf645('0x7d')]:_0x381f16[_0xf645('0x2e')],'direction':_0x381f16[_0xf645('0xc3')]==='out'?'A':'C','secret':_0x381f16[_0xf645('0xc4')],'read':_0x381f16[_0xf645('0xc5')],'attachment':_0x381f16[_0xf645('0x75')]};})[_0xf645('0xd6')]()};return ejs['renderFile'](path[_0xf645('0x7a')](config[_0xf645('0xbb')],_0xf645('0xbe'),_0xf645('0xd7'),'downloadInteraction.ejs'),{'interaction':_0x1e31ff})[_0xf645('0x15')](function(_0xf3db3){var _0x4160bd=path[_0xf645('0x7a')](_0x20910c,_0xf645('0xd8')+_0x2bd3a8['id']+'-'+_0x56720f+'.pdf');var _0x837093={'path':_0x4160bd,'channel':'Chat','interactionId':_0x2bd3a8['id']};return pdf['createPdfFromHTML'](_0xf3db3,_0x837093);})[_0xf645('0x15')](function(){return new Promise(function(_0x320b00,_0x15a839){zipdir(_0x20910c,{'saveTo':_0x53ed3a},function(_0x38a8d2,_0x48e3bf){if(_0x38a8d2)return _0x15a839(_0x38a8d2);return _0x320b00(_0x48e3bf);});})['then'](function(){return new BPromise(function(_0x5e9ddd,_0x4c7840){rimraf(_0x20910c,function(_0x557ba8){if(_0x557ba8)_0x4c7840(_0x557ba8);return _0x5e9ddd();});});})[_0xf645('0x15')](function(){return _0x1ebf31[_0xf645('0x7e')](_0x53ed3a,_0x483b07,function(_0x2cedbe){if(_0x2cedbe){logger[_0xf645('0x1b')]('err',_0x2cedbe);}else{fs['unlinkSync'](_0x53ed3a);}});});});}else{return _0x1ebf31[_0xf645('0x21')](0xc8);}})['catch'](handleError(_0x1ebf31,null));};