5407e04b2e4e8fe65fbd5d56076d0f74becca593
[motion2.git] / server / api / realtime / realtime.controller.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 _0x5afb=['../../errors/api','undefined','limit','count','offset','getAgents','query','globalStatusFilter','pauseTypeFilter','pause','then','channel','isValidChannel','Channel\x20parameter\x20%s\x20is\x20invalid','toLowerCase','sort','includes','globalStatus','validateSortParameter','fields','order','getAgentIdsByQueues','getRealtimeAgents','rows','getSortedAgentsIdsByStatus','User','fieldName','key','role','agent','online','getOptions','raw','length','sequelize','literal','FIELD(id,','*pause','push','Pause','forEach','where','merge','user','Agents','extraQueryConditions','assign','UserProfile','findAndCountAll','map','find','filterAgentAttributes','getAgentStatus','Status','sortAgentsByStatus','split','getQueues','resolve','format','startCase','type','name','voice','outbound','dialActive','userProfileId','QueueCampaigns','Queues','filter','getRealtimeQueues','catch','lodash','util','../../components/realtime/agents','../../config/utils','../../mysqldb','APIBadRequestError'];(function(_0x3f6ef0,_0xd32324){var _0x17c1f0=function(_0x9b4222){while(--_0x9b4222){_0x3f6ef0['push'](_0x3f6ef0['shift']());}};_0x17c1f0(++_0xd32324);}(_0x5afb,0x1c8));var _0xb5af=function(_0x3e2789,_0x6d23c6){_0x3e2789=_0x3e2789-0x0;var _0x4371ba=_0x5afb[_0x3e2789];return _0x4371ba;};'use strict';var _=require(_0xb5af('0x0'));var util=require(_0xb5af('0x1'));var rta=require(_0xb5af('0x2'));var rtq=require('../../components/realtime/queues');var qs=require('../../components/parsers/qs');var utils=require(_0xb5af('0x3'));var db=require(_0xb5af('0x4'))['db'];var APIBadRequestError=require('../../errors/api')[_0xb5af('0x5')];var APIHandleError=require(_0xb5af('0x6'))['handleError'];function respondWithFilteredResult(_0x3811ef,_0x51d067){return function(_0x2e3180){if(_0x2e3180){var _0x3a01de=typeof _0x51d067['offset']===_0xb5af('0x7')&&typeof _0x51d067[_0xb5af('0x8')]===_0xb5af('0x7');var _0x57a63d=_0x2e3180[_0xb5af('0x9')];var _0x9631fd=_0x3a01de?0x0:_0x51d067['offset'];var _0x124218=_0x3a01de?_0x2e3180[_0xb5af('0x9')]:_0x51d067[_0xb5af('0xa')]+_0x51d067[_0xb5af('0x8')];var _0x236301;if(_0x124218>=_0x57a63d){_0x124218=_0x57a63d;_0x236301=0xc8;}else{_0x236301=0xce;}_0x3811ef['status'](_0x236301);return _0x3811ef['set']('Content-Range',_0x9631fd+'-'+_0x124218+'/'+_0x57a63d)['json'](_0x2e3180);}return null;};}exports[_0xb5af('0xb')]=function(_0x53262d,_0x5aace1){var _0x317411={'count':0x0,'rows':[]};var _0x2dbf72={};var _0x464645;var _0x5fa68=utils['getChannels']();var _0x4a4c16;var _0x211938;var _0x3eede2=_0x53262d[_0xb5af('0xc')]['globalQueueFilter'];var _0x20d1e5=_0x53262d[_0xb5af('0xc')][_0xb5af('0xd')];var _0x1a038b=_0x53262d[_0xb5af('0xc')][_0xb5af('0xe')];var _0x4df08e=_0x20d1e5===_0xb5af('0xf')||_0x20d1e5==='*pause'?!![]:![];var _0x2d8130=[];var _0x21acd4;var _0xaaca6b;Promise['resolve']()[_0xb5af('0x10')](function(){if(_0x53262d['query'][_0xb5af('0x11')]){if(!utils[_0xb5af('0x12')](_0x53262d[_0xb5af('0xc')][_0xb5af('0x11')])){throw new APIBadRequestError(util['format'](_0xb5af('0x13'),_0x53262d[_0xb5af('0xc')][_0xb5af('0x11')]));}_0x464645=_0x53262d['query'][_0xb5af('0x11')][_0xb5af('0x14')]();}if(!_0x53262d[_0xb5af('0xc')]['sort']||!_0x53262d['query'][_0xb5af('0x15')][_0xb5af('0x16')](_0xb5af('0x17'))){_0x53262d[_0xb5af('0xc')]['sort']=rta[_0xb5af('0x18')](_0x53262d[_0xb5af('0xc')][_0xb5af('0x15')]);return;}var _0xb6d9a2=rta['getUpdatedSortOptions'](_0x53262d[_0xb5af('0xc')][_0xb5af('0x15')]);_0x53262d['query'][_0xb5af('0x15')]=_0xb6d9a2[_0xb5af('0x19')]['join'](',');_0x21acd4=_0xb6d9a2[_0xb5af('0x1a')];})[_0xb5af('0x10')](function(){if(!_0x3eede2){return[];}return rta[_0xb5af('0x1b')](_0x3eede2,_0x464645);})['then'](function(_0x457b85){_0x2d8130=_0x457b85;return rta[_0xb5af('0x1c')]();})[_0xb5af('0x10')](function(_0x4ef6a6){_0x211938=_0x4ef6a6[_0xb5af('0x1d')];if((!_0x20d1e5||_0x4df08e)&&!_0x21acd4){return _0x2d8130;}if(_0x21acd4&&!_0x20d1e5){return rta[_0xb5af('0x1e')](_0x211938,_0x2d8130,_0x21acd4);}else{return rta['getAgentIdsByStatus'](_0x211938,_0x2d8130,_0x20d1e5);}})[_0xb5af('0x10')](function(_0x4add79){_0x2d8130=_0x4add79;var _0x1ede56=![];if(_0x2d8130['length']===0x0){if(_0x3eede2||_0x20d1e5){_0x1ede56=!![];}if(_0x4df08e){_0x1ede56=![];}}if(_0x1ede56){return _0x317411;}var _0x3a622e=_['map'](db[_0xb5af('0x1f')]['rawAttributes'],function(_0x438422){return{'name':_0x438422[_0xb5af('0x20')],'type':_0x438422['type'][_0xb5af('0x21')]};});_0x53262d[_0xb5af('0xc')][_0xb5af('0x22')]=_0xb5af('0x23');_0x53262d['query'][_0xb5af('0x24')]=!![];_0x2dbf72=qs[_0xb5af('0x25')](_0x3a622e,_0x53262d);_0x2dbf72[_0xb5af('0x26')]=!![];var _0x180056=[];if(_0x2d8130[_0xb5af('0x27')]>0x0){_0x180056['push']({'id':{'$in':_0x2d8130}});if(_0x21acd4&&!_0x20d1e5){_0xaaca6b=[db[_0xb5af('0x28')][_0xb5af('0x29')](_0xb5af('0x2a')+_0x2d8130+')')];_0x2dbf72[_0xb5af('0x1a')]=_0xaaca6b;}}if(_0x4df08e){_0x5fa68['forEach'](function(_0x16ae66){var _0x2bbdaa=!![];if(_0x20d1e5===_0xb5af('0x2b')&&_0x464645!==_0x16ae66){_0x2bbdaa=![];}_0x180056[_0xb5af('0x2c')]({[_0x16ae66+_0xb5af('0x2d')]:_0x2bbdaa});});if(_0x1a038b){_0x180056['push']({'pauseType':_0x1a038b});}}else if(_0x20d1e5){_0x5fa68[_0xb5af('0x2e')](function(_0x3fcdcb){_0x180056[_0xb5af('0x2c')]({[_0x3fcdcb+_0xb5af('0x2d')]:![]});});}_0x2dbf72[_0xb5af('0x2f')]=_[_0xb5af('0x30')](_0x2dbf72['where'],{'$and':_0x180056});if(_0x53262d['user'][_0xb5af('0x22')]===_0xb5af('0x31')){var _0x45593a={'params':{'id':_0x53262d[_0xb5af('0x31')]['userProfileId']},'query':{'section':_0xb5af('0x32'),'role':_0xb5af('0x23'),'online':!![],'raw':!![],'ids':_0x2d8130,'customSorting':_0xaaca6b}};if(_0x4df08e){_0x45593a[_0xb5af('0xc')][_0xb5af('0x33')]=_0x180056;}_0x45593a[_0xb5af('0xc')]=Object[_0xb5af('0x34')](_0x45593a[_0xb5af('0xc')],_0x53262d[_0xb5af('0xc')]);delete _0x45593a[_0xb5af('0xc')]['id'];return db[_0xb5af('0x35')]['getResources'](_0x45593a);}return db['User'][_0xb5af('0x36')](_0x2dbf72);})[_0xb5af('0x10')](function(_0x4aa048){_0x317411[_0xb5af('0x9')]=_0x4aa048['count'];_0x4a4c16=_0x4aa048[_0xb5af('0x1d')];return _0x4a4c16[_0xb5af('0x37')](function(_0x411cec){var _0x48b084=_[_0xb5af('0x38')](_0x211938,['id',_0x411cec['id']]);if(_0x48b084){var _0x2df06d=rta[_0xb5af('0x39')](_0x48b084);_[_0xb5af('0x30')](_0x411cec,_0x2df06d);}_0x411cec[_0xb5af('0x17')]=rta[_0xb5af('0x3a')](_0x411cec);if(_0x411cec['globalStatus'][_0xb5af('0x16')]('pause')){_0x5fa68[_0xb5af('0x2e')](function(_0x34f464){if(_0x411cec[_0x34f464+_0xb5af('0x2d')]){_0x411cec[_0x34f464+_0xb5af('0x3b')]=_0xb5af('0xf');}});}return _0x411cec;});})[_0xb5af('0x10')](function(_0x50cf5f){if(!_0x21acd4||_0xaaca6b){return _0x50cf5f;}return rta[_0xb5af('0x3c')](_0x50cf5f,_0x21acd4);})[_0xb5af('0x10')](function(_0x185ab4){var _0xd3201d=null;if(_0x53262d[_0xb5af('0xc')][_0xb5af('0x19')]){var _0x447991=_0x53262d[_0xb5af('0xc')]['fields'][_0xb5af('0x3d')](',');_0xd3201d=_0x185ab4[_0xb5af('0x37')](function(_0x474229){return _['pick'](_0x474229,_0x447991);});}_0x317411[_0xb5af('0x1d')]=_0xd3201d||_0x185ab4;return _0x317411;})['then'](respondWithFilteredResult(_0x5aace1,_0x2dbf72))['catch'](APIHandleError(_0x5aace1));};exports[_0xb5af('0x3e')]=function(_0x1ce5dc,_0x314f24){var _0x4ef252={'count':0x0,'rows':[]};var _0x50c6de={};var _0x3b00b1;var _0x517a2d;var _0x447a76;Promise[_0xb5af('0x3f')]()[_0xb5af('0x10')](function(){if(_0x1ce5dc['query'][_0xb5af('0x11')]){if(!utils[_0xb5af('0x12')](_0x1ce5dc['query'][_0xb5af('0x11')])){throw new APIBadRequestError(util[_0xb5af('0x40')]('Channel\x20parameter\x20%s\x20is\x20invalid',_0x1ce5dc[_0xb5af('0xc')][_0xb5af('0x11')]));}_0x3b00b1=_0x1ce5dc[_0xb5af('0xc')]['channel'][_0xb5af('0x14')]();}var _0x62ebe1=_[_0xb5af('0x41')](_0x3b00b1)+'Queue';var _0x3acbf6=_[_0xb5af('0x37')](db[_0x62ebe1]['rawAttributes'],function(_0x2ec1a9){return{'name':_0x2ec1a9['fieldName'],'type':_0x2ec1a9[_0xb5af('0x42')]['key']};});if(!_0x1ce5dc[_0xb5af('0xc')][_0xb5af('0x19')]){var _0xb99f65=['id',_0xb5af('0x43')];if(_0x3b00b1===_0xb5af('0x44')){_0xb99f65['push'](_0xb5af('0x42'));if(_0x1ce5dc[_0xb5af('0xc')][_0xb5af('0x42')]===_0xb5af('0x45')){_0xb99f65[_0xb5af('0x2c')]([_0xb5af('0x46'),'dialMethod']);}}_0x1ce5dc['query']['fields']=_0xb99f65['join'](',');}_0x50c6de=qs[_0xb5af('0x25')](_0x3acbf6,_0x1ce5dc);if(_0x1ce5dc[_0xb5af('0x31')]['role']===_0xb5af('0x31')){var _0x4aa28a={'params':{'id':_0x1ce5dc['user'][_0xb5af('0x47')]},'query':{'section':_0x3b00b1===_0xb5af('0x44')&&_0x1ce5dc[_0xb5af('0xc')]['type']===_0xb5af('0x45')?_0xb5af('0x48'):_[_0xb5af('0x41')](_0x3b00b1)+_0xb5af('0x49'),'type':_0x1ce5dc[_0xb5af('0xc')]['type'],'nolimit':!![],'sort':_0xb5af('0x43'),'filter':_0x1ce5dc[_0xb5af('0xc')][_0xb5af('0x4a')]}};return db[_0xb5af('0x35')]['getResources'](_0x4aa28a);}return db[_0x62ebe1][_0xb5af('0x36')](_0x50c6de);})['then'](function(_0x131877){_0x4ef252[_0xb5af('0x9')]=_0x131877[_0xb5af('0x9')];_0x517a2d=_0x131877[_0xb5af('0x1d')];return rtq[_0xb5af('0x4b')](_0x1ce5dc,_0x3b00b1);})[_0xb5af('0x10')](function(_0x4895e3){_0x447a76=_0x4895e3[_0xb5af('0x1d')];return rtq['initializeQueues'](_0x517a2d,_0x447a76,_0x3b00b1,_0x1ce5dc[_0xb5af('0xc')][_0xb5af('0x42')]);})['then'](function(_0x532e4c){_0x4ef252[_0xb5af('0x1d')]=_0x532e4c;return _0x4ef252;})[_0xb5af('0x10')](respondWithFilteredResult(_0x314f24,_0x50c6de))[_0xb5af('0x4c')](APIHandleError(_0x314f24));};