Built motion from commit 449495f6.|2.6.27
[motion2.git] / server / api / voiceExtension / voiceExtension.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 _0x3835=['where','pick','merge','VIRTUAL','includeAll','include','findAll','rows','params','keys','options','catch','create','body','exten','concat','user','Unable\x20to\x20retrieve\x20the\x20current\x20user','OutboundRoutes','outbound','UserProfileSection','userProfileId','autoAssociation','Unable\x20to\x20associate\x20the\x20resource\x20to\x20the\x20User\x20Profile:\x20%s','update','context','description','cutdigits','mandatoryDisposition','sequelize','transaction','Transaction','READ_COMMITTED','findOne','VoiceExtensionId','createRouting','bulkCreate','findAndCountAll','cast(priority\x20as\x20unsigned)','finally','lodash','ioredis','../../config/logger','../../config/environment','defaults','redis','localhost','socket.io-emitter','register','sendStatus','status','end','json','offset','undefined','count','destroy','then','get','inbound','InternalRoutes','internal','filter','map','includes','type','find','value','name','UserProfileResource','error','stack','send','VoiceExtension','rawAttributes','key','model','query','filters','intersection','fields','attributes','hasOwnProperty','nolimit','limit','order','sort'];(function(_0x1dd48a,_0x839bf1){var _0x379d7d=function(_0x1d3409){while(--_0x1d3409){_0x1dd48a['push'](_0x1dd48a['shift']());}};_0x379d7d(++_0x839bf1);}(_0x3835,0x1db));var _0x5383=function(_0x5d9c37,_0x518b93){_0x5d9c37=_0x5d9c37-0x0;var _0x1ba9eb=_0x3835[_0x5d9c37];return _0x1ba9eb;};'use strict';var _=require(_0x5383('0x0'));var Redis=require(_0x5383('0x1'));var qs=require('../../components/parsers/qs');var logger=require(_0x5383('0x2'))('api');var config=require(_0x5383('0x3'));var db=require('../../mysqldb')['db'];config['redis']=_[_0x5383('0x4')](config[_0x5383('0x5')],{'host':_0x5383('0x6'),'port':0x18eb});var socket=require(_0x5383('0x7'))(new Redis(config[_0x5383('0x5')]));require('./voiceExtension.socket')[_0x5383('0x8')](socket);function respondWithStatusCode(_0x363cf8,_0x3bd32){_0x3bd32=_0x3bd32||0xcc;return function(_0x2a0e64){if(_0x2a0e64){return _0x363cf8[_0x5383('0x9')](_0x3bd32);}return _0x363cf8[_0x5383('0xa')](_0x3bd32)[_0x5383('0xb')]();};}function respondWithResult(_0x410ab4,_0x559eae){_0x559eae=_0x559eae||0xc8;return function(_0x3ce92a){if(_0x3ce92a){return _0x410ab4[_0x5383('0xa')](_0x559eae)[_0x5383('0xc')](_0x3ce92a);}};}function respondWithFilteredResult(_0x44dc97,_0x31b5e3){return function(_0x2a50b1){if(_0x2a50b1){var _0x18eacf=typeof _0x31b5e3[_0x5383('0xd')]===_0x5383('0xe')&&typeof _0x31b5e3['limit']===_0x5383('0xe');var _0x13801e=_0x2a50b1[_0x5383('0xf')];var _0x3022a0=_0x18eacf?0x0:_0x31b5e3[_0x5383('0xd')];var _0x129406=_0x18eacf?_0x2a50b1[_0x5383('0xf')]:_0x31b5e3[_0x5383('0xd')]+_0x31b5e3['limit'];var _0x53a7f7;if(_0x129406>=_0x13801e){_0x129406=_0x13801e;_0x53a7f7=0xc8;}else{_0x53a7f7=0xce;}_0x44dc97[_0x5383('0xa')](_0x53a7f7);return _0x44dc97['set']('Content-Range',_0x3022a0+'-'+_0x129406+'/'+_0x13801e)[_0x5383('0xc')](_0x2a50b1);}return null;};}function removeEntity(_0x548525){return function(_0x4ca797){if(_0x4ca797){return _0x4ca797[_0x5383('0x10')]()[_0x5383('0x11')](function(){var _0x196c93=_0x4ca797[_0x5383('0x12')]({'plain':!![]});var _0x35e228=[{'name':'InboundRoutes','value':_0x5383('0x13'),'ignore':![]},{'name':'OutboundRoutes','value':'outbound','ignore':![]},{'name':_0x5383('0x14'),'value':_0x5383('0x15'),'ignore':![]}];var _0x4c1911=_(_0x35e228)[_0x5383('0x16')]('ignore')[_0x5383('0x17')]('value')['value']();if(_[_0x5383('0x18')](_0x4c1911,_0x196c93[_0x5383('0x19')])){return;}var _0x1e6b80=_[_0x5383('0x1a')](_0x35e228,[_0x5383('0x1b'),_0x196c93[_0x5383('0x19')]])[_0x5383('0x1c')];return db[_0x5383('0x1d')][_0x5383('0x10')]({'where':{'type':_0x1e6b80,'resourceId':_0x196c93['id']}})[_0x5383('0x11')](function(){return _0x4ca797;});})[_0x5383('0x11')](function(){_0x548525[_0x5383('0xa')](0xcc)[_0x5383('0xb')]();});}};}function handleEntityNotFound(_0x32ae53){return function(_0x5b1cc9){if(!_0x5b1cc9){_0x32ae53[_0x5383('0x9')](0x194);}return _0x5b1cc9;};}function handleError(_0x1bf8ab,_0x1387ac){_0x1387ac=_0x1387ac||0x1f4;return function(_0x34c8ae){logger[_0x5383('0x1e')](_0x34c8ae[_0x5383('0x1f')]);if(_0x34c8ae[_0x5383('0x1c')]){delete _0x34c8ae['name'];}_0x1bf8ab[_0x5383('0xa')](_0x1387ac)[_0x5383('0x20')](_0x34c8ae);};}exports['index']=function(_0x2ae80c,_0x2b8fb8){var _0xaf111c={},_0x106e31={},_0x39812d={'count':0x0,'rows':[]};var _0x149291=_[_0x5383('0x17')](db[_0x5383('0x21')][_0x5383('0x22')],function(_0x566980){return{'name':_0x566980['fieldName'],'type':_0x566980[_0x5383('0x19')][_0x5383('0x23')]};});_0x106e31[_0x5383('0x24')]=_[_0x5383('0x17')](_0x149291,_0x5383('0x1c'));_0x106e31['query']=_['keys'](_0x2ae80c[_0x5383('0x25')]);_0x106e31[_0x5383('0x26')]=_[_0x5383('0x27')](_0x106e31['model'],_0x106e31[_0x5383('0x25')]);_0xaf111c['attributes']=_[_0x5383('0x27')](_0x106e31[_0x5383('0x24')],qs[_0x5383('0x28')](_0x2ae80c[_0x5383('0x25')]['fields']));_0xaf111c[_0x5383('0x29')]=_0xaf111c[_0x5383('0x29')]['length']?_0xaf111c[_0x5383('0x29')]:_0x106e31[_0x5383('0x24')];if(!_0x2ae80c[_0x5383('0x25')][_0x5383('0x2a')](_0x5383('0x2b'))){_0xaf111c[_0x5383('0x2c')]=qs[_0x5383('0x2c')](_0x2ae80c['query'][_0x5383('0x2c')]);_0xaf111c[_0x5383('0xd')]=qs[_0x5383('0xd')](_0x2ae80c[_0x5383('0x25')][_0x5383('0xd')]);}_0xaf111c[_0x5383('0x2d')]=qs[_0x5383('0x2e')](_0x2ae80c[_0x5383('0x25')][_0x5383('0x2e')]);_0xaf111c[_0x5383('0x2f')]=qs['filters'](_[_0x5383('0x30')](_0x2ae80c['query'],_0x106e31[_0x5383('0x26')]),_0x149291);if(_0x2ae80c[_0x5383('0x25')][_0x5383('0x16')]){_0xaf111c['where']=_[_0x5383('0x31')](_0xaf111c['where'],{'$or':_[_0x5383('0x17')](_0x149291,function(_0x5a450b){if(_0x5a450b[_0x5383('0x19')]!==_0x5383('0x32')){var _0x39686a={};_0x39686a[_0x5a450b['name']]={'$like':'%'+_0x2ae80c[_0x5383('0x25')]['filter']+'%'};return _0x39686a;}})});}_0xaf111c=_['merge']({},_0xaf111c,_0x2ae80c['options']);var _0x1341a1={'where':_0xaf111c['where']};return db[_0x5383('0x21')][_0x5383('0xf')](_0x1341a1)['then'](function(_0xc322d6){_0x39812d[_0x5383('0xf')]=_0xc322d6;if(_0x2ae80c[_0x5383('0x25')][_0x5383('0x33')]){_0xaf111c[_0x5383('0x34')]=[{'all':!![]}];}return db[_0x5383('0x21')][_0x5383('0x35')](_0xaf111c);})['then'](function(_0x5ccfd6){_0x39812d[_0x5383('0x36')]=_0x5ccfd6;return _0x39812d;})[_0x5383('0x11')](respondWithFilteredResult(_0x2b8fb8,_0xaf111c))['catch'](handleError(_0x2b8fb8,null));};exports['show']=function(_0x289a7a,_0x430d79){var _0x16dace={'raw':![],'where':{'id':_0x289a7a[_0x5383('0x37')]['id']}},_0x17e933={};_0x17e933[_0x5383('0x24')]=_[_0x5383('0x38')](db[_0x5383('0x21')][_0x5383('0x22')]);_0x17e933[_0x5383('0x25')]=_['keys'](_0x289a7a[_0x5383('0x25')]);_0x17e933[_0x5383('0x26')]=_['intersection'](_0x17e933[_0x5383('0x24')],_0x17e933[_0x5383('0x25')]);_0x16dace[_0x5383('0x29')]=_[_0x5383('0x27')](_0x17e933[_0x5383('0x24')],qs[_0x5383('0x28')](_0x289a7a[_0x5383('0x25')][_0x5383('0x28')]));_0x16dace[_0x5383('0x29')]=_0x16dace[_0x5383('0x29')]['length']?_0x16dace[_0x5383('0x29')]:_0x17e933[_0x5383('0x24')];if(_0x289a7a[_0x5383('0x25')]['includeAll']){_0x16dace[_0x5383('0x34')]=[{'all':!![]}];}_0x16dace=_[_0x5383('0x31')]({},_0x16dace,_0x289a7a[_0x5383('0x39')]);return db[_0x5383('0x21')][_0x5383('0x1a')](_0x16dace)[_0x5383('0x11')](handleEntityNotFound(_0x430d79,null))[_0x5383('0x11')](respondWithResult(_0x430d79,null))['catch'](handleError(_0x430d79,null));};exports[_0x5383('0x10')]=function(_0x1c5a55,_0x2011ce){return db['VoiceExtension'][_0x5383('0x1a')]({'where':{'id':_0x1c5a55[_0x5383('0x37')]['id']}})[_0x5383('0x11')](handleEntityNotFound(_0x2011ce,null))[_0x5383('0x11')](removeEntity(_0x2011ce,null))[_0x5383('0x3a')](handleError(_0x2011ce,null));};exports[_0x5383('0x3b')]=function(_0x41658d,_0x41ce3b){if(_0x41658d[_0x5383('0x3c')][_0x5383('0x19')]==='outbound'&&!_['startsWith'](_0x41658d[_0x5383('0x3c')]['exten'],'_')){_0x41658d['body'][_0x5383('0x3d')]='_'[_0x5383('0x3e')](_0x41658d[_0x5383('0x3c')][_0x5383('0x3d')]);}return db[_0x5383('0x21')][_0x5383('0x3b')](_0x41658d[_0x5383('0x3c')],{'raw':![]})[_0x5383('0x11')](function(_0x594f31){var _0x7becd9=_0x41658d[_0x5383('0x3f')][_0x5383('0x12')]({'plain':!![]});if(!_0x7becd9)throw new Error(_0x5383('0x40'));if(_0x7becd9['role']==='user'){var _0x45abb6=_0x594f31[_0x5383('0x12')]({'plain':!![]});var _0x2cc25a=[{'name':'InboundRoutes','value':_0x5383('0x13')},{'name':_0x5383('0x41'),'value':_0x5383('0x42')},{'name':_0x5383('0x14'),'value':_0x5383('0x15')}];var _0x46809c=_[_0x5383('0x1a')](_0x2cc25a,[_0x5383('0x1b'),_0x45abb6[_0x5383('0x19')]])['name'];return db[_0x5383('0x43')][_0x5383('0x1a')]({'where':{'name':_0x46809c,'userProfileId':_0x7becd9[_0x5383('0x44')]},'raw':!![]})[_0x5383('0x11')](function(_0x5d0e04){if(_0x5d0e04&&_0x5d0e04[_0x5383('0x45')]===0x0){return db['UserProfileResource'][_0x5383('0x3b')]({'name':_0x45abb6[_0x5383('0x1c')],'resourceId':_0x45abb6['id'],'type':_0x5d0e04[_0x5383('0x1c')],'sectionId':_0x5d0e04['id']},{})[_0x5383('0x11')](function(){return _0x594f31;});}else{return _0x594f31;}})[_0x5383('0x3a')](function(_0x5ab3b5){logger['error'](_0x5383('0x46'),_0x5ab3b5);throw _0x5ab3b5;});}return _0x594f31;})[_0x5383('0x11')](respondWithResult(_0x41ce3b,0xc9))['catch'](handleError(_0x41ce3b,null));};exports[_0x5383('0x47')]=function(_0x1f4f58,_0x42f6d0){if(_0x1f4f58[_0x5383('0x3c')]['id']){delete _0x1f4f58[_0x5383('0x3c')]['id'];}return db[_0x5383('0x21')]['find']({'where':{'id':_0x1f4f58[_0x5383('0x37')]['id']}})['then'](handleEntityNotFound(_0x42f6d0,null))[_0x5383('0x11')](function(_0x1ae879){if(_0x1ae879){return db[_0x5383('0x21')][_0x5383('0x47')](_['pick'](_0x1f4f58[_0x5383('0x3c')],[_0x5383('0x3d'),_0x5383('0x48'),_0x5383('0x49'),'recordingFormat',_0x5383('0x4a'),_0x5383('0x4b'),'alias']),{'where':{'context':_0x1ae879[_0x5383('0x48')],'exten':_0x1ae879[_0x5383('0x3d')]},'individualHooks':!![]})['then'](function(_0x1e8f7b){return _0x1e8f7b;});}return null;})[_0x5383('0x11')](respondWithStatusCode(_0x42f6d0,null))[_0x5383('0x3a')](handleError(_0x42f6d0,null));};exports['addApplications']=function(_0x4fe1f5,_0x50f826){var _0x207746=_0x4fe1f5['params']['id'];var _0x17c7a9=_0x4fe1f5[_0x5383('0x3c')];var _0x29cbaf=0xc8;var _0x473b5d=null;return db[_0x5383('0x4c')][_0x5383('0x4d')]({'isolationLevel':db[_0x5383('0x4c')][_0x5383('0x4e')]['ISOLATION_LEVELS'][_0x5383('0x4f')]},function(_0x2f3d31){return db[_0x5383('0x21')][_0x5383('0x50')]({'where':{'id':_0x207746},'transaction':_0x2f3d31})[_0x5383('0x11')](function(_0x16c83d){if(_0x16c83d){return db['VoiceExtension'][_0x5383('0x10')]({'where':{'VoiceExtensionId':_0x207746},'transaction':_0x2f3d31})[_0x5383('0x11')](function(){var _0x223621=_['map'](_0x17c7a9,function(_0x163818){_0x163818[_0x5383('0x51')]=_0x207746;_0x163818['isApp']=!![];return _0x163818;});return _0x16c83d[_0x5383('0x52')](_0x223621);})[_0x5383('0x11')](function(_0x26231d){return db[_0x5383('0x21')][_0x5383('0x53')](_0x26231d,{'transaction':_0x2f3d31});});}else{_0x29cbaf=0x194;_0x473b5d=[];}});})[_0x5383('0x11')](function(){if(_0x29cbaf!==0x194){return db[_0x5383('0x21')][_0x5383('0x54')]({'where':{'isApp':!![],'VoiceExtensionId':_0x207746},'order':_0x5383('0x55')})[_0x5383('0x11')](function(_0x5c9abf){_0x473b5d=_0x5c9abf;});}})[_0x5383('0x3a')](function(_0xeb26e0){_0x29cbaf=0x1f4;logger['error'](_0xeb26e0[_0x5383('0x1f')]);if(_0xeb26e0[_0x5383('0x1c')]){delete _0xeb26e0['name'];}_0x473b5d=_0xeb26e0;})[_0x5383('0x56')](function(){if(_0x473b5d===null){_0x50f826[_0x5383('0x9')](_0x29cbaf);}else{if(_0x29cbaf===0x1f4){_0x50f826[_0x5383('0xa')](_0x29cbaf)['send'](_0x473b5d);}else{_0x50f826[_0x5383('0xa')](_0x29cbaf)['json'](_0x473b5d);}}});};