Built motion from commit b0a9a629.|2.6.33
[motion2.git] / server / components / realtime / agents.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 _0x2ff5=['getAgentIdsByStatus','resolve','User','findAll','fullname','chatPause','smsPause','pauseType','find','getSortedAgentsIdsByStatus','fields','globalStatus','order','startsWith','ASC','getUpdatedSortOptions','internal','substring','indexOf','validateSortParameter','sort','DESC','toLowerCase','sortAgentsByStatus','userpic','online','lastPauseAt','address','chatCapacity','openchannelCapacity','mailCapacity','smsCapacity','chatCurrentCapacity','openchannelCurrentCapacity','mailCurrentCapacity','smsCurrentCapacity','whatsappCurrentCapacity','faxCurrentCapacity','mailStatus','chatStatusTime','openchannelStatusTime','smsStatusTime','whatsappStatusTime','faxStatusTime','voiceStatusTime','destconnectedlinenum','screenrecording','phoneBarEnableScreenRecordingByAgent','phoneBarEnableAutomaticScreenRecording','omit','role','mailStatusTime','faxCapacity','whatsappCapacity','onhold','queue','getRealtimeAgentsQueryFields','jayson/promise','../../mysqldb','http','client','faxPause','mailPause','openchannelPause','voicePause','whatsappPause','every','filter','length','idle','unknown','unavailable','ringing','isNil','pause','chatStatus','faxStatus','openchannelStatus','smsStatus','voiceStatus','whatsappStatus','request','Agents','error','Unable\x20to\x20retrieve\x20RPC\x20agents\x20(AMI)','result','then','Unable\x20to\x20retrieve\x20RPC\x20agents\x20(Routing)','merge','catch','getRealtimeAgents','*pause','busy','getAgentStatus','isArray','Queue','map','getAgents','flatten','uniq','value','getAgentIdsByQueues','includes'];(function(_0x1fa3e2,_0x58020c){var _0x1261fc=function(_0x439e3b){while(--_0x439e3b){_0x1fa3e2['push'](_0x1fa3e2['shift']());}};_0x1261fc(++_0x58020c);}(_0x2ff5,0x1d5));var _0x52ff=function(_0xec24f8,_0x3ec72a){_0xec24f8=_0xec24f8-0x0;var _0x60f49f=_0x2ff5[_0xec24f8];return _0x60f49f;};'use strict';var _=require('lodash');var jayson=require(_0x52ff('0x0'));var db=require(_0x52ff('0x1'))['db'];var amiClient=jayson['client'][_0x52ff('0x2')]({'port':0x232a});var routingClient=jayson[_0x52ff('0x3')][_0x52ff('0x2')]({'port':0x232c});function isAgentPaused(_0x36b0e9,_0x190df8){var _0xbfba62=[_0x36b0e9['chatPause'],_0x36b0e9[_0x52ff('0x4')],_0x36b0e9[_0x52ff('0x5')],_0x36b0e9[_0x52ff('0x6')],_0x36b0e9['smsPause'],_0x36b0e9[_0x52ff('0x7')],_0x36b0e9[_0x52ff('0x8')]];if(!_0x190df8)return _[_0x52ff('0x9')](_0xbfba62);var _0x17ca0b=_[_0x52ff('0xa')](_0xbfba62);return _0x17ca0b[_0x52ff('0xb')]>0x0&&_0x17ca0b[_0x52ff('0xb')]<_0xbfba62[_0x52ff('0xb')]?!![]:![];}function isIdle(_0x5e127e){return _0x5e127e===_0x52ff('0xc')||_0x5e127e===_0x52ff('0xd')||_0x5e127e===_0x52ff('0xe')||_0x5e127e===_0x52ff('0xf')||_[_0x52ff('0x10')](_0x5e127e);}function isAgentIdle(_0x300409){if(_0x300409['voiceStatus']!=='idle'&&_0x300409['voicePause']){_0x300409['voiceStatus']=_0x52ff('0x11');return![];}return _['every']([_0x300409[_0x52ff('0x12')],_0x300409[_0x52ff('0x13')],_0x300409['mailStatus'],_0x300409[_0x52ff('0x14')],_0x300409[_0x52ff('0x15')],_0x300409[_0x52ff('0x16')],_0x300409[_0x52ff('0x17')]],isIdle);}function getRealtimeAgents(){return amiClient[_0x52ff('0x18')](_0x52ff('0x19'),{})['then'](function(_0x3a1a83){if(_0x3a1a83[_0x52ff('0x1a')]){throw new Error(_0x52ff('0x1b'));}var _0x4c83f=_0x3a1a83[_0x52ff('0x1c')];return routingClient[_0x52ff('0x18')]('Agents',{})[_0x52ff('0x1d')](function(_0x154419){if(_0x154419[_0x52ff('0x1a')]){throw new Error(_0x52ff('0x1e'));}var _0x5a87ac=_0x154419[_0x52ff('0x1c')];return _[_0x52ff('0x1f')]({},_0x4c83f,_0x5a87ac);});})[_0x52ff('0x20')](function(_0x58efb4){throw _0x58efb4;});}exports[_0x52ff('0x21')]=getRealtimeAgents;function getAgentStatus(_0x10ad04){var _0x2ba460=_0x52ff('0xd');if(isAgentPaused(_0x10ad04))_0x2ba460=_0x52ff('0x11');else if(isAgentPaused(_0x10ad04,!![]))_0x2ba460=_0x52ff('0x22');else if(isAgentIdle(_0x10ad04))_0x2ba460=_0x52ff('0xc');else _0x2ba460=_0x52ff('0x23');return _0x2ba460;}exports[_0x52ff('0x24')]=getAgentStatus;function getAgentIdsByQueues(_0x439011,_0x174a01){var _0xe68eb5=_[_0x52ff('0x25')](_0x439011)?_0x439011:[_0x439011];var _0x3e441b=_['startCase'](_0x174a01)+_0x52ff('0x26');var _0x27b209=_[_0x52ff('0x27')](_0xe68eb5,function(_0x3accd4){return db[_0x3e441b]['findOne']({'where':{'id':_0x3accd4}})[_0x52ff('0x1d')](function(_0xc68d3b){if(!_0xc68d3b)return[];return _0xc68d3b[_0x52ff('0x28')]({'attributes':['id'],'raw':!![]});});});return Promise['all'](_0x27b209)['then'](function(_0x1ac770){var _0x3f5e22=_(_0x1ac770)[_0x52ff('0x29')]()[_0x52ff('0x27')]('id')[_0x52ff('0x2a')]()[_0x52ff('0x2b')]();return _0x3f5e22;});}exports[_0x52ff('0x2c')]=getAgentIdsByQueues;function getAgentIdsByStatus(_0x322260,_0x1a706c,_0x3a049f){try{if(_0x1a706c[_0x52ff('0xb')]>0x0){_0x322260=_0x322260[_0x52ff('0xa')](function(_0x406343){return _0x1a706c[_0x52ff('0x2d')](_0x406343['id']);});}var _0x4e4571=_0x322260[_0x52ff('0xa')](function(_0x5cc35d){var _0x233a3e=_0x52ff('0x23');if(isAgentIdle(_0x5cc35d)){_0x233a3e='idle';}return _0x233a3e===_0x3a049f;});return _[_0x52ff('0x27')](_0x4e4571,'id');}catch(_0x326d23){throw _0x326d23;}}exports[_0x52ff('0x2e')]=getAgentIdsByStatus;function getSortedAgentsIdsByStatus(_0x2e6c9a,_0x50fd8b,_0x320c07){return Promise[_0x52ff('0x2f')]()['then'](function(){if(_0x50fd8b[_0x52ff('0xb')]>0x0){_0x2e6c9a=_0x2e6c9a[_0x52ff('0xa')](function(_0xef339d){return _0x50fd8b[_0x52ff('0x2d')](_0xef339d['id']);});}return _0x2e6c9a;})['then'](function(){return db[_0x52ff('0x30')][_0x52ff('0x31')]({'attributes':['id','name',_0x52ff('0x32'),_0x52ff('0x33'),_0x52ff('0x4'),_0x52ff('0x5'),'openchannelPause',_0x52ff('0x34'),'voicePause',_0x52ff('0x8'),_0x52ff('0x35')],'where':{'id':{'$in':_['map'](_0x2e6c9a,'id')}},'raw':!![]});})[_0x52ff('0x1d')](function(_0x10cfdd){return _0x10cfdd[_0x52ff('0x27')](function(_0x5e5ac1){var _0x4f3d97=_[_0x52ff('0x36')](_0x2e6c9a,['id',_0x5e5ac1['id']]);var _0x4b72cd=filterAgentAttributes(_0x4f3d97);_[_0x52ff('0x1f')](_0x5e5ac1,_0x4b72cd);_0x5e5ac1['globalStatus']=getAgentStatus(_0x5e5ac1);return _0x5e5ac1;});})[_0x52ff('0x1d')](function(_0x2a5653){var _0x59643f=sortAgentsByStatus(_0x2a5653,_0x320c07);return _[_0x52ff('0x27')](_0x59643f,'id');})[_0x52ff('0x20')](function(_0x2b38b7){throw _0x2b38b7;});}exports[_0x52ff('0x37')]=getSortedAgentsIdsByStatus;function getUpdatedSortOptions(_0xcb5b6c){var _0x3dc9bb={'fields':[],'order':null};var _0x45d059=_0xcb5b6c['split'](',');_0x3dc9bb[_0x52ff('0x38')]=_[_0x52ff('0xa')](_0x45d059,function(_0x422c95){if(!_0x422c95[_0x52ff('0x2d')](_0x52ff('0x39'))){return!![];}_0x3dc9bb[_0x52ff('0x3a')]=_[_0x52ff('0x3b')](_0x422c95,'-')?'DESC':_0x52ff('0x3c');return![];});return _0x3dc9bb;}exports[_0x52ff('0x3d')]=getUpdatedSortOptions;function validateSortParameter(_0xf2f766){if(!_0xf2f766){_0xf2f766='fullname,internal';}if(_0xf2f766['includes'](_0x52ff('0x32'))&&!_0xf2f766['includes'](_0x52ff('0x3e'))){var _0xbbfef8=_0xf2f766[_0x52ff('0x3f')](0x0,_0xf2f766[_0x52ff('0x40')](_0x52ff('0x32')));_0xf2f766+=','+_0xbbfef8+_0x52ff('0x3e');}return _0xf2f766;}exports[_0x52ff('0x41')]=validateSortParameter;function sortAgentsByStatus(_0x18a780,_0x379684){var _0x501a2f=_0x18a780[_0x52ff('0x42')](function(_0x1cf8ee,_0x5a5c37){if(_0x1cf8ee[_0x52ff('0x39')]===_0x5a5c37[_0x52ff('0x39')]){if(_0x1cf8ee[_0x52ff('0x39')]===_0x52ff('0x11')){if(_0x379684===_0x52ff('0x43')){return(_0x1cf8ee[_0x52ff('0x35')]||'')<(_0x5a5c37[_0x52ff('0x35')]||'')||_0x1cf8ee['fullname'][_0x52ff('0x44')]()>_0x5a5c37[_0x52ff('0x32')][_0x52ff('0x44')]()?0x1:-0x1;}else{return(_0x1cf8ee[_0x52ff('0x35')]||'')<(_0x5a5c37['pauseType']||'')||_0x1cf8ee['fullname'][_0x52ff('0x44')]()>_0x5a5c37[_0x52ff('0x32')][_0x52ff('0x44')]()?-0x1:0x1;}}else{if(_0x379684===_0x52ff('0x43')){return _0x1cf8ee[_0x52ff('0x32')]['toLowerCase']()<_0x5a5c37[_0x52ff('0x32')][_0x52ff('0x44')]()?0x1:-0x1;}else{return _0x1cf8ee[_0x52ff('0x32')][_0x52ff('0x44')]()<_0x5a5c37[_0x52ff('0x32')][_0x52ff('0x44')]()?-0x1:0x1;}}}else{if(_0x379684===_0x52ff('0x43')){return _0x1cf8ee[_0x52ff('0x39')]<_0x5a5c37[_0x52ff('0x39')]?0x1:-0x1;}else{return _0x1cf8ee[_0x52ff('0x39')]<_0x5a5c37['globalStatus']?-0x1:0x1;}}});return _0x501a2f;}exports[_0x52ff('0x45')]=sortAgentsByStatus;function filterAgentAttributes(_0xdcb5d){var _0x41930f=[_0x52ff('0x46'),_0x52ff('0x47'),'lastLoginAt',_0x52ff('0x48'),_0x52ff('0x35'),'lastPauseAt',_0x52ff('0x3e'),_0x52ff('0x49'),_0x52ff('0x33'),_0x52ff('0x6'),'mailPause','smsPause',_0x52ff('0x8'),_0x52ff('0x4'),_0x52ff('0x7'),_0x52ff('0x4a'),_0x52ff('0x4b'),_0x52ff('0x4c'),_0x52ff('0x4d'),'whatsappCapacity','faxCapacity',_0x52ff('0x4e'),_0x52ff('0x4f'),_0x52ff('0x50'),_0x52ff('0x51'),_0x52ff('0x52'),_0x52ff('0x53'),_0x52ff('0x12'),_0x52ff('0x14'),_0x52ff('0x54'),'smsStatus',_0x52ff('0x17'),_0x52ff('0x13'),_0x52ff('0x16'),_0x52ff('0x55'),_0x52ff('0x56'),'mailStatusTime',_0x52ff('0x57'),_0x52ff('0x58'),_0x52ff('0x59'),_0x52ff('0x5a'),'queue',_0x52ff('0x5b'),_0x52ff('0x5c'),_0x52ff('0x5d'),_0x52ff('0x5e')];var _0x4479c1=[_0x52ff('0x33'),'openchannelPause',_0x52ff('0x5'),'smsPause',_0x52ff('0x8'),_0x52ff('0x4'),_0x52ff('0x7'),_0x52ff('0x47')];var _0x4a1e3c=_(_0xdcb5d)[_0x52ff('0x5f')](_0x4479c1)['pick'](_0x41930f)[_0x52ff('0x2b')]();return _0x4a1e3c;}exports['filterAgentAttributes']=filterAgentAttributes;function getRealtimeAgentsQueryFields(){return['id','name',_0x52ff('0x32'),_0x52ff('0x60'),'internal',_0x52ff('0x47'),'globalStatus',_0x52ff('0x46'),'lastLoginAt',_0x52ff('0x35'),_0x52ff('0x48'),_0x52ff('0x4'),_0x52ff('0x33'),'mailPause',_0x52ff('0x6'),_0x52ff('0x34'),_0x52ff('0x7'),_0x52ff('0x8'),_0x52ff('0x12'),_0x52ff('0x13'),_0x52ff('0x54'),'openchannelStatus','smsStatus','voiceStatus','whatsappStatus',_0x52ff('0x55'),_0x52ff('0x59'),_0x52ff('0x61'),_0x52ff('0x56'),_0x52ff('0x57'),_0x52ff('0x5a'),'whatsappStatusTime',_0x52ff('0x4a'),_0x52ff('0x62'),_0x52ff('0x4c'),_0x52ff('0x4b'),_0x52ff('0x4d'),_0x52ff('0x63'),'chatCurrentCapacity',_0x52ff('0x53'),_0x52ff('0x50'),_0x52ff('0x4f'),_0x52ff('0x51'),_0x52ff('0x52'),_0x52ff('0x5d'),_0x52ff('0x5e'),_0x52ff('0x5b'),_0x52ff('0x64'),_0x52ff('0x65'),_0x52ff('0x5c')];}exports[_0x52ff('0x66')]=getRealtimeAgentsQueryFields;