3e71fd77d3b012c2cc98094a444962e5967f5079
[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 _0xf567=['then','Unable\x20to\x20retrieve\x20RPC\x20agents\x20(Routing)','merge','catch','getRealtimeAgents','*pause','busy','getAgentStatus','isArray','Queue','map','findOne','flatten','uniq','getAgentIdsByQueues','includes','resolve','User','findAll','name','fullname','faxPause','pauseType','fields','order','startsWith','ASC','getUpdatedSortOptions','fullname,internal','internal','substring','indexOf','validateSortParameter','sort','globalStatus','toLowerCase','DESC','userpic','online','lastLoginAt','address','chatCapacity','mailCapacity','smsCapacity','whatsappCapacity','faxCapacity','chatCurrentCapacity','openchannelCurrentCapacity','mailCurrentCapacity','smsCurrentCapacity','whatsappCurrentCapacity','whatsappStatus','chatStatusTime','mailStatusTime','smsStatusTime','whatsappStatusTime','voiceStatusTime','queue','destconnectedlinenum','screenrecording','phoneBarEnableAutomaticScreenRecording','omit','pick','value','filterAgentAttributes','role','lastPauseAt','mailStatus','faxStatusTime','openchannelStatusTime','openchannelCapacity','faxCurrentCapacity','phoneBarEnableScreenRecordingByAgent','onhold','lodash','jayson/promise','../../mysqldb','http','client','chatPause','mailPause','openchannelPause','smsPause','whatsappPause','every','filter','length','idle','unknown','unavailable','ringing','isNil','voiceStatus','voicePause','pause','chatStatus','faxStatus','openchannelStatus','smsStatus','request','Agents','error','result'];(function(_0x171de5,_0x260fd1){var _0x1c2d68=function(_0x111adc){while(--_0x111adc){_0x171de5['push'](_0x171de5['shift']());}};_0x1c2d68(++_0x260fd1);}(_0xf567,0x118));var _0x7f56=function(_0x20a584,_0xe384a5){_0x20a584=_0x20a584-0x0;var _0x4b41a8=_0xf567[_0x20a584];return _0x4b41a8;};'use strict';var _=require(_0x7f56('0x0'));var jayson=require(_0x7f56('0x1'));var db=require(_0x7f56('0x2'))['db'];var amiClient=jayson['client'][_0x7f56('0x3')]({'port':0x232a});var routingClient=jayson[_0x7f56('0x4')]['http']({'port':0x232c});function isAgentPaused(_0x548b5b,_0x26ecb0){var _0x271ddb=[_0x548b5b[_0x7f56('0x5')],_0x548b5b['faxPause'],_0x548b5b[_0x7f56('0x6')],_0x548b5b[_0x7f56('0x7')],_0x548b5b[_0x7f56('0x8')],_0x548b5b['voicePause'],_0x548b5b[_0x7f56('0x9')]];if(!_0x26ecb0)return _[_0x7f56('0xa')](_0x271ddb);var _0x22d94e=_[_0x7f56('0xb')](_0x271ddb);return _0x22d94e[_0x7f56('0xc')]>0x0&&_0x22d94e[_0x7f56('0xc')]<_0x271ddb[_0x7f56('0xc')]?!![]:![];}function isIdle(_0x502a2b){return _0x502a2b===_0x7f56('0xd')||_0x502a2b===_0x7f56('0xe')||_0x502a2b===_0x7f56('0xf')||_0x502a2b===_0x7f56('0x10')||_[_0x7f56('0x11')](_0x502a2b);}function isAgentIdle(_0x2a10d9){if(_0x2a10d9[_0x7f56('0x12')]!==_0x7f56('0xd')&&_0x2a10d9[_0x7f56('0x13')]){_0x2a10d9[_0x7f56('0x12')]=_0x7f56('0x14');return![];}return _[_0x7f56('0xa')]([_0x2a10d9[_0x7f56('0x15')],_0x2a10d9[_0x7f56('0x16')],_0x2a10d9['mailStatus'],_0x2a10d9[_0x7f56('0x17')],_0x2a10d9[_0x7f56('0x18')],_0x2a10d9[_0x7f56('0x12')],_0x2a10d9['whatsappStatus']],isIdle);}function getRealtimeAgents(){return amiClient[_0x7f56('0x19')](_0x7f56('0x1a'),{})['then'](function(_0x5f9e3c){if(_0x5f9e3c[_0x7f56('0x1b')]){throw new Error('Unable\x20to\x20retrieve\x20RPC\x20agents\x20(AMI)');}var _0x5b2b27=_0x5f9e3c[_0x7f56('0x1c')];return routingClient['request'](_0x7f56('0x1a'),{})[_0x7f56('0x1d')](function(_0x5201eb){if(_0x5201eb[_0x7f56('0x1b')]){throw new Error(_0x7f56('0x1e'));}var _0x350c7a=_0x5201eb[_0x7f56('0x1c')];return _[_0x7f56('0x1f')]({},_0x5b2b27,_0x350c7a);});})[_0x7f56('0x20')](function(_0x4b1421){throw _0x4b1421;});}exports[_0x7f56('0x21')]=getRealtimeAgents;function getAgentStatus(_0x48b56a){var _0x179521='unknown';if(isAgentPaused(_0x48b56a))_0x179521=_0x7f56('0x14');else if(isAgentPaused(_0x48b56a,!![]))_0x179521=_0x7f56('0x22');else if(isAgentIdle(_0x48b56a))_0x179521=_0x7f56('0xd');else _0x179521=_0x7f56('0x23');return _0x179521;}exports[_0x7f56('0x24')]=getAgentStatus;function getAgentIdsByQueues(_0x4480b1,_0x9b7f15){var _0x3f54f8=_[_0x7f56('0x25')](_0x4480b1)?_0x4480b1:[_0x4480b1];var _0x36151d=_['startCase'](_0x9b7f15)+_0x7f56('0x26');var _0xe8f270=_[_0x7f56('0x27')](_0x3f54f8,function(_0x3c8658){return db[_0x36151d][_0x7f56('0x28')]({'where':{'id':_0x3c8658}})[_0x7f56('0x1d')](function(_0x100ea7){if(!_0x100ea7)return[];return _0x100ea7['getAgents']({'attributes':['id'],'raw':!![]});});});return Promise['all'](_0xe8f270)['then'](function(_0x29de7c){var _0x5d2c39=_(_0x29de7c)[_0x7f56('0x29')]()[_0x7f56('0x27')]('id')[_0x7f56('0x2a')]()['value']();return _0x5d2c39;});}exports[_0x7f56('0x2b')]=getAgentIdsByQueues;function getAgentIdsByStatus(_0x3d0faa,_0x3dbb41,_0xb8197f){try{if(_0x3dbb41['length']>0x0){_0x3d0faa=_0x3d0faa[_0x7f56('0xb')](function(_0x35d40b){return _0x3dbb41[_0x7f56('0x2c')](_0x35d40b['id']);});}var _0x57d1db=_0x3d0faa[_0x7f56('0xb')](function(_0x1f247e){var _0x5f1de2='busy';if(isAgentIdle(_0x1f247e)){_0x5f1de2='idle';}return _0x5f1de2===_0xb8197f;});return _[_0x7f56('0x27')](_0x57d1db,'id');}catch(_0x47e4ce){throw _0x47e4ce;}}exports['getAgentIdsByStatus']=getAgentIdsByStatus;function getSortedAgentsIdsByStatus(_0x25bd28,_0x56a70b,_0x290739){return Promise[_0x7f56('0x2d')]()[_0x7f56('0x1d')](function(){if(_0x56a70b['length']>0x0){_0x25bd28=_0x25bd28[_0x7f56('0xb')](function(_0x5052ce){return _0x56a70b[_0x7f56('0x2c')](_0x5052ce['id']);});}return _0x25bd28;})['then'](function(){return db[_0x7f56('0x2e')][_0x7f56('0x2f')]({'attributes':['id',_0x7f56('0x30'),_0x7f56('0x31'),_0x7f56('0x5'),_0x7f56('0x32'),_0x7f56('0x6'),_0x7f56('0x7'),_0x7f56('0x8'),_0x7f56('0x13'),_0x7f56('0x9'),_0x7f56('0x33')],'where':{'id':{'$in':_['map'](_0x25bd28,'id')}},'raw':!![]});})['then'](function(_0x26e48c){return _0x26e48c['map'](function(_0x222a08){var _0x39c0d7=_['find'](_0x25bd28,['id',_0x222a08['id']]);var _0x444c9b=filterAgentAttributes(_0x39c0d7);_[_0x7f56('0x1f')](_0x222a08,_0x444c9b);_0x222a08['globalStatus']=getAgentStatus(_0x222a08);return _0x222a08;});})[_0x7f56('0x1d')](function(_0x3e7a7e){var _0x584d1b=sortAgentsByStatus(_0x3e7a7e,_0x290739);return _[_0x7f56('0x27')](_0x584d1b,'id');})[_0x7f56('0x20')](function(_0x44dff2){throw _0x44dff2;});}exports['getSortedAgentsIdsByStatus']=getSortedAgentsIdsByStatus;function getUpdatedSortOptions(_0x20452d){var _0x58c1d6={'fields':[],'order':null};var _0x140176=_0x20452d['split'](',');_0x58c1d6[_0x7f56('0x34')]=_[_0x7f56('0xb')](_0x140176,function(_0x509c75){if(!_0x509c75['includes']('globalStatus')){return!![];}_0x58c1d6[_0x7f56('0x35')]=_[_0x7f56('0x36')](_0x509c75,'-')?'DESC':_0x7f56('0x37');return![];});return _0x58c1d6;}exports[_0x7f56('0x38')]=getUpdatedSortOptions;function validateSortParameter(_0x3af75a){if(!_0x3af75a){_0x3af75a=_0x7f56('0x39');}if(_0x3af75a[_0x7f56('0x2c')](_0x7f56('0x31'))&&!_0x3af75a[_0x7f56('0x2c')](_0x7f56('0x3a'))){var _0x453741=_0x3af75a[_0x7f56('0x3b')](0x0,_0x3af75a[_0x7f56('0x3c')](_0x7f56('0x31')));_0x3af75a+=','+_0x453741+_0x7f56('0x3a');}return _0x3af75a;}exports[_0x7f56('0x3d')]=validateSortParameter;function sortAgentsByStatus(_0x352456,_0xa7e287){var _0x40ee3f=_0x352456[_0x7f56('0x3e')](function(_0x1ec37b,_0xe02966){if(_0x1ec37b[_0x7f56('0x3f')]===_0xe02966[_0x7f56('0x3f')]){if(_0x1ec37b['globalStatus']===_0x7f56('0x14')){if(_0xa7e287==='DESC'){return(_0x1ec37b['pauseType']||'')<(_0xe02966[_0x7f56('0x33')]||'')||_0x1ec37b[_0x7f56('0x31')][_0x7f56('0x40')]()>_0xe02966[_0x7f56('0x31')][_0x7f56('0x40')]()?0x1:-0x1;}else{return(_0x1ec37b[_0x7f56('0x33')]||'')<(_0xe02966['pauseType']||'')||_0x1ec37b[_0x7f56('0x31')][_0x7f56('0x40')]()>_0xe02966['fullname'][_0x7f56('0x40')]()?-0x1:0x1;}}else{if(_0xa7e287===_0x7f56('0x41')){return _0x1ec37b[_0x7f56('0x31')][_0x7f56('0x40')]()<_0xe02966[_0x7f56('0x31')][_0x7f56('0x40')]()?0x1:-0x1;}else{return _0x1ec37b[_0x7f56('0x31')][_0x7f56('0x40')]()<_0xe02966[_0x7f56('0x31')][_0x7f56('0x40')]()?-0x1:0x1;}}}else{if(_0xa7e287===_0x7f56('0x41')){return _0x1ec37b['globalStatus']<_0xe02966['globalStatus']?0x1:-0x1;}else{return _0x1ec37b[_0x7f56('0x3f')]<_0xe02966[_0x7f56('0x3f')]?-0x1:0x1;}}});return _0x40ee3f;}exports['sortAgentsByStatus']=sortAgentsByStatus;function filterAgentAttributes(_0x3742af){var _0x4c5cc4=[_0x7f56('0x42'),_0x7f56('0x43'),_0x7f56('0x44'),'lastPauseAt',_0x7f56('0x33'),'lastPauseAt','internal',_0x7f56('0x45'),_0x7f56('0x5'),'openchannelPause',_0x7f56('0x6'),'smsPause',_0x7f56('0x9'),_0x7f56('0x32'),_0x7f56('0x13'),_0x7f56('0x46'),'openchannelCapacity',_0x7f56('0x47'),_0x7f56('0x48'),_0x7f56('0x49'),_0x7f56('0x4a'),_0x7f56('0x4b'),_0x7f56('0x4c'),_0x7f56('0x4d'),_0x7f56('0x4e'),_0x7f56('0x4f'),'faxCurrentCapacity','chatStatus',_0x7f56('0x17'),'mailStatus',_0x7f56('0x18'),_0x7f56('0x50'),'faxStatus',_0x7f56('0x12'),_0x7f56('0x51'),'openchannelStatusTime',_0x7f56('0x52'),_0x7f56('0x53'),_0x7f56('0x54'),'faxStatusTime',_0x7f56('0x55'),_0x7f56('0x56'),_0x7f56('0x57'),_0x7f56('0x58'),'phoneBarEnableScreenRecordingByAgent',_0x7f56('0x59')];var _0x47b14a=[_0x7f56('0x5'),_0x7f56('0x7'),_0x7f56('0x6'),_0x7f56('0x8'),_0x7f56('0x9'),_0x7f56('0x32'),_0x7f56('0x13'),_0x7f56('0x43')];var _0x3aa56a=_(_0x3742af)[_0x7f56('0x5a')](_0x47b14a)[_0x7f56('0x5b')](_0x4c5cc4)[_0x7f56('0x5c')]();return _0x3aa56a;}exports[_0x7f56('0x5d')]=filterAgentAttributes;function getRealtimeAgentsQueryFields(){return['id',_0x7f56('0x30'),_0x7f56('0x31'),_0x7f56('0x5e'),_0x7f56('0x3a'),_0x7f56('0x43'),'globalStatus',_0x7f56('0x42'),_0x7f56('0x44'),'pauseType',_0x7f56('0x5f'),'faxPause',_0x7f56('0x5'),'mailPause',_0x7f56('0x7'),'smsPause',_0x7f56('0x13'),_0x7f56('0x9'),_0x7f56('0x15'),'faxStatus',_0x7f56('0x60'),_0x7f56('0x17'),_0x7f56('0x18'),_0x7f56('0x12'),_0x7f56('0x50'),_0x7f56('0x51'),_0x7f56('0x61'),_0x7f56('0x52'),_0x7f56('0x62'),_0x7f56('0x53'),_0x7f56('0x55'),_0x7f56('0x54'),_0x7f56('0x46'),_0x7f56('0x4a'),_0x7f56('0x47'),_0x7f56('0x63'),_0x7f56('0x48'),_0x7f56('0x49'),_0x7f56('0x4b'),_0x7f56('0x64'),_0x7f56('0x4d'),_0x7f56('0x4c'),_0x7f56('0x4e'),_0x7f56('0x4f'),_0x7f56('0x65'),_0x7f56('0x59'),_0x7f56('0x57'),_0x7f56('0x66'),_0x7f56('0x56'),_0x7f56('0x58')];}exports['getRealtimeAgentsQueryFields']=getRealtimeAgentsQueryFields;