Built motion from commit 377f727e.|2.6.28
[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 _0xfcf9=['rawAttributes','fieldName','key','keys','query','intersection','model','attributes','fields','length','order','sort','where','filters','pick','merge','VIRTUAL','options','includeAll','include','findAll','rows','catch','show','params','create','outbound','startsWith','body','exten','concat','Unable\x20to\x20retrieve\x20the\x20current\x20user','role','user','InboundRoutes','InternalRoutes','UserProfileSection','userProfileId','error','update','context','description','mandatoryDisposition','addApplications','sequelize','transaction','ISOLATION_LEVELS','VoiceExtensionId','isApp','createRouting','bulkCreate','findAndCountAll','finally','send','ioredis','../../components/parsers/qs','api','../../config/environment','../../mysqldb','defaults','socket.io-emitter','redis','./voiceExtension.socket','register','sendStatus','end','status','json','offset','undefined','limit','count','set','destroy','get','inbound','OutboundRoutes','internal','filter','map','value','includes','find','type','UserProfileResource','then','stack','name','VoiceExtension'];(function(_0x1cc942,_0x5c49b2){var _0x148105=function(_0x3d6061){while(--_0x3d6061){_0x1cc942['push'](_0x1cc942['shift']());}};_0x148105(++_0x5c49b2);}(_0xfcf9,0x141));var _0x9fcf=function(_0x558cba,_0x524c4b){_0x558cba=_0x558cba-0x0;var _0xbcca64=_0xfcf9[_0x558cba];return _0xbcca64;};'use strict';var _=require('lodash');var Redis=require(_0x9fcf('0x0'));var qs=require(_0x9fcf('0x1'));var logger=require('../../config/logger')(_0x9fcf('0x2'));var config=require(_0x9fcf('0x3'));var db=require(_0x9fcf('0x4'))['db'];config['redis']=_[_0x9fcf('0x5')](config['redis'],{'host':'localhost','port':0x18eb});var socket=require(_0x9fcf('0x6'))(new Redis(config[_0x9fcf('0x7')]));require(_0x9fcf('0x8'))[_0x9fcf('0x9')](socket);function respondWithStatusCode(_0xf493ef,_0xa7af58){_0xa7af58=_0xa7af58||0xcc;return function(_0x374721){if(_0x374721){return _0xf493ef[_0x9fcf('0xa')](_0xa7af58);}return _0xf493ef['status'](_0xa7af58)[_0x9fcf('0xb')]();};}function respondWithResult(_0x43991a,_0x22c6dc){_0x22c6dc=_0x22c6dc||0xc8;return function(_0x32cfe6){if(_0x32cfe6){return _0x43991a[_0x9fcf('0xc')](_0x22c6dc)[_0x9fcf('0xd')](_0x32cfe6);}};}function respondWithFilteredResult(_0x4c7315,_0x4b0ff7){return function(_0x12fd32){if(_0x12fd32){var _0x242c9a=typeof _0x4b0ff7[_0x9fcf('0xe')]===_0x9fcf('0xf')&&typeof _0x4b0ff7[_0x9fcf('0x10')]===_0x9fcf('0xf');var _0x3b4fc2=_0x12fd32[_0x9fcf('0x11')];var _0x34005d=_0x242c9a?0x0:_0x4b0ff7[_0x9fcf('0xe')];var _0x296cc4=_0x242c9a?_0x12fd32['count']:_0x4b0ff7[_0x9fcf('0xe')]+_0x4b0ff7[_0x9fcf('0x10')];var _0x179081;if(_0x296cc4>=_0x3b4fc2){_0x296cc4=_0x3b4fc2;_0x179081=0xc8;}else{_0x179081=0xce;}_0x4c7315['status'](_0x179081);return _0x4c7315[_0x9fcf('0x12')]('Content-Range',_0x34005d+'-'+_0x296cc4+'/'+_0x3b4fc2)['json'](_0x12fd32);}return null;};}function removeEntity(_0x210ade){return function(_0x398e54){if(_0x398e54){return _0x398e54[_0x9fcf('0x13')]()['then'](function(){var _0x8ba661=_0x398e54[_0x9fcf('0x14')]({'plain':!![]});var _0x4e148d=[{'name':'InboundRoutes','value':_0x9fcf('0x15'),'ignore':![]},{'name':_0x9fcf('0x16'),'value':'outbound','ignore':![]},{'name':'InternalRoutes','value':_0x9fcf('0x17'),'ignore':![]}];var _0x456a47=_(_0x4e148d)[_0x9fcf('0x18')]('ignore')[_0x9fcf('0x19')](_0x9fcf('0x1a'))['value']();if(_[_0x9fcf('0x1b')](_0x456a47,_0x8ba661['type'])){return;}var _0x5622b0=_[_0x9fcf('0x1c')](_0x4e148d,[_0x9fcf('0x1a'),_0x8ba661[_0x9fcf('0x1d')]])['name'];return db[_0x9fcf('0x1e')][_0x9fcf('0x13')]({'where':{'type':_0x5622b0,'resourceId':_0x8ba661['id']}})[_0x9fcf('0x1f')](function(){return _0x398e54;});})['then'](function(){_0x210ade[_0x9fcf('0xc')](0xcc)[_0x9fcf('0xb')]();});}};}function handleEntityNotFound(_0x512b35){return function(_0x4fe464){if(!_0x4fe464){_0x512b35[_0x9fcf('0xa')](0x194);}return _0x4fe464;};}function handleError(_0x161e5f,_0x8fd87b){_0x8fd87b=_0x8fd87b||0x1f4;return function(_0x1b997e){logger['error'](_0x1b997e[_0x9fcf('0x20')]);if(_0x1b997e[_0x9fcf('0x21')]){delete _0x1b997e[_0x9fcf('0x21')];}_0x161e5f[_0x9fcf('0xc')](_0x8fd87b)['send'](_0x1b997e);};}exports['index']=function(_0x594bd2,_0x3919f2){var _0x26ed89={},_0x9e5bdd={},_0xecb90a={'count':0x0,'rows':[]};var _0x1b5465=_[_0x9fcf('0x19')](db[_0x9fcf('0x22')][_0x9fcf('0x23')],function(_0x2c647c){return{'name':_0x2c647c[_0x9fcf('0x24')],'type':_0x2c647c[_0x9fcf('0x1d')][_0x9fcf('0x25')]};});_0x9e5bdd['model']=_[_0x9fcf('0x19')](_0x1b5465,_0x9fcf('0x21'));_0x9e5bdd['query']=_[_0x9fcf('0x26')](_0x594bd2[_0x9fcf('0x27')]);_0x9e5bdd['filters']=_[_0x9fcf('0x28')](_0x9e5bdd[_0x9fcf('0x29')],_0x9e5bdd[_0x9fcf('0x27')]);_0x26ed89[_0x9fcf('0x2a')]=_[_0x9fcf('0x28')](_0x9e5bdd[_0x9fcf('0x29')],qs[_0x9fcf('0x2b')](_0x594bd2[_0x9fcf('0x27')][_0x9fcf('0x2b')]));_0x26ed89[_0x9fcf('0x2a')]=_0x26ed89['attributes'][_0x9fcf('0x2c')]?_0x26ed89[_0x9fcf('0x2a')]:_0x9e5bdd[_0x9fcf('0x29')];if(!_0x594bd2['query']['hasOwnProperty']('nolimit')){_0x26ed89['limit']=qs['limit'](_0x594bd2[_0x9fcf('0x27')][_0x9fcf('0x10')]);_0x26ed89['offset']=qs[_0x9fcf('0xe')](_0x594bd2['query'][_0x9fcf('0xe')]);}_0x26ed89[_0x9fcf('0x2d')]=qs['sort'](_0x594bd2['query'][_0x9fcf('0x2e')]);_0x26ed89[_0x9fcf('0x2f')]=qs[_0x9fcf('0x30')](_[_0x9fcf('0x31')](_0x594bd2['query'],_0x9e5bdd['filters']),_0x1b5465);if(_0x594bd2[_0x9fcf('0x27')][_0x9fcf('0x18')]){_0x26ed89[_0x9fcf('0x2f')]=_[_0x9fcf('0x32')](_0x26ed89['where'],{'$or':_[_0x9fcf('0x19')](_0x1b5465,function(_0x6e01c){if(_0x6e01c[_0x9fcf('0x1d')]!==_0x9fcf('0x33')){var _0x31102f={};_0x31102f[_0x6e01c['name']]={'$like':'%'+_0x594bd2['query']['filter']+'%'};return _0x31102f;}})});}_0x26ed89=_[_0x9fcf('0x32')]({},_0x26ed89,_0x594bd2[_0x9fcf('0x34')]);var _0x30105d={'where':_0x26ed89[_0x9fcf('0x2f')]};return db[_0x9fcf('0x22')][_0x9fcf('0x11')](_0x30105d)[_0x9fcf('0x1f')](function(_0x55310b){_0xecb90a['count']=_0x55310b;if(_0x594bd2[_0x9fcf('0x27')][_0x9fcf('0x35')]){_0x26ed89[_0x9fcf('0x36')]=[{'all':!![]}];}return db['VoiceExtension'][_0x9fcf('0x37')](_0x26ed89);})['then'](function(_0x3846e6){_0xecb90a[_0x9fcf('0x38')]=_0x3846e6;return _0xecb90a;})['then'](respondWithFilteredResult(_0x3919f2,_0x26ed89))[_0x9fcf('0x39')](handleError(_0x3919f2,null));};exports[_0x9fcf('0x3a')]=function(_0x4398ac,_0x581c19){var _0x3ff16a={'raw':![],'where':{'id':_0x4398ac['params']['id']}},_0x4c9ad9={};_0x4c9ad9[_0x9fcf('0x29')]=_[_0x9fcf('0x26')](db[_0x9fcf('0x22')][_0x9fcf('0x23')]);_0x4c9ad9[_0x9fcf('0x27')]=_[_0x9fcf('0x26')](_0x4398ac[_0x9fcf('0x27')]);_0x4c9ad9['filters']=_['intersection'](_0x4c9ad9[_0x9fcf('0x29')],_0x4c9ad9[_0x9fcf('0x27')]);_0x3ff16a[_0x9fcf('0x2a')]=_[_0x9fcf('0x28')](_0x4c9ad9[_0x9fcf('0x29')],qs[_0x9fcf('0x2b')](_0x4398ac[_0x9fcf('0x27')][_0x9fcf('0x2b')]));_0x3ff16a['attributes']=_0x3ff16a[_0x9fcf('0x2a')]['length']?_0x3ff16a[_0x9fcf('0x2a')]:_0x4c9ad9[_0x9fcf('0x29')];if(_0x4398ac['query'][_0x9fcf('0x35')]){_0x3ff16a['include']=[{'all':!![]}];}_0x3ff16a=_[_0x9fcf('0x32')]({},_0x3ff16a,_0x4398ac[_0x9fcf('0x34')]);return db[_0x9fcf('0x22')]['find'](_0x3ff16a)[_0x9fcf('0x1f')](handleEntityNotFound(_0x581c19,null))[_0x9fcf('0x1f')](respondWithResult(_0x581c19,null))[_0x9fcf('0x39')](handleError(_0x581c19,null));};exports[_0x9fcf('0x13')]=function(_0x11112b,_0x33c77b){return db['VoiceExtension'][_0x9fcf('0x1c')]({'where':{'id':_0x11112b[_0x9fcf('0x3b')]['id']}})[_0x9fcf('0x1f')](handleEntityNotFound(_0x33c77b,null))['then'](removeEntity(_0x33c77b,null))[_0x9fcf('0x39')](handleError(_0x33c77b,null));};exports[_0x9fcf('0x3c')]=function(_0xb1d9c,_0x40e27e){if(_0xb1d9c['body'][_0x9fcf('0x1d')]===_0x9fcf('0x3d')&&!_[_0x9fcf('0x3e')](_0xb1d9c[_0x9fcf('0x3f')]['exten'],'_')){_0xb1d9c['body'][_0x9fcf('0x40')]='_'[_0x9fcf('0x41')](_0xb1d9c[_0x9fcf('0x3f')][_0x9fcf('0x40')]);}return db['VoiceExtension']['create'](_0xb1d9c[_0x9fcf('0x3f')],{'raw':![]})[_0x9fcf('0x1f')](function(_0x345a5d){var _0xde3b20=_0xb1d9c['user'][_0x9fcf('0x14')]({'plain':!![]});if(!_0xde3b20)throw new Error(_0x9fcf('0x42'));if(_0xde3b20[_0x9fcf('0x43')]===_0x9fcf('0x44')){var _0x4ad946=_0x345a5d[_0x9fcf('0x14')]({'plain':!![]});var _0x30e697=[{'name':_0x9fcf('0x45'),'value':_0x9fcf('0x15')},{'name':_0x9fcf('0x16'),'value':_0x9fcf('0x3d')},{'name':_0x9fcf('0x46'),'value':'internal'}];var _0x5b5f15=_[_0x9fcf('0x1c')](_0x30e697,[_0x9fcf('0x1a'),_0x4ad946[_0x9fcf('0x1d')]])[_0x9fcf('0x21')];return db[_0x9fcf('0x47')][_0x9fcf('0x1c')]({'where':{'name':_0x5b5f15,'userProfileId':_0xde3b20[_0x9fcf('0x48')]},'raw':!![]})[_0x9fcf('0x1f')](function(_0xacffb3){if(_0xacffb3&&_0xacffb3['autoAssociation']===0x0){return db[_0x9fcf('0x1e')]['create']({'name':_0x4ad946[_0x9fcf('0x21')],'resourceId':_0x4ad946['id'],'type':_0xacffb3[_0x9fcf('0x21')],'sectionId':_0xacffb3['id']},{})[_0x9fcf('0x1f')](function(){return _0x345a5d;});}else{return _0x345a5d;}})['catch'](function(_0x3df3ab){logger[_0x9fcf('0x49')]('Unable\x20to\x20associate\x20the\x20resource\x20to\x20the\x20User\x20Profile:\x20%s',_0x3df3ab);throw _0x3df3ab;});}return _0x345a5d;})[_0x9fcf('0x1f')](respondWithResult(_0x40e27e,0xc9))[_0x9fcf('0x39')](handleError(_0x40e27e,null));};exports[_0x9fcf('0x4a')]=function(_0x5e4172,_0x4c369c){if(_0x5e4172[_0x9fcf('0x3f')]['id']){delete _0x5e4172[_0x9fcf('0x3f')]['id'];}return db[_0x9fcf('0x22')][_0x9fcf('0x1c')]({'where':{'id':_0x5e4172['params']['id']}})[_0x9fcf('0x1f')](handleEntityNotFound(_0x4c369c,null))[_0x9fcf('0x1f')](function(_0x12d592){if(_0x12d592){return db[_0x9fcf('0x22')]['update'](_[_0x9fcf('0x31')](_0x5e4172[_0x9fcf('0x3f')],['exten',_0x9fcf('0x4b'),_0x9fcf('0x4c'),'recordingFormat','cutdigits',_0x9fcf('0x4d'),'alias']),{'where':{'context':_0x12d592[_0x9fcf('0x4b')],'exten':_0x12d592['exten']},'individualHooks':!![]})['then'](function(_0x407826){return _0x407826;});}return null;})[_0x9fcf('0x1f')](respondWithStatusCode(_0x4c369c,null))[_0x9fcf('0x39')](handleError(_0x4c369c,null));};exports[_0x9fcf('0x4e')]=function(_0xf7ba67,_0x5cb255){var _0x507db3=_0xf7ba67['params']['id'];var _0x164491=_0xf7ba67[_0x9fcf('0x3f')];var _0x5e92b8=0xc8;var _0x2365c9=null;return db[_0x9fcf('0x4f')][_0x9fcf('0x50')]({'isolationLevel':db[_0x9fcf('0x4f')]['Transaction'][_0x9fcf('0x51')]['READ_COMMITTED']},function(_0x2daf48){return db[_0x9fcf('0x22')]['findOne']({'where':{'id':_0x507db3},'transaction':_0x2daf48})['then'](function(_0x51d8fe){if(_0x51d8fe){return db[_0x9fcf('0x22')]['destroy']({'where':{'VoiceExtensionId':_0x507db3},'transaction':_0x2daf48})[_0x9fcf('0x1f')](function(){var _0x310083=_['map'](_0x164491,function(_0x49c366){_0x49c366[_0x9fcf('0x52')]=_0x507db3;_0x49c366[_0x9fcf('0x53')]=!![];return _0x49c366;});return _0x51d8fe[_0x9fcf('0x54')](_0x310083);})[_0x9fcf('0x1f')](function(_0x9cfaef){return db['VoiceExtension'][_0x9fcf('0x55')](_0x9cfaef,{'transaction':_0x2daf48});});}else{_0x5e92b8=0x194;_0x2365c9=[];}});})['then'](function(){if(_0x5e92b8!==0x194){return db[_0x9fcf('0x22')][_0x9fcf('0x56')]({'where':{'isApp':!![],'VoiceExtensionId':_0x507db3},'order':'cast(priority\x20as\x20unsigned)'})['then'](function(_0x27fc03){_0x2365c9=_0x27fc03;});}})[_0x9fcf('0x39')](function(_0x498db8){_0x5e92b8=0x1f4;logger[_0x9fcf('0x49')](_0x498db8[_0x9fcf('0x20')]);if(_0x498db8[_0x9fcf('0x21')]){delete _0x498db8[_0x9fcf('0x21')];}_0x2365c9=_0x498db8;})[_0x9fcf('0x57')](function(){if(_0x2365c9===null){_0x5cb255[_0x9fcf('0xa')](_0x5e92b8);}else{if(_0x5e92b8===0x1f4){_0x5cb255[_0x9fcf('0xc')](_0x5e92b8)[_0x9fcf('0x58')](_0x2365c9);}else{_0x5cb255[_0x9fcf('0xc')](_0x5e92b8)['json'](_0x2365c9);}}});};