Built motion from commit b5413ace.|2.6.9
[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 _0x49b7=['Unable\x20to\x20associate\x20the\x20resource\x20to\x20the\x20User\x20Profile:\x20%s','update','description','recordingFormat','cutdigits','mandatoryDisposition','alias','context','addApplications','sequelize','transaction','ISOLATION_LEVELS','READ_COMMITTED','isApp','createRouting','bulkCreate','findAndCountAll','cast(priority\x20as\x20unsigned)','finally','lodash','ioredis','../../components/parsers/qs','../../config/logger','../../mysqldb','redis','defaults','sendStatus','end','status','json','offset','undefined','limit','count','Content-Range','destroy','then','get','inbound','OutboundRoutes','outbound','InternalRoutes','internal','filter','ignore','map','value','includes','type','find','name','error','send','index','rawAttributes','fieldName','model','query','intersection','attributes','fields','length','hasOwnProperty','order','sort','where','filters','pick','VIRTUAL','merge','options','VoiceExtension','includeAll','include','findAll','catch','keys','params','create','startsWith','body','exten','concat','Unable\x20to\x20retrieve\x20the\x20current\x20user','role','user','InboundRoutes','UserProfileSection','userProfileId','autoAssociation','UserProfileResource'];(function(_0x353660,_0x48b645){var _0x339760=function(_0x5ee4d8){while(--_0x5ee4d8){_0x353660['push'](_0x353660['shift']());}};_0x339760(++_0x48b645);}(_0x49b7,0x1da));var _0x749b=function(_0x10aa73,_0xc744f4){_0x10aa73=_0x10aa73-0x0;var _0x3496d9=_0x49b7[_0x10aa73];return _0x3496d9;};'use strict';var _=require(_0x749b('0x0'));var Redis=require(_0x749b('0x1'));var qs=require(_0x749b('0x2'));var logger=require(_0x749b('0x3'))('api');var config=require('../../config/environment');var db=require(_0x749b('0x4'))['db'];config[_0x749b('0x5')]=_[_0x749b('0x6')](config['redis'],{'host':'localhost','port':0x18eb});var socket=require('socket.io-emitter')(new Redis(config[_0x749b('0x5')]));require('./voiceExtension.socket')['register'](socket);function respondWithStatusCode(_0x5040ff,_0x261301){_0x261301=_0x261301||0xcc;return function(_0x4e04cb){if(_0x4e04cb){return _0x5040ff[_0x749b('0x7')](_0x261301);}return _0x5040ff['status'](_0x261301)[_0x749b('0x8')]();};}function respondWithResult(_0x55069f,_0x28b89c){_0x28b89c=_0x28b89c||0xc8;return function(_0x5d1881){if(_0x5d1881){return _0x55069f[_0x749b('0x9')](_0x28b89c)[_0x749b('0xa')](_0x5d1881);}};}function respondWithFilteredResult(_0x1ab1c3,_0x50f921){return function(_0x2f6a3d){if(_0x2f6a3d){var _0x35186a=typeof _0x50f921[_0x749b('0xb')]===_0x749b('0xc')&&typeof _0x50f921[_0x749b('0xd')]===_0x749b('0xc');var _0xcfb41d=_0x2f6a3d[_0x749b('0xe')];var _0x5838e9=_0x35186a?0x0:_0x50f921[_0x749b('0xb')];var _0x5c627d=_0x35186a?_0x2f6a3d['count']:_0x50f921[_0x749b('0xb')]+_0x50f921[_0x749b('0xd')];var _0x285aaa;if(_0x5c627d>=_0xcfb41d){_0x5c627d=_0xcfb41d;_0x285aaa=0xc8;}else{_0x285aaa=0xce;}_0x1ab1c3[_0x749b('0x9')](_0x285aaa);return _0x1ab1c3['set'](_0x749b('0xf'),_0x5838e9+'-'+_0x5c627d+'/'+_0xcfb41d)[_0x749b('0xa')](_0x2f6a3d);}return null;};}function removeEntity(_0x490642){return function(_0x331f48){if(_0x331f48){return _0x331f48[_0x749b('0x10')]()[_0x749b('0x11')](function(){var _0x498771=_0x331f48[_0x749b('0x12')]({'plain':!![]});var _0x2b6454=[{'name':'InboundRoutes','value':_0x749b('0x13'),'ignore':![]},{'name':_0x749b('0x14'),'value':_0x749b('0x15'),'ignore':![]},{'name':_0x749b('0x16'),'value':_0x749b('0x17'),'ignore':![]}];var _0x241fc6=_(_0x2b6454)[_0x749b('0x18')](_0x749b('0x19'))[_0x749b('0x1a')](_0x749b('0x1b'))['value']();if(_[_0x749b('0x1c')](_0x241fc6,_0x498771[_0x749b('0x1d')])){return;}var _0x303eeb=_[_0x749b('0x1e')](_0x2b6454,[_0x749b('0x1b'),_0x498771[_0x749b('0x1d')]])[_0x749b('0x1f')];return db['UserProfileResource'][_0x749b('0x10')]({'where':{'type':_0x303eeb,'resourceId':_0x498771['id']}})[_0x749b('0x11')](function(){return _0x331f48;});})[_0x749b('0x11')](function(){_0x490642[_0x749b('0x9')](0xcc)['end']();});}};}function handleEntityNotFound(_0x4fa06f){return function(_0x4783a0){if(!_0x4783a0){_0x4fa06f[_0x749b('0x7')](0x194);}return _0x4783a0;};}function handleError(_0x4d84cd,_0x3cbea6){_0x3cbea6=_0x3cbea6||0x1f4;return function(_0x5cbd80){logger[_0x749b('0x20')](_0x5cbd80['stack']);if(_0x5cbd80[_0x749b('0x1f')]){delete _0x5cbd80[_0x749b('0x1f')];}_0x4d84cd[_0x749b('0x9')](_0x3cbea6)[_0x749b('0x21')](_0x5cbd80);};}exports[_0x749b('0x22')]=function(_0x132c90,_0x143ab1){var _0x4dcd0f={},_0x46d4aa={},_0x2f5888={'count':0x0,'rows':[]};var _0x321b13=_[_0x749b('0x1a')](db['VoiceExtension'][_0x749b('0x23')],function(_0x4fa486){return{'name':_0x4fa486[_0x749b('0x24')],'type':_0x4fa486['type']['key']};});_0x46d4aa[_0x749b('0x25')]=_[_0x749b('0x1a')](_0x321b13,_0x749b('0x1f'));_0x46d4aa['query']=_['keys'](_0x132c90[_0x749b('0x26')]);_0x46d4aa['filters']=_[_0x749b('0x27')](_0x46d4aa['model'],_0x46d4aa[_0x749b('0x26')]);_0x4dcd0f[_0x749b('0x28')]=_[_0x749b('0x27')](_0x46d4aa[_0x749b('0x25')],qs[_0x749b('0x29')](_0x132c90['query'][_0x749b('0x29')]));_0x4dcd0f[_0x749b('0x28')]=_0x4dcd0f['attributes'][_0x749b('0x2a')]?_0x4dcd0f['attributes']:_0x46d4aa[_0x749b('0x25')];if(!_0x132c90[_0x749b('0x26')][_0x749b('0x2b')]('nolimit')){_0x4dcd0f[_0x749b('0xd')]=qs['limit'](_0x132c90[_0x749b('0x26')][_0x749b('0xd')]);_0x4dcd0f['offset']=qs['offset'](_0x132c90['query'][_0x749b('0xb')]);}_0x4dcd0f[_0x749b('0x2c')]=qs[_0x749b('0x2d')](_0x132c90[_0x749b('0x26')][_0x749b('0x2d')]);_0x4dcd0f[_0x749b('0x2e')]=qs[_0x749b('0x2f')](_[_0x749b('0x30')](_0x132c90[_0x749b('0x26')],_0x46d4aa[_0x749b('0x2f')]),_0x321b13);if(_0x132c90[_0x749b('0x26')][_0x749b('0x18')]){_0x4dcd0f[_0x749b('0x2e')]=_['merge'](_0x4dcd0f['where'],{'$or':_[_0x749b('0x1a')](_0x321b13,function(_0x23cc1d){if(_0x23cc1d[_0x749b('0x1d')]!==_0x749b('0x31')){var _0x54c01b={};_0x54c01b[_0x23cc1d[_0x749b('0x1f')]]={'$like':'%'+_0x132c90['query']['filter']+'%'};return _0x54c01b;}})});}_0x4dcd0f=_[_0x749b('0x32')]({},_0x4dcd0f,_0x132c90[_0x749b('0x33')]);var _0x45881d={'where':_0x4dcd0f[_0x749b('0x2e')]};return db[_0x749b('0x34')][_0x749b('0xe')](_0x45881d)[_0x749b('0x11')](function(_0x495320){_0x2f5888[_0x749b('0xe')]=_0x495320;if(_0x132c90[_0x749b('0x26')][_0x749b('0x35')]){_0x4dcd0f[_0x749b('0x36')]=[{'all':!![]}];}return db[_0x749b('0x34')][_0x749b('0x37')](_0x4dcd0f);})[_0x749b('0x11')](function(_0x2ef62c){_0x2f5888['rows']=_0x2ef62c;return _0x2f5888;})[_0x749b('0x11')](respondWithFilteredResult(_0x143ab1,_0x4dcd0f))[_0x749b('0x38')](handleError(_0x143ab1,null));};exports['show']=function(_0x4859bb,_0x1b0a04){var _0x488937={'raw':![],'where':{'id':_0x4859bb['params']['id']}},_0x4e7d8e={};_0x4e7d8e['model']=_[_0x749b('0x39')](db[_0x749b('0x34')][_0x749b('0x23')]);_0x4e7d8e[_0x749b('0x26')]=_[_0x749b('0x39')](_0x4859bb[_0x749b('0x26')]);_0x4e7d8e[_0x749b('0x2f')]=_['intersection'](_0x4e7d8e['model'],_0x4e7d8e[_0x749b('0x26')]);_0x488937[_0x749b('0x28')]=_[_0x749b('0x27')](_0x4e7d8e[_0x749b('0x25')],qs['fields'](_0x4859bb[_0x749b('0x26')][_0x749b('0x29')]));_0x488937[_0x749b('0x28')]=_0x488937[_0x749b('0x28')][_0x749b('0x2a')]?_0x488937[_0x749b('0x28')]:_0x4e7d8e[_0x749b('0x25')];if(_0x4859bb['query'][_0x749b('0x35')]){_0x488937[_0x749b('0x36')]=[{'all':!![]}];}_0x488937=_[_0x749b('0x32')]({},_0x488937,_0x4859bb['options']);return db[_0x749b('0x34')][_0x749b('0x1e')](_0x488937)['then'](handleEntityNotFound(_0x1b0a04,null))[_0x749b('0x11')](respondWithResult(_0x1b0a04,null))[_0x749b('0x38')](handleError(_0x1b0a04,null));};exports[_0x749b('0x10')]=function(_0x4e5bc4,_0x450dec){return db[_0x749b('0x34')]['find']({'where':{'id':_0x4e5bc4[_0x749b('0x3a')]['id']}})[_0x749b('0x11')](handleEntityNotFound(_0x450dec,null))[_0x749b('0x11')](removeEntity(_0x450dec,null))['catch'](handleError(_0x450dec,null));};exports[_0x749b('0x3b')]=function(_0x3dcba3,_0x2fd636){if(_0x3dcba3['body'][_0x749b('0x1d')]==='outbound'&&!_[_0x749b('0x3c')](_0x3dcba3['body']['exten'],'_')){_0x3dcba3[_0x749b('0x3d')][_0x749b('0x3e')]='_'[_0x749b('0x3f')](_0x3dcba3[_0x749b('0x3d')]['exten']);}return db[_0x749b('0x34')][_0x749b('0x3b')](_0x3dcba3[_0x749b('0x3d')],{'raw':![]})[_0x749b('0x11')](function(_0x45a319){var _0x20232e=_0x3dcba3['user'][_0x749b('0x12')]({'plain':!![]});if(!_0x20232e)throw new Error(_0x749b('0x40'));if(_0x20232e[_0x749b('0x41')]===_0x749b('0x42')){var _0x41f7e1=_0x45a319[_0x749b('0x12')]({'plain':!![]});var _0xdda48=[{'name':_0x749b('0x43'),'value':_0x749b('0x13')},{'name':_0x749b('0x14'),'value':_0x749b('0x15')},{'name':_0x749b('0x16'),'value':_0x749b('0x17')}];var _0x3553f6=_['find'](_0xdda48,[_0x749b('0x1b'),_0x41f7e1['type']])[_0x749b('0x1f')];return db[_0x749b('0x44')][_0x749b('0x1e')]({'where':{'name':_0x3553f6,'userProfileId':_0x20232e[_0x749b('0x45')]},'raw':!![]})[_0x749b('0x11')](function(_0x10f3e5){if(_0x10f3e5&&_0x10f3e5[_0x749b('0x46')]===0x0){return db[_0x749b('0x47')][_0x749b('0x3b')]({'name':_0x41f7e1['name'],'resourceId':_0x41f7e1['id'],'type':_0x10f3e5['name'],'sectionId':_0x10f3e5['id']},{})[_0x749b('0x11')](function(){return _0x45a319;});}else{return _0x45a319;}})[_0x749b('0x38')](function(_0x2ee4a0){logger['error'](_0x749b('0x48'),_0x2ee4a0);throw _0x2ee4a0;});}return _0x45a319;})[_0x749b('0x11')](respondWithResult(_0x2fd636,0xc9))['catch'](handleError(_0x2fd636,null));};exports[_0x749b('0x49')]=function(_0x2975ef,_0x2762ba){if(_0x2975ef[_0x749b('0x3d')]['id']){delete _0x2975ef[_0x749b('0x3d')]['id'];}return db[_0x749b('0x34')][_0x749b('0x1e')]({'where':{'id':_0x2975ef[_0x749b('0x3a')]['id']}})[_0x749b('0x11')](handleEntityNotFound(_0x2762ba,null))[_0x749b('0x11')](function(_0x3395cf){if(_0x3395cf){return db['VoiceExtension']['update'](_['pick'](_0x2975ef[_0x749b('0x3d')],[_0x749b('0x3e'),'context',_0x749b('0x4a'),_0x749b('0x4b'),_0x749b('0x4c'),_0x749b('0x4d'),_0x749b('0x4e')]),{'where':{'context':_0x3395cf[_0x749b('0x4f')],'exten':_0x3395cf[_0x749b('0x3e')]},'individualHooks':!![]})[_0x749b('0x11')](function(_0x5c7b0b){return _0x5c7b0b;});}return null;})[_0x749b('0x11')](respondWithStatusCode(_0x2762ba,null))[_0x749b('0x38')](handleError(_0x2762ba,null));};exports[_0x749b('0x50')]=function(_0x29681a,_0x298faa){var _0x2183fb=_0x29681a['params']['id'];var _0x553722=_0x29681a[_0x749b('0x3d')];var _0x4a085b=0xc8;var _0x2295f7=null;return db[_0x749b('0x51')][_0x749b('0x52')]({'isolationLevel':db[_0x749b('0x51')]['Transaction'][_0x749b('0x53')][_0x749b('0x54')]},function(_0x2758af){return db[_0x749b('0x34')]['findOne']({'where':{'id':_0x2183fb},'transaction':_0x2758af})['then'](function(_0x4a5978){if(_0x4a5978){return db['VoiceExtension'][_0x749b('0x10')]({'where':{'VoiceExtensionId':_0x2183fb},'transaction':_0x2758af})[_0x749b('0x11')](function(){var _0x450ef7=_['map'](_0x553722,function(_0x49b0ad){_0x49b0ad['VoiceExtensionId']=_0x2183fb;_0x49b0ad[_0x749b('0x55')]=!![];return _0x49b0ad;});return _0x4a5978[_0x749b('0x56')](_0x450ef7);})['then'](function(_0x35e565){return db[_0x749b('0x34')][_0x749b('0x57')](_0x35e565,{'transaction':_0x2758af});});}else{_0x4a085b=0x194;_0x2295f7=[];}});})[_0x749b('0x11')](function(){if(_0x4a085b!==0x194){return db[_0x749b('0x34')][_0x749b('0x58')]({'where':{'isApp':!![],'VoiceExtensionId':_0x2183fb},'order':_0x749b('0x59')})[_0x749b('0x11')](function(_0xda7da9){_0x2295f7=_0xda7da9;});}})[_0x749b('0x38')](function(_0x43c4b4){_0x4a085b=0x1f4;logger[_0x749b('0x20')](_0x43c4b4['stack']);if(_0x43c4b4[_0x749b('0x1f')]){delete _0x43c4b4[_0x749b('0x1f')];}_0x2295f7=_0x43c4b4;})[_0x749b('0x5a')](function(){if(_0x2295f7===null){_0x298faa[_0x749b('0x7')](_0x4a085b);}else{if(_0x4a085b===0x1f4){_0x298faa[_0x749b('0x9')](_0x4a085b)[_0x749b('0x21')](_0x2295f7);}else{_0x298faa[_0x749b('0x9')](_0x4a085b)['json'](_0x2295f7);}}});};