8083963bb979ef131d9b08176066136f8eb2e6a0
[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 _0x0164=['faxPause','mailPause','openchannelPause','voicePause','filter','length','idle','unknown','unavailable','ringing','isNil','voiceStatus','pause','faxStatus','mailStatus','openchannelStatus','smsStatus','whatsappStatus','Agents','then','error','result','request','merge','catch','getRealtimeAgents','busy','getAgentStatus','isArray','startCase','map','findOne','getAgents','all','uniq','value','getAgentIdsByQueues','includes','getAgentIdsByStatus','resolve','User','findAll','name','fullname','chatPause','smsPause','find','globalStatus','getSortedAgentsIdsByStatus','split','fields','order','startsWith','DESC','ASC','getUpdatedSortOptions','internal','substring','indexOf','validateSortParameter','pauseType','toLowerCase','sortAgentsByStatus','userpic','online','lastLoginAt','lastPauseAt','whatsappPause','mailCapacity','smsCapacity','whatsappCapacity','chatCurrentCapacity','chatStatus','chatStatusTime','openchannelStatusTime','mailStatusTime','whatsappStatusTime','faxStatusTime','voiceStatusTime','destconnectedlinenum','screenrecording','phoneBarEnableAutomaticScreenRecording','omit','pick','filterAgentAttributes','lodash','jayson/promise','../../mysqldb','http'];(function(_0x502c7d,_0x2f1285){var _0x30b7ed=function(_0x2d92f3){while(--_0x2d92f3){_0x502c7d['push'](_0x502c7d['shift']());}};_0x30b7ed(++_0x2f1285);}(_0x0164,0x160));var _0x4016=function(_0x4d07cc,_0x587613){_0x4d07cc=_0x4d07cc-0x0;var _0x42de3b=_0x0164[_0x4d07cc];return _0x42de3b;};'use strict';var _=require(_0x4016('0x0'));var jayson=require(_0x4016('0x1'));var db=require(_0x4016('0x2'))['db'];var amiClient=jayson['client'][_0x4016('0x3')]({'port':0x232a});var routingClient=jayson['client'][_0x4016('0x3')]({'port':0x232c});function isAgentPaused(_0x407b78,_0x42c678){var _0x216d60=[_0x407b78['chatPause'],_0x407b78[_0x4016('0x4')],_0x407b78[_0x4016('0x5')],_0x407b78[_0x4016('0x6')],_0x407b78['smsPause'],_0x407b78[_0x4016('0x7')],_0x407b78['whatsappPause']];if(!_0x42c678)return _['every'](_0x216d60);var _0x34f543=_[_0x4016('0x8')](_0x216d60);return _0x34f543[_0x4016('0x9')]>0x0&&_0x34f543[_0x4016('0x9')]<_0x216d60[_0x4016('0x9')]?!![]:![];}function isIdle(_0x5382a0){return _0x5382a0===_0x4016('0xa')||_0x5382a0===_0x4016('0xb')||_0x5382a0===_0x4016('0xc')||_0x5382a0===_0x4016('0xd')||_[_0x4016('0xe')](_0x5382a0);}function isAgentIdle(_0x3a8bfa){if(_0x3a8bfa[_0x4016('0xf')]!==_0x4016('0xa')&&_0x3a8bfa[_0x4016('0x7')]){_0x3a8bfa[_0x4016('0xf')]=_0x4016('0x10');return![];}return _['every']([_0x3a8bfa['chatStatus'],_0x3a8bfa[_0x4016('0x11')],_0x3a8bfa[_0x4016('0x12')],_0x3a8bfa[_0x4016('0x13')],_0x3a8bfa[_0x4016('0x14')],_0x3a8bfa['voiceStatus'],_0x3a8bfa[_0x4016('0x15')]],isIdle);}function getRealtimeAgents(){return amiClient['request'](_0x4016('0x16'),{})[_0x4016('0x17')](function(_0x5ea5c9){if(_0x5ea5c9[_0x4016('0x18')]){throw new Error('Unable\x20to\x20retrieve\x20RPC\x20agents\x20(AMI)');}var _0xf1ca1a=_0x5ea5c9[_0x4016('0x19')];return routingClient[_0x4016('0x1a')](_0x4016('0x16'),{})[_0x4016('0x17')](function(_0x38891e){if(_0x38891e[_0x4016('0x18')]){throw new Error('Unable\x20to\x20retrieve\x20RPC\x20agents\x20(Routing)');}var _0x43c96b=_0x38891e[_0x4016('0x19')];return _[_0x4016('0x1b')]({},_0xf1ca1a,_0x43c96b);});})[_0x4016('0x1c')](function(_0x2fc52f){throw _0x2fc52f;});}exports[_0x4016('0x1d')]=getRealtimeAgents;function getAgentStatus(_0x22d184){var _0x3b8921=_0x4016('0xb');if(isAgentPaused(_0x22d184))_0x3b8921=_0x4016('0x10');else if(isAgentPaused(_0x22d184,!![]))_0x3b8921='*pause';else if(isAgentIdle(_0x22d184))_0x3b8921='idle';else _0x3b8921=_0x4016('0x1e');return _0x3b8921;}exports[_0x4016('0x1f')]=getAgentStatus;function getAgentIdsByQueues(_0x5c357d,_0x1fd797){var _0x262af6=_[_0x4016('0x20')](_0x5c357d)?_0x5c357d:[_0x5c357d];var _0x3124bf=_[_0x4016('0x21')](_0x1fd797)+'Queue';var _0x5772e0=_[_0x4016('0x22')](_0x262af6,function(_0x26d710){return db[_0x3124bf][_0x4016('0x23')]({'where':{'id':_0x26d710}})[_0x4016('0x17')](function(_0x240bc1){if(!_0x240bc1)return[];return _0x240bc1[_0x4016('0x24')]({'attributes':['id'],'raw':!![]});});});return Promise[_0x4016('0x25')](_0x5772e0)[_0x4016('0x17')](function(_0xf190dd){var _0x591bce=_(_0xf190dd)['flatten']()['map']('id')[_0x4016('0x26')]()[_0x4016('0x27')]();return _0x591bce;});}exports[_0x4016('0x28')]=getAgentIdsByQueues;function getAgentIdsByStatus(_0x37273c,_0x19ac7,_0x190a57){try{if(_0x19ac7[_0x4016('0x9')]>0x0){_0x37273c=_0x37273c['filter'](function(_0x1a5de2){return _0x19ac7[_0x4016('0x29')](_0x1a5de2['id']);});}var _0x525d7e=_0x37273c['filter'](function(_0x5b9230){var _0x42dee8=_0x4016('0x1e');if(isAgentIdle(_0x5b9230)){_0x42dee8=_0x4016('0xa');}return _0x42dee8===_0x190a57;});return _['map'](_0x525d7e,'id');}catch(_0x5212e6){throw _0x5212e6;}}exports[_0x4016('0x2a')]=getAgentIdsByStatus;function getSortedAgentsIdsByStatus(_0x2bd7a3,_0xfcfa7,_0x15d0f1){return Promise[_0x4016('0x2b')]()['then'](function(){if(_0xfcfa7[_0x4016('0x9')]>0x0){_0x2bd7a3=_0x2bd7a3[_0x4016('0x8')](function(_0x497976){return _0xfcfa7[_0x4016('0x29')](_0x497976['id']);});}return _0x2bd7a3;})[_0x4016('0x17')](function(){return db[_0x4016('0x2c')][_0x4016('0x2d')]({'attributes':['id',_0x4016('0x2e'),_0x4016('0x2f'),_0x4016('0x30'),'faxPause','mailPause',_0x4016('0x6'),_0x4016('0x31'),'voicePause','whatsappPause','pauseType'],'where':{'id':{'$in':_[_0x4016('0x22')](_0x2bd7a3,'id')}},'raw':!![]});})[_0x4016('0x17')](function(_0x138185){return _0x138185[_0x4016('0x22')](function(_0x26b213){var _0x279c15=_[_0x4016('0x32')](_0x2bd7a3,['id',_0x26b213['id']]);var _0x91a0ee=filterAgentAttributes(_0x279c15);_['merge'](_0x26b213,_0x91a0ee);_0x26b213[_0x4016('0x33')]=getAgentStatus(_0x26b213);return _0x26b213;});})['then'](function(_0x8e4517){var _0x436adb=sortAgentsByStatus(_0x8e4517,_0x15d0f1);return _[_0x4016('0x22')](_0x436adb,'id');})[_0x4016('0x1c')](function(_0x2490b5){throw _0x2490b5;});}exports[_0x4016('0x34')]=getSortedAgentsIdsByStatus;function getUpdatedSortOptions(_0x22b9c0){var _0x15d845={'fields':[],'order':null};var _0x4e7473=_0x22b9c0[_0x4016('0x35')](',');_0x15d845[_0x4016('0x36')]=_[_0x4016('0x8')](_0x4e7473,function(_0x46b105){if(!_0x46b105[_0x4016('0x29')](_0x4016('0x33'))){return!![];}_0x15d845[_0x4016('0x37')]=_[_0x4016('0x38')](_0x46b105,'-')?_0x4016('0x39'):_0x4016('0x3a');return![];});return _0x15d845;}exports[_0x4016('0x3b')]=getUpdatedSortOptions;function validateSortParameter(_0x5ee115){if(_0x5ee115[_0x4016('0x29')]('fullname')&&!_0x5ee115[_0x4016('0x29')](_0x4016('0x3c'))){var _0x5b597e=_0x5ee115[_0x4016('0x3d')](0x0,_0x5ee115[_0x4016('0x3e')](_0x4016('0x2f')));_0x5ee115+=','+_0x5b597e+_0x4016('0x3c');}return _0x5ee115;}exports[_0x4016('0x3f')]=validateSortParameter;function sortAgentsByStatus(_0x4e7370,_0x404301){var _0x4af13d=_0x4e7370['sort'](function(_0x4b2d49,_0x2876f9){if(_0x4b2d49['globalStatus']===_0x2876f9[_0x4016('0x33')]){if(_0x4b2d49[_0x4016('0x33')]===_0x4016('0x10')){if(_0x404301===_0x4016('0x39')){return(_0x4b2d49[_0x4016('0x40')]||'')<(_0x2876f9['pauseType']||'')||_0x4b2d49['fullname'][_0x4016('0x41')]()>_0x2876f9[_0x4016('0x2f')]['toLowerCase']()?0x1:-0x1;}else{return(_0x4b2d49['pauseType']||'')<(_0x2876f9[_0x4016('0x40')]||'')||_0x4b2d49[_0x4016('0x2f')][_0x4016('0x41')]()>_0x2876f9['fullname'][_0x4016('0x41')]()?-0x1:0x1;}}else{if(_0x404301==='DESC'){return _0x4b2d49[_0x4016('0x2f')][_0x4016('0x41')]()<_0x2876f9[_0x4016('0x2f')][_0x4016('0x41')]()?0x1:-0x1;}else{return _0x4b2d49[_0x4016('0x2f')][_0x4016('0x41')]()<_0x2876f9[_0x4016('0x2f')][_0x4016('0x41')]()?-0x1:0x1;}}}else{if(_0x404301==='DESC'){return _0x4b2d49['globalStatus']<_0x2876f9[_0x4016('0x33')]?0x1:-0x1;}else{return _0x4b2d49[_0x4016('0x33')]<_0x2876f9[_0x4016('0x33')]?-0x1:0x1;}}});return _0x4af13d;}exports[_0x4016('0x42')]=sortAgentsByStatus;function filterAgentAttributes(_0x86db59){var _0x230205=[_0x4016('0x43'),_0x4016('0x44'),_0x4016('0x45'),'lastPauseAt','pauseType',_0x4016('0x46'),'internal','address',_0x4016('0x30'),'openchannelPause','mailPause',_0x4016('0x31'),_0x4016('0x47'),_0x4016('0x4'),_0x4016('0x7'),'chatCapacity','openchannelCapacity',_0x4016('0x48'),_0x4016('0x49'),_0x4016('0x4a'),'faxCapacity',_0x4016('0x4b'),'openchannelCurrentCapacity','mailCurrentCapacity','smsCurrentCapacity','whatsappCurrentCapacity','faxCurrentCapacity',_0x4016('0x4c'),_0x4016('0x13'),'mailStatus','smsStatus',_0x4016('0x15'),'faxStatus',_0x4016('0xf'),_0x4016('0x4d'),_0x4016('0x4e'),_0x4016('0x4f'),'smsStatusTime',_0x4016('0x50'),_0x4016('0x51'),_0x4016('0x52'),'queue',_0x4016('0x53'),_0x4016('0x54'),'phoneBarEnableScreenRecordingByAgent',_0x4016('0x55'),'routealias'];var _0x1508c7=[_0x4016('0x30'),_0x4016('0x6'),_0x4016('0x5'),_0x4016('0x31'),_0x4016('0x47'),_0x4016('0x4'),_0x4016('0x7'),_0x4016('0x44')];var _0x144522=_(_0x86db59)[_0x4016('0x56')](_0x1508c7)[_0x4016('0x57')](_0x230205)['value']();return _0x144522;}exports[_0x4016('0x58')]=filterAgentAttributes;