1c76e0045af8d67cd3f311f6a866e57d504b8cd3
[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 _0x53d2=['../../components/parsers/advancedSearch','../../config/license/hardware','../../config/logger','../../config/license/util','../../mysqldb','jayson/promise','client','http','request','then','info','Realtime,\x20%s,\x20%s','request\x20sent','Realtime,\x20%s,\x20%s,\x20%s','stringify','error','code','message','result','catch','sendStatus','status','json','offset','undefined','limit','count','set','Content-Range','reject','save','update','destroy','end','stack','name','getQueues','mail','chat','openchannel','fax','whatsapp','voice','resolve','query','channel','includes','toLowerCase','Invalid\x20channel','startCase','Queue','map','rawAttributes','fieldName','type','getOptions','user','role','outbound','filter','UserProfile','getResources','findAndCountAll','Queues','loggedIn','waiting','talking','push','pTalking','dialActive','dialMethod','Trunk','startPredictive','startProgressive','erlangAbandonmentRate','erlangBusyFactor','dialPredictiveInterval','dialPredictiveIntervalMaxThreshold','predictiveIntervalAvailable','predictiveIntervalTotalCalls','predictiveIntervalAnsweredCalls','predictiveIntervalDroppedCallsTimeout','predictiveIntervalDroppedCallsCallersExit','predictiveIntervalAvgHoldtime','predictiveIntervalAvgTalktime','rows','get','loggedInDb','available','paused','originated','agents','merge','pick','getAgents','getMembers','length','find','online','User','QueueId','Pause','findAll','UserId','eml-format','zip-dir','fast-json-patch','request-promise','moment','bluebird','mustache','util','path','to-csv','fs-extra','lodash','squel','crypto','jsforce','desk.js','papaparse','ioredis','../../components/auth/service','../../components/parsers/qs'];(function(_0x3ea7e9,_0x472bc5){var _0x26388b=function(_0x131ea1){while(--_0x131ea1){_0x3ea7e9['push'](_0x3ea7e9['shift']());}};_0x26388b(++_0x472bc5);}(_0x53d2,0x1dc));var _0x253d=function(_0x3dd15e,_0x1f0015){_0x3dd15e=_0x3dd15e-0x0;var _0x231fd0=_0x53d2[_0x3dd15e];return _0x231fd0;};'use strict';var emlformat=require(_0x253d('0x0'));var rimraf=require('rimraf');var zipdir=require(_0x253d('0x1'));var jsonpatch=require(_0x253d('0x2'));var rp=require(_0x253d('0x3'));var moment=require(_0x253d('0x4'));var BPromise=require(_0x253d('0x5'));var Mustache=require(_0x253d('0x6'));var util=require(_0x253d('0x7'));var path=require(_0x253d('0x8'));var sox=require('sox');var csv=require(_0x253d('0x9'));var ejs=require('ejs');var fs=require('fs');var fs_extra=require(_0x253d('0xa'));var _=require(_0x253d('0xb'));var squel=require(_0x253d('0xc'));var crypto=require(_0x253d('0xd'));var jsforce=require(_0x253d('0xe'));var deskjs=require(_0x253d('0xf'));var toCsv=require('to-csv');var querystring=require('querystring');var Papa=require(_0x253d('0x10'));var Redis=require(_0x253d('0x11'));var authService=require(_0x253d('0x12'));var qs=require(_0x253d('0x13'));var as=require(_0x253d('0x14'));var hardwareService=require(_0x253d('0x15'));var logger=require(_0x253d('0x16'))('api');var utils=require('../../config/utils');var config=require('../../config/environment');var licenseUtil=require(_0x253d('0x17'));var db=require(_0x253d('0x18'))['db'];var jayson=require(_0x253d('0x19'));var client=jayson[_0x253d('0x1a')][_0x253d('0x1b')]({'port':0x232a});var routingClient=jayson[_0x253d('0x1a')][_0x253d('0x1b')]({'port':0x232c});function respondWithRpcPromise(_0x1baf26,_0x9711b0,_0x571f5d,_0x48a9fe){return new BPromise(function(_0x35325b,_0x14dc9c){var _0x114f57=_0x48a9fe||client;return _0x114f57[_0x253d('0x1c')](_0x1baf26,_0x571f5d)[_0x253d('0x1d')](function(_0x25b4bb){logger[_0x253d('0x1e')](_0x253d('0x1f'),_0x9711b0,_0x253d('0x20'));logger['debug'](_0x253d('0x21'),_0x9711b0,'request\x20sent',JSON[_0x253d('0x22')](_0x25b4bb));if(_0x25b4bb[_0x253d('0x23')]){if(_0x25b4bb[_0x253d('0x23')][_0x253d('0x24')]===0x1f4){logger['error'](_0x253d('0x1f'),_0x9711b0,_0x25b4bb[_0x253d('0x23')][_0x253d('0x25')]);return _0x14dc9c(_0x25b4bb[_0x253d('0x23')][_0x253d('0x25')]);}logger[_0x253d('0x23')](_0x253d('0x1f'),_0x9711b0,_0x25b4bb[_0x253d('0x23')]['message']);return _0x35325b(_0x25b4bb[_0x253d('0x23')][_0x253d('0x25')]);}else{logger[_0x253d('0x1e')](_0x253d('0x1f'),_0x9711b0,_0x253d('0x20'));_0x35325b(_0x25b4bb[_0x253d('0x26')]['message']);}})[_0x253d('0x27')](function(_0xea4c5){logger[_0x253d('0x23')]('Realtime,\x20%s,\x20%s',_0x9711b0,_0xea4c5);_0x14dc9c(_0xea4c5);});});}function respondWithStatusCode(_0x1280fc,_0x53f32b){_0x53f32b=_0x53f32b||0xcc;return function(_0x2d1254){if(_0x2d1254){return _0x1280fc[_0x253d('0x28')](_0x53f32b);}return _0x1280fc[_0x253d('0x29')](_0x53f32b)['end']();};}function respondWithResult(_0x2c7e5e,_0x4a6296){_0x4a6296=_0x4a6296||0xc8;return function(_0x5359bf){if(_0x5359bf){return _0x2c7e5e[_0x253d('0x29')](_0x4a6296)[_0x253d('0x2a')](_0x5359bf);}};}function respondWithFilteredResult(_0x36a88e,_0x482c41){return function(_0x916f1a){if(_0x916f1a){var _0x1bf790=typeof _0x482c41[_0x253d('0x2b')]===_0x253d('0x2c')&&typeof _0x482c41[_0x253d('0x2d')]===_0x253d('0x2c');var _0x38f6a6=_0x916f1a[_0x253d('0x2e')];var _0x2fb454=_0x1bf790?0x0:_0x482c41['offset'];var _0x388e1a=_0x1bf790?_0x916f1a[_0x253d('0x2e')]:_0x482c41[_0x253d('0x2b')]+_0x482c41[_0x253d('0x2d')];var _0x31a546;if(_0x388e1a>=_0x38f6a6){_0x388e1a=_0x38f6a6;_0x31a546=0xc8;}else{_0x31a546=0xce;}_0x36a88e[_0x253d('0x29')](_0x31a546);return _0x36a88e[_0x253d('0x2f')](_0x253d('0x30'),_0x2fb454+'-'+_0x388e1a+'/'+_0x38f6a6)[_0x253d('0x2a')](_0x916f1a);}return null;};}function patchUpdates(_0x513598){return function(_0x108dcf){try{jsonpatch['apply'](_0x108dcf,_0x513598,!![]);}catch(_0x4c7dac){return BPromise[_0x253d('0x31')](_0x4c7dac);}return _0x108dcf[_0x253d('0x32')]();};}function saveUpdates(_0x32a2ab,_0x1b227b){return function(_0x55db3c){if(_0x55db3c){return _0x55db3c[_0x253d('0x33')](_0x32a2ab)[_0x253d('0x1d')](function(_0x5f3387){return _0x5f3387;});}return null;};}function removeEntity(_0x48ae3d,_0x14a20f){return function(_0x3c5ad0){if(_0x3c5ad0){return _0x3c5ad0[_0x253d('0x34')]()[_0x253d('0x1d')](function(){_0x48ae3d[_0x253d('0x29')](0xcc)[_0x253d('0x35')]();});}};}function handleEntityNotFound(_0x93b7b8,_0x6f0aec){return function(_0x23f955){if(!_0x23f955){_0x93b7b8[_0x253d('0x28')](0x194);}return _0x23f955;};}function handleError(_0x5a6709,_0x2d00fb){_0x2d00fb=_0x2d00fb||0x1f4;return function(_0x1f71d2){logger[_0x253d('0x23')](_0x1f71d2[_0x253d('0x36')]);if(_0x1f71d2[_0x253d('0x37')]){delete _0x1f71d2[_0x253d('0x37')];}_0x5a6709[_0x253d('0x29')](_0x2d00fb)['send'](_0x1f71d2);};}exports[_0x253d('0x38')]=function(_0x4d7e9e,_0x5aabce){var _0xf27244=[_0x253d('0x39'),_0x253d('0x3a'),'sms',_0x253d('0x3b'),_0x253d('0x3c'),_0x253d('0x3d'),_0x253d('0x3e')];var _0x2ec8ce={'count':0x0,'rows':[]},_0x4a7a80={},_0x539151;Promise[_0x253d('0x3f')]()[_0x253d('0x1d')](function(){if(_0x4d7e9e[_0x253d('0x40')][_0x253d('0x41')]){if(!_[_0x253d('0x42')](_0xf27244,_0x4d7e9e['query'][_0x253d('0x41')][_0x253d('0x43')]()))throw new Error(_0x253d('0x44'));_0x539151=_0x4d7e9e['query'][_0x253d('0x41')][_0x253d('0x43')]();}var _0x2f8bb6=_[_0x253d('0x45')](_0x539151)+_0x253d('0x46');var _0xfd91bb=_[_0x253d('0x47')](db[_0x2f8bb6][_0x253d('0x48')],function(_0x3a60a6){return{'name':_0x3a60a6[_0x253d('0x49')],'type':_0x3a60a6[_0x253d('0x4a')]['key']};});_0x4a7a80=qs[_0x253d('0x4b')](_0xfd91bb,_0x4d7e9e);if(_0x4d7e9e[_0x253d('0x4c')][_0x253d('0x4d')]==='user'){var _0x17fd92={'params':{'id':_0x4d7e9e[_0x253d('0x4c')]['userProfileId']},'query':{'section':_0x539151===_0x253d('0x3e')&&_0x4d7e9e[_0x253d('0x40')][_0x253d('0x4a')]===_0x253d('0x4e')?'QueueCampaigns':_[_0x253d('0x45')](_0x539151)+'Queues','type':_0x4d7e9e[_0x253d('0x40')]['type'],'nolimit':!![],'sort':'name','filter':_0x4d7e9e[_0x253d('0x40')][_0x253d('0x4f')]}};return db[_0x253d('0x50')][_0x253d('0x51')](_0x17fd92);}return db[_0x2f8bb6][_0x253d('0x52')](_0x4a7a80);})[_0x253d('0x1d')](function(_0x5aa90a){_0x2ec8ce[_0x253d('0x2e')]=_0x5aa90a[_0x253d('0x2e')];var _0x179534=_0x539151===_0x253d('0x3e')?client:routingClient;return _0x179534['request'](_[_0x253d('0x45')](_0x539151)+_0x253d('0x53'),_0x4d7e9e)[_0x253d('0x1d')](function(_0x423046){if(_0x423046[_0x253d('0x23')])throw new Error(util['format']('Unable\x20to\x20retrieve\x20RPC\x20%s\x20queues',_0x539151));var _0x1b37a6=_0x423046['result'];var _0xa474ab=[_0x253d('0x54'),'available',_0x253d('0x55'),_0x253d('0x56'),'paused'];if(_0x539151===_0x253d('0x3e')&&_0x4d7e9e[_0x253d('0x40')][_0x253d('0x4a')]==='outbound'){_0xa474ab[_0x253d('0x57')](_0x253d('0x58'),'originated',_0x253d('0x25'),_0x253d('0x59'),_0x253d('0x5a'),_0x253d('0x5b'),_0x253d('0x5c'),_0x253d('0x5d'),'erlangCalls','erlangCallToSecond',_0x253d('0x5e'),_0x253d('0x5f'),_0x253d('0x60'),_0x253d('0x61'),'dialPredictiveIntervalMinThreshold',_0x253d('0x62'),_0x253d('0x63'),_0x253d('0x64'),_0x253d('0x65'),_0x253d('0x66'),_0x253d('0x67'),_0x253d('0x68'));}var _0x1d9a33=_0x5aa90a[_0x253d('0x69')]['map'](function(_0x15a388){var _0x5d34f9=_0x15a388[_0x253d('0x6a')]({'plain':!![]});_0x5d34f9['paused']=0x0;_0x5d34f9[_0x253d('0x6b')]=0x0;_0x5d34f9[_0x253d('0x54')]=0x0;_0x5d34f9[_0x253d('0x6c')]=0x0;_0x5d34f9[_0x253d('0x6d')]=0x0;_0x5d34f9[_0x253d('0x55')]=0x0;if(_0x539151===_0x253d('0x3e')){_0x5d34f9['talking']=0x0;if(_0x4d7e9e[_0x253d('0x40')][_0x253d('0x4a')]===_0x253d('0x4e')){_0x5d34f9['pTalking']=0x0;_0x5d34f9[_0x253d('0x6e')]=0x0;}}_0x5d34f9[_0x253d('0x6f')]=[];var _0x12e65b=_['find'](_0x1b37a6[_0x253d('0x69')],['id',_0x5d34f9['id']]);if(_0x12e65b)_[_0x253d('0x70')](_0x5d34f9,_[_0x253d('0x71')](_0x12e65b,_0xa474ab));if(_0x539151===_0x253d('0x3e')){return _0x15a388[_0x253d('0x72')]({'attributes':['id',_0x253d('0x37'),'online'],'joinTableAttributes':[],'raw':!![]})['then'](function(_0x4e60f7){return _0x15a388[_0x253d('0x73')]({'raw':!![]})[_0x253d('0x1d')](function(_0x303cfd){for(var _0x4f2f66=0x0;_0x4f2f66<_0x4e60f7[_0x253d('0x74')];_0x4f2f66++){var _0x4181dd=_[_0x253d('0x75')](_0x303cfd,['UserId',_0x4e60f7[_0x4f2f66]['id']]);var _0x51c619={'id':_0x4e60f7[_0x4f2f66]['id'],'name':_0x4e60f7[_0x4f2f66][_0x253d('0x37')],'online':Boolean(_0x4e60f7[_0x4f2f66][_0x253d('0x76')]),'paused':_0x4181dd?Boolean(_0x4181dd[_0x253d('0x6d')]):![]};if(_0x51c619[_0x253d('0x76')])_0x5d34f9[_0x253d('0x6b')]++;if(_0x51c619[_0x253d('0x6d')])_0x5d34f9[_0x253d('0x6d')]++;_0x5d34f9[_0x253d('0x6f')]['push'](_0x51c619);}return _0x5d34f9;});});}else{var _0x4b992e=_0x253d('0x77')+_[_0x253d('0x45')](_0x539151)+'Queue';var _0x4b37f2=_['startCase'](_0x539151)+_0x253d('0x78');var _0x4dde9a=_0x539151+_0x253d('0x79');return db[_0x4b992e][_0x253d('0x7a')]({'where':{['userQueueKey']:_0x15a388['id']},'raw':!![]})['then'](function(_0x188cac){return db[_0x253d('0x77')][_0x253d('0x7a')]({'attributes':['id',_0x253d('0x37'),_0x253d('0x76'),_0x4dde9a],'where':{'id':_['map'](_0x188cac,_0x253d('0x7b')),'role':'agent'},'raw':!![]});})['then'](function(_0x159dd8){for(var _0x5c01e4=0x0;_0x5c01e4<_0x159dd8[_0x253d('0x74')];_0x5c01e4++){var _0x317676={'id':_0x159dd8[_0x5c01e4]['id'],'name':_0x159dd8[_0x5c01e4]['name'],'online':Boolean(_0x159dd8[_0x5c01e4]['online']),'paused':Boolean(_0x159dd8[_0x5c01e4][_0x4dde9a])};if(_0x317676[_0x253d('0x76')])_0x5d34f9['loggedInDb']++;if(_0x317676[_0x253d('0x6d')])_0x5d34f9['paused']++;_0x5d34f9[_0x253d('0x6f')][_0x253d('0x57')](_0x317676);}return _0x5d34f9;});}});return Promise['all'](_0x1d9a33);});})[_0x253d('0x1d')](function(_0x26c440){_0x2ec8ce[_0x253d('0x69')]=_0x26c440;return _0x2ec8ce;})[_0x253d('0x1d')](respondWithFilteredResult(_0x5aabce,_0x4a7a80))[_0x253d('0x27')](handleError(_0x5aabce,null));};