4fa2b94f3af8f710dd5be0370bf0ae5be784954c
[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 _0x1c01=['type','key','getOptions','user','role','userProfileId','QueueCampaigns','UserProfile','getResources','rows','all','util','jayson/promise','../../config/logger','../../mysqldb','client','offset','undefined','count','limit','status','set','Content-Range','json','error','stack','name','send','request','then','Unable\x20to\x20retrieve\x20RPC\x20agents\x20(AMI)','result','Agents','Unable\x20to\x20retrieve\x20RPC\x20agents\x20(Routing)','merge','catch','voice','Queues','format','Unable\x20to\x20retrieve\x20RPC\x20%s\x20queues','VoiceQueuesChannels','loggedIn','available','waiting','paused','outbound','push','message','dialActive','dialMethod','startPredictive','startProgressive','erlangCalls','erlangCallToSecond','erlangAbandonmentRate','erlangBusyFactor','dialPredictiveInterval','dialPredictiveIntervalMaxThreshold','dialPredictiveIntervalMinThreshold','predictiveIntervalTotalCalls','predictiveIntervalAnsweredCalls','predictiveIntervalDroppedCallsCallersExit','predictiveIntervalAvgTalktime','map','get','loggedInDb','talking','busy','pTalking','originated','filter','queue','some','length','find','UserId','accountcode','online','agents','User','startCase','Queue','findAll','QueueId','mail','chat','openchannel','fax','query','includes','channel','toLowerCase','Invalid\x20channel','rawAttributes'];(function(_0x17d9e3,_0x1f969c){var _0x14192d=function(_0x159745){while(--_0x159745){_0x17d9e3['push'](_0x17d9e3['shift']());}};_0x14192d(++_0x1f969c);}(_0x1c01,0x183));var _0x11c0=function(_0x34c390,_0x16d196){_0x34c390=_0x34c390-0x0;var _0x3de8c7=_0x1c01[_0x34c390];return _0x3de8c7;};'use strict';var util=require(_0x11c0('0x0'));var _=require('lodash');var jayson=require(_0x11c0('0x1'));var qs=require('../../components/parsers/qs');var logger=require(_0x11c0('0x2'))('api');var db=require(_0x11c0('0x3'))['db'];var amiClient=jayson['client']['http']({'port':0x232a});var routingClient=jayson[_0x11c0('0x4')]['http']({'port':0x232c});function respondWithFilteredResult(_0x380b5b,_0x212c53){return function(_0x5bdd7c){if(_0x5bdd7c){var _0x34d59b=typeof _0x212c53[_0x11c0('0x5')]===_0x11c0('0x6')&&typeof _0x212c53['limit']===_0x11c0('0x6');var _0x13d327=_0x5bdd7c[_0x11c0('0x7')];var _0x1af77e=_0x34d59b?0x0:_0x212c53[_0x11c0('0x5')];var _0x3b7a1b=_0x34d59b?_0x5bdd7c['count']:_0x212c53[_0x11c0('0x5')]+_0x212c53[_0x11c0('0x8')];var _0x274f9d;if(_0x3b7a1b>=_0x13d327){_0x3b7a1b=_0x13d327;_0x274f9d=0xc8;}else{_0x274f9d=0xce;}_0x380b5b[_0x11c0('0x9')](_0x274f9d);return _0x380b5b[_0x11c0('0xa')](_0x11c0('0xb'),_0x1af77e+'-'+_0x3b7a1b+'/'+_0x13d327)[_0x11c0('0xc')](_0x5bdd7c);}return null;};}function handleError(_0x547278,_0x2d14d4){_0x2d14d4=_0x2d14d4||0x1f4;return function(_0x3ce3d2){logger[_0x11c0('0xd')](_0x3ce3d2[_0x11c0('0xe')]);if(_0x3ce3d2[_0x11c0('0xf')]){delete _0x3ce3d2['name'];}_0x547278[_0x11c0('0x9')](_0x2d14d4)[_0x11c0('0x10')](_0x3ce3d2);};}function getRealtimeAgents(){return new Promise(function(_0x51f8d9,_0x8f7646){return amiClient[_0x11c0('0x11')]('Agents',{})[_0x11c0('0x12')](function(_0x4ba841){if(_0x4ba841['error'])throw new Error(_0x11c0('0x13'));var _0x5ce412=_0x4ba841[_0x11c0('0x14')];return routingClient[_0x11c0('0x11')](_0x11c0('0x15'),{})[_0x11c0('0x12')](function(_0x57dbb3){if(_0x57dbb3[_0x11c0('0xd')])throw new Error(_0x11c0('0x16'));var _0x24533b=_0x57dbb3[_0x11c0('0x14')];return _0x51f8d9(_[_0x11c0('0x17')]({},_0x5ce412,_0x24533b));});})[_0x11c0('0x18')](function(_0x9459e7){_0x8f7646(_0x9459e7);});});}function getRealtimeQueues(_0x38c445,_0x4a92ab){return new Promise(function(_0x3c8378,_0x37e6cc){var _0x108c26=_0x4a92ab===_0x11c0('0x19')?amiClient:routingClient;return _0x108c26['request'](_['startCase'](_0x4a92ab)+_0x11c0('0x1a'),_0x38c445)['then'](function(_0x851202){if(_0x851202[_0x11c0('0xd')])throw new Error(util[_0x11c0('0x1b')](_0x11c0('0x1c'),_0x4a92ab));return _0x3c8378(_0x851202[_0x11c0('0x14')]);})[_0x11c0('0x18')](function(_0x1b7d13){_0x37e6cc(_0x1b7d13);});});}function getRealtimeVoiceQueueChannels(){return new Promise(function(_0x50c986,_0x5d4bb9){return amiClient['request'](_0x11c0('0x1d'),{})[_0x11c0('0x12')](function(_0x1316d8){if(_0x1316d8[_0x11c0('0xd')])throw new Error('Unable\x20to\x20retrieve\x20voice\x20queues\x20channels');return _0x50c986(_0x1316d8[_0x11c0('0x14')]);})[_0x11c0('0x18')](function(_0x3a627a){_0x5d4bb9(_0x3a627a);});});}function initializeQueues(_0x2700d9,_0x3ff929,_0x3fa703,_0x5c3fe8){var _0x1d5e82=[_0x11c0('0x1e'),_0x11c0('0x1f'),_0x11c0('0x20'),'talking',_0x11c0('0x21')];if(_0x3fa703==='voice'&&_0x5c3fe8===_0x11c0('0x22')){_0x1d5e82[_0x11c0('0x23')]('pTalking','originated',_0x11c0('0x24'),_0x11c0('0x25'),_0x11c0('0x26'),'Trunk',_0x11c0('0x27'),_0x11c0('0x28'),_0x11c0('0x29'),_0x11c0('0x2a'),_0x11c0('0x2b'),_0x11c0('0x2c'),_0x11c0('0x2d'),_0x11c0('0x2e'),_0x11c0('0x2f'),'predictiveIntervalAvailable',_0x11c0('0x30'),_0x11c0('0x31'),'predictiveIntervalDroppedCallsTimeout',_0x11c0('0x32'),'predictiveIntervalAvgHoldtime',_0x11c0('0x33'));}return _0x2700d9[_0x11c0('0x34')](function(_0x4c9827){var _0x1e1a27=_0x4c9827[_0x11c0('0x35')]({'plain':!![]});_0x1e1a27['agents']=[];_0x1e1a27[_0x11c0('0x21')]=0x0;_0x1e1a27[_0x11c0('0x36')]=0x0;_0x1e1a27[_0x11c0('0x1e')]=0x0;_0x1e1a27[_0x11c0('0x1f')]=0x0;_0x1e1a27['waiting']=0x0;if(_0x3fa703===_0x11c0('0x19')){_0x1e1a27[_0x11c0('0x37')]=0x0;_0x1e1a27[_0x11c0('0x38')]=0x0;if(_0x5c3fe8===_0x11c0('0x22')){_0x1e1a27[_0x11c0('0x39')]=0x0;_0x1e1a27[_0x11c0('0x3a')]=0x0;}}var _0x55c7ef=_['find'](_0x3ff929,['id',_0x1e1a27['id']]);if(_0x55c7ef)_['merge'](_0x1e1a27,_['pick'](_0x55c7ef,_0x1d5e82));return _0x1e1a27;});}function updateAgentVoiceStatus(_0x64ec25,_0x415bdb,_0x59d122){var _0x410967=_[_0x11c0('0x3b')](_0x59d122,[_0x11c0('0x3c'),_0x415bdb[_0x11c0('0xf')]]);if(_0x410967['length']===0x0)return _0x64ec25[_0x11c0('0x38')]=!![];if(_[_0x11c0('0x3d')](_0x410967,['connectedlinenum',_0x64ec25['internal']['toString']()]))return _0x64ec25[_0x11c0('0x37')]=!![];if(!_0x64ec25[_0x11c0('0x37')])return _0x64ec25[_0x11c0('0x38')]=!![];}function getVoiceQueueAgents(_0xb36b90,_0x2d186d,_0x4f70e2,_0x156550){var _0x35c2b9=_['find'](_0x2d186d,['id',_0xb36b90['id']]);if(!_0x35c2b9)return _0xb36b90;return _0x35c2b9['getMembers']({'raw':!![]})[_0x11c0('0x12')](function(_0x188cd2){for(var _0x54cf94=0x0;_0x54cf94<_0x188cd2[_0x11c0('0x3e')];_0x54cf94++){var _0x574b1b=_[_0x11c0('0x3f')](_0x4f70e2,['id',_0x188cd2[_0x54cf94][_0x11c0('0x40')]]);if(!_0x574b1b){_0x574b1b={'id':_0x188cd2[_0x54cf94][_0x11c0('0x40')],'name':_0x188cd2[_0x54cf94][_0x11c0('0x40')],'accountcode':'','online':![]};}var _0x542f2b={'id':_0x574b1b['id'],'name':_0x574b1b[_0x11c0('0xf')],'internal':_0x574b1b[_0x11c0('0x41')],'online':Boolean(_0x574b1b[_0x11c0('0x42')]),'paused':Boolean(_0x188cd2[_0x54cf94][_0x11c0('0x21')])};if(_0x574b1b['voiceStatus']===_0x11c0('0x37'))updateAgentVoiceStatus(_0x542f2b,_0xb36b90,_0x156550);if(_0x542f2b[_0x11c0('0x42')])_0xb36b90[_0x11c0('0x36')]++;if(_0x542f2b[_0x11c0('0x38')])_0xb36b90[_0x11c0('0x38')]++;if(_0x542f2b['paused']&&!_0x542f2b['busy']&&!_0x542f2b[_0x11c0('0x37')])_0xb36b90[_0x11c0('0x21')]++;_0xb36b90[_0x11c0('0x43')][_0x11c0('0x23')](_0x542f2b);}return _0xb36b90;});}function getQueueAgents(_0x5f571b,_0xb64d57,_0x5d0b39){var _0x299644=_0x11c0('0x44')+_[_0x11c0('0x45')](_0x5d0b39)+_0x11c0('0x46');var _0xaedee0=_0x5d0b39+'Pause';return db[_0x299644][_0x11c0('0x47')]({'where':{[_[_0x11c0('0x45')](_0x5d0b39)+_0x11c0('0x48')]:_0x5f571b['id']},'raw':!![]})[_0x11c0('0x12')](function(_0x55457f){for(var _0x3eb122=0x0;_0x3eb122<_0x55457f[_0x11c0('0x3e')];_0x3eb122++){var _0x4f543f=_[_0x11c0('0x3f')](_0xb64d57,['id',_0x55457f[_0x3eb122]['UserId']]);if(!_0x4f543f){_0x4f543f={'id':_0x55457f[_0x3eb122][_0x11c0('0x40')],'name':_0x55457f[_0x3eb122][_0x11c0('0x40')],'online':![],['pauseField']:![]};}var _0x1a0d03={'id':_0x4f543f['id'],'name':_0x4f543f[_0x11c0('0xf')],'online':Boolean(_0x4f543f[_0x11c0('0x42')]),'paused':Boolean(_0x4f543f[_0xaedee0])};if(_0x1a0d03['online'])_0x5f571b['loggedInDb']++;_0x5f571b[_0x11c0('0x43')][_0x11c0('0x23')](_0x1a0d03);}return _0x5f571b;});}exports['getQueues']=function(_0x3840da,_0x358328){var _0x370d90=[_0x11c0('0x49'),_0x11c0('0x4a'),'sms',_0x11c0('0x4b'),_0x11c0('0x4c'),'whatsapp',_0x11c0('0x19')];var _0x2ec5f9={'count':0x0,'rows':[]},_0x56dc98={},_0x1a5c0a,_0x2a4c48,_0xee5a44,_0x232d1d,_0x2dcff5;Promise['resolve']()[_0x11c0('0x12')](function(){if(_0x3840da[_0x11c0('0x4d')]['channel']){if(!_[_0x11c0('0x4e')](_0x370d90,_0x3840da[_0x11c0('0x4d')][_0x11c0('0x4f')][_0x11c0('0x50')]()))throw new Error(_0x11c0('0x51'));_0x1a5c0a=_0x3840da['query'][_0x11c0('0x4f')][_0x11c0('0x50')]();}var _0x54deaf=_['startCase'](_0x1a5c0a)+_0x11c0('0x46');var _0x32361f=_[_0x11c0('0x34')](db[_0x54deaf][_0x11c0('0x52')],function(_0x22c59f){return{'name':_0x22c59f['fieldName'],'type':_0x22c59f[_0x11c0('0x53')][_0x11c0('0x54')]};});_0x56dc98=qs[_0x11c0('0x55')](_0x32361f,_0x3840da);if(_0x3840da[_0x11c0('0x56')][_0x11c0('0x57')]===_0x11c0('0x56')){var _0x2532a6={'params':{'id':_0x3840da['user'][_0x11c0('0x58')]},'query':{'section':_0x1a5c0a===_0x11c0('0x19')&&_0x3840da[_0x11c0('0x4d')][_0x11c0('0x53')]===_0x11c0('0x22')?_0x11c0('0x59'):_[_0x11c0('0x45')](_0x1a5c0a)+_0x11c0('0x1a'),'type':_0x3840da[_0x11c0('0x4d')][_0x11c0('0x53')],'nolimit':!![],'sort':'name','filter':_0x3840da[_0x11c0('0x4d')][_0x11c0('0x3b')]}};return db[_0x11c0('0x5a')][_0x11c0('0x5b')](_0x2532a6);}return db[_0x54deaf]['findAndCountAll'](_0x56dc98);})[_0x11c0('0x12')](function(_0x6a44e8){_0x2ec5f9[_0x11c0('0x7')]=_0x6a44e8['count'];_0x2a4c48=_0x6a44e8[_0x11c0('0x5c')];return getRealtimeQueues(_0x3840da,_0x1a5c0a);})[_0x11c0('0x12')](function(_0x26ec43){_0x232d1d=_0x26ec43[_0x11c0('0x5c')];return getRealtimeAgents();})[_0x11c0('0x12')](function(_0x5eca33){_0xee5a44=_0x5eca33[_0x11c0('0x5c')];if(_0x1a5c0a!=_0x11c0('0x19'))return;return getRealtimeVoiceQueueChannels();})['then'](function(_0x2a3817){_0x2dcff5=_0x2a3817?_0x2a3817['rows']:[];return initializeQueues(_0x2a4c48,_0x232d1d,_0x1a5c0a,_0x3840da[_0x11c0('0x4d')][_0x11c0('0x53')]);})[_0x11c0('0x12')](function(_0x5d02ff){var _0x45894d=_0x5d02ff[_0x11c0('0x34')](function(_0x30d2e0){if(_0x1a5c0a===_0x11c0('0x19'))return getVoiceQueueAgents(_0x30d2e0,_0x2a4c48,_0xee5a44,_0x2dcff5);else return getQueueAgents(_0x30d2e0,_0xee5a44,_0x1a5c0a);});return Promise[_0x11c0('0x5d')](_0x45894d);})[_0x11c0('0x12')](function(_0x137834){_0x2ec5f9[_0x11c0('0x5c')]=_0x137834;return _0x2ec5f9;})[_0x11c0('0x12')](respondWithFilteredResult(_0x358328,_0x56dc98))[_0x11c0('0x18')](handleError(_0x358328,null));};