Built motion from commit b0a9a629.|2.6.33
[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 _0x447e=['Error\x20in\x20conversion\x20vidaooMetadata','Error\x20in\x20Vidaoo\x20configuration','join_url','out','ChatMessage','Unable\x20to\x20create\x20Vidaoo\x20session','updateAttributes','customUpdate','abandon','abandonInteraction,\x20%s,\x20%s,\x20%s','entity\x20not\x20found','closed','disposition','AbandonInteraction','attachmentUpload','originalname','basename','file','type','mimetype','attachmentDownload','findOne','Unauthorized','Attachment','attachId','ChatOfflineMessageId','join','server/files/attachments/','offline-chat/','download','close','mailTranscript','root','server/files/chat/upload/','transcript-%s.txt','pathTranscript','User','CmContact','ContactId','Messages','format','%s%d','agentAlias','UserId','customer','Visitor','agentIdentifier','website_alias','System','alias','fullname','createdAt','MM/DD/YYYY\x20HH:mm:ss','direction','secret','MailAccount','MailServerOut','Smtp','CloudProvider','getSmtpOptions','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','ChatInteractionId','filter','$gte','split','rows','includeAgent','push','addTags','setTags','Tag','removeTags','toString','server','files','tmp','transcript-%d-%s.zip','Contact','firstName','attachments','read','Account','Owner','Chat','N.A.','YYYY-MM-DD\x20HH:mm:ss','closedAt','browserName','osName','referer','customerPort','tail','existsSync','createReadStream','createWriteStream','lastName','renderFile','views','downloadInteraction.ejs','transcript-','.pdf','createPdfFromHTML','err','unlinkSync','rimraf','zip-dir','request-promise','moment','bluebird','mustache','util','ejs','lodash','../../components/interaction/service','../../components/parsers/qs','../../components/export/pdf','../../config/logger','api','../../config/environment','../../mysqldb','redis','defaults','localhost','socket.io-emitter','./chatInteraction.socket','register','jayson/promise','client','http','request','then','info','ChatInteraction,\x20%s,\x20%s','debug','ChatInteraction,\x20%s,\x20%s,\x20%s','request\x20sent','stringify','error','message','result','catch','sendStatus','end','status','json','offset','undefined','limit','count','Content-Range','update','destroy','stack','name','send','render','merge','body','contact','agent','interaction','account','map','ChatInteraction','rawAttributes','fieldName','key','getOptions','findAll','chat','query','user','params','model','keys','filters','attributes','intersection','fields','length','includeAll','include','options','find','create','createVidaooSession','ChatWebsite','ChatWebsiteId','vidaooApiKey','vidaooEscalation','vidaooMetadata','email','https://api-vidaoo.xcally.com/api/v1/meetings','vidaooTopic','isNull','vidaooNote','follow','parse','metadata','Sequelize','ValidationError'];(function(_0x59ee6f,_0x450239){var _0x4c0979=function(_0x565e92){while(--_0x565e92){_0x59ee6f['push'](_0x59ee6f['shift']());}};_0x4c0979(++_0x450239);}(_0x447e,0x75));var _0xe447=function(_0x40b434,_0x1b71d8){_0x40b434=_0x40b434-0x0;var _0xbccbcc=_0x447e[_0x40b434];return _0xbccbcc;};'use strict';var rimraf=require(_0xe447('0x0'));var zipdir=require(_0xe447('0x1'));var rp=require(_0xe447('0x2'));var moment=require(_0xe447('0x3'));var BPromise=require(_0xe447('0x4'));var Mustache=require(_0xe447('0x5'));var util=require(_0xe447('0x6'));var path=require('path');var ejs=require(_0xe447('0x7'));var fs=require('fs');var _=require(_0xe447('0x8'));var Redis=require('ioredis');var interaction=require(_0xe447('0x9'));var qs=require(_0xe447('0xa'));var pdf=require(_0xe447('0xb'));var logger=require(_0xe447('0xc'))(_0xe447('0xd'));var config=require(_0xe447('0xe'));var db=require(_0xe447('0xf'))['db'];config[_0xe447('0x10')]=_[_0xe447('0x11')](config['redis'],{'host':_0xe447('0x12'),'port':0x18eb});var socket=require(_0xe447('0x13'))(new Redis(config[_0xe447('0x10')]));require(_0xe447('0x14'))[_0xe447('0x15')](socket);var jayson=require(_0xe447('0x16'));var client=jayson[_0xe447('0x17')][_0xe447('0x18')]({'port':0x232b});var client9004=jayson[_0xe447('0x17')]['http']({'port':0x232c});function respondWithRpcPromise(_0x498072,_0xf22741,_0x2a5383,_0x57ae3b){return new BPromise(function(_0x32f3ba,_0x14c77e){var _0x2f120e=_0x57ae3b||client;return _0x2f120e[_0xe447('0x19')](_0x498072,_0x2a5383)[_0xe447('0x1a')](function(_0x619e84){logger[_0xe447('0x1b')](_0xe447('0x1c'),_0xf22741,'request\x20sent');logger[_0xe447('0x1d')](_0xe447('0x1e'),_0xf22741,_0xe447('0x1f'),JSON[_0xe447('0x20')](_0x619e84));if(_0x619e84[_0xe447('0x21')]){if(_0x619e84[_0xe447('0x21')]['code']===0x1f4){logger['error'](_0xe447('0x1c'),_0xf22741,_0x619e84['error'][_0xe447('0x22')]);return _0x14c77e(_0x619e84[_0xe447('0x21')][_0xe447('0x22')]);}logger[_0xe447('0x21')](_0xe447('0x1c'),_0xf22741,_0x619e84[_0xe447('0x21')][_0xe447('0x22')]);return _0x32f3ba(_0x619e84[_0xe447('0x21')][_0xe447('0x22')]);}else{logger[_0xe447('0x1b')](_0xe447('0x1c'),_0xf22741,'request\x20sent');_0x32f3ba(_0x619e84[_0xe447('0x23')][_0xe447('0x22')]);}})[_0xe447('0x24')](function(_0x32c182){logger[_0xe447('0x21')](_0xe447('0x1c'),_0xf22741,_0x32c182);_0x14c77e(_0x32c182);});});}function respondWithStatusCode(_0x4fad17,_0x2ebd12){_0x2ebd12=_0x2ebd12||0xcc;return function(_0x9d499c){if(_0x9d499c){return _0x4fad17[_0xe447('0x25')](_0x2ebd12);}return _0x4fad17['status'](_0x2ebd12)[_0xe447('0x26')]();};}function respondWithResult(_0x54439e,_0x395a1a){_0x395a1a=_0x395a1a||0xc8;return function(_0x463fb2){if(_0x463fb2){return _0x54439e[_0xe447('0x27')](_0x395a1a)[_0xe447('0x28')](_0x463fb2);}};}function respondWithFilteredResult(_0x212ab7,_0xdaf354){return function(_0x1281fa){if(_0x1281fa){var _0x3d9f1f=typeof _0xdaf354[_0xe447('0x29')]===_0xe447('0x2a')&&typeof _0xdaf354[_0xe447('0x2b')]===_0xe447('0x2a');var _0x34019b=_0x1281fa[_0xe447('0x2c')];var _0x31deb1=_0x3d9f1f?0x0:_0xdaf354[_0xe447('0x29')];var _0x572b75=_0x3d9f1f?_0x1281fa[_0xe447('0x2c')]:_0xdaf354[_0xe447('0x29')]+_0xdaf354['limit'];var _0x5e1c15;if(_0x572b75>=_0x34019b){_0x572b75=_0x34019b;_0x5e1c15=0xc8;}else{_0x5e1c15=0xce;}_0x212ab7[_0xe447('0x27')](_0x5e1c15);return _0x212ab7['set'](_0xe447('0x2d'),_0x31deb1+'-'+_0x572b75+'/'+_0x34019b)[_0xe447('0x28')](_0x1281fa);}return null;};}function saveUpdates(_0x9d5f9f){return function(_0x2ea3d2){if(_0x2ea3d2){return _0x2ea3d2[_0xe447('0x2e')](_0x9d5f9f)[_0xe447('0x1a')](function(_0x1e52dd){return _0x1e52dd;});}return null;};}function removeEntity(_0x1dda62){return function(_0x3f2bd8){if(_0x3f2bd8){return _0x3f2bd8[_0xe447('0x2f')]()['then'](function(){_0x1dda62[_0xe447('0x27')](0xcc)['end']();});}};}function handleEntityNotFound(_0x1984bd){return function(_0x15568c){if(!_0x15568c){_0x1984bd[_0xe447('0x25')](0x194);}return _0x15568c;};}function handleError(_0x5e15a7,_0xe2d068){_0xe2d068=_0xe2d068||0x1f4;return function(_0x11c2e){logger[_0xe447('0x21')](_0x11c2e[_0xe447('0x30')]);if(_0x11c2e[_0xe447('0x31')]){delete _0x11c2e[_0xe447('0x31')];}_0x5e15a7[_0xe447('0x27')](_0xe2d068)[_0xe447('0x32')](_0x11c2e);};}function getMustacheRender(_0x3fd972,_0x46d588){return Mustache[_0xe447('0x33')](_0x3fd972,_[_0xe447('0x34')](_0x46d588[_0xe447('0x35')],{'contact':_0x46d588[_0xe447('0x36')],'message':_0x46d588[_0xe447('0x22')],'agent':_0x46d588[_0xe447('0x37')],'interaction':_0x46d588[_0xe447('0x38')],'account':_0x46d588[_0xe447('0x39')]}))||'';}exports['index']=function(_0x44e55b,_0x4cc3a2){var _0x5ce1b1={};var _0x505dde=_[_0xe447('0x3a')](db[_0xe447('0x3b')][_0xe447('0x3c')],function(_0x54bb40){return{'name':_0x54bb40[_0xe447('0x3d')],'type':_0x54bb40['type'][_0xe447('0x3e')]};});_0x5ce1b1=qs[_0xe447('0x3f')](_0x505dde,_0x44e55b);return interaction[_0xe447('0x40')](_0xe447('0x41'),_0x44e55b[_0xe447('0x42')],_0x44e55b[_0xe447('0x43')],_0x5ce1b1,_0x505dde)[_0xe447('0x1a')](respondWithFilteredResult(_0x4cc3a2,_0x5ce1b1))[_0xe447('0x24')](handleError(_0x4cc3a2,null));};exports['show']=function(_0x506530,_0x16f5f1){var _0x1e2577={'raw':![],'where':{'id':_0x506530[_0xe447('0x44')]['id']}},_0x78829e={};_0x78829e[_0xe447('0x45')]=_[_0xe447('0x46')](db['ChatInteraction'][_0xe447('0x3c')]);_0x78829e[_0xe447('0x42')]=_[_0xe447('0x46')](_0x506530[_0xe447('0x42')]);_0x78829e[_0xe447('0x47')]=_['intersection'](_0x78829e[_0xe447('0x45')],_0x78829e[_0xe447('0x42')]);_0x1e2577[_0xe447('0x48')]=_[_0xe447('0x49')](_0x78829e['model'],qs[_0xe447('0x4a')](_0x506530['query']['fields']));_0x1e2577['attributes']=_0x1e2577[_0xe447('0x48')][_0xe447('0x4b')]?_0x1e2577[_0xe447('0x48')]:_0x78829e[_0xe447('0x45')];if(_0x506530[_0xe447('0x42')][_0xe447('0x4c')]){_0x1e2577[_0xe447('0x4d')]=[{'all':!![]}];}_0x1e2577=_[_0xe447('0x34')]({},_0x1e2577,_0x506530[_0xe447('0x4e')]);return db['ChatInteraction'][_0xe447('0x4f')](_0x1e2577)[_0xe447('0x1a')](handleEntityNotFound(_0x16f5f1,null))[_0xe447('0x1a')](respondWithResult(_0x16f5f1,null))['catch'](handleError(_0x16f5f1,null));};exports[_0xe447('0x50')]=function(_0x265f52,_0x38c725){return db[_0xe447('0x3b')][_0xe447('0x50')](_0x265f52[_0xe447('0x35')],{})[_0xe447('0x1a')](respondWithResult(_0x38c725,0xc9))[_0xe447('0x24')](handleError(_0x38c725,null));};exports[_0xe447('0x2e')]=function(_0x39b2f0,_0x11f2a2){if(_0x39b2f0[_0xe447('0x35')]['id']){delete _0x39b2f0[_0xe447('0x35')]['id'];}return db['ChatInteraction'][_0xe447('0x4f')]({'where':{'id':_0x39b2f0[_0xe447('0x44')]['id']}})[_0xe447('0x1a')](handleEntityNotFound(_0x11f2a2,null))[_0xe447('0x1a')](saveUpdates(_0x39b2f0[_0xe447('0x35')],null))[_0xe447('0x1a')](respondWithResult(_0x11f2a2,null))[_0xe447('0x24')](handleError(_0x11f2a2,null));};exports[_0xe447('0x2f')]=function(_0x815bbe,_0x21dea4){return db[_0xe447('0x3b')][_0xe447('0x4f')]({'where':{'id':_0x815bbe[_0xe447('0x44')]['id']}})[_0xe447('0x1a')](handleEntityNotFound(_0x21dea4,null))[_0xe447('0x1a')](removeEntity(_0x21dea4,null))[_0xe447('0x24')](handleError(_0x21dea4,null));};exports['describe']=function(_0x55be9b,_0x7314a5){return db[_0xe447('0x3b')]['describe']()[_0xe447('0x1a')](respondWithResult(_0x7314a5,null))[_0xe447('0x24')](handleError(_0x7314a5,null));};exports[_0xe447('0x51')]=function(_0x57d327,_0x289a65){var _0x5e96e2=_0x57d327['params']['id'];var _0x1af8b0;var _0x170d70;var _0x41d810;return db[_0xe447('0x3b')]['findOne']({'where':{'id':_0x5e96e2}})[_0xe447('0x1a')](function(_0x5e4804){_0x1af8b0=_0x5e4804;return db[_0xe447('0x52')]['findOne']({'where':{'id':_0x5e4804[_0xe447('0x53')]},'attributes':['id',_0xe447('0x54'),_0xe447('0x55'),'vidaooTopic','vidaooNote',_0xe447('0x56')],'raw':!![]});})[_0xe447('0x1a')](function(_0x4f919f){if(_0x4f919f&&_0x4f919f[_0xe447('0x54')]&&_0x4f919f['vidaooEscalation']){_0x170d70=_0x4f919f;var _0x138e28={'interaction':{'id':_0x5e96e2},'agent':{'id':_0x57d327[_0xe447('0x43')]['id'],'name':_0x57d327[_0xe447('0x43')][_0xe447('0x31')],'fullname':_0x57d327[_0xe447('0x43')]['fullname'],'email':_0x57d327[_0xe447('0x43')][_0xe447('0x57')]},'contact':_0x57d327[_0xe447('0x35')][_0xe447('0x36')],'account':_0x57d327[_0xe447('0x35')]['account']};var _0x460cd2={'rejectUnauthorized':![],'method':'POST','url':_0xe447('0x58'),'headers':{'X-APIKEY':_0x4f919f[_0xe447('0x54')]},'body':{'duration':null,'scheduledAt':null,'participants':[_0x57d327[_0xe447('0x43')]['email']],'buttons':null,'topic':_0x170d70[_0xe447('0x59')],'note':_[_0xe447('0x5a')](_0x170d70[_0xe447('0x5b')])?null:getMustacheRender(_0x170d70[_0xe447('0x5b')],_0x138e28),'metadata':null},'redirect':_0xe447('0x5c'),'json':!![]};try{if(!_[_0xe447('0x5a')](_0x170d70['vidaooMetadata'])){var _0x4d3b89=getMustacheRender(_0x170d70[_0xe447('0x56')],_0x138e28);var _0x4d10b6=JSON[_0xe447('0x5d')](_0x4d3b89);_0x460cd2['body'][_0xe447('0x5e')]=_0x4d10b6;}}catch(_0x47256f){throw new db[(_0xe447('0x5f'))][(_0xe447('0x60'))](_0xe447('0x61'));}return rp(_0x460cd2);}else{throw new db[(_0xe447('0x5f'))][(_0xe447('0x60'))](_0xe447('0x62'));}})['then'](function(_0x4908e6){_0x41d810=_0x4908e6;if(_0x41d810['id']){var _0x18a719={'body':_0x41d810[_0xe447('0x63')],'ChatWebsiteId':_0x170d70['id'],'ChatInteractionId':_0x1af8b0['id'],'direction':_0xe447('0x64'),'UserId':_0x57d327[_0xe447('0x43')]['id']};return db[_0xe447('0x65')]['create'](_0x18a719,{});}else{throw new db[(_0xe447('0x5f'))][(_0xe447('0x60'))](_0xe447('0x66'));}})[_0xe447('0x1a')](function(){return _0x1af8b0[_0xe447('0x67')]({'vidaooSessionId':_0x41d810['id']});})[_0xe447('0x1a')](respondWithResult(_0x289a65,null))['catch'](handleError(_0x289a65,null));};exports[_0xe447('0x68')]=function(_0x3affa2,_0x1c2fad){if(_0x3affa2[_0xe447('0x35')]['id']){delete _0x3affa2[_0xe447('0x35')]['id'];}return db[_0xe447('0x3b')]['find']({'where':{'id':_0x3affa2[_0xe447('0x44')]['id']},'include':[{'model':db['CmContact'],'as':'Contact'}]})[_0xe447('0x1a')](handleEntityNotFound(_0x1c2fad,null))[_0xe447('0x1a')](saveUpdates(_0x3affa2['body'],null))[_0xe447('0x1a')](respondWithResult(_0x1c2fad,null))[_0xe447('0x24')](handleError(_0x1c2fad,null));};exports[_0xe447('0x69')]=function(_0x3ec99c,_0x2964e6){if(_0x3ec99c[_0xe447('0x35')]['id']){delete _0x3ec99c[_0xe447('0x35')]['id'];}return db['ChatInteraction'][_0xe447('0x4f')]({'where':{'id':_0x3ec99c[_0xe447('0x44')]['id']}})[_0xe447('0x1a')](function(_0xb082ea){if(!_0xb082ea){logger[_0xe447('0x21')](_0xe447('0x6a'),_0xe447('0x69'),0x194,_0xe447('0x6b'));_0x2964e6['sendStatus'](0x194);}else{if(_0xb082ea['UserId']){throw new db[(_0xe447('0x5f'))][(_0xe447('0x60'))]('Interaction\x20already\x20assigned');}}return _0xb082ea;})[_0xe447('0x1a')](function(_0x1c4224){if(_0x1c4224){return _0x1c4224[_0xe447('0x2e')]({'closed':_0x3ec99c[_0xe447('0x35')][_0xe447('0x6c')],'disposition':_0x3ec99c[_0xe447('0x35')][_0xe447('0x6d')]})['then'](function(_0x5711b1){return _0x5711b1;});}})['then'](function(){return respondWithRpcPromise(_0xe447('0x6e'),'abandon',_0x3ec99c['body'],client9004);})[_0xe447('0x1a')](respondWithResult(_0x2964e6,null))[_0xe447('0x24')](handleError(_0x2964e6,null));};exports[_0xe447('0x6f')]=function(_0x1dd4a1,_0xe3095b){_0x1dd4a1[_0xe447('0x35')][_0xe447('0x31')]=_0x1dd4a1['file'][_0xe447('0x70')];_0x1dd4a1[_0xe447('0x35')][_0xe447('0x71')]=_0x1dd4a1[_0xe447('0x72')]['filename'];_0x1dd4a1[_0xe447('0x35')][_0xe447('0x73')]=_0x1dd4a1[_0xe447('0x72')][_0xe447('0x74')];return db['Attachment'][_0xe447('0x50')](_0x1dd4a1[_0xe447('0x35')])[_0xe447('0x1a')](respondWithResult(_0xe3095b,0xc9))[_0xe447('0x24')](handleError(_0xe3095b,null));};exports[_0xe447('0x75')]=function(_0xd56a0,_0x2fa340){return db[_0xe447('0x3b')][_0xe447('0x76')]({'where':{'id':_0xd56a0[_0xe447('0x44')]['id']},'raw':!![]})[_0xe447('0x1a')](handleEntityNotFound(_0x2fa340,null))[_0xe447('0x1a')](function(_0x21ee05){if(_0x21ee05){if(_0x21ee05['closed']){return _0x2fa340[_0xe447('0x27')](0x193)['json']({'status':0x193,'message':_0xe447('0x77')});}return db[_0xe447('0x78')][_0xe447('0x76')]({'where':{'id':_0xd56a0[_0xe447('0x42')][_0xe447('0x79')]},'raw':!![]})[_0xe447('0x1a')](handleEntityNotFound(_0x2fa340,null));}})[_0xe447('0x1a')](function(_0x23e649){if(_0x23e649){var _0x2daa1e=_0x23e649[_0xe447('0x7a')]!==null;var _0x596e11=path[_0xe447('0x7b')](config['root'],_0xe447('0x7c'));if(_0x2daa1e){_0x596e11=path[_0xe447('0x7b')](_0x596e11,_0xe447('0x7d'));}var _0x363314=path['join'](_0x596e11,_0x23e649['basename']);return _0x2fa340[_0xe447('0x7e')](_0x363314);}})[_0xe447('0x24')](handleError(_0x2fa340,null));};exports[_0xe447('0x7f')]=function(_0x126bcb,_0xf1ad6e){var _0x105e04=null;var _0x117176=null;var _0x34e426=null;if(_0x126bcb[_0xe447('0x35')]['id']){delete _0x126bcb['body']['id'];}if(_0x126bcb[_0xe447('0x35')][_0xe447('0x80')]){_0x34e426=path['join'](config[_0xe447('0x81')],_0xe447('0x82'),util['format'](_0xe447('0x83'),Date['now']()));_0x126bcb['body'][_0xe447('0x84')]=_0x34e426;}return db[_0xe447('0x3b')][_0xe447('0x4f')]({'where':{'id':_0x126bcb[_0xe447('0x44')]['id']},'include':[{'model':db[_0xe447('0x65')],'as':'Messages','include':[{'model':db['User'],'as':_0xe447('0x85')}]}]})['then'](handleEntityNotFound(_0xf1ad6e,null))[_0xe447('0x1a')](saveUpdates(_0x126bcb['body'],null))[_0xe447('0x1a')](function(_0x53670e){if(_0x53670e){_0x105e04=_0x53670e;return db[_0xe447('0x52')][_0xe447('0x4f')]({'where':{'id':_0x105e04[_0xe447('0x53')]}});}})[_0xe447('0x1a')](function(_0x24a92e){if(_0x24a92e){_0x117176=_0x24a92e;return db[_0xe447('0x86')][_0xe447('0x4f')]({'where':{'id':_0x105e04[_0xe447('0x87')]}});}})[_0xe447('0x1a')](function(_0x166a8c){if(_0x166a8c){if(_0x34e426){var _0x1f8a1f=null;for(var _0x972d28=0x1;_0x972d28<_0x105e04['Messages'][_0xe447('0x4b')];++_0x972d28){_0x1f8a1f=_0x105e04[_0xe447('0x88')][_0x972d28];var _0x22f395=_0x1f8a1f['UserId']?util[_0xe447('0x89')](_0xe447('0x8a'),_0x117176[_0xe447('0x8b')],_0x1f8a1f[_0xe447('0x8c')]):'System';var _0x3bd527=_0x126bcb[_0xe447('0x35')][_0xe447('0x8d')]||util[_0xe447('0x89')](_0xe447('0x8a'),_0xe447('0x8e'),_0x1f8a1f[_0xe447('0x87')]);switch(_0x117176[_0xe447('0x8f')]){case _0xe447('0x90'):_0x22f395=_0x1f8a1f[_0xe447('0x8c')]?util[_0xe447('0x89')](_0xe447('0x8a'),_0x117176[_0xe447('0x8b')],_0x1f8a1f[_0xe447('0x8c')]):_0xe447('0x91');break;case'agent_alias':_0x22f395=_0x1f8a1f['UserId']?_0x1f8a1f[_0xe447('0x85')][_0xe447('0x92')]||_0x1f8a1f['User'][_0xe447('0x93')]:'System';break;case'agent_fullname':_0x22f395=_0x1f8a1f[_0xe447('0x8c')]?_0x1f8a1f[_0xe447('0x85')]['fullname']:_0xe447('0x91');break;default:_0x22f395=_0x1f8a1f[_0xe447('0x8c')]?util['format'](_0xe447('0x8a'),_0x117176[_0xe447('0x8b')],_0x1f8a1f['UserId']):'System';}var _0x364c85=util['format']('[%s]\x20%s:\x20%s\x0a',moment(_0x1f8a1f[_0xe447('0x94')])[_0xe447('0x89')](_0xe447('0x95')),_0x1f8a1f[_0xe447('0x96')]==='in'?_0x3bd527:_0x22f395,_0x1f8a1f[_0xe447('0x35')]);if(!_0x1f8a1f[_0xe447('0x97')]){fs['appendFileSync'](_0x34e426,_0x364c85);}}return db[_0xe447('0x98')][_0xe447('0x76')]({'where':{'service':!![]},'include':[{'model':db[_0xe447('0x99')],'as':_0xe447('0x9a'),'raw':!![],'include':[{'model':db[_0xe447('0x9b')],'as':_0xe447('0x9b'),'raw':!![]}]}]});}}})['then'](function(_0x4d1c8c){if(!_0x4d1c8c||!_0x4d1c8c[_0xe447('0x9a')]||!_0x126bcb[_0xe447('0x35')])return _0x105e04;var _0xb69fbc=_0x4d1c8c[_0xe447('0x9c')]();return respondWithRpcPromise('SendMail',_0xe447('0x9d'),{'account':_0xb69fbc,'message':{'from':util[_0xe447('0x89')](_0xe447('0x9e'),_0x4d1c8c['name'],_0x4d1c8c[_0xe447('0x57')]||_0x4d1c8c['Smtp'][_0xe447('0x43')]),'to':_0x126bcb['body'][_0xe447('0x80')],'subject':_0xe447('0x9f'),'text':util[_0xe447('0x89')](_0xe447('0xa0'),_0x126bcb[_0xe447('0x35')][_0xe447('0x80')]),'attachments':[{'path':_0x34e426}]}})[_0xe447('0x1a')](function(){return _0x105e04;})[_0xe447('0x24')](function(_0x2f0158){logger[_0xe447('0x21')](_0x2f0158);});})[_0xe447('0x1a')](respondWithStatusCode(_0xf1ad6e,0xc8))[_0xe447('0x24')](handleError(_0xf1ad6e,null));};exports[_0xe447('0xa1')]=function(_0x2d962b,_0x26f99c){return db[_0xe447('0x65')]['find']({'where':{'id':_0x2d962b[_0xe447('0x44')]['id']}})[_0xe447('0x1a')](handleEntityNotFound(_0x26f99c,null))[_0xe447('0x1a')](function(_0x689e93){if(_0x689e93){return _0x689e93[_0xe447('0xa1')](_0x2d962b[_0xe447('0x35')][_0xe447('0xa2')],_[_0xe447('0xa3')](_0x2d962b[_0xe447('0x35')],[_0xe447('0xa2'),'id'])||{});}})[_0xe447('0x1a')](respondWithResult(_0x26f99c,null))[_0xe447('0x24')](handleError(_0x26f99c,null));};exports[_0xe447('0xa4')]=function(_0x808e5e,_0x2e3f77){var _0x5cf7ba={'raw':![],'where':{}};var _0x3c1d05={};var _0x22928b={'count':0x0,'rows':[]};return db[_0xe447('0x3b')]['findOne']({'where':{'id':_0x808e5e[_0xe447('0x44')]['id']}})['then'](handleEntityNotFound(_0x2e3f77,null))[_0xe447('0x1a')](function(_0x4ca406){if(_0x4ca406){_0x3c1d05[_0xe447('0x45')]=_['keys'](db[_0xe447('0x65')][_0xe447('0x3c')]);_0x3c1d05[_0xe447('0x42')]=_[_0xe447('0x46')](_0x808e5e['query']);_0x3c1d05[_0xe447('0x47')]=_[_0xe447('0x49')](_0x3c1d05['model'],_0x3c1d05[_0xe447('0x42')]);_0x5cf7ba[_0xe447('0x48')]=_[_0xe447('0x49')](_0x3c1d05['model'],qs[_0xe447('0x4a')](_0x808e5e['query']['fields']));_0x5cf7ba[_0xe447('0x48')]=_0x5cf7ba[_0xe447('0x48')][_0xe447('0x4b')]?_0x5cf7ba[_0xe447('0x48')]:_0x3c1d05[_0xe447('0x45')];if(!_0x808e5e[_0xe447('0x42')][_0xe447('0xa5')](_0xe447('0xa6'))){_0x5cf7ba['limit']=qs[_0xe447('0x2b')](_0x808e5e[_0xe447('0x42')][_0xe447('0x2b')]);_0x5cf7ba[_0xe447('0x29')]=qs[_0xe447('0x29')](_0x808e5e[_0xe447('0x42')][_0xe447('0x29')]);}_0x5cf7ba[_0xe447('0xa7')]=qs[_0xe447('0xa8')](_0x808e5e[_0xe447('0x42')][_0xe447('0xa8')]);_0x5cf7ba[_0xe447('0xa9')]=qs[_0xe447('0x47')](_[_0xe447('0xaa')](_0x808e5e[_0xe447('0x42')],_0x3c1d05[_0xe447('0x47')]));_0x5cf7ba[_0xe447('0xa9')][_0xe447('0xab')]=_0x4ca406['id'];if(_0x808e5e[_0xe447('0x42')][_0xe447('0xac')]){_0x5cf7ba[_0xe447('0xa9')]=_[_0xe447('0x34')](_0x5cf7ba[_0xe447('0xa9')],{'$or':_[_0xe447('0x3a')](_0x5cf7ba['attributes'],function(_0x268429){var _0x1116fb={};_0x1116fb[_0x268429]={'$like':'%'+_0x808e5e['query'][_0xe447('0xac')]+'%'};return _0x1116fb;})});}if(_0x808e5e[_0xe447('0x42')][_0xe447('0xad')]){var _0x19f1fa=_0x808e5e[_0xe447('0x42')][_0xe447('0xad')][_0xe447('0xae')](',');var _0x5c9113={};_0x5c9113[_0x19f1fa[0x0]]={'$gte':moment(_0x19f1fa[0x1])[_0xe447('0x89')]('YYYY-MM-DD\x20HH:mm:ss')};_0x5cf7ba[_0xe447('0xa9')]=_[_0xe447('0x34')](_0x5cf7ba[_0xe447('0xa9')],_0x5c9113);}_0x5cf7ba=_[_0xe447('0x34')]({},_0x5cf7ba,_0x808e5e[_0xe447('0x4e')]);return db[_0xe447('0x65')][_0xe447('0x2c')]({'where':_0x5cf7ba[_0xe447('0xa9')]})[_0xe447('0x1a')](function(_0x513a84){_0x22928b[_0xe447('0x2c')]=_0x513a84;if(_0x808e5e[_0xe447('0x42')][_0xe447('0x4c')]){_0x5cf7ba[_0xe447('0x4d')]=[{'all':!![]}];}return db[_0xe447('0x65')][_0xe447('0x40')](_0x5cf7ba);})[_0xe447('0x1a')](function(_0x50ccdd){_0x22928b[_0xe447('0xaf')]=_0x50ccdd;return _0x22928b;});}})['then'](respondWithFilteredResult(_0x2e3f77,_0x5cf7ba))[_0xe447('0x24')](handleError(_0x2e3f77,null));};exports['getMyMessages']=function(_0xd51aa,_0x4de4e9){var _0x2d4397={'where':{}},_0x2e580f={},_0x1c6833={'count':0x0,'rows':[]};return db['ChatInteraction'][_0xe447('0x76')]({'where':{'id':_0xd51aa['params']['id']}})[_0xe447('0x1a')](handleEntityNotFound(_0x4de4e9,null))[_0xe447('0x1a')](function(_0x6561bf){if(_0x6561bf){_0x2e580f['model']=_['keys'](db[_0xe447('0x65')][_0xe447('0x3c')]);_0x2e580f[_0xe447('0x42')]=_['keys'](_0xd51aa['query']);_0x2e580f[_0xe447('0x47')]=_[_0xe447('0x49')](_0x2e580f[_0xe447('0x45')],_0x2e580f[_0xe447('0x42')]);_0x2d4397[_0xe447('0x48')]=_[_0xe447('0x49')](_0x2e580f[_0xe447('0x45')],qs[_0xe447('0x4a')](_0xd51aa['query'][_0xe447('0x4a')]));_0x2d4397['attributes']=_0x2d4397[_0xe447('0x48')][_0xe447('0x4b')]?_0x2d4397[_0xe447('0x48')]:_0x2e580f['model'];if(!_0xd51aa[_0xe447('0x42')][_0xe447('0xa5')](_0xe447('0xa6'))){_0x2d4397[_0xe447('0x2b')]=qs[_0xe447('0x2b')](_0xd51aa[_0xe447('0x42')][_0xe447('0x2b')]);_0x2d4397[_0xe447('0x29')]=qs[_0xe447('0x29')](_0xd51aa['query'][_0xe447('0x29')]);}_0x2d4397[_0xe447('0xa7')]=qs[_0xe447('0xa8')](_0xd51aa[_0xe447('0x42')][_0xe447('0xa8')]);_0x2d4397['where']=qs[_0xe447('0x47')](_['pick'](_0xd51aa['query'],_0x2e580f[_0xe447('0x47')]));_0x2d4397[_0xe447('0xa9')]['ChatInteractionId']=_0x6561bf['id'];if(_0xd51aa[_0xe447('0x42')][_0xe447('0xac')]){_0x2d4397[_0xe447('0xa9')]=_[_0xe447('0x34')](_0x2d4397[_0xe447('0xa9')],{'$or':_['map'](_0x2d4397['attributes'],function(_0x1b6c8a){var _0x18a04b={};_0x18a04b[_0x1b6c8a]={'$like':'%'+_0xd51aa[_0xe447('0x42')][_0xe447('0xac')]+'%'};return _0x18a04b;})});}_0x2d4397['where'][_0xe447('0x97')]=![];if(_0xd51aa[_0xe447('0x42')]['id']){_0x2d4397[_0xe447('0xa9')]['id']={'$gt':_0xd51aa[_0xe447('0x42')]['id']};}_0x2d4397=_['merge']({},_0x2d4397,_0xd51aa[_0xe447('0x4e')]);return db[_0xe447('0x65')]['count']({'where':_0x2d4397['where']})[_0xe447('0x1a')](function(_0x1e96c7){_0x1c6833[_0xe447('0x2c')]=_0x1e96c7;_0x2d4397[_0xe447('0x4d')]=[];if(_0xd51aa[_0xe447('0x42')][_0xe447('0xb0')]){_0x2d4397['include'][_0xe447('0xb1')]({'model':db[_0xe447('0x85')],'as':'User','attributes':['id','fullname','alias']});}if(_0xd51aa[_0xe447('0x42')]['includeAll']){_0x2d4397[_0xe447('0x4d')]=[{'all':!![]}];}return db[_0xe447('0x65')]['findAll'](_0x2d4397);})['then'](function(_0x5805c4){_0x1c6833[_0xe447('0xaf')]=_0x5805c4;return _0x1c6833;});}})[_0xe447('0x1a')](respondWithFilteredResult(_0x4de4e9,_0x2d4397))[_0xe447('0x24')](handleError(_0x4de4e9,null));};exports[_0xe447('0xb2')]=function(_0x765b62,_0x2834e8){return db[_0xe447('0x3b')][_0xe447('0x4f')]({'where':{'id':_0x765b62[_0xe447('0x44')]['id']}})[_0xe447('0x1a')](handleEntityNotFound(_0x2834e8,null))[_0xe447('0x1a')](function(_0x27d55d){if(_0x27d55d){return _0x27d55d[_0xe447('0xb3')](_0x765b62[_0xe447('0x35')][_0xe447('0xa2')],_[_0xe447('0xa3')](_0x765b62[_0xe447('0x35')],['ids','id'])||{})['spread'](function(){return db[_0xe447('0xb4')][_0xe447('0x40')]({'attributes':['id','name','color'],'where':{'id':_0x765b62['body'][_0xe447('0xa2')]}});})[_0xe447('0x1a')](function(_0x567ed6){socket['emit']('chatInteractionTags:save',{'id':Number(_0x765b62['params']['id']),'tags':_0x567ed6||[]});return{'id':Number(_0x765b62[_0xe447('0x44')]['id']),'tags':_0x567ed6||[]};});}})[_0xe447('0x1a')](respondWithResult(_0x2834e8,null))[_0xe447('0x24')](handleError(_0x2834e8,null));};exports[_0xe447('0xb5')]=function(_0x2ac63b,_0x1fa7ca){return db[_0xe447('0x3b')]['find']({'where':{'id':_0x2ac63b['params']['id']}})['then'](handleEntityNotFound(_0x1fa7ca,null))[_0xe447('0x1a')](function(_0x7eeef2){if(_0x7eeef2){return _0x7eeef2[_0xe447('0xb5')](_0x2ac63b[_0xe447('0x42')][_0xe447('0xa2')]);}})[_0xe447('0x1a')](respondWithStatusCode(_0x1fa7ca,null))[_0xe447('0x24')](handleError(_0x1fa7ca,null));};exports[_0xe447('0x7e')]=function(_0x4b45a6,_0x59b75c){var _0x2dd546=moment()['unix']()[_0xe447('0xb6')]();var _0x18f47b=path[_0xe447('0x7b')](config[_0xe447('0x81')],_0xe447('0xb7'),_0xe447('0xb8'),_0xe447('0xb9'));var _0x1958dd=path[_0xe447('0x7b')](config[_0xe447('0x81')],_0xe447('0xb7'),_0xe447('0xb8'),'attachments');var _0x6b0ff5=path[_0xe447('0x7b')](_0x18f47b,_0x2dd546);var _0x46891c=util[_0xe447('0x89')](_0xe447('0xba'),_0x4b45a6[_0xe447('0x44')]['id'],_0x2dd546);var _0x34eafb=path[_0xe447('0x7b')](_0x18f47b,_0x46891c);var _0x29adb2=[];_0x29adb2['push']({'model':db[_0xe447('0x85')],'as':_0xe447('0x85'),'attributes':['id',_0xe447('0x93')],'raw':!![]});_0x29adb2[_0xe447('0xb1')]({'model':db['CmContact'],'as':_0xe447('0xbb'),'attributes':['id',_0xe447('0xbc'),'lastName'],'raw':!![]});if(_0x4b45a6[_0xe447('0x42')][_0xe447('0xbd')]){_0x29adb2[_0xe447('0xb1')]({'model':db['Attachment'],'as':_0xe447('0x78'),'raw':!![]});}var _0x1ac4ef=[{'model':db[_0xe447('0x65')],'as':_0xe447('0x88'),'attributes':['id',_0xe447('0x35'),_0xe447('0x94'),_0xe447('0x96'),_0xe447('0x97'),_0xe447('0xbe')],'include':_0x29adb2}];_0x1ac4ef[_0xe447('0xb1')]({'model':db['ChatWebsite'],'as':_0xe447('0xbf'),'attributes':['id',_0xe447('0x31')]});_0x1ac4ef[_0xe447('0xb1')]({'model':db[_0xe447('0x85')],'as':_0xe447('0xc0'),'attributes':['id','fullname']});return db[_0xe447('0x3b')]['find']({'where':{'id':_0x4b45a6[_0xe447('0x44')]['id']},'include':_0x1ac4ef})[_0xe447('0x1a')](handleEntityNotFound(_0x59b75c,null))['then'](function(_0x2fa7d9){if(_0x2fa7d9){var _0x3dc96e=_0x2fa7d9['get']({'plain':!![]});fs['mkdirSync'](_0x6b0ff5);var _0x23ce05={'channel':_0xe447('0xc1'),'account':_0x3dc96e[_0xe447('0xbf')][_0xe447('0x31')],'agent':_0x3dc96e['Owner']?_0x3dc96e[_0xe447('0xc0')][_0xe447('0x93')]:_0xe447('0xc2'),'createdAt':moment(_0x3dc96e[_0xe447('0x94')])['format'](_0xe447('0xc3'))[_0xe447('0xb6')](),'closedAt':_0x3dc96e[_0xe447('0x6c')]?moment(_0x3dc96e[_0xe447('0xc4')])[_0xe447('0x89')](_0xe447('0xc3'))[_0xe447('0xb6')]():'','disposition':_0x3dc96e[_0xe447('0x6d')],'chatInfo':{'browser':_0x3dc96e[_0xe447('0xc5')],'os':_0x3dc96e[_0xe447('0xc6')],'referer':_0x3dc96e[_0xe447('0xc7')],'customerIp':_0x3dc96e['customerIp'],'customerPort':_0x3dc96e[_0xe447('0xc8')]?_0x3dc96e[_0xe447('0xc8')]:_0xe447('0xc2')},'messages':_(_0x3dc96e[_0xe447('0x88')])[_0xe447('0xc9')]()[_0xe447('0xac')]({'secret':![]})[_0xe447('0x3a')](function(_0x172036){if(_0x172036['Attachment']){var _0x230e9a=path[_0xe447('0x7b')](_0x1958dd,_0x172036[_0xe447('0x78')][_0xe447('0x71')]);if(fs[_0xe447('0xca')](_0x230e9a)){fs[_0xe447('0xcb')](_0x230e9a)['pipe'](fs[_0xe447('0xcc')](path[_0xe447('0x7b')](_0x6b0ff5,_0x172036[_0xe447('0x78')][_0xe447('0x71')])));}}return{'date':moment(_0x172036[_0xe447('0x94')])[_0xe447('0x89')]('YYYY-MM-DD\x20HH:mm:ss')[_0xe447('0xb6')](),'sender':_0x172036[_0xe447('0x96')]===_0xe447('0x64')?_0x172036[_0xe447('0x85')]?_0x172036[_0xe447('0x85')]['fullname']:_0xe447('0x91'):_0x172036['Contact'][_0xe447('0xbc')]+(_0x172036[_0xe447('0xbb')][_0xe447('0xcd')]?'\x20'+_0x172036['Contact'][_0xe447('0xcd')]:''),'body':_0x172036[_0xe447('0x78')]?_0x172036[_0xe447('0x78')][_0xe447('0x71')]:_0x172036['body'],'direction':_0x172036[_0xe447('0x96')]===_0xe447('0x64')?'A':'C','secret':_0x172036['secret'],'read':_0x172036['read'],'attachment':_0x172036['Attachment']};})['value']()};return ejs[_0xe447('0xce')](path[_0xe447('0x7b')](config[_0xe447('0x81')],'server',_0xe447('0xcf'),_0xe447('0xd0')),{'interaction':_0x23ce05})[_0xe447('0x1a')](function(_0x4f52a6){var _0x9ad498=path['join'](_0x6b0ff5,_0xe447('0xd1')+_0x3dc96e['id']+'-'+_0x2dd546+_0xe447('0xd2'));var _0x90bc5d={'path':_0x9ad498,'channel':'Chat','interactionId':_0x3dc96e['id']};return pdf[_0xe447('0xd3')](_0x4f52a6,_0x90bc5d);})['then'](function(){return new Promise(function(_0xabd564,_0x33a593){zipdir(_0x6b0ff5,{'saveTo':_0x34eafb},function(_0x35201d,_0x2a6a01){if(_0x35201d)return _0x33a593(_0x35201d);return _0xabd564(_0x2a6a01);});})[_0xe447('0x1a')](function(){return new BPromise(function(_0x4ce283,_0x5253b0){rimraf(_0x6b0ff5,function(_0x1f4bd4){if(_0x1f4bd4)_0x5253b0(_0x1f4bd4);return _0x4ce283();});});})[_0xe447('0x1a')](function(){return _0x59b75c[_0xe447('0x7e')](_0x34eafb,_0x46891c,function(_0x305035){if(_0x305035){logger[_0xe447('0x21')](_0xe447('0xd4'),_0x305035);}else{fs[_0xe447('0xd5')](_0x34eafb);}});});});}else{return _0x59b75c[_0xe447('0x25')](0xc8);}})[_0xe447('0x24')](handleError(_0x59b75c,null));};