7230b80d290f1cb03c4c5ad3522ccb3f078fb099
[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 _0x5fc3=['user','sort','getResources','findAndCountAll','rows','omit','globalStatus','globalStatusFilter','pauseTypeFilter','pauseType','getQueues','mail','chat','openchannel','whatsapp','channel','includes','toLowerCase','Invalid\x20channel','userProfileId','QueueCampaigns','UserProfile','all','util','lodash','../../components/parsers/qs','../../config/logger','api','../../mysqldb','client','http','undefined','limit','count','offset','status','Content-Range','json','stack','name','send','then','error','Unable\x20to\x20retrieve\x20RPC\x20agents\x20(AMI)','result','Agents','Unable\x20to\x20retrieve\x20RPC\x20agents\x20(Routing)','merge','catch','voice','request','startCase','Queues','format','Unable\x20to\x20retrieve\x20RPC\x20%s\x20queues','loggedIn','available','talking','paused','outbound','push','pTalking','originated','message','dialMethod','Trunk','startPredictive','erlangCalls','erlangCallToSecond','erlangAbandonmentRate','erlangBusyFactor','dialPredictiveInterval','dialPredictiveIntervalMaxThreshold','dialPredictiveIntervalMinThreshold','predictiveIntervalAvailable','predictiveIntervalTotalCalls','predictiveIntervalAnsweredCalls','predictiveIntervalAvgHoldtime','map','get','agents','waiting','busy','length','some','internal','toString','getMembers','UserId','accountcode','voiceStatus','online','loggedInDb','Queue','Pause','find','idle','unknown','unavailable','isNil','chatPause','faxPause','mailPause','openchannelPause','smsPause','voicePause','whatsappPause','filter','pause','every','chatStatus','faxStatus','smsStatus','whatsappStatus','getAgents','userpic','lastLoginAt','lastPauseAt','address','openchannelCapacity','mailCapacity','smsCapacity','faxCapacity','chatCurrentCapacity','mailCurrentCapacity','smsCurrentCapacity','whatsappCurrentCapacity','faxCurrentCapacity','openchannelStatus','mailStatus','chatStatusTime','mailStatusTime','voiceStatusTime','queue','destconnectedlinenum','screenrecording','phoneBarEnableScreenRecordingByAgent','resolve','User','rawAttributes','fieldName','type','query','agent','getOptions'];(function(_0x594533,_0x2f4541){var _0x455ac8=function(_0x2c33f1){while(--_0x2c33f1){_0x594533['push'](_0x594533['shift']());}};_0x455ac8(++_0x2f4541);}(_0x5fc3,0xa8));var _0x35fc=function(_0xed5f9c,_0x5a25cd){_0xed5f9c=_0xed5f9c-0x0;var _0x3a1c43=_0x5fc3[_0xed5f9c];return _0x3a1c43;};'use strict';var util=require(_0x35fc('0x0'));var _=require(_0x35fc('0x1'));var jayson=require('jayson/promise');var qs=require(_0x35fc('0x2'));var logger=require(_0x35fc('0x3'))(_0x35fc('0x4'));var db=require(_0x35fc('0x5'))['db'];var amiClient=jayson[_0x35fc('0x6')]['http']({'port':0x232a});var routingClient=jayson[_0x35fc('0x6')][_0x35fc('0x7')]({'port':0x232c});function respondWithFilteredResult(_0x186a06,_0x5b85ad){return function(_0x5754be){if(_0x5754be){var _0x19a0d1=typeof _0x5b85ad['offset']===_0x35fc('0x8')&&typeof _0x5b85ad[_0x35fc('0x9')]===_0x35fc('0x8');var _0x1e5f90=_0x5754be[_0x35fc('0xa')];var _0xf9d3a6=_0x19a0d1?0x0:_0x5b85ad[_0x35fc('0xb')];var _0x565f95=_0x19a0d1?_0x5754be[_0x35fc('0xa')]:_0x5b85ad[_0x35fc('0xb')]+_0x5b85ad['limit'];var _0x471549;if(_0x565f95>=_0x1e5f90){_0x565f95=_0x1e5f90;_0x471549=0xc8;}else{_0x471549=0xce;}_0x186a06[_0x35fc('0xc')](_0x471549);return _0x186a06['set'](_0x35fc('0xd'),_0xf9d3a6+'-'+_0x565f95+'/'+_0x1e5f90)[_0x35fc('0xe')](_0x5754be);}return null;};}function handleError(_0x5469f5,_0x4cbe74){_0x4cbe74=_0x4cbe74||0x1f4;return function(_0x409357){logger['error'](_0x409357[_0x35fc('0xf')]);if(_0x409357[_0x35fc('0x10')]){delete _0x409357[_0x35fc('0x10')];}_0x5469f5[_0x35fc('0xc')](_0x4cbe74)[_0x35fc('0x11')](_0x409357);};}function getRealtimeAgents(){return new Promise(function(_0x5cfe94,_0x59507a){return amiClient['request']('Agents',{})[_0x35fc('0x12')](function(_0x229d56){if(_0x229d56[_0x35fc('0x13')])throw new Error(_0x35fc('0x14'));var _0x227a49=_0x229d56[_0x35fc('0x15')];return routingClient['request'](_0x35fc('0x16'),{})[_0x35fc('0x12')](function(_0x553677){if(_0x553677[_0x35fc('0x13')])throw new Error(_0x35fc('0x17'));var _0x2e513b=_0x553677['result'];return _0x5cfe94(_[_0x35fc('0x18')]({},_0x227a49,_0x2e513b));});})[_0x35fc('0x19')](function(_0x465eb3){_0x59507a(_0x465eb3);});});}function getRealtimeQueues(_0x488234,_0x402cff){return new Promise(function(_0x350a27,_0x3229d1){var _0x251a1f=_0x402cff===_0x35fc('0x1a')?amiClient:routingClient;return _0x251a1f[_0x35fc('0x1b')](_[_0x35fc('0x1c')](_0x402cff)+_0x35fc('0x1d'),_0x488234)[_0x35fc('0x12')](function(_0x427eae){if(_0x427eae['error'])throw new Error(util[_0x35fc('0x1e')](_0x35fc('0x1f'),_0x402cff));return _0x350a27(_0x427eae[_0x35fc('0x15')]);})[_0x35fc('0x19')](function(_0x3b4743){_0x3229d1(_0x3b4743);});});}function getRealtimeVoiceQueueChannels(){return new Promise(function(_0x55e9e7,_0x33f5e8){return amiClient[_0x35fc('0x1b')]('VoiceQueuesChannels',{})[_0x35fc('0x12')](function(_0x5a93df){if(_0x5a93df[_0x35fc('0x13')])throw new Error('Unable\x20to\x20retrieve\x20voice\x20queues\x20channels');return _0x55e9e7(_0x5a93df[_0x35fc('0x15')]);})['catch'](function(_0x1bf180){_0x33f5e8(_0x1bf180);});});}function initializeQueues(_0xdfab9f,_0x5cb986,_0x519264,_0x47774c){var _0x58edb6=[_0x35fc('0x20'),_0x35fc('0x21'),'waiting',_0x35fc('0x22'),_0x35fc('0x23')];if(_0x519264===_0x35fc('0x1a')&&_0x47774c===_0x35fc('0x24')){_0x58edb6[_0x35fc('0x25')](_0x35fc('0x26'),_0x35fc('0x27'),_0x35fc('0x28'),'dialActive',_0x35fc('0x29'),_0x35fc('0x2a'),_0x35fc('0x2b'),'startProgressive',_0x35fc('0x2c'),_0x35fc('0x2d'),_0x35fc('0x2e'),_0x35fc('0x2f'),_0x35fc('0x30'),_0x35fc('0x31'),_0x35fc('0x32'),_0x35fc('0x33'),_0x35fc('0x34'),_0x35fc('0x35'),'predictiveIntervalDroppedCallsTimeout','predictiveIntervalDroppedCallsCallersExit',_0x35fc('0x36'),'predictiveIntervalAvgTalktime');}return _0xdfab9f[_0x35fc('0x37')](function(_0x39454e){var _0x1c4b8b=_0x39454e[_0x35fc('0x38')]({'plain':!![]});_0x1c4b8b[_0x35fc('0x39')]=[];_0x1c4b8b[_0x35fc('0x23')]=0x0;_0x1c4b8b['loggedInDb']=0x0;_0x1c4b8b[_0x35fc('0x20')]=0x0;_0x1c4b8b[_0x35fc('0x21')]=0x0;_0x1c4b8b[_0x35fc('0x3a')]=0x0;if(_0x519264===_0x35fc('0x1a')){_0x1c4b8b[_0x35fc('0x22')]=0x0;_0x1c4b8b[_0x35fc('0x3b')]=0x0;if(_0x47774c==='outbound'){_0x1c4b8b[_0x35fc('0x26')]=0x0;_0x1c4b8b[_0x35fc('0x27')]=0x0;}}var _0x1fecdf=_['find'](_0x5cb986,['id',_0x1c4b8b['id']]);if(_0x1fecdf)_[_0x35fc('0x18')](_0x1c4b8b,_['pick'](_0x1fecdf,_0x58edb6));return _0x1c4b8b;});}function updateAgentVoiceStatus(_0x2ecb2e,_0x33a377,_0x266a11){var _0x2e00e3=_['filter'](_0x266a11,['queue',_0x33a377['name']]);if(_0x2e00e3[_0x35fc('0x3c')]===0x0)return _0x2ecb2e['busy']=!![];if(_[_0x35fc('0x3d')](_0x2e00e3,['connectedlinenum',_0x2ecb2e[_0x35fc('0x3e')][_0x35fc('0x3f')]()]))return _0x2ecb2e[_0x35fc('0x22')]=!![];if(!_0x2ecb2e[_0x35fc('0x22')])return _0x2ecb2e[_0x35fc('0x3b')]=!![];}function getVoiceQueueAgents(_0x3313fa,_0x9fe7d6,_0x4ec0dc,_0x4467f8){var _0x16087d=_['find'](_0x9fe7d6,['id',_0x3313fa['id']]);if(!_0x16087d)return _0x3313fa;return _0x16087d[_0x35fc('0x40')]({'raw':!![]})[_0x35fc('0x12')](function(_0x1aac67){for(var _0x1d2ffe=0x0;_0x1d2ffe<_0x1aac67[_0x35fc('0x3c')];_0x1d2ffe++){var _0x31d926=_['find'](_0x4ec0dc,['id',_0x1aac67[_0x1d2ffe][_0x35fc('0x41')]]);if(!_0x31d926){_0x31d926={'id':_0x1aac67[_0x1d2ffe][_0x35fc('0x41')],'name':_0x1aac67[_0x1d2ffe][_0x35fc('0x41')],'accountcode':'','online':![]};}var _0x52543b={'id':_0x31d926['id'],'name':_0x31d926[_0x35fc('0x10')],'internal':_0x31d926[_0x35fc('0x42')],'online':Boolean(_0x31d926['online']),'paused':Boolean(_0x1aac67[_0x1d2ffe][_0x35fc('0x23')])};if(_0x31d926[_0x35fc('0x43')]===_0x35fc('0x22'))updateAgentVoiceStatus(_0x52543b,_0x3313fa,_0x4467f8);if(_0x52543b[_0x35fc('0x44')])_0x3313fa[_0x35fc('0x45')]++;if(_0x52543b[_0x35fc('0x3b')])_0x3313fa[_0x35fc('0x3b')]++;if(_0x52543b[_0x35fc('0x23')]&&!_0x52543b['busy']&&!_0x52543b['talking'])_0x3313fa[_0x35fc('0x23')]++;_0x3313fa[_0x35fc('0x39')]['push'](_0x52543b);}return _0x3313fa;});}function getQueueAgents(_0x5c2e5c,_0x4ff202,_0x79677d){var _0x10f98b='User'+_[_0x35fc('0x1c')](_0x79677d)+_0x35fc('0x46');var _0x1fb0a6=_0x79677d+_0x35fc('0x47');return db[_0x10f98b]['findAll']({'where':{[_['startCase'](_0x79677d)+'QueueId']:_0x5c2e5c['id']},'raw':!![]})['then'](function(_0x56bdb9){for(var _0xb26e3f=0x0;_0xb26e3f<_0x56bdb9[_0x35fc('0x3c')];_0xb26e3f++){var _0x4fc5bc=_[_0x35fc('0x48')](_0x4ff202,['id',_0x56bdb9[_0xb26e3f]['UserId']]);if(!_0x4fc5bc){_0x4fc5bc={'id':_0x56bdb9[_0xb26e3f][_0x35fc('0x41')],'name':_0x56bdb9[_0xb26e3f][_0x35fc('0x41')],'online':![],['pauseField']:![]};}var _0x350237={'id':_0x4fc5bc['id'],'name':_0x4fc5bc[_0x35fc('0x10')],'online':Boolean(_0x4fc5bc[_0x35fc('0x44')]),'paused':Boolean(_0x4fc5bc[_0x1fb0a6])};if(_0x350237['online'])_0x5c2e5c[_0x35fc('0x45')]++;_0x5c2e5c[_0x35fc('0x39')]['push'](_0x350237);}return _0x5c2e5c;});}function isIdle(_0x18181b){return _0x18181b===_0x35fc('0x49')||_0x18181b===_0x35fc('0x4a')||_0x18181b===_0x35fc('0x4b')||_0x18181b==='ringing'||_[_0x35fc('0x4c')](_0x18181b);}function isAgentPaused(_0x4c82d8,_0x583f67){var _0x1b6160=[_0x4c82d8[_0x35fc('0x4d')],_0x4c82d8[_0x35fc('0x4e')],_0x4c82d8[_0x35fc('0x4f')],_0x4c82d8[_0x35fc('0x50')],_0x4c82d8[_0x35fc('0x51')],_0x4c82d8[_0x35fc('0x52')],_0x4c82d8[_0x35fc('0x53')]];if(!_0x583f67)return _['every'](_0x1b6160);var _0x3e2961=_[_0x35fc('0x54')](_0x1b6160);return _0x3e2961[_0x35fc('0x3c')]>0x0&&_0x3e2961[_0x35fc('0x3c')]<_0x1b6160[_0x35fc('0x3c')]?!![]:![];}function isAgentIdle(_0x1e3221){if(_0x1e3221[_0x35fc('0x43')]!==_0x35fc('0x49')&&_0x1e3221[_0x35fc('0x52')]){_0x1e3221[_0x35fc('0x43')]=_0x35fc('0x55');return![];}return _[_0x35fc('0x56')]([_0x1e3221[_0x35fc('0x57')],_0x1e3221[_0x35fc('0x58')],_0x1e3221['mailStatus'],_0x1e3221['openchannelStatus'],_0x1e3221[_0x35fc('0x59')],_0x1e3221[_0x35fc('0x43')],_0x1e3221[_0x35fc('0x5a')]],isIdle);}function getAgentStatus(_0x2bf0b0){var _0x3ee296=_0x35fc('0x4a');if(isAgentPaused(_0x2bf0b0))_0x3ee296='pause';else if(isAgentPaused(_0x2bf0b0,!![]))_0x3ee296='*pause';else if(isAgentIdle(_0x2bf0b0))_0x3ee296=_0x35fc('0x49');else _0x3ee296=_0x35fc('0x3b');return _0x3ee296;}exports[_0x35fc('0x5b')]=function(_0x38bc78,_0x5b2595){var _0x467980={'count':0x0,'rows':[]},_0x13281f={},_0x4965d2,_0x199a00,_0x2536d3=[_0x35fc('0x5c'),_0x35fc('0x44'),_0x35fc('0x5d'),_0x35fc('0x5e'),'pauseType','lastPauseAt','internal',_0x35fc('0x5f'),_0x35fc('0x4d'),'openchannelPause',_0x35fc('0x4f'),_0x35fc('0x51'),_0x35fc('0x53'),_0x35fc('0x4e'),_0x35fc('0x52'),'chatCapacity',_0x35fc('0x60'),_0x35fc('0x61'),_0x35fc('0x62'),'whatsappCapacity',_0x35fc('0x63'),_0x35fc('0x64'),'openchannelCurrentCapacity',_0x35fc('0x65'),_0x35fc('0x66'),_0x35fc('0x67'),_0x35fc('0x68'),_0x35fc('0x57'),_0x35fc('0x69'),_0x35fc('0x6a'),_0x35fc('0x59'),'whatsappStatus',_0x35fc('0x58'),_0x35fc('0x43'),_0x35fc('0x6b'),'openchannelStatusTime',_0x35fc('0x6c'),'smsStatusTime','whatsappStatusTime','faxStatusTime',_0x35fc('0x6d'),_0x35fc('0x6e'),_0x35fc('0x6f'),_0x35fc('0x70'),_0x35fc('0x71'),'phoneBarEnableAutomaticScreenRecording','routealias'],_0x590faa=['chatPause','openchannelPause','mailPause',_0x35fc('0x51'),_0x35fc('0x53'),_0x35fc('0x4e'),_0x35fc('0x52'),'online'];Promise[_0x35fc('0x72')]()[_0x35fc('0x12')](function(){var _0x390e16=_[_0x35fc('0x37')](db[_0x35fc('0x73')][_0x35fc('0x74')],function(_0x27071b){return{'name':_0x27071b[_0x35fc('0x75')],'type':_0x27071b[_0x35fc('0x76')]['key']};});_0x38bc78[_0x35fc('0x77')]['role']=_0x35fc('0x78');_0x38bc78[_0x35fc('0x77')][_0x35fc('0x44')]=!![];_0x13281f=qs[_0x35fc('0x79')](_0x390e16,_0x38bc78);_0x13281f['raw']=!![];if(_0x38bc78[_0x35fc('0x7a')]['role']==='user'){var _0x5f1d4a={'params':{'id':_0x38bc78[_0x35fc('0x7a')]['userProfileId']},'query':{'section':_0x35fc('0x16'),'role':'agent','online':!![],'nolimit':_0x38bc78[_0x35fc('0x77')]['nolimit'],'sort':_0x38bc78[_0x35fc('0x77')][_0x35fc('0x7b')],'filter':_0x38bc78[_0x35fc('0x77')][_0x35fc('0x54')],'raw':!![]}};return db['UserProfile'][_0x35fc('0x7c')](_0x5f1d4a);}return db[_0x35fc('0x73')][_0x35fc('0x7d')](_0x13281f);})['then'](function(_0x3aa7fa){_0x467980[_0x35fc('0xa')]=_0x3aa7fa[_0x35fc('0xa')];_0x4965d2=_0x3aa7fa[_0x35fc('0x7e')];return getRealtimeAgents();})[_0x35fc('0x12')](function(_0x2c4309){_0x199a00=_0x2c4309[_0x35fc('0x7e')];return _0x4965d2[_0x35fc('0x37')](function(_0x5159a0){var _0x28a44b=_[_0x35fc('0x48')](_0x199a00,['id',_0x5159a0['id']]);if(_0x28a44b)_[_0x35fc('0x18')](_0x5159a0,_['pick'](_[_0x35fc('0x7f')](_0x28a44b,_0x590faa),_0x2536d3));_0x5159a0[_0x35fc('0x80')]=getAgentStatus(_0x5159a0);return _0x5159a0;});})['then'](function(_0x4d09d4){if(_0x38bc78[_0x35fc('0x77')]['globalStatusFilter']){var _0x223ad3=_[_0x35fc('0x54')](_0x4d09d4,[_0x35fc('0x80'),_0x38bc78[_0x35fc('0x77')][_0x35fc('0x81')]]);if(_0x38bc78[_0x35fc('0x77')][_0x35fc('0x82')]){_0x223ad3=_[_0x35fc('0x54')](_0x223ad3,[_0x35fc('0x83'),_0x38bc78[_0x35fc('0x77')][_0x35fc('0x82')]]);}}_0x467980[_0x35fc('0x7e')]=_0x223ad3||_0x4d09d4;return _0x467980;})[_0x35fc('0x12')](respondWithFilteredResult(_0x5b2595,_0x13281f))[_0x35fc('0x19')](handleError(_0x5b2595,null));};exports[_0x35fc('0x84')]=function(_0xd1e7ed,_0x2d8219){var _0x2d7b16=[_0x35fc('0x85'),_0x35fc('0x86'),'sms',_0x35fc('0x87'),'fax',_0x35fc('0x88'),_0x35fc('0x1a')];var _0x2185ce={'count':0x0,'rows':[]},_0x516b26={},_0x25af78,_0x3f56f4,_0x2913ba,_0x32023a,_0x2fed7b;Promise[_0x35fc('0x72')]()[_0x35fc('0x12')](function(){if(_0xd1e7ed['query'][_0x35fc('0x89')]){if(!_[_0x35fc('0x8a')](_0x2d7b16,_0xd1e7ed[_0x35fc('0x77')][_0x35fc('0x89')][_0x35fc('0x8b')]()))throw new Error(_0x35fc('0x8c'));_0x25af78=_0xd1e7ed['query'][_0x35fc('0x89')][_0x35fc('0x8b')]();}var _0x277514=_[_0x35fc('0x1c')](_0x25af78)+_0x35fc('0x46');var _0x3e2a8e=_[_0x35fc('0x37')](db[_0x277514][_0x35fc('0x74')],function(_0xa92234){return{'name':_0xa92234['fieldName'],'type':_0xa92234[_0x35fc('0x76')]['key']};});_0x516b26=qs[_0x35fc('0x79')](_0x3e2a8e,_0xd1e7ed);if(_0xd1e7ed[_0x35fc('0x7a')]['role']===_0x35fc('0x7a')){var _0x41002a={'params':{'id':_0xd1e7ed[_0x35fc('0x7a')][_0x35fc('0x8d')]},'query':{'section':_0x25af78===_0x35fc('0x1a')&&_0xd1e7ed[_0x35fc('0x77')][_0x35fc('0x76')]===_0x35fc('0x24')?_0x35fc('0x8e'):_['startCase'](_0x25af78)+'Queues','type':_0xd1e7ed['query']['type'],'nolimit':!![],'sort':_0x35fc('0x10'),'filter':_0xd1e7ed[_0x35fc('0x77')][_0x35fc('0x54')]}};return db[_0x35fc('0x8f')][_0x35fc('0x7c')](_0x41002a);}return db[_0x277514]['findAndCountAll'](_0x516b26);})[_0x35fc('0x12')](function(_0x19682e){_0x2185ce[_0x35fc('0xa')]=_0x19682e[_0x35fc('0xa')];_0x3f56f4=_0x19682e['rows'];return getRealtimeQueues(_0xd1e7ed,_0x25af78);})[_0x35fc('0x12')](function(_0x3ddd21){_0x32023a=_0x3ddd21[_0x35fc('0x7e')];return getRealtimeAgents();})['then'](function(_0x314886){_0x2913ba=_0x314886[_0x35fc('0x7e')];if(_0x25af78!=_0x35fc('0x1a'))return;return getRealtimeVoiceQueueChannels();})[_0x35fc('0x12')](function(_0x99c2a7){_0x2fed7b=_0x99c2a7?_0x99c2a7[_0x35fc('0x7e')]:[];return initializeQueues(_0x3f56f4,_0x32023a,_0x25af78,_0xd1e7ed[_0x35fc('0x77')][_0x35fc('0x76')]);})['then'](function(_0x2faff7){var _0xa2c5f8=_0x2faff7[_0x35fc('0x37')](function(_0x2799c8){if(_0x25af78===_0x35fc('0x1a'))return getVoiceQueueAgents(_0x2799c8,_0x3f56f4,_0x2913ba,_0x2fed7b);else return getQueueAgents(_0x2799c8,_0x2913ba,_0x25af78);});return Promise[_0x35fc('0x90')](_0xa2c5f8);})['then'](function(_0x495cf6){_0x2185ce['rows']=_0x495cf6;return _0x2185ce;})['then'](respondWithFilteredResult(_0x2d8219,_0x516b26))[_0x35fc('0x19')](handleError(_0x2d8219,null));};