Built motion from commit 67e5df37.|2.0.66
[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 _0xb10b=['send','index','model','keys','rawAttributes','dateStart','query','dateEnd','attributes','fields','length','hasOwnProperty','order','sort','where','filters','pick','add','day','merge','filter','map','options','include','tag','push','Tag','Tags','distinct','read','ChatMessage','Messages','direction','createdAt','ChatInteraction','includeAll','User','Users','fullname','CmContact','Contact','Owner','internal','Account','key','color','ThroughTags','findAll','rows','catch','show','params','intersection','find','create','body','describe','abandonInteraction,\x20%s,\x20%s,\x20%s','UserId','Sequelize','ValidationError','Interaction\x20already\x20assigned','disposition','attachmentUpload','file','originalname','basename','filename','type','mimetype','Attachment','attachmentDownload','findOne','closed','Unauthorized','download','server/files/attachments/','close','mailTranscript','join','server/files/chat/upload/','format','pathTranscript','ChatWebsite','ChatWebsiteId','ContactId','%s%d','System','customer','Visitor','website_alias','agentAlias','alias','agent_fullname','[%s]\x20%s:\x20%s\x0a','MM/DD/YYYY\x20HH:mm:ss','appendFileSync','MailAccount','MailServerOut','Smtp','service','host','port','secure','auth','pass','sendMail','\x22%s\x22\x20<%s>','user','Mail\x20Transcript','Hi\x20%s,\x20attached\x20you\x20can\x20find\x20your\x20transcript\x20file.\x20\x0a\x0aRegards.\x20\x0a\x0a','addMessage','ids','omit','getMessages','nolimit','ChatInteractionId','$gte','split','getMyMessages','includeAgent','addTags','emit','chatInteractionTags:save','removeTags','fast-json-patch','moment','bluebird','mustache','path','sox','to-csv','ejs','lodash','squel','jsforce','desk.js','querystring','papaparse','../../components/parsers/qs','../../config/logger','api','../../config/utils','../../config/environment','../../mysqldb','./chatInteraction.socket','register','jayson/promise','http','request','then','info','ChatInteraction,\x20%s,\x20%s','request\x20sent','debug','ChatInteraction,\x20%s,\x20%s,\x20%s','error','code','message','result','sendStatus','status','end','count','offset','limit','set','json','apply','reject','destroy','stack','name'];(function(_0x2eaeac,_0x3ab473){var _0x15baf2=function(_0x189728){while(--_0x189728){_0x2eaeac['push'](_0x2eaeac['shift']());}};_0x15baf2(++_0x3ab473);}(_0xb10b,0x12a));var _0xbb10=function(_0x486008,_0x2b2d05){_0x486008=_0x486008-0x0;var _0x2fefc7=_0xb10b[_0x486008];return _0x2fefc7;};'use strict';var jsonpatch=require(_0xbb10('0x0'));var rp=require('request-promise');var moment=require(_0xbb10('0x1'));var BPromise=require(_0xbb10('0x2'));var Mustache=require(_0xbb10('0x3'));var util=require('util');var path=require(_0xbb10('0x4'));var sox=require(_0xbb10('0x5'));var csv=require(_0xbb10('0x6'));var ejs=require(_0xbb10('0x7'));var fs=require('fs');var _=require(_0xbb10('0x8'));var squel=require(_0xbb10('0x9'));var crypto=require('crypto');var jsforce=require(_0xbb10('0xa'));var deskjs=require(_0xbb10('0xb'));var toCsv=require('to-csv');var querystring=require(_0xbb10('0xc'));var Papa=require(_0xbb10('0xd'));var qs=require(_0xbb10('0xe'));var logger=require(_0xbb10('0xf'))(_0xbb10('0x10'));var utils=require(_0xbb10('0x11'));var config=require(_0xbb10('0x12'));var db=require(_0xbb10('0x13'))['db'];var socket=require('socket.io-emitter')({'host':'localhost','port':0x18eb});require(_0xbb10('0x14'))[_0xbb10('0x15')](socket);var jayson=require(_0xbb10('0x16'));var client=jayson['client'][_0xbb10('0x17')]({'port':0x232b});var client9004=jayson['client'][_0xbb10('0x17')]({'port':0x232c});function respondWithRpcPromise(_0x916a32,_0x285009,_0x193a27,_0x442548){return new BPromise(function(_0x147230,_0x58a612){var _0x26e021=_0x442548||client;return _0x26e021[_0xbb10('0x18')](_0x916a32,_0x193a27)[_0xbb10('0x19')](function(_0x17fd46){logger[_0xbb10('0x1a')](_0xbb10('0x1b'),_0x285009,_0xbb10('0x1c'));logger[_0xbb10('0x1d')](_0xbb10('0x1e'),_0x285009,_0xbb10('0x1c'),JSON['stringify'](_0x17fd46));if(_0x17fd46[_0xbb10('0x1f')]){if(_0x17fd46[_0xbb10('0x1f')][_0xbb10('0x20')]===0x1f4){logger[_0xbb10('0x1f')](_0xbb10('0x1b'),_0x285009,_0x17fd46[_0xbb10('0x1f')][_0xbb10('0x21')]);return _0x58a612(_0x17fd46[_0xbb10('0x1f')][_0xbb10('0x21')]);}logger[_0xbb10('0x1f')]('ChatInteraction,\x20%s,\x20%s',_0x285009,_0x17fd46[_0xbb10('0x1f')][_0xbb10('0x21')]);return _0x147230(_0x17fd46[_0xbb10('0x1f')][_0xbb10('0x21')]);}else{logger[_0xbb10('0x1a')](_0xbb10('0x1b'),_0x285009,'request\x20sent');_0x147230(_0x17fd46[_0xbb10('0x22')][_0xbb10('0x21')]);}})['catch'](function(_0x4c4b0d){logger[_0xbb10('0x1f')](_0xbb10('0x1b'),_0x285009,_0x4c4b0d);_0x58a612(_0x4c4b0d);});});}function respondWithStatusCode(_0x2e911d,_0x317da4){_0x317da4=_0x317da4||0xcc;return function(_0x445761){if(_0x445761){return _0x2e911d[_0xbb10('0x23')](_0x317da4);}return _0x2e911d[_0xbb10('0x24')](_0x317da4)[_0xbb10('0x25')]();};}function respondWithResult(_0x7f2377,_0x395404){_0x395404=_0x395404||0xc8;return function(_0x3ba6cd){if(_0x3ba6cd){return _0x7f2377[_0xbb10('0x24')](_0x395404)['json'](_0x3ba6cd);}};}function respondWithFilteredResult(_0x4fedac,_0x4fb5e0){return function(_0xc93a04){if(_0xc93a04){var _0x3c5ae7=_0xc93a04[_0xbb10('0x26')],_0x2c89b9=_0x4fb5e0[_0xbb10('0x27')],_0x4e25ff=_0x4fb5e0[_0xbb10('0x27')]+_0x4fb5e0[_0xbb10('0x28')],_0x2fdde9;if(_0x4e25ff>=_0x3c5ae7){_0x4e25ff=_0x3c5ae7;_0x2fdde9=0xc8;}else{_0x2fdde9=0xce;}_0x4fedac[_0xbb10('0x24')](_0x2fdde9);return _0x4fedac[_0xbb10('0x29')]('Content-Range',_0x2c89b9+'-'+_0x4e25ff+'/'+_0x3c5ae7)[_0xbb10('0x2a')](_0xc93a04);}return null;};}function patchUpdates(_0x21ac54){return function(_0xfc3595){try{jsonpatch[_0xbb10('0x2b')](_0xfc3595,_0x21ac54,!![]);}catch(_0x1d30aa){return BPromise[_0xbb10('0x2c')](_0x1d30aa);}return _0xfc3595['save']();};}function saveUpdates(_0x221624,_0x4ee02e){return function(_0x448d5c){if(_0x448d5c){return _0x448d5c['update'](_0x221624)[_0xbb10('0x19')](function(_0x1dcc9c){return _0x1dcc9c;});}return null;};}function removeEntity(_0x1b59f9,_0x491db4){return function(_0x518d47){if(_0x518d47){return _0x518d47[_0xbb10('0x2d')]()[_0xbb10('0x19')](function(){_0x1b59f9['status'](0xcc)[_0xbb10('0x25')]();});}};}function handleEntityNotFound(_0x37dde7,_0x2f653f){return function(_0x1c0b57){if(!_0x1c0b57){_0x37dde7[_0xbb10('0x23')](0x194);}return _0x1c0b57;};}function handleError(_0x138bc6,_0xf85197){_0xf85197=_0xf85197||0x1f4;return function(_0x4975f5){logger[_0xbb10('0x1f')](_0x4975f5[_0xbb10('0x2e')]);if(_0x4975f5[_0xbb10('0x2f')]){delete _0x4975f5[_0xbb10('0x2f')];}_0x138bc6[_0xbb10('0x24')](_0xf85197)[_0xbb10('0x30')](_0x4975f5);};}exports[_0xbb10('0x31')]=function(_0x10202c,_0x3c8d7d){var _0x56943c={},_0x2b5352={},_0x150107={'count':0x0,'rows':[]};_0x2b5352[_0xbb10('0x32')]=_[_0xbb10('0x33')](db['ChatInteraction'][_0xbb10('0x34')]);var _0x5de37b={'dateStart':_0x10202c['query'][_0xbb10('0x35')],'dateEnd':_0x10202c[_0xbb10('0x36')][_0xbb10('0x37')]};delete _0x10202c[_0xbb10('0x36')][_0xbb10('0x35')];delete _0x10202c[_0xbb10('0x36')]['dateEnd'];_0x2b5352[_0xbb10('0x36')]=_[_0xbb10('0x33')](_0x10202c['query']);_0x2b5352['filters']=_['intersection'](_0x2b5352[_0xbb10('0x32')],_0x2b5352[_0xbb10('0x36')]);_0x56943c[_0xbb10('0x38')]=_['intersection'](_0x2b5352[_0xbb10('0x32')],qs[_0xbb10('0x39')](_0x10202c[_0xbb10('0x36')]['fields']));_0x56943c[_0xbb10('0x38')]=_0x56943c[_0xbb10('0x38')][_0xbb10('0x3a')]?_0x56943c[_0xbb10('0x38')]:_0x2b5352['model'];if(!_0x10202c[_0xbb10('0x36')][_0xbb10('0x3b')]('nolimit')){_0x56943c[_0xbb10('0x28')]=qs[_0xbb10('0x28')](_0x10202c[_0xbb10('0x36')][_0xbb10('0x28')]);_0x56943c[_0xbb10('0x27')]=qs[_0xbb10('0x27')](_0x10202c['query'][_0xbb10('0x27')]);}_0x56943c[_0xbb10('0x3c')]=qs[_0xbb10('0x3d')](_0x10202c[_0xbb10('0x36')]['sort']);_0x56943c[_0xbb10('0x3e')]=qs[_0xbb10('0x3f')](_[_0xbb10('0x40')](_0x10202c[_0xbb10('0x36')],_0x2b5352['filters']));if(_0x5de37b[_0xbb10('0x35')]){if(_0x5de37b[_0xbb10('0x37')]){_0x5de37b['dateEnd']=moment(_0x5de37b['dateEnd'])[_0xbb10('0x41')](0x1,'day');}else{_0x5de37b[_0xbb10('0x37')]=moment(_0x5de37b[_0xbb10('0x35')])['add'](0x1,_0xbb10('0x42'));}_[_0xbb10('0x43')](_0x56943c[_0xbb10('0x3e')],{'createdAt':{'$gte':_0x5de37b['dateStart'],'$lte':_0x5de37b[_0xbb10('0x37')]}});}if(_0x10202c[_0xbb10('0x36')][_0xbb10('0x44')]){_0x56943c[_0xbb10('0x3e')]=_[_0xbb10('0x43')](_0x56943c[_0xbb10('0x3e')],{'$or':_[_0xbb10('0x45')](_0x56943c[_0xbb10('0x38')],function(_0x205bb4){var _0x43b948={};_0x43b948[_0x205bb4]={'$like':'%'+_0x10202c[_0xbb10('0x36')][_0xbb10('0x44')]+'%'};return _0x43b948;})});}_0x56943c=_['merge']({},_0x56943c,_0x10202c[_0xbb10('0x46')]);var _0x41bbf8={'where':_0x56943c['where']};_0x41bbf8[_0xbb10('0x47')]=[];if(_0x10202c[_0xbb10('0x36')][_0xbb10('0x48')]){_0x41bbf8[_0xbb10('0x47')][_0xbb10('0x49')]({'model':db[_0xbb10('0x4a')],'as':_0xbb10('0x4b'),'attributes':['id'],'where':{'id':_0x10202c['query']['tag']},'required':!![]});_0x41bbf8[_0xbb10('0x4c')]=!![];}if(_0x10202c[_0xbb10('0x36')][_0xbb10('0x4d')]){_0x41bbf8['include']['push']({'model':db[_0xbb10('0x4e')],'as':_0xbb10('0x4f'),'attributes':['id',_0xbb10('0x50'),_0xbb10('0x51'),_0xbb10('0x4d')],'where':{'direction':'in','read':_0x10202c[_0xbb10('0x36')][_0xbb10('0x4d')]||![]},'required':!![]});_0x41bbf8[_0xbb10('0x4c')]=!![];}return db[_0xbb10('0x52')][_0xbb10('0x26')](_0x41bbf8)['then'](function(_0x5eafac){_0x150107[_0xbb10('0x26')]=_0x5eafac;if(_0x10202c[_0xbb10('0x36')][_0xbb10('0x53')]){_0x56943c[_0xbb10('0x47')]=[{'model':db[_0xbb10('0x54')],'as':_0xbb10('0x55'),'required':![],'attributes':['id',_0xbb10('0x2f'),_0xbb10('0x56')]},{'model':db[_0xbb10('0x57')],'as':_0xbb10('0x58'),'required':![]},{'model':db[_0xbb10('0x54')],'as':_0xbb10('0x59'),'attributes':[_0xbb10('0x2f'),_0xbb10('0x56'),_0xbb10('0x5a')],'required':![]},{'model':db['ChatWebsite'],'as':_0xbb10('0x5b'),'attributes':['name',_0xbb10('0x5c')],'required':![]},{'model':db[_0xbb10('0x4e')],'as':_0xbb10('0x4f'),'attributes':['id',_0xbb10('0x50'),_0xbb10('0x51'),_0xbb10('0x4d')],'where':{'direction':'in','read':_0x10202c[_0xbb10('0x36')][_0xbb10('0x4d')]||![]},'required':_0x10202c[_0xbb10('0x36')][_0xbb10('0x4d')]||![]},{'model':db[_0xbb10('0x4a')],'as':_0xbb10('0x4b'),'attributes':['id',_0xbb10('0x2f'),_0xbb10('0x5d')],'required':_0x10202c[_0xbb10('0x36')]['tag']?!![]:![]},{'model':db[_0xbb10('0x4a')],'as':_0xbb10('0x5e'),'attributes':['id',_0xbb10('0x2f'),_0xbb10('0x5d')],'through':_0x10202c[_0xbb10('0x36')][_0xbb10('0x48')]?{'attributes':[],'where':{'TagId':_0x10202c['query'][_0xbb10('0x48')]||undefined}}:{},'required':_0x10202c[_0xbb10('0x36')][_0xbb10('0x48')]?!![]:![]}];}return db[_0xbb10('0x52')][_0xbb10('0x5f')](_0x56943c);})[_0xbb10('0x19')](function(_0x175872){_0x150107[_0xbb10('0x60')]=_0x175872;return _0x150107;})[_0xbb10('0x19')](respondWithFilteredResult(_0x3c8d7d,_0x56943c))[_0xbb10('0x61')](handleError(_0x3c8d7d,null));};exports[_0xbb10('0x62')]=function(_0x2a1ce1,_0x16932f){var _0x4e7fac={'raw':![],'where':{'id':_0x2a1ce1[_0xbb10('0x63')]['id']}},_0x4ea1a6={};_0x4ea1a6[_0xbb10('0x32')]=_[_0xbb10('0x33')](db[_0xbb10('0x52')]['rawAttributes']);_0x4ea1a6['query']=_[_0xbb10('0x33')](_0x2a1ce1[_0xbb10('0x36')]);_0x4ea1a6[_0xbb10('0x3f')]=_[_0xbb10('0x64')](_0x4ea1a6[_0xbb10('0x32')],_0x4ea1a6[_0xbb10('0x36')]);_0x4e7fac[_0xbb10('0x38')]=_['intersection'](_0x4ea1a6[_0xbb10('0x32')],qs[_0xbb10('0x39')](_0x2a1ce1['query']['fields']));_0x4e7fac['attributes']=_0x4e7fac[_0xbb10('0x38')][_0xbb10('0x3a')]?_0x4e7fac[_0xbb10('0x38')]:_0x4ea1a6['model'];if(_0x2a1ce1['query']['includeAll']){_0x4e7fac[_0xbb10('0x47')]=[{'all':!![]}];}_0x4e7fac=_[_0xbb10('0x43')]({},_0x4e7fac,_0x2a1ce1[_0xbb10('0x46')]);return db['ChatInteraction'][_0xbb10('0x65')](_0x4e7fac)[_0xbb10('0x19')](handleEntityNotFound(_0x16932f,null))['then'](respondWithResult(_0x16932f,null))['catch'](handleError(_0x16932f,null));};exports[_0xbb10('0x66')]=function(_0x308896,_0xd77753){return db[_0xbb10('0x52')]['create'](_0x308896[_0xbb10('0x67')],{})[_0xbb10('0x19')](respondWithResult(_0xd77753,0xc9))[_0xbb10('0x61')](handleError(_0xd77753,null));};exports['update']=function(_0x402a01,_0x2f05bc){if(_0x402a01[_0xbb10('0x67')]['id']){delete _0x402a01['body']['id'];}return db[_0xbb10('0x52')][_0xbb10('0x65')]({'where':{'id':_0x402a01[_0xbb10('0x63')]['id']}})[_0xbb10('0x19')](handleEntityNotFound(_0x2f05bc,null))['then'](saveUpdates(_0x402a01[_0xbb10('0x67')],null))[_0xbb10('0x19')](respondWithResult(_0x2f05bc,null))[_0xbb10('0x61')](handleError(_0x2f05bc,null));};exports[_0xbb10('0x2d')]=function(_0x56b3a3,_0x354ea7){return db[_0xbb10('0x52')][_0xbb10('0x65')]({'where':{'id':_0x56b3a3[_0xbb10('0x63')]['id']}})[_0xbb10('0x19')](handleEntityNotFound(_0x354ea7,null))[_0xbb10('0x19')](removeEntity(_0x354ea7,null))[_0xbb10('0x61')](handleError(_0x354ea7,null));};exports['describe']=function(_0x4a13d7,_0x5acd5e){return db[_0xbb10('0x52')][_0xbb10('0x68')]()[_0xbb10('0x19')](respondWithResult(_0x5acd5e,null))['catch'](handleError(_0x5acd5e,null));};exports['abandon']=function(_0x46f462,_0x1b11c0,_0xd9e743){if(_0x46f462[_0xbb10('0x67')]['id']){delete _0x46f462[_0xbb10('0x67')]['id'];}return db[_0xbb10('0x52')]['find']({'where':{'id':_0x46f462[_0xbb10('0x63')]['id']}})[_0xbb10('0x19')](function(_0x270fdb){if(!_0x270fdb){logger[_0xbb10('0x1f')](_0xbb10('0x69'),'abandon',0x194,'entity\x20not\x20found');_0x1b11c0['sendStatus'](0x194);}else{if(_0x270fdb[_0xbb10('0x6a')]){throw new db[(_0xbb10('0x6b'))][(_0xbb10('0x6c'))](_0xbb10('0x6d'));}}return _0x270fdb;})[_0xbb10('0x19')](function(_0x1d40b8){if(_0x1d40b8){return _0x1d40b8['update']({'closed':_0x46f462[_0xbb10('0x67')]['closed'],'closedAt':_0x46f462['body']['closedAt'],'disposition':_0x46f462[_0xbb10('0x67')][_0xbb10('0x6e')]})[_0xbb10('0x19')](function(_0x108f99){return _0x108f99;});}})['then'](function(_0x122d38){return respondWithRpcPromise('AbandonInteraction','abandon',_0x46f462[_0xbb10('0x67')],client9004);})[_0xbb10('0x19')](respondWithResult(_0x1b11c0,null))[_0xbb10('0x61')](handleError(_0x1b11c0,null));};exports[_0xbb10('0x6f')]=function(_0xfa3c7c,_0x4fccc5,_0x2d52ea){_0xfa3c7c[_0xbb10('0x67')][_0xbb10('0x2f')]=_0xfa3c7c[_0xbb10('0x70')][_0xbb10('0x71')];_0xfa3c7c[_0xbb10('0x67')][_0xbb10('0x72')]=_0xfa3c7c['file'][_0xbb10('0x73')];_0xfa3c7c[_0xbb10('0x67')][_0xbb10('0x74')]=_0xfa3c7c[_0xbb10('0x70')][_0xbb10('0x75')];return db[_0xbb10('0x76')][_0xbb10('0x66')](_0xfa3c7c[_0xbb10('0x67')])['then'](respondWithResult(_0x4fccc5,0xc9))[_0xbb10('0x61')](handleError(_0x4fccc5,null));};exports[_0xbb10('0x77')]=function(_0x145baf,_0x56416a,_0x4ee101){return db['ChatInteraction'][_0xbb10('0x78')]({'where':{'id':_0x145baf['params']['id']},'raw':!![]})['then'](handleEntityNotFound(_0x56416a,null))[_0xbb10('0x19')](function(_0x3ee56b){if(_0x3ee56b){if(_0x3ee56b[_0xbb10('0x79')]){return _0x56416a[_0xbb10('0x24')](0x193)[_0xbb10('0x2a')]({'status':0x193,'message':_0xbb10('0x7a')});}return db[_0xbb10('0x76')][_0xbb10('0x78')]({'where':{'id':_0x145baf[_0xbb10('0x36')]['attachId']},'raw':!![]})[_0xbb10('0x19')](handleEntityNotFound(_0x56416a,null));}})[_0xbb10('0x19')](function(_0x46b6d2){if(_0x46b6d2){return _0x56416a[_0xbb10('0x7b')](path['join'](config['root'],_0xbb10('0x7c'),_0x46b6d2[_0xbb10('0x72')]));}})[_0xbb10('0x61')](handleError(_0x56416a,null));};exports[_0xbb10('0x7d')]=function(_0x5ee705,_0x17d596){var _0x275bce=null;var _0x3f0d74=null;var _0xf7b3e1=null;var _0x35c1e8=null;if(_0x5ee705['body']['id']){delete _0x5ee705['body']['id'];}if(_0x5ee705[_0xbb10('0x67')][_0xbb10('0x7e')]){_0x35c1e8=path[_0xbb10('0x7f')](config['root'],_0xbb10('0x80'),util[_0xbb10('0x81')]('transcript-%s.txt',Date['now']()));_0x5ee705[_0xbb10('0x67')][_0xbb10('0x82')]=_0x35c1e8;}return db[_0xbb10('0x52')]['find']({'where':{'id':_0x5ee705[_0xbb10('0x63')]['id']},'include':[{'model':db[_0xbb10('0x4e')],'as':_0xbb10('0x4f'),'include':[{'model':db[_0xbb10('0x54')],'as':_0xbb10('0x54')}]}]})[_0xbb10('0x19')](handleEntityNotFound(_0x17d596,null))[_0xbb10('0x19')](saveUpdates(_0x5ee705[_0xbb10('0x67')],null))[_0xbb10('0x19')](function(_0x479d51){if(_0x479d51){_0x275bce=_0x479d51;return db[_0xbb10('0x83')][_0xbb10('0x65')]({'where':{'id':_0x275bce[_0xbb10('0x84')]}});}})[_0xbb10('0x19')](function(_0x3e3083){if(_0x3e3083){_0xf7b3e1=_0x3e3083;return db[_0xbb10('0x57')][_0xbb10('0x65')]({'where':{'id':_0x275bce[_0xbb10('0x85')]}});}})[_0xbb10('0x19')](function(_0x1a5538){if(_0x1a5538){_0x3f0d74=_0x1a5538;if(_0x35c1e8){var _0x15e043=null;for(var _0x2a4278=0x1;_0x2a4278<_0x275bce['Messages']['length'];++_0x2a4278){_0x15e043=_0x275bce[_0xbb10('0x4f')][_0x2a4278];var _0x3fa344=_0x15e043[_0xbb10('0x6a')]?util[_0xbb10('0x81')](_0xbb10('0x86'),_0xf7b3e1['agentAlias'],_0x15e043[_0xbb10('0x6a')]):_0xbb10('0x87');var _0x3b4d0b=_0x5ee705[_0xbb10('0x67')][_0xbb10('0x88')]||util[_0xbb10('0x81')]('%s%d',_0xbb10('0x89'),_0x15e043[_0xbb10('0x85')]);switch(_0xf7b3e1['agentIdentifier']){case _0xbb10('0x8a'):_0x3fa344=_0x15e043['UserId']?util[_0xbb10('0x81')](_0xbb10('0x86'),_0xf7b3e1[_0xbb10('0x8b')],_0x15e043[_0xbb10('0x6a')]):_0xbb10('0x87');break;case'agent_alias':_0x3fa344=_0x15e043[_0xbb10('0x6a')]?_0x15e043['User'][_0xbb10('0x8c')]||_0x15e043[_0xbb10('0x54')]['fullname']:'System';break;case _0xbb10('0x8d'):_0x3fa344=_0x15e043[_0xbb10('0x6a')]?_0x15e043[_0xbb10('0x54')][_0xbb10('0x56')]:_0xbb10('0x87');break;default:_0x3fa344=_0x15e043[_0xbb10('0x6a')]?util[_0xbb10('0x81')](_0xbb10('0x86'),_0xf7b3e1[_0xbb10('0x8b')],_0x15e043[_0xbb10('0x6a')]):_0xbb10('0x87');}var _0x4c0b83=util[_0xbb10('0x81')](_0xbb10('0x8e'),moment(_0x15e043[_0xbb10('0x51')])[_0xbb10('0x81')](_0xbb10('0x8f')),_0x15e043[_0xbb10('0x50')]==='in'?_0x3b4d0b:_0x3fa344,_0x15e043[_0xbb10('0x67')]);fs[_0xbb10('0x90')](_0x35c1e8,_0x4c0b83);}return db[_0xbb10('0x91')][_0xbb10('0x78')]({'where':{'service':!![]},'include':[{'model':db[_0xbb10('0x92')],'as':_0xbb10('0x93'),'raw':!![]}]});}}})['then'](function(_0xe1d006){if(_0xe1d006&&_0xe1d006['Smtp']&&_0x5ee705[_0xbb10('0x67')]){var _0x3675cd={'tls':{'rejectUnauthorized':![]}};if(_0xe1d006[_0xbb10('0x93')][_0xbb10('0x94')]){_0x3675cd[_0xbb10('0x94')]=_0xe1d006['Smtp'][_0xbb10('0x94')];}else{_0x3675cd[_0xbb10('0x95')]=_0xe1d006[_0xbb10('0x93')][_0xbb10('0x95')];_0x3675cd[_0xbb10('0x96')]=_0xe1d006[_0xbb10('0x93')]['port'];_0x3675cd['secure']=_0xe1d006[_0xbb10('0x93')][_0xbb10('0x97')];}if(_0xe1d006[_0xbb10('0x93')]['authentication']){_0x3675cd[_0xbb10('0x98')]={'user':_0xe1d006[_0xbb10('0x93')]['user'],'pass':_0xe1d006[_0xbb10('0x93')][_0xbb10('0x99')]};}return respondWithRpcPromise('SendMail',_0xbb10('0x9a'),{'account':_0x3675cd,'message':{'from':util['format'](_0xbb10('0x9b'),_0xe1d006[_0xbb10('0x2f')],_0xe1d006['email']||_0xe1d006[_0xbb10('0x93')][_0xbb10('0x9c')]),'to':_0x5ee705['body']['mailTranscript'],'subject':_0xbb10('0x9d'),'text':util[_0xbb10('0x81')](_0xbb10('0x9e'),_0x5ee705[_0xbb10('0x67')][_0xbb10('0x7e')]),'attachments':[{'path':_0x35c1e8}]}})['then'](function(){return _0x275bce;})[_0xbb10('0x61')](function(_0x9f2e0f){console[_0xbb10('0x1f')](_0x9f2e0f);});}else{return _0x275bce;}})['then'](respondWithStatusCode(_0x17d596,0xc8))[_0xbb10('0x61')](handleError(_0x17d596,null));};exports[_0xbb10('0x9f')]=function(_0x248887,_0xa5391d,_0x44a3e1){return db[_0xbb10('0x4e')][_0xbb10('0x65')]({'where':{'id':_0x248887[_0xbb10('0x63')]['id']}})[_0xbb10('0x19')](handleEntityNotFound(_0xa5391d,null))['then'](function(_0x4fc544){if(_0x4fc544){return _0x4fc544[_0xbb10('0x9f')](_0x248887[_0xbb10('0x67')][_0xbb10('0xa0')],_[_0xbb10('0xa1')](_0x248887[_0xbb10('0x67')],[_0xbb10('0xa0'),'id'])||{});}})[_0xbb10('0x19')](respondWithResult(_0xa5391d,null))[_0xbb10('0x61')](handleError(_0xa5391d,null));};exports[_0xbb10('0xa2')]=function(_0x471827,_0xcbaf69,_0x167734){var _0x2c6d2c={'raw':![],'where':{}};var _0x5f0844={};var _0x1e022d={'count':0x0,'rows':[]};return db[_0xbb10('0x52')][_0xbb10('0x78')]({'where':{'id':_0x471827[_0xbb10('0x63')]['id']}})[_0xbb10('0x19')](handleEntityNotFound(_0xcbaf69,null))[_0xbb10('0x19')](function(_0x4258fc){if(_0x4258fc){_0x5f0844[_0xbb10('0x32')]=_[_0xbb10('0x33')](db[_0xbb10('0x4e')][_0xbb10('0x34')]);_0x5f0844['query']=_[_0xbb10('0x33')](_0x471827['query']);_0x5f0844[_0xbb10('0x3f')]=_[_0xbb10('0x64')](_0x5f0844[_0xbb10('0x32')],_0x5f0844[_0xbb10('0x36')]);_0x2c6d2c[_0xbb10('0x38')]=_[_0xbb10('0x64')](_0x5f0844['model'],qs[_0xbb10('0x39')](_0x471827['query'][_0xbb10('0x39')]));_0x2c6d2c[_0xbb10('0x38')]=_0x2c6d2c[_0xbb10('0x38')][_0xbb10('0x3a')]?_0x2c6d2c[_0xbb10('0x38')]:_0x5f0844[_0xbb10('0x32')];if(!_0x471827[_0xbb10('0x36')][_0xbb10('0x3b')](_0xbb10('0xa3'))){_0x2c6d2c[_0xbb10('0x28')]=qs[_0xbb10('0x28')](_0x471827[_0xbb10('0x36')]['limit']);_0x2c6d2c[_0xbb10('0x27')]=qs['offset'](_0x471827[_0xbb10('0x36')][_0xbb10('0x27')]);}_0x2c6d2c[_0xbb10('0x3c')]=qs[_0xbb10('0x3d')](_0x471827[_0xbb10('0x36')][_0xbb10('0x3d')]);_0x2c6d2c[_0xbb10('0x3e')]=qs['filters'](_[_0xbb10('0x40')](_0x471827['query'],_0x5f0844['filters']));_0x2c6d2c[_0xbb10('0x3e')][_0xbb10('0xa4')]=_0x4258fc['id'];if(_0x471827[_0xbb10('0x36')][_0xbb10('0x44')]){_0x2c6d2c[_0xbb10('0x3e')]=_[_0xbb10('0x43')](_0x2c6d2c['where'],{'$or':_[_0xbb10('0x45')](_0x2c6d2c[_0xbb10('0x38')],function(_0x39aa9e){var _0xb1b7c8={};_0xb1b7c8[_0x39aa9e]={'$like':'%'+_0x471827[_0xbb10('0x36')][_0xbb10('0x44')]+'%'};return _0xb1b7c8;})});}if(_0x471827['query'][_0xbb10('0xa5')]){var _0x4093a4=_0x471827[_0xbb10('0x36')][_0xbb10('0xa5')][_0xbb10('0xa6')](',');var _0x5ecb87={};_0x5ecb87[_0x4093a4[0x0]]={'$gte':moment(_0x4093a4[0x1])[_0xbb10('0x81')]('YYYY-MM-DD\x20HH:mm:ss')};_0x2c6d2c[_0xbb10('0x3e')]=_['merge'](_0x2c6d2c[_0xbb10('0x3e')],_0x5ecb87);}_0x2c6d2c=_['merge']({},_0x2c6d2c,_0x471827['options']);return db[_0xbb10('0x4e')][_0xbb10('0x26')]({'where':_0x2c6d2c[_0xbb10('0x3e')]})[_0xbb10('0x19')](function(_0x2ad286){_0x1e022d['count']=_0x2ad286;if(_0x471827[_0xbb10('0x36')]['includeAll']){_0x2c6d2c['include']=[{'all':!![]}];}return db['ChatMessage'][_0xbb10('0x5f')](_0x2c6d2c);})[_0xbb10('0x19')](function(_0x1b9012){_0x1e022d[_0xbb10('0x60')]=_0x1b9012;return _0x1e022d;});}})[_0xbb10('0x19')](respondWithFilteredResult(_0xcbaf69,_0x2c6d2c))['catch'](handleError(_0xcbaf69,null));};exports[_0xbb10('0xa7')]=function(_0x42a2d7,_0x352de4,_0x1bd1c1){var _0x4b3b21={'where':{}},_0x37ff15={},_0x2ef6ee={'count':0x0,'rows':[]};return db['ChatInteraction']['findOne']({'where':{'id':_0x42a2d7['params']['id']}})[_0xbb10('0x19')](handleEntityNotFound(_0x352de4,null))[_0xbb10('0x19')](function(_0x2ae3c6){if(_0x2ae3c6){_0x37ff15[_0xbb10('0x32')]=_[_0xbb10('0x33')](db[_0xbb10('0x4e')][_0xbb10('0x34')]);_0x37ff15[_0xbb10('0x36')]=_[_0xbb10('0x33')](_0x42a2d7[_0xbb10('0x36')]);_0x37ff15['filters']=_['intersection'](_0x37ff15['model'],_0x37ff15[_0xbb10('0x36')]);_0x4b3b21[_0xbb10('0x38')]=_[_0xbb10('0x64')](_0x37ff15['model'],qs[_0xbb10('0x39')](_0x42a2d7[_0xbb10('0x36')][_0xbb10('0x39')]));_0x4b3b21[_0xbb10('0x38')]=_0x4b3b21[_0xbb10('0x38')][_0xbb10('0x3a')]?_0x4b3b21['attributes']:_0x37ff15['model'];if(!_0x42a2d7[_0xbb10('0x36')]['hasOwnProperty'](_0xbb10('0xa3'))){_0x4b3b21[_0xbb10('0x28')]=qs[_0xbb10('0x28')](_0x42a2d7[_0xbb10('0x36')][_0xbb10('0x28')]);_0x4b3b21[_0xbb10('0x27')]=qs[_0xbb10('0x27')](_0x42a2d7[_0xbb10('0x36')][_0xbb10('0x27')]);}_0x4b3b21[_0xbb10('0x3c')]=qs['sort'](_0x42a2d7[_0xbb10('0x36')]['sort']);_0x4b3b21[_0xbb10('0x3e')]=qs['filters'](_[_0xbb10('0x40')](_0x42a2d7[_0xbb10('0x36')],_0x37ff15[_0xbb10('0x3f')]));_0x4b3b21[_0xbb10('0x3e')]['ChatInteractionId']=_0x2ae3c6['id'];if(_0x42a2d7[_0xbb10('0x36')]['filter']){_0x4b3b21[_0xbb10('0x3e')]=_[_0xbb10('0x43')](_0x4b3b21[_0xbb10('0x3e')],{'$or':_['map'](_0x4b3b21[_0xbb10('0x38')],function(_0x316050){var _0x140173={};_0x140173[_0x316050]={'$like':'%'+_0x42a2d7[_0xbb10('0x36')][_0xbb10('0x44')]+'%'};return _0x140173;})});}_0x4b3b21[_0xbb10('0x3e')]['secret']=![];if(_0x42a2d7['query']['id']){_0x4b3b21['where']['id']={'$gt':_0x42a2d7[_0xbb10('0x36')]['id']};}_0x4b3b21=_['merge']({},_0x4b3b21,_0x42a2d7[_0xbb10('0x46')]);return db['ChatMessage'][_0xbb10('0x26')]({'where':_0x4b3b21[_0xbb10('0x3e')]})[_0xbb10('0x19')](function(_0x5d5288){_0x2ef6ee['count']=_0x5d5288;_0x4b3b21[_0xbb10('0x47')]=[];if(_0x42a2d7[_0xbb10('0x36')][_0xbb10('0xa8')]){_0x4b3b21['include'][_0xbb10('0x49')]({'model':db[_0xbb10('0x54')],'as':_0xbb10('0x54'),'attributes':['id','fullname','alias']});}if(_0x42a2d7['query'][_0xbb10('0x53')]){_0x4b3b21['include']=[{'all':!![]}];}return db[_0xbb10('0x4e')][_0xbb10('0x5f')](_0x4b3b21);})[_0xbb10('0x19')](function(_0x72f1c1){_0x2ef6ee[_0xbb10('0x60')]=_0x72f1c1;return _0x2ef6ee;});}})[_0xbb10('0x19')](respondWithFilteredResult(_0x352de4,_0x4b3b21))[_0xbb10('0x61')](handleError(_0x352de4,null));};exports[_0xbb10('0xa9')]=function(_0x2746eb,_0x431792,_0x2e2710){return db['ChatInteraction'][_0xbb10('0x65')]({'where':{'id':_0x2746eb[_0xbb10('0x63')]['id']}})['then'](handleEntityNotFound(_0x431792,null))[_0xbb10('0x19')](function(_0x5e3fac){if(_0x5e3fac){return _0x5e3fac['setTags'](_0x2746eb[_0xbb10('0x67')][_0xbb10('0xa0')],_[_0xbb10('0xa1')](_0x2746eb[_0xbb10('0x67')],[_0xbb10('0xa0'),'id'])||{})['spread'](function(){return db[_0xbb10('0x4a')][_0xbb10('0x5f')]({'attributes':['id','name',_0xbb10('0x5d')],'where':{'id':_0x2746eb[_0xbb10('0x67')]['ids']}});})[_0xbb10('0x19')](function(_0x4c769b){socket[_0xbb10('0xaa')](_0xbb10('0xab'),{'id':Number(_0x2746eb[_0xbb10('0x63')]['id']),'tags':_0x4c769b||[]});return{'id':Number(_0x2746eb[_0xbb10('0x63')]['id']),'tags':_0x4c769b||[]};});}})[_0xbb10('0x19')](respondWithResult(_0x431792,null))[_0xbb10('0x61')](handleError(_0x431792,null));};exports[_0xbb10('0xac')]=function(_0x56f902,_0x1c2a75,_0x4720a1){var _0x1cdad0;return db[_0xbb10('0x52')][_0xbb10('0x65')]({'where':{'id':_0x56f902[_0xbb10('0x63')]['id']}})['then'](handleEntityNotFound(_0x1c2a75,null))['then'](function(_0x5a40e8){if(_0x5a40e8){return _0x5a40e8[_0xbb10('0xac')](_0x56f902[_0xbb10('0x36')][_0xbb10('0xa0')]);}})[_0xbb10('0x19')](respondWithStatusCode(_0x1c2a75,null))['catch'](handleError(_0x1c2a75,null));};