9147f628d1c1a43f6630d5a2300b4ecd71d920ef
[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 _0x52f2=['update','stack','name','send','render','merge','contact','agent','index','map','ChatInteraction','fieldName','type','key','chat','query','user','show','model','keys','rawAttributes','filters','intersection','attributes','fields','includeAll','include','options','find','create','body','params','destroy','describe','createVidaooSession','findOne','ChatWebsiteId','vidaooEscalation','vidaooTopic','vidaooMetadata','vidaooApiKey','fullname','email','https://api-vidaoo.xcally.com/api/v1/meetings','isNull','vidaooNote','parse','metadata','Sequelize','Error\x20in\x20conversion\x20vidaooMetadata','ValidationError','join_url','ChatMessage','CmContact','Contact','abandon','abandonInteraction,\x20%s,\x20%s,\x20%s','entity\x20not\x20found','UserId','Interaction\x20already\x20assigned','closed','file','filename','mimetype','Attachment','attachmentDownload','Unauthorized','attachId','ChatOfflineMessageId','join','basename','download','close','mailTranscript','root','format','transcript-%s.txt','now','pathTranscript','User','Messages','length','agentAlias','System','customer','%s%d','ContactId','agent_fullname','[%s]\x20%s:\x20%s\x0a','createdAt','direction','secret','appendFileSync','MailAccount','MailServerOut','Smtp','getSmtpOptions','sendMail','\x22%s\x22\x20<%s>','addMessage','ids','omit','nolimit','order','sort','where','pick','ChatInteractionId','filter','$gte','split','YYYY-MM-DD\x20HH:mm:ss','findAll','rows','getMyMessages','hasOwnProperty','includeAgent','push','alias','addTags','setTags','spread','Tag','color','emit','removeTags','unix','toString','server','files','tmp','attachments','lastName','read','ChatWebsite','Account','Owner','get','mkdirSync','Chat','N.A.','closedAt','disposition','browserName','osName','referer','customerIp','customerPort','tail','createReadStream','pipe','out','firstName','value','views','downloadInteraction.ejs','transcript-','.pdf','createPdfFromHTML','err','unlinkSync','zip-dir','moment','bluebird','mustache','util','lodash','ioredis','../../components/interaction/service','../../components/parsers/qs','../../config/logger','api','../../config/environment','redis','defaults','localhost','./chatInteraction.socket','register','jayson/promise','client','http','request','then','request\x20sent','debug','ChatInteraction,\x20%s,\x20%s,\x20%s','stringify','error','code','ChatInteraction,\x20%s,\x20%s','message','result','catch','sendStatus','status','end','json','offset','limit','undefined','count','set'];(function(_0xa3d443,_0x121b37){var _0x489486=function(_0x16ecd1){while(--_0x16ecd1){_0xa3d443['push'](_0xa3d443['shift']());}};_0x489486(++_0x121b37);}(_0x52f2,0x16b));var _0x252f=function(_0x9aada2,_0xd2d349){_0x9aada2=_0x9aada2-0x0;var _0x196dff=_0x52f2[_0x9aada2];return _0x196dff;};'use strict';var rimraf=require('rimraf');var zipdir=require(_0x252f('0x0'));var rp=require('request-promise');var moment=require(_0x252f('0x1'));var BPromise=require(_0x252f('0x2'));var Mustache=require(_0x252f('0x3'));var util=require(_0x252f('0x4'));var path=require('path');var ejs=require('ejs');var fs=require('fs');var _=require(_0x252f('0x5'));var Redis=require(_0x252f('0x6'));var interaction=require(_0x252f('0x7'));var qs=require(_0x252f('0x8'));var pdf=require('../../components/export/pdf');var logger=require(_0x252f('0x9'))(_0x252f('0xa'));var config=require(_0x252f('0xb'));var db=require('../../mysqldb')['db'];config[_0x252f('0xc')]=_[_0x252f('0xd')](config[_0x252f('0xc')],{'host':_0x252f('0xe'),'port':0x18eb});var socket=require('socket.io-emitter')(new Redis(config[_0x252f('0xc')]));require(_0x252f('0xf'))[_0x252f('0x10')](socket);var jayson=require(_0x252f('0x11'));var client=jayson[_0x252f('0x12')][_0x252f('0x13')]({'port':0x232b});var client9004=jayson['client'][_0x252f('0x13')]({'port':0x232c});function respondWithRpcPromise(_0x54c569,_0x2487cf,_0x240724,_0x1d6059){return new BPromise(function(_0xbc81a,_0x44da4e){var _0x1caae9=_0x1d6059||client;return _0x1caae9[_0x252f('0x14')](_0x54c569,_0x240724)[_0x252f('0x15')](function(_0x3e8db6){logger['info']('ChatInteraction,\x20%s,\x20%s',_0x2487cf,_0x252f('0x16'));logger[_0x252f('0x17')](_0x252f('0x18'),_0x2487cf,_0x252f('0x16'),JSON[_0x252f('0x19')](_0x3e8db6));if(_0x3e8db6[_0x252f('0x1a')]){if(_0x3e8db6['error'][_0x252f('0x1b')]===0x1f4){logger['error'](_0x252f('0x1c'),_0x2487cf,_0x3e8db6[_0x252f('0x1a')][_0x252f('0x1d')]);return _0x44da4e(_0x3e8db6['error'][_0x252f('0x1d')]);}logger[_0x252f('0x1a')](_0x252f('0x1c'),_0x2487cf,_0x3e8db6[_0x252f('0x1a')]['message']);return _0xbc81a(_0x3e8db6[_0x252f('0x1a')][_0x252f('0x1d')]);}else{logger['info'](_0x252f('0x1c'),_0x2487cf,'request\x20sent');_0xbc81a(_0x3e8db6[_0x252f('0x1e')]['message']);}})[_0x252f('0x1f')](function(_0x578f73){logger[_0x252f('0x1a')](_0x252f('0x1c'),_0x2487cf,_0x578f73);_0x44da4e(_0x578f73);});});}function respondWithStatusCode(_0xfd680f,_0x1339e4){_0x1339e4=_0x1339e4||0xcc;return function(_0xd94eeb){if(_0xd94eeb){return _0xfd680f[_0x252f('0x20')](_0x1339e4);}return _0xfd680f[_0x252f('0x21')](_0x1339e4)[_0x252f('0x22')]();};}function respondWithResult(_0x145199,_0x52e810){_0x52e810=_0x52e810||0xc8;return function(_0x484169){if(_0x484169){return _0x145199[_0x252f('0x21')](_0x52e810)[_0x252f('0x23')](_0x484169);}};}function respondWithFilteredResult(_0x5a2b37,_0x1ab2d7){return function(_0x1916f6){if(_0x1916f6){var _0x286ae0=typeof _0x1ab2d7[_0x252f('0x24')]==='undefined'&&typeof _0x1ab2d7[_0x252f('0x25')]===_0x252f('0x26');var _0x32b1e8=_0x1916f6[_0x252f('0x27')];var _0x4649a6=_0x286ae0?0x0:_0x1ab2d7[_0x252f('0x24')];var _0xcea077=_0x286ae0?_0x1916f6[_0x252f('0x27')]:_0x1ab2d7[_0x252f('0x24')]+_0x1ab2d7[_0x252f('0x25')];var _0x50c773;if(_0xcea077>=_0x32b1e8){_0xcea077=_0x32b1e8;_0x50c773=0xc8;}else{_0x50c773=0xce;}_0x5a2b37[_0x252f('0x21')](_0x50c773);return _0x5a2b37[_0x252f('0x28')]('Content-Range',_0x4649a6+'-'+_0xcea077+'/'+_0x32b1e8)[_0x252f('0x23')](_0x1916f6);}return null;};}function saveUpdates(_0xa75d72){return function(_0x54a958){if(_0x54a958){return _0x54a958[_0x252f('0x29')](_0xa75d72)[_0x252f('0x15')](function(_0x56006c){return _0x56006c;});}return null;};}function removeEntity(_0x21f960){return function(_0x8fd69a){if(_0x8fd69a){return _0x8fd69a['destroy']()[_0x252f('0x15')](function(){_0x21f960[_0x252f('0x21')](0xcc)['end']();});}};}function handleEntityNotFound(_0x48974c){return function(_0x4bd72f){if(!_0x4bd72f){_0x48974c[_0x252f('0x20')](0x194);}return _0x4bd72f;};}function handleError(_0x431955,_0x52fc83){_0x52fc83=_0x52fc83||0x1f4;return function(_0x3e8605){logger[_0x252f('0x1a')](_0x3e8605[_0x252f('0x2a')]);if(_0x3e8605[_0x252f('0x2b')]){delete _0x3e8605['name'];}_0x431955[_0x252f('0x21')](_0x52fc83)[_0x252f('0x2c')](_0x3e8605);};}function getMustacheRender(_0x5a0b48,_0x2ac684){return Mustache[_0x252f('0x2d')](_0x5a0b48,_[_0x252f('0x2e')](_0x2ac684['body'],{'contact':_0x2ac684[_0x252f('0x2f')],'message':_0x2ac684[_0x252f('0x1d')],'agent':_0x2ac684[_0x252f('0x30')],'interaction':_0x2ac684['interaction'],'account':_0x2ac684['account']}))||'';}exports[_0x252f('0x31')]=function(_0x4592ce,_0x1ddb21){var _0x510368={};var _0x200d06=_[_0x252f('0x32')](db[_0x252f('0x33')]['rawAttributes'],function(_0x147dcc){return{'name':_0x147dcc[_0x252f('0x34')],'type':_0x147dcc[_0x252f('0x35')][_0x252f('0x36')]};});_0x510368=qs['getOptions'](_0x200d06,_0x4592ce);return interaction['findAll'](_0x252f('0x37'),_0x4592ce[_0x252f('0x38')],_0x4592ce[_0x252f('0x39')],_0x510368,_0x200d06)[_0x252f('0x15')](respondWithFilteredResult(_0x1ddb21,_0x510368))[_0x252f('0x1f')](handleError(_0x1ddb21,null));};exports[_0x252f('0x3a')]=function(_0xf5973c,_0x38e393){var _0x563161={'raw':![],'where':{'id':_0xf5973c['params']['id']}},_0x259a39={};_0x259a39[_0x252f('0x3b')]=_[_0x252f('0x3c')](db[_0x252f('0x33')][_0x252f('0x3d')]);_0x259a39[_0x252f('0x38')]=_[_0x252f('0x3c')](_0xf5973c[_0x252f('0x38')]);_0x259a39[_0x252f('0x3e')]=_[_0x252f('0x3f')](_0x259a39['model'],_0x259a39['query']);_0x563161[_0x252f('0x40')]=_[_0x252f('0x3f')](_0x259a39['model'],qs[_0x252f('0x41')](_0xf5973c['query'][_0x252f('0x41')]));_0x563161[_0x252f('0x40')]=_0x563161[_0x252f('0x40')]['length']?_0x563161[_0x252f('0x40')]:_0x259a39[_0x252f('0x3b')];if(_0xf5973c['query'][_0x252f('0x42')]){_0x563161[_0x252f('0x43')]=[{'all':!![]}];}_0x563161=_[_0x252f('0x2e')]({},_0x563161,_0xf5973c[_0x252f('0x44')]);return db['ChatInteraction'][_0x252f('0x45')](_0x563161)[_0x252f('0x15')](handleEntityNotFound(_0x38e393,null))['then'](respondWithResult(_0x38e393,null))[_0x252f('0x1f')](handleError(_0x38e393,null));};exports[_0x252f('0x46')]=function(_0xb10a0b,_0x3af9a9){return db[_0x252f('0x33')][_0x252f('0x46')](_0xb10a0b[_0x252f('0x47')],{})['then'](respondWithResult(_0x3af9a9,0xc9))[_0x252f('0x1f')](handleError(_0x3af9a9,null));};exports['update']=function(_0x321d6f,_0x5a33ec){if(_0x321d6f[_0x252f('0x47')]['id']){delete _0x321d6f['body']['id'];}return db[_0x252f('0x33')][_0x252f('0x45')]({'where':{'id':_0x321d6f[_0x252f('0x48')]['id']}})[_0x252f('0x15')](handleEntityNotFound(_0x5a33ec,null))[_0x252f('0x15')](saveUpdates(_0x321d6f[_0x252f('0x47')],null))['then'](respondWithResult(_0x5a33ec,null))[_0x252f('0x1f')](handleError(_0x5a33ec,null));};exports[_0x252f('0x49')]=function(_0x45724b,_0x548f06){return db['ChatInteraction']['find']({'where':{'id':_0x45724b[_0x252f('0x48')]['id']}})[_0x252f('0x15')](handleEntityNotFound(_0x548f06,null))[_0x252f('0x15')](removeEntity(_0x548f06,null))['catch'](handleError(_0x548f06,null));};exports[_0x252f('0x4a')]=function(_0x51ef3b,_0x4fbf88){return db[_0x252f('0x33')][_0x252f('0x4a')]()[_0x252f('0x15')](respondWithResult(_0x4fbf88,null))['catch'](handleError(_0x4fbf88,null));};exports[_0x252f('0x4b')]=function(_0x1da66b,_0x5571da){var _0x1f0c1e=_0x1da66b['params']['id'];var _0x2f1df8;var _0x5df2c0;var _0x337ba9;return db['ChatInteraction'][_0x252f('0x4c')]({'where':{'id':_0x1f0c1e}})[_0x252f('0x15')](function(_0x3de19b){_0x2f1df8=_0x3de19b;return db['ChatWebsite'][_0x252f('0x4c')]({'where':{'id':_0x3de19b[_0x252f('0x4d')]},'attributes':['id','vidaooApiKey',_0x252f('0x4e'),_0x252f('0x4f'),'vidaooNote',_0x252f('0x50')],'raw':!![]});})[_0x252f('0x15')](function(_0x244a99){if(_0x244a99&&_0x244a99[_0x252f('0x51')]&&_0x244a99['vidaooEscalation']){_0x5df2c0=_0x244a99;var _0x1ac587={'interaction':{'id':_0x1f0c1e},'agent':{'id':_0x1da66b[_0x252f('0x39')]['id'],'name':_0x1da66b[_0x252f('0x39')][_0x252f('0x2b')],'fullname':_0x1da66b[_0x252f('0x39')][_0x252f('0x52')],'email':_0x1da66b[_0x252f('0x39')][_0x252f('0x53')]},'contact':_0x1da66b[_0x252f('0x47')]['contact'],'account':_0x1da66b[_0x252f('0x47')]['account']};var _0x5e24bb={'rejectUnauthorized':![],'method':'POST','url':_0x252f('0x54'),'headers':{'X-APIKEY':_0x244a99[_0x252f('0x51')]},'body':{'duration':null,'scheduledAt':null,'participants':[_0x1da66b['user']['email']],'buttons':null,'topic':_0x5df2c0[_0x252f('0x4f')],'note':_[_0x252f('0x55')](_0x5df2c0[_0x252f('0x56')])?null:getMustacheRender(_0x5df2c0[_0x252f('0x56')],_0x1ac587),'metadata':null},'redirect':'follow','json':!![]};try{if(!_['isNull'](_0x5df2c0['vidaooMetadata'])){var _0x40b15d=getMustacheRender(_0x5df2c0[_0x252f('0x50')],_0x1ac587);var _0x1e520e=JSON[_0x252f('0x57')](_0x40b15d);_0x5e24bb[_0x252f('0x47')][_0x252f('0x58')]=_0x1e520e;}}catch(_0x13a5b0){throw new db[(_0x252f('0x59'))]['ValidationError'](_0x252f('0x5a'));}return rp(_0x5e24bb);}else{throw new db['Sequelize'][(_0x252f('0x5b'))]('Error\x20in\x20Vidaoo\x20configuration');}})[_0x252f('0x15')](function(_0x44d7a2){_0x337ba9=_0x44d7a2;if(_0x337ba9['id']){var _0x543e51={'body':_0x337ba9[_0x252f('0x5c')],'ChatWebsiteId':_0x5df2c0['id'],'ChatInteractionId':_0x2f1df8['id'],'direction':'out','UserId':_0x1da66b[_0x252f('0x39')]['id']};return db[_0x252f('0x5d')][_0x252f('0x46')](_0x543e51,{});}else{throw new db[(_0x252f('0x59'))][(_0x252f('0x5b'))]('Unable\x20to\x20create\x20Vidaoo\x20session');}})[_0x252f('0x15')](function(){return _0x2f1df8['updateAttributes']({'vidaooSessionId':_0x337ba9['id']});})['then'](respondWithResult(_0x5571da,null))[_0x252f('0x1f')](handleError(_0x5571da,null));};exports['customUpdate']=function(_0x3d1f6a,_0x20fa60){if(_0x3d1f6a['body']['id']){delete _0x3d1f6a[_0x252f('0x47')]['id'];}return db[_0x252f('0x33')][_0x252f('0x45')]({'where':{'id':_0x3d1f6a[_0x252f('0x48')]['id']},'include':[{'model':db[_0x252f('0x5e')],'as':_0x252f('0x5f')}]})[_0x252f('0x15')](handleEntityNotFound(_0x20fa60,null))[_0x252f('0x15')](saveUpdates(_0x3d1f6a[_0x252f('0x47')],null))[_0x252f('0x15')](respondWithResult(_0x20fa60,null))[_0x252f('0x1f')](handleError(_0x20fa60,null));};exports[_0x252f('0x60')]=function(_0x293723,_0x29d904){if(_0x293723[_0x252f('0x47')]['id']){delete _0x293723['body']['id'];}return db[_0x252f('0x33')][_0x252f('0x45')]({'where':{'id':_0x293723['params']['id']}})[_0x252f('0x15')](function(_0x22e4dc){if(!_0x22e4dc){logger[_0x252f('0x1a')](_0x252f('0x61'),_0x252f('0x60'),0x194,_0x252f('0x62'));_0x29d904[_0x252f('0x20')](0x194);}else{if(_0x22e4dc[_0x252f('0x63')]){throw new db[(_0x252f('0x59'))][(_0x252f('0x5b'))](_0x252f('0x64'));}}return _0x22e4dc;})[_0x252f('0x15')](function(_0x4e8f0e){if(_0x4e8f0e){return _0x4e8f0e[_0x252f('0x29')]({'closed':_0x293723['body'][_0x252f('0x65')],'disposition':_0x293723[_0x252f('0x47')]['disposition']})[_0x252f('0x15')](function(_0x3b6362){return _0x3b6362;});}})[_0x252f('0x15')](function(){return respondWithRpcPromise('AbandonInteraction',_0x252f('0x60'),_0x293723[_0x252f('0x47')],client9004);})['then'](respondWithResult(_0x29d904,null))[_0x252f('0x1f')](handleError(_0x29d904,null));};exports['attachmentUpload']=function(_0x842418,_0x515d5d){_0x842418[_0x252f('0x47')][_0x252f('0x2b')]=_0x842418[_0x252f('0x66')]['originalname'];_0x842418[_0x252f('0x47')]['basename']=_0x842418[_0x252f('0x66')][_0x252f('0x67')];_0x842418['body'][_0x252f('0x35')]=_0x842418[_0x252f('0x66')][_0x252f('0x68')];return db[_0x252f('0x69')][_0x252f('0x46')](_0x842418[_0x252f('0x47')])['then'](respondWithResult(_0x515d5d,0xc9))[_0x252f('0x1f')](handleError(_0x515d5d,null));};exports[_0x252f('0x6a')]=function(_0x32ff64,_0x7efec0){return db[_0x252f('0x33')][_0x252f('0x4c')]({'where':{'id':_0x32ff64['params']['id']},'raw':!![]})[_0x252f('0x15')](handleEntityNotFound(_0x7efec0,null))['then'](function(_0x2e559b){if(_0x2e559b){if(_0x2e559b[_0x252f('0x65')]){return _0x7efec0[_0x252f('0x21')](0x193)['json']({'status':0x193,'message':_0x252f('0x6b')});}return db[_0x252f('0x69')]['findOne']({'where':{'id':_0x32ff64[_0x252f('0x38')][_0x252f('0x6c')]},'raw':!![]})[_0x252f('0x15')](handleEntityNotFound(_0x7efec0,null));}})[_0x252f('0x15')](function(_0xd05024){if(_0xd05024){var _0x4ac8ed=_0xd05024[_0x252f('0x6d')]!==null;var _0x4aed6a=path[_0x252f('0x6e')](config['root'],'server/files/attachments/');if(_0x4ac8ed){_0x4aed6a=path[_0x252f('0x6e')](_0x4aed6a,'offline-chat/');}var _0x1a29df=path[_0x252f('0x6e')](_0x4aed6a,_0xd05024[_0x252f('0x6f')]);return _0x7efec0[_0x252f('0x70')](_0x1a29df);}})['catch'](handleError(_0x7efec0,null));};exports[_0x252f('0x71')]=function(_0xa2d280,_0x20376c){var _0x5ec61f=null;var _0x4a815f=null;var _0xe7431b=null;if(_0xa2d280[_0x252f('0x47')]['id']){delete _0xa2d280[_0x252f('0x47')]['id'];}if(_0xa2d280[_0x252f('0x47')][_0x252f('0x72')]){_0xe7431b=path[_0x252f('0x6e')](config[_0x252f('0x73')],'server/files/chat/upload/',util[_0x252f('0x74')](_0x252f('0x75'),Date[_0x252f('0x76')]()));_0xa2d280['body'][_0x252f('0x77')]=_0xe7431b;}return db['ChatInteraction']['find']({'where':{'id':_0xa2d280[_0x252f('0x48')]['id']},'include':[{'model':db['ChatMessage'],'as':'Messages','include':[{'model':db[_0x252f('0x78')],'as':_0x252f('0x78')}]}]})[_0x252f('0x15')](handleEntityNotFound(_0x20376c,null))[_0x252f('0x15')](saveUpdates(_0xa2d280['body'],null))[_0x252f('0x15')](function(_0x2a16b0){if(_0x2a16b0){_0x5ec61f=_0x2a16b0;return db['ChatWebsite'][_0x252f('0x45')]({'where':{'id':_0x5ec61f['ChatWebsiteId']}});}})[_0x252f('0x15')](function(_0x2291fe){if(_0x2291fe){_0x4a815f=_0x2291fe;return db[_0x252f('0x5e')][_0x252f('0x45')]({'where':{'id':_0x5ec61f['ContactId']}});}})[_0x252f('0x15')](function(_0x2fd6f0){if(_0x2fd6f0){if(_0xe7431b){var _0x1e022f=null;for(var _0x18aa9c=0x1;_0x18aa9c<_0x5ec61f[_0x252f('0x79')][_0x252f('0x7a')];++_0x18aa9c){_0x1e022f=_0x5ec61f[_0x252f('0x79')][_0x18aa9c];var _0x33c37c=_0x1e022f[_0x252f('0x63')]?util[_0x252f('0x74')]('%s%d',_0x4a815f[_0x252f('0x7b')],_0x1e022f[_0x252f('0x63')]):_0x252f('0x7c');var _0x2741b9=_0xa2d280[_0x252f('0x47')][_0x252f('0x7d')]||util['format'](_0x252f('0x7e'),'Visitor',_0x1e022f[_0x252f('0x7f')]);switch(_0x4a815f['agentIdentifier']){case'website_alias':_0x33c37c=_0x1e022f[_0x252f('0x63')]?util[_0x252f('0x74')](_0x252f('0x7e'),_0x4a815f['agentAlias'],_0x1e022f[_0x252f('0x63')]):_0x252f('0x7c');break;case'agent_alias':_0x33c37c=_0x1e022f[_0x252f('0x63')]?_0x1e022f['User']['alias']||_0x1e022f[_0x252f('0x78')][_0x252f('0x52')]:_0x252f('0x7c');break;case _0x252f('0x80'):_0x33c37c=_0x1e022f[_0x252f('0x63')]?_0x1e022f['User'][_0x252f('0x52')]:_0x252f('0x7c');break;default:_0x33c37c=_0x1e022f['UserId']?util[_0x252f('0x74')](_0x252f('0x7e'),_0x4a815f['agentAlias'],_0x1e022f[_0x252f('0x63')]):_0x252f('0x7c');}var _0x3a207e=util[_0x252f('0x74')](_0x252f('0x81'),moment(_0x1e022f[_0x252f('0x82')])[_0x252f('0x74')]('MM/DD/YYYY\x20HH:mm:ss'),_0x1e022f[_0x252f('0x83')]==='in'?_0x2741b9:_0x33c37c,_0x1e022f[_0x252f('0x47')]);if(!_0x1e022f[_0x252f('0x84')]){fs[_0x252f('0x85')](_0xe7431b,_0x3a207e);}}return db[_0x252f('0x86')][_0x252f('0x4c')]({'where':{'service':!![]},'include':[{'model':db[_0x252f('0x87')],'as':_0x252f('0x88'),'raw':!![],'include':[{'model':db['CloudProvider'],'as':'CloudProvider','raw':!![]}]}]});}}})[_0x252f('0x15')](function(_0xe42d5){if(!_0xe42d5||!_0xe42d5['Smtp']||!_0xa2d280[_0x252f('0x47')])return _0x5ec61f;var _0x55d4b7=_0xe42d5[_0x252f('0x89')]();return respondWithRpcPromise('SendMail',_0x252f('0x8a'),{'account':_0x55d4b7,'message':{'from':util['format'](_0x252f('0x8b'),_0xe42d5[_0x252f('0x2b')],_0xe42d5[_0x252f('0x53')]||_0xe42d5[_0x252f('0x88')][_0x252f('0x39')]),'to':_0xa2d280[_0x252f('0x47')]['mailTranscript'],'subject':'Mail\x20Transcript','text':util['format']('Hi\x20%s,\x20attached\x20you\x20can\x20find\x20your\x20transcript\x20file.\x20\x0a\x0aRegards.\x20\x0a\x0a',_0xa2d280[_0x252f('0x47')]['mailTranscript']),'attachments':[{'path':_0xe7431b}]}})[_0x252f('0x15')](function(){return _0x5ec61f;})[_0x252f('0x1f')](function(_0x4178b7){logger[_0x252f('0x1a')](_0x4178b7);});})[_0x252f('0x15')](respondWithStatusCode(_0x20376c,0xc8))['catch'](handleError(_0x20376c,null));};exports['addMessage']=function(_0x112c9a,_0x5109af){return db[_0x252f('0x5d')][_0x252f('0x45')]({'where':{'id':_0x112c9a[_0x252f('0x48')]['id']}})[_0x252f('0x15')](handleEntityNotFound(_0x5109af,null))[_0x252f('0x15')](function(_0x3b7fbf){if(_0x3b7fbf){return _0x3b7fbf[_0x252f('0x8c')](_0x112c9a[_0x252f('0x47')][_0x252f('0x8d')],_[_0x252f('0x8e')](_0x112c9a[_0x252f('0x47')],[_0x252f('0x8d'),'id'])||{});}})[_0x252f('0x15')](respondWithResult(_0x5109af,null))[_0x252f('0x1f')](handleError(_0x5109af,null));};exports['getMessages']=function(_0x58de24,_0x8b464a){var _0x28644b={'raw':![],'where':{}};var _0x1cc4fa={};var _0x245faa={'count':0x0,'rows':[]};return db[_0x252f('0x33')][_0x252f('0x4c')]({'where':{'id':_0x58de24[_0x252f('0x48')]['id']}})[_0x252f('0x15')](handleEntityNotFound(_0x8b464a,null))[_0x252f('0x15')](function(_0x328dbd){if(_0x328dbd){_0x1cc4fa[_0x252f('0x3b')]=_[_0x252f('0x3c')](db['ChatMessage'][_0x252f('0x3d')]);_0x1cc4fa[_0x252f('0x38')]=_['keys'](_0x58de24[_0x252f('0x38')]);_0x1cc4fa[_0x252f('0x3e')]=_['intersection'](_0x1cc4fa[_0x252f('0x3b')],_0x1cc4fa[_0x252f('0x38')]);_0x28644b[_0x252f('0x40')]=_[_0x252f('0x3f')](_0x1cc4fa[_0x252f('0x3b')],qs['fields'](_0x58de24[_0x252f('0x38')][_0x252f('0x41')]));_0x28644b[_0x252f('0x40')]=_0x28644b[_0x252f('0x40')][_0x252f('0x7a')]?_0x28644b[_0x252f('0x40')]:_0x1cc4fa[_0x252f('0x3b')];if(!_0x58de24[_0x252f('0x38')]['hasOwnProperty'](_0x252f('0x8f'))){_0x28644b[_0x252f('0x25')]=qs[_0x252f('0x25')](_0x58de24[_0x252f('0x38')][_0x252f('0x25')]);_0x28644b['offset']=qs[_0x252f('0x24')](_0x58de24['query'][_0x252f('0x24')]);}_0x28644b[_0x252f('0x90')]=qs['sort'](_0x58de24[_0x252f('0x38')][_0x252f('0x91')]);_0x28644b[_0x252f('0x92')]=qs[_0x252f('0x3e')](_[_0x252f('0x93')](_0x58de24['query'],_0x1cc4fa[_0x252f('0x3e')]));_0x28644b[_0x252f('0x92')][_0x252f('0x94')]=_0x328dbd['id'];if(_0x58de24[_0x252f('0x38')][_0x252f('0x95')]){_0x28644b[_0x252f('0x92')]=_[_0x252f('0x2e')](_0x28644b[_0x252f('0x92')],{'$or':_[_0x252f('0x32')](_0x28644b[_0x252f('0x40')],function(_0x5405dd){var _0x4441a2={};_0x4441a2[_0x5405dd]={'$like':'%'+_0x58de24[_0x252f('0x38')]['filter']+'%'};return _0x4441a2;})});}if(_0x58de24[_0x252f('0x38')][_0x252f('0x96')]){var _0x3f56ac=_0x58de24[_0x252f('0x38')][_0x252f('0x96')][_0x252f('0x97')](',');var _0x19ed10={};_0x19ed10[_0x3f56ac[0x0]]={'$gte':moment(_0x3f56ac[0x1])['format'](_0x252f('0x98'))};_0x28644b['where']=_[_0x252f('0x2e')](_0x28644b[_0x252f('0x92')],_0x19ed10);}_0x28644b=_['merge']({},_0x28644b,_0x58de24[_0x252f('0x44')]);return db[_0x252f('0x5d')][_0x252f('0x27')]({'where':_0x28644b['where']})[_0x252f('0x15')](function(_0x327441){_0x245faa['count']=_0x327441;if(_0x58de24['query']['includeAll']){_0x28644b['include']=[{'all':!![]}];}return db['ChatMessage'][_0x252f('0x99')](_0x28644b);})[_0x252f('0x15')](function(_0x186815){_0x245faa[_0x252f('0x9a')]=_0x186815;return _0x245faa;});}})[_0x252f('0x15')](respondWithFilteredResult(_0x8b464a,_0x28644b))[_0x252f('0x1f')](handleError(_0x8b464a,null));};exports[_0x252f('0x9b')]=function(_0x21cac0,_0x2e6e08){var _0x2f228c={'where':{}},_0x5c454b={},_0x10cbc4={'count':0x0,'rows':[]};return db[_0x252f('0x33')][_0x252f('0x4c')]({'where':{'id':_0x21cac0[_0x252f('0x48')]['id']}})[_0x252f('0x15')](handleEntityNotFound(_0x2e6e08,null))[_0x252f('0x15')](function(_0x477eb3){if(_0x477eb3){_0x5c454b['model']=_[_0x252f('0x3c')](db[_0x252f('0x5d')][_0x252f('0x3d')]);_0x5c454b[_0x252f('0x38')]=_[_0x252f('0x3c')](_0x21cac0['query']);_0x5c454b['filters']=_[_0x252f('0x3f')](_0x5c454b[_0x252f('0x3b')],_0x5c454b[_0x252f('0x38')]);_0x2f228c[_0x252f('0x40')]=_[_0x252f('0x3f')](_0x5c454b[_0x252f('0x3b')],qs[_0x252f('0x41')](_0x21cac0['query']['fields']));_0x2f228c[_0x252f('0x40')]=_0x2f228c[_0x252f('0x40')][_0x252f('0x7a')]?_0x2f228c[_0x252f('0x40')]:_0x5c454b[_0x252f('0x3b')];if(!_0x21cac0['query'][_0x252f('0x9c')](_0x252f('0x8f'))){_0x2f228c['limit']=qs[_0x252f('0x25')](_0x21cac0[_0x252f('0x38')][_0x252f('0x25')]);_0x2f228c[_0x252f('0x24')]=qs[_0x252f('0x24')](_0x21cac0['query'][_0x252f('0x24')]);}_0x2f228c[_0x252f('0x90')]=qs[_0x252f('0x91')](_0x21cac0['query'][_0x252f('0x91')]);_0x2f228c[_0x252f('0x92')]=qs[_0x252f('0x3e')](_[_0x252f('0x93')](_0x21cac0['query'],_0x5c454b[_0x252f('0x3e')]));_0x2f228c[_0x252f('0x92')][_0x252f('0x94')]=_0x477eb3['id'];if(_0x21cac0[_0x252f('0x38')][_0x252f('0x95')]){_0x2f228c[_0x252f('0x92')]=_[_0x252f('0x2e')](_0x2f228c['where'],{'$or':_[_0x252f('0x32')](_0x2f228c[_0x252f('0x40')],function(_0x47ed1b){var _0x1b5e17={};_0x1b5e17[_0x47ed1b]={'$like':'%'+_0x21cac0[_0x252f('0x38')][_0x252f('0x95')]+'%'};return _0x1b5e17;})});}_0x2f228c['where'][_0x252f('0x84')]=![];if(_0x21cac0['query']['id']){_0x2f228c['where']['id']={'$gt':_0x21cac0[_0x252f('0x38')]['id']};}_0x2f228c=_[_0x252f('0x2e')]({},_0x2f228c,_0x21cac0[_0x252f('0x44')]);return db[_0x252f('0x5d')]['count']({'where':_0x2f228c['where']})[_0x252f('0x15')](function(_0x5978ba){_0x10cbc4[_0x252f('0x27')]=_0x5978ba;_0x2f228c['include']=[];if(_0x21cac0[_0x252f('0x38')][_0x252f('0x9d')]){_0x2f228c['include'][_0x252f('0x9e')]({'model':db[_0x252f('0x78')],'as':_0x252f('0x78'),'attributes':['id',_0x252f('0x52'),_0x252f('0x9f')]});}if(_0x21cac0[_0x252f('0x38')][_0x252f('0x42')]){_0x2f228c[_0x252f('0x43')]=[{'all':!![]}];}return db['ChatMessage'][_0x252f('0x99')](_0x2f228c);})[_0x252f('0x15')](function(_0x50171c){_0x10cbc4['rows']=_0x50171c;return _0x10cbc4;});}})['then'](respondWithFilteredResult(_0x2e6e08,_0x2f228c))[_0x252f('0x1f')](handleError(_0x2e6e08,null));};exports[_0x252f('0xa0')]=function(_0x392b5f,_0xc9ab90){return db[_0x252f('0x33')]['find']({'where':{'id':_0x392b5f[_0x252f('0x48')]['id']}})[_0x252f('0x15')](handleEntityNotFound(_0xc9ab90,null))[_0x252f('0x15')](function(_0x202ab3){if(_0x202ab3){return _0x202ab3[_0x252f('0xa1')](_0x392b5f[_0x252f('0x47')][_0x252f('0x8d')],_['omit'](_0x392b5f[_0x252f('0x47')],[_0x252f('0x8d'),'id'])||{})[_0x252f('0xa2')](function(){return db[_0x252f('0xa3')]['findAll']({'attributes':['id',_0x252f('0x2b'),_0x252f('0xa4')],'where':{'id':_0x392b5f['body']['ids']}});})['then'](function(_0x50284d){socket[_0x252f('0xa5')]('chatInteractionTags:save',{'id':Number(_0x392b5f['params']['id']),'tags':_0x50284d||[]});return{'id':Number(_0x392b5f[_0x252f('0x48')]['id']),'tags':_0x50284d||[]};});}})[_0x252f('0x15')](respondWithResult(_0xc9ab90,null))['catch'](handleError(_0xc9ab90,null));};exports[_0x252f('0xa6')]=function(_0x32d409,_0x457d78){return db['ChatInteraction'][_0x252f('0x45')]({'where':{'id':_0x32d409['params']['id']}})['then'](handleEntityNotFound(_0x457d78,null))['then'](function(_0x438cf0){if(_0x438cf0){return _0x438cf0[_0x252f('0xa6')](_0x32d409['query'][_0x252f('0x8d')]);}})[_0x252f('0x15')](respondWithStatusCode(_0x457d78,null))['catch'](handleError(_0x457d78,null));};exports[_0x252f('0x70')]=function(_0x501524,_0x19481d){var _0x1419a9=moment()[_0x252f('0xa7')]()[_0x252f('0xa8')]();var _0x5a4827=path[_0x252f('0x6e')](config[_0x252f('0x73')],_0x252f('0xa9'),_0x252f('0xaa'),_0x252f('0xab'));var _0x29de57=path[_0x252f('0x6e')](config['root'],_0x252f('0xa9'),'files',_0x252f('0xac'));var _0x192d2f=path[_0x252f('0x6e')](_0x5a4827,_0x1419a9);var _0x1d0baa=util['format']('transcript-%d-%s.zip',_0x501524[_0x252f('0x48')]['id'],_0x1419a9);var _0x3fdc34=path['join'](_0x5a4827,_0x1d0baa);var _0xdae8dd=[];_0xdae8dd[_0x252f('0x9e')]({'model':db[_0x252f('0x78')],'as':_0x252f('0x78'),'attributes':['id',_0x252f('0x52')],'raw':!![]});_0xdae8dd[_0x252f('0x9e')]({'model':db[_0x252f('0x5e')],'as':_0x252f('0x5f'),'attributes':['id','firstName',_0x252f('0xad')],'raw':!![]});if(_0x501524[_0x252f('0x38')][_0x252f('0xac')]){_0xdae8dd[_0x252f('0x9e')]({'model':db[_0x252f('0x69')],'as':_0x252f('0x69'),'raw':!![]});}var _0x1ed94c=[{'model':db[_0x252f('0x5d')],'as':_0x252f('0x79'),'attributes':['id',_0x252f('0x47'),'createdAt',_0x252f('0x83'),_0x252f('0x84'),_0x252f('0xae')],'include':_0xdae8dd}];_0x1ed94c[_0x252f('0x9e')]({'model':db[_0x252f('0xaf')],'as':_0x252f('0xb0'),'attributes':['id','name']});_0x1ed94c[_0x252f('0x9e')]({'model':db[_0x252f('0x78')],'as':_0x252f('0xb1'),'attributes':['id',_0x252f('0x52')]});return db[_0x252f('0x33')][_0x252f('0x45')]({'where':{'id':_0x501524[_0x252f('0x48')]['id']},'include':_0x1ed94c})[_0x252f('0x15')](handleEntityNotFound(_0x19481d,null))[_0x252f('0x15')](function(_0x11e1cb){if(_0x11e1cb){var _0x32d13f=_0x11e1cb[_0x252f('0xb2')]({'plain':!![]});fs[_0x252f('0xb3')](_0x192d2f);var _0x4272e0={'channel':_0x252f('0xb4'),'account':_0x32d13f[_0x252f('0xb0')][_0x252f('0x2b')],'agent':_0x32d13f[_0x252f('0xb1')]?_0x32d13f['Owner'][_0x252f('0x52')]:_0x252f('0xb5'),'createdAt':moment(_0x32d13f[_0x252f('0x82')])[_0x252f('0x74')](_0x252f('0x98'))[_0x252f('0xa8')](),'closedAt':_0x32d13f[_0x252f('0x65')]?moment(_0x32d13f[_0x252f('0xb6')])[_0x252f('0x74')]('YYYY-MM-DD\x20HH:mm:ss')[_0x252f('0xa8')]():'','disposition':_0x32d13f[_0x252f('0xb7')],'chatInfo':{'browser':_0x32d13f[_0x252f('0xb8')],'os':_0x32d13f[_0x252f('0xb9')],'referer':_0x32d13f[_0x252f('0xba')],'customerIp':_0x32d13f[_0x252f('0xbb')],'customerPort':_0x32d13f[_0x252f('0xbc')]?_0x32d13f[_0x252f('0xbc')]:_0x252f('0xb5')},'messages':_(_0x32d13f[_0x252f('0x79')])[_0x252f('0xbd')]()['filter']({'secret':![]})['map'](function(_0x3a0478){if(_0x3a0478[_0x252f('0x69')]){var _0x19e895=path[_0x252f('0x6e')](_0x29de57,_0x3a0478[_0x252f('0x69')][_0x252f('0x6f')]);if(fs['existsSync'](_0x19e895)){fs[_0x252f('0xbe')](_0x19e895)[_0x252f('0xbf')](fs['createWriteStream'](path[_0x252f('0x6e')](_0x192d2f,_0x3a0478['Attachment'][_0x252f('0x6f')])));}}return{'date':moment(_0x3a0478[_0x252f('0x82')])[_0x252f('0x74')](_0x252f('0x98'))[_0x252f('0xa8')](),'sender':_0x3a0478[_0x252f('0x83')]===_0x252f('0xc0')?_0x3a0478[_0x252f('0x78')]?_0x3a0478[_0x252f('0x78')][_0x252f('0x52')]:_0x252f('0x7c'):_0x3a0478[_0x252f('0x5f')][_0x252f('0xc1')]+(_0x3a0478[_0x252f('0x5f')][_0x252f('0xad')]?'\x20'+_0x3a0478[_0x252f('0x5f')][_0x252f('0xad')]:''),'body':_0x3a0478[_0x252f('0x69')]?_0x3a0478[_0x252f('0x69')][_0x252f('0x6f')]:_0x3a0478[_0x252f('0x47')],'direction':_0x3a0478[_0x252f('0x83')]===_0x252f('0xc0')?'A':'C','secret':_0x3a0478[_0x252f('0x84')],'read':_0x3a0478[_0x252f('0xae')],'attachment':_0x3a0478[_0x252f('0x69')]};})[_0x252f('0xc2')]()};return ejs['renderFile'](path[_0x252f('0x6e')](config[_0x252f('0x73')],_0x252f('0xa9'),_0x252f('0xc3'),_0x252f('0xc4')),{'interaction':_0x4272e0})[_0x252f('0x15')](function(_0x328d7a){var _0x59858b=path[_0x252f('0x6e')](_0x192d2f,_0x252f('0xc5')+_0x32d13f['id']+'-'+_0x1419a9+_0x252f('0xc6'));var _0x4c9daf={'path':_0x59858b,'channel':_0x252f('0xb4'),'interactionId':_0x32d13f['id']};return pdf[_0x252f('0xc7')](_0x328d7a,_0x4c9daf);})[_0x252f('0x15')](function(){return new Promise(function(_0x58d465,_0xf4423a){zipdir(_0x192d2f,{'saveTo':_0x3fdc34},function(_0x4b9e5c,_0x12fbe7){if(_0x4b9e5c)return _0xf4423a(_0x4b9e5c);return _0x58d465(_0x12fbe7);});})['then'](function(){return new BPromise(function(_0x2867f7,_0x13ba73){rimraf(_0x192d2f,function(_0xeb9fb4){if(_0xeb9fb4)_0x13ba73(_0xeb9fb4);return _0x2867f7();});});})[_0x252f('0x15')](function(){return _0x19481d[_0x252f('0x70')](_0x3fdc34,_0x1d0baa,function(_0x4ed3ab){if(_0x4ed3ab){logger[_0x252f('0x1a')](_0x252f('0xc8'),_0x4ed3ab);}else{fs[_0x252f('0xc9')](_0x3fdc34);}});});});}else{return _0x19481d[_0x252f('0x20')](0xc8);}})[_0x252f('0x1f')](handleError(_0x19481d,null));};