91e53c7423c7138e80fcf99b664b82ab4fbcf4bf
[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 _0x3f8a=['includes','order','startsWith','DESC','getUpdatedSortOptions','substring','indexOf','internal','sort','globalStatus','toLowerCase','sortAgentsByStatus','userpic','online','lastLoginAt','lastPauseAt','address','chatCapacity','openchannelCapacity','mailCapacity','smsCapacity','whatsappCapacity','faxCapacity','chatCurrentCapacity','openchannelCurrentCapacity','mailCurrentCapacity','smsCurrentCapacity','whatsappCurrentCapacity','faxCurrentCapacity','smsStatus','chatStatusTime','mailStatusTime','smsStatusTime','whatsappStatusTime','faxStatusTime','voiceStatusTime','queue','destconnectedlinenum','screenrecording','phoneBarEnableScreenRecordingByAgent','phoneBarEnableAutomaticScreenRecording','routealias','omit','pick','filterAgentAttributes','role','openchannelStatusTime','onhold','getRealtimeAgentsQueryFields','lodash','jayson/promise','../../mysqldb','client','http','chatPause','faxPause','mailPause','openchannelPause','smsPause','voicePause','whatsappPause','every','filter','length','idle','unknown','unavailable','isNil','voiceStatus','pause','chatStatus','faxStatus','mailStatus','openchannelStatus','whatsappStatus','request','Agents','error','Unable\x20to\x20retrieve\x20RPC\x20agents\x20(AMI)','result','then','merge','catch','getRealtimeAgents','busy','getAgentStatus','isArray','startCase','map','findOne','getAgents','all','flatten','uniq','getAgentIdsByQueues','getAgentIdsByStatus','resolve','User','name','fullname','pauseType','find','getSortedAgentsIdsByStatus','split','fields'];(function(_0xbdbdf0,_0x16aec3){var _0x1097df=function(_0x4361bb){while(--_0x4361bb){_0xbdbdf0['push'](_0xbdbdf0['shift']());}};_0x1097df(++_0x16aec3);}(_0x3f8a,0x1d5));var _0xa3f8=function(_0x38a052,_0x33f0af){_0x38a052=_0x38a052-0x0;var _0x489892=_0x3f8a[_0x38a052];return _0x489892;};'use strict';var _=require(_0xa3f8('0x0'));var jayson=require(_0xa3f8('0x1'));var db=require(_0xa3f8('0x2'))['db'];var amiClient=jayson[_0xa3f8('0x3')]['http']({'port':0x232a});var routingClient=jayson[_0xa3f8('0x3')][_0xa3f8('0x4')]({'port':0x232c});function isAgentPaused(_0x928738,_0x15b6aa){var _0x2553fd=[_0x928738[_0xa3f8('0x5')],_0x928738[_0xa3f8('0x6')],_0x928738[_0xa3f8('0x7')],_0x928738[_0xa3f8('0x8')],_0x928738[_0xa3f8('0x9')],_0x928738[_0xa3f8('0xa')],_0x928738[_0xa3f8('0xb')]];if(!_0x15b6aa)return _[_0xa3f8('0xc')](_0x2553fd);var _0x2448de=_[_0xa3f8('0xd')](_0x2553fd);return _0x2448de[_0xa3f8('0xe')]>0x0&&_0x2448de[_0xa3f8('0xe')]<_0x2553fd['length']?!![]:![];}function isIdle(_0x47496a){return _0x47496a===_0xa3f8('0xf')||_0x47496a===_0xa3f8('0x10')||_0x47496a===_0xa3f8('0x11')||_0x47496a==='ringing'||_[_0xa3f8('0x12')](_0x47496a);}function isAgentIdle(_0x26f0a0){if(_0x26f0a0[_0xa3f8('0x13')]!==_0xa3f8('0xf')&&_0x26f0a0[_0xa3f8('0xa')]){_0x26f0a0[_0xa3f8('0x13')]=_0xa3f8('0x14');return![];}return _[_0xa3f8('0xc')]([_0x26f0a0[_0xa3f8('0x15')],_0x26f0a0[_0xa3f8('0x16')],_0x26f0a0[_0xa3f8('0x17')],_0x26f0a0[_0xa3f8('0x18')],_0x26f0a0['smsStatus'],_0x26f0a0[_0xa3f8('0x13')],_0x26f0a0[_0xa3f8('0x19')]],isIdle);}function getRealtimeAgents(){return amiClient[_0xa3f8('0x1a')](_0xa3f8('0x1b'),{})['then'](function(_0x39c450){if(_0x39c450[_0xa3f8('0x1c')]){throw new Error(_0xa3f8('0x1d'));}var _0x8cd130=_0x39c450[_0xa3f8('0x1e')];return routingClient[_0xa3f8('0x1a')](_0xa3f8('0x1b'),{})[_0xa3f8('0x1f')](function(_0x2cb49f){if(_0x2cb49f[_0xa3f8('0x1c')]){throw new Error('Unable\x20to\x20retrieve\x20RPC\x20agents\x20(Routing)');}var _0x3ad8cd=_0x2cb49f['result'];return _[_0xa3f8('0x20')]({},_0x8cd130,_0x3ad8cd);});})[_0xa3f8('0x21')](function(_0x191f99){throw _0x191f99;});}exports[_0xa3f8('0x22')]=getRealtimeAgents;function getAgentStatus(_0x5c916e){var _0x1f0bb8='unknown';if(isAgentPaused(_0x5c916e))_0x1f0bb8=_0xa3f8('0x14');else if(isAgentPaused(_0x5c916e,!![]))_0x1f0bb8='*pause';else if(isAgentIdle(_0x5c916e))_0x1f0bb8=_0xa3f8('0xf');else _0x1f0bb8=_0xa3f8('0x23');return _0x1f0bb8;}exports[_0xa3f8('0x24')]=getAgentStatus;function getAgentIdsByQueues(_0x474893,_0x3a6a57){var _0x1e5f30=_[_0xa3f8('0x25')](_0x474893)?_0x474893:[_0x474893];var _0x490717=_[_0xa3f8('0x26')](_0x3a6a57)+'Queue';var _0x3ce57b=_[_0xa3f8('0x27')](_0x1e5f30,function(_0x24985a){return db[_0x490717][_0xa3f8('0x28')]({'where':{'id':_0x24985a}})[_0xa3f8('0x1f')](function(_0x2c5d10){if(!_0x2c5d10)return[];return _0x2c5d10[_0xa3f8('0x29')]({'attributes':['id'],'raw':!![]});});});return Promise[_0xa3f8('0x2a')](_0x3ce57b)[_0xa3f8('0x1f')](function(_0x1eb2b9){var _0x545a28=_(_0x1eb2b9)[_0xa3f8('0x2b')]()[_0xa3f8('0x27')]('id')[_0xa3f8('0x2c')]()['value']();return _0x545a28;});}exports[_0xa3f8('0x2d')]=getAgentIdsByQueues;function getAgentIdsByStatus(_0x57f6be,_0xbad6a6,_0x3a3746){try{if(_0xbad6a6[_0xa3f8('0xe')]>0x0){_0x57f6be=_0x57f6be['filter'](function(_0x458824){return _0xbad6a6['includes'](_0x458824['id']);});}var _0x21c57e=_0x57f6be[_0xa3f8('0xd')](function(_0x226153){var _0x406140=_0xa3f8('0x23');if(isAgentIdle(_0x226153)){_0x406140=_0xa3f8('0xf');}return _0x406140===_0x3a3746;});return _[_0xa3f8('0x27')](_0x21c57e,'id');}catch(_0x424a3e){throw _0x424a3e;}}exports[_0xa3f8('0x2e')]=getAgentIdsByStatus;function getSortedAgentsIdsByStatus(_0x290a4e,_0x6e83b0,_0x57c465){return Promise[_0xa3f8('0x2f')]()[_0xa3f8('0x1f')](function(){if(_0x6e83b0[_0xa3f8('0xe')]>0x0){_0x290a4e=_0x290a4e[_0xa3f8('0xd')](function(_0x3bb4b0){return _0x6e83b0['includes'](_0x3bb4b0['id']);});}return _0x290a4e;})[_0xa3f8('0x1f')](function(){return db[_0xa3f8('0x30')]['findAll']({'attributes':['id',_0xa3f8('0x31'),_0xa3f8('0x32'),'chatPause',_0xa3f8('0x6'),'mailPause','openchannelPause',_0xa3f8('0x9'),_0xa3f8('0xa'),'whatsappPause',_0xa3f8('0x33')],'where':{'id':{'$in':_['map'](_0x290a4e,'id')}},'raw':!![]});})[_0xa3f8('0x1f')](function(_0x30a972){return _0x30a972[_0xa3f8('0x27')](function(_0x22e8ea){var _0x52a80b=_[_0xa3f8('0x34')](_0x290a4e,['id',_0x22e8ea['id']]);var _0x53c7b2=filterAgentAttributes(_0x52a80b);_[_0xa3f8('0x20')](_0x22e8ea,_0x53c7b2);_0x22e8ea['globalStatus']=getAgentStatus(_0x22e8ea);return _0x22e8ea;});})[_0xa3f8('0x1f')](function(_0x5f459a){var _0x5eca3c=sortAgentsByStatus(_0x5f459a,_0x57c465);return _['map'](_0x5eca3c,'id');})[_0xa3f8('0x21')](function(_0x1674f1){throw _0x1674f1;});}exports[_0xa3f8('0x35')]=getSortedAgentsIdsByStatus;function getUpdatedSortOptions(_0x2ffeb3){var _0x4b93ba={'fields':[],'order':null};var _0x447824=_0x2ffeb3[_0xa3f8('0x36')](',');_0x4b93ba[_0xa3f8('0x37')]=_[_0xa3f8('0xd')](_0x447824,function(_0x238da1){if(!_0x238da1[_0xa3f8('0x38')]('globalStatus')){return!![];}_0x4b93ba[_0xa3f8('0x39')]=_[_0xa3f8('0x3a')](_0x238da1,'-')?_0xa3f8('0x3b'):'ASC';return![];});return _0x4b93ba;}exports[_0xa3f8('0x3c')]=getUpdatedSortOptions;function validateSortParameter(_0x110b23){if(!_0x110b23){_0x110b23='fullname,internal';}if(_0x110b23[_0xa3f8('0x38')]('fullname')&&!_0x110b23[_0xa3f8('0x38')]('internal')){var _0x9ede8d=_0x110b23[_0xa3f8('0x3d')](0x0,_0x110b23[_0xa3f8('0x3e')](_0xa3f8('0x32')));_0x110b23+=','+_0x9ede8d+_0xa3f8('0x3f');}return _0x110b23;}exports['validateSortParameter']=validateSortParameter;function sortAgentsByStatus(_0xa6f0a4,_0x88108){var _0x4db9c5=_0xa6f0a4[_0xa3f8('0x40')](function(_0x4e13f1,_0x6ff4d4){if(_0x4e13f1[_0xa3f8('0x41')]===_0x6ff4d4[_0xa3f8('0x41')]){if(_0x4e13f1[_0xa3f8('0x41')]===_0xa3f8('0x14')){if(_0x88108==='DESC'){return(_0x4e13f1[_0xa3f8('0x33')]||'')<(_0x6ff4d4[_0xa3f8('0x33')]||'')||_0x4e13f1[_0xa3f8('0x32')][_0xa3f8('0x42')]()>_0x6ff4d4[_0xa3f8('0x32')]['toLowerCase']()?0x1:-0x1;}else{return(_0x4e13f1[_0xa3f8('0x33')]||'')<(_0x6ff4d4[_0xa3f8('0x33')]||'')||_0x4e13f1[_0xa3f8('0x32')][_0xa3f8('0x42')]()>_0x6ff4d4[_0xa3f8('0x32')][_0xa3f8('0x42')]()?-0x1:0x1;}}else{if(_0x88108===_0xa3f8('0x3b')){return _0x4e13f1[_0xa3f8('0x32')][_0xa3f8('0x42')]()<_0x6ff4d4[_0xa3f8('0x32')][_0xa3f8('0x42')]()?0x1:-0x1;}else{return _0x4e13f1[_0xa3f8('0x32')][_0xa3f8('0x42')]()<_0x6ff4d4['fullname']['toLowerCase']()?-0x1:0x1;}}}else{if(_0x88108==='DESC'){return _0x4e13f1[_0xa3f8('0x41')]<_0x6ff4d4[_0xa3f8('0x41')]?0x1:-0x1;}else{return _0x4e13f1[_0xa3f8('0x41')]<_0x6ff4d4[_0xa3f8('0x41')]?-0x1:0x1;}}});return _0x4db9c5;}exports[_0xa3f8('0x43')]=sortAgentsByStatus;function filterAgentAttributes(_0x33a141){var _0x4a751f=[_0xa3f8('0x44'),_0xa3f8('0x45'),_0xa3f8('0x46'),_0xa3f8('0x47'),'pauseType',_0xa3f8('0x47'),_0xa3f8('0x3f'),_0xa3f8('0x48'),'chatPause','openchannelPause','mailPause',_0xa3f8('0x9'),_0xa3f8('0xb'),'faxPause',_0xa3f8('0xa'),_0xa3f8('0x49'),_0xa3f8('0x4a'),_0xa3f8('0x4b'),_0xa3f8('0x4c'),_0xa3f8('0x4d'),_0xa3f8('0x4e'),_0xa3f8('0x4f'),_0xa3f8('0x50'),_0xa3f8('0x51'),_0xa3f8('0x52'),_0xa3f8('0x53'),_0xa3f8('0x54'),_0xa3f8('0x15'),_0xa3f8('0x18'),_0xa3f8('0x17'),_0xa3f8('0x55'),'whatsappStatus','faxStatus',_0xa3f8('0x13'),_0xa3f8('0x56'),'openchannelStatusTime',_0xa3f8('0x57'),_0xa3f8('0x58'),_0xa3f8('0x59'),_0xa3f8('0x5a'),_0xa3f8('0x5b'),_0xa3f8('0x5c'),_0xa3f8('0x5d'),_0xa3f8('0x5e'),_0xa3f8('0x5f'),_0xa3f8('0x60'),_0xa3f8('0x61')];var _0x3752fa=[_0xa3f8('0x5'),_0xa3f8('0x8'),_0xa3f8('0x7'),_0xa3f8('0x9'),'whatsappPause','faxPause',_0xa3f8('0xa'),'online'];var _0x2410aa=_(_0x33a141)[_0xa3f8('0x62')](_0x3752fa)[_0xa3f8('0x63')](_0x4a751f)['value']();return _0x2410aa;}exports[_0xa3f8('0x64')]=filterAgentAttributes;function getRealtimeAgentsQueryFields(){return['id',_0xa3f8('0x31'),_0xa3f8('0x32'),_0xa3f8('0x65'),'internal',_0xa3f8('0x45'),'globalStatus',_0xa3f8('0x44'),_0xa3f8('0x46'),_0xa3f8('0x33'),'lastPauseAt','faxPause',_0xa3f8('0x5'),_0xa3f8('0x7'),_0xa3f8('0x8'),_0xa3f8('0x9'),'voicePause',_0xa3f8('0xb'),_0xa3f8('0x15'),_0xa3f8('0x16'),_0xa3f8('0x17'),_0xa3f8('0x18'),'smsStatus','voiceStatus',_0xa3f8('0x19'),_0xa3f8('0x56'),_0xa3f8('0x5a'),_0xa3f8('0x57'),_0xa3f8('0x66'),_0xa3f8('0x58'),_0xa3f8('0x5b'),'whatsappStatusTime','chatCapacity','faxCapacity',_0xa3f8('0x4b'),_0xa3f8('0x4a'),_0xa3f8('0x4c'),_0xa3f8('0x4d'),_0xa3f8('0x4f'),_0xa3f8('0x54'),_0xa3f8('0x51'),_0xa3f8('0x50'),'smsCurrentCapacity',_0xa3f8('0x53'),_0xa3f8('0x5f'),_0xa3f8('0x60'),_0xa3f8('0x5d'),_0xa3f8('0x67'),_0xa3f8('0x5c'),_0xa3f8('0x61'),'screenrecording'];}exports[_0xa3f8('0x68')]=getRealtimeAgentsQueryFields;