Built motion from commit 10af8726.|2.6.34
[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 _0xbf5b=['unlinkSync','rimraf','zip-dir','request-promise','moment','bluebird','mustache','util','path','ejs','../../components/interaction/service','../../components/parsers/qs','../../components/export/pdf','../../config/environment','../../mysqldb','redis','socket.io-emitter','./chatInteraction.socket','jayson/promise','client','http','request','then','debug','ChatInteraction,\x20%s,\x20%s,\x20%s','request\x20sent','stringify','error','code','ChatInteraction,\x20%s,\x20%s','message','info','result','catch','sendStatus','end','status','json','offset','undefined','limit','count','Content-Range','destroy','stack','name','render','merge','body','contact','agent','interaction','account','map','ChatInteraction','rawAttributes','fieldName','type','key','getOptions','findAll','query','user','show','params','model','keys','filters','intersection','attributes','length','includeAll','options','find','create','describe','findOne','ChatWebsite','ChatWebsiteId','vidaooApiKey','vidaooMetadata','fullname','email','POST','vidaooTopic','isNull','vidaooNote','parse','metadata','Sequelize','ValidationError','Error\x20in\x20conversion\x20vidaooMetadata','Error\x20in\x20Vidaoo\x20configuration','join_url','out','ChatMessage','Unable\x20to\x20create\x20Vidaoo\x20session','CmContact','abandon','UserId','Interaction\x20already\x20assigned','update','AbandonInteraction','attachmentUpload','file','originalname','basename','filename','mimetype','Attachment','Unauthorized','attachId','ChatOfflineMessageId','join','root','server/files/attachments/','download','close','mailTranscript','format','transcript-%s.txt','pathTranscript','User','Messages','%s%d','agentAlias','System','customer','Visitor','ContactId','agentIdentifier','agent_alias','alias','createdAt','MailAccount','CloudProvider','Smtp','getSmtpOptions','sendMail','Mail\x20Transcript','addMessage','ids','getMessages','fields','hasOwnProperty','nolimit','order','sort','where','pick','ChatInteractionId','filter','$gte','split','YYYY-MM-DD\x20HH:mm:ss','include','rows','getMyMessages','secret','includeAgent','push','addTags','setTags','spread','Tag','color','emit','chatInteractionTags:save','removeTags','unix','toString','server','files','attachments','firstName','read','Account','Owner','get','mkdirSync','Chat','closed','closedAt','disposition','osName','customerIp','customerPort','N.A.','tail','createReadStream','pipe','createWriteStream','Contact','lastName','direction','value','views','downloadInteraction.ejs','transcript-','.pdf','createPdfFromHTML'];(function(_0x4a3a9a,_0x21d963){var _0x5805b6=function(_0x393d6e){while(--_0x393d6e){_0x4a3a9a['push'](_0x4a3a9a['shift']());}};_0x5805b6(++_0x21d963);}(_0xbf5b,0x193));var _0xbbf5=function(_0x47fd36,_0x41535d){_0x47fd36=_0x47fd36-0x0;var _0x21a0fc=_0xbf5b[_0x47fd36];return _0x21a0fc;};'use strict';var rimraf=require(_0xbbf5('0x0'));var zipdir=require(_0xbbf5('0x1'));var rp=require(_0xbbf5('0x2'));var moment=require(_0xbbf5('0x3'));var BPromise=require(_0xbbf5('0x4'));var Mustache=require(_0xbbf5('0x5'));var util=require(_0xbbf5('0x6'));var path=require(_0xbbf5('0x7'));var ejs=require(_0xbbf5('0x8'));var fs=require('fs');var _=require('lodash');var Redis=require('ioredis');var interaction=require(_0xbbf5('0x9'));var qs=require(_0xbbf5('0xa'));var pdf=require(_0xbbf5('0xb'));var logger=require('../../config/logger')('api');var config=require(_0xbbf5('0xc'));var db=require(_0xbbf5('0xd'))['db'];config[_0xbbf5('0xe')]=_['defaults'](config['redis'],{'host':'localhost','port':0x18eb});var socket=require(_0xbbf5('0xf'))(new Redis(config['redis']));require(_0xbbf5('0x10'))['register'](socket);var jayson=require(_0xbbf5('0x11'));var client=jayson[_0xbbf5('0x12')][_0xbbf5('0x13')]({'port':0x232b});var client9004=jayson['client'][_0xbbf5('0x13')]({'port':0x232c});function respondWithRpcPromise(_0x328ada,_0x4117e6,_0xa36601,_0x85052d){return new BPromise(function(_0x652060,_0x9ce373){var _0x47deea=_0x85052d||client;return _0x47deea[_0xbbf5('0x14')](_0x328ada,_0xa36601)[_0xbbf5('0x15')](function(_0x2c4ad8){logger['info']('ChatInteraction,\x20%s,\x20%s',_0x4117e6,'request\x20sent');logger[_0xbbf5('0x16')](_0xbbf5('0x17'),_0x4117e6,_0xbbf5('0x18'),JSON[_0xbbf5('0x19')](_0x2c4ad8));if(_0x2c4ad8['error']){if(_0x2c4ad8[_0xbbf5('0x1a')][_0xbbf5('0x1b')]===0x1f4){logger['error'](_0xbbf5('0x1c'),_0x4117e6,_0x2c4ad8[_0xbbf5('0x1a')][_0xbbf5('0x1d')]);return _0x9ce373(_0x2c4ad8['error'][_0xbbf5('0x1d')]);}logger['error']('ChatInteraction,\x20%s,\x20%s',_0x4117e6,_0x2c4ad8[_0xbbf5('0x1a')][_0xbbf5('0x1d')]);return _0x652060(_0x2c4ad8['error'][_0xbbf5('0x1d')]);}else{logger[_0xbbf5('0x1e')]('ChatInteraction,\x20%s,\x20%s',_0x4117e6,'request\x20sent');_0x652060(_0x2c4ad8[_0xbbf5('0x1f')][_0xbbf5('0x1d')]);}})[_0xbbf5('0x20')](function(_0x108e25){logger[_0xbbf5('0x1a')](_0xbbf5('0x1c'),_0x4117e6,_0x108e25);_0x9ce373(_0x108e25);});});}function respondWithStatusCode(_0x18f8d5,_0x38a9df){_0x38a9df=_0x38a9df||0xcc;return function(_0x191f55){if(_0x191f55){return _0x18f8d5[_0xbbf5('0x21')](_0x38a9df);}return _0x18f8d5['status'](_0x38a9df)[_0xbbf5('0x22')]();};}function respondWithResult(_0xd4e58d,_0x430365){_0x430365=_0x430365||0xc8;return function(_0x2e1cc0){if(_0x2e1cc0){return _0xd4e58d[_0xbbf5('0x23')](_0x430365)[_0xbbf5('0x24')](_0x2e1cc0);}};}function respondWithFilteredResult(_0x34fc6f,_0xc831cf){return function(_0x532cce){if(_0x532cce){var _0x4d4e42=typeof _0xc831cf[_0xbbf5('0x25')]===_0xbbf5('0x26')&&typeof _0xc831cf[_0xbbf5('0x27')]==='undefined';var _0x1875d0=_0x532cce['count'];var _0x106f0d=_0x4d4e42?0x0:_0xc831cf[_0xbbf5('0x25')];var _0x48aa67=_0x4d4e42?_0x532cce[_0xbbf5('0x28')]:_0xc831cf[_0xbbf5('0x25')]+_0xc831cf[_0xbbf5('0x27')];var _0xfe14d4;if(_0x48aa67>=_0x1875d0){_0x48aa67=_0x1875d0;_0xfe14d4=0xc8;}else{_0xfe14d4=0xce;}_0x34fc6f['status'](_0xfe14d4);return _0x34fc6f['set'](_0xbbf5('0x29'),_0x106f0d+'-'+_0x48aa67+'/'+_0x1875d0)['json'](_0x532cce);}return null;};}function saveUpdates(_0x3a24af){return function(_0x47af38){if(_0x47af38){return _0x47af38['update'](_0x3a24af)[_0xbbf5('0x15')](function(_0x587dcb){return _0x587dcb;});}return null;};}function removeEntity(_0x2c39e4){return function(_0x3e9a72){if(_0x3e9a72){return _0x3e9a72[_0xbbf5('0x2a')]()[_0xbbf5('0x15')](function(){_0x2c39e4[_0xbbf5('0x23')](0xcc)[_0xbbf5('0x22')]();});}};}function handleEntityNotFound(_0x43e429){return function(_0x1cacbc){if(!_0x1cacbc){_0x43e429[_0xbbf5('0x21')](0x194);}return _0x1cacbc;};}function handleError(_0x929dc6,_0x1e9a54){_0x1e9a54=_0x1e9a54||0x1f4;return function(_0x58a5b5){logger[_0xbbf5('0x1a')](_0x58a5b5[_0xbbf5('0x2b')]);if(_0x58a5b5['name']){delete _0x58a5b5[_0xbbf5('0x2c')];}_0x929dc6[_0xbbf5('0x23')](_0x1e9a54)['send'](_0x58a5b5);};}function getMustacheRender(_0x2c4126,_0x2f92da){return Mustache[_0xbbf5('0x2d')](_0x2c4126,_[_0xbbf5('0x2e')](_0x2f92da[_0xbbf5('0x2f')],{'contact':_0x2f92da[_0xbbf5('0x30')],'message':_0x2f92da[_0xbbf5('0x1d')],'agent':_0x2f92da[_0xbbf5('0x31')],'interaction':_0x2f92da[_0xbbf5('0x32')],'account':_0x2f92da[_0xbbf5('0x33')]}))||'';}exports['index']=function(_0x4de97d,_0x294935){var _0x2132fb={};var _0x3d6c8c=_[_0xbbf5('0x34')](db[_0xbbf5('0x35')][_0xbbf5('0x36')],function(_0x5bbff1){return{'name':_0x5bbff1[_0xbbf5('0x37')],'type':_0x5bbff1[_0xbbf5('0x38')][_0xbbf5('0x39')]};});_0x2132fb=qs[_0xbbf5('0x3a')](_0x3d6c8c,_0x4de97d);return interaction[_0xbbf5('0x3b')]('chat',_0x4de97d[_0xbbf5('0x3c')],_0x4de97d[_0xbbf5('0x3d')],_0x2132fb,_0x3d6c8c)[_0xbbf5('0x15')](respondWithFilteredResult(_0x294935,_0x2132fb))[_0xbbf5('0x20')](handleError(_0x294935,null));};exports[_0xbbf5('0x3e')]=function(_0x55903a,_0x29a7cd){var _0x336abb={'raw':![],'where':{'id':_0x55903a[_0xbbf5('0x3f')]['id']}},_0x5c890c={};_0x5c890c[_0xbbf5('0x40')]=_[_0xbbf5('0x41')](db[_0xbbf5('0x35')][_0xbbf5('0x36')]);_0x5c890c['query']=_[_0xbbf5('0x41')](_0x55903a[_0xbbf5('0x3c')]);_0x5c890c[_0xbbf5('0x42')]=_[_0xbbf5('0x43')](_0x5c890c[_0xbbf5('0x40')],_0x5c890c[_0xbbf5('0x3c')]);_0x336abb[_0xbbf5('0x44')]=_[_0xbbf5('0x43')](_0x5c890c['model'],qs['fields'](_0x55903a[_0xbbf5('0x3c')]['fields']));_0x336abb[_0xbbf5('0x44')]=_0x336abb[_0xbbf5('0x44')][_0xbbf5('0x45')]?_0x336abb[_0xbbf5('0x44')]:_0x5c890c['model'];if(_0x55903a[_0xbbf5('0x3c')][_0xbbf5('0x46')]){_0x336abb['include']=[{'all':!![]}];}_0x336abb=_[_0xbbf5('0x2e')]({},_0x336abb,_0x55903a[_0xbbf5('0x47')]);return db[_0xbbf5('0x35')][_0xbbf5('0x48')](_0x336abb)[_0xbbf5('0x15')](handleEntityNotFound(_0x29a7cd,null))['then'](respondWithResult(_0x29a7cd,null))[_0xbbf5('0x20')](handleError(_0x29a7cd,null));};exports[_0xbbf5('0x49')]=function(_0x80f2a2,_0x534588){return db['ChatInteraction'][_0xbbf5('0x49')](_0x80f2a2[_0xbbf5('0x2f')],{})[_0xbbf5('0x15')](respondWithResult(_0x534588,0xc9))[_0xbbf5('0x20')](handleError(_0x534588,null));};exports['update']=function(_0x2ff8eb,_0x46a403){if(_0x2ff8eb[_0xbbf5('0x2f')]['id']){delete _0x2ff8eb[_0xbbf5('0x2f')]['id'];}return db[_0xbbf5('0x35')]['find']({'where':{'id':_0x2ff8eb[_0xbbf5('0x3f')]['id']}})[_0xbbf5('0x15')](handleEntityNotFound(_0x46a403,null))[_0xbbf5('0x15')](saveUpdates(_0x2ff8eb['body'],null))[_0xbbf5('0x15')](respondWithResult(_0x46a403,null))[_0xbbf5('0x20')](handleError(_0x46a403,null));};exports[_0xbbf5('0x2a')]=function(_0x5950bd,_0x2619e5){return db[_0xbbf5('0x35')][_0xbbf5('0x48')]({'where':{'id':_0x5950bd[_0xbbf5('0x3f')]['id']}})[_0xbbf5('0x15')](handleEntityNotFound(_0x2619e5,null))[_0xbbf5('0x15')](removeEntity(_0x2619e5,null))['catch'](handleError(_0x2619e5,null));};exports['describe']=function(_0x547ead,_0x1b85a9){return db[_0xbbf5('0x35')][_0xbbf5('0x4a')]()[_0xbbf5('0x15')](respondWithResult(_0x1b85a9,null))['catch'](handleError(_0x1b85a9,null));};exports['createVidaooSession']=function(_0x5ba428,_0x1f79ee){var _0x4318fa=_0x5ba428[_0xbbf5('0x3f')]['id'];var _0x750998;var _0x9ec7f7;var _0x3437c3;return db['ChatInteraction'][_0xbbf5('0x4b')]({'where':{'id':_0x4318fa}})[_0xbbf5('0x15')](function(_0x2716fe){_0x750998=_0x2716fe;return db[_0xbbf5('0x4c')]['findOne']({'where':{'id':_0x2716fe[_0xbbf5('0x4d')]},'attributes':['id',_0xbbf5('0x4e'),'vidaooEscalation','vidaooTopic','vidaooNote',_0xbbf5('0x4f')],'raw':!![]});})[_0xbbf5('0x15')](function(_0x17a83f){if(_0x17a83f&&_0x17a83f[_0xbbf5('0x4e')]&&_0x17a83f['vidaooEscalation']){_0x9ec7f7=_0x17a83f;var _0x340e41={'interaction':{'id':_0x4318fa},'agent':{'id':_0x5ba428[_0xbbf5('0x3d')]['id'],'name':_0x5ba428[_0xbbf5('0x3d')][_0xbbf5('0x2c')],'fullname':_0x5ba428[_0xbbf5('0x3d')][_0xbbf5('0x50')],'email':_0x5ba428['user'][_0xbbf5('0x51')]},'contact':_0x5ba428[_0xbbf5('0x2f')]['contact'],'account':_0x5ba428['body'][_0xbbf5('0x33')]};var _0x386c0f={'rejectUnauthorized':![],'method':_0xbbf5('0x52'),'url':'https://api-vidaoo.xcally.com/api/v1/meetings','headers':{'X-APIKEY':_0x17a83f[_0xbbf5('0x4e')]},'body':{'duration':null,'scheduledAt':null,'participants':[_0x5ba428[_0xbbf5('0x3d')][_0xbbf5('0x51')]],'buttons':null,'topic':_0x9ec7f7[_0xbbf5('0x53')],'note':_[_0xbbf5('0x54')](_0x9ec7f7[_0xbbf5('0x55')])?null:getMustacheRender(_0x9ec7f7[_0xbbf5('0x55')],_0x340e41),'metadata':null},'redirect':'follow','json':!![]};try{if(!_[_0xbbf5('0x54')](_0x9ec7f7[_0xbbf5('0x4f')])){var _0x3ab44e=getMustacheRender(_0x9ec7f7[_0xbbf5('0x4f')],_0x340e41);var _0x234b61=JSON[_0xbbf5('0x56')](_0x3ab44e);_0x386c0f[_0xbbf5('0x2f')][_0xbbf5('0x57')]=_0x234b61;}}catch(_0x358dbd){throw new db[(_0xbbf5('0x58'))][(_0xbbf5('0x59'))](_0xbbf5('0x5a'));}return rp(_0x386c0f);}else{throw new db['Sequelize']['ValidationError'](_0xbbf5('0x5b'));}})[_0xbbf5('0x15')](function(_0x426894){_0x3437c3=_0x426894;if(_0x3437c3['id']){var _0x2ce428={'body':_0x3437c3[_0xbbf5('0x5c')],'ChatWebsiteId':_0x9ec7f7['id'],'ChatInteractionId':_0x750998['id'],'direction':_0xbbf5('0x5d'),'UserId':_0x5ba428['user']['id']};return db[_0xbbf5('0x5e')]['create'](_0x2ce428,{});}else{throw new db[(_0xbbf5('0x58'))][(_0xbbf5('0x59'))](_0xbbf5('0x5f'));}})[_0xbbf5('0x15')](function(){return _0x750998['updateAttributes']({'vidaooSessionId':_0x3437c3['id']});})['then'](respondWithResult(_0x1f79ee,null))['catch'](handleError(_0x1f79ee,null));};exports['customUpdate']=function(_0x42c3d6,_0x555f0b){if(_0x42c3d6[_0xbbf5('0x2f')]['id']){delete _0x42c3d6[_0xbbf5('0x2f')]['id'];}return db['ChatInteraction'][_0xbbf5('0x48')]({'where':{'id':_0x42c3d6[_0xbbf5('0x3f')]['id']},'include':[{'model':db[_0xbbf5('0x60')],'as':'Contact'}]})[_0xbbf5('0x15')](handleEntityNotFound(_0x555f0b,null))[_0xbbf5('0x15')](saveUpdates(_0x42c3d6['body'],null))[_0xbbf5('0x15')](respondWithResult(_0x555f0b,null))[_0xbbf5('0x20')](handleError(_0x555f0b,null));};exports['abandon']=function(_0x2b06ae,_0x3a021b){if(_0x2b06ae[_0xbbf5('0x2f')]['id']){delete _0x2b06ae['body']['id'];}return db[_0xbbf5('0x35')][_0xbbf5('0x48')]({'where':{'id':_0x2b06ae['params']['id']}})['then'](function(_0xefc17d){if(!_0xefc17d){logger[_0xbbf5('0x1a')]('abandonInteraction,\x20%s,\x20%s,\x20%s',_0xbbf5('0x61'),0x194,'entity\x20not\x20found');_0x3a021b[_0xbbf5('0x21')](0x194);}else{if(_0xefc17d[_0xbbf5('0x62')]){throw new db[(_0xbbf5('0x58'))][(_0xbbf5('0x59'))](_0xbbf5('0x63'));}}return _0xefc17d;})[_0xbbf5('0x15')](function(_0x2834b6){if(_0x2834b6){return _0x2834b6[_0xbbf5('0x64')]({'closed':_0x2b06ae[_0xbbf5('0x2f')]['closed'],'disposition':_0x2b06ae[_0xbbf5('0x2f')]['disposition']})['then'](function(_0x2a5b23){return _0x2a5b23;});}})[_0xbbf5('0x15')](function(){return respondWithRpcPromise(_0xbbf5('0x65'),'abandon',_0x2b06ae[_0xbbf5('0x2f')],client9004);})[_0xbbf5('0x15')](respondWithResult(_0x3a021b,null))[_0xbbf5('0x20')](handleError(_0x3a021b,null));};exports[_0xbbf5('0x66')]=function(_0x44c452,_0x392565){_0x44c452[_0xbbf5('0x2f')][_0xbbf5('0x2c')]=_0x44c452[_0xbbf5('0x67')][_0xbbf5('0x68')];_0x44c452[_0xbbf5('0x2f')][_0xbbf5('0x69')]=_0x44c452[_0xbbf5('0x67')][_0xbbf5('0x6a')];_0x44c452[_0xbbf5('0x2f')][_0xbbf5('0x38')]=_0x44c452['file'][_0xbbf5('0x6b')];return db[_0xbbf5('0x6c')][_0xbbf5('0x49')](_0x44c452[_0xbbf5('0x2f')])[_0xbbf5('0x15')](respondWithResult(_0x392565,0xc9))[_0xbbf5('0x20')](handleError(_0x392565,null));};exports['attachmentDownload']=function(_0x3f84f0,_0x57b008){return db[_0xbbf5('0x35')][_0xbbf5('0x4b')]({'where':{'id':_0x3f84f0[_0xbbf5('0x3f')]['id']},'raw':!![]})[_0xbbf5('0x15')](handleEntityNotFound(_0x57b008,null))['then'](function(_0x4c646b){if(_0x4c646b){if(_0x4c646b['closed']){return _0x57b008[_0xbbf5('0x23')](0x193)[_0xbbf5('0x24')]({'status':0x193,'message':_0xbbf5('0x6d')});}return db['Attachment'][_0xbbf5('0x4b')]({'where':{'id':_0x3f84f0[_0xbbf5('0x3c')][_0xbbf5('0x6e')]},'raw':!![]})[_0xbbf5('0x15')](handleEntityNotFound(_0x57b008,null));}})[_0xbbf5('0x15')](function(_0x417d4d){if(_0x417d4d){var _0x32f8f2=_0x417d4d[_0xbbf5('0x6f')]!==null;var _0x401879=path[_0xbbf5('0x70')](config[_0xbbf5('0x71')],_0xbbf5('0x72'));if(_0x32f8f2){_0x401879=path[_0xbbf5('0x70')](_0x401879,'offline-chat/');}var _0x55a138=path[_0xbbf5('0x70')](_0x401879,_0x417d4d['basename']);return _0x57b008[_0xbbf5('0x73')](_0x55a138);}})[_0xbbf5('0x20')](handleError(_0x57b008,null));};exports[_0xbbf5('0x74')]=function(_0x3d2a1e,_0xb0c46d){var _0x19670b=null;var _0x329862=null;var _0x5ab3c=null;if(_0x3d2a1e['body']['id']){delete _0x3d2a1e[_0xbbf5('0x2f')]['id'];}if(_0x3d2a1e[_0xbbf5('0x2f')][_0xbbf5('0x75')]){_0x5ab3c=path[_0xbbf5('0x70')](config['root'],'server/files/chat/upload/',util[_0xbbf5('0x76')](_0xbbf5('0x77'),Date['now']()));_0x3d2a1e['body'][_0xbbf5('0x78')]=_0x5ab3c;}return db[_0xbbf5('0x35')][_0xbbf5('0x48')]({'where':{'id':_0x3d2a1e['params']['id']},'include':[{'model':db[_0xbbf5('0x5e')],'as':'Messages','include':[{'model':db[_0xbbf5('0x79')],'as':'User'}]}]})[_0xbbf5('0x15')](handleEntityNotFound(_0xb0c46d,null))[_0xbbf5('0x15')](saveUpdates(_0x3d2a1e[_0xbbf5('0x2f')],null))[_0xbbf5('0x15')](function(_0x407adf){if(_0x407adf){_0x19670b=_0x407adf;return db[_0xbbf5('0x4c')][_0xbbf5('0x48')]({'where':{'id':_0x19670b['ChatWebsiteId']}});}})[_0xbbf5('0x15')](function(_0x5f04fa){if(_0x5f04fa){_0x329862=_0x5f04fa;return db['CmContact'][_0xbbf5('0x48')]({'where':{'id':_0x19670b['ContactId']}});}})['then'](function(_0x2f836f){if(_0x2f836f){if(_0x5ab3c){var _0x1dc044=null;for(var _0x5566d0=0x1;_0x5566d0<_0x19670b[_0xbbf5('0x7a')][_0xbbf5('0x45')];++_0x5566d0){_0x1dc044=_0x19670b[_0xbbf5('0x7a')][_0x5566d0];var _0x54f5a1=_0x1dc044[_0xbbf5('0x62')]?util[_0xbbf5('0x76')](_0xbbf5('0x7b'),_0x329862[_0xbbf5('0x7c')],_0x1dc044[_0xbbf5('0x62')]):_0xbbf5('0x7d');var _0x2fa5ca=_0x3d2a1e[_0xbbf5('0x2f')][_0xbbf5('0x7e')]||util[_0xbbf5('0x76')](_0xbbf5('0x7b'),_0xbbf5('0x7f'),_0x1dc044[_0xbbf5('0x80')]);switch(_0x329862[_0xbbf5('0x81')]){case'website_alias':_0x54f5a1=_0x1dc044[_0xbbf5('0x62')]?util[_0xbbf5('0x76')]('%s%d',_0x329862['agentAlias'],_0x1dc044[_0xbbf5('0x62')]):'System';break;case _0xbbf5('0x82'):_0x54f5a1=_0x1dc044[_0xbbf5('0x62')]?_0x1dc044['User'][_0xbbf5('0x83')]||_0x1dc044[_0xbbf5('0x79')][_0xbbf5('0x50')]:_0xbbf5('0x7d');break;case'agent_fullname':_0x54f5a1=_0x1dc044['UserId']?_0x1dc044['User'][_0xbbf5('0x50')]:_0xbbf5('0x7d');break;default:_0x54f5a1=_0x1dc044[_0xbbf5('0x62')]?util[_0xbbf5('0x76')](_0xbbf5('0x7b'),_0x329862[_0xbbf5('0x7c')],_0x1dc044['UserId']):_0xbbf5('0x7d');}var _0x2caa66=util[_0xbbf5('0x76')]('[%s]\x20%s:\x20%s\x0a',moment(_0x1dc044[_0xbbf5('0x84')])[_0xbbf5('0x76')]('MM/DD/YYYY\x20HH:mm:ss'),_0x1dc044['direction']==='in'?_0x2fa5ca:_0x54f5a1,_0x1dc044[_0xbbf5('0x2f')]);if(!_0x1dc044['secret']){fs['appendFileSync'](_0x5ab3c,_0x2caa66);}}return db[_0xbbf5('0x85')][_0xbbf5('0x4b')]({'where':{'service':!![]},'include':[{'model':db['MailServerOut'],'as':'Smtp','raw':!![],'include':[{'model':db[_0xbbf5('0x86')],'as':'CloudProvider','raw':!![]}]}]});}}})[_0xbbf5('0x15')](function(_0x33054a){if(!_0x33054a||!_0x33054a[_0xbbf5('0x87')]||!_0x3d2a1e['body'])return _0x19670b;var _0x1f4856=_0x33054a[_0xbbf5('0x88')]();return respondWithRpcPromise('SendMail',_0xbbf5('0x89'),{'account':_0x1f4856,'message':{'from':util['format']('\x22%s\x22\x20<%s>',_0x33054a[_0xbbf5('0x2c')],_0x33054a[_0xbbf5('0x51')]||_0x33054a[_0xbbf5('0x87')][_0xbbf5('0x3d')]),'to':_0x3d2a1e['body'][_0xbbf5('0x75')],'subject':_0xbbf5('0x8a'),'text':util[_0xbbf5('0x76')]('Hi\x20%s,\x20attached\x20you\x20can\x20find\x20your\x20transcript\x20file.\x20\x0a\x0aRegards.\x20\x0a\x0a',_0x3d2a1e[_0xbbf5('0x2f')][_0xbbf5('0x75')]),'attachments':[{'path':_0x5ab3c}]}})[_0xbbf5('0x15')](function(){return _0x19670b;})[_0xbbf5('0x20')](function(_0x5b617d){logger[_0xbbf5('0x1a')](_0x5b617d);});})[_0xbbf5('0x15')](respondWithStatusCode(_0xb0c46d,0xc8))[_0xbbf5('0x20')](handleError(_0xb0c46d,null));};exports[_0xbbf5('0x8b')]=function(_0x8133e3,_0x35c26f){return db[_0xbbf5('0x5e')][_0xbbf5('0x48')]({'where':{'id':_0x8133e3[_0xbbf5('0x3f')]['id']}})[_0xbbf5('0x15')](handleEntityNotFound(_0x35c26f,null))[_0xbbf5('0x15')](function(_0x4b1e71){if(_0x4b1e71){return _0x4b1e71[_0xbbf5('0x8b')](_0x8133e3['body'][_0xbbf5('0x8c')],_['omit'](_0x8133e3['body'],[_0xbbf5('0x8c'),'id'])||{});}})[_0xbbf5('0x15')](respondWithResult(_0x35c26f,null))[_0xbbf5('0x20')](handleError(_0x35c26f,null));};exports[_0xbbf5('0x8d')]=function(_0xe8933c,_0x155520){var _0x5bb210={'raw':![],'where':{}};var _0x34ca36={};var _0x54c38f={'count':0x0,'rows':[]};return db[_0xbbf5('0x35')][_0xbbf5('0x4b')]({'where':{'id':_0xe8933c['params']['id']}})[_0xbbf5('0x15')](handleEntityNotFound(_0x155520,null))['then'](function(_0x21a645){if(_0x21a645){_0x34ca36[_0xbbf5('0x40')]=_[_0xbbf5('0x41')](db[_0xbbf5('0x5e')]['rawAttributes']);_0x34ca36[_0xbbf5('0x3c')]=_[_0xbbf5('0x41')](_0xe8933c['query']);_0x34ca36[_0xbbf5('0x42')]=_[_0xbbf5('0x43')](_0x34ca36[_0xbbf5('0x40')],_0x34ca36[_0xbbf5('0x3c')]);_0x5bb210[_0xbbf5('0x44')]=_['intersection'](_0x34ca36['model'],qs[_0xbbf5('0x8e')](_0xe8933c[_0xbbf5('0x3c')]['fields']));_0x5bb210[_0xbbf5('0x44')]=_0x5bb210[_0xbbf5('0x44')][_0xbbf5('0x45')]?_0x5bb210[_0xbbf5('0x44')]:_0x34ca36[_0xbbf5('0x40')];if(!_0xe8933c[_0xbbf5('0x3c')][_0xbbf5('0x8f')](_0xbbf5('0x90'))){_0x5bb210[_0xbbf5('0x27')]=qs[_0xbbf5('0x27')](_0xe8933c['query'][_0xbbf5('0x27')]);_0x5bb210[_0xbbf5('0x25')]=qs[_0xbbf5('0x25')](_0xe8933c[_0xbbf5('0x3c')][_0xbbf5('0x25')]);}_0x5bb210[_0xbbf5('0x91')]=qs[_0xbbf5('0x92')](_0xe8933c[_0xbbf5('0x3c')]['sort']);_0x5bb210[_0xbbf5('0x93')]=qs['filters'](_[_0xbbf5('0x94')](_0xe8933c[_0xbbf5('0x3c')],_0x34ca36[_0xbbf5('0x42')]));_0x5bb210[_0xbbf5('0x93')][_0xbbf5('0x95')]=_0x21a645['id'];if(_0xe8933c[_0xbbf5('0x3c')][_0xbbf5('0x96')]){_0x5bb210[_0xbbf5('0x93')]=_[_0xbbf5('0x2e')](_0x5bb210[_0xbbf5('0x93')],{'$or':_[_0xbbf5('0x34')](_0x5bb210[_0xbbf5('0x44')],function(_0x274e7e){var _0x4ceced={};_0x4ceced[_0x274e7e]={'$like':'%'+_0xe8933c[_0xbbf5('0x3c')][_0xbbf5('0x96')]+'%'};return _0x4ceced;})});}if(_0xe8933c[_0xbbf5('0x3c')]['$gte']){var _0x3cf109=_0xe8933c[_0xbbf5('0x3c')][_0xbbf5('0x97')][_0xbbf5('0x98')](',');var _0x334ddd={};_0x334ddd[_0x3cf109[0x0]]={'$gte':moment(_0x3cf109[0x1])[_0xbbf5('0x76')](_0xbbf5('0x99'))};_0x5bb210[_0xbbf5('0x93')]=_[_0xbbf5('0x2e')](_0x5bb210['where'],_0x334ddd);}_0x5bb210=_['merge']({},_0x5bb210,_0xe8933c[_0xbbf5('0x47')]);return db[_0xbbf5('0x5e')][_0xbbf5('0x28')]({'where':_0x5bb210['where']})[_0xbbf5('0x15')](function(_0x5587a7){_0x54c38f[_0xbbf5('0x28')]=_0x5587a7;if(_0xe8933c[_0xbbf5('0x3c')][_0xbbf5('0x46')]){_0x5bb210[_0xbbf5('0x9a')]=[{'all':!![]}];}return db[_0xbbf5('0x5e')][_0xbbf5('0x3b')](_0x5bb210);})[_0xbbf5('0x15')](function(_0x134f8e){_0x54c38f[_0xbbf5('0x9b')]=_0x134f8e;return _0x54c38f;});}})[_0xbbf5('0x15')](respondWithFilteredResult(_0x155520,_0x5bb210))[_0xbbf5('0x20')](handleError(_0x155520,null));};exports[_0xbbf5('0x9c')]=function(_0x53d1d9,_0xc4cc9d){var _0x5e163c={'where':{}},_0x166271={},_0x5ae9b3={'count':0x0,'rows':[]};return db[_0xbbf5('0x35')]['findOne']({'where':{'id':_0x53d1d9[_0xbbf5('0x3f')]['id']}})[_0xbbf5('0x15')](handleEntityNotFound(_0xc4cc9d,null))[_0xbbf5('0x15')](function(_0x40684c){if(_0x40684c){_0x166271[_0xbbf5('0x40')]=_[_0xbbf5('0x41')](db[_0xbbf5('0x5e')]['rawAttributes']);_0x166271[_0xbbf5('0x3c')]=_[_0xbbf5('0x41')](_0x53d1d9[_0xbbf5('0x3c')]);_0x166271[_0xbbf5('0x42')]=_['intersection'](_0x166271[_0xbbf5('0x40')],_0x166271[_0xbbf5('0x3c')]);_0x5e163c[_0xbbf5('0x44')]=_[_0xbbf5('0x43')](_0x166271[_0xbbf5('0x40')],qs['fields'](_0x53d1d9['query']['fields']));_0x5e163c[_0xbbf5('0x44')]=_0x5e163c[_0xbbf5('0x44')][_0xbbf5('0x45')]?_0x5e163c[_0xbbf5('0x44')]:_0x166271[_0xbbf5('0x40')];if(!_0x53d1d9[_0xbbf5('0x3c')][_0xbbf5('0x8f')](_0xbbf5('0x90'))){_0x5e163c[_0xbbf5('0x27')]=qs[_0xbbf5('0x27')](_0x53d1d9[_0xbbf5('0x3c')][_0xbbf5('0x27')]);_0x5e163c['offset']=qs[_0xbbf5('0x25')](_0x53d1d9[_0xbbf5('0x3c')][_0xbbf5('0x25')]);}_0x5e163c[_0xbbf5('0x91')]=qs[_0xbbf5('0x92')](_0x53d1d9[_0xbbf5('0x3c')][_0xbbf5('0x92')]);_0x5e163c[_0xbbf5('0x93')]=qs['filters'](_['pick'](_0x53d1d9[_0xbbf5('0x3c')],_0x166271[_0xbbf5('0x42')]));_0x5e163c[_0xbbf5('0x93')][_0xbbf5('0x95')]=_0x40684c['id'];if(_0x53d1d9[_0xbbf5('0x3c')][_0xbbf5('0x96')]){_0x5e163c['where']=_[_0xbbf5('0x2e')](_0x5e163c[_0xbbf5('0x93')],{'$or':_[_0xbbf5('0x34')](_0x5e163c['attributes'],function(_0x307318){var _0x1af612={};_0x1af612[_0x307318]={'$like':'%'+_0x53d1d9[_0xbbf5('0x3c')][_0xbbf5('0x96')]+'%'};return _0x1af612;})});}_0x5e163c[_0xbbf5('0x93')][_0xbbf5('0x9d')]=![];if(_0x53d1d9[_0xbbf5('0x3c')]['id']){_0x5e163c['where']['id']={'$gt':_0x53d1d9[_0xbbf5('0x3c')]['id']};}_0x5e163c=_[_0xbbf5('0x2e')]({},_0x5e163c,_0x53d1d9[_0xbbf5('0x47')]);return db['ChatMessage'][_0xbbf5('0x28')]({'where':_0x5e163c[_0xbbf5('0x93')]})['then'](function(_0x2b00b6){_0x5ae9b3['count']=_0x2b00b6;_0x5e163c[_0xbbf5('0x9a')]=[];if(_0x53d1d9[_0xbbf5('0x3c')][_0xbbf5('0x9e')]){_0x5e163c[_0xbbf5('0x9a')][_0xbbf5('0x9f')]({'model':db[_0xbbf5('0x79')],'as':_0xbbf5('0x79'),'attributes':['id',_0xbbf5('0x50'),_0xbbf5('0x83')]});}if(_0x53d1d9[_0xbbf5('0x3c')]['includeAll']){_0x5e163c[_0xbbf5('0x9a')]=[{'all':!![]}];}return db[_0xbbf5('0x5e')][_0xbbf5('0x3b')](_0x5e163c);})[_0xbbf5('0x15')](function(_0x56f697){_0x5ae9b3[_0xbbf5('0x9b')]=_0x56f697;return _0x5ae9b3;});}})[_0xbbf5('0x15')](respondWithFilteredResult(_0xc4cc9d,_0x5e163c))[_0xbbf5('0x20')](handleError(_0xc4cc9d,null));};exports[_0xbbf5('0xa0')]=function(_0x552d4c,_0x3a923e){return db[_0xbbf5('0x35')][_0xbbf5('0x48')]({'where':{'id':_0x552d4c[_0xbbf5('0x3f')]['id']}})[_0xbbf5('0x15')](handleEntityNotFound(_0x3a923e,null))[_0xbbf5('0x15')](function(_0x6159e8){if(_0x6159e8){return _0x6159e8[_0xbbf5('0xa1')](_0x552d4c[_0xbbf5('0x2f')][_0xbbf5('0x8c')],_['omit'](_0x552d4c['body'],[_0xbbf5('0x8c'),'id'])||{})[_0xbbf5('0xa2')](function(){return db[_0xbbf5('0xa3')]['findAll']({'attributes':['id',_0xbbf5('0x2c'),_0xbbf5('0xa4')],'where':{'id':_0x552d4c[_0xbbf5('0x2f')][_0xbbf5('0x8c')]}});})[_0xbbf5('0x15')](function(_0x2ac4e7){socket[_0xbbf5('0xa5')](_0xbbf5('0xa6'),{'id':Number(_0x552d4c['params']['id']),'tags':_0x2ac4e7||[]});return{'id':Number(_0x552d4c[_0xbbf5('0x3f')]['id']),'tags':_0x2ac4e7||[]};});}})[_0xbbf5('0x15')](respondWithResult(_0x3a923e,null))[_0xbbf5('0x20')](handleError(_0x3a923e,null));};exports[_0xbbf5('0xa7')]=function(_0x243a4c,_0x2a3009){return db[_0xbbf5('0x35')][_0xbbf5('0x48')]({'where':{'id':_0x243a4c[_0xbbf5('0x3f')]['id']}})[_0xbbf5('0x15')](handleEntityNotFound(_0x2a3009,null))['then'](function(_0x35a7a1){if(_0x35a7a1){return _0x35a7a1[_0xbbf5('0xa7')](_0x243a4c[_0xbbf5('0x3c')]['ids']);}})[_0xbbf5('0x15')](respondWithStatusCode(_0x2a3009,null))[_0xbbf5('0x20')](handleError(_0x2a3009,null));};exports['download']=function(_0x1d4188,_0x57c8b3){var _0x4ddb05=moment()[_0xbbf5('0xa8')]()[_0xbbf5('0xa9')]();var _0x252c44=path[_0xbbf5('0x70')](config[_0xbbf5('0x71')],_0xbbf5('0xaa'),_0xbbf5('0xab'),'tmp');var _0x3330c4=path[_0xbbf5('0x70')](config[_0xbbf5('0x71')],'server',_0xbbf5('0xab'),_0xbbf5('0xac'));var _0x31116d=path[_0xbbf5('0x70')](_0x252c44,_0x4ddb05);var _0x2084ad=util[_0xbbf5('0x76')]('transcript-%d-%s.zip',_0x1d4188[_0xbbf5('0x3f')]['id'],_0x4ddb05);var _0x469307=path[_0xbbf5('0x70')](_0x252c44,_0x2084ad);var _0x4a0fe0=[];_0x4a0fe0[_0xbbf5('0x9f')]({'model':db[_0xbbf5('0x79')],'as':'User','attributes':['id',_0xbbf5('0x50')],'raw':!![]});_0x4a0fe0[_0xbbf5('0x9f')]({'model':db[_0xbbf5('0x60')],'as':'Contact','attributes':['id',_0xbbf5('0xad'),'lastName'],'raw':!![]});if(_0x1d4188[_0xbbf5('0x3c')][_0xbbf5('0xac')]){_0x4a0fe0[_0xbbf5('0x9f')]({'model':db[_0xbbf5('0x6c')],'as':_0xbbf5('0x6c'),'raw':!![]});}var _0x4c6585=[{'model':db[_0xbbf5('0x5e')],'as':_0xbbf5('0x7a'),'attributes':['id',_0xbbf5('0x2f'),_0xbbf5('0x84'),'direction',_0xbbf5('0x9d'),_0xbbf5('0xae')],'include':_0x4a0fe0}];_0x4c6585['push']({'model':db['ChatWebsite'],'as':_0xbbf5('0xaf'),'attributes':['id','name']});_0x4c6585[_0xbbf5('0x9f')]({'model':db[_0xbbf5('0x79')],'as':_0xbbf5('0xb0'),'attributes':['id',_0xbbf5('0x50')]});return db[_0xbbf5('0x35')][_0xbbf5('0x48')]({'where':{'id':_0x1d4188['params']['id']},'include':_0x4c6585})['then'](handleEntityNotFound(_0x57c8b3,null))[_0xbbf5('0x15')](function(_0x1c7592){if(_0x1c7592){var _0x212223=_0x1c7592[_0xbbf5('0xb1')]({'plain':!![]});fs[_0xbbf5('0xb2')](_0x31116d);var _0x5efcdd={'channel':_0xbbf5('0xb3'),'account':_0x212223['Account'][_0xbbf5('0x2c')],'agent':_0x212223[_0xbbf5('0xb0')]?_0x212223['Owner']['fullname']:'N.A.','createdAt':moment(_0x212223['createdAt'])[_0xbbf5('0x76')](_0xbbf5('0x99'))['toString'](),'closedAt':_0x212223[_0xbbf5('0xb4')]?moment(_0x212223[_0xbbf5('0xb5')])['format']('YYYY-MM-DD\x20HH:mm:ss')[_0xbbf5('0xa9')]():'','disposition':_0x212223[_0xbbf5('0xb6')],'chatInfo':{'browser':_0x212223['browserName'],'os':_0x212223[_0xbbf5('0xb7')],'referer':_0x212223['referer'],'customerIp':_0x212223[_0xbbf5('0xb8')],'customerPort':_0x212223[_0xbbf5('0xb9')]?_0x212223[_0xbbf5('0xb9')]:_0xbbf5('0xba')},'messages':_(_0x212223[_0xbbf5('0x7a')])[_0xbbf5('0xbb')]()[_0xbbf5('0x96')]({'secret':![]})[_0xbbf5('0x34')](function(_0x4b73da){if(_0x4b73da['Attachment']){var _0x45c035=path[_0xbbf5('0x70')](_0x3330c4,_0x4b73da[_0xbbf5('0x6c')][_0xbbf5('0x69')]);if(fs['existsSync'](_0x45c035)){fs[_0xbbf5('0xbc')](_0x45c035)[_0xbbf5('0xbd')](fs[_0xbbf5('0xbe')](path[_0xbbf5('0x70')](_0x31116d,_0x4b73da[_0xbbf5('0x6c')]['basename'])));}}return{'date':moment(_0x4b73da[_0xbbf5('0x84')])[_0xbbf5('0x76')](_0xbbf5('0x99'))[_0xbbf5('0xa9')](),'sender':_0x4b73da['direction']==='out'?_0x4b73da[_0xbbf5('0x79')]?_0x4b73da[_0xbbf5('0x79')][_0xbbf5('0x50')]:_0xbbf5('0x7d'):_0x4b73da[_0xbbf5('0xbf')]['firstName']+(_0x4b73da['Contact'][_0xbbf5('0xc0')]?'\x20'+_0x4b73da['Contact'][_0xbbf5('0xc0')]:''),'body':_0x4b73da['Attachment']?_0x4b73da[_0xbbf5('0x6c')][_0xbbf5('0x69')]:_0x4b73da['body'],'direction':_0x4b73da[_0xbbf5('0xc1')]===_0xbbf5('0x5d')?'A':'C','secret':_0x4b73da['secret'],'read':_0x4b73da[_0xbbf5('0xae')],'attachment':_0x4b73da[_0xbbf5('0x6c')]};})[_0xbbf5('0xc2')]()};return ejs['renderFile'](path[_0xbbf5('0x70')](config[_0xbbf5('0x71')],_0xbbf5('0xaa'),_0xbbf5('0xc3'),_0xbbf5('0xc4')),{'interaction':_0x5efcdd})[_0xbbf5('0x15')](function(_0x15d7e7){var _0xbf0523=path[_0xbbf5('0x70')](_0x31116d,_0xbbf5('0xc5')+_0x212223['id']+'-'+_0x4ddb05+_0xbbf5('0xc6'));var _0x3a80b6={'path':_0xbf0523,'channel':_0xbbf5('0xb3'),'interactionId':_0x212223['id']};return pdf[_0xbbf5('0xc7')](_0x15d7e7,_0x3a80b6);})[_0xbbf5('0x15')](function(){return new Promise(function(_0x1ba24c,_0x2736be){zipdir(_0x31116d,{'saveTo':_0x469307},function(_0x4363d2,_0xdc0629){if(_0x4363d2)return _0x2736be(_0x4363d2);return _0x1ba24c(_0xdc0629);});})[_0xbbf5('0x15')](function(){return new BPromise(function(_0x48fce0,_0x5860a8){rimraf(_0x31116d,function(_0x319b0f){if(_0x319b0f)_0x5860a8(_0x319b0f);return _0x48fce0();});});})[_0xbbf5('0x15')](function(){return _0x57c8b3[_0xbbf5('0x73')](_0x469307,_0x2084ad,function(_0x504b03){if(_0x504b03){logger[_0xbbf5('0x1a')]('err',_0x504b03);}else{fs[_0xbbf5('0xc8')](_0x469307);}});});});}else{return _0x57c8b3[_0xbbf5('0x21')](0xc8);}})[_0xbbf5('0x20')](handleError(_0x57c8b3,null));};