Built motion from commit 9efc8423.|2.6.31
[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 _0x659c=['faxCapacity','chatCurrentCapacity','mailCurrentCapacity','whatsappCurrentCapacity','faxCurrentCapacity','mailStatus','openchannelStatusTime','mailStatusTime','smsStatusTime','faxStatusTime','voiceStatusTime','queue','destconnectedlinenum','screenrecording','phoneBarEnableScreenRecordingByAgent','phoneBarEnableAutomaticScreenRecording','omit','pick','filterAgentAttributes','role','chatStatusTime','mailCapacity','openchannelCurrentCapacity','smsCurrentCapacity','onhold','getRealtimeAgentsQueryFields','lodash','client','http','chatPause','faxPause','mailPause','openchannelPause','smsPause','voicePause','length','idle','unknown','unavailable','ringing','isNil','voiceStatus','chatStatus','faxStatus','openchannelStatus','smsStatus','whatsappStatus','request','Agents','then','error','Unable\x20to\x20retrieve\x20RPC\x20agents\x20(AMI)','result','merge','getRealtimeAgents','busy','getAgentStatus','isArray','map','findOne','getAgents','all','flatten','uniq','value','getAgentIdsByQueues','includes','filter','getAgentIdsByStatus','resolve','User','findAll','fullname','whatsappPause','find','globalStatus','catch','getSortedAgentsIdsByStatus','split','fields','order','DESC','fullname,internal','internal','substring','indexOf','validateSortParameter','pause','pauseType','toLowerCase','sortAgentsByStatus','userpic','online','lastLoginAt','lastPauseAt','address','chatCapacity','openchannelCapacity','smsCapacity','whatsappCapacity'];(function(_0xe11cf9,_0x588758){var _0x5e674f=function(_0x175cdf){while(--_0x175cdf){_0xe11cf9['push'](_0xe11cf9['shift']());}};_0x5e674f(++_0x588758);}(_0x659c,0x1aa));var _0xc659=function(_0x6a2228,_0x4a1d8b){_0x6a2228=_0x6a2228-0x0;var _0x2d7c81=_0x659c[_0x6a2228];return _0x2d7c81;};'use strict';var _=require(_0xc659('0x0'));var jayson=require('jayson/promise');var db=require('../../mysqldb')['db'];var amiClient=jayson[_0xc659('0x1')][_0xc659('0x2')]({'port':0x232a});var routingClient=jayson['client'][_0xc659('0x2')]({'port':0x232c});function isAgentPaused(_0x3c4438,_0x4b3258){var _0x5464e2=[_0x3c4438[_0xc659('0x3')],_0x3c4438[_0xc659('0x4')],_0x3c4438[_0xc659('0x5')],_0x3c4438[_0xc659('0x6')],_0x3c4438[_0xc659('0x7')],_0x3c4438[_0xc659('0x8')],_0x3c4438['whatsappPause']];if(!_0x4b3258)return _['every'](_0x5464e2);var _0x2cc93d=_['filter'](_0x5464e2);return _0x2cc93d['length']>0x0&&_0x2cc93d['length']<_0x5464e2[_0xc659('0x9')]?!![]:![];}function isIdle(_0x2ea975){return _0x2ea975===_0xc659('0xa')||_0x2ea975===_0xc659('0xb')||_0x2ea975===_0xc659('0xc')||_0x2ea975===_0xc659('0xd')||_[_0xc659('0xe')](_0x2ea975);}function isAgentIdle(_0x4267f3){if(_0x4267f3['voiceStatus']!==_0xc659('0xa')&&_0x4267f3[_0xc659('0x8')]){_0x4267f3[_0xc659('0xf')]='pause';return![];}return _['every']([_0x4267f3[_0xc659('0x10')],_0x4267f3[_0xc659('0x11')],_0x4267f3['mailStatus'],_0x4267f3[_0xc659('0x12')],_0x4267f3[_0xc659('0x13')],_0x4267f3[_0xc659('0xf')],_0x4267f3[_0xc659('0x14')]],isIdle);}function getRealtimeAgents(){return amiClient[_0xc659('0x15')](_0xc659('0x16'),{})[_0xc659('0x17')](function(_0x10b98a){if(_0x10b98a[_0xc659('0x18')]){throw new Error(_0xc659('0x19'));}var _0x57ec09=_0x10b98a[_0xc659('0x1a')];return routingClient[_0xc659('0x15')](_0xc659('0x16'),{})[_0xc659('0x17')](function(_0x28ad0d){if(_0x28ad0d[_0xc659('0x18')]){throw new Error('Unable\x20to\x20retrieve\x20RPC\x20agents\x20(Routing)');}var _0x2e421a=_0x28ad0d[_0xc659('0x1a')];return _[_0xc659('0x1b')]({},_0x57ec09,_0x2e421a);});})['catch'](function(_0x485da2){throw _0x485da2;});}exports[_0xc659('0x1c')]=getRealtimeAgents;function getAgentStatus(_0x6af6f9){var _0x48be52=_0xc659('0xb');if(isAgentPaused(_0x6af6f9))_0x48be52='pause';else if(isAgentPaused(_0x6af6f9,!![]))_0x48be52='*pause';else if(isAgentIdle(_0x6af6f9))_0x48be52='idle';else _0x48be52=_0xc659('0x1d');return _0x48be52;}exports[_0xc659('0x1e')]=getAgentStatus;function getAgentIdsByQueues(_0x3b3d95,_0x21374d){var _0x364f1f=_[_0xc659('0x1f')](_0x3b3d95)?_0x3b3d95:[_0x3b3d95];var _0x5971bb=_['startCase'](_0x21374d)+'Queue';var _0x14233e=_[_0xc659('0x20')](_0x364f1f,function(_0x4c0fb7){return db[_0x5971bb][_0xc659('0x21')]({'where':{'id':_0x4c0fb7}})['then'](function(_0x1b19c7){if(!_0x1b19c7)return[];return _0x1b19c7[_0xc659('0x22')]({'attributes':['id'],'raw':!![]});});});return Promise[_0xc659('0x23')](_0x14233e)[_0xc659('0x17')](function(_0x34aefc){var _0x22ec27=_(_0x34aefc)[_0xc659('0x24')]()[_0xc659('0x20')]('id')[_0xc659('0x25')]()[_0xc659('0x26')]();return _0x22ec27;});}exports[_0xc659('0x27')]=getAgentIdsByQueues;function getAgentIdsByStatus(_0x72ff75,_0x4a0a69,_0x271e8f){try{if(_0x4a0a69[_0xc659('0x9')]>0x0){_0x72ff75=_0x72ff75['filter'](function(_0x4962c7){return _0x4a0a69[_0xc659('0x28')](_0x4962c7['id']);});}var _0x29c5ea=_0x72ff75[_0xc659('0x29')](function(_0x3e6267){var _0x5bfbe4=_0xc659('0x1d');if(isAgentIdle(_0x3e6267)){_0x5bfbe4=_0xc659('0xa');}return _0x5bfbe4===_0x271e8f;});return _[_0xc659('0x20')](_0x29c5ea,'id');}catch(_0x4d5018){throw _0x4d5018;}}exports[_0xc659('0x2a')]=getAgentIdsByStatus;function getSortedAgentsIdsByStatus(_0x3dbc44,_0x492f57,_0x510b3e){return Promise[_0xc659('0x2b')]()['then'](function(){if(_0x492f57[_0xc659('0x9')]>0x0){_0x3dbc44=_0x3dbc44[_0xc659('0x29')](function(_0x55dca5){return _0x492f57[_0xc659('0x28')](_0x55dca5['id']);});}return _0x3dbc44;})[_0xc659('0x17')](function(){return db[_0xc659('0x2c')][_0xc659('0x2d')]({'attributes':['id','name',_0xc659('0x2e'),_0xc659('0x3'),'faxPause',_0xc659('0x5'),'openchannelPause',_0xc659('0x7'),'voicePause',_0xc659('0x2f'),'pauseType'],'where':{'id':{'$in':_[_0xc659('0x20')](_0x3dbc44,'id')}},'raw':!![]});})[_0xc659('0x17')](function(_0x3725d4){return _0x3725d4[_0xc659('0x20')](function(_0x105475){var _0x2596f1=_[_0xc659('0x30')](_0x3dbc44,['id',_0x105475['id']]);var _0x343b94=filterAgentAttributes(_0x2596f1);_[_0xc659('0x1b')](_0x105475,_0x343b94);_0x105475[_0xc659('0x31')]=getAgentStatus(_0x105475);return _0x105475;});})['then'](function(_0x54b04c){var _0x41b2a2=sortAgentsByStatus(_0x54b04c,_0x510b3e);return _[_0xc659('0x20')](_0x41b2a2,'id');})[_0xc659('0x32')](function(_0x5b6386){throw _0x5b6386;});}exports[_0xc659('0x33')]=getSortedAgentsIdsByStatus;function getUpdatedSortOptions(_0x20d36d){var _0x13c7c0={'fields':[],'order':null};var _0x143e5a=_0x20d36d[_0xc659('0x34')](',');_0x13c7c0[_0xc659('0x35')]=_['filter'](_0x143e5a,function(_0x536b69){if(!_0x536b69[_0xc659('0x28')](_0xc659('0x31'))){return!![];}_0x13c7c0[_0xc659('0x36')]=_['startsWith'](_0x536b69,'-')?_0xc659('0x37'):'ASC';return![];});return _0x13c7c0;}exports['getUpdatedSortOptions']=getUpdatedSortOptions;function validateSortParameter(_0x2c793f){if(!_0x2c793f){_0x2c793f=_0xc659('0x38');}if(_0x2c793f[_0xc659('0x28')](_0xc659('0x2e'))&&!_0x2c793f[_0xc659('0x28')](_0xc659('0x39'))){var _0x5f4fbd=_0x2c793f[_0xc659('0x3a')](0x0,_0x2c793f[_0xc659('0x3b')](_0xc659('0x2e')));_0x2c793f+=','+_0x5f4fbd+_0xc659('0x39');}return _0x2c793f;}exports[_0xc659('0x3c')]=validateSortParameter;function sortAgentsByStatus(_0x54677e,_0x39b0d1){var _0x1e7f26=_0x54677e['sort'](function(_0x43a8b4,_0x4b71c3){if(_0x43a8b4['globalStatus']===_0x4b71c3[_0xc659('0x31')]){if(_0x43a8b4['globalStatus']===_0xc659('0x3d')){if(_0x39b0d1===_0xc659('0x37')){return(_0x43a8b4[_0xc659('0x3e')]||'')<(_0x4b71c3[_0xc659('0x3e')]||'')||_0x43a8b4[_0xc659('0x2e')]['toLowerCase']()>_0x4b71c3[_0xc659('0x2e')]['toLowerCase']()?0x1:-0x1;}else{return(_0x43a8b4['pauseType']||'')<(_0x4b71c3['pauseType']||'')||_0x43a8b4[_0xc659('0x2e')][_0xc659('0x3f')]()>_0x4b71c3[_0xc659('0x2e')][_0xc659('0x3f')]()?-0x1:0x1;}}else{if(_0x39b0d1===_0xc659('0x37')){return _0x43a8b4[_0xc659('0x2e')]['toLowerCase']()<_0x4b71c3[_0xc659('0x2e')][_0xc659('0x3f')]()?0x1:-0x1;}else{return _0x43a8b4[_0xc659('0x2e')]['toLowerCase']()<_0x4b71c3[_0xc659('0x2e')][_0xc659('0x3f')]()?-0x1:0x1;}}}else{if(_0x39b0d1===_0xc659('0x37')){return _0x43a8b4['globalStatus']<_0x4b71c3[_0xc659('0x31')]?0x1:-0x1;}else{return _0x43a8b4[_0xc659('0x31')]<_0x4b71c3[_0xc659('0x31')]?-0x1:0x1;}}});return _0x1e7f26;}exports[_0xc659('0x40')]=sortAgentsByStatus;function filterAgentAttributes(_0x4c7d51){var _0xfbb4d3=[_0xc659('0x41'),_0xc659('0x42'),_0xc659('0x43'),_0xc659('0x44'),_0xc659('0x3e'),_0xc659('0x44'),_0xc659('0x39'),_0xc659('0x45'),_0xc659('0x3'),'openchannelPause','mailPause',_0xc659('0x7'),_0xc659('0x2f'),_0xc659('0x4'),_0xc659('0x8'),_0xc659('0x46'),_0xc659('0x47'),'mailCapacity',_0xc659('0x48'),_0xc659('0x49'),_0xc659('0x4a'),_0xc659('0x4b'),'openchannelCurrentCapacity',_0xc659('0x4c'),'smsCurrentCapacity',_0xc659('0x4d'),_0xc659('0x4e'),_0xc659('0x10'),_0xc659('0x12'),_0xc659('0x4f'),_0xc659('0x13'),_0xc659('0x14'),_0xc659('0x11'),_0xc659('0xf'),'chatStatusTime',_0xc659('0x50'),_0xc659('0x51'),_0xc659('0x52'),'whatsappStatusTime',_0xc659('0x53'),_0xc659('0x54'),_0xc659('0x55'),_0xc659('0x56'),_0xc659('0x57'),_0xc659('0x58'),_0xc659('0x59')];var _0x3849da=[_0xc659('0x3'),'openchannelPause',_0xc659('0x5'),'smsPause',_0xc659('0x2f'),_0xc659('0x4'),_0xc659('0x8'),_0xc659('0x42')];var _0x27908c=_(_0x4c7d51)[_0xc659('0x5a')](_0x3849da)[_0xc659('0x5b')](_0xfbb4d3)[_0xc659('0x26')]();return _0x27908c;}exports[_0xc659('0x5c')]=filterAgentAttributes;function getRealtimeAgentsQueryFields(){return['id','name',_0xc659('0x2e'),_0xc659('0x5d'),_0xc659('0x39'),_0xc659('0x42'),_0xc659('0x31'),_0xc659('0x41'),_0xc659('0x43'),_0xc659('0x3e'),_0xc659('0x44'),_0xc659('0x4'),_0xc659('0x3'),_0xc659('0x5'),'openchannelPause',_0xc659('0x7'),_0xc659('0x8'),'whatsappPause',_0xc659('0x10'),_0xc659('0x11'),_0xc659('0x4f'),_0xc659('0x12'),_0xc659('0x13'),_0xc659('0xf'),_0xc659('0x14'),_0xc659('0x5e'),_0xc659('0x53'),'mailStatusTime',_0xc659('0x50'),_0xc659('0x52'),_0xc659('0x54'),'whatsappStatusTime',_0xc659('0x46'),_0xc659('0x4a'),_0xc659('0x5f'),_0xc659('0x47'),_0xc659('0x48'),_0xc659('0x49'),_0xc659('0x4b'),'faxCurrentCapacity',_0xc659('0x4c'),_0xc659('0x60'),_0xc659('0x61'),'whatsappCurrentCapacity',_0xc659('0x58'),_0xc659('0x59'),_0xc659('0x56'),_0xc659('0x62'),_0xc659('0x55'),'screenrecording'];}exports[_0xc659('0x63')]=getRealtimeAgentsQueryFields;