Built motion from commit 9e1399f3.|2.5.37
[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 _0x2b51=['outbound','pTalking','find','queue','length','some','connectedlinenum','internal','toString','talking','getMembers','UserId','online','voiceStatus','loggedInDb','push','User','Queue','Pause','idle','unknown','ringing','isNil','chatPause','faxPause','whatsappPause','filter','every','chatStatus','faxStatus','openchannelStatus','smsStatus','whatsappStatus','*pause','userpic','lastLoginAt','lastPauseAt','pauseType','address','openchannelPause','mailPause','voicePause','mailCapacity','smsCapacity','faxCapacity','chatCurrentCapacity','faxCurrentCapacity','mailStatus','chatStatusTime','openchannelStatusTime','smsStatusTime','whatsappStatusTime','voiceStatusTime','destconnectedlinenum','screenrecording','phoneBarEnableScreenRecordingByAgent','phoneBarEnableAutomaticScreenRecording','routealias','map','fieldName','type','key','query','role','agent','raw','user','nolimit','getResources','rows','pick','globalStatus','globalStatusFilter','pauseTypeFilter','getQueues','chat','openchannel','fax','whatsapp','resolve','includes','channel','toLowerCase','Invalid\x20channel','getOptions','QueueCampaigns','findAndCountAll','all','util','lodash','jayson/promise','../../components/parsers/qs','api','../../mysqldb','client','http','offset','undefined','count','limit','set','json','error','name','status','request','Agents','then','result','Unable\x20to\x20retrieve\x20RPC\x20agents\x20(Routing)','merge','voice','startCase','Queues','format','Unable\x20to\x20retrieve\x20RPC\x20%s\x20queues','catch','VoiceQueuesChannels','Unable\x20to\x20retrieve\x20voice\x20queues\x20channels','loggedIn','available','waiting','paused','originated','message','dialActive','dialMethod','startPredictive','startProgressive','erlangCallToSecond','erlangAbandonmentRate','erlangBusyFactor','dialPredictiveIntervalMaxThreshold','dialPredictiveIntervalMinThreshold','predictiveIntervalAvailable','predictiveIntervalAnsweredCalls','predictiveIntervalDroppedCallsTimeout','predictiveIntervalDroppedCallsCallersExit','predictiveIntervalAvgHoldtime','predictiveIntervalAvgTalktime','get','agents','busy'];(function(_0x5e4a64,_0x2323f9){var _0x130b02=function(_0x534bbb){while(--_0x534bbb){_0x5e4a64['push'](_0x5e4a64['shift']());}};_0x130b02(++_0x2323f9);}(_0x2b51,0xe7));var _0x12b5=function(_0x10eeb1,_0x11b1dd){_0x10eeb1=_0x10eeb1-0x0;var _0x3827a9=_0x2b51[_0x10eeb1];return _0x3827a9;};'use strict';var util=require(_0x12b5('0x0'));var _=require(_0x12b5('0x1'));var jayson=require(_0x12b5('0x2'));var qs=require(_0x12b5('0x3'));var logger=require('../../config/logger')(_0x12b5('0x4'));var db=require(_0x12b5('0x5'))['db'];var amiClient=jayson[_0x12b5('0x6')][_0x12b5('0x7')]({'port':0x232a});var routingClient=jayson['client'][_0x12b5('0x7')]({'port':0x232c});function respondWithFilteredResult(_0xde8f02,_0x55136a){return function(_0x173204){if(_0x173204){var _0x5efae3=typeof _0x55136a[_0x12b5('0x8')]===_0x12b5('0x9')&&typeof _0x55136a['limit']==='undefined';var _0xcbd05=_0x173204['count'];var _0x348d0f=_0x5efae3?0x0:_0x55136a[_0x12b5('0x8')];var _0x4cd90f=_0x5efae3?_0x173204[_0x12b5('0xa')]:_0x55136a[_0x12b5('0x8')]+_0x55136a[_0x12b5('0xb')];var _0x40a7e7;if(_0x4cd90f>=_0xcbd05){_0x4cd90f=_0xcbd05;_0x40a7e7=0xc8;}else{_0x40a7e7=0xce;}_0xde8f02['status'](_0x40a7e7);return _0xde8f02[_0x12b5('0xc')]('Content-Range',_0x348d0f+'-'+_0x4cd90f+'/'+_0xcbd05)[_0x12b5('0xd')](_0x173204);}return null;};}function handleError(_0x4109f2,_0x147da7){_0x147da7=_0x147da7||0x1f4;return function(_0x3b82ab){logger[_0x12b5('0xe')](_0x3b82ab['stack']);if(_0x3b82ab[_0x12b5('0xf')]){delete _0x3b82ab[_0x12b5('0xf')];}_0x4109f2[_0x12b5('0x10')](_0x147da7)['send'](_0x3b82ab);};}function getRealtimeAgents(){return new Promise(function(_0xf9b8bc,_0x9b123b){return amiClient[_0x12b5('0x11')](_0x12b5('0x12'),{})[_0x12b5('0x13')](function(_0x2da51b){if(_0x2da51b['error'])throw new Error('Unable\x20to\x20retrieve\x20RPC\x20agents\x20(AMI)');var _0x11eec4=_0x2da51b[_0x12b5('0x14')];return routingClient[_0x12b5('0x11')]('Agents',{})[_0x12b5('0x13')](function(_0xe1a0e3){if(_0xe1a0e3[_0x12b5('0xe')])throw new Error(_0x12b5('0x15'));var _0x23476a=_0xe1a0e3[_0x12b5('0x14')];return _0xf9b8bc(_[_0x12b5('0x16')]({},_0x11eec4,_0x23476a));});})['catch'](function(_0x26990d){_0x9b123b(_0x26990d);});});}function getRealtimeQueues(_0x59aa6a,_0x29815b){return new Promise(function(_0x199116,_0x4108a4){var _0x538c62=_0x29815b===_0x12b5('0x17')?amiClient:routingClient;return _0x538c62[_0x12b5('0x11')](_[_0x12b5('0x18')](_0x29815b)+_0x12b5('0x19'),_0x59aa6a)[_0x12b5('0x13')](function(_0x23c1a4){if(_0x23c1a4[_0x12b5('0xe')])throw new Error(util[_0x12b5('0x1a')](_0x12b5('0x1b'),_0x29815b));return _0x199116(_0x23c1a4[_0x12b5('0x14')]);})[_0x12b5('0x1c')](function(_0xd882dc){_0x4108a4(_0xd882dc);});});}function getRealtimeVoiceQueueChannels(){return new Promise(function(_0x16c4b3,_0x576b58){return amiClient[_0x12b5('0x11')](_0x12b5('0x1d'),{})[_0x12b5('0x13')](function(_0x5bbeed){if(_0x5bbeed[_0x12b5('0xe')])throw new Error(_0x12b5('0x1e'));return _0x16c4b3(_0x5bbeed[_0x12b5('0x14')]);})['catch'](function(_0x2cf178){_0x576b58(_0x2cf178);});});}function initializeQueues(_0x530822,_0x8f37b9,_0x1c2da9,_0x35c9ff){var _0x2047d7=[_0x12b5('0x1f'),_0x12b5('0x20'),_0x12b5('0x21'),'talking',_0x12b5('0x22')];if(_0x1c2da9==='voice'&&_0x35c9ff==='outbound'){_0x2047d7['push']('pTalking',_0x12b5('0x23'),_0x12b5('0x24'),_0x12b5('0x25'),_0x12b5('0x26'),'Trunk',_0x12b5('0x27'),_0x12b5('0x28'),'erlangCalls',_0x12b5('0x29'),_0x12b5('0x2a'),_0x12b5('0x2b'),'dialPredictiveInterval',_0x12b5('0x2c'),_0x12b5('0x2d'),_0x12b5('0x2e'),'predictiveIntervalTotalCalls',_0x12b5('0x2f'),_0x12b5('0x30'),_0x12b5('0x31'),_0x12b5('0x32'),_0x12b5('0x33'));}return _0x530822['map'](function(_0x3a9474){var _0x5ec68e=_0x3a9474[_0x12b5('0x34')]({'plain':!![]});_0x5ec68e[_0x12b5('0x35')]=[];_0x5ec68e[_0x12b5('0x22')]=0x0;_0x5ec68e['loggedInDb']=0x0;_0x5ec68e[_0x12b5('0x1f')]=0x0;_0x5ec68e[_0x12b5('0x20')]=0x0;_0x5ec68e[_0x12b5('0x21')]=0x0;if(_0x1c2da9==='voice'){_0x5ec68e['talking']=0x0;_0x5ec68e[_0x12b5('0x36')]=0x0;if(_0x35c9ff===_0x12b5('0x37')){_0x5ec68e[_0x12b5('0x38')]=0x0;_0x5ec68e['originated']=0x0;}}var _0x4e7c03=_[_0x12b5('0x39')](_0x8f37b9,['id',_0x5ec68e['id']]);if(_0x4e7c03)_[_0x12b5('0x16')](_0x5ec68e,_['pick'](_0x4e7c03,_0x2047d7));return _0x5ec68e;});}function updateAgentVoiceStatus(_0x32f858,_0x140221,_0x28a9cc){var _0x5b8da6=_['filter'](_0x28a9cc,[_0x12b5('0x3a'),_0x140221[_0x12b5('0xf')]]);if(_0x5b8da6[_0x12b5('0x3b')]===0x0)return _0x32f858[_0x12b5('0x36')]=!![];if(_[_0x12b5('0x3c')](_0x5b8da6,[_0x12b5('0x3d'),_0x32f858[_0x12b5('0x3e')][_0x12b5('0x3f')]()]))return _0x32f858[_0x12b5('0x40')]=!![];if(!_0x32f858[_0x12b5('0x40')])return _0x32f858['busy']=!![];}function getVoiceQueueAgents(_0x5c331e,_0x2dee9a,_0x408bf2,_0x523025){var _0x1631ba=_[_0x12b5('0x39')](_0x2dee9a,['id',_0x5c331e['id']]);if(!_0x1631ba)return _0x5c331e;return _0x1631ba[_0x12b5('0x41')]({'raw':!![]})[_0x12b5('0x13')](function(_0x340896){for(var _0x6449f7=0x0;_0x6449f7<_0x340896[_0x12b5('0x3b')];_0x6449f7++){var _0x441225=_[_0x12b5('0x39')](_0x408bf2,['id',_0x340896[_0x6449f7]['UserId']]);if(!_0x441225){_0x441225={'id':_0x340896[_0x6449f7][_0x12b5('0x42')],'name':_0x340896[_0x6449f7]['UserId'],'accountcode':'','online':![]};}var _0x2948fb={'id':_0x441225['id'],'name':_0x441225[_0x12b5('0xf')],'internal':_0x441225['accountcode'],'online':Boolean(_0x441225[_0x12b5('0x43')]),'paused':Boolean(_0x340896[_0x6449f7][_0x12b5('0x22')])};if(_0x441225[_0x12b5('0x44')]===_0x12b5('0x40'))updateAgentVoiceStatus(_0x2948fb,_0x5c331e,_0x523025);if(_0x2948fb[_0x12b5('0x43')])_0x5c331e[_0x12b5('0x45')]++;if(_0x2948fb[_0x12b5('0x36')])_0x5c331e[_0x12b5('0x36')]++;if(_0x2948fb['paused']&&!_0x2948fb[_0x12b5('0x36')]&&!_0x2948fb[_0x12b5('0x40')])_0x5c331e[_0x12b5('0x22')]++;_0x5c331e[_0x12b5('0x35')][_0x12b5('0x46')](_0x2948fb);}return _0x5c331e;});}function getQueueAgents(_0x1bf785,_0x7914c1,_0x5350b4){var _0x9e4aa5=_0x12b5('0x47')+_[_0x12b5('0x18')](_0x5350b4)+_0x12b5('0x48');var _0x4613bd=_0x5350b4+_0x12b5('0x49');return db[_0x9e4aa5]['findAll']({'where':{[_[_0x12b5('0x18')](_0x5350b4)+'QueueId']:_0x1bf785['id']},'raw':!![]})[_0x12b5('0x13')](function(_0x546145){for(var _0x1dedc4=0x0;_0x1dedc4<_0x546145[_0x12b5('0x3b')];_0x1dedc4++){var _0x5e7bfa=_[_0x12b5('0x39')](_0x7914c1,['id',_0x546145[_0x1dedc4][_0x12b5('0x42')]]);if(!_0x5e7bfa){_0x5e7bfa={'id':_0x546145[_0x1dedc4][_0x12b5('0x42')],'name':_0x546145[_0x1dedc4][_0x12b5('0x42')],'online':![],['pauseField']:![]};}var _0xa00764={'id':_0x5e7bfa['id'],'name':_0x5e7bfa['name'],'online':Boolean(_0x5e7bfa['online']),'paused':Boolean(_0x5e7bfa[_0x4613bd])};if(_0xa00764[_0x12b5('0x43')])_0x1bf785[_0x12b5('0x45')]++;_0x1bf785[_0x12b5('0x35')][_0x12b5('0x46')](_0xa00764);}return _0x1bf785;});}function isIdle(_0x5e2f4e){return _0x5e2f4e===_0x12b5('0x4a')||_0x5e2f4e===_0x12b5('0x4b')||_0x5e2f4e==='unavailable'||_0x5e2f4e===_0x12b5('0x4c')||_[_0x12b5('0x4d')](_0x5e2f4e);}function isAgentPaused(_0x15d5ad,_0x4c51aa){var _0x18bf6f=[_0x15d5ad[_0x12b5('0x4e')],_0x15d5ad[_0x12b5('0x4f')],_0x15d5ad['mailPause'],_0x15d5ad['openchannelPause'],_0x15d5ad['smsPause'],_0x15d5ad['voicePause'],_0x15d5ad[_0x12b5('0x50')]];if(!_0x4c51aa)return _['every'](_0x18bf6f);var _0x2c9c86=_[_0x12b5('0x51')](_0x18bf6f);return _0x2c9c86['length']>0x0&&_0x2c9c86['length']<_0x18bf6f[_0x12b5('0x3b')]?!![]:![];}function isAgentIdle(_0x3f7d05){if(_0x3f7d05[_0x12b5('0x44')]!=='idle'&&_0x3f7d05['voicePause']){_0x3f7d05[_0x12b5('0x44')]='pause';return![];}return _[_0x12b5('0x52')]([_0x3f7d05[_0x12b5('0x53')],_0x3f7d05[_0x12b5('0x54')],_0x3f7d05['mailStatus'],_0x3f7d05[_0x12b5('0x55')],_0x3f7d05[_0x12b5('0x56')],_0x3f7d05['voiceStatus'],_0x3f7d05[_0x12b5('0x57')]],isIdle);}function getAgentStatus(_0x4ce15c){var _0x1e5a94=_0x12b5('0x4b');if(isAgentPaused(_0x4ce15c))_0x1e5a94='pause';else if(isAgentPaused(_0x4ce15c,!![]))_0x1e5a94=_0x12b5('0x58');else if(isAgentIdle(_0x4ce15c))_0x1e5a94=_0x12b5('0x4a');else _0x1e5a94=_0x12b5('0x36');return _0x1e5a94;}exports['getAgents']=function(_0xe2fcc7,_0x16b235){var _0x1a4040={'count':0x0,'rows':[]},_0x57f9de={},_0x22c785,_0x544910,_0x51a2ff=[_0x12b5('0x59'),_0x12b5('0x43'),_0x12b5('0x5a'),_0x12b5('0x5b'),_0x12b5('0x5c'),_0x12b5('0x5b'),_0x12b5('0x3e'),_0x12b5('0x5d'),_0x12b5('0x4e'),_0x12b5('0x5e'),_0x12b5('0x5f'),'smsPause',_0x12b5('0x50'),_0x12b5('0x4f'),_0x12b5('0x60'),'chatCapacity','openchannelCapacity',_0x12b5('0x61'),_0x12b5('0x62'),'whatsappCapacity',_0x12b5('0x63'),_0x12b5('0x64'),'openchannelCurrentCapacity','mailCurrentCapacity','smsCurrentCapacity','whatsappCurrentCapacity',_0x12b5('0x65'),_0x12b5('0x53'),'openchannelStatus',_0x12b5('0x66'),'smsStatus',_0x12b5('0x57'),_0x12b5('0x54'),_0x12b5('0x44'),_0x12b5('0x67'),_0x12b5('0x68'),'mailStatusTime',_0x12b5('0x69'),_0x12b5('0x6a'),'faxStatusTime',_0x12b5('0x6b'),_0x12b5('0x3a'),_0x12b5('0x6c'),_0x12b5('0x6d'),_0x12b5('0x6e'),_0x12b5('0x6f'),_0x12b5('0x70')],_0x238822=['chatPause',_0x12b5('0x5e'),'mailPause','smsPause',_0x12b5('0x50'),_0x12b5('0x4f'),_0x12b5('0x60'),_0x12b5('0x43')];Promise['resolve']()[_0x12b5('0x13')](function(){var _0x2b6509=_[_0x12b5('0x71')](db['User']['rawAttributes'],function(_0x3d5574){return{'name':_0x3d5574[_0x12b5('0x72')],'type':_0x3d5574[_0x12b5('0x73')][_0x12b5('0x74')]};});_0xe2fcc7[_0x12b5('0x75')][_0x12b5('0x76')]=_0x12b5('0x77');_0xe2fcc7[_0x12b5('0x75')][_0x12b5('0x43')]=!![];_0x57f9de=qs['getOptions'](_0x2b6509,_0xe2fcc7);_0x57f9de[_0x12b5('0x78')]=!![];if(_0xe2fcc7['user']['role']===_0x12b5('0x79')){var _0x1882f3={'params':{'id':_0xe2fcc7['user']['userProfileId']},'query':{'section':_0x12b5('0x12'),'role':_0x12b5('0x77'),'online':!![],'nolimit':_0xe2fcc7[_0x12b5('0x75')][_0x12b5('0x7a')],'sort':_0xe2fcc7[_0x12b5('0x75')]['sort'],'filter':_0xe2fcc7['query'][_0x12b5('0x51')]},'raw':!![]};return db['UserProfile'][_0x12b5('0x7b')](_0x1882f3);}return db[_0x12b5('0x47')]['findAndCountAll'](_0x57f9de);})[_0x12b5('0x13')](function(_0x6ac455){_0x1a4040[_0x12b5('0xa')]=_0x6ac455['count'];_0x22c785=_0x6ac455[_0x12b5('0x7c')];return getRealtimeAgents();})['then'](function(_0x429450){_0x544910=_0x429450['rows'];return _0x22c785['map'](function(_0x454ab2){var _0x2339de=_[_0x12b5('0x39')](_0x544910,['id',_0x454ab2['id']]);if(_0x2339de)_[_0x12b5('0x16')](_0x454ab2,_[_0x12b5('0x7d')](_['omit'](_0x2339de,_0x238822),_0x51a2ff));_0x454ab2[_0x12b5('0x7e')]=getAgentStatus(_0x454ab2);return _0x454ab2;});})[_0x12b5('0x13')](function(_0x4ca767){if(_0xe2fcc7[_0x12b5('0x75')][_0x12b5('0x7f')]){var _0x193755=_[_0x12b5('0x51')](_0x4ca767,[_0x12b5('0x7e'),_0xe2fcc7[_0x12b5('0x75')][_0x12b5('0x7f')]]);if(_0xe2fcc7[_0x12b5('0x75')][_0x12b5('0x80')]){_0x193755=_[_0x12b5('0x51')](_0x193755,['pauseType',_0xe2fcc7[_0x12b5('0x75')]['pauseTypeFilter']]);}}_0x1a4040['rows']=_0x193755||_0x4ca767;return _0x1a4040;})[_0x12b5('0x13')](respondWithFilteredResult(_0x16b235,_0x57f9de))[_0x12b5('0x1c')](handleError(_0x16b235,null));};exports[_0x12b5('0x81')]=function(_0x324b58,_0x275275){var _0x150c9b=['mail',_0x12b5('0x82'),'sms',_0x12b5('0x83'),_0x12b5('0x84'),_0x12b5('0x85'),_0x12b5('0x17')];var _0x33defc={'count':0x0,'rows':[]},_0x42b3f4={},_0x44ac34,_0x1b4600,_0x2f6029,_0x4156b3,_0x1126ab;Promise[_0x12b5('0x86')]()[_0x12b5('0x13')](function(){if(_0x324b58[_0x12b5('0x75')]['channel']){if(!_[_0x12b5('0x87')](_0x150c9b,_0x324b58[_0x12b5('0x75')][_0x12b5('0x88')][_0x12b5('0x89')]()))throw new Error(_0x12b5('0x8a'));_0x44ac34=_0x324b58[_0x12b5('0x75')]['channel'][_0x12b5('0x89')]();}var _0x44b89c=_[_0x12b5('0x18')](_0x44ac34)+_0x12b5('0x48');var _0x42c945=_[_0x12b5('0x71')](db[_0x44b89c]['rawAttributes'],function(_0x22232e){return{'name':_0x22232e['fieldName'],'type':_0x22232e[_0x12b5('0x73')][_0x12b5('0x74')]};});_0x42b3f4=qs[_0x12b5('0x8b')](_0x42c945,_0x324b58);if(_0x324b58['user'][_0x12b5('0x76')]==='user'){var _0x484cd2={'params':{'id':_0x324b58[_0x12b5('0x79')]['userProfileId']},'query':{'section':_0x44ac34===_0x12b5('0x17')&&_0x324b58[_0x12b5('0x75')][_0x12b5('0x73')]===_0x12b5('0x37')?_0x12b5('0x8c'):_[_0x12b5('0x18')](_0x44ac34)+_0x12b5('0x19'),'type':_0x324b58[_0x12b5('0x75')]['type'],'nolimit':!![],'sort':_0x12b5('0xf'),'filter':_0x324b58[_0x12b5('0x75')]['filter']}};return db['UserProfile']['getResources'](_0x484cd2);}return db[_0x44b89c][_0x12b5('0x8d')](_0x42b3f4);})[_0x12b5('0x13')](function(_0x2553e1){_0x33defc[_0x12b5('0xa')]=_0x2553e1[_0x12b5('0xa')];_0x1b4600=_0x2553e1[_0x12b5('0x7c')];return getRealtimeQueues(_0x324b58,_0x44ac34);})[_0x12b5('0x13')](function(_0x196f46){_0x4156b3=_0x196f46[_0x12b5('0x7c')];return getRealtimeAgents();})[_0x12b5('0x13')](function(_0x318190){_0x2f6029=_0x318190[_0x12b5('0x7c')];if(_0x44ac34!=_0x12b5('0x17'))return;return getRealtimeVoiceQueueChannels();})[_0x12b5('0x13')](function(_0x7edd7e){_0x1126ab=_0x7edd7e?_0x7edd7e['rows']:[];return initializeQueues(_0x1b4600,_0x4156b3,_0x44ac34,_0x324b58['query'][_0x12b5('0x73')]);})[_0x12b5('0x13')](function(_0x1d109e){var _0x441772=_0x1d109e[_0x12b5('0x71')](function(_0x17dfe7){if(_0x44ac34==='voice')return getVoiceQueueAgents(_0x17dfe7,_0x1b4600,_0x2f6029,_0x1126ab);else return getQueueAgents(_0x17dfe7,_0x2f6029,_0x44ac34);});return Promise[_0x12b5('0x8e')](_0x441772);})[_0x12b5('0x13')](function(_0x9f3cad){_0x33defc[_0x12b5('0x7c')]=_0x9f3cad;return _0x33defc;})[_0x12b5('0x13')](respondWithFilteredResult(_0x275275,_0x42b3f4))[_0x12b5('0x1c')](handleError(_0x275275,null));};