e312ba40ce94e3c288fe4a28e098a4c5fa593ab6
[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 _0xb114=['smsPause','voicePause','whatsappPause','every','length','idle','unknown','unavailable','ringing','voiceStatus','chatStatus','faxStatus','mailStatus','openchannelStatus','smsStatus','whatsappStatus','then','error','request','Agents','Unable\x20to\x20retrieve\x20RPC\x20agents\x20(Routing)','result','merge','catch','getRealtimeAgents','pause','busy','getAgentStatus','isArray','startCase','Queue','all','flatten','uniq','value','getAgentIdsByQueues','filter','includes','map','getAgentIdsByStatus','resolve','User','fullname','faxPause','pauseType','find','globalStatus','getSortedAgentsIdsByStatus','split','fields','order','startsWith','getUpdatedSortOptions','fullname,internal','internal','substring','indexOf','sort','toLowerCase','DESC','sortAgentsByStatus','online','lastLoginAt','lastPauseAt','chatCapacity','openchannelCapacity','mailCapacity','smsCapacity','whatsappCapacity','openchannelCurrentCapacity','mailCurrentCapacity','smsCurrentCapacity','whatsappCurrentCapacity','faxCurrentCapacity','chatStatusTime','openchannelStatusTime','smsStatusTime','faxStatusTime','voiceStatusTime','destconnectedlinenum','screenrecording','phoneBarEnableScreenRecordingByAgent','phoneBarEnableAutomaticScreenRecording','pick','filterAgentAttributes','name','role','whatsappStatusTime','onhold','queue','getRealtimeAgentsQueryFields','lodash','../../mysqldb','client','http','chatPause','mailPause','openchannelPause'];(function(_0xcc6812,_0x5cdc5b){var _0x1815ea=function(_0x5b340e){while(--_0x5b340e){_0xcc6812['push'](_0xcc6812['shift']());}};_0x1815ea(++_0x5cdc5b);}(_0xb114,0x11f));var _0x4b11=function(_0x8c43de,_0x90ff31){_0x8c43de=_0x8c43de-0x0;var _0xa6591b=_0xb114[_0x8c43de];return _0xa6591b;};'use strict';var _=require(_0x4b11('0x0'));var jayson=require('jayson/promise');var db=require(_0x4b11('0x1'))['db'];var amiClient=jayson[_0x4b11('0x2')]['http']({'port':0x232a});var routingClient=jayson[_0x4b11('0x2')][_0x4b11('0x3')]({'port':0x232c});function isAgentPaused(_0x1e2928,_0x12de1d){var _0x1ad72f=[_0x1e2928[_0x4b11('0x4')],_0x1e2928['faxPause'],_0x1e2928[_0x4b11('0x5')],_0x1e2928[_0x4b11('0x6')],_0x1e2928[_0x4b11('0x7')],_0x1e2928[_0x4b11('0x8')],_0x1e2928[_0x4b11('0x9')]];if(!_0x12de1d)return _[_0x4b11('0xa')](_0x1ad72f);var _0x25ff0a=_['filter'](_0x1ad72f);return _0x25ff0a[_0x4b11('0xb')]>0x0&&_0x25ff0a['length']<_0x1ad72f[_0x4b11('0xb')]?!![]:![];}function isIdle(_0x7f1ed0){return _0x7f1ed0===_0x4b11('0xc')||_0x7f1ed0===_0x4b11('0xd')||_0x7f1ed0===_0x4b11('0xe')||_0x7f1ed0===_0x4b11('0xf')||_['isNil'](_0x7f1ed0);}function isAgentIdle(_0x2ca996){if(_0x2ca996[_0x4b11('0x10')]!==_0x4b11('0xc')&&_0x2ca996[_0x4b11('0x8')]){_0x2ca996['voiceStatus']='pause';return![];}return _[_0x4b11('0xa')]([_0x2ca996[_0x4b11('0x11')],_0x2ca996[_0x4b11('0x12')],_0x2ca996[_0x4b11('0x13')],_0x2ca996[_0x4b11('0x14')],_0x2ca996[_0x4b11('0x15')],_0x2ca996[_0x4b11('0x10')],_0x2ca996[_0x4b11('0x16')]],isIdle);}function getRealtimeAgents(){return amiClient['request']('Agents',{})[_0x4b11('0x17')](function(_0x26c3e2){if(_0x26c3e2[_0x4b11('0x18')]){throw new Error('Unable\x20to\x20retrieve\x20RPC\x20agents\x20(AMI)');}var _0x4b5fac=_0x26c3e2['result'];return routingClient[_0x4b11('0x19')](_0x4b11('0x1a'),{})[_0x4b11('0x17')](function(_0xf14a7f){if(_0xf14a7f[_0x4b11('0x18')]){throw new Error(_0x4b11('0x1b'));}var _0x40579b=_0xf14a7f[_0x4b11('0x1c')];return _[_0x4b11('0x1d')]({},_0x4b5fac,_0x40579b);});})[_0x4b11('0x1e')](function(_0x50a977){throw _0x50a977;});}exports[_0x4b11('0x1f')]=getRealtimeAgents;function getAgentStatus(_0x15fc32){var _0x34b94f='unknown';if(isAgentPaused(_0x15fc32))_0x34b94f=_0x4b11('0x20');else if(isAgentPaused(_0x15fc32,!![]))_0x34b94f='*pause';else if(isAgentIdle(_0x15fc32))_0x34b94f=_0x4b11('0xc');else _0x34b94f=_0x4b11('0x21');return _0x34b94f;}exports[_0x4b11('0x22')]=getAgentStatus;function getAgentIdsByQueues(_0x12a7cc,_0x113a1e){var _0x28b219=_[_0x4b11('0x23')](_0x12a7cc)?_0x12a7cc:[_0x12a7cc];var _0x2c9f15=_[_0x4b11('0x24')](_0x113a1e)+_0x4b11('0x25');var _0x3e9a73=_['map'](_0x28b219,function(_0x34212b){return db[_0x2c9f15]['findOne']({'where':{'id':_0x34212b}})[_0x4b11('0x17')](function(_0x51ab68){if(!_0x51ab68)return[];return _0x51ab68['getAgents']({'attributes':['id'],'raw':!![]});});});return Promise[_0x4b11('0x26')](_0x3e9a73)[_0x4b11('0x17')](function(_0x39c860){var _0x347d32=_(_0x39c860)[_0x4b11('0x27')]()['map']('id')[_0x4b11('0x28')]()[_0x4b11('0x29')]();return _0x347d32;});}exports[_0x4b11('0x2a')]=getAgentIdsByQueues;function getAgentIdsByStatus(_0x3e099f,_0xcdd632,_0x222c42){try{if(_0xcdd632[_0x4b11('0xb')]>0x0){_0x3e099f=_0x3e099f[_0x4b11('0x2b')](function(_0x5897ea){return _0xcdd632[_0x4b11('0x2c')](_0x5897ea['id']);});}var _0x15d27d=_0x3e099f['filter'](function(_0x25e87f){var _0x37301d=_0x4b11('0x21');if(isAgentIdle(_0x25e87f)){_0x37301d=_0x4b11('0xc');}return _0x37301d===_0x222c42;});return _[_0x4b11('0x2d')](_0x15d27d,'id');}catch(_0x704ada){throw _0x704ada;}}exports[_0x4b11('0x2e')]=getAgentIdsByStatus;function getSortedAgentsIdsByStatus(_0x582d54,_0x409b01,_0x23941b){return Promise[_0x4b11('0x2f')]()['then'](function(){if(_0x409b01[_0x4b11('0xb')]>0x0){_0x582d54=_0x582d54[_0x4b11('0x2b')](function(_0x36f517){return _0x409b01[_0x4b11('0x2c')](_0x36f517['id']);});}return _0x582d54;})[_0x4b11('0x17')](function(){return db[_0x4b11('0x30')]['findAll']({'attributes':['id','name',_0x4b11('0x31'),_0x4b11('0x4'),_0x4b11('0x32'),_0x4b11('0x5'),_0x4b11('0x6'),_0x4b11('0x7'),_0x4b11('0x8'),'whatsappPause',_0x4b11('0x33')],'where':{'id':{'$in':_[_0x4b11('0x2d')](_0x582d54,'id')}},'raw':!![]});})[_0x4b11('0x17')](function(_0x24a8f8){return _0x24a8f8[_0x4b11('0x2d')](function(_0x5a7e1d){var _0x52db82=_[_0x4b11('0x34')](_0x582d54,['id',_0x5a7e1d['id']]);var _0x16f977=filterAgentAttributes(_0x52db82);_[_0x4b11('0x1d')](_0x5a7e1d,_0x16f977);_0x5a7e1d[_0x4b11('0x35')]=getAgentStatus(_0x5a7e1d);return _0x5a7e1d;});})['then'](function(_0x5332bd){var _0x4ae98c=sortAgentsByStatus(_0x5332bd,_0x23941b);return _[_0x4b11('0x2d')](_0x4ae98c,'id');})[_0x4b11('0x1e')](function(_0x55c05f){throw _0x55c05f;});}exports[_0x4b11('0x36')]=getSortedAgentsIdsByStatus;function getUpdatedSortOptions(_0x4a6735){var _0x2e6623={'fields':[],'order':null};var _0x1b3c26=_0x4a6735[_0x4b11('0x37')](',');_0x2e6623[_0x4b11('0x38')]=_[_0x4b11('0x2b')](_0x1b3c26,function(_0x27e2fc){if(!_0x27e2fc[_0x4b11('0x2c')](_0x4b11('0x35'))){return!![];}_0x2e6623[_0x4b11('0x39')]=_[_0x4b11('0x3a')](_0x27e2fc,'-')?'DESC':'ASC';return![];});return _0x2e6623;}exports[_0x4b11('0x3b')]=getUpdatedSortOptions;function validateSortParameter(_0x70a91){if(!_0x70a91){_0x70a91=_0x4b11('0x3c');}if(_0x70a91[_0x4b11('0x2c')](_0x4b11('0x31'))&&!_0x70a91[_0x4b11('0x2c')](_0x4b11('0x3d'))){var _0x34c285=_0x70a91[_0x4b11('0x3e')](0x0,_0x70a91[_0x4b11('0x3f')]('fullname'));_0x70a91+=','+_0x34c285+'internal';}return _0x70a91;}exports['validateSortParameter']=validateSortParameter;function sortAgentsByStatus(_0x24e664,_0x21b205){var _0x8f7bd7=_0x24e664[_0x4b11('0x40')](function(_0xb2ec0f,_0x2884f1){if(_0xb2ec0f[_0x4b11('0x35')]===_0x2884f1[_0x4b11('0x35')]){if(_0xb2ec0f[_0x4b11('0x35')]===_0x4b11('0x20')){if(_0x21b205==='DESC'){return(_0xb2ec0f[_0x4b11('0x33')]||'')<(_0x2884f1[_0x4b11('0x33')]||'')||_0xb2ec0f['fullname'][_0x4b11('0x41')]()>_0x2884f1[_0x4b11('0x31')][_0x4b11('0x41')]()?0x1:-0x1;}else{return(_0xb2ec0f[_0x4b11('0x33')]||'')<(_0x2884f1['pauseType']||'')||_0xb2ec0f[_0x4b11('0x31')][_0x4b11('0x41')]()>_0x2884f1['fullname'][_0x4b11('0x41')]()?-0x1:0x1;}}else{if(_0x21b205===_0x4b11('0x42')){return _0xb2ec0f['fullname'][_0x4b11('0x41')]()<_0x2884f1[_0x4b11('0x31')][_0x4b11('0x41')]()?0x1:-0x1;}else{return _0xb2ec0f['fullname'][_0x4b11('0x41')]()<_0x2884f1[_0x4b11('0x31')]['toLowerCase']()?-0x1:0x1;}}}else{if(_0x21b205===_0x4b11('0x42')){return _0xb2ec0f[_0x4b11('0x35')]<_0x2884f1[_0x4b11('0x35')]?0x1:-0x1;}else{return _0xb2ec0f[_0x4b11('0x35')]<_0x2884f1[_0x4b11('0x35')]?-0x1:0x1;}}});return _0x8f7bd7;}exports[_0x4b11('0x43')]=sortAgentsByStatus;function filterAgentAttributes(_0x3ba91f){var _0x5c76bf=['userpic',_0x4b11('0x44'),_0x4b11('0x45'),_0x4b11('0x46'),_0x4b11('0x33'),_0x4b11('0x46'),_0x4b11('0x3d'),'address',_0x4b11('0x4'),'openchannelPause',_0x4b11('0x5'),_0x4b11('0x7'),_0x4b11('0x9'),_0x4b11('0x32'),_0x4b11('0x8'),_0x4b11('0x47'),_0x4b11('0x48'),_0x4b11('0x49'),_0x4b11('0x4a'),_0x4b11('0x4b'),'faxCapacity','chatCurrentCapacity',_0x4b11('0x4c'),_0x4b11('0x4d'),_0x4b11('0x4e'),_0x4b11('0x4f'),_0x4b11('0x50'),_0x4b11('0x11'),'openchannelStatus','mailStatus','smsStatus',_0x4b11('0x16'),_0x4b11('0x12'),'voiceStatus',_0x4b11('0x51'),_0x4b11('0x52'),'mailStatusTime',_0x4b11('0x53'),'whatsappStatusTime',_0x4b11('0x54'),_0x4b11('0x55'),'queue',_0x4b11('0x56'),_0x4b11('0x57'),_0x4b11('0x58'),_0x4b11('0x59')];var _0x5071f3=['chatPause','openchannelPause',_0x4b11('0x5'),'smsPause',_0x4b11('0x9'),_0x4b11('0x32'),'voicePause','online'];var _0x303e15=_(_0x3ba91f)['omit'](_0x5071f3)[_0x4b11('0x5a')](_0x5c76bf)[_0x4b11('0x29')]();return _0x303e15;}exports[_0x4b11('0x5b')]=filterAgentAttributes;function getRealtimeAgentsQueryFields(){return['id',_0x4b11('0x5c'),_0x4b11('0x31'),_0x4b11('0x5d'),_0x4b11('0x3d'),'online',_0x4b11('0x35'),'userpic',_0x4b11('0x45'),_0x4b11('0x33'),_0x4b11('0x46'),_0x4b11('0x32'),_0x4b11('0x4'),_0x4b11('0x5'),_0x4b11('0x6'),'smsPause',_0x4b11('0x8'),'whatsappPause',_0x4b11('0x11'),_0x4b11('0x12'),_0x4b11('0x13'),_0x4b11('0x14'),_0x4b11('0x15'),_0x4b11('0x10'),_0x4b11('0x16'),_0x4b11('0x51'),_0x4b11('0x54'),'mailStatusTime',_0x4b11('0x52'),_0x4b11('0x53'),_0x4b11('0x55'),_0x4b11('0x5e'),'chatCapacity','faxCapacity',_0x4b11('0x49'),_0x4b11('0x48'),_0x4b11('0x4a'),_0x4b11('0x4b'),'chatCurrentCapacity',_0x4b11('0x50'),_0x4b11('0x4d'),_0x4b11('0x4c'),_0x4b11('0x4e'),'whatsappCurrentCapacity','phoneBarEnableScreenRecordingByAgent',_0x4b11('0x59'),_0x4b11('0x56'),_0x4b11('0x5f'),_0x4b11('0x60'),_0x4b11('0x57')];}exports[_0x4b11('0x61')]=getRealtimeAgentsQueryFields;