c219f22fc0e33eeb5a5f1779f38f8ad601e890f4
[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 _0x4bf6=['existsSync','createReadStream','createWriteStream','out','firstName','value','views','downloadInteraction.ejs','transcript-','.pdf','createPdfFromHTML','err','unlinkSync','rimraf','zip-dir','request-promise','moment','mustache','util','lodash','ioredis','../../components/interaction/service','../../components/export/pdf','../../config/logger','api','../../mysqldb','redis','defaults','socket.io-emitter','./chatInteraction.socket','register','jayson/promise','client','http','request','then','info','ChatInteraction,\x20%s,\x20%s','request\x20sent','debug','ChatInteraction,\x20%s,\x20%s,\x20%s','stringify','error','code','message','result','catch','sendStatus','status','end','json','undefined','limit','count','offset','set','Content-Range','update','destroy','stack','name','send','render','interaction','map','ChatInteraction','fieldName','type','getOptions','findAll','chat','query','user','show','model','keys','intersection','attributes','fields','length','includeAll','merge','find','create','body','params','describe','findOne','ChatWebsite','ChatWebsiteId','vidaooTopic','vidaooNote','vidaooMetadata','vidaooApiKey','vidaooEscalation','fullname','email','contact','account','POST','isNull','follow','parse','Sequelize','ValidationError','Error\x20in\x20Vidaoo\x20configuration','join_url','Unable\x20to\x20create\x20Vidaoo\x20session','updateAttributes','customUpdate','CmContact','Contact','abandon','abandonInteraction,\x20%s,\x20%s,\x20%s','Interaction\x20already\x20assigned','closed','disposition','attachmentUpload','file','originalname','filename','mimetype','Attachment','attachmentDownload','Unauthorized','ChatOfflineMessageId','join','root','offline-chat/','basename','download','close','mailTranscript','server/files/chat/upload/','format','transcript-%s.txt','now','pathTranscript','ChatMessage','Messages','User','ContactId','UserId','%s%d','customer','Visitor','agentIdentifier','website_alias','agentAlias','alias','System','agent_fullname','[%s]\x20%s:\x20%s\x0a','createdAt','MM/DD/YYYY\x20HH:mm:ss','direction','appendFileSync','MailAccount','CloudProvider','Smtp','SendMail','sendMail','Mail\x20Transcript','Hi\x20%s,\x20attached\x20you\x20can\x20find\x20your\x20transcript\x20file.\x20\x0a\x0aRegards.\x20\x0a\x0a','addMessage','ids','filters','hasOwnProperty','nolimit','order','sort','where','pick','filter','$gte','YYYY-MM-DD\x20HH:mm:ss','options','include','rows','getMyMessages','rawAttributes','ChatInteractionId','secret','omit','spread','color','emit','chatInteractionTags:save','removeTags','unix','server','files','push','lastName','attachments','read','Owner','get','Chat','Account','N.A.','toString','closedAt','osName','referer','customerIp','customerPort','tail'];(function(_0x49b207,_0x408ade){var _0x2dc8fa=function(_0x150975){while(--_0x150975){_0x49b207['push'](_0x49b207['shift']());}};_0x2dc8fa(++_0x408ade);}(_0x4bf6,0xdd));var _0x64bf=function(_0x3bd353,_0x493b19){_0x3bd353=_0x3bd353-0x0;var _0x2f1304=_0x4bf6[_0x3bd353];return _0x2f1304;};'use strict';var rimraf=require(_0x64bf('0x0'));var zipdir=require(_0x64bf('0x1'));var rp=require(_0x64bf('0x2'));var moment=require(_0x64bf('0x3'));var BPromise=require('bluebird');var Mustache=require(_0x64bf('0x4'));var util=require(_0x64bf('0x5'));var path=require('path');var ejs=require('ejs');var fs=require('fs');var _=require(_0x64bf('0x6'));var Redis=require(_0x64bf('0x7'));var interaction=require(_0x64bf('0x8'));var qs=require('../../components/parsers/qs');var pdf=require(_0x64bf('0x9'));var logger=require(_0x64bf('0xa'))(_0x64bf('0xb'));var config=require('../../config/environment');var db=require(_0x64bf('0xc'))['db'];config[_0x64bf('0xd')]=_[_0x64bf('0xe')](config['redis'],{'host':'localhost','port':0x18eb});var socket=require(_0x64bf('0xf'))(new Redis(config[_0x64bf('0xd')]));require(_0x64bf('0x10'))[_0x64bf('0x11')](socket);var jayson=require(_0x64bf('0x12'));var client=jayson[_0x64bf('0x13')][_0x64bf('0x14')]({'port':0x232b});var client9004=jayson[_0x64bf('0x13')][_0x64bf('0x14')]({'port':0x232c});function respondWithRpcPromise(_0x3bc3b1,_0x52566e,_0x41a2e3,_0x1fce11){return new BPromise(function(_0x1cbdec,_0x53b09e){var _0x5d4871=_0x1fce11||client;return _0x5d4871[_0x64bf('0x15')](_0x3bc3b1,_0x41a2e3)[_0x64bf('0x16')](function(_0x4632bd){logger[_0x64bf('0x17')](_0x64bf('0x18'),_0x52566e,_0x64bf('0x19'));logger[_0x64bf('0x1a')](_0x64bf('0x1b'),_0x52566e,_0x64bf('0x19'),JSON[_0x64bf('0x1c')](_0x4632bd));if(_0x4632bd[_0x64bf('0x1d')]){if(_0x4632bd[_0x64bf('0x1d')][_0x64bf('0x1e')]===0x1f4){logger[_0x64bf('0x1d')](_0x64bf('0x18'),_0x52566e,_0x4632bd[_0x64bf('0x1d')][_0x64bf('0x1f')]);return _0x53b09e(_0x4632bd[_0x64bf('0x1d')][_0x64bf('0x1f')]);}logger[_0x64bf('0x1d')](_0x64bf('0x18'),_0x52566e,_0x4632bd[_0x64bf('0x1d')]['message']);return _0x1cbdec(_0x4632bd[_0x64bf('0x1d')]['message']);}else{logger[_0x64bf('0x17')](_0x64bf('0x18'),_0x52566e,_0x64bf('0x19'));_0x1cbdec(_0x4632bd[_0x64bf('0x20')][_0x64bf('0x1f')]);}})[_0x64bf('0x21')](function(_0x3cdf33){logger['error'](_0x64bf('0x18'),_0x52566e,_0x3cdf33);_0x53b09e(_0x3cdf33);});});}function respondWithStatusCode(_0x436524,_0x4d2c30){_0x4d2c30=_0x4d2c30||0xcc;return function(_0xcefd03){if(_0xcefd03){return _0x436524[_0x64bf('0x22')](_0x4d2c30);}return _0x436524[_0x64bf('0x23')](_0x4d2c30)[_0x64bf('0x24')]();};}function respondWithResult(_0x5d887c,_0xd3e105){_0xd3e105=_0xd3e105||0xc8;return function(_0x2360a9){if(_0x2360a9){return _0x5d887c['status'](_0xd3e105)[_0x64bf('0x25')](_0x2360a9);}};}function respondWithFilteredResult(_0xff0309,_0x5cd4e0){return function(_0x57bb29){if(_0x57bb29){var _0x308e6a=typeof _0x5cd4e0['offset']===_0x64bf('0x26')&&typeof _0x5cd4e0[_0x64bf('0x27')]===_0x64bf('0x26');var _0x36d955=_0x57bb29[_0x64bf('0x28')];var _0x39e2bc=_0x308e6a?0x0:_0x5cd4e0[_0x64bf('0x29')];var _0x5b5a05=_0x308e6a?_0x57bb29['count']:_0x5cd4e0[_0x64bf('0x29')]+_0x5cd4e0[_0x64bf('0x27')];var _0x2dd122;if(_0x5b5a05>=_0x36d955){_0x5b5a05=_0x36d955;_0x2dd122=0xc8;}else{_0x2dd122=0xce;}_0xff0309[_0x64bf('0x23')](_0x2dd122);return _0xff0309[_0x64bf('0x2a')](_0x64bf('0x2b'),_0x39e2bc+'-'+_0x5b5a05+'/'+_0x36d955)['json'](_0x57bb29);}return null;};}function saveUpdates(_0x15d909){return function(_0x15d9c6){if(_0x15d9c6){return _0x15d9c6[_0x64bf('0x2c')](_0x15d909)[_0x64bf('0x16')](function(_0x4deef1){return _0x4deef1;});}return null;};}function removeEntity(_0x37cc9c){return function(_0x40a1e3){if(_0x40a1e3){return _0x40a1e3[_0x64bf('0x2d')]()[_0x64bf('0x16')](function(){_0x37cc9c[_0x64bf('0x23')](0xcc)[_0x64bf('0x24')]();});}};}function handleEntityNotFound(_0x49eb7b){return function(_0x318e11){if(!_0x318e11){_0x49eb7b['sendStatus'](0x194);}return _0x318e11;};}function handleError(_0x56aed3,_0x510815){_0x510815=_0x510815||0x1f4;return function(_0x4dcda6){logger['error'](_0x4dcda6[_0x64bf('0x2e')]);if(_0x4dcda6[_0x64bf('0x2f')]){delete _0x4dcda6[_0x64bf('0x2f')];}_0x56aed3[_0x64bf('0x23')](_0x510815)[_0x64bf('0x30')](_0x4dcda6);};}function getMustacheRender(_0xa12135,_0x40698e){return Mustache[_0x64bf('0x31')](_0xa12135,_['merge'](_0x40698e['body'],{'contact':_0x40698e['contact'],'message':_0x40698e[_0x64bf('0x1f')],'agent':_0x40698e['agent'],'interaction':_0x40698e[_0x64bf('0x32')],'account':_0x40698e['account']}))||'';}exports['index']=function(_0x3d75ce,_0x449f34){var _0x339e1a={};var _0x11b910=_[_0x64bf('0x33')](db[_0x64bf('0x34')]['rawAttributes'],function(_0x1b8469){return{'name':_0x1b8469[_0x64bf('0x35')],'type':_0x1b8469[_0x64bf('0x36')]['key']};});_0x339e1a=qs[_0x64bf('0x37')](_0x11b910,_0x3d75ce);return interaction[_0x64bf('0x38')](_0x64bf('0x39'),_0x3d75ce[_0x64bf('0x3a')],_0x3d75ce[_0x64bf('0x3b')],_0x339e1a,_0x11b910)[_0x64bf('0x16')](respondWithFilteredResult(_0x449f34,_0x339e1a))['catch'](handleError(_0x449f34,null));};exports[_0x64bf('0x3c')]=function(_0x33a1b9,_0x1ddba2){var _0x4e38aa={'raw':![],'where':{'id':_0x33a1b9['params']['id']}},_0x5b0781={};_0x5b0781[_0x64bf('0x3d')]=_[_0x64bf('0x3e')](db[_0x64bf('0x34')]['rawAttributes']);_0x5b0781[_0x64bf('0x3a')]=_[_0x64bf('0x3e')](_0x33a1b9[_0x64bf('0x3a')]);_0x5b0781['filters']=_[_0x64bf('0x3f')](_0x5b0781['model'],_0x5b0781[_0x64bf('0x3a')]);_0x4e38aa[_0x64bf('0x40')]=_[_0x64bf('0x3f')](_0x5b0781[_0x64bf('0x3d')],qs[_0x64bf('0x41')](_0x33a1b9[_0x64bf('0x3a')][_0x64bf('0x41')]));_0x4e38aa[_0x64bf('0x40')]=_0x4e38aa[_0x64bf('0x40')][_0x64bf('0x42')]?_0x4e38aa[_0x64bf('0x40')]:_0x5b0781[_0x64bf('0x3d')];if(_0x33a1b9[_0x64bf('0x3a')][_0x64bf('0x43')]){_0x4e38aa['include']=[{'all':!![]}];}_0x4e38aa=_[_0x64bf('0x44')]({},_0x4e38aa,_0x33a1b9['options']);return db[_0x64bf('0x34')][_0x64bf('0x45')](_0x4e38aa)[_0x64bf('0x16')](handleEntityNotFound(_0x1ddba2,null))[_0x64bf('0x16')](respondWithResult(_0x1ddba2,null))['catch'](handleError(_0x1ddba2,null));};exports[_0x64bf('0x46')]=function(_0x381e60,_0x8ca264){return db[_0x64bf('0x34')][_0x64bf('0x46')](_0x381e60[_0x64bf('0x47')],{})[_0x64bf('0x16')](respondWithResult(_0x8ca264,0xc9))[_0x64bf('0x21')](handleError(_0x8ca264,null));};exports[_0x64bf('0x2c')]=function(_0x1e0fdb,_0x2d20e1){if(_0x1e0fdb['body']['id']){delete _0x1e0fdb[_0x64bf('0x47')]['id'];}return db[_0x64bf('0x34')][_0x64bf('0x45')]({'where':{'id':_0x1e0fdb[_0x64bf('0x48')]['id']}})['then'](handleEntityNotFound(_0x2d20e1,null))[_0x64bf('0x16')](saveUpdates(_0x1e0fdb[_0x64bf('0x47')],null))[_0x64bf('0x16')](respondWithResult(_0x2d20e1,null))[_0x64bf('0x21')](handleError(_0x2d20e1,null));};exports[_0x64bf('0x2d')]=function(_0x3a55bc,_0xe53651){return db['ChatInteraction'][_0x64bf('0x45')]({'where':{'id':_0x3a55bc['params']['id']}})[_0x64bf('0x16')](handleEntityNotFound(_0xe53651,null))[_0x64bf('0x16')](removeEntity(_0xe53651,null))['catch'](handleError(_0xe53651,null));};exports[_0x64bf('0x49')]=function(_0x1e5ccf,_0x22fa02){return db[_0x64bf('0x34')]['describe']()[_0x64bf('0x16')](respondWithResult(_0x22fa02,null))[_0x64bf('0x21')](handleError(_0x22fa02,null));};exports['createVidaooSession']=function(_0x3f1215,_0x1ff283){var _0x425ef2=_0x3f1215[_0x64bf('0x48')]['id'];var _0x229f5f;var _0x4e8bfa;var _0x3cb19e;return db[_0x64bf('0x34')][_0x64bf('0x4a')]({'where':{'id':_0x425ef2}})[_0x64bf('0x16')](function(_0x9a56f1){_0x229f5f=_0x9a56f1;return db[_0x64bf('0x4b')][_0x64bf('0x4a')]({'where':{'id':_0x9a56f1[_0x64bf('0x4c')]},'attributes':['id','vidaooApiKey','vidaooEscalation',_0x64bf('0x4d'),_0x64bf('0x4e'),_0x64bf('0x4f')],'raw':!![]});})[_0x64bf('0x16')](function(_0x549289){if(_0x549289&&_0x549289[_0x64bf('0x50')]&&_0x549289[_0x64bf('0x51')]){_0x4e8bfa=_0x549289;var _0x76a157={'interaction':{'id':_0x425ef2},'agent':{'id':_0x3f1215[_0x64bf('0x3b')]['id'],'name':_0x3f1215[_0x64bf('0x3b')]['name'],'fullname':_0x3f1215[_0x64bf('0x3b')][_0x64bf('0x52')],'email':_0x3f1215[_0x64bf('0x3b')][_0x64bf('0x53')]},'contact':_0x3f1215[_0x64bf('0x47')][_0x64bf('0x54')],'account':_0x3f1215['body'][_0x64bf('0x55')]};var _0x114863={'rejectUnauthorized':![],'method':_0x64bf('0x56'),'url':'https://api-vidaoo.xcally.com/api/v1/meetings','headers':{'X-APIKEY':_0x549289[_0x64bf('0x50')]},'body':{'duration':null,'scheduledAt':null,'participants':[_0x3f1215[_0x64bf('0x3b')][_0x64bf('0x53')]],'buttons':null,'topic':_0x4e8bfa['vidaooTopic'],'note':_[_0x64bf('0x57')](_0x4e8bfa['vidaooNote'])?null:getMustacheRender(_0x4e8bfa[_0x64bf('0x4e')],_0x76a157),'metadata':null},'redirect':_0x64bf('0x58'),'json':!![]};try{if(!_[_0x64bf('0x57')](_0x4e8bfa[_0x64bf('0x4f')])){var _0x406822=getMustacheRender(_0x4e8bfa[_0x64bf('0x4f')],_0x76a157);var _0x1a0ddd=JSON[_0x64bf('0x59')](_0x406822);_0x114863[_0x64bf('0x47')]['metadata']=_0x1a0ddd;}}catch(_0x154037){throw new db['Sequelize']['ValidationError']('Error\x20in\x20conversion\x20vidaooMetadata');}return rp(_0x114863);}else{throw new db[(_0x64bf('0x5a'))][(_0x64bf('0x5b'))](_0x64bf('0x5c'));}})[_0x64bf('0x16')](function(_0x73e862){_0x3cb19e=_0x73e862;if(_0x3cb19e['id']){var _0x21d396={'body':_0x3cb19e[_0x64bf('0x5d')],'ChatWebsiteId':_0x4e8bfa['id'],'ChatInteractionId':_0x229f5f['id'],'direction':'out','UserId':_0x3f1215[_0x64bf('0x3b')]['id']};return db['ChatMessage'][_0x64bf('0x46')](_0x21d396,{});}else{throw new db[(_0x64bf('0x5a'))]['ValidationError'](_0x64bf('0x5e'));}})[_0x64bf('0x16')](function(){return _0x229f5f[_0x64bf('0x5f')]({'vidaooSessionId':_0x3cb19e['id']});})['then'](respondWithResult(_0x1ff283,null))[_0x64bf('0x21')](handleError(_0x1ff283,null));};exports[_0x64bf('0x60')]=function(_0x26cc50,_0x5cadd5){if(_0x26cc50[_0x64bf('0x47')]['id']){delete _0x26cc50['body']['id'];}return db[_0x64bf('0x34')]['find']({'where':{'id':_0x26cc50[_0x64bf('0x48')]['id']},'include':[{'model':db[_0x64bf('0x61')],'as':_0x64bf('0x62')}]})[_0x64bf('0x16')](handleEntityNotFound(_0x5cadd5,null))[_0x64bf('0x16')](saveUpdates(_0x26cc50[_0x64bf('0x47')],null))[_0x64bf('0x16')](respondWithResult(_0x5cadd5,null))['catch'](handleError(_0x5cadd5,null));};exports[_0x64bf('0x63')]=function(_0x7ab698,_0x16a14f){if(_0x7ab698[_0x64bf('0x47')]['id']){delete _0x7ab698[_0x64bf('0x47')]['id'];}return db[_0x64bf('0x34')][_0x64bf('0x45')]({'where':{'id':_0x7ab698['params']['id']}})[_0x64bf('0x16')](function(_0x143a99){if(!_0x143a99){logger[_0x64bf('0x1d')](_0x64bf('0x64'),_0x64bf('0x63'),0x194,'entity\x20not\x20found');_0x16a14f['sendStatus'](0x194);}else{if(_0x143a99['UserId']){throw new db['Sequelize']['ValidationError'](_0x64bf('0x65'));}}return _0x143a99;})['then'](function(_0x5449fe){if(_0x5449fe){return _0x5449fe[_0x64bf('0x2c')]({'closed':_0x7ab698['body'][_0x64bf('0x66')],'disposition':_0x7ab698[_0x64bf('0x47')][_0x64bf('0x67')]})[_0x64bf('0x16')](function(_0x2be8c4){return _0x2be8c4;});}})[_0x64bf('0x16')](function(){return respondWithRpcPromise('AbandonInteraction','abandon',_0x7ab698['body'],client9004);})['then'](respondWithResult(_0x16a14f,null))[_0x64bf('0x21')](handleError(_0x16a14f,null));};exports[_0x64bf('0x68')]=function(_0x12a538,_0x3e6e27){_0x12a538[_0x64bf('0x47')][_0x64bf('0x2f')]=_0x12a538[_0x64bf('0x69')][_0x64bf('0x6a')];_0x12a538['body']['basename']=_0x12a538[_0x64bf('0x69')][_0x64bf('0x6b')];_0x12a538[_0x64bf('0x47')][_0x64bf('0x36')]=_0x12a538[_0x64bf('0x69')][_0x64bf('0x6c')];return db[_0x64bf('0x6d')][_0x64bf('0x46')](_0x12a538[_0x64bf('0x47')])[_0x64bf('0x16')](respondWithResult(_0x3e6e27,0xc9))[_0x64bf('0x21')](handleError(_0x3e6e27,null));};exports[_0x64bf('0x6e')]=function(_0x1e712e,_0x448dd0){return db[_0x64bf('0x34')][_0x64bf('0x4a')]({'where':{'id':_0x1e712e[_0x64bf('0x48')]['id']},'raw':!![]})[_0x64bf('0x16')](handleEntityNotFound(_0x448dd0,null))[_0x64bf('0x16')](function(_0x253d44){if(_0x253d44){if(_0x253d44[_0x64bf('0x66')]){return _0x448dd0[_0x64bf('0x23')](0x193)[_0x64bf('0x25')]({'status':0x193,'message':_0x64bf('0x6f')});}return db[_0x64bf('0x6d')][_0x64bf('0x4a')]({'where':{'id':_0x1e712e[_0x64bf('0x3a')]['attachId']},'raw':!![]})[_0x64bf('0x16')](handleEntityNotFound(_0x448dd0,null));}})[_0x64bf('0x16')](function(_0x54f9fe){if(_0x54f9fe){var _0x4a4631=_0x54f9fe[_0x64bf('0x70')]!==null;var _0x2c25ca=path[_0x64bf('0x71')](config[_0x64bf('0x72')],'server/files/attachments/');if(_0x4a4631){_0x2c25ca=path[_0x64bf('0x71')](_0x2c25ca,_0x64bf('0x73'));}var _0x4f035c=path[_0x64bf('0x71')](_0x2c25ca,_0x54f9fe[_0x64bf('0x74')]);return _0x448dd0[_0x64bf('0x75')](_0x4f035c);}})[_0x64bf('0x21')](handleError(_0x448dd0,null));};exports[_0x64bf('0x76')]=function(_0x3b1d3b,_0x410768){var _0x3804b3=null;var _0x4a3156=null;var _0x128087=null;if(_0x3b1d3b['body']['id']){delete _0x3b1d3b[_0x64bf('0x47')]['id'];}if(_0x3b1d3b[_0x64bf('0x47')][_0x64bf('0x77')]){_0x128087=path[_0x64bf('0x71')](config[_0x64bf('0x72')],_0x64bf('0x78'),util[_0x64bf('0x79')](_0x64bf('0x7a'),Date[_0x64bf('0x7b')]()));_0x3b1d3b[_0x64bf('0x47')][_0x64bf('0x7c')]=_0x128087;}return db['ChatInteraction']['find']({'where':{'id':_0x3b1d3b['params']['id']},'include':[{'model':db[_0x64bf('0x7d')],'as':_0x64bf('0x7e'),'include':[{'model':db[_0x64bf('0x7f')],'as':'User'}]}]})[_0x64bf('0x16')](handleEntityNotFound(_0x410768,null))[_0x64bf('0x16')](saveUpdates(_0x3b1d3b[_0x64bf('0x47')],null))[_0x64bf('0x16')](function(_0x4da5ba){if(_0x4da5ba){_0x3804b3=_0x4da5ba;return db[_0x64bf('0x4b')][_0x64bf('0x45')]({'where':{'id':_0x3804b3[_0x64bf('0x4c')]}});}})[_0x64bf('0x16')](function(_0xa18e27){if(_0xa18e27){_0x4a3156=_0xa18e27;return db['CmContact'][_0x64bf('0x45')]({'where':{'id':_0x3804b3[_0x64bf('0x80')]}});}})[_0x64bf('0x16')](function(_0x383dc7){if(_0x383dc7){if(_0x128087){var _0x3aa0fa=null;for(var _0x792fb9=0x1;_0x792fb9<_0x3804b3[_0x64bf('0x7e')][_0x64bf('0x42')];++_0x792fb9){_0x3aa0fa=_0x3804b3[_0x64bf('0x7e')][_0x792fb9];var _0x59c646=_0x3aa0fa[_0x64bf('0x81')]?util['format'](_0x64bf('0x82'),_0x4a3156['agentAlias'],_0x3aa0fa['UserId']):'System';var _0x531391=_0x3b1d3b[_0x64bf('0x47')][_0x64bf('0x83')]||util[_0x64bf('0x79')](_0x64bf('0x82'),_0x64bf('0x84'),_0x3aa0fa['ContactId']);switch(_0x4a3156[_0x64bf('0x85')]){case _0x64bf('0x86'):_0x59c646=_0x3aa0fa[_0x64bf('0x81')]?util[_0x64bf('0x79')](_0x64bf('0x82'),_0x4a3156[_0x64bf('0x87')],_0x3aa0fa['UserId']):'System';break;case'agent_alias':_0x59c646=_0x3aa0fa[_0x64bf('0x81')]?_0x3aa0fa[_0x64bf('0x7f')][_0x64bf('0x88')]||_0x3aa0fa[_0x64bf('0x7f')]['fullname']:_0x64bf('0x89');break;case _0x64bf('0x8a'):_0x59c646=_0x3aa0fa[_0x64bf('0x81')]?_0x3aa0fa[_0x64bf('0x7f')][_0x64bf('0x52')]:_0x64bf('0x89');break;default:_0x59c646=_0x3aa0fa[_0x64bf('0x81')]?util['format'](_0x64bf('0x82'),_0x4a3156[_0x64bf('0x87')],_0x3aa0fa['UserId']):_0x64bf('0x89');}var _0x42649e=util['format'](_0x64bf('0x8b'),moment(_0x3aa0fa[_0x64bf('0x8c')])[_0x64bf('0x79')](_0x64bf('0x8d')),_0x3aa0fa[_0x64bf('0x8e')]==='in'?_0x531391:_0x59c646,_0x3aa0fa[_0x64bf('0x47')]);if(!_0x3aa0fa['secret']){fs[_0x64bf('0x8f')](_0x128087,_0x42649e);}}return db[_0x64bf('0x90')][_0x64bf('0x4a')]({'where':{'service':!![]},'include':[{'model':db['MailServerOut'],'as':'Smtp','raw':!![],'include':[{'model':db[_0x64bf('0x91')],'as':_0x64bf('0x91'),'raw':!![]}]}]});}}})[_0x64bf('0x16')](function(_0x83efeb){if(!_0x83efeb||!_0x83efeb[_0x64bf('0x92')]||!_0x3b1d3b['body'])return _0x3804b3;var _0x4d629f=_0x83efeb['getSmtpOptions']();return respondWithRpcPromise(_0x64bf('0x93'),_0x64bf('0x94'),{'account':_0x4d629f,'message':{'from':util[_0x64bf('0x79')]('\x22%s\x22\x20<%s>',_0x83efeb[_0x64bf('0x2f')],_0x83efeb[_0x64bf('0x53')]||_0x83efeb['Smtp'][_0x64bf('0x3b')]),'to':_0x3b1d3b[_0x64bf('0x47')][_0x64bf('0x77')],'subject':_0x64bf('0x95'),'text':util['format'](_0x64bf('0x96'),_0x3b1d3b[_0x64bf('0x47')][_0x64bf('0x77')]),'attachments':[{'path':_0x128087}]}})[_0x64bf('0x16')](function(){return _0x3804b3;})[_0x64bf('0x21')](function(_0x5599a6){logger[_0x64bf('0x1d')](_0x5599a6);});})[_0x64bf('0x16')](respondWithStatusCode(_0x410768,0xc8))[_0x64bf('0x21')](handleError(_0x410768,null));};exports[_0x64bf('0x97')]=function(_0x5f58a6,_0x47a759){return db[_0x64bf('0x7d')]['find']({'where':{'id':_0x5f58a6[_0x64bf('0x48')]['id']}})[_0x64bf('0x16')](handleEntityNotFound(_0x47a759,null))[_0x64bf('0x16')](function(_0x11b4ae){if(_0x11b4ae){return _0x11b4ae[_0x64bf('0x97')](_0x5f58a6[_0x64bf('0x47')][_0x64bf('0x98')],_['omit'](_0x5f58a6[_0x64bf('0x47')],['ids','id'])||{});}})['then'](respondWithResult(_0x47a759,null))[_0x64bf('0x21')](handleError(_0x47a759,null));};exports['getMessages']=function(_0x1a183f,_0x47d217){var _0x2f27eb={'raw':![],'where':{}};var _0x5ba494={};var _0xf6b002={'count':0x0,'rows':[]};return db[_0x64bf('0x34')][_0x64bf('0x4a')]({'where':{'id':_0x1a183f[_0x64bf('0x48')]['id']}})[_0x64bf('0x16')](handleEntityNotFound(_0x47d217,null))['then'](function(_0x30d695){if(_0x30d695){_0x5ba494[_0x64bf('0x3d')]=_[_0x64bf('0x3e')](db[_0x64bf('0x7d')]['rawAttributes']);_0x5ba494['query']=_['keys'](_0x1a183f[_0x64bf('0x3a')]);_0x5ba494[_0x64bf('0x99')]=_[_0x64bf('0x3f')](_0x5ba494[_0x64bf('0x3d')],_0x5ba494[_0x64bf('0x3a')]);_0x2f27eb[_0x64bf('0x40')]=_[_0x64bf('0x3f')](_0x5ba494['model'],qs['fields'](_0x1a183f[_0x64bf('0x3a')][_0x64bf('0x41')]));_0x2f27eb['attributes']=_0x2f27eb[_0x64bf('0x40')][_0x64bf('0x42')]?_0x2f27eb['attributes']:_0x5ba494[_0x64bf('0x3d')];if(!_0x1a183f[_0x64bf('0x3a')][_0x64bf('0x9a')](_0x64bf('0x9b'))){_0x2f27eb[_0x64bf('0x27')]=qs['limit'](_0x1a183f[_0x64bf('0x3a')][_0x64bf('0x27')]);_0x2f27eb[_0x64bf('0x29')]=qs[_0x64bf('0x29')](_0x1a183f[_0x64bf('0x3a')]['offset']);}_0x2f27eb[_0x64bf('0x9c')]=qs[_0x64bf('0x9d')](_0x1a183f[_0x64bf('0x3a')][_0x64bf('0x9d')]);_0x2f27eb[_0x64bf('0x9e')]=qs['filters'](_[_0x64bf('0x9f')](_0x1a183f[_0x64bf('0x3a')],_0x5ba494[_0x64bf('0x99')]));_0x2f27eb[_0x64bf('0x9e')]['ChatInteractionId']=_0x30d695['id'];if(_0x1a183f[_0x64bf('0x3a')][_0x64bf('0xa0')]){_0x2f27eb[_0x64bf('0x9e')]=_[_0x64bf('0x44')](_0x2f27eb[_0x64bf('0x9e')],{'$or':_[_0x64bf('0x33')](_0x2f27eb[_0x64bf('0x40')],function(_0x83795d){var _0x1760d5={};_0x1760d5[_0x83795d]={'$like':'%'+_0x1a183f[_0x64bf('0x3a')]['filter']+'%'};return _0x1760d5;})});}if(_0x1a183f[_0x64bf('0x3a')][_0x64bf('0xa1')]){var _0x5d51b0=_0x1a183f[_0x64bf('0x3a')][_0x64bf('0xa1')]['split'](',');var _0x2a267a={};_0x2a267a[_0x5d51b0[0x0]]={'$gte':moment(_0x5d51b0[0x1])[_0x64bf('0x79')](_0x64bf('0xa2'))};_0x2f27eb['where']=_[_0x64bf('0x44')](_0x2f27eb[_0x64bf('0x9e')],_0x2a267a);}_0x2f27eb=_[_0x64bf('0x44')]({},_0x2f27eb,_0x1a183f[_0x64bf('0xa3')]);return db['ChatMessage'][_0x64bf('0x28')]({'where':_0x2f27eb[_0x64bf('0x9e')]})['then'](function(_0x162f5c){_0xf6b002[_0x64bf('0x28')]=_0x162f5c;if(_0x1a183f[_0x64bf('0x3a')]['includeAll']){_0x2f27eb[_0x64bf('0xa4')]=[{'all':!![]}];}return db[_0x64bf('0x7d')][_0x64bf('0x38')](_0x2f27eb);})['then'](function(_0xa9adc2){_0xf6b002[_0x64bf('0xa5')]=_0xa9adc2;return _0xf6b002;});}})[_0x64bf('0x16')](respondWithFilteredResult(_0x47d217,_0x2f27eb))[_0x64bf('0x21')](handleError(_0x47d217,null));};exports[_0x64bf('0xa6')]=function(_0x788c6f,_0xa89504){var _0x7d6c58={'where':{}},_0x4fc0f7={},_0xe96477={'count':0x0,'rows':[]};return db[_0x64bf('0x34')][_0x64bf('0x4a')]({'where':{'id':_0x788c6f['params']['id']}})[_0x64bf('0x16')](handleEntityNotFound(_0xa89504,null))[_0x64bf('0x16')](function(_0x39be51){if(_0x39be51){_0x4fc0f7[_0x64bf('0x3d')]=_[_0x64bf('0x3e')](db[_0x64bf('0x7d')][_0x64bf('0xa7')]);_0x4fc0f7[_0x64bf('0x3a')]=_['keys'](_0x788c6f[_0x64bf('0x3a')]);_0x4fc0f7['filters']=_['intersection'](_0x4fc0f7[_0x64bf('0x3d')],_0x4fc0f7[_0x64bf('0x3a')]);_0x7d6c58[_0x64bf('0x40')]=_['intersection'](_0x4fc0f7[_0x64bf('0x3d')],qs[_0x64bf('0x41')](_0x788c6f[_0x64bf('0x3a')][_0x64bf('0x41')]));_0x7d6c58['attributes']=_0x7d6c58['attributes'][_0x64bf('0x42')]?_0x7d6c58[_0x64bf('0x40')]:_0x4fc0f7[_0x64bf('0x3d')];if(!_0x788c6f['query']['hasOwnProperty']('nolimit')){_0x7d6c58[_0x64bf('0x27')]=qs[_0x64bf('0x27')](_0x788c6f['query'][_0x64bf('0x27')]);_0x7d6c58[_0x64bf('0x29')]=qs[_0x64bf('0x29')](_0x788c6f[_0x64bf('0x3a')][_0x64bf('0x29')]);}_0x7d6c58['order']=qs[_0x64bf('0x9d')](_0x788c6f[_0x64bf('0x3a')][_0x64bf('0x9d')]);_0x7d6c58['where']=qs[_0x64bf('0x99')](_[_0x64bf('0x9f')](_0x788c6f[_0x64bf('0x3a')],_0x4fc0f7['filters']));_0x7d6c58[_0x64bf('0x9e')][_0x64bf('0xa8')]=_0x39be51['id'];if(_0x788c6f[_0x64bf('0x3a')][_0x64bf('0xa0')]){_0x7d6c58[_0x64bf('0x9e')]=_['merge'](_0x7d6c58[_0x64bf('0x9e')],{'$or':_['map'](_0x7d6c58[_0x64bf('0x40')],function(_0xb85e0e){var _0x3d6c29={};_0x3d6c29[_0xb85e0e]={'$like':'%'+_0x788c6f[_0x64bf('0x3a')][_0x64bf('0xa0')]+'%'};return _0x3d6c29;})});}_0x7d6c58[_0x64bf('0x9e')][_0x64bf('0xa9')]=![];if(_0x788c6f['query']['id']){_0x7d6c58['where']['id']={'$gt':_0x788c6f[_0x64bf('0x3a')]['id']};}_0x7d6c58=_[_0x64bf('0x44')]({},_0x7d6c58,_0x788c6f[_0x64bf('0xa3')]);return db[_0x64bf('0x7d')]['count']({'where':_0x7d6c58[_0x64bf('0x9e')]})[_0x64bf('0x16')](function(_0x6f9253){_0xe96477[_0x64bf('0x28')]=_0x6f9253;_0x7d6c58[_0x64bf('0xa4')]=[];if(_0x788c6f['query']['includeAgent']){_0x7d6c58[_0x64bf('0xa4')]['push']({'model':db['User'],'as':_0x64bf('0x7f'),'attributes':['id',_0x64bf('0x52'),_0x64bf('0x88')]});}if(_0x788c6f[_0x64bf('0x3a')]['includeAll']){_0x7d6c58['include']=[{'all':!![]}];}return db['ChatMessage'][_0x64bf('0x38')](_0x7d6c58);})[_0x64bf('0x16')](function(_0x127d07){_0xe96477[_0x64bf('0xa5')]=_0x127d07;return _0xe96477;});}})['then'](respondWithFilteredResult(_0xa89504,_0x7d6c58))['catch'](handleError(_0xa89504,null));};exports['addTags']=function(_0x1a5b6f,_0x433ec0){return db[_0x64bf('0x34')][_0x64bf('0x45')]({'where':{'id':_0x1a5b6f['params']['id']}})['then'](handleEntityNotFound(_0x433ec0,null))[_0x64bf('0x16')](function(_0x1d20f){if(_0x1d20f){return _0x1d20f['setTags'](_0x1a5b6f[_0x64bf('0x47')][_0x64bf('0x98')],_[_0x64bf('0xaa')](_0x1a5b6f[_0x64bf('0x47')],[_0x64bf('0x98'),'id'])||{})[_0x64bf('0xab')](function(){return db['Tag']['findAll']({'attributes':['id',_0x64bf('0x2f'),_0x64bf('0xac')],'where':{'id':_0x1a5b6f[_0x64bf('0x47')][_0x64bf('0x98')]}});})[_0x64bf('0x16')](function(_0x2f24d8){socket[_0x64bf('0xad')](_0x64bf('0xae'),{'id':Number(_0x1a5b6f[_0x64bf('0x48')]['id']),'tags':_0x2f24d8||[]});return{'id':Number(_0x1a5b6f[_0x64bf('0x48')]['id']),'tags':_0x2f24d8||[]};});}})[_0x64bf('0x16')](respondWithResult(_0x433ec0,null))[_0x64bf('0x21')](handleError(_0x433ec0,null));};exports[_0x64bf('0xaf')]=function(_0x119b05,_0x110ea4){return db[_0x64bf('0x34')][_0x64bf('0x45')]({'where':{'id':_0x119b05[_0x64bf('0x48')]['id']}})[_0x64bf('0x16')](handleEntityNotFound(_0x110ea4,null))[_0x64bf('0x16')](function(_0x1c706d){if(_0x1c706d){return _0x1c706d[_0x64bf('0xaf')](_0x119b05['query'][_0x64bf('0x98')]);}})[_0x64bf('0x16')](respondWithStatusCode(_0x110ea4,null))[_0x64bf('0x21')](handleError(_0x110ea4,null));};exports[_0x64bf('0x75')]=function(_0x4f244a,_0x1d71a5){var _0x50773d=moment()[_0x64bf('0xb0')]()['toString']();var _0x251f9c=path[_0x64bf('0x71')](config['root'],_0x64bf('0xb1'),_0x64bf('0xb2'),'tmp');var _0x596ff2=path[_0x64bf('0x71')](config[_0x64bf('0x72')],_0x64bf('0xb1'),_0x64bf('0xb2'),'attachments');var _0x15d2d7=path[_0x64bf('0x71')](_0x251f9c,_0x50773d);var _0x32fff8=util['format']('transcript-%d-%s.zip',_0x4f244a[_0x64bf('0x48')]['id'],_0x50773d);var _0x4dc0f9=path[_0x64bf('0x71')](_0x251f9c,_0x32fff8);var _0x5a75ab=[];_0x5a75ab[_0x64bf('0xb3')]({'model':db['User'],'as':'User','attributes':['id',_0x64bf('0x52')],'raw':!![]});_0x5a75ab[_0x64bf('0xb3')]({'model':db['CmContact'],'as':_0x64bf('0x62'),'attributes':['id','firstName',_0x64bf('0xb4')],'raw':!![]});if(_0x4f244a['query'][_0x64bf('0xb5')]){_0x5a75ab[_0x64bf('0xb3')]({'model':db[_0x64bf('0x6d')],'as':_0x64bf('0x6d'),'raw':!![]});}var _0xc68501=[{'model':db['ChatMessage'],'as':_0x64bf('0x7e'),'attributes':['id',_0x64bf('0x47'),_0x64bf('0x8c'),_0x64bf('0x8e'),_0x64bf('0xa9'),_0x64bf('0xb6')],'include':_0x5a75ab}];_0xc68501[_0x64bf('0xb3')]({'model':db['ChatWebsite'],'as':'Account','attributes':['id','name']});_0xc68501[_0x64bf('0xb3')]({'model':db[_0x64bf('0x7f')],'as':_0x64bf('0xb7'),'attributes':['id',_0x64bf('0x52')]});return db[_0x64bf('0x34')][_0x64bf('0x45')]({'where':{'id':_0x4f244a['params']['id']},'include':_0xc68501})['then'](handleEntityNotFound(_0x1d71a5,null))[_0x64bf('0x16')](function(_0x1fcb2c){if(_0x1fcb2c){var _0x5ee9c8=_0x1fcb2c[_0x64bf('0xb8')]({'plain':!![]});fs['mkdirSync'](_0x15d2d7);var _0x440af5={'channel':_0x64bf('0xb9'),'account':_0x5ee9c8[_0x64bf('0xba')]['name'],'agent':_0x5ee9c8[_0x64bf('0xb7')]?_0x5ee9c8[_0x64bf('0xb7')][_0x64bf('0x52')]:_0x64bf('0xbb'),'createdAt':moment(_0x5ee9c8[_0x64bf('0x8c')])[_0x64bf('0x79')](_0x64bf('0xa2'))[_0x64bf('0xbc')](),'closedAt':_0x5ee9c8[_0x64bf('0x66')]?moment(_0x5ee9c8[_0x64bf('0xbd')])[_0x64bf('0x79')](_0x64bf('0xa2'))['toString']():'','disposition':_0x5ee9c8[_0x64bf('0x67')],'chatInfo':{'browser':_0x5ee9c8['browserName'],'os':_0x5ee9c8[_0x64bf('0xbe')],'referer':_0x5ee9c8[_0x64bf('0xbf')],'customerIp':_0x5ee9c8[_0x64bf('0xc0')],'customerPort':_0x5ee9c8[_0x64bf('0xc1')]?_0x5ee9c8[_0x64bf('0xc1')]:_0x64bf('0xbb')},'messages':_(_0x5ee9c8[_0x64bf('0x7e')])[_0x64bf('0xc2')]()['filter']({'secret':![]})[_0x64bf('0x33')](function(_0x47ec62){if(_0x47ec62[_0x64bf('0x6d')]){var _0x516a66=path[_0x64bf('0x71')](_0x596ff2,_0x47ec62[_0x64bf('0x6d')]['basename']);if(fs[_0x64bf('0xc3')](_0x516a66)){fs[_0x64bf('0xc4')](_0x516a66)['pipe'](fs[_0x64bf('0xc5')](path[_0x64bf('0x71')](_0x15d2d7,_0x47ec62['Attachment'][_0x64bf('0x74')])));}}return{'date':moment(_0x47ec62[_0x64bf('0x8c')])[_0x64bf('0x79')](_0x64bf('0xa2'))[_0x64bf('0xbc')](),'sender':_0x47ec62[_0x64bf('0x8e')]===_0x64bf('0xc6')?_0x47ec62['User']?_0x47ec62[_0x64bf('0x7f')][_0x64bf('0x52')]:'System':_0x47ec62[_0x64bf('0x62')][_0x64bf('0xc7')]+(_0x47ec62[_0x64bf('0x62')]['lastName']?'\x20'+_0x47ec62['Contact'][_0x64bf('0xb4')]:''),'body':_0x47ec62[_0x64bf('0x6d')]?_0x47ec62['Attachment']['basename']:_0x47ec62['body'],'direction':_0x47ec62[_0x64bf('0x8e')]===_0x64bf('0xc6')?'A':'C','secret':_0x47ec62[_0x64bf('0xa9')],'read':_0x47ec62[_0x64bf('0xb6')],'attachment':_0x47ec62['Attachment']};})[_0x64bf('0xc8')]()};return ejs['renderFile'](path[_0x64bf('0x71')](config[_0x64bf('0x72')],'server',_0x64bf('0xc9'),_0x64bf('0xca')),{'interaction':_0x440af5})[_0x64bf('0x16')](function(_0x5cc49a){var _0x2ffb44=path['join'](_0x15d2d7,_0x64bf('0xcb')+_0x5ee9c8['id']+'-'+_0x50773d+_0x64bf('0xcc'));var _0x5ac3da={'path':_0x2ffb44,'channel':_0x64bf('0xb9'),'interactionId':_0x5ee9c8['id']};return pdf[_0x64bf('0xcd')](_0x5cc49a,_0x5ac3da);})[_0x64bf('0x16')](function(){return new Promise(function(_0x33a3a2,_0x3e6dac){zipdir(_0x15d2d7,{'saveTo':_0x4dc0f9},function(_0x3ddd02,_0x1d04b6){if(_0x3ddd02)return _0x3e6dac(_0x3ddd02);return _0x33a3a2(_0x1d04b6);});})[_0x64bf('0x16')](function(){return new BPromise(function(_0x282894,_0x232c94){rimraf(_0x15d2d7,function(_0x22e246){if(_0x22e246)_0x232c94(_0x22e246);return _0x282894();});});})[_0x64bf('0x16')](function(){return _0x1d71a5[_0x64bf('0x75')](_0x4dc0f9,_0x32fff8,function(_0x581e9c){if(_0x581e9c){logger[_0x64bf('0x1d')](_0x64bf('0xce'),_0x581e9c);}else{fs[_0x64bf('0xcf')](_0x4dc0f9);}});});});}else{return _0x1d71a5[_0x64bf('0x22')](0xc8);}})['catch'](handleError(_0x1d71a5,null));};