Built motion from commit 0c5f406c.|2.6.29
[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 _0x9563=['isArray','startCase','Queue','map','findOne','getAgents','all','flatten','uniq','value','getAgentIdsByQueues','includes','getAgentIdsByStatus','resolve','User','findAll','fullname','pauseType','find','globalStatus','getSortedAgentsIdsByStatus','split','fields','startsWith','DESC','ASC','getUpdatedSortOptions','fullname,internal','internal','substring','toLowerCase','userpic','online','lastLoginAt','lastPauseAt','address','chatPause','chatCapacity','openchannelCapacity','mailCapacity','smsCapacity','faxCapacity','mailCurrentCapacity','smsCurrentCapacity','whatsappCurrentCapacity','faxCurrentCapacity','openchannelStatus','whatsappStatus','faxStatus','chatStatusTime','openchannelStatusTime','mailStatusTime','whatsappStatusTime','voiceStatusTime','queue','destconnectedlinenum','screenrecording','phoneBarEnableAutomaticScreenRecording','omit','pick','filterAgentAttributes','name','role','mailStatus','faxStatusTime','smsStatusTime','whatsappCapacity','openchannelCurrentCapacity','phoneBarEnableScreenRecordingByAgent','onhold','getRealtimeAgentsQueryFields','lodash','jayson/promise','client','http','faxPause','mailPause','openchannelPause','smsPause','whatsappPause','every','filter','length','idle','unknown','unavailable','isNil','voiceStatus','voicePause','pause','chatStatus','smsStatus','request','Agents','then','error','Unable\x20to\x20retrieve\x20RPC\x20agents\x20(AMI)','result','Unable\x20to\x20retrieve\x20RPC\x20agents\x20(Routing)','merge','catch','getRealtimeAgents','busy','getAgentStatus'];(function(_0x139468,_0x12f145){var _0x121e76=function(_0x16ded0){while(--_0x16ded0){_0x139468['push'](_0x139468['shift']());}};_0x121e76(++_0x12f145);}(_0x9563,0x1e7));var _0x3956=function(_0x17198c,_0x1d6134){_0x17198c=_0x17198c-0x0;var _0xac0b0d=_0x9563[_0x17198c];return _0xac0b0d;};'use strict';var _=require(_0x3956('0x0'));var jayson=require(_0x3956('0x1'));var db=require('../../mysqldb')['db'];var amiClient=jayson[_0x3956('0x2')][_0x3956('0x3')]({'port':0x232a});var routingClient=jayson[_0x3956('0x2')][_0x3956('0x3')]({'port':0x232c});function isAgentPaused(_0x5c89e9,_0x58382b){var _0x11ded2=[_0x5c89e9['chatPause'],_0x5c89e9[_0x3956('0x4')],_0x5c89e9[_0x3956('0x5')],_0x5c89e9[_0x3956('0x6')],_0x5c89e9[_0x3956('0x7')],_0x5c89e9['voicePause'],_0x5c89e9[_0x3956('0x8')]];if(!_0x58382b)return _[_0x3956('0x9')](_0x11ded2);var _0x2863cd=_[_0x3956('0xa')](_0x11ded2);return _0x2863cd[_0x3956('0xb')]>0x0&&_0x2863cd['length']<_0x11ded2['length']?!![]:![];}function isIdle(_0x3cc5cc){return _0x3cc5cc===_0x3956('0xc')||_0x3cc5cc===_0x3956('0xd')||_0x3cc5cc===_0x3956('0xe')||_0x3cc5cc==='ringing'||_[_0x3956('0xf')](_0x3cc5cc);}function isAgentIdle(_0x19019b){if(_0x19019b[_0x3956('0x10')]!==_0x3956('0xc')&&_0x19019b[_0x3956('0x11')]){_0x19019b[_0x3956('0x10')]=_0x3956('0x12');return![];}return _[_0x3956('0x9')]([_0x19019b[_0x3956('0x13')],_0x19019b['faxStatus'],_0x19019b['mailStatus'],_0x19019b['openchannelStatus'],_0x19019b[_0x3956('0x14')],_0x19019b[_0x3956('0x10')],_0x19019b['whatsappStatus']],isIdle);}function getRealtimeAgents(){return amiClient[_0x3956('0x15')](_0x3956('0x16'),{})[_0x3956('0x17')](function(_0x19bd9c){if(_0x19bd9c[_0x3956('0x18')]){throw new Error(_0x3956('0x19'));}var _0x429d28=_0x19bd9c[_0x3956('0x1a')];return routingClient[_0x3956('0x15')](_0x3956('0x16'),{})[_0x3956('0x17')](function(_0x189e6c){if(_0x189e6c[_0x3956('0x18')]){throw new Error(_0x3956('0x1b'));}var _0x3a90b2=_0x189e6c[_0x3956('0x1a')];return _[_0x3956('0x1c')]({},_0x429d28,_0x3a90b2);});})[_0x3956('0x1d')](function(_0x31552d){throw _0x31552d;});}exports[_0x3956('0x1e')]=getRealtimeAgents;function getAgentStatus(_0x4b9acc){var _0x3be8ab=_0x3956('0xd');if(isAgentPaused(_0x4b9acc))_0x3be8ab='pause';else if(isAgentPaused(_0x4b9acc,!![]))_0x3be8ab='*pause';else if(isAgentIdle(_0x4b9acc))_0x3be8ab='idle';else _0x3be8ab=_0x3956('0x1f');return _0x3be8ab;}exports[_0x3956('0x20')]=getAgentStatus;function getAgentIdsByQueues(_0x4e7e56,_0x2f9bb5){var _0x5b2fdd=_[_0x3956('0x21')](_0x4e7e56)?_0x4e7e56:[_0x4e7e56];var _0x49758f=_[_0x3956('0x22')](_0x2f9bb5)+_0x3956('0x23');var _0x1395d4=_[_0x3956('0x24')](_0x5b2fdd,function(_0x31b849){return db[_0x49758f][_0x3956('0x25')]({'where':{'id':_0x31b849}})['then'](function(_0x5a6339){if(!_0x5a6339)return[];return _0x5a6339[_0x3956('0x26')]({'attributes':['id'],'raw':!![]});});});return Promise[_0x3956('0x27')](_0x1395d4)['then'](function(_0x51a47c){var _0x10c67b=_(_0x51a47c)[_0x3956('0x28')]()['map']('id')[_0x3956('0x29')]()[_0x3956('0x2a')]();return _0x10c67b;});}exports[_0x3956('0x2b')]=getAgentIdsByQueues;function getAgentIdsByStatus(_0x98bbcf,_0x2a1bdd,_0x4389be){try{if(_0x2a1bdd['length']>0x0){_0x98bbcf=_0x98bbcf[_0x3956('0xa')](function(_0x222986){return _0x2a1bdd[_0x3956('0x2c')](_0x222986['id']);});}var _0x5e680c=_0x98bbcf[_0x3956('0xa')](function(_0x3956f5){var _0x113f5d='busy';if(isAgentIdle(_0x3956f5)){_0x113f5d=_0x3956('0xc');}return _0x113f5d===_0x4389be;});return _[_0x3956('0x24')](_0x5e680c,'id');}catch(_0x8d5bdb){throw _0x8d5bdb;}}exports[_0x3956('0x2d')]=getAgentIdsByStatus;function getSortedAgentsIdsByStatus(_0xef1b56,_0x2e60f9,_0x424f67){return Promise[_0x3956('0x2e')]()[_0x3956('0x17')](function(){if(_0x2e60f9[_0x3956('0xb')]>0x0){_0xef1b56=_0xef1b56['filter'](function(_0x17e769){return _0x2e60f9[_0x3956('0x2c')](_0x17e769['id']);});}return _0xef1b56;})['then'](function(){return db[_0x3956('0x2f')][_0x3956('0x30')]({'attributes':['id','name',_0x3956('0x31'),'chatPause',_0x3956('0x4'),_0x3956('0x5'),_0x3956('0x6'),'smsPause','voicePause',_0x3956('0x8'),_0x3956('0x32')],'where':{'id':{'$in':_['map'](_0xef1b56,'id')}},'raw':!![]});})[_0x3956('0x17')](function(_0x4f2eb1){return _0x4f2eb1[_0x3956('0x24')](function(_0x221eaf){var _0x265eb5=_[_0x3956('0x33')](_0xef1b56,['id',_0x221eaf['id']]);var _0x35e10e=filterAgentAttributes(_0x265eb5);_[_0x3956('0x1c')](_0x221eaf,_0x35e10e);_0x221eaf[_0x3956('0x34')]=getAgentStatus(_0x221eaf);return _0x221eaf;});})[_0x3956('0x17')](function(_0x5c6e67){var _0x2bbc96=sortAgentsByStatus(_0x5c6e67,_0x424f67);return _['map'](_0x2bbc96,'id');})[_0x3956('0x1d')](function(_0x389787){throw _0x389787;});}exports[_0x3956('0x35')]=getSortedAgentsIdsByStatus;function getUpdatedSortOptions(_0x2577bf){var _0x1d8604={'fields':[],'order':null};var _0x1980e6=_0x2577bf[_0x3956('0x36')](',');_0x1d8604[_0x3956('0x37')]=_[_0x3956('0xa')](_0x1980e6,function(_0x210eff){if(!_0x210eff[_0x3956('0x2c')](_0x3956('0x34'))){return!![];}_0x1d8604['order']=_[_0x3956('0x38')](_0x210eff,'-')?_0x3956('0x39'):_0x3956('0x3a');return![];});return _0x1d8604;}exports[_0x3956('0x3b')]=getUpdatedSortOptions;function validateSortParameter(_0x4145cf){if(!_0x4145cf){_0x4145cf=_0x3956('0x3c');}if(_0x4145cf[_0x3956('0x2c')](_0x3956('0x31'))&&!_0x4145cf['includes'](_0x3956('0x3d'))){var _0xbddef1=_0x4145cf[_0x3956('0x3e')](0x0,_0x4145cf['indexOf'](_0x3956('0x31')));_0x4145cf+=','+_0xbddef1+_0x3956('0x3d');}return _0x4145cf;}exports['validateSortParameter']=validateSortParameter;function sortAgentsByStatus(_0x4774c6,_0x267e1a){var _0x3ba20b=_0x4774c6['sort'](function(_0x116aef,_0x3e7e2e){if(_0x116aef['globalStatus']===_0x3e7e2e['globalStatus']){if(_0x116aef[_0x3956('0x34')]===_0x3956('0x12')){if(_0x267e1a===_0x3956('0x39')){return(_0x116aef[_0x3956('0x32')]||'')<(_0x3e7e2e[_0x3956('0x32')]||'')||_0x116aef['fullname'][_0x3956('0x3f')]()>_0x3e7e2e['fullname'][_0x3956('0x3f')]()?0x1:-0x1;}else{return(_0x116aef[_0x3956('0x32')]||'')<(_0x3e7e2e['pauseType']||'')||_0x116aef[_0x3956('0x31')]['toLowerCase']()>_0x3e7e2e[_0x3956('0x31')][_0x3956('0x3f')]()?-0x1:0x1;}}else{if(_0x267e1a===_0x3956('0x39')){return _0x116aef[_0x3956('0x31')][_0x3956('0x3f')]()<_0x3e7e2e[_0x3956('0x31')][_0x3956('0x3f')]()?0x1:-0x1;}else{return _0x116aef[_0x3956('0x31')][_0x3956('0x3f')]()<_0x3e7e2e['fullname'][_0x3956('0x3f')]()?-0x1:0x1;}}}else{if(_0x267e1a==='DESC'){return _0x116aef[_0x3956('0x34')]<_0x3e7e2e[_0x3956('0x34')]?0x1:-0x1;}else{return _0x116aef[_0x3956('0x34')]<_0x3e7e2e['globalStatus']?-0x1:0x1;}}});return _0x3ba20b;}exports['sortAgentsByStatus']=sortAgentsByStatus;function filterAgentAttributes(_0x4e628b){var _0x337f27=[_0x3956('0x40'),_0x3956('0x41'),_0x3956('0x42'),'lastPauseAt',_0x3956('0x32'),_0x3956('0x43'),_0x3956('0x3d'),_0x3956('0x44'),_0x3956('0x45'),_0x3956('0x6'),_0x3956('0x5'),_0x3956('0x7'),_0x3956('0x8'),'faxPause','voicePause',_0x3956('0x46'),_0x3956('0x47'),_0x3956('0x48'),_0x3956('0x49'),'whatsappCapacity',_0x3956('0x4a'),'chatCurrentCapacity','openchannelCurrentCapacity',_0x3956('0x4b'),_0x3956('0x4c'),_0x3956('0x4d'),_0x3956('0x4e'),_0x3956('0x13'),_0x3956('0x4f'),'mailStatus',_0x3956('0x14'),_0x3956('0x50'),_0x3956('0x51'),'voiceStatus',_0x3956('0x52'),_0x3956('0x53'),_0x3956('0x54'),'smsStatusTime',_0x3956('0x55'),'faxStatusTime',_0x3956('0x56'),_0x3956('0x57'),_0x3956('0x58'),_0x3956('0x59'),'phoneBarEnableScreenRecordingByAgent',_0x3956('0x5a')];var _0x37ae0d=[_0x3956('0x45'),_0x3956('0x6'),_0x3956('0x5'),_0x3956('0x7'),'whatsappPause',_0x3956('0x4'),_0x3956('0x11'),_0x3956('0x41')];var _0x42f699=_(_0x4e628b)[_0x3956('0x5b')](_0x37ae0d)[_0x3956('0x5c')](_0x337f27)[_0x3956('0x2a')]();return _0x42f699;}exports[_0x3956('0x5d')]=filterAgentAttributes;function getRealtimeAgentsQueryFields(){return['id',_0x3956('0x5e'),_0x3956('0x31'),_0x3956('0x5f'),_0x3956('0x3d'),'online',_0x3956('0x34'),'userpic','lastLoginAt',_0x3956('0x32'),_0x3956('0x43'),'faxPause',_0x3956('0x45'),_0x3956('0x5'),_0x3956('0x6'),'smsPause',_0x3956('0x11'),_0x3956('0x8'),'chatStatus','faxStatus',_0x3956('0x60'),_0x3956('0x4f'),_0x3956('0x14'),_0x3956('0x10'),_0x3956('0x50'),'chatStatusTime',_0x3956('0x61'),'mailStatusTime',_0x3956('0x53'),_0x3956('0x62'),_0x3956('0x56'),'whatsappStatusTime',_0x3956('0x46'),_0x3956('0x4a'),'mailCapacity',_0x3956('0x47'),'smsCapacity',_0x3956('0x63'),'chatCurrentCapacity',_0x3956('0x4e'),_0x3956('0x4b'),_0x3956('0x64'),_0x3956('0x4c'),'whatsappCurrentCapacity',_0x3956('0x65'),'phoneBarEnableAutomaticScreenRecording',_0x3956('0x58'),_0x3956('0x66'),_0x3956('0x57'),_0x3956('0x59')];}exports[_0x3956('0x67')]=getRealtimeAgentsQueryFields;