32ad004ef0aae6ea1eaeca79d49e87967f925951
[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 _0xbc9b=['destroy','get','InboundRoutes','OutboundRoutes','outbound','InternalRoutes','internal','value','includes','type','find','name','then','stack','send','index','map','VoiceExtension','rawAttributes','model','query','filters','intersection','attributes','fields','length','nolimit','order','sort','where','pick','filter','VIRTUAL','merge','options','includeAll','rows','catch','show','keys','include','params','create','body','concat','exten','role','inbound','UserProfileSection','autoAssociation','UserProfileResource','error','update','context','description','recordingFormat','cutdigits','mandatoryDisposition','alias','addApplications','transaction','sequelize','Transaction','ISOLATION_LEVELS','findOne','isApp','bulkCreate','findAndCountAll','cast(priority\x20as\x20unsigned)','finally','ioredis','../../components/parsers/qs','../../config/logger','api','../../config/environment','../../mysqldb','redis','defaults','localhost','socket.io-emitter','./voiceExtension.socket','sendStatus','status','end','json','offset','limit','undefined','count','set','Content-Range'];(function(_0x173479,_0x237f88){var _0xdc2ed6=function(_0x4e93af){while(--_0x4e93af){_0x173479['push'](_0x173479['shift']());}};_0xdc2ed6(++_0x237f88);}(_0xbc9b,0x1b2));var _0xbbc9=function(_0xaf1fb2,_0x46c137){_0xaf1fb2=_0xaf1fb2-0x0;var _0x4024cc=_0xbc9b[_0xaf1fb2];return _0x4024cc;};'use strict';var _=require('lodash');var Redis=require(_0xbbc9('0x0'));var qs=require(_0xbbc9('0x1'));var logger=require(_0xbbc9('0x2'))(_0xbbc9('0x3'));var config=require(_0xbbc9('0x4'));var db=require(_0xbbc9('0x5'))['db'];config[_0xbbc9('0x6')]=_[_0xbbc9('0x7')](config[_0xbbc9('0x6')],{'host':_0xbbc9('0x8'),'port':0x18eb});var socket=require(_0xbbc9('0x9'))(new Redis(config[_0xbbc9('0x6')]));require(_0xbbc9('0xa'))['register'](socket);function respondWithStatusCode(_0x3f1da4,_0x4a4b1f){_0x4a4b1f=_0x4a4b1f||0xcc;return function(_0x5d5f6c){if(_0x5d5f6c){return _0x3f1da4[_0xbbc9('0xb')](_0x4a4b1f);}return _0x3f1da4[_0xbbc9('0xc')](_0x4a4b1f)[_0xbbc9('0xd')]();};}function respondWithResult(_0x5268b9,_0x34806d){_0x34806d=_0x34806d||0xc8;return function(_0xfb6f50){if(_0xfb6f50){return _0x5268b9[_0xbbc9('0xc')](_0x34806d)[_0xbbc9('0xe')](_0xfb6f50);}};}function respondWithFilteredResult(_0x36f58f,_0x14c621){return function(_0xd5cbb){if(_0xd5cbb){var _0x1edc95=typeof _0x14c621[_0xbbc9('0xf')]==='undefined'&&typeof _0x14c621[_0xbbc9('0x10')]===_0xbbc9('0x11');var _0x4cc09c=_0xd5cbb[_0xbbc9('0x12')];var _0x6c6266=_0x1edc95?0x0:_0x14c621['offset'];var _0x50cd70=_0x1edc95?_0xd5cbb[_0xbbc9('0x12')]:_0x14c621[_0xbbc9('0xf')]+_0x14c621[_0xbbc9('0x10')];var _0xe1c8fe;if(_0x50cd70>=_0x4cc09c){_0x50cd70=_0x4cc09c;_0xe1c8fe=0xc8;}else{_0xe1c8fe=0xce;}_0x36f58f[_0xbbc9('0xc')](_0xe1c8fe);return _0x36f58f[_0xbbc9('0x13')](_0xbbc9('0x14'),_0x6c6266+'-'+_0x50cd70+'/'+_0x4cc09c)['json'](_0xd5cbb);}return null;};}function removeEntity(_0x5bf84e){return function(_0x2bc938){if(_0x2bc938){return _0x2bc938[_0xbbc9('0x15')]()['then'](function(){var _0x590535=_0x2bc938[_0xbbc9('0x16')]({'plain':!![]});var _0x12ee35=[{'name':_0xbbc9('0x17'),'value':'inbound','ignore':![]},{'name':_0xbbc9('0x18'),'value':_0xbbc9('0x19'),'ignore':![]},{'name':_0xbbc9('0x1a'),'value':_0xbbc9('0x1b'),'ignore':![]}];var _0x5c1123=_(_0x12ee35)['filter']('ignore')['map']('value')[_0xbbc9('0x1c')]();if(_[_0xbbc9('0x1d')](_0x5c1123,_0x590535[_0xbbc9('0x1e')])){return;}var _0xbc0870=_[_0xbbc9('0x1f')](_0x12ee35,[_0xbbc9('0x1c'),_0x590535[_0xbbc9('0x1e')]])[_0xbbc9('0x20')];return db['UserProfileResource'][_0xbbc9('0x15')]({'where':{'type':_0xbc0870,'resourceId':_0x590535['id']}})[_0xbbc9('0x21')](function(){return _0x2bc938;});})['then'](function(){_0x5bf84e[_0xbbc9('0xc')](0xcc)[_0xbbc9('0xd')]();});}};}function handleEntityNotFound(_0x60a8c5){return function(_0x3e7636){if(!_0x3e7636){_0x60a8c5[_0xbbc9('0xb')](0x194);}return _0x3e7636;};}function handleError(_0x13854e,_0x539c29){_0x539c29=_0x539c29||0x1f4;return function(_0xaeaab5){logger['error'](_0xaeaab5[_0xbbc9('0x22')]);if(_0xaeaab5[_0xbbc9('0x20')]){delete _0xaeaab5[_0xbbc9('0x20')];}_0x13854e[_0xbbc9('0xc')](_0x539c29)[_0xbbc9('0x23')](_0xaeaab5);};}exports[_0xbbc9('0x24')]=function(_0x59c219,_0x2d23c8){var _0x44abed={},_0x584457={},_0x3e039d={'count':0x0,'rows':[]};var _0x34f0ca=_[_0xbbc9('0x25')](db[_0xbbc9('0x26')][_0xbbc9('0x27')],function(_0x2529af){return{'name':_0x2529af['fieldName'],'type':_0x2529af[_0xbbc9('0x1e')]['key']};});_0x584457[_0xbbc9('0x28')]=_[_0xbbc9('0x25')](_0x34f0ca,_0xbbc9('0x20'));_0x584457[_0xbbc9('0x29')]=_['keys'](_0x59c219[_0xbbc9('0x29')]);_0x584457[_0xbbc9('0x2a')]=_[_0xbbc9('0x2b')](_0x584457[_0xbbc9('0x28')],_0x584457['query']);_0x44abed[_0xbbc9('0x2c')]=_[_0xbbc9('0x2b')](_0x584457[_0xbbc9('0x28')],qs[_0xbbc9('0x2d')](_0x59c219[_0xbbc9('0x29')][_0xbbc9('0x2d')]));_0x44abed[_0xbbc9('0x2c')]=_0x44abed[_0xbbc9('0x2c')][_0xbbc9('0x2e')]?_0x44abed['attributes']:_0x584457['model'];if(!_0x59c219['query']['hasOwnProperty'](_0xbbc9('0x2f'))){_0x44abed[_0xbbc9('0x10')]=qs[_0xbbc9('0x10')](_0x59c219[_0xbbc9('0x29')][_0xbbc9('0x10')]);_0x44abed[_0xbbc9('0xf')]=qs[_0xbbc9('0xf')](_0x59c219[_0xbbc9('0x29')][_0xbbc9('0xf')]);}_0x44abed[_0xbbc9('0x30')]=qs['sort'](_0x59c219[_0xbbc9('0x29')][_0xbbc9('0x31')]);_0x44abed[_0xbbc9('0x32')]=qs['filters'](_[_0xbbc9('0x33')](_0x59c219['query'],_0x584457[_0xbbc9('0x2a')]),_0x34f0ca);if(_0x59c219[_0xbbc9('0x29')][_0xbbc9('0x34')]){_0x44abed[_0xbbc9('0x32')]=_['merge'](_0x44abed[_0xbbc9('0x32')],{'$or':_[_0xbbc9('0x25')](_0x34f0ca,function(_0x37efc1){if(_0x37efc1['type']!==_0xbbc9('0x35')){var _0x4505d8={};_0x4505d8[_0x37efc1['name']]={'$like':'%'+_0x59c219[_0xbbc9('0x29')]['filter']+'%'};return _0x4505d8;}})});}_0x44abed=_[_0xbbc9('0x36')]({},_0x44abed,_0x59c219[_0xbbc9('0x37')]);var _0x31303e={'where':_0x44abed['where']};return db[_0xbbc9('0x26')][_0xbbc9('0x12')](_0x31303e)[_0xbbc9('0x21')](function(_0x49cd08){_0x3e039d[_0xbbc9('0x12')]=_0x49cd08;if(_0x59c219[_0xbbc9('0x29')][_0xbbc9('0x38')]){_0x44abed['include']=[{'all':!![]}];}return db[_0xbbc9('0x26')]['findAll'](_0x44abed);})[_0xbbc9('0x21')](function(_0x3e9d18){_0x3e039d[_0xbbc9('0x39')]=_0x3e9d18;return _0x3e039d;})[_0xbbc9('0x21')](respondWithFilteredResult(_0x2d23c8,_0x44abed))[_0xbbc9('0x3a')](handleError(_0x2d23c8,null));};exports[_0xbbc9('0x3b')]=function(_0xa4e068,_0x2904f6){var _0x1cd575={'raw':![],'where':{'id':_0xa4e068['params']['id']}},_0x3871a1={};_0x3871a1[_0xbbc9('0x28')]=_[_0xbbc9('0x3c')](db[_0xbbc9('0x26')]['rawAttributes']);_0x3871a1[_0xbbc9('0x29')]=_[_0xbbc9('0x3c')](_0xa4e068[_0xbbc9('0x29')]);_0x3871a1[_0xbbc9('0x2a')]=_['intersection'](_0x3871a1[_0xbbc9('0x28')],_0x3871a1[_0xbbc9('0x29')]);_0x1cd575['attributes']=_[_0xbbc9('0x2b')](_0x3871a1['model'],qs[_0xbbc9('0x2d')](_0xa4e068[_0xbbc9('0x29')][_0xbbc9('0x2d')]));_0x1cd575[_0xbbc9('0x2c')]=_0x1cd575[_0xbbc9('0x2c')]['length']?_0x1cd575[_0xbbc9('0x2c')]:_0x3871a1[_0xbbc9('0x28')];if(_0xa4e068[_0xbbc9('0x29')][_0xbbc9('0x38')]){_0x1cd575[_0xbbc9('0x3d')]=[{'all':!![]}];}_0x1cd575=_[_0xbbc9('0x36')]({},_0x1cd575,_0xa4e068[_0xbbc9('0x37')]);return db[_0xbbc9('0x26')][_0xbbc9('0x1f')](_0x1cd575)[_0xbbc9('0x21')](handleEntityNotFound(_0x2904f6,null))[_0xbbc9('0x21')](respondWithResult(_0x2904f6,null))['catch'](handleError(_0x2904f6,null));};exports[_0xbbc9('0x15')]=function(_0x4e1f33,_0x417b6c){return db[_0xbbc9('0x26')][_0xbbc9('0x1f')]({'where':{'id':_0x4e1f33[_0xbbc9('0x3e')]['id']}})['then'](handleEntityNotFound(_0x417b6c,null))['then'](removeEntity(_0x417b6c,null))['catch'](handleError(_0x417b6c,null));};exports[_0xbbc9('0x3f')]=function(_0x4f9e7a,_0x2e1d57){if(_0x4f9e7a[_0xbbc9('0x40')][_0xbbc9('0x1e')]===_0xbbc9('0x19')&&!_['startsWith'](_0x4f9e7a[_0xbbc9('0x40')]['exten'],'_')){_0x4f9e7a[_0xbbc9('0x40')]['exten']='_'[_0xbbc9('0x41')](_0x4f9e7a[_0xbbc9('0x40')][_0xbbc9('0x42')]);}return db[_0xbbc9('0x26')]['create'](_0x4f9e7a[_0xbbc9('0x40')],{'raw':![]})[_0xbbc9('0x21')](function(_0x38d96f){var _0x18330a=_0x4f9e7a['user'][_0xbbc9('0x16')]({'plain':!![]});if(!_0x18330a)throw new Error('Unable\x20to\x20retrieve\x20the\x20current\x20user');if(_0x18330a[_0xbbc9('0x43')]==='user'){var _0x2a492b=_0x38d96f[_0xbbc9('0x16')]({'plain':!![]});var _0x187f0a=[{'name':_0xbbc9('0x17'),'value':_0xbbc9('0x44')},{'name':_0xbbc9('0x18'),'value':_0xbbc9('0x19')},{'name':'InternalRoutes','value':'internal'}];var _0x286f1e=_[_0xbbc9('0x1f')](_0x187f0a,[_0xbbc9('0x1c'),_0x2a492b[_0xbbc9('0x1e')]])[_0xbbc9('0x20')];return db[_0xbbc9('0x45')][_0xbbc9('0x1f')]({'where':{'name':_0x286f1e,'userProfileId':_0x18330a['userProfileId']},'raw':!![]})[_0xbbc9('0x21')](function(_0x4e1286){if(_0x4e1286&&_0x4e1286[_0xbbc9('0x46')]===0x0){return db[_0xbbc9('0x47')]['create']({'name':_0x2a492b[_0xbbc9('0x20')],'resourceId':_0x2a492b['id'],'type':_0x4e1286[_0xbbc9('0x20')],'sectionId':_0x4e1286['id']},{})[_0xbbc9('0x21')](function(){return _0x38d96f;});}else{return _0x38d96f;}})[_0xbbc9('0x3a')](function(_0x41f8fa){logger[_0xbbc9('0x48')]('Unable\x20to\x20associate\x20the\x20resource\x20to\x20the\x20User\x20Profile:\x20%s',_0x41f8fa);throw _0x41f8fa;});}return _0x38d96f;})[_0xbbc9('0x21')](respondWithResult(_0x2e1d57,0xc9))['catch'](handleError(_0x2e1d57,null));};exports['update']=function(_0x4d4156,_0x5d71e8){if(_0x4d4156[_0xbbc9('0x40')]['id']){delete _0x4d4156['body']['id'];}return db[_0xbbc9('0x26')][_0xbbc9('0x1f')]({'where':{'id':_0x4d4156[_0xbbc9('0x3e')]['id']}})[_0xbbc9('0x21')](handleEntityNotFound(_0x5d71e8,null))['then'](function(_0x248220){if(_0x248220){return db['VoiceExtension'][_0xbbc9('0x49')](_[_0xbbc9('0x33')](_0x4d4156[_0xbbc9('0x40')],['exten',_0xbbc9('0x4a'),_0xbbc9('0x4b'),_0xbbc9('0x4c'),_0xbbc9('0x4d'),_0xbbc9('0x4e'),_0xbbc9('0x4f')]),{'where':{'context':_0x248220[_0xbbc9('0x4a')],'exten':_0x248220['exten']},'individualHooks':!![]})[_0xbbc9('0x21')](function(_0x2a8c31){return _0x2a8c31;});}return null;})[_0xbbc9('0x21')](respondWithStatusCode(_0x5d71e8,null))[_0xbbc9('0x3a')](handleError(_0x5d71e8,null));};exports[_0xbbc9('0x50')]=function(_0x508d66,_0x4e6790){var _0xcc1dcf=_0x508d66[_0xbbc9('0x3e')]['id'];var _0x5255c3=_0x508d66['body'];var _0x26e4fa=0xc8;var _0x77cbcd=null;return db['sequelize'][_0xbbc9('0x51')]({'isolationLevel':db[_0xbbc9('0x52')][_0xbbc9('0x53')][_0xbbc9('0x54')]['READ_COMMITTED']},function(_0x12f8ba){return db['VoiceExtension'][_0xbbc9('0x55')]({'where':{'id':_0xcc1dcf},'transaction':_0x12f8ba})['then'](function(_0xb68268){if(_0xb68268){return db['VoiceExtension']['destroy']({'where':{'VoiceExtensionId':_0xcc1dcf},'transaction':_0x12f8ba})[_0xbbc9('0x21')](function(){var _0x4e96cd=_[_0xbbc9('0x25')](_0x5255c3,function(_0x31cccb){_0x31cccb['VoiceExtensionId']=_0xcc1dcf;_0x31cccb[_0xbbc9('0x56')]=!![];return _0x31cccb;});return _0xb68268['createRouting'](_0x4e96cd);})[_0xbbc9('0x21')](function(_0x1f929a){return db[_0xbbc9('0x26')][_0xbbc9('0x57')](_0x1f929a,{'transaction':_0x12f8ba});});}else{_0x26e4fa=0x194;_0x77cbcd=[];}});})['then'](function(){if(_0x26e4fa!==0x194){return db[_0xbbc9('0x26')][_0xbbc9('0x58')]({'where':{'isApp':!![],'VoiceExtensionId':_0xcc1dcf},'order':_0xbbc9('0x59')})[_0xbbc9('0x21')](function(_0x24c9c1){_0x77cbcd=_0x24c9c1;});}})['catch'](function(_0x4187f8){_0x26e4fa=0x1f4;logger[_0xbbc9('0x48')](_0x4187f8[_0xbbc9('0x22')]);if(_0x4187f8[_0xbbc9('0x20')]){delete _0x4187f8[_0xbbc9('0x20')];}_0x77cbcd=_0x4187f8;})[_0xbbc9('0x5a')](function(){if(_0x77cbcd===null){_0x4e6790[_0xbbc9('0xb')](_0x26e4fa);}else{if(_0x26e4fa===0x1f4){_0x4e6790['status'](_0x26e4fa)[_0xbbc9('0x23')](_0x77cbcd);}else{_0x4e6790[_0xbbc9('0xc')](_0x26e4fa)[_0xbbc9('0xe')](_0x77cbcd);}}});};