Built motion from commit 00bf23f6.|2.6.16
[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 _0x4201=['set','Content-Range','destroy','get','InboundRoutes','inbound','outbound','InternalRoutes','internal','filter','ignore','map','value','includes','find','type','UserProfileResource','then','stack','name','send','VoiceExtension','fieldName','key','model','query','keys','intersection','attributes','fields','hasOwnProperty','nolimit','order','sort','pick','filters','where','VIRTUAL','merge','options','includeAll','include','findAll','rows','show','params','length','catch','create','body','startsWith','exten','concat','Unable\x20to\x20retrieve\x20the\x20current\x20user','role','user','UserProfileSection','userProfileId','autoAssociation','error','Unable\x20to\x20associate\x20the\x20resource\x20to\x20the\x20User\x20Profile:\x20%s','context','description','recordingFormat','alias','addApplications','sequelize','transaction','Transaction','READ_COMMITTED','VoiceExtensionId','createRouting','findAndCountAll','cast(priority\x20as\x20unsigned)','finally','lodash','../../config/logger','api','../../config/environment','redis','localhost','socket.io-emitter','./voiceExtension.socket','register','end','status','json','offset','undefined','limit','count'];(function(_0x48999d,_0x1e02cb){var _0x1ad0d1=function(_0x3f974e){while(--_0x3f974e){_0x48999d['push'](_0x48999d['shift']());}};_0x1ad0d1(++_0x1e02cb);}(_0x4201,0xa6));var _0x1420=function(_0x2bcd1f,_0x4f1972){_0x2bcd1f=_0x2bcd1f-0x0;var _0x2381ca=_0x4201[_0x2bcd1f];return _0x2381ca;};'use strict';var _=require(_0x1420('0x0'));var Redis=require('ioredis');var qs=require('../../components/parsers/qs');var logger=require(_0x1420('0x1'))(_0x1420('0x2'));var config=require(_0x1420('0x3'));var db=require('../../mysqldb')['db'];config[_0x1420('0x4')]=_['defaults'](config[_0x1420('0x4')],{'host':_0x1420('0x5'),'port':0x18eb});var socket=require(_0x1420('0x6'))(new Redis(config['redis']));require(_0x1420('0x7'))[_0x1420('0x8')](socket);function respondWithStatusCode(_0x420a00,_0x425782){_0x425782=_0x425782||0xcc;return function(_0x55facb){if(_0x55facb){return _0x420a00['sendStatus'](_0x425782);}return _0x420a00['status'](_0x425782)[_0x1420('0x9')]();};}function respondWithResult(_0x336936,_0x434cde){_0x434cde=_0x434cde||0xc8;return function(_0x5ebf2a){if(_0x5ebf2a){return _0x336936[_0x1420('0xa')](_0x434cde)[_0x1420('0xb')](_0x5ebf2a);}};}function respondWithFilteredResult(_0x4df520,_0x112315){return function(_0x421939){if(_0x421939){var _0x3a9c84=typeof _0x112315[_0x1420('0xc')]===_0x1420('0xd')&&typeof _0x112315[_0x1420('0xe')]===_0x1420('0xd');var _0x4859e7=_0x421939[_0x1420('0xf')];var _0x5a159c=_0x3a9c84?0x0:_0x112315[_0x1420('0xc')];var _0x48606d=_0x3a9c84?_0x421939[_0x1420('0xf')]:_0x112315[_0x1420('0xc')]+_0x112315[_0x1420('0xe')];var _0x43f0aa;if(_0x48606d>=_0x4859e7){_0x48606d=_0x4859e7;_0x43f0aa=0xc8;}else{_0x43f0aa=0xce;}_0x4df520[_0x1420('0xa')](_0x43f0aa);return _0x4df520[_0x1420('0x10')](_0x1420('0x11'),_0x5a159c+'-'+_0x48606d+'/'+_0x4859e7)[_0x1420('0xb')](_0x421939);}return null;};}function removeEntity(_0x92cb47){return function(_0x4e7cdd){if(_0x4e7cdd){return _0x4e7cdd[_0x1420('0x12')]()['then'](function(){var _0x2a1165=_0x4e7cdd[_0x1420('0x13')]({'plain':!![]});var _0x213bf8=[{'name':_0x1420('0x14'),'value':_0x1420('0x15'),'ignore':![]},{'name':'OutboundRoutes','value':_0x1420('0x16'),'ignore':![]},{'name':_0x1420('0x17'),'value':_0x1420('0x18'),'ignore':![]}];var _0x559cf0=_(_0x213bf8)[_0x1420('0x19')](_0x1420('0x1a'))[_0x1420('0x1b')]('value')[_0x1420('0x1c')]();if(_[_0x1420('0x1d')](_0x559cf0,_0x2a1165['type'])){return;}var _0xb504e3=_[_0x1420('0x1e')](_0x213bf8,[_0x1420('0x1c'),_0x2a1165[_0x1420('0x1f')]])['name'];return db[_0x1420('0x20')][_0x1420('0x12')]({'where':{'type':_0xb504e3,'resourceId':_0x2a1165['id']}})[_0x1420('0x21')](function(){return _0x4e7cdd;});})[_0x1420('0x21')](function(){_0x92cb47[_0x1420('0xa')](0xcc)[_0x1420('0x9')]();});}};}function handleEntityNotFound(_0x4223ed){return function(_0x49b2aa){if(!_0x49b2aa){_0x4223ed['sendStatus'](0x194);}return _0x49b2aa;};}function handleError(_0x54702f,_0x1da04f){_0x1da04f=_0x1da04f||0x1f4;return function(_0x2c2560){logger['error'](_0x2c2560[_0x1420('0x22')]);if(_0x2c2560['name']){delete _0x2c2560[_0x1420('0x23')];}_0x54702f['status'](_0x1da04f)[_0x1420('0x24')](_0x2c2560);};}exports['index']=function(_0x3fc020,_0x2aae4f){var _0x1cff93={},_0xff6157={},_0x267668={'count':0x0,'rows':[]};var _0x40fd15=_[_0x1420('0x1b')](db[_0x1420('0x25')]['rawAttributes'],function(_0x4e147f){return{'name':_0x4e147f[_0x1420('0x26')],'type':_0x4e147f[_0x1420('0x1f')][_0x1420('0x27')]};});_0xff6157[_0x1420('0x28')]=_[_0x1420('0x1b')](_0x40fd15,_0x1420('0x23'));_0xff6157[_0x1420('0x29')]=_[_0x1420('0x2a')](_0x3fc020[_0x1420('0x29')]);_0xff6157['filters']=_[_0x1420('0x2b')](_0xff6157['model'],_0xff6157['query']);_0x1cff93[_0x1420('0x2c')]=_[_0x1420('0x2b')](_0xff6157['model'],qs[_0x1420('0x2d')](_0x3fc020[_0x1420('0x29')]['fields']));_0x1cff93[_0x1420('0x2c')]=_0x1cff93['attributes']['length']?_0x1cff93[_0x1420('0x2c')]:_0xff6157[_0x1420('0x28')];if(!_0x3fc020[_0x1420('0x29')][_0x1420('0x2e')](_0x1420('0x2f'))){_0x1cff93['limit']=qs['limit'](_0x3fc020[_0x1420('0x29')]['limit']);_0x1cff93['offset']=qs[_0x1420('0xc')](_0x3fc020[_0x1420('0x29')][_0x1420('0xc')]);}_0x1cff93[_0x1420('0x30')]=qs[_0x1420('0x31')](_0x3fc020[_0x1420('0x29')][_0x1420('0x31')]);_0x1cff93['where']=qs['filters'](_[_0x1420('0x32')](_0x3fc020[_0x1420('0x29')],_0xff6157[_0x1420('0x33')]),_0x40fd15);if(_0x3fc020['query'][_0x1420('0x19')]){_0x1cff93[_0x1420('0x34')]=_['merge'](_0x1cff93[_0x1420('0x34')],{'$or':_[_0x1420('0x1b')](_0x40fd15,function(_0x5cb9a1){if(_0x5cb9a1[_0x1420('0x1f')]!==_0x1420('0x35')){var _0x3b06eb={};_0x3b06eb[_0x5cb9a1['name']]={'$like':'%'+_0x3fc020[_0x1420('0x29')]['filter']+'%'};return _0x3b06eb;}})});}_0x1cff93=_[_0x1420('0x36')]({},_0x1cff93,_0x3fc020[_0x1420('0x37')]);var _0x5be90a={'where':_0x1cff93[_0x1420('0x34')]};return db[_0x1420('0x25')][_0x1420('0xf')](_0x5be90a)[_0x1420('0x21')](function(_0x563160){_0x267668[_0x1420('0xf')]=_0x563160;if(_0x3fc020[_0x1420('0x29')][_0x1420('0x38')]){_0x1cff93[_0x1420('0x39')]=[{'all':!![]}];}return db[_0x1420('0x25')][_0x1420('0x3a')](_0x1cff93);})[_0x1420('0x21')](function(_0x20abba){_0x267668[_0x1420('0x3b')]=_0x20abba;return _0x267668;})[_0x1420('0x21')](respondWithFilteredResult(_0x2aae4f,_0x1cff93))['catch'](handleError(_0x2aae4f,null));};exports[_0x1420('0x3c')]=function(_0x2eda3d,_0x447e1d){var _0x67c21b={'raw':![],'where':{'id':_0x2eda3d[_0x1420('0x3d')]['id']}},_0x44480d={};_0x44480d['model']=_['keys'](db['VoiceExtension']['rawAttributes']);_0x44480d['query']=_[_0x1420('0x2a')](_0x2eda3d[_0x1420('0x29')]);_0x44480d['filters']=_[_0x1420('0x2b')](_0x44480d['model'],_0x44480d[_0x1420('0x29')]);_0x67c21b[_0x1420('0x2c')]=_['intersection'](_0x44480d['model'],qs[_0x1420('0x2d')](_0x2eda3d[_0x1420('0x29')][_0x1420('0x2d')]));_0x67c21b[_0x1420('0x2c')]=_0x67c21b['attributes'][_0x1420('0x3e')]?_0x67c21b[_0x1420('0x2c')]:_0x44480d[_0x1420('0x28')];if(_0x2eda3d[_0x1420('0x29')]['includeAll']){_0x67c21b['include']=[{'all':!![]}];}_0x67c21b=_[_0x1420('0x36')]({},_0x67c21b,_0x2eda3d[_0x1420('0x37')]);return db[_0x1420('0x25')][_0x1420('0x1e')](_0x67c21b)[_0x1420('0x21')](handleEntityNotFound(_0x447e1d,null))[_0x1420('0x21')](respondWithResult(_0x447e1d,null))['catch'](handleError(_0x447e1d,null));};exports[_0x1420('0x12')]=function(_0xf4d537,_0x5a47a1){return db[_0x1420('0x25')][_0x1420('0x1e')]({'where':{'id':_0xf4d537[_0x1420('0x3d')]['id']}})[_0x1420('0x21')](handleEntityNotFound(_0x5a47a1,null))[_0x1420('0x21')](removeEntity(_0x5a47a1,null))[_0x1420('0x3f')](handleError(_0x5a47a1,null));};exports[_0x1420('0x40')]=function(_0x4571fc,_0x255e48){if(_0x4571fc[_0x1420('0x41')][_0x1420('0x1f')]===_0x1420('0x16')&&!_[_0x1420('0x42')](_0x4571fc['body'][_0x1420('0x43')],'_')){_0x4571fc[_0x1420('0x41')][_0x1420('0x43')]='_'[_0x1420('0x44')](_0x4571fc[_0x1420('0x41')]['exten']);}return db[_0x1420('0x25')]['create'](_0x4571fc['body'],{'raw':![]})['then'](function(_0x376f8c){var _0x23e48c=_0x4571fc['user'][_0x1420('0x13')]({'plain':!![]});if(!_0x23e48c)throw new Error(_0x1420('0x45'));if(_0x23e48c[_0x1420('0x46')]===_0x1420('0x47')){var _0x495d86=_0x376f8c[_0x1420('0x13')]({'plain':!![]});var _0x7ca1d2=[{'name':'InboundRoutes','value':_0x1420('0x15')},{'name':'OutboundRoutes','value':_0x1420('0x16')},{'name':'InternalRoutes','value':_0x1420('0x18')}];var _0x2cad72=_[_0x1420('0x1e')](_0x7ca1d2,[_0x1420('0x1c'),_0x495d86['type']])['name'];return db[_0x1420('0x48')][_0x1420('0x1e')]({'where':{'name':_0x2cad72,'userProfileId':_0x23e48c[_0x1420('0x49')]},'raw':!![]})[_0x1420('0x21')](function(_0x45150b){if(_0x45150b&&_0x45150b[_0x1420('0x4a')]===0x0){return db[_0x1420('0x20')][_0x1420('0x40')]({'name':_0x495d86['name'],'resourceId':_0x495d86['id'],'type':_0x45150b['name'],'sectionId':_0x45150b['id']},{})[_0x1420('0x21')](function(){return _0x376f8c;});}else{return _0x376f8c;}})['catch'](function(_0x3f590b){logger[_0x1420('0x4b')](_0x1420('0x4c'),_0x3f590b);throw _0x3f590b;});}return _0x376f8c;})[_0x1420('0x21')](respondWithResult(_0x255e48,0xc9))[_0x1420('0x3f')](handleError(_0x255e48,null));};exports['update']=function(_0x52af5f,_0x56a37e){if(_0x52af5f[_0x1420('0x41')]['id']){delete _0x52af5f[_0x1420('0x41')]['id'];}return db[_0x1420('0x25')][_0x1420('0x1e')]({'where':{'id':_0x52af5f[_0x1420('0x3d')]['id']}})['then'](handleEntityNotFound(_0x56a37e,null))[_0x1420('0x21')](function(_0x50ddb7){if(_0x50ddb7){return db['VoiceExtension']['update'](_[_0x1420('0x32')](_0x52af5f[_0x1420('0x41')],['exten',_0x1420('0x4d'),_0x1420('0x4e'),_0x1420('0x4f'),'cutdigits','mandatoryDisposition',_0x1420('0x50')]),{'where':{'context':_0x50ddb7[_0x1420('0x4d')],'exten':_0x50ddb7['exten']},'individualHooks':!![]})[_0x1420('0x21')](function(_0x55ecd4){return _0x55ecd4;});}return null;})['then'](respondWithStatusCode(_0x56a37e,null))[_0x1420('0x3f')](handleError(_0x56a37e,null));};exports[_0x1420('0x51')]=function(_0x2f1760,_0x5bf072){var _0x5c558e=_0x2f1760[_0x1420('0x3d')]['id'];var _0x103185=_0x2f1760[_0x1420('0x41')];var _0x22094a=0xc8;var _0x81843d=null;return db[_0x1420('0x52')][_0x1420('0x53')]({'isolationLevel':db['sequelize'][_0x1420('0x54')]['ISOLATION_LEVELS'][_0x1420('0x55')]},function(_0x31ef8f){return db[_0x1420('0x25')]['findOne']({'where':{'id':_0x5c558e},'transaction':_0x31ef8f})[_0x1420('0x21')](function(_0x318dad){if(_0x318dad){return db[_0x1420('0x25')][_0x1420('0x12')]({'where':{'VoiceExtensionId':_0x5c558e},'transaction':_0x31ef8f})[_0x1420('0x21')](function(){var _0x4341b5=_[_0x1420('0x1b')](_0x103185,function(_0x3a74d5){_0x3a74d5[_0x1420('0x56')]=_0x5c558e;_0x3a74d5['isApp']=!![];return _0x3a74d5;});return _0x318dad[_0x1420('0x57')](_0x4341b5);})[_0x1420('0x21')](function(_0x194a46){return db[_0x1420('0x25')]['bulkCreate'](_0x194a46,{'transaction':_0x31ef8f});});}else{_0x22094a=0x194;_0x81843d=[];}});})[_0x1420('0x21')](function(){if(_0x22094a!==0x194){return db[_0x1420('0x25')][_0x1420('0x58')]({'where':{'isApp':!![],'VoiceExtensionId':_0x5c558e},'order':_0x1420('0x59')})[_0x1420('0x21')](function(_0x3c3d6b){_0x81843d=_0x3c3d6b;});}})['catch'](function(_0x4f3a9e){_0x22094a=0x1f4;logger[_0x1420('0x4b')](_0x4f3a9e[_0x1420('0x22')]);if(_0x4f3a9e['name']){delete _0x4f3a9e[_0x1420('0x23')];}_0x81843d=_0x4f3a9e;})[_0x1420('0x5a')](function(){if(_0x81843d===null){_0x5bf072['sendStatus'](_0x22094a);}else{if(_0x22094a===0x1f4){_0x5bf072[_0x1420('0xa')](_0x22094a)[_0x1420('0x24')](_0x81843d);}else{_0x5bf072[_0x1420('0xa')](_0x22094a)[_0x1420('0xb')](_0x81843d);}}});};