Built motion from commit da617fac.|2.6.6
[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 _0x0aae=['client','count','offset','status','set','Content-Range','error','name','send','request','Queues','then','format','Unable\x20to\x20retrieve\x20RPC\x20%s\x20queues','result','catch','Unable\x20to\x20retrieve\x20voice\x20queues\x20channels','available','waiting','talking','paused','voice','push','pTalking','originated','message','dialActive','startPredictive','startProgressive','erlangBusyFactor','dialPredictiveInterval','dialPredictiveIntervalMaxThreshold','dialPredictiveIntervalMinThreshold','predictiveIntervalAvailable','predictiveIntervalTotalCalls','predictiveIntervalAnsweredCalls','predictiveIntervalDroppedCallsCallersExit','predictiveIntervalAvgTalktime','map','get','agents','loggedInDb','loggedIn','busy','merge','pick','length','some','connectedlinenum','toString','getMembers','find','UserId','online','voiceStatus','User','startCase','Pause','QueueId','getChannels','Invalid\x20channel','toLowerCase','getAgents','globalQueueFilter','query','globalStatusFilter','pause','*pause','resolve','channel','sort','includes','globalStatus','validateSortParameter','getUpdatedSortOptions','fields','getSortedAgentsIdsByStatus','getAgentIdsByStatus','fieldName','type','key','role','agent','getOptions','raw','sequelize','literal','FIELD(id,','order','forEach','where','user','Agents','assign','UserProfile','findAndCountAll','rows','getAgentStatus','sortAgentsByStatus','rawAttributes','userProfileId','outbound','QueueCampaigns','filter','getResources','all','util','lodash','jayson/promise','../../components/realtime/agents','../../components/parsers/qs','../../config/utils','../../config/logger','../../mysqldb','http'];(function(_0x5ee85d,_0x1a8510){var _0x5b1966=function(_0x31e1d1){while(--_0x31e1d1){_0x5ee85d['push'](_0x5ee85d['shift']());}};_0x5b1966(++_0x1a8510);}(_0x0aae,0x1c3));var _0xe0aa=function(_0x34d641,_0x5d2b8a){_0x34d641=_0x34d641-0x0;var _0x525ffb=_0x0aae[_0x34d641];return _0x525ffb;};'use strict';var util=require(_0xe0aa('0x0'));var _=require(_0xe0aa('0x1'));var jayson=require(_0xe0aa('0x2'));var rta=require(_0xe0aa('0x3'));var qs=require(_0xe0aa('0x4'));var utils=require(_0xe0aa('0x5'));var logger=require(_0xe0aa('0x6'))('api');var db=require(_0xe0aa('0x7'))['db'];var amiClient=jayson['client'][_0xe0aa('0x8')]({'port':0x232a});var routingClient=jayson[_0xe0aa('0x9')][_0xe0aa('0x8')]({'port':0x232c});function respondWithFilteredResult(_0xab5c5d,_0x5149b6){return function(_0x27b361){if(_0x27b361){var _0x2a3261=typeof _0x5149b6['offset']==='undefined'&&typeof _0x5149b6['limit']==='undefined';var _0x38a43f=_0x27b361[_0xe0aa('0xa')];var _0x55202d=_0x2a3261?0x0:_0x5149b6[_0xe0aa('0xb')];var _0x1fdb47=_0x2a3261?_0x27b361[_0xe0aa('0xa')]:_0x5149b6[_0xe0aa('0xb')]+_0x5149b6['limit'];var _0x697b96;if(_0x1fdb47>=_0x38a43f){_0x1fdb47=_0x38a43f;_0x697b96=0xc8;}else{_0x697b96=0xce;}_0xab5c5d[_0xe0aa('0xc')](_0x697b96);return _0xab5c5d[_0xe0aa('0xd')](_0xe0aa('0xe'),_0x55202d+'-'+_0x1fdb47+'/'+_0x38a43f)['json'](_0x27b361);}return null;};}function handleError(_0xce29a2,_0x4fcb38){_0x4fcb38=_0x4fcb38||0x1f4;return function(_0x2f926f){logger[_0xe0aa('0xf')](_0x2f926f['stack']);if(_0x2f926f[_0xe0aa('0x10')]){delete _0x2f926f['name'];}_0xce29a2[_0xe0aa('0xc')](_0x4fcb38)[_0xe0aa('0x11')](_0x2f926f);};}function getRealtimeQueues(_0x1115b3,_0x160599){return new Promise(function(_0x61d895,_0x1d1960){var _0x481321=_0x160599==='voice'?amiClient:routingClient;return _0x481321[_0xe0aa('0x12')](_['startCase'](_0x160599)+_0xe0aa('0x13'),_0x1115b3)[_0xe0aa('0x14')](function(_0x4e42ba){if(_0x4e42ba[_0xe0aa('0xf')])throw new Error(util[_0xe0aa('0x15')](_0xe0aa('0x16'),_0x160599));return _0x61d895(_0x4e42ba[_0xe0aa('0x17')]);})[_0xe0aa('0x18')](function(_0x1b6193){_0x1d1960(_0x1b6193);});});}function getRealtimeVoiceQueueChannels(){return new Promise(function(_0x3b3ab2,_0x1ce46c){return amiClient[_0xe0aa('0x12')]('VoiceQueuesChannels',{})[_0xe0aa('0x14')](function(_0x1b8d01){if(_0x1b8d01[_0xe0aa('0xf')])throw new Error(_0xe0aa('0x19'));return _0x3b3ab2(_0x1b8d01[_0xe0aa('0x17')]);})[_0xe0aa('0x18')](function(_0x2791b5){_0x1ce46c(_0x2791b5);});});}function initializeQueues(_0x2b1adc,_0x59cd0f,_0x1fbc1f,_0x3cf96e){var _0x391e8c=['loggedIn',_0xe0aa('0x1a'),_0xe0aa('0x1b'),_0xe0aa('0x1c'),_0xe0aa('0x1d')];if(_0x1fbc1f===_0xe0aa('0x1e')&&_0x3cf96e==='outbound'){_0x391e8c[_0xe0aa('0x1f')](_0xe0aa('0x20'),_0xe0aa('0x21'),_0xe0aa('0x22'),_0xe0aa('0x23'),'dialMethod','Trunk',_0xe0aa('0x24'),_0xe0aa('0x25'),'erlangCalls','erlangCallToSecond','erlangAbandonmentRate',_0xe0aa('0x26'),_0xe0aa('0x27'),_0xe0aa('0x28'),_0xe0aa('0x29'),_0xe0aa('0x2a'),_0xe0aa('0x2b'),_0xe0aa('0x2c'),'predictiveIntervalDroppedCallsTimeout',_0xe0aa('0x2d'),'predictiveIntervalAvgHoldtime',_0xe0aa('0x2e'));}return _0x2b1adc[_0xe0aa('0x2f')](function(_0x40d8f4){var _0x13a284=_0x40d8f4[_0xe0aa('0x30')]({'plain':!![]});_0x13a284[_0xe0aa('0x31')]=[];_0x13a284[_0xe0aa('0x1d')]=0x0;_0x13a284[_0xe0aa('0x32')]=0x0;_0x13a284[_0xe0aa('0x33')]=0x0;_0x13a284['available']=0x0;_0x13a284[_0xe0aa('0x1b')]=0x0;if(_0x1fbc1f==='voice'){_0x13a284[_0xe0aa('0x1c')]=0x0;_0x13a284[_0xe0aa('0x34')]=0x0;if(_0x3cf96e==='outbound'){_0x13a284[_0xe0aa('0x20')]=0x0;_0x13a284[_0xe0aa('0x21')]=0x0;}}var _0xf7541a=_['find'](_0x59cd0f,['id',_0x13a284['id']]);if(_0xf7541a)_[_0xe0aa('0x35')](_0x13a284,_[_0xe0aa('0x36')](_0xf7541a,_0x391e8c));return _0x13a284;});}function updateAgentVoiceStatus(_0x1fa61d,_0x409fb4,_0x4e748e){var _0x47d63c=_['filter'](_0x4e748e,['queue',_0x409fb4[_0xe0aa('0x10')]]);if(_0x47d63c[_0xe0aa('0x37')]===0x0)return _0x1fa61d[_0xe0aa('0x34')]=!![];if(_[_0xe0aa('0x38')](_0x47d63c,[_0xe0aa('0x39'),_0x1fa61d['internal'][_0xe0aa('0x3a')]()]))return _0x1fa61d[_0xe0aa('0x1c')]=!![];if(!_0x1fa61d[_0xe0aa('0x1c')])return _0x1fa61d[_0xe0aa('0x34')]=!![];}function getVoiceQueueAgents(_0x395fad,_0x210469,_0xeef056,_0x2d429c){var _0x2bd60e=_['find'](_0x210469,['id',_0x395fad['id']]);if(!_0x2bd60e)return _0x395fad;return _0x2bd60e[_0xe0aa('0x3b')]({'raw':!![]})['then'](function(_0x2ff34b){for(var _0x12ba4d=0x0;_0x12ba4d<_0x2ff34b['length'];_0x12ba4d++){var _0x51769a=_[_0xe0aa('0x3c')](_0xeef056,['id',_0x2ff34b[_0x12ba4d][_0xe0aa('0x3d')]]);if(!_0x51769a){_0x51769a={'id':_0x2ff34b[_0x12ba4d]['UserId'],'name':_0x2ff34b[_0x12ba4d]['UserId'],'accountcode':'','online':![]};}var _0x57c636={'id':_0x51769a['id'],'name':_0x51769a[_0xe0aa('0x10')],'internal':_0x51769a['accountcode'],'online':Boolean(_0x51769a[_0xe0aa('0x3e')]),'paused':Boolean(_0x2ff34b[_0x12ba4d][_0xe0aa('0x1d')])};if(_0x51769a[_0xe0aa('0x3f')]===_0xe0aa('0x1c'))updateAgentVoiceStatus(_0x57c636,_0x395fad,_0x2d429c);if(_0x57c636[_0xe0aa('0x3e')])_0x395fad[_0xe0aa('0x32')]++;if(_0x57c636[_0xe0aa('0x34')])_0x395fad[_0xe0aa('0x34')]++;if(_0x57c636[_0xe0aa('0x1d')]&&!_0x57c636[_0xe0aa('0x34')]&&!_0x57c636[_0xe0aa('0x1c')])_0x395fad[_0xe0aa('0x1d')]++;_0x395fad[_0xe0aa('0x31')]['push'](_0x57c636);}return _0x395fad;});}function getQueueAgents(_0x4fc0b1,_0x3a0976,_0x400b59){var _0x62046=_0xe0aa('0x40')+_[_0xe0aa('0x41')](_0x400b59)+'Queue';var _0x501a4d=_0x400b59+_0xe0aa('0x42');return db[_0x62046]['findAll']({'where':{[_[_0xe0aa('0x41')](_0x400b59)+_0xe0aa('0x43')]:_0x4fc0b1['id']},'raw':!![]})[_0xe0aa('0x14')](function(_0x3d82db){for(var _0x45ff61=0x0;_0x45ff61<_0x3d82db[_0xe0aa('0x37')];_0x45ff61++){var _0x3a0778=_[_0xe0aa('0x3c')](_0x3a0976,['id',_0x3d82db[_0x45ff61]['UserId']]);if(!_0x3a0778){_0x3a0778={'id':_0x3d82db[_0x45ff61][_0xe0aa('0x3d')],'name':_0x3d82db[_0x45ff61][_0xe0aa('0x3d')],'online':![],['pauseField']:![]};}var _0x1464a3={'id':_0x3a0778['id'],'name':_0x3a0778[_0xe0aa('0x10')],'online':Boolean(_0x3a0778[_0xe0aa('0x3e')]),'paused':Boolean(_0x3a0778[_0x501a4d])};if(_0x1464a3['online'])_0x4fc0b1[_0xe0aa('0x32')]++;_0x4fc0b1['agents'][_0xe0aa('0x1f')](_0x1464a3);}return _0x4fc0b1;});}function validateChannel(_0x111b88){var _0x250528=utils[_0xe0aa('0x44')]();if(!_['includes'](_0x250528,_0x111b88['toLowerCase']()))throw new Error(_0xe0aa('0x45'));return _0x111b88[_0xe0aa('0x46')]();}exports[_0xe0aa('0x47')]=function(_0x4d863e,_0x30208d){var _0x52fe6f={'count':0x0,'rows':[]};var _0x452335={};var _0x5ea079;var _0x4e28b1;var _0x372cd0;var _0x348d10;var _0x1692c0=utils['getChannels']();var _0x1f8a7e=_0x4d863e['query'][_0xe0aa('0x48')];var _0xa1318d=_0x4d863e[_0xe0aa('0x49')][_0xe0aa('0x4a')];var _0x4f6b48=_0x4d863e[_0xe0aa('0x49')]['pauseTypeFilter'];var _0xd65b2e=_0xa1318d===_0xe0aa('0x4b')||_0xa1318d===_0xe0aa('0x4c')?!![]:![];var _0x5b71b8=[];var _0x13813d;Promise[_0xe0aa('0x4d')]()['then'](function(){if(_0x4d863e[_0xe0aa('0x49')][_0xe0aa('0x4e')]){_0x5ea079=validateChannel(_0x4d863e[_0xe0aa('0x49')]['channel']);}if(!_0x4d863e[_0xe0aa('0x49')][_0xe0aa('0x4f')]||!_0x4d863e['query']['sort'][_0xe0aa('0x50')](_0xe0aa('0x51'))){_0x4d863e[_0xe0aa('0x49')][_0xe0aa('0x4f')]=rta[_0xe0aa('0x52')](_0x4d863e['query'][_0xe0aa('0x4f')]);return;}var _0x327b4f=rta[_0xe0aa('0x53')](_0x4d863e[_0xe0aa('0x49')]['sort']);_0x4d863e['query'][_0xe0aa('0x4f')]=_0x327b4f[_0xe0aa('0x54')]['join'](',');_0x4e28b1=_0x327b4f['order'];})[_0xe0aa('0x14')](function(){if(!_0x1f8a7e){return[];}return rta['getAgentIdsByQueues'](_0x1f8a7e,_0x5ea079);})[_0xe0aa('0x14')](function(_0x31f25e){_0x5b71b8=_0x31f25e;return rta['getRealtimeAgents']();})[_0xe0aa('0x14')](function(_0x6cb91b){_0x348d10=_0x6cb91b['rows'];if((!_0xa1318d||_0xd65b2e)&&!_0x4e28b1){return _0x5b71b8;}if(_0x4e28b1&&!_0xa1318d){return rta[_0xe0aa('0x55')](_0x348d10,_0x5b71b8,_0x4e28b1);}else{return rta[_0xe0aa('0x56')](_0x348d10,_0x5b71b8,_0xa1318d);}})[_0xe0aa('0x14')](function(_0x15c779){_0x5b71b8=_0x15c779;var _0x403812=![];if(_0x5b71b8['length']===0x0){if(_0x1f8a7e||_0xa1318d){_0x403812=!![];}if(_0xd65b2e){_0x403812=![];}}if(_0x403812){return _0x52fe6f;}var _0x3d27bd=_[_0xe0aa('0x2f')](db['User']['rawAttributes'],function(_0x25d1d4){return{'name':_0x25d1d4[_0xe0aa('0x57')],'type':_0x25d1d4[_0xe0aa('0x58')][_0xe0aa('0x59')]};});_0x4d863e[_0xe0aa('0x49')][_0xe0aa('0x5a')]=_0xe0aa('0x5b');_0x4d863e[_0xe0aa('0x49')][_0xe0aa('0x3e')]=!![];_0x452335=qs[_0xe0aa('0x5c')](_0x3d27bd,_0x4d863e);_0x452335[_0xe0aa('0x5d')]=!![];var _0x234861=[];if(_0x5b71b8['length']>0x0){_0x234861[_0xe0aa('0x1f')]({'id':{'$in':_0x5b71b8}});if(_0x4e28b1&&!_0xa1318d){_0x13813d=[db[_0xe0aa('0x5e')][_0xe0aa('0x5f')](_0xe0aa('0x60')+_0x5b71b8+')')];_0x452335[_0xe0aa('0x61')]=_0x13813d;}}if(_0xd65b2e){_0x1692c0[_0xe0aa('0x62')](function(_0xab7a55){var _0x325fdf=!![];if(_0xa1318d===_0xe0aa('0x4c')&&_0x5ea079!==_0xab7a55){_0x325fdf=![];}_0x234861['push']({[_0xab7a55+'Pause']:_0x325fdf});});if(_0x4f6b48){_0x234861[_0xe0aa('0x1f')]({'pauseType':_0x4f6b48});}}else if(_0xa1318d){_0x1692c0[_0xe0aa('0x62')](function(_0x388d7f){_0x234861[_0xe0aa('0x1f')]({[_0x388d7f+_0xe0aa('0x42')]:![]});});}_0x452335[_0xe0aa('0x63')]=_[_0xe0aa('0x35')](_0x452335[_0xe0aa('0x63')],{'$and':_0x234861});if(_0x4d863e[_0xe0aa('0x64')][_0xe0aa('0x5a')]===_0xe0aa('0x64')){var _0x396f6c={'params':{'id':_0x4d863e[_0xe0aa('0x64')]['userProfileId']},'query':{'section':_0xe0aa('0x65'),'role':_0xe0aa('0x5b'),'online':!![],'raw':!![],'ids':_0x5b71b8,'customSorting':_0x13813d}};if(_0xd65b2e){_0x396f6c[_0xe0aa('0x49')]['extraQueryConditions']=_0x234861;}_0x396f6c[_0xe0aa('0x49')]=Object[_0xe0aa('0x66')](_0x396f6c[_0xe0aa('0x49')],_0x4d863e[_0xe0aa('0x49')]);delete _0x396f6c[_0xe0aa('0x49')]['id'];return db[_0xe0aa('0x67')]['getResources'](_0x396f6c);}return db[_0xe0aa('0x40')][_0xe0aa('0x68')](_0x452335);})['then'](function(_0x2b0d4b){_0x52fe6f['count']=_0x2b0d4b[_0xe0aa('0xa')];_0x372cd0=_0x2b0d4b[_0xe0aa('0x69')];return _0x372cd0[_0xe0aa('0x2f')](function(_0x2375e8){var _0x5295a7=_['find'](_0x348d10,['id',_0x2375e8['id']]);if(_0x5295a7){var _0x139d73=rta['filterAgentAttributes'](_0x5295a7);_[_0xe0aa('0x35')](_0x2375e8,_0x139d73);}_0x2375e8[_0xe0aa('0x51')]=rta[_0xe0aa('0x6a')](_0x2375e8);if(_0x2375e8[_0xe0aa('0x51')][_0xe0aa('0x50')](_0xe0aa('0x4b'))){_0x1692c0[_0xe0aa('0x62')](function(_0x4e046e){if(_0x2375e8[_0x4e046e+_0xe0aa('0x42')]){_0x2375e8[_0x4e046e+'Status']='pause';}});}return _0x2375e8;});})[_0xe0aa('0x14')](function(_0x236fc8){if(!_0x4e28b1||_0x13813d){return _0x236fc8;}return rta[_0xe0aa('0x6b')](_0x236fc8,_0x4e28b1);})[_0xe0aa('0x14')](function(_0x54d9da){_0x52fe6f['rows']=_0x54d9da;return _0x52fe6f;})[_0xe0aa('0x14')](respondWithFilteredResult(_0x30208d,_0x452335))[_0xe0aa('0x18')](handleError(_0x30208d,null));};exports['getQueues']=function(_0x523656,_0x5ebfa7){var _0x4e52b9={'count':0x0,'rows':[]},_0x3b4736={},_0x2e645b,_0x57e0f4,_0x1a7bd3,_0x1631c9,_0x5e25c1;Promise['resolve']()[_0xe0aa('0x14')](function(){if(_0x523656[_0xe0aa('0x49')][_0xe0aa('0x4e')])_0x2e645b=validateChannel(_0x523656[_0xe0aa('0x49')]['channel']);var _0x26e333=_[_0xe0aa('0x41')](_0x2e645b)+'Queue';var _0x3f39ad=_['map'](db[_0x26e333][_0xe0aa('0x6c')],function(_0x106356){return{'name':_0x106356[_0xe0aa('0x57')],'type':_0x106356['type'][_0xe0aa('0x59')]};});_0x3b4736=qs[_0xe0aa('0x5c')](_0x3f39ad,_0x523656);if(_0x523656[_0xe0aa('0x64')]['role']===_0xe0aa('0x64')){var _0x4f757a={'params':{'id':_0x523656[_0xe0aa('0x64')][_0xe0aa('0x6d')]},'query':{'section':_0x2e645b===_0xe0aa('0x1e')&&_0x523656[_0xe0aa('0x49')][_0xe0aa('0x58')]===_0xe0aa('0x6e')?_0xe0aa('0x6f'):_[_0xe0aa('0x41')](_0x2e645b)+'Queues','type':_0x523656[_0xe0aa('0x49')][_0xe0aa('0x58')],'nolimit':!![],'sort':_0xe0aa('0x10'),'filter':_0x523656['query'][_0xe0aa('0x70')]}};return db[_0xe0aa('0x67')][_0xe0aa('0x71')](_0x4f757a);}return db[_0x26e333]['findAndCountAll'](_0x3b4736);})[_0xe0aa('0x14')](function(_0x2eb9d5){_0x4e52b9['count']=_0x2eb9d5[_0xe0aa('0xa')];_0x57e0f4=_0x2eb9d5[_0xe0aa('0x69')];return getRealtimeQueues(_0x523656,_0x2e645b);})['then'](function(_0xd162c2){_0x1631c9=_0xd162c2[_0xe0aa('0x69')];return rta['getRealtimeAgents']();})['then'](function(_0x5358be){_0x1a7bd3=_0x5358be[_0xe0aa('0x69')];if(_0x2e645b!='voice')return;return getRealtimeVoiceQueueChannels();})[_0xe0aa('0x14')](function(_0x137236){_0x5e25c1=_0x137236?_0x137236[_0xe0aa('0x69')]:[];return initializeQueues(_0x57e0f4,_0x1631c9,_0x2e645b,_0x523656[_0xe0aa('0x49')][_0xe0aa('0x58')]);})['then'](function(_0x2da767){var _0x36d57d=_0x2da767['map'](function(_0x4f37a5){if(_0x2e645b===_0xe0aa('0x1e'))return getVoiceQueueAgents(_0x4f37a5,_0x57e0f4,_0x1a7bd3,_0x5e25c1);else return getQueueAgents(_0x4f37a5,_0x1a7bd3,_0x2e645b);});return Promise[_0xe0aa('0x72')](_0x36d57d);})['then'](function(_0x24027f){_0x4e52b9[_0xe0aa('0x69')]=_0x24027f;return _0x4e52b9;})[_0xe0aa('0x14')](respondWithFilteredResult(_0x5ebfa7,_0x3b4736))[_0xe0aa('0x18')](handleError(_0x5ebfa7,null));};