b3f2b1d601108429ab96141de0b045506e0ad71e
[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 _0x22f9=['util','../../components/realtime/agents','../../components/parsers/qs','../../mysqldb','../../errors/api','APIBadRequestError','handleError','offset','undefined','limit','count','status','set','Content-Range','json','getAgents','getChannels','query','globalStatusFilter','pauseTypeFilter','pause','*pause','then','isValidChannel','channel','Channel\x20parameter\x20%s\x20is\x20invalid','toLowerCase','sort','validateSortParameter','getUpdatedSortOptions','fields','join','order','getRealtimeAgents','rows','getAgentIdsByStatus','length','User','rawAttributes','fieldName','key','role','agent','getOptions','raw','sequelize','literal','FIELD(id,','forEach','Pause','push','merge','where','Agents','online','extraQueryConditions','omit','getResources','findAndCountAll','find','globalStatus','getAgentStatus','includes','voiceStatus','talking','sortAgentsByStatus','getRealtimeAgentsQueryFields','map','catch','format','startCase','Queue','type','voice','dialActive','dialMethod','user','userProfileId','outbound','Queues','name','filter','UserProfile','getRealtimeQueues','lodash'];(function(_0xd0a847,_0x72562b){var _0x2536ec=function(_0x20e4db){while(--_0x20e4db){_0xd0a847['push'](_0xd0a847['shift']());}};_0x2536ec(++_0x72562b);}(_0x22f9,0xfe));var _0x922f=function(_0x3b0624,_0x597d87){_0x3b0624=_0x3b0624-0x0;var _0x4bfaee=_0x22f9[_0x3b0624];return _0x4bfaee;};'use strict';var _=require(_0x922f('0x0'));var util=require(_0x922f('0x1'));var rta=require(_0x922f('0x2'));var rtq=require('../../components/realtime/queues');var qs=require(_0x922f('0x3'));var utils=require('../../config/utils');var db=require(_0x922f('0x4'))['db'];var APIBadRequestError=require(_0x922f('0x5'))[_0x922f('0x6')];var APIHandleError=require('../../errors/api')[_0x922f('0x7')];function respondWithFilteredResult(_0x5f11af,_0x5c12e8){return function(_0x2dfe97){if(_0x2dfe97){var _0x5a684d=typeof _0x5c12e8[_0x922f('0x8')]===_0x922f('0x9')&&typeof _0x5c12e8[_0x922f('0xa')]===_0x922f('0x9');var _0x482ff5=_0x2dfe97[_0x922f('0xb')];var _0xb0000d=_0x5a684d?0x0:_0x5c12e8[_0x922f('0x8')];var _0x27f777=_0x5a684d?_0x2dfe97[_0x922f('0xb')]:_0x5c12e8[_0x922f('0x8')]+_0x5c12e8[_0x922f('0xa')];var _0x112d2b;if(_0x27f777>=_0x482ff5){_0x27f777=_0x482ff5;_0x112d2b=0xc8;}else{_0x112d2b=0xce;}_0x5f11af[_0x922f('0xc')](_0x112d2b);return _0x5f11af[_0x922f('0xd')](_0x922f('0xe'),_0xb0000d+'-'+_0x27f777+'/'+_0x482ff5)[_0x922f('0xf')](_0x2dfe97);}return null;};}exports[_0x922f('0x10')]=function(_0x2e0a35,_0x3d533b){var _0xa0a83e={'count':0x0,'rows':[]};var _0x394710={};var _0x3d0556;var _0x85d46c=utils[_0x922f('0x11')]();var _0x56eaeb;var _0x5615e0;var _0x3ffb3e=_0x2e0a35['query']['globalQueueFilter'];var _0x25506a=_0x2e0a35[_0x922f('0x12')][_0x922f('0x13')];var _0x100e85=_0x2e0a35[_0x922f('0x12')][_0x922f('0x14')];var _0x19b85a=_0x25506a===_0x922f('0x15')||_0x25506a===_0x922f('0x16')?!![]:![];var _0x6960b7=[];var _0x2689d1;var _0x1b997c;Promise['resolve']()[_0x922f('0x17')](function(){if(_0x2e0a35[_0x922f('0x12')]['channel']){if(!utils[_0x922f('0x18')](_0x2e0a35[_0x922f('0x12')][_0x922f('0x19')])){throw new APIBadRequestError(util['format'](_0x922f('0x1a'),_0x2e0a35[_0x922f('0x12')]['channel']));}_0x3d0556=_0x2e0a35[_0x922f('0x12')][_0x922f('0x19')][_0x922f('0x1b')]();}if(!_0x2e0a35['query'][_0x922f('0x1c')]||!_0x2e0a35[_0x922f('0x12')][_0x922f('0x1c')]['includes']('globalStatus')){_0x2e0a35['query'][_0x922f('0x1c')]=rta[_0x922f('0x1d')](_0x2e0a35[_0x922f('0x12')]['sort']);return;}var _0x484b08=rta[_0x922f('0x1e')](_0x2e0a35[_0x922f('0x12')][_0x922f('0x1c')]);_0x2e0a35[_0x922f('0x12')][_0x922f('0x1c')]=_0x484b08[_0x922f('0x1f')][_0x922f('0x20')](',');_0x2689d1=_0x484b08[_0x922f('0x21')];})[_0x922f('0x17')](function(){if(!_0x3ffb3e){return[];}return rta['getAgentIdsByQueues'](_0x3ffb3e,_0x3d0556);})[_0x922f('0x17')](function(_0x5ae140){_0x6960b7=_0x5ae140;return rta[_0x922f('0x22')]();})['then'](function(_0x177a27){_0x5615e0=_0x177a27[_0x922f('0x23')];if((!_0x25506a||_0x19b85a)&&!_0x2689d1){return _0x6960b7;}if(_0x2689d1&&!_0x25506a){return rta['getSortedAgentsIdsByStatus'](_0x5615e0,_0x6960b7,_0x2689d1);}else{return rta[_0x922f('0x24')](_0x5615e0,_0x6960b7,_0x25506a);}})[_0x922f('0x17')](function(_0x2e11c0){_0x6960b7=_0x2e11c0;var _0x866e6e=![];if(_0x6960b7[_0x922f('0x25')]===0x0){if(_0x3ffb3e||_0x25506a){_0x866e6e=!![];}if(_0x19b85a){_0x866e6e=![];}}if(_0x866e6e){return _0xa0a83e;}var _0x3bb66a=_['map'](db[_0x922f('0x26')][_0x922f('0x27')],function(_0xeb022f){return{'name':_0xeb022f[_0x922f('0x28')],'type':_0xeb022f['type'][_0x922f('0x29')]};});_0x2e0a35[_0x922f('0x12')][_0x922f('0x2a')]=_0x922f('0x2b');_0x394710=qs[_0x922f('0x2c')](_0x3bb66a,_0x2e0a35);_0x394710[_0x922f('0x2d')]=!![];var _0x5d5424=[];if(_0x6960b7[_0x922f('0x25')]>0x0){_0x5d5424['push']({'id':{'$in':_0x6960b7}});if(_0x2689d1&&!_0x25506a){_0x1b997c=[db[_0x922f('0x2e')][_0x922f('0x2f')](_0x922f('0x30')+_0x6960b7+')')];_0x394710[_0x922f('0x21')]=_0x1b997c;}}if(_0x19b85a){_0x85d46c[_0x922f('0x31')](function(_0x293574){var _0x25c5c1=!![];if(_0x25506a==='*pause'&&_0x3d0556!==_0x293574){_0x25c5c1=![];}_0x5d5424['push']({[_0x293574+_0x922f('0x32')]:_0x25c5c1});});if(_0x100e85){_0x5d5424[_0x922f('0x33')]({'pauseType':_0x100e85});}}else if(_0x25506a){_0x85d46c['forEach'](function(_0x3149b9){_0x5d5424[_0x922f('0x33')]({[_0x3149b9+_0x922f('0x32')]:![]});});}_0x394710['where']=_[_0x922f('0x34')](_0x394710[_0x922f('0x35')],{'$and':_0x5d5424});if(_0x2e0a35['user'][_0x922f('0x2a')]==='user'){var _0xf0aef5={'params':{'id':_0x2e0a35['user']['userProfileId']},'query':{'section':_0x922f('0x36'),'role':_0x922f('0x2b'),'raw':!![],'ids':_0x6960b7,'customSorting':_0x1b997c}};if(_0x2e0a35['query'][_0x922f('0x37')]){_0xf0aef5[_0x922f('0x12')][_0x922f('0x37')]=!![];}if(_0x19b85a){_0xf0aef5[_0x922f('0x12')][_0x922f('0x38')]=_0x5d5424;}_0xf0aef5[_0x922f('0x12')]=Object['assign'](_0xf0aef5[_0x922f('0x12')],_[_0x922f('0x39')](_0x2e0a35[_0x922f('0x12')],'id'));return db['UserProfile'][_0x922f('0x3a')](_0xf0aef5);}return db[_0x922f('0x26')][_0x922f('0x3b')](_0x394710);})['then'](function(_0x50e557){_0xa0a83e[_0x922f('0xb')]=_0x50e557[_0x922f('0xb')];_0x56eaeb=_0x50e557[_0x922f('0x23')];return _0x56eaeb['map'](function(_0x563fec){var _0x15498d=_[_0x922f('0x3c')](_0x5615e0,['id',_0x563fec['id']]);if(_0x15498d){var _0x42cb70=rta['filterAgentAttributes'](_0x15498d);_['merge'](_0x563fec,_0x42cb70);}_0x563fec[_0x922f('0x3d')]=rta[_0x922f('0x3e')](_0x563fec);if(_0x563fec[_0x922f('0x3d')][_0x922f('0x3f')](_0x922f('0x15'))){_0x85d46c[_0x922f('0x31')](function(_0x4741df){if(_0x563fec[_0x4741df+'Pause']){if(_0x4741df==='voice'){if(_0x563fec[_0x922f('0x40')]!==_0x922f('0x41')){_0x563fec[_0x922f('0x40')]=_0x922f('0x15');}}else{_0x563fec[_0x4741df+'Status']='pause';}}});}return _0x563fec;});})[_0x922f('0x17')](function(_0x4f2d13){if(!_0x2689d1||_0x1b997c){return _0x4f2d13;}return rta[_0x922f('0x42')](_0x4f2d13,_0x2689d1);})['then'](function(_0x425864){var _0x1b695f=rta[_0x922f('0x43')]();_0xa0a83e['rows']=_0x425864[_0x922f('0x44')](function(_0x312e84){return _['pick'](_0x312e84,_0x1b695f);});return _0xa0a83e;})[_0x922f('0x17')](respondWithFilteredResult(_0x3d533b,_0x394710))[_0x922f('0x45')](APIHandleError(_0x3d533b));};exports['getQueues']=function(_0x1bf0c9,_0x2de136){var _0x3fc839={'count':0x0,'rows':[]};var _0x3c8771={};var _0xcd8538;var _0x436ae1;var _0xa12146;Promise['resolve']()[_0x922f('0x17')](function(){if(_0x1bf0c9[_0x922f('0x12')][_0x922f('0x19')]){if(!utils[_0x922f('0x18')](_0x1bf0c9[_0x922f('0x12')][_0x922f('0x19')])){throw new APIBadRequestError(util[_0x922f('0x46')](_0x922f('0x1a'),_0x1bf0c9[_0x922f('0x12')][_0x922f('0x19')]));}_0xcd8538=_0x1bf0c9['query'][_0x922f('0x19')][_0x922f('0x1b')]();}var _0xf05f3a=_[_0x922f('0x47')](_0xcd8538)+_0x922f('0x48');var _0xebbd73=_['map'](db[_0xf05f3a][_0x922f('0x27')],function(_0x14584c){return{'name':_0x14584c['fieldName'],'type':_0x14584c[_0x922f('0x49')]['key']};});if(!_0x1bf0c9['query'][_0x922f('0x1f')]){var _0x452a40=['id','name'];if(_0xcd8538===_0x922f('0x4a')){_0x452a40[_0x922f('0x33')]('type');if(_0x1bf0c9[_0x922f('0x12')][_0x922f('0x49')]==='outbound'){_0x452a40[_0x922f('0x33')]([_0x922f('0x4b'),_0x922f('0x4c')]);}}_0x1bf0c9['query']['fields']=_0x452a40[_0x922f('0x20')](',');}_0x3c8771=qs[_0x922f('0x2c')](_0xebbd73,_0x1bf0c9);if(_0x1bf0c9['user'][_0x922f('0x2a')]===_0x922f('0x4d')){var _0x3e50b5={'params':{'id':_0x1bf0c9['user'][_0x922f('0x4e')]},'query':{'section':_0xcd8538===_0x922f('0x4a')&&_0x1bf0c9[_0x922f('0x12')][_0x922f('0x49')]===_0x922f('0x4f')?'QueueCampaigns':_[_0x922f('0x47')](_0xcd8538)+_0x922f('0x50'),'type':_0x1bf0c9[_0x922f('0x12')][_0x922f('0x49')],'nolimit':!![],'sort':_0x922f('0x51'),'filter':_0x1bf0c9['query'][_0x922f('0x52')]}};_0x3e50b5[_0x922f('0x12')]=Object['assign'](_0x3e50b5[_0x922f('0x12')],_0x1bf0c9['query']);delete _0x3e50b5[_0x922f('0x12')]['id'];return db[_0x922f('0x53')][_0x922f('0x3a')](_0x3e50b5);}return db[_0xf05f3a][_0x922f('0x3b')](_0x3c8771);})['then'](function(_0x1dfc43){_0x3fc839[_0x922f('0xb')]=_0x1dfc43['count'];_0x436ae1=_0x1dfc43['rows'];return rtq[_0x922f('0x54')](_0x1bf0c9,_0xcd8538);})[_0x922f('0x17')](function(_0xd8d4a5){_0xa12146=_0xd8d4a5['rows'];return rtq['initializeQueues'](_0x436ae1,_0xa12146,_0xcd8538,_0x1bf0c9[_0x922f('0x12')][_0x922f('0x49')]);})[_0x922f('0x17')](function(_0x55f20b){_0x3fc839[_0x922f('0x23')]=_0x55f20b;return _0x3fc839;})[_0x922f('0x17')](respondWithFilteredResult(_0x2de136,_0x3c8771))['catch'](APIHandleError(_0x2de136));};