Built motion from commit 87cd5f02.|2.5.48
[motion2.git] / server / api / realtime / realtime.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 _0x6f47=['address','faxPause','openchannelCapacity','mailCapacity','smsCapacity','whatsappCapacity','faxCapacity','chatCurrentCapacity','openchannelCurrentCapacity','mailCurrentCapacity','smsCurrentCapacity','openchannelStatus','chatStatusTime','mailStatusTime','whatsappStatusTime','faxStatusTime','voiceStatusTime','screenrecording','phoneBarEnableScreenRecordingByAgent','phoneBarEnableAutomaticScreenRecording','routealias','mailPause','resolve','fieldName','type','query','agent','getOptions','raw','user','role','userProfileId','sort','UserProfile','getResources','findAndCountAll','rows','omit','globalStatus','globalStatusFilter','pauseTypeFilter','getQueues','chat','openchannel','fax','channel','includes','toLowerCase','Invalid\x20channel','rawAttributes','key','lodash','jayson/promise','../../config/logger','../../mysqldb','http','client','offset','limit','undefined','count','status','set','Content-Range','json','error','stack','name','request','Agents','then','Unable\x20to\x20retrieve\x20RPC\x20agents\x20(AMI)','result','Unable\x20to\x20retrieve\x20RPC\x20agents\x20(Routing)','merge','catch','startCase','Queues','format','Unable\x20to\x20retrieve\x20RPC\x20%s\x20queues','available','voice','outbound','push','pTalking','message','dialMethod','Trunk','startPredictive','startProgressive','erlangCalls','erlangBusyFactor','dialPredictiveInterval','dialPredictiveIntervalMaxThreshold','dialPredictiveIntervalMinThreshold','predictiveIntervalAvailable','predictiveIntervalTotalCalls','predictiveIntervalAnsweredCalls','predictiveIntervalDroppedCallsTimeout','predictiveIntervalDroppedCallsCallersExit','predictiveIntervalAvgHoldtime','predictiveIntervalAvgTalktime','map','paused','loggedInDb','loggedIn','waiting','talking','busy','originated','find','pick','filter','queue','length','connectedlinenum','internal','toString','getMembers','UserId','accountcode','online','voiceStatus','agents','User','Queue','findAll','QueueId','idle','unknown','ringing','isNil','chatPause','openchannelPause','smsPause','voicePause','whatsappPause','every','pause','chatStatus','faxStatus','mailStatus','smsStatus','whatsappStatus','*pause','userpic','lastPauseAt','pauseType'];(function(_0x3b150c,_0x2f93f8){var _0x5cd414=function(_0x26305e){while(--_0x26305e){_0x3b150c['push'](_0x3b150c['shift']());}};_0x5cd414(++_0x2f93f8);}(_0x6f47,0xc7));var _0x76f4=function(_0x502c7d,_0x2f1285){_0x502c7d=_0x502c7d-0x0;var _0x30b7ed=_0x6f47[_0x502c7d];return _0x30b7ed;};'use strict';var util=require('util');var _=require(_0x76f4('0x0'));var jayson=require(_0x76f4('0x1'));var qs=require('../../components/parsers/qs');var logger=require(_0x76f4('0x2'))('api');var db=require(_0x76f4('0x3'))['db'];var amiClient=jayson['client'][_0x76f4('0x4')]({'port':0x232a});var routingClient=jayson[_0x76f4('0x5')][_0x76f4('0x4')]({'port':0x232c});function respondWithFilteredResult(_0x2523ba,_0x5754ca){return function(_0x35602f){if(_0x35602f){var _0xfc2405=typeof _0x5754ca[_0x76f4('0x6')]==='undefined'&&typeof _0x5754ca[_0x76f4('0x7')]===_0x76f4('0x8');var _0x65d2c7=_0x35602f[_0x76f4('0x9')];var _0x101f55=_0xfc2405?0x0:_0x5754ca['offset'];var _0x5367d6=_0xfc2405?_0x35602f[_0x76f4('0x9')]:_0x5754ca[_0x76f4('0x6')]+_0x5754ca['limit'];var _0x51d500;if(_0x5367d6>=_0x65d2c7){_0x5367d6=_0x65d2c7;_0x51d500=0xc8;}else{_0x51d500=0xce;}_0x2523ba[_0x76f4('0xa')](_0x51d500);return _0x2523ba[_0x76f4('0xb')](_0x76f4('0xc'),_0x101f55+'-'+_0x5367d6+'/'+_0x65d2c7)[_0x76f4('0xd')](_0x35602f);}return null;};}function handleError(_0x1aee7f,_0xafd21e){_0xafd21e=_0xafd21e||0x1f4;return function(_0xf7e347){logger[_0x76f4('0xe')](_0xf7e347[_0x76f4('0xf')]);if(_0xf7e347['name']){delete _0xf7e347[_0x76f4('0x10')];}_0x1aee7f[_0x76f4('0xa')](_0xafd21e)['send'](_0xf7e347);};}function getRealtimeAgents(){return new Promise(function(_0x4b6021,_0x5d6f8e){return amiClient[_0x76f4('0x11')](_0x76f4('0x12'),{})[_0x76f4('0x13')](function(_0x40252d){if(_0x40252d[_0x76f4('0xe')])throw new Error(_0x76f4('0x14'));var _0x46ba51=_0x40252d[_0x76f4('0x15')];return routingClient[_0x76f4('0x11')](_0x76f4('0x12'),{})[_0x76f4('0x13')](function(_0x2d4bd1){if(_0x2d4bd1[_0x76f4('0xe')])throw new Error(_0x76f4('0x16'));var _0x17625e=_0x2d4bd1[_0x76f4('0x15')];return _0x4b6021(_[_0x76f4('0x17')]({},_0x46ba51,_0x17625e));});})[_0x76f4('0x18')](function(_0x435534){_0x5d6f8e(_0x435534);});});}function getRealtimeQueues(_0x35b406,_0x46f057){return new Promise(function(_0x285232,_0x5bf3d5){var _0x262937=_0x46f057==='voice'?amiClient:routingClient;return _0x262937[_0x76f4('0x11')](_[_0x76f4('0x19')](_0x46f057)+_0x76f4('0x1a'),_0x35b406)['then'](function(_0x567f2a){if(_0x567f2a['error'])throw new Error(util[_0x76f4('0x1b')](_0x76f4('0x1c'),_0x46f057));return _0x285232(_0x567f2a[_0x76f4('0x15')]);})[_0x76f4('0x18')](function(_0x42ef70){_0x5bf3d5(_0x42ef70);});});}function getRealtimeVoiceQueueChannels(){return new Promise(function(_0x4786c9,_0x3f4d11){return amiClient['request']('VoiceQueuesChannels',{})[_0x76f4('0x13')](function(_0x497913){if(_0x497913[_0x76f4('0xe')])throw new Error('Unable\x20to\x20retrieve\x20voice\x20queues\x20channels');return _0x4786c9(_0x497913['result']);})['catch'](function(_0x470242){_0x3f4d11(_0x470242);});});}function initializeQueues(_0x38300d,_0xd368a2,_0x1d1fe6,_0x13d219){var _0x447dbc=['loggedIn',_0x76f4('0x1d'),'waiting','talking','paused'];if(_0x1d1fe6===_0x76f4('0x1e')&&_0x13d219===_0x76f4('0x1f')){_0x447dbc[_0x76f4('0x20')](_0x76f4('0x21'),'originated',_0x76f4('0x22'),'dialActive',_0x76f4('0x23'),_0x76f4('0x24'),_0x76f4('0x25'),_0x76f4('0x26'),_0x76f4('0x27'),'erlangCallToSecond','erlangAbandonmentRate',_0x76f4('0x28'),_0x76f4('0x29'),_0x76f4('0x2a'),_0x76f4('0x2b'),_0x76f4('0x2c'),_0x76f4('0x2d'),_0x76f4('0x2e'),_0x76f4('0x2f'),_0x76f4('0x30'),_0x76f4('0x31'),_0x76f4('0x32'));}return _0x38300d[_0x76f4('0x33')](function(_0x5326b2){var _0x58d4b7=_0x5326b2['get']({'plain':!![]});_0x58d4b7['agents']=[];_0x58d4b7[_0x76f4('0x34')]=0x0;_0x58d4b7[_0x76f4('0x35')]=0x0;_0x58d4b7[_0x76f4('0x36')]=0x0;_0x58d4b7[_0x76f4('0x1d')]=0x0;_0x58d4b7[_0x76f4('0x37')]=0x0;if(_0x1d1fe6==='voice'){_0x58d4b7[_0x76f4('0x38')]=0x0;_0x58d4b7[_0x76f4('0x39')]=0x0;if(_0x13d219===_0x76f4('0x1f')){_0x58d4b7[_0x76f4('0x21')]=0x0;_0x58d4b7[_0x76f4('0x3a')]=0x0;}}var _0x23278f=_[_0x76f4('0x3b')](_0xd368a2,['id',_0x58d4b7['id']]);if(_0x23278f)_[_0x76f4('0x17')](_0x58d4b7,_[_0x76f4('0x3c')](_0x23278f,_0x447dbc));return _0x58d4b7;});}function updateAgentVoiceStatus(_0xdce828,_0x6953ca,_0x244607){var _0x2d744e=_[_0x76f4('0x3d')](_0x244607,[_0x76f4('0x3e'),_0x6953ca[_0x76f4('0x10')]]);if(_0x2d744e[_0x76f4('0x3f')]===0x0)return _0xdce828[_0x76f4('0x39')]=!![];if(_['some'](_0x2d744e,[_0x76f4('0x40'),_0xdce828[_0x76f4('0x41')][_0x76f4('0x42')]()]))return _0xdce828['talking']=!![];if(!_0xdce828['talking'])return _0xdce828[_0x76f4('0x39')]=!![];}function getVoiceQueueAgents(_0x487926,_0x229d1e,_0x5ac18f,_0x152af8){var _0x16f36d=_[_0x76f4('0x3b')](_0x229d1e,['id',_0x487926['id']]);if(!_0x16f36d)return _0x487926;return _0x16f36d[_0x76f4('0x43')]({'raw':!![]})[_0x76f4('0x13')](function(_0x2d99e3){for(var _0x21ae82=0x0;_0x21ae82<_0x2d99e3[_0x76f4('0x3f')];_0x21ae82++){var _0x16779e=_[_0x76f4('0x3b')](_0x5ac18f,['id',_0x2d99e3[_0x21ae82]['UserId']]);if(!_0x16779e){_0x16779e={'id':_0x2d99e3[_0x21ae82]['UserId'],'name':_0x2d99e3[_0x21ae82][_0x76f4('0x44')],'accountcode':'','online':![]};}var _0x13a5a7={'id':_0x16779e['id'],'name':_0x16779e[_0x76f4('0x10')],'internal':_0x16779e[_0x76f4('0x45')],'online':Boolean(_0x16779e[_0x76f4('0x46')]),'paused':Boolean(_0x2d99e3[_0x21ae82][_0x76f4('0x34')])};if(_0x16779e[_0x76f4('0x47')]===_0x76f4('0x38'))updateAgentVoiceStatus(_0x13a5a7,_0x487926,_0x152af8);if(_0x13a5a7[_0x76f4('0x46')])_0x487926['loggedInDb']++;if(_0x13a5a7[_0x76f4('0x39')])_0x487926[_0x76f4('0x39')]++;if(_0x13a5a7['paused']&&!_0x13a5a7['busy']&&!_0x13a5a7[_0x76f4('0x38')])_0x487926[_0x76f4('0x34')]++;_0x487926[_0x76f4('0x48')][_0x76f4('0x20')](_0x13a5a7);}return _0x487926;});}function getQueueAgents(_0x212f50,_0x597734,_0x1b6183){var _0x4126aa=_0x76f4('0x49')+_[_0x76f4('0x19')](_0x1b6183)+_0x76f4('0x4a');var _0x673267=_0x1b6183+'Pause';return db[_0x4126aa][_0x76f4('0x4b')]({'where':{[_[_0x76f4('0x19')](_0x1b6183)+_0x76f4('0x4c')]:_0x212f50['id']},'raw':!![]})['then'](function(_0x571261){for(var _0x11ea58=0x0;_0x11ea58<_0x571261[_0x76f4('0x3f')];_0x11ea58++){var _0x306cc2=_[_0x76f4('0x3b')](_0x597734,['id',_0x571261[_0x11ea58][_0x76f4('0x44')]]);if(!_0x306cc2){_0x306cc2={'id':_0x571261[_0x11ea58]['UserId'],'name':_0x571261[_0x11ea58]['UserId'],'online':![],['pauseField']:![]};}var _0x8e5b60={'id':_0x306cc2['id'],'name':_0x306cc2['name'],'online':Boolean(_0x306cc2[_0x76f4('0x46')]),'paused':Boolean(_0x306cc2[_0x673267])};if(_0x8e5b60[_0x76f4('0x46')])_0x212f50['loggedInDb']++;_0x212f50['agents'][_0x76f4('0x20')](_0x8e5b60);}return _0x212f50;});}function isIdle(_0x40833d){return _0x40833d===_0x76f4('0x4d')||_0x40833d===_0x76f4('0x4e')||_0x40833d==='unavailable'||_0x40833d===_0x76f4('0x4f')||_[_0x76f4('0x50')](_0x40833d);}function isAgentPaused(_0x28922a,_0x533dca){var _0x23afe4=[_0x28922a[_0x76f4('0x51')],_0x28922a['faxPause'],_0x28922a['mailPause'],_0x28922a[_0x76f4('0x52')],_0x28922a[_0x76f4('0x53')],_0x28922a[_0x76f4('0x54')],_0x28922a[_0x76f4('0x55')]];if(!_0x533dca)return _[_0x76f4('0x56')](_0x23afe4);var _0x524503=_[_0x76f4('0x3d')](_0x23afe4);return _0x524503['length']>0x0&&_0x524503[_0x76f4('0x3f')]<_0x23afe4[_0x76f4('0x3f')]?!![]:![];}function isAgentIdle(_0x270baf){if(_0x270baf['voiceStatus']!==_0x76f4('0x4d')&&_0x270baf[_0x76f4('0x54')]){_0x270baf['voiceStatus']=_0x76f4('0x57');return![];}return _[_0x76f4('0x56')]([_0x270baf[_0x76f4('0x58')],_0x270baf[_0x76f4('0x59')],_0x270baf[_0x76f4('0x5a')],_0x270baf['openchannelStatus'],_0x270baf[_0x76f4('0x5b')],_0x270baf['voiceStatus'],_0x270baf[_0x76f4('0x5c')]],isIdle);}function getAgentStatus(_0x38dc69){var _0x154ba='unknown';if(isAgentPaused(_0x38dc69))_0x154ba=_0x76f4('0x57');else if(isAgentPaused(_0x38dc69,!![]))_0x154ba=_0x76f4('0x5d');else if(isAgentIdle(_0x38dc69))_0x154ba=_0x76f4('0x4d');else _0x154ba=_0x76f4('0x39');return _0x154ba;}exports['getAgents']=function(_0x2028e9,_0xf11e07){var _0xaad66b={'count':0x0,'rows':[]},_0xff4b9b={},_0x4dab07,_0x1d1117,_0x5b492c=[_0x76f4('0x5e'),'online','lastLoginAt',_0x76f4('0x5f'),_0x76f4('0x60'),_0x76f4('0x5f'),'internal',_0x76f4('0x61'),_0x76f4('0x51'),_0x76f4('0x52'),'mailPause','smsPause',_0x76f4('0x55'),_0x76f4('0x62'),'voicePause','chatCapacity',_0x76f4('0x63'),_0x76f4('0x64'),_0x76f4('0x65'),_0x76f4('0x66'),_0x76f4('0x67'),_0x76f4('0x68'),_0x76f4('0x69'),_0x76f4('0x6a'),_0x76f4('0x6b'),'whatsappCurrentCapacity','faxCurrentCapacity','chatStatus',_0x76f4('0x6c'),_0x76f4('0x5a'),_0x76f4('0x5b'),_0x76f4('0x5c'),'faxStatus','voiceStatus',_0x76f4('0x6d'),'openchannelStatusTime',_0x76f4('0x6e'),'smsStatusTime',_0x76f4('0x6f'),_0x76f4('0x70'),_0x76f4('0x71'),_0x76f4('0x3e'),'destconnectedlinenum',_0x76f4('0x72'),_0x76f4('0x73'),_0x76f4('0x74'),_0x76f4('0x75')],_0x129048=[_0x76f4('0x51'),_0x76f4('0x52'),_0x76f4('0x76'),'smsPause',_0x76f4('0x55'),_0x76f4('0x62'),_0x76f4('0x54'),_0x76f4('0x46')];Promise[_0x76f4('0x77')]()[_0x76f4('0x13')](function(){var _0x2e3653=_[_0x76f4('0x33')](db['User']['rawAttributes'],function(_0xa3091a){return{'name':_0xa3091a[_0x76f4('0x78')],'type':_0xa3091a[_0x76f4('0x79')]['key']};});_0x2028e9[_0x76f4('0x7a')]['role']=_0x76f4('0x7b');_0x2028e9[_0x76f4('0x7a')]['online']=!![];_0xff4b9b=qs[_0x76f4('0x7c')](_0x2e3653,_0x2028e9);_0xff4b9b[_0x76f4('0x7d')]=!![];if(_0x2028e9[_0x76f4('0x7e')][_0x76f4('0x7f')]===_0x76f4('0x7e')){var _0x351fde={'params':{'id':_0x2028e9[_0x76f4('0x7e')][_0x76f4('0x80')]},'query':{'section':_0x76f4('0x12'),'role':'agent','online':!![],'nolimit':_0x2028e9[_0x76f4('0x7a')]['nolimit'],'sort':_0x2028e9[_0x76f4('0x7a')][_0x76f4('0x81')],'filter':_0x2028e9[_0x76f4('0x7a')][_0x76f4('0x3d')],'raw':!![]}};return db[_0x76f4('0x82')][_0x76f4('0x83')](_0x351fde);}return db[_0x76f4('0x49')][_0x76f4('0x84')](_0xff4b9b);})[_0x76f4('0x13')](function(_0x373848){_0xaad66b['count']=_0x373848[_0x76f4('0x9')];_0x4dab07=_0x373848[_0x76f4('0x85')];return getRealtimeAgents();})['then'](function(_0x30b1f7){_0x1d1117=_0x30b1f7[_0x76f4('0x85')];return _0x4dab07[_0x76f4('0x33')](function(_0x53974b){var _0x318f78=_[_0x76f4('0x3b')](_0x1d1117,['id',_0x53974b['id']]);if(_0x318f78)_[_0x76f4('0x17')](_0x53974b,_['pick'](_[_0x76f4('0x86')](_0x318f78,_0x129048),_0x5b492c));_0x53974b[_0x76f4('0x87')]=getAgentStatus(_0x53974b);return _0x53974b;});})[_0x76f4('0x13')](function(_0x1ed3b0){if(_0x2028e9[_0x76f4('0x7a')][_0x76f4('0x88')]){var _0x19b99c=_[_0x76f4('0x3d')](_0x1ed3b0,[_0x76f4('0x87'),_0x2028e9['query'][_0x76f4('0x88')]]);if(_0x2028e9[_0x76f4('0x7a')][_0x76f4('0x89')]){_0x19b99c=_[_0x76f4('0x3d')](_0x19b99c,['pauseType',_0x2028e9[_0x76f4('0x7a')][_0x76f4('0x89')]]);}}_0xaad66b[_0x76f4('0x85')]=_0x19b99c||_0x1ed3b0;return _0xaad66b;})['then'](respondWithFilteredResult(_0xf11e07,_0xff4b9b))[_0x76f4('0x18')](handleError(_0xf11e07,null));};exports[_0x76f4('0x8a')]=function(_0x31249b,_0x99fbd6){var _0x400050=['mail',_0x76f4('0x8b'),'sms',_0x76f4('0x8c'),_0x76f4('0x8d'),'whatsapp','voice'];var _0x58251e={'count':0x0,'rows':[]},_0x3aa958={},_0x462f3e,_0x1e7b6,_0x3e2a07,_0x32fe75,_0x2a677d;Promise['resolve']()[_0x76f4('0x13')](function(){if(_0x31249b[_0x76f4('0x7a')][_0x76f4('0x8e')]){if(!_[_0x76f4('0x8f')](_0x400050,_0x31249b[_0x76f4('0x7a')][_0x76f4('0x8e')][_0x76f4('0x90')]()))throw new Error(_0x76f4('0x91'));_0x462f3e=_0x31249b['query'][_0x76f4('0x8e')]['toLowerCase']();}var _0x4b5532=_[_0x76f4('0x19')](_0x462f3e)+_0x76f4('0x4a');var _0x220a42=_[_0x76f4('0x33')](db[_0x4b5532][_0x76f4('0x92')],function(_0x36b601){return{'name':_0x36b601[_0x76f4('0x78')],'type':_0x36b601['type'][_0x76f4('0x93')]};});_0x3aa958=qs['getOptions'](_0x220a42,_0x31249b);if(_0x31249b['user']['role']===_0x76f4('0x7e')){var _0x402209={'params':{'id':_0x31249b[_0x76f4('0x7e')][_0x76f4('0x80')]},'query':{'section':_0x462f3e==='voice'&&_0x31249b[_0x76f4('0x7a')][_0x76f4('0x79')]===_0x76f4('0x1f')?'QueueCampaigns':_[_0x76f4('0x19')](_0x462f3e)+_0x76f4('0x1a'),'type':_0x31249b[_0x76f4('0x7a')][_0x76f4('0x79')],'nolimit':!![],'sort':_0x76f4('0x10'),'filter':_0x31249b[_0x76f4('0x7a')][_0x76f4('0x3d')]}};return db['UserProfile'][_0x76f4('0x83')](_0x402209);}return db[_0x4b5532][_0x76f4('0x84')](_0x3aa958);})['then'](function(_0x3bc914){_0x58251e[_0x76f4('0x9')]=_0x3bc914[_0x76f4('0x9')];_0x1e7b6=_0x3bc914[_0x76f4('0x85')];return getRealtimeQueues(_0x31249b,_0x462f3e);})[_0x76f4('0x13')](function(_0x1f62a9){_0x32fe75=_0x1f62a9[_0x76f4('0x85')];return getRealtimeAgents();})[_0x76f4('0x13')](function(_0x330d61){_0x3e2a07=_0x330d61['rows'];if(_0x462f3e!=_0x76f4('0x1e'))return;return getRealtimeVoiceQueueChannels();})[_0x76f4('0x13')](function(_0x1f5f0d){_0x2a677d=_0x1f5f0d?_0x1f5f0d[_0x76f4('0x85')]:[];return initializeQueues(_0x1e7b6,_0x32fe75,_0x462f3e,_0x31249b['query'][_0x76f4('0x79')]);})['then'](function(_0x420281){var _0x4a7a98=_0x420281[_0x76f4('0x33')](function(_0x7380f){if(_0x462f3e==='voice')return getVoiceQueueAgents(_0x7380f,_0x1e7b6,_0x3e2a07,_0x2a677d);else return getQueueAgents(_0x7380f,_0x3e2a07,_0x462f3e);});return Promise['all'](_0x4a7a98);})[_0x76f4('0x13')](function(_0xc709fd){_0x58251e[_0x76f4('0x85')]=_0xc709fd;return _0x58251e;})['then'](respondWithFilteredResult(_0x99fbd6,_0x3aa958))['catch'](handleError(_0x99fbd6,null));};