Built motion from commit 9d1906d1.|2.5.40
[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 _0xa900=['predictiveIntervalDroppedCallsTimeout','predictiveIntervalDroppedCallsCallersExit','predictiveIntervalAvgTalktime','map','get','loggedInDb','voice','busy','originated','find','queue','connectedlinenum','internal','UserId','accountcode','online','voiceStatus','agents','User','Queue','Pause','findAll','startCase','QueueId','unknown','unavailable','ringing','isNil','chatPause','faxPause','mailPause','openchannelPause','smsPause','voicePause','whatsappPause','filter','length','pause','every','chatStatus','mailStatus','openchannelStatus','smsStatus','*pause','getAgents','userpic','lastLoginAt','lastPauseAt','pauseType','address','whatsappCapacity','faxCapacity','chatCurrentCapacity','openchannelCurrentCapacity','mailCurrentCapacity','smsCurrentCapacity','faxCurrentCapacity','whatsappStatus','chatStatusTime','openchannelStatusTime','mailStatusTime','smsStatusTime','whatsappStatusTime','voiceStatusTime','destconnectedlinenum','phoneBarEnableScreenRecordingByAgent','phoneBarEnableAutomaticScreenRecording','routealias','resolve','type','key','query','role','agent','raw','user','userProfileId','nolimit','sort','UserProfile','getResources','rows','pick','omit','globalStatus','globalStatusFilter','pauseTypeFilter','getQueues','mail','chat','sms','fax','whatsapp','includes','channel','toLowerCase','Invalid\x20channel','rawAttributes','fieldName','getOptions','findAndCountAll','all','util','lodash','jayson/promise','../../components/parsers/qs','../../config/logger','api','client','http','offset','undefined','limit','count','status','set','Content-Range','json','error','stack','name','send','request','Agents','then','Unable\x20to\x20retrieve\x20RPC\x20agents\x20(AMI)','Unable\x20to\x20retrieve\x20RPC\x20agents\x20(Routing)','result','merge','Queues','format','Unable\x20to\x20retrieve\x20RPC\x20%s\x20queues','catch','VoiceQueuesChannels','Unable\x20to\x20retrieve\x20voice\x20queues\x20channels','loggedIn','available','waiting','talking','paused','outbound','push','pTalking','message','dialMethod','Trunk','startProgressive','erlangCallToSecond','erlangBusyFactor','dialPredictiveInterval','dialPredictiveIntervalMaxThreshold','dialPredictiveIntervalMinThreshold','predictiveIntervalAvailable','predictiveIntervalTotalCalls','predictiveIntervalAnsweredCalls'];(function(_0x59ecf9,_0x9a29d){var _0x3c68c1=function(_0x4992c5){while(--_0x4992c5){_0x59ecf9['push'](_0x59ecf9['shift']());}};_0x3c68c1(++_0x9a29d);}(_0xa900,0x101));var _0x0a90=function(_0x15beeb,_0x32bd4f){_0x15beeb=_0x15beeb-0x0;var _0x5b681a=_0xa900[_0x15beeb];return _0x5b681a;};'use strict';var util=require(_0x0a90('0x0'));var _=require(_0x0a90('0x1'));var jayson=require(_0x0a90('0x2'));var qs=require(_0x0a90('0x3'));var logger=require(_0x0a90('0x4'))(_0x0a90('0x5'));var db=require('../../mysqldb')['db'];var amiClient=jayson[_0x0a90('0x6')]['http']({'port':0x232a});var routingClient=jayson['client'][_0x0a90('0x7')]({'port':0x232c});function respondWithFilteredResult(_0x6cbb24,_0x461b32){return function(_0x34ee43){if(_0x34ee43){var _0x56a1e5=typeof _0x461b32[_0x0a90('0x8')]===_0x0a90('0x9')&&typeof _0x461b32[_0x0a90('0xa')]==='undefined';var _0x29c836=_0x34ee43[_0x0a90('0xb')];var _0x36c82f=_0x56a1e5?0x0:_0x461b32['offset'];var _0x308372=_0x56a1e5?_0x34ee43['count']:_0x461b32[_0x0a90('0x8')]+_0x461b32[_0x0a90('0xa')];var _0x57c104;if(_0x308372>=_0x29c836){_0x308372=_0x29c836;_0x57c104=0xc8;}else{_0x57c104=0xce;}_0x6cbb24[_0x0a90('0xc')](_0x57c104);return _0x6cbb24[_0x0a90('0xd')](_0x0a90('0xe'),_0x36c82f+'-'+_0x308372+'/'+_0x29c836)[_0x0a90('0xf')](_0x34ee43);}return null;};}function handleError(_0x351767,_0x37fd09){_0x37fd09=_0x37fd09||0x1f4;return function(_0x198fec){logger[_0x0a90('0x10')](_0x198fec[_0x0a90('0x11')]);if(_0x198fec[_0x0a90('0x12')]){delete _0x198fec[_0x0a90('0x12')];}_0x351767[_0x0a90('0xc')](_0x37fd09)[_0x0a90('0x13')](_0x198fec);};}function getRealtimeAgents(){return new Promise(function(_0x50dfee,_0x5ad3d2){return amiClient[_0x0a90('0x14')](_0x0a90('0x15'),{})[_0x0a90('0x16')](function(_0x166806){if(_0x166806[_0x0a90('0x10')])throw new Error(_0x0a90('0x17'));var _0x5d99b8=_0x166806['result'];return routingClient[_0x0a90('0x14')](_0x0a90('0x15'),{})['then'](function(_0x2c0719){if(_0x2c0719[_0x0a90('0x10')])throw new Error(_0x0a90('0x18'));var _0x44b75d=_0x2c0719[_0x0a90('0x19')];return _0x50dfee(_[_0x0a90('0x1a')]({},_0x5d99b8,_0x44b75d));});})['catch'](function(_0x1b4238){_0x5ad3d2(_0x1b4238);});});}function getRealtimeQueues(_0x2af720,_0x41492c){return new Promise(function(_0x5a46c5,_0x4ba044){var _0x7808b0=_0x41492c==='voice'?amiClient:routingClient;return _0x7808b0[_0x0a90('0x14')](_['startCase'](_0x41492c)+_0x0a90('0x1b'),_0x2af720)[_0x0a90('0x16')](function(_0xf3e76f){if(_0xf3e76f[_0x0a90('0x10')])throw new Error(util[_0x0a90('0x1c')](_0x0a90('0x1d'),_0x41492c));return _0x5a46c5(_0xf3e76f[_0x0a90('0x19')]);})[_0x0a90('0x1e')](function(_0x2c3431){_0x4ba044(_0x2c3431);});});}function getRealtimeVoiceQueueChannels(){return new Promise(function(_0x228279,_0x5a1bf3){return amiClient[_0x0a90('0x14')](_0x0a90('0x1f'),{})[_0x0a90('0x16')](function(_0x569dc3){if(_0x569dc3[_0x0a90('0x10')])throw new Error(_0x0a90('0x20'));return _0x228279(_0x569dc3[_0x0a90('0x19')]);})[_0x0a90('0x1e')](function(_0x5261e7){_0x5a1bf3(_0x5261e7);});});}function initializeQueues(_0x1711e2,_0xba44e,_0x74f558,_0x328294){var _0xe4729b=[_0x0a90('0x21'),_0x0a90('0x22'),_0x0a90('0x23'),_0x0a90('0x24'),_0x0a90('0x25')];if(_0x74f558==='voice'&&_0x328294===_0x0a90('0x26')){_0xe4729b[_0x0a90('0x27')](_0x0a90('0x28'),'originated',_0x0a90('0x29'),'dialActive',_0x0a90('0x2a'),_0x0a90('0x2b'),'startPredictive',_0x0a90('0x2c'),'erlangCalls',_0x0a90('0x2d'),'erlangAbandonmentRate',_0x0a90('0x2e'),_0x0a90('0x2f'),_0x0a90('0x30'),_0x0a90('0x31'),_0x0a90('0x32'),_0x0a90('0x33'),_0x0a90('0x34'),_0x0a90('0x35'),_0x0a90('0x36'),'predictiveIntervalAvgHoldtime',_0x0a90('0x37'));}return _0x1711e2[_0x0a90('0x38')](function(_0x5dc425){var _0x481eeb=_0x5dc425[_0x0a90('0x39')]({'plain':!![]});_0x481eeb['agents']=[];_0x481eeb['paused']=0x0;_0x481eeb[_0x0a90('0x3a')]=0x0;_0x481eeb[_0x0a90('0x21')]=0x0;_0x481eeb[_0x0a90('0x22')]=0x0;_0x481eeb[_0x0a90('0x23')]=0x0;if(_0x74f558===_0x0a90('0x3b')){_0x481eeb[_0x0a90('0x24')]=0x0;_0x481eeb[_0x0a90('0x3c')]=0x0;if(_0x328294==='outbound'){_0x481eeb[_0x0a90('0x28')]=0x0;_0x481eeb[_0x0a90('0x3d')]=0x0;}}var _0x1eaf10=_[_0x0a90('0x3e')](_0xba44e,['id',_0x481eeb['id']]);if(_0x1eaf10)_['merge'](_0x481eeb,_['pick'](_0x1eaf10,_0xe4729b));return _0x481eeb;});}function updateAgentVoiceStatus(_0x4fa814,_0x4f91b9,_0x1303ed){var _0x2b8fdc=_['filter'](_0x1303ed,[_0x0a90('0x3f'),_0x4f91b9[_0x0a90('0x12')]]);if(_0x2b8fdc['length']===0x0)return _0x4fa814[_0x0a90('0x3c')]=!![];if(_['some'](_0x2b8fdc,[_0x0a90('0x40'),_0x4fa814[_0x0a90('0x41')]['toString']()]))return _0x4fa814['talking']=!![];if(!_0x4fa814['talking'])return _0x4fa814[_0x0a90('0x3c')]=!![];}function getVoiceQueueAgents(_0x3b3139,_0x2cc916,_0x578dba,_0x1b3cd3){var _0x51fc21=_[_0x0a90('0x3e')](_0x2cc916,['id',_0x3b3139['id']]);if(!_0x51fc21)return _0x3b3139;return _0x51fc21['getMembers']({'raw':!![]})[_0x0a90('0x16')](function(_0x1c6673){for(var _0x135091=0x0;_0x135091<_0x1c6673['length'];_0x135091++){var _0x23313e=_[_0x0a90('0x3e')](_0x578dba,['id',_0x1c6673[_0x135091]['UserId']]);if(!_0x23313e){_0x23313e={'id':_0x1c6673[_0x135091][_0x0a90('0x42')],'name':_0x1c6673[_0x135091]['UserId'],'accountcode':'','online':![]};}var _0x24ce75={'id':_0x23313e['id'],'name':_0x23313e[_0x0a90('0x12')],'internal':_0x23313e[_0x0a90('0x43')],'online':Boolean(_0x23313e[_0x0a90('0x44')]),'paused':Boolean(_0x1c6673[_0x135091][_0x0a90('0x25')])};if(_0x23313e[_0x0a90('0x45')]===_0x0a90('0x24'))updateAgentVoiceStatus(_0x24ce75,_0x3b3139,_0x1b3cd3);if(_0x24ce75[_0x0a90('0x44')])_0x3b3139['loggedInDb']++;if(_0x24ce75[_0x0a90('0x3c')])_0x3b3139['busy']++;if(_0x24ce75[_0x0a90('0x25')]&&!_0x24ce75[_0x0a90('0x3c')]&&!_0x24ce75[_0x0a90('0x24')])_0x3b3139[_0x0a90('0x25')]++;_0x3b3139[_0x0a90('0x46')][_0x0a90('0x27')](_0x24ce75);}return _0x3b3139;});}function getQueueAgents(_0x1ab11d,_0x39b9c2,_0x30a9a6){var _0x1bb351=_0x0a90('0x47')+_['startCase'](_0x30a9a6)+_0x0a90('0x48');var _0x3bdf80=_0x30a9a6+_0x0a90('0x49');return db[_0x1bb351][_0x0a90('0x4a')]({'where':{[_[_0x0a90('0x4b')](_0x30a9a6)+_0x0a90('0x4c')]:_0x1ab11d['id']},'raw':!![]})[_0x0a90('0x16')](function(_0x2199cb){for(var _0x1f1b94=0x0;_0x1f1b94<_0x2199cb['length'];_0x1f1b94++){var _0x14fb05=_[_0x0a90('0x3e')](_0x39b9c2,['id',_0x2199cb[_0x1f1b94]['UserId']]);if(!_0x14fb05){_0x14fb05={'id':_0x2199cb[_0x1f1b94]['UserId'],'name':_0x2199cb[_0x1f1b94][_0x0a90('0x42')],'online':![],['pauseField']:![]};}var _0x42956e={'id':_0x14fb05['id'],'name':_0x14fb05[_0x0a90('0x12')],'online':Boolean(_0x14fb05[_0x0a90('0x44')]),'paused':Boolean(_0x14fb05[_0x3bdf80])};if(_0x42956e[_0x0a90('0x44')])_0x1ab11d[_0x0a90('0x3a')]++;_0x1ab11d[_0x0a90('0x46')][_0x0a90('0x27')](_0x42956e);}return _0x1ab11d;});}function isIdle(_0x2bc19f){return _0x2bc19f==='idle'||_0x2bc19f===_0x0a90('0x4d')||_0x2bc19f===_0x0a90('0x4e')||_0x2bc19f===_0x0a90('0x4f')||_[_0x0a90('0x50')](_0x2bc19f);}function isAgentPaused(_0x1ad1e0,_0x4a3c81){var _0x594bb0=[_0x1ad1e0[_0x0a90('0x51')],_0x1ad1e0[_0x0a90('0x52')],_0x1ad1e0[_0x0a90('0x53')],_0x1ad1e0[_0x0a90('0x54')],_0x1ad1e0[_0x0a90('0x55')],_0x1ad1e0[_0x0a90('0x56')],_0x1ad1e0[_0x0a90('0x57')]];if(!_0x4a3c81)return _['every'](_0x594bb0);var _0x5d7cb4=_[_0x0a90('0x58')](_0x594bb0);return _0x5d7cb4[_0x0a90('0x59')]>0x0&&_0x5d7cb4['length']<_0x594bb0[_0x0a90('0x59')]?!![]:![];}function isAgentIdle(_0x2707be){if(_0x2707be['voiceStatus']!=='idle'&&_0x2707be[_0x0a90('0x56')]){_0x2707be[_0x0a90('0x45')]=_0x0a90('0x5a');return![];}return _[_0x0a90('0x5b')]([_0x2707be[_0x0a90('0x5c')],_0x2707be['faxStatus'],_0x2707be[_0x0a90('0x5d')],_0x2707be[_0x0a90('0x5e')],_0x2707be[_0x0a90('0x5f')],_0x2707be[_0x0a90('0x45')],_0x2707be['whatsappStatus']],isIdle);}function getAgentStatus(_0x542d67){var _0x2f2e3b=_0x0a90('0x4d');if(isAgentPaused(_0x542d67))_0x2f2e3b=_0x0a90('0x5a');else if(isAgentPaused(_0x542d67,!![]))_0x2f2e3b=_0x0a90('0x60');else if(isAgentIdle(_0x542d67))_0x2f2e3b='idle';else _0x2f2e3b=_0x0a90('0x3c');return _0x2f2e3b;}exports[_0x0a90('0x61')]=function(_0x5619c9,_0x1cdc27){var _0x22250b={'count':0x0,'rows':[]},_0x24bde7={},_0x24f415,_0x527fcd,_0x271b63=[_0x0a90('0x62'),_0x0a90('0x44'),_0x0a90('0x63'),_0x0a90('0x64'),_0x0a90('0x65'),_0x0a90('0x64'),_0x0a90('0x41'),_0x0a90('0x66'),_0x0a90('0x51'),'openchannelPause',_0x0a90('0x53'),'smsPause',_0x0a90('0x57'),_0x0a90('0x52'),_0x0a90('0x56'),'chatCapacity','openchannelCapacity','mailCapacity','smsCapacity',_0x0a90('0x67'),_0x0a90('0x68'),_0x0a90('0x69'),_0x0a90('0x6a'),_0x0a90('0x6b'),_0x0a90('0x6c'),'whatsappCurrentCapacity',_0x0a90('0x6d'),_0x0a90('0x5c'),_0x0a90('0x5e'),'mailStatus',_0x0a90('0x5f'),_0x0a90('0x6e'),'faxStatus',_0x0a90('0x45'),_0x0a90('0x6f'),_0x0a90('0x70'),_0x0a90('0x71'),_0x0a90('0x72'),_0x0a90('0x73'),'faxStatusTime',_0x0a90('0x74'),_0x0a90('0x3f'),_0x0a90('0x75'),'screenrecording',_0x0a90('0x76'),_0x0a90('0x77'),_0x0a90('0x78')],_0x2ec89b=[_0x0a90('0x51'),_0x0a90('0x54'),_0x0a90('0x53'),'smsPause','whatsappPause',_0x0a90('0x52'),_0x0a90('0x56'),_0x0a90('0x44')];Promise[_0x0a90('0x79')]()['then'](function(){var _0x42ae73=_[_0x0a90('0x38')](db[_0x0a90('0x47')]['rawAttributes'],function(_0x45dcd8){return{'name':_0x45dcd8['fieldName'],'type':_0x45dcd8[_0x0a90('0x7a')][_0x0a90('0x7b')]};});_0x5619c9[_0x0a90('0x7c')][_0x0a90('0x7d')]=_0x0a90('0x7e');_0x5619c9['query'][_0x0a90('0x44')]=!![];_0x24bde7=qs['getOptions'](_0x42ae73,_0x5619c9);_0x24bde7[_0x0a90('0x7f')]=!![];if(_0x5619c9[_0x0a90('0x80')]['role']===_0x0a90('0x80')){var _0x381516={'params':{'id':_0x5619c9[_0x0a90('0x80')][_0x0a90('0x81')]},'query':{'section':_0x0a90('0x15'),'role':_0x0a90('0x7e'),'online':!![],'nolimit':_0x5619c9[_0x0a90('0x7c')][_0x0a90('0x82')],'sort':_0x5619c9['query'][_0x0a90('0x83')],'filter':_0x5619c9[_0x0a90('0x7c')][_0x0a90('0x58')]},'raw':!![]};return db[_0x0a90('0x84')][_0x0a90('0x85')](_0x381516);}return db[_0x0a90('0x47')]['findAndCountAll'](_0x24bde7);})[_0x0a90('0x16')](function(_0x5730bf){_0x22250b[_0x0a90('0xb')]=_0x5730bf[_0x0a90('0xb')];_0x24f415=_0x5730bf[_0x0a90('0x86')];return getRealtimeAgents();})[_0x0a90('0x16')](function(_0x20ebc0){_0x527fcd=_0x20ebc0[_0x0a90('0x86')];return _0x24f415[_0x0a90('0x38')](function(_0x5e69df){var _0x1add51=_['find'](_0x527fcd,['id',_0x5e69df['id']]);if(_0x1add51)_[_0x0a90('0x1a')](_0x5e69df,_[_0x0a90('0x87')](_[_0x0a90('0x88')](_0x1add51,_0x2ec89b),_0x271b63));_0x5e69df[_0x0a90('0x89')]=getAgentStatus(_0x5e69df);return _0x5e69df;});})['then'](function(_0xaac1e6){if(_0x5619c9['query'][_0x0a90('0x8a')]){var _0x14c74a=_[_0x0a90('0x58')](_0xaac1e6,['globalStatus',_0x5619c9['query'][_0x0a90('0x8a')]]);if(_0x5619c9['query']['pauseTypeFilter']){_0x14c74a=_[_0x0a90('0x58')](_0x14c74a,[_0x0a90('0x65'),_0x5619c9[_0x0a90('0x7c')][_0x0a90('0x8b')]]);}}_0x22250b[_0x0a90('0x86')]=_0x14c74a||_0xaac1e6;return _0x22250b;})[_0x0a90('0x16')](respondWithFilteredResult(_0x1cdc27,_0x24bde7))[_0x0a90('0x1e')](handleError(_0x1cdc27,null));};exports[_0x0a90('0x8c')]=function(_0x35244f,_0x2c2103){var _0x102837=[_0x0a90('0x8d'),_0x0a90('0x8e'),_0x0a90('0x8f'),'openchannel',_0x0a90('0x90'),_0x0a90('0x91'),'voice'];var _0x2c5b3a={'count':0x0,'rows':[]},_0x3f6ae0={},_0x443f52,_0x47bd27,_0x52723b,_0x991d1a,_0x3ac416;Promise[_0x0a90('0x79')]()['then'](function(){if(_0x35244f[_0x0a90('0x7c')]['channel']){if(!_[_0x0a90('0x92')](_0x102837,_0x35244f[_0x0a90('0x7c')][_0x0a90('0x93')][_0x0a90('0x94')]()))throw new Error(_0x0a90('0x95'));_0x443f52=_0x35244f[_0x0a90('0x7c')][_0x0a90('0x93')]['toLowerCase']();}var _0x58a9dc=_[_0x0a90('0x4b')](_0x443f52)+'Queue';var _0x5e5e4e=_[_0x0a90('0x38')](db[_0x58a9dc][_0x0a90('0x96')],function(_0x478bc6){return{'name':_0x478bc6[_0x0a90('0x97')],'type':_0x478bc6['type'][_0x0a90('0x7b')]};});_0x3f6ae0=qs[_0x0a90('0x98')](_0x5e5e4e,_0x35244f);if(_0x35244f[_0x0a90('0x80')][_0x0a90('0x7d')]==='user'){var _0x5ca644={'params':{'id':_0x35244f[_0x0a90('0x80')][_0x0a90('0x81')]},'query':{'section':_0x443f52===_0x0a90('0x3b')&&_0x35244f[_0x0a90('0x7c')][_0x0a90('0x7a')]===_0x0a90('0x26')?'QueueCampaigns':_[_0x0a90('0x4b')](_0x443f52)+_0x0a90('0x1b'),'type':_0x35244f['query'][_0x0a90('0x7a')],'nolimit':!![],'sort':_0x0a90('0x12'),'filter':_0x35244f[_0x0a90('0x7c')][_0x0a90('0x58')]}};return db[_0x0a90('0x84')][_0x0a90('0x85')](_0x5ca644);}return db[_0x58a9dc][_0x0a90('0x99')](_0x3f6ae0);})['then'](function(_0x5ebe27){_0x2c5b3a[_0x0a90('0xb')]=_0x5ebe27[_0x0a90('0xb')];_0x47bd27=_0x5ebe27[_0x0a90('0x86')];return getRealtimeQueues(_0x35244f,_0x443f52);})[_0x0a90('0x16')](function(_0xa70942){_0x991d1a=_0xa70942['rows'];return getRealtimeAgents();})[_0x0a90('0x16')](function(_0xfc9ddb){_0x52723b=_0xfc9ddb['rows'];if(_0x443f52!=_0x0a90('0x3b'))return;return getRealtimeVoiceQueueChannels();})[_0x0a90('0x16')](function(_0xde87a8){_0x3ac416=_0xde87a8?_0xde87a8[_0x0a90('0x86')]:[];return initializeQueues(_0x47bd27,_0x991d1a,_0x443f52,_0x35244f[_0x0a90('0x7c')]['type']);})[_0x0a90('0x16')](function(_0x313aa4){var _0x2a9fd3=_0x313aa4[_0x0a90('0x38')](function(_0x121e21){if(_0x443f52==='voice')return getVoiceQueueAgents(_0x121e21,_0x47bd27,_0x52723b,_0x3ac416);else return getQueueAgents(_0x121e21,_0x52723b,_0x443f52);});return Promise[_0x0a90('0x9a')](_0x2a9fd3);})[_0x0a90('0x16')](function(_0x44b7b1){_0x2c5b3a['rows']=_0x44b7b1;return _0x2c5b3a;})[_0x0a90('0x16')](respondWithFilteredResult(_0x2c2103,_0x3f6ae0))[_0x0a90('0x1e')](handleError(_0x2c2103,null));};