eebae174f955a139535ea739ac440c65c04faa49
[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 _0x4975=['undefined','limit','count','status','error','stack','name','send','request','then','Unable\x20to\x20retrieve\x20RPC\x20agents\x20(AMI)','result','Unable\x20to\x20retrieve\x20RPC\x20agents\x20(Routing)','merge','catch','voice','Queues','format','Unable\x20to\x20retrieve\x20RPC\x20%s\x20queues','VoiceQueuesChannels','talking','outbound','push','pTalking','dialMethod','Trunk','startPredictive','erlangAbandonmentRate','dialPredictiveInterval','dialPredictiveIntervalMaxThreshold','dialPredictiveIntervalMinThreshold','predictiveIntervalTotalCalls','predictiveIntervalAnsweredCalls','predictiveIntervalDroppedCallsCallersExit','predictiveIntervalAvgHoldtime','predictiveIntervalAvgTalktime','get','agents','paused','loggedIn','available','busy','originated','find','pick','filter','queue','length','some','internal','toString','getMembers','UserId','accountcode','online','voiceStatus','loggedInDb','User','startCase','Pause','idle','unknown','unavailable','isNil','chatPause','mailPause','openchannelPause','smsPause','whatsappPause','every','voicePause','pause','chatStatus','mailStatus','whatsappStatus','*pause','userpic','lastLoginAt','pauseType','lastPauseAt','address','faxPause','chatCapacity','openchannelCapacity','mailCapacity','smsCapacity','faxCapacity','smsCurrentCapacity','whatsappCurrentCapacity','faxCurrentCapacity','openchannelStatus','faxStatus','chatStatusTime','openchannelStatusTime','mailStatusTime','whatsappStatusTime','faxStatusTime','voiceStatusTime','screenrecording','phoneBarEnableScreenRecordingByAgent','phoneBarEnableAutomaticScreenRecording','routealias','map','rawAttributes','fieldName','type','query','role','agent','getOptions','raw','user','userProfileId','Agents','UserProfile','findAndCountAll','rows','omit','globalStatusFilter','globalStatus','pauseTypeFilter','mail','chat','sms','openchannel','fax','resolve','channel','includes','toLowerCase','key','QueueCampaigns','getResources','all','util','jayson/promise','../../components/parsers/qs','../../config/logger','api','../../mysqldb','client','http','offset'];(function(_0x30197d,_0x23e443){var _0x3d8b5b=function(_0x398c54){while(--_0x398c54){_0x30197d['push'](_0x30197d['shift']());}};_0x3d8b5b(++_0x23e443);}(_0x4975,0x86));var _0x5497=function(_0x443835,_0x2b20fd){_0x443835=_0x443835-0x0;var _0x3cd51f=_0x4975[_0x443835];return _0x3cd51f;};'use strict';var util=require(_0x5497('0x0'));var _=require('lodash');var jayson=require(_0x5497('0x1'));var qs=require(_0x5497('0x2'));var logger=require(_0x5497('0x3'))(_0x5497('0x4'));var db=require(_0x5497('0x5'))['db'];var amiClient=jayson[_0x5497('0x6')][_0x5497('0x7')]({'port':0x232a});var routingClient=jayson[_0x5497('0x6')][_0x5497('0x7')]({'port':0x232c});function respondWithFilteredResult(_0x5d5272,_0x539a27){return function(_0x37e48d){if(_0x37e48d){var _0x4f563d=typeof _0x539a27[_0x5497('0x8')]===_0x5497('0x9')&&typeof _0x539a27[_0x5497('0xa')]==='undefined';var _0x42af11=_0x37e48d['count'];var _0x1f63d8=_0x4f563d?0x0:_0x539a27[_0x5497('0x8')];var _0x2ec5a4=_0x4f563d?_0x37e48d[_0x5497('0xb')]:_0x539a27[_0x5497('0x8')]+_0x539a27[_0x5497('0xa')];var _0x3e7886;if(_0x2ec5a4>=_0x42af11){_0x2ec5a4=_0x42af11;_0x3e7886=0xc8;}else{_0x3e7886=0xce;}_0x5d5272[_0x5497('0xc')](_0x3e7886);return _0x5d5272['set']('Content-Range',_0x1f63d8+'-'+_0x2ec5a4+'/'+_0x42af11)['json'](_0x37e48d);}return null;};}function handleError(_0xc250bb,_0x1b23df){_0x1b23df=_0x1b23df||0x1f4;return function(_0x1bb794){logger[_0x5497('0xd')](_0x1bb794[_0x5497('0xe')]);if(_0x1bb794[_0x5497('0xf')]){delete _0x1bb794[_0x5497('0xf')];}_0xc250bb[_0x5497('0xc')](_0x1b23df)[_0x5497('0x10')](_0x1bb794);};}function getRealtimeAgents(){return new Promise(function(_0x1a4c74,_0x55f590){return amiClient[_0x5497('0x11')]('Agents',{})[_0x5497('0x12')](function(_0x2bfd8a){if(_0x2bfd8a[_0x5497('0xd')])throw new Error(_0x5497('0x13'));var _0x37c18e=_0x2bfd8a[_0x5497('0x14')];return routingClient[_0x5497('0x11')]('Agents',{})['then'](function(_0x3f4d20){if(_0x3f4d20[_0x5497('0xd')])throw new Error(_0x5497('0x15'));var _0x5d9e24=_0x3f4d20[_0x5497('0x14')];return _0x1a4c74(_[_0x5497('0x16')]({},_0x37c18e,_0x5d9e24));});})[_0x5497('0x17')](function(_0x43da2c){_0x55f590(_0x43da2c);});});}function getRealtimeQueues(_0x1b1b43,_0x1f6f18){return new Promise(function(_0x16d378,_0x10226f){var _0x2e3960=_0x1f6f18===_0x5497('0x18')?amiClient:routingClient;return _0x2e3960[_0x5497('0x11')](_['startCase'](_0x1f6f18)+_0x5497('0x19'),_0x1b1b43)['then'](function(_0x19200a){if(_0x19200a[_0x5497('0xd')])throw new Error(util[_0x5497('0x1a')](_0x5497('0x1b'),_0x1f6f18));return _0x16d378(_0x19200a[_0x5497('0x14')]);})[_0x5497('0x17')](function(_0x5018fa){_0x10226f(_0x5018fa);});});}function getRealtimeVoiceQueueChannels(){return new Promise(function(_0x54043a,_0x1eefce){return amiClient[_0x5497('0x11')](_0x5497('0x1c'),{})[_0x5497('0x12')](function(_0x5223b5){if(_0x5223b5[_0x5497('0xd')])throw new Error('Unable\x20to\x20retrieve\x20voice\x20queues\x20channels');return _0x54043a(_0x5223b5[_0x5497('0x14')]);})[_0x5497('0x17')](function(_0x429d36){_0x1eefce(_0x429d36);});});}function initializeQueues(_0x34a1a5,_0x163a30,_0x31cd01,_0x48b835){var _0x4af5f4=['loggedIn','available','waiting',_0x5497('0x1d'),'paused'];if(_0x31cd01==='voice'&&_0x48b835===_0x5497('0x1e')){_0x4af5f4[_0x5497('0x1f')](_0x5497('0x20'),'originated','message','dialActive',_0x5497('0x21'),_0x5497('0x22'),_0x5497('0x23'),'startProgressive','erlangCalls','erlangCallToSecond',_0x5497('0x24'),'erlangBusyFactor',_0x5497('0x25'),_0x5497('0x26'),_0x5497('0x27'),'predictiveIntervalAvailable',_0x5497('0x28'),_0x5497('0x29'),'predictiveIntervalDroppedCallsTimeout',_0x5497('0x2a'),_0x5497('0x2b'),_0x5497('0x2c'));}return _0x34a1a5['map'](function(_0x3395f6){var _0x2fe1f2=_0x3395f6[_0x5497('0x2d')]({'plain':!![]});_0x2fe1f2[_0x5497('0x2e')]=[];_0x2fe1f2[_0x5497('0x2f')]=0x0;_0x2fe1f2['loggedInDb']=0x0;_0x2fe1f2[_0x5497('0x30')]=0x0;_0x2fe1f2[_0x5497('0x31')]=0x0;_0x2fe1f2['waiting']=0x0;if(_0x31cd01===_0x5497('0x18')){_0x2fe1f2[_0x5497('0x1d')]=0x0;_0x2fe1f2[_0x5497('0x32')]=0x0;if(_0x48b835===_0x5497('0x1e')){_0x2fe1f2[_0x5497('0x20')]=0x0;_0x2fe1f2[_0x5497('0x33')]=0x0;}}var _0x580e86=_[_0x5497('0x34')](_0x163a30,['id',_0x2fe1f2['id']]);if(_0x580e86)_['merge'](_0x2fe1f2,_[_0x5497('0x35')](_0x580e86,_0x4af5f4));return _0x2fe1f2;});}function updateAgentVoiceStatus(_0x212199,_0x2751f5,_0x5ab18c){var _0x4ec5f7=_[_0x5497('0x36')](_0x5ab18c,[_0x5497('0x37'),_0x2751f5[_0x5497('0xf')]]);if(_0x4ec5f7[_0x5497('0x38')]===0x0)return _0x212199[_0x5497('0x32')]=!![];if(_[_0x5497('0x39')](_0x4ec5f7,['connectedlinenum',_0x212199[_0x5497('0x3a')][_0x5497('0x3b')]()]))return _0x212199[_0x5497('0x1d')]=!![];if(!_0x212199[_0x5497('0x1d')])return _0x212199[_0x5497('0x32')]=!![];}function getVoiceQueueAgents(_0x468902,_0x1e3b6a,_0x179635,_0x59627b){var _0x544065=_[_0x5497('0x34')](_0x1e3b6a,['id',_0x468902['id']]);if(!_0x544065)return _0x468902;return _0x544065[_0x5497('0x3c')]({'raw':!![]})[_0x5497('0x12')](function(_0x3c0339){for(var _0x15029d=0x0;_0x15029d<_0x3c0339[_0x5497('0x38')];_0x15029d++){var _0x4299be=_['find'](_0x179635,['id',_0x3c0339[_0x15029d][_0x5497('0x3d')]]);if(!_0x4299be){_0x4299be={'id':_0x3c0339[_0x15029d][_0x5497('0x3d')],'name':_0x3c0339[_0x15029d][_0x5497('0x3d')],'accountcode':'','online':![]};}var _0x35993c={'id':_0x4299be['id'],'name':_0x4299be[_0x5497('0xf')],'internal':_0x4299be[_0x5497('0x3e')],'online':Boolean(_0x4299be[_0x5497('0x3f')]),'paused':Boolean(_0x3c0339[_0x15029d]['paused'])};if(_0x4299be[_0x5497('0x40')]===_0x5497('0x1d'))updateAgentVoiceStatus(_0x35993c,_0x468902,_0x59627b);if(_0x35993c[_0x5497('0x3f')])_0x468902[_0x5497('0x41')]++;if(_0x35993c[_0x5497('0x32')])_0x468902[_0x5497('0x32')]++;if(_0x35993c[_0x5497('0x2f')]&&!_0x35993c[_0x5497('0x32')]&&!_0x35993c[_0x5497('0x1d')])_0x468902[_0x5497('0x2f')]++;_0x468902['agents'][_0x5497('0x1f')](_0x35993c);}return _0x468902;});}function getQueueAgents(_0x4a9fd7,_0x40822a,_0x9fc24){var _0x26ef01=_0x5497('0x42')+_[_0x5497('0x43')](_0x9fc24)+'Queue';var _0x1f7c90=_0x9fc24+_0x5497('0x44');return db[_0x26ef01]['findAll']({'where':{[_[_0x5497('0x43')](_0x9fc24)+'QueueId']:_0x4a9fd7['id']},'raw':!![]})[_0x5497('0x12')](function(_0x4aa6b8){for(var _0x133606=0x0;_0x133606<_0x4aa6b8[_0x5497('0x38')];_0x133606++){var _0x2e8c6a=_[_0x5497('0x34')](_0x40822a,['id',_0x4aa6b8[_0x133606][_0x5497('0x3d')]]);if(!_0x2e8c6a){_0x2e8c6a={'id':_0x4aa6b8[_0x133606]['UserId'],'name':_0x4aa6b8[_0x133606]['UserId'],'online':![],['pauseField']:![]};}var _0x498de5={'id':_0x2e8c6a['id'],'name':_0x2e8c6a['name'],'online':Boolean(_0x2e8c6a[_0x5497('0x3f')]),'paused':Boolean(_0x2e8c6a[_0x1f7c90])};if(_0x498de5[_0x5497('0x3f')])_0x4a9fd7[_0x5497('0x41')]++;_0x4a9fd7[_0x5497('0x2e')][_0x5497('0x1f')](_0x498de5);}return _0x4a9fd7;});}function isIdle(_0x33a733){return _0x33a733===_0x5497('0x45')||_0x33a733===_0x5497('0x46')||_0x33a733===_0x5497('0x47')||_0x33a733==='ringing'||_[_0x5497('0x48')](_0x33a733);}function isAgentPaused(_0x18db5b,_0x1a9138){var _0x52a189=[_0x18db5b[_0x5497('0x49')],_0x18db5b['faxPause'],_0x18db5b[_0x5497('0x4a')],_0x18db5b[_0x5497('0x4b')],_0x18db5b[_0x5497('0x4c')],_0x18db5b['voicePause'],_0x18db5b[_0x5497('0x4d')]];if(!_0x1a9138)return _[_0x5497('0x4e')](_0x52a189);var _0x515c3e=_[_0x5497('0x36')](_0x52a189);return _0x515c3e['length']>0x0&&_0x515c3e[_0x5497('0x38')]<_0x52a189['length']?!![]:![];}function isAgentIdle(_0x416f76){if(_0x416f76[_0x5497('0x40')]!==_0x5497('0x45')&&_0x416f76[_0x5497('0x4f')]){_0x416f76[_0x5497('0x40')]=_0x5497('0x50');return![];}return _[_0x5497('0x4e')]([_0x416f76[_0x5497('0x51')],_0x416f76['faxStatus'],_0x416f76[_0x5497('0x52')],_0x416f76['openchannelStatus'],_0x416f76['smsStatus'],_0x416f76[_0x5497('0x40')],_0x416f76[_0x5497('0x53')]],isIdle);}function getAgentStatus(_0x592120){var _0x2ddccd=_0x5497('0x46');if(isAgentPaused(_0x592120))_0x2ddccd=_0x5497('0x50');else if(isAgentPaused(_0x592120,!![]))_0x2ddccd=_0x5497('0x54');else if(isAgentIdle(_0x592120))_0x2ddccd='idle';else _0x2ddccd=_0x5497('0x32');return _0x2ddccd;}exports['getAgents']=function(_0x493dd0,_0xf9ebd3){var _0x33dd07={'count':0x0,'rows':[]},_0x2f7ec8={},_0x49c85a,_0x4b37d2,_0x2e2ac9=[_0x5497('0x55'),'online',_0x5497('0x56'),'lastPauseAt',_0x5497('0x57'),_0x5497('0x58'),'internal',_0x5497('0x59'),'chatPause','openchannelPause',_0x5497('0x4a'),_0x5497('0x4c'),'whatsappPause',_0x5497('0x5a'),_0x5497('0x4f'),_0x5497('0x5b'),_0x5497('0x5c'),_0x5497('0x5d'),_0x5497('0x5e'),'whatsappCapacity',_0x5497('0x5f'),'chatCurrentCapacity','openchannelCurrentCapacity','mailCurrentCapacity',_0x5497('0x60'),_0x5497('0x61'),_0x5497('0x62'),'chatStatus',_0x5497('0x63'),'mailStatus','smsStatus',_0x5497('0x53'),_0x5497('0x64'),_0x5497('0x40'),_0x5497('0x65'),_0x5497('0x66'),_0x5497('0x67'),'smsStatusTime',_0x5497('0x68'),_0x5497('0x69'),_0x5497('0x6a'),_0x5497('0x37'),'destconnectedlinenum',_0x5497('0x6b'),_0x5497('0x6c'),_0x5497('0x6d'),_0x5497('0x6e')],_0x5acedf=['chatPause',_0x5497('0x4b'),_0x5497('0x4a'),_0x5497('0x4c'),'whatsappPause',_0x5497('0x5a'),'voicePause','online'];Promise['resolve']()[_0x5497('0x12')](function(){var _0x1ca4d8=_[_0x5497('0x6f')](db['User'][_0x5497('0x70')],function(_0x1e1b12){return{'name':_0x1e1b12[_0x5497('0x71')],'type':_0x1e1b12[_0x5497('0x72')]['key']};});_0x493dd0[_0x5497('0x73')][_0x5497('0x74')]=_0x5497('0x75');_0x493dd0['query'][_0x5497('0x3f')]=!![];_0x2f7ec8=qs[_0x5497('0x76')](_0x1ca4d8,_0x493dd0);_0x2f7ec8[_0x5497('0x77')]=!![];if(_0x493dd0[_0x5497('0x78')][_0x5497('0x74')]===_0x5497('0x78')){var _0x15c05b={'params':{'id':_0x493dd0['user'][_0x5497('0x79')]},'query':{'section':_0x5497('0x7a'),'role':_0x5497('0x75'),'online':!![],'nolimit':_0x493dd0[_0x5497('0x73')]['nolimit'],'sort':_0x493dd0['query']['sort'],'filter':_0x493dd0[_0x5497('0x73')][_0x5497('0x36')],'raw':!![]}};return db[_0x5497('0x7b')]['getResources'](_0x15c05b);}return db[_0x5497('0x42')][_0x5497('0x7c')](_0x2f7ec8);})[_0x5497('0x12')](function(_0x464df3){_0x33dd07[_0x5497('0xb')]=_0x464df3[_0x5497('0xb')];_0x49c85a=_0x464df3[_0x5497('0x7d')];return getRealtimeAgents();})[_0x5497('0x12')](function(_0x21da3d){_0x4b37d2=_0x21da3d[_0x5497('0x7d')];return _0x49c85a[_0x5497('0x6f')](function(_0x5a8b3a){var _0x15d5a4=_[_0x5497('0x34')](_0x4b37d2,['id',_0x5a8b3a['id']]);if(_0x15d5a4)_[_0x5497('0x16')](_0x5a8b3a,_['pick'](_[_0x5497('0x7e')](_0x15d5a4,_0x5acedf),_0x2e2ac9));_0x5a8b3a['globalStatus']=getAgentStatus(_0x5a8b3a);return _0x5a8b3a;});})[_0x5497('0x12')](function(_0x469e97){if(_0x493dd0['query'][_0x5497('0x7f')]){var _0x2c07bb=_[_0x5497('0x36')](_0x469e97,[_0x5497('0x80'),_0x493dd0['query'][_0x5497('0x7f')]]);if(_0x493dd0[_0x5497('0x73')][_0x5497('0x81')]){_0x2c07bb=_['filter'](_0x2c07bb,[_0x5497('0x57'),_0x493dd0[_0x5497('0x73')]['pauseTypeFilter']]);}}_0x33dd07[_0x5497('0x7d')]=_0x2c07bb||_0x469e97;return _0x33dd07;})[_0x5497('0x12')](respondWithFilteredResult(_0xf9ebd3,_0x2f7ec8))[_0x5497('0x17')](handleError(_0xf9ebd3,null));};exports['getQueues']=function(_0x4dbd4e,_0x3cc968){var _0x3faf97=[_0x5497('0x82'),_0x5497('0x83'),_0x5497('0x84'),_0x5497('0x85'),_0x5497('0x86'),'whatsapp','voice'];var _0x166daa={'count':0x0,'rows':[]},_0x24a160={},_0x540362,_0x15ca59,_0x268722,_0x7415ba,_0x510078;Promise[_0x5497('0x87')]()[_0x5497('0x12')](function(){if(_0x4dbd4e['query'][_0x5497('0x88')]){if(!_[_0x5497('0x89')](_0x3faf97,_0x4dbd4e[_0x5497('0x73')][_0x5497('0x88')][_0x5497('0x8a')]()))throw new Error('Invalid\x20channel');_0x540362=_0x4dbd4e[_0x5497('0x73')][_0x5497('0x88')][_0x5497('0x8a')]();}var _0x23e259=_[_0x5497('0x43')](_0x540362)+'Queue';var _0x394bf3=_[_0x5497('0x6f')](db[_0x23e259][_0x5497('0x70')],function(_0x9e3f4){return{'name':_0x9e3f4['fieldName'],'type':_0x9e3f4[_0x5497('0x72')][_0x5497('0x8b')]};});_0x24a160=qs['getOptions'](_0x394bf3,_0x4dbd4e);if(_0x4dbd4e[_0x5497('0x78')][_0x5497('0x74')]===_0x5497('0x78')){var _0x3a8cf9={'params':{'id':_0x4dbd4e['user']['userProfileId']},'query':{'section':_0x540362==='voice'&&_0x4dbd4e['query']['type']===_0x5497('0x1e')?_0x5497('0x8c'):_[_0x5497('0x43')](_0x540362)+_0x5497('0x19'),'type':_0x4dbd4e[_0x5497('0x73')]['type'],'nolimit':!![],'sort':'name','filter':_0x4dbd4e[_0x5497('0x73')][_0x5497('0x36')]}};return db[_0x5497('0x7b')][_0x5497('0x8d')](_0x3a8cf9);}return db[_0x23e259][_0x5497('0x7c')](_0x24a160);})[_0x5497('0x12')](function(_0x1f31fb){_0x166daa[_0x5497('0xb')]=_0x1f31fb[_0x5497('0xb')];_0x15ca59=_0x1f31fb[_0x5497('0x7d')];return getRealtimeQueues(_0x4dbd4e,_0x540362);})['then'](function(_0x1515f1){_0x7415ba=_0x1515f1[_0x5497('0x7d')];return getRealtimeAgents();})[_0x5497('0x12')](function(_0x25f06e){_0x268722=_0x25f06e[_0x5497('0x7d')];if(_0x540362!=_0x5497('0x18'))return;return getRealtimeVoiceQueueChannels();})['then'](function(_0x1ef7fc){_0x510078=_0x1ef7fc?_0x1ef7fc['rows']:[];return initializeQueues(_0x15ca59,_0x7415ba,_0x540362,_0x4dbd4e[_0x5497('0x73')][_0x5497('0x72')]);})[_0x5497('0x12')](function(_0x5b0f9d){var _0x59960b=_0x5b0f9d[_0x5497('0x6f')](function(_0x27ca0f){if(_0x540362===_0x5497('0x18'))return getVoiceQueueAgents(_0x27ca0f,_0x15ca59,_0x268722,_0x510078);else return getQueueAgents(_0x27ca0f,_0x268722,_0x540362);});return Promise[_0x5497('0x8e')](_0x59960b);})[_0x5497('0x12')](function(_0x1876d2){_0x166daa[_0x5497('0x7d')]=_0x1876d2;return _0x166daa;})[_0x5497('0x12')](respondWithFilteredResult(_0x3cc968,_0x24a160))['catch'](handleError(_0x3cc968,null));};