Built motion from commit c116dc8b.|2.6.30
[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 _0x39d0=['mailCapacity','smsCapacity','whatsappCapacity','faxCapacity','chatCurrentCapacity','openchannelCurrentCapacity','mailCurrentCapacity','smsCurrentCapacity','faxCurrentCapacity','smsStatus','chatStatusTime','mailStatusTime','whatsappStatusTime','voiceStatusTime','queue','phoneBarEnableScreenRecordingByAgent','phoneBarEnableAutomaticScreenRecording','omit','pick','filterAgentAttributes','role','lastLoginAt','faxStatusTime','smsStatusTime','chatCapacity','whatsappCurrentCapacity','destconnectedlinenum','onhold','screenrecording','getRealtimeAgentsQueryFields','lodash','client','http','chatPause','faxPause','mailPause','openchannelPause','smsPause','voicePause','whatsappPause','every','filter','length','idle','unknown','unavailable','ringing','voiceStatus','pause','chatStatus','faxStatus','mailStatus','openchannelStatus','whatsappStatus','request','Agents','then','error','result','Unable\x20to\x20retrieve\x20RPC\x20agents\x20(Routing)','merge','catch','busy','isArray','Queue','map','findOne','getAgents','all','flatten','uniq','value','getAgentIdsByQueues','includes','getAgentIdsByStatus','User','findAll','name','fullname','find','globalStatus','getSortedAgentsIdsByStatus','split','startsWith','DESC','ASC','getUpdatedSortOptions','internal','indexOf','sort','pauseType','toLowerCase','sortAgentsByStatus','userpic','online','lastPauseAt','address','openchannelCapacity'];(function(_0x20e5d4,_0x4ae460){var _0x54d52a=function(_0x3c3192){while(--_0x3c3192){_0x20e5d4['push'](_0x20e5d4['shift']());}};_0x54d52a(++_0x4ae460);}(_0x39d0,0x144));var _0x039d=function(_0x5c19fa,_0x48802f){_0x5c19fa=_0x5c19fa-0x0;var _0x29fb52=_0x39d0[_0x5c19fa];return _0x29fb52;};'use strict';var _=require(_0x039d('0x0'));var jayson=require('jayson/promise');var db=require('../../mysqldb')['db'];var amiClient=jayson[_0x039d('0x1')][_0x039d('0x2')]({'port':0x232a});var routingClient=jayson[_0x039d('0x1')][_0x039d('0x2')]({'port':0x232c});function isAgentPaused(_0x310274,_0x53c769){var _0x4539ca=[_0x310274[_0x039d('0x3')],_0x310274[_0x039d('0x4')],_0x310274[_0x039d('0x5')],_0x310274[_0x039d('0x6')],_0x310274[_0x039d('0x7')],_0x310274[_0x039d('0x8')],_0x310274[_0x039d('0x9')]];if(!_0x53c769)return _[_0x039d('0xa')](_0x4539ca);var _0x210090=_[_0x039d('0xb')](_0x4539ca);return _0x210090['length']>0x0&&_0x210090['length']<_0x4539ca[_0x039d('0xc')]?!![]:![];}function isIdle(_0x5d6ba2){return _0x5d6ba2===_0x039d('0xd')||_0x5d6ba2===_0x039d('0xe')||_0x5d6ba2===_0x039d('0xf')||_0x5d6ba2===_0x039d('0x10')||_['isNil'](_0x5d6ba2);}function isAgentIdle(_0xdc4305){if(_0xdc4305[_0x039d('0x11')]!==_0x039d('0xd')&&_0xdc4305['voicePause']){_0xdc4305[_0x039d('0x11')]=_0x039d('0x12');return![];}return _[_0x039d('0xa')]([_0xdc4305[_0x039d('0x13')],_0xdc4305[_0x039d('0x14')],_0xdc4305[_0x039d('0x15')],_0xdc4305[_0x039d('0x16')],_0xdc4305['smsStatus'],_0xdc4305['voiceStatus'],_0xdc4305[_0x039d('0x17')]],isIdle);}function getRealtimeAgents(){return amiClient[_0x039d('0x18')](_0x039d('0x19'),{})[_0x039d('0x1a')](function(_0x24c16f){if(_0x24c16f[_0x039d('0x1b')]){throw new Error('Unable\x20to\x20retrieve\x20RPC\x20agents\x20(AMI)');}var _0x1dc31d=_0x24c16f[_0x039d('0x1c')];return routingClient[_0x039d('0x18')]('Agents',{})['then'](function(_0xaedb24){if(_0xaedb24[_0x039d('0x1b')]){throw new Error(_0x039d('0x1d'));}var _0x4fad10=_0xaedb24['result'];return _[_0x039d('0x1e')]({},_0x1dc31d,_0x4fad10);});})[_0x039d('0x1f')](function(_0x54e096){throw _0x54e096;});}exports['getRealtimeAgents']=getRealtimeAgents;function getAgentStatus(_0xa152d9){var _0x5bfcce=_0x039d('0xe');if(isAgentPaused(_0xa152d9))_0x5bfcce=_0x039d('0x12');else if(isAgentPaused(_0xa152d9,!![]))_0x5bfcce='*pause';else if(isAgentIdle(_0xa152d9))_0x5bfcce=_0x039d('0xd');else _0x5bfcce=_0x039d('0x20');return _0x5bfcce;}exports['getAgentStatus']=getAgentStatus;function getAgentIdsByQueues(_0x6e1cb9,_0x15dcaa){var _0xd5c776=_[_0x039d('0x21')](_0x6e1cb9)?_0x6e1cb9:[_0x6e1cb9];var _0x2d4221=_['startCase'](_0x15dcaa)+_0x039d('0x22');var _0x7e230f=_[_0x039d('0x23')](_0xd5c776,function(_0x4d3983){return db[_0x2d4221][_0x039d('0x24')]({'where':{'id':_0x4d3983}})['then'](function(_0x30a57e){if(!_0x30a57e)return[];return _0x30a57e[_0x039d('0x25')]({'attributes':['id'],'raw':!![]});});});return Promise[_0x039d('0x26')](_0x7e230f)['then'](function(_0x316d47){var _0x5e0065=_(_0x316d47)[_0x039d('0x27')]()[_0x039d('0x23')]('id')[_0x039d('0x28')]()[_0x039d('0x29')]();return _0x5e0065;});}exports[_0x039d('0x2a')]=getAgentIdsByQueues;function getAgentIdsByStatus(_0x170e34,_0xc6b469,_0x9da588){try{if(_0xc6b469[_0x039d('0xc')]>0x0){_0x170e34=_0x170e34[_0x039d('0xb')](function(_0x3d3a40){return _0xc6b469[_0x039d('0x2b')](_0x3d3a40['id']);});}var _0x42b830=_0x170e34[_0x039d('0xb')](function(_0x3fecb4){var _0x199b8e=_0x039d('0x20');if(isAgentIdle(_0x3fecb4)){_0x199b8e=_0x039d('0xd');}return _0x199b8e===_0x9da588;});return _[_0x039d('0x23')](_0x42b830,'id');}catch(_0xf074d8){throw _0xf074d8;}}exports[_0x039d('0x2c')]=getAgentIdsByStatus;function getSortedAgentsIdsByStatus(_0x2eef2b,_0x1babe4,_0x15e999){return Promise['resolve']()['then'](function(){if(_0x1babe4[_0x039d('0xc')]>0x0){_0x2eef2b=_0x2eef2b['filter'](function(_0x54fdf1){return _0x1babe4[_0x039d('0x2b')](_0x54fdf1['id']);});}return _0x2eef2b;})[_0x039d('0x1a')](function(){return db[_0x039d('0x2d')][_0x039d('0x2e')]({'attributes':['id',_0x039d('0x2f'),_0x039d('0x30'),'chatPause','faxPause',_0x039d('0x5'),_0x039d('0x6'),'smsPause',_0x039d('0x8'),_0x039d('0x9'),'pauseType'],'where':{'id':{'$in':_[_0x039d('0x23')](_0x2eef2b,'id')}},'raw':!![]});})[_0x039d('0x1a')](function(_0x17a3c9){return _0x17a3c9[_0x039d('0x23')](function(_0x55352e){var _0x490bc2=_[_0x039d('0x31')](_0x2eef2b,['id',_0x55352e['id']]);var _0x1b0849=filterAgentAttributes(_0x490bc2);_[_0x039d('0x1e')](_0x55352e,_0x1b0849);_0x55352e[_0x039d('0x32')]=getAgentStatus(_0x55352e);return _0x55352e;});})[_0x039d('0x1a')](function(_0x5c2a4d){var _0x3fc91b=sortAgentsByStatus(_0x5c2a4d,_0x15e999);return _[_0x039d('0x23')](_0x3fc91b,'id');})[_0x039d('0x1f')](function(_0x19116d){throw _0x19116d;});}exports[_0x039d('0x33')]=getSortedAgentsIdsByStatus;function getUpdatedSortOptions(_0x542efb){var _0x1357f6={'fields':[],'order':null};var _0x41db8a=_0x542efb[_0x039d('0x34')](',');_0x1357f6['fields']=_[_0x039d('0xb')](_0x41db8a,function(_0x2f2ceb){if(!_0x2f2ceb[_0x039d('0x2b')](_0x039d('0x32'))){return!![];}_0x1357f6['order']=_[_0x039d('0x35')](_0x2f2ceb,'-')?_0x039d('0x36'):_0x039d('0x37');return![];});return _0x1357f6;}exports[_0x039d('0x38')]=getUpdatedSortOptions;function validateSortParameter(_0x4cca34){if(!_0x4cca34){_0x4cca34='fullname,internal';}if(_0x4cca34[_0x039d('0x2b')](_0x039d('0x30'))&&!_0x4cca34[_0x039d('0x2b')](_0x039d('0x39'))){var _0xbb1a62=_0x4cca34['substring'](0x0,_0x4cca34[_0x039d('0x3a')]('fullname'));_0x4cca34+=','+_0xbb1a62+_0x039d('0x39');}return _0x4cca34;}exports['validateSortParameter']=validateSortParameter;function sortAgentsByStatus(_0x40df67,_0x79e8b){var _0x269072=_0x40df67[_0x039d('0x3b')](function(_0x51c9d6,_0x397d9e){if(_0x51c9d6[_0x039d('0x32')]===_0x397d9e['globalStatus']){if(_0x51c9d6[_0x039d('0x32')]===_0x039d('0x12')){if(_0x79e8b==='DESC'){return(_0x51c9d6['pauseType']||'')<(_0x397d9e[_0x039d('0x3c')]||'')||_0x51c9d6[_0x039d('0x30')]['toLowerCase']()>_0x397d9e[_0x039d('0x30')][_0x039d('0x3d')]()?0x1:-0x1;}else{return(_0x51c9d6[_0x039d('0x3c')]||'')<(_0x397d9e[_0x039d('0x3c')]||'')||_0x51c9d6[_0x039d('0x30')][_0x039d('0x3d')]()>_0x397d9e[_0x039d('0x30')][_0x039d('0x3d')]()?-0x1:0x1;}}else{if(_0x79e8b==='DESC'){return _0x51c9d6['fullname']['toLowerCase']()<_0x397d9e[_0x039d('0x30')][_0x039d('0x3d')]()?0x1:-0x1;}else{return _0x51c9d6['fullname'][_0x039d('0x3d')]()<_0x397d9e[_0x039d('0x30')][_0x039d('0x3d')]()?-0x1:0x1;}}}else{if(_0x79e8b===_0x039d('0x36')){return _0x51c9d6[_0x039d('0x32')]<_0x397d9e['globalStatus']?0x1:-0x1;}else{return _0x51c9d6[_0x039d('0x32')]<_0x397d9e[_0x039d('0x32')]?-0x1:0x1;}}});return _0x269072;}exports[_0x039d('0x3e')]=sortAgentsByStatus;function filterAgentAttributes(_0x276981){var _0x55ca2a=[_0x039d('0x3f'),_0x039d('0x40'),'lastLoginAt','lastPauseAt','pauseType',_0x039d('0x41'),'internal',_0x039d('0x42'),_0x039d('0x3'),_0x039d('0x6'),_0x039d('0x5'),'smsPause',_0x039d('0x9'),_0x039d('0x4'),_0x039d('0x8'),'chatCapacity',_0x039d('0x43'),_0x039d('0x44'),_0x039d('0x45'),_0x039d('0x46'),_0x039d('0x47'),_0x039d('0x48'),_0x039d('0x49'),_0x039d('0x4a'),_0x039d('0x4b'),'whatsappCurrentCapacity',_0x039d('0x4c'),_0x039d('0x13'),_0x039d('0x16'),_0x039d('0x15'),_0x039d('0x4d'),_0x039d('0x17'),_0x039d('0x14'),_0x039d('0x11'),_0x039d('0x4e'),'openchannelStatusTime',_0x039d('0x4f'),'smsStatusTime',_0x039d('0x50'),'faxStatusTime',_0x039d('0x51'),_0x039d('0x52'),'destconnectedlinenum','screenrecording',_0x039d('0x53'),_0x039d('0x54')];var _0x2b4d68=[_0x039d('0x3'),_0x039d('0x6'),'mailPause','smsPause','whatsappPause',_0x039d('0x4'),'voicePause',_0x039d('0x40')];var _0x4bd2cf=_(_0x276981)[_0x039d('0x55')](_0x2b4d68)[_0x039d('0x56')](_0x55ca2a)['value']();return _0x4bd2cf;}exports[_0x039d('0x57')]=filterAgentAttributes;function getRealtimeAgentsQueryFields(){return['id',_0x039d('0x2f'),'fullname',_0x039d('0x58'),_0x039d('0x39'),_0x039d('0x40'),_0x039d('0x32'),_0x039d('0x3f'),_0x039d('0x59'),_0x039d('0x3c'),_0x039d('0x41'),_0x039d('0x4'),_0x039d('0x3'),_0x039d('0x5'),_0x039d('0x6'),'smsPause',_0x039d('0x8'),_0x039d('0x9'),_0x039d('0x13'),_0x039d('0x14'),_0x039d('0x15'),'openchannelStatus','smsStatus',_0x039d('0x11'),'whatsappStatus','chatStatusTime',_0x039d('0x5a'),_0x039d('0x4f'),'openchannelStatusTime',_0x039d('0x5b'),_0x039d('0x51'),'whatsappStatusTime',_0x039d('0x5c'),_0x039d('0x47'),_0x039d('0x44'),'openchannelCapacity',_0x039d('0x45'),_0x039d('0x46'),_0x039d('0x48'),_0x039d('0x4c'),'mailCurrentCapacity','openchannelCurrentCapacity',_0x039d('0x4b'),_0x039d('0x5d'),_0x039d('0x53'),_0x039d('0x54'),_0x039d('0x5e'),_0x039d('0x5f'),'queue',_0x039d('0x60')];}exports[_0x039d('0x61')]=getRealtimeAgentsQueryFields;