39208cc24742cab15626388c89445ecba231e457
[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 _0x17df=['api','../../config/environment','../../mysqldb','redis','defaults','localhost','./voiceExtension.socket','register','status','end','json','offset','undefined','limit','count','Content-Range','then','get','InboundRoutes','inbound','OutboundRoutes','outbound','InternalRoutes','ignore','map','value','type','name','UserProfileResource','destroy','sendStatus','error','stack','send','VoiceExtension','rawAttributes','fieldName','key','model','query','keys','filters','intersection','attributes','fields','length','hasOwnProperty','order','sort','where','filter','merge','VIRTUAL','options','include','findAll','catch','show','params','includeAll','find','body','startsWith','exten','create','role','user','userProfileId','autoAssociation','update','pick','recordingFormat','cutdigits','mandatoryDisposition','alias','context','sequelize','Transaction','findOne','VoiceExtensionId','isApp','bulkCreate','findAndCountAll','cast(priority\x20as\x20unsigned)','finally','lodash','../../components/parsers/qs','../../config/logger'];(function(_0x465fd8,_0x240950){var _0x43c674=function(_0x1630de){while(--_0x1630de){_0x465fd8['push'](_0x465fd8['shift']());}};_0x43c674(++_0x240950);}(_0x17df,0xad));var _0xf17d=function(_0x2e181,_0x30ecb1){_0x2e181=_0x2e181-0x0;var _0x462b14=_0x17df[_0x2e181];return _0x462b14;};'use strict';var _=require(_0xf17d('0x0'));var Redis=require('ioredis');var qs=require(_0xf17d('0x1'));var logger=require(_0xf17d('0x2'))(_0xf17d('0x3'));var config=require(_0xf17d('0x4'));var db=require(_0xf17d('0x5'))['db'];config[_0xf17d('0x6')]=_[_0xf17d('0x7')](config[_0xf17d('0x6')],{'host':_0xf17d('0x8'),'port':0x18eb});var socket=require('socket.io-emitter')(new Redis(config[_0xf17d('0x6')]));require(_0xf17d('0x9'))[_0xf17d('0xa')](socket);function respondWithStatusCode(_0x5b0f5c,_0xb2ae1a){_0xb2ae1a=_0xb2ae1a||0xcc;return function(_0x922b4a){if(_0x922b4a){return _0x5b0f5c['sendStatus'](_0xb2ae1a);}return _0x5b0f5c[_0xf17d('0xb')](_0xb2ae1a)[_0xf17d('0xc')]();};}function respondWithResult(_0x184d51,_0x33944e){_0x33944e=_0x33944e||0xc8;return function(_0x279e59){if(_0x279e59){return _0x184d51[_0xf17d('0xb')](_0x33944e)[_0xf17d('0xd')](_0x279e59);}};}function respondWithFilteredResult(_0x16f9ff,_0x35945d){return function(_0x2fe737){if(_0x2fe737){var _0x28b67d=typeof _0x35945d[_0xf17d('0xe')]===_0xf17d('0xf')&&typeof _0x35945d[_0xf17d('0x10')]==='undefined';var _0x43cf03=_0x2fe737[_0xf17d('0x11')];var _0x1fbbdb=_0x28b67d?0x0:_0x35945d['offset'];var _0x4486bd=_0x28b67d?_0x2fe737[_0xf17d('0x11')]:_0x35945d['offset']+_0x35945d[_0xf17d('0x10')];var _0x6b247b;if(_0x4486bd>=_0x43cf03){_0x4486bd=_0x43cf03;_0x6b247b=0xc8;}else{_0x6b247b=0xce;}_0x16f9ff['status'](_0x6b247b);return _0x16f9ff['set'](_0xf17d('0x12'),_0x1fbbdb+'-'+_0x4486bd+'/'+_0x43cf03)[_0xf17d('0xd')](_0x2fe737);}return null;};}function removeEntity(_0x3d520c){return function(_0x28d0fd){if(_0x28d0fd){return _0x28d0fd['destroy']()[_0xf17d('0x13')](function(){var _0x10bd92=_0x28d0fd[_0xf17d('0x14')]({'plain':!![]});var _0x47fd16=[{'name':_0xf17d('0x15'),'value':_0xf17d('0x16'),'ignore':![]},{'name':_0xf17d('0x17'),'value':_0xf17d('0x18'),'ignore':![]},{'name':_0xf17d('0x19'),'value':'internal','ignore':![]}];var _0x544c23=_(_0x47fd16)['filter'](_0xf17d('0x1a'))[_0xf17d('0x1b')](_0xf17d('0x1c'))[_0xf17d('0x1c')]();if(_['includes'](_0x544c23,_0x10bd92['type'])){return;}var _0x14cefa=_['find'](_0x47fd16,[_0xf17d('0x1c'),_0x10bd92[_0xf17d('0x1d')]])[_0xf17d('0x1e')];return db[_0xf17d('0x1f')][_0xf17d('0x20')]({'where':{'type':_0x14cefa,'resourceId':_0x10bd92['id']}})[_0xf17d('0x13')](function(){return _0x28d0fd;});})['then'](function(){_0x3d520c[_0xf17d('0xb')](0xcc)[_0xf17d('0xc')]();});}};}function handleEntityNotFound(_0x22ed35){return function(_0x579ed9){if(!_0x579ed9){_0x22ed35[_0xf17d('0x21')](0x194);}return _0x579ed9;};}function handleError(_0x296113,_0x5169dd){_0x5169dd=_0x5169dd||0x1f4;return function(_0x56d7df){logger[_0xf17d('0x22')](_0x56d7df[_0xf17d('0x23')]);if(_0x56d7df[_0xf17d('0x1e')]){delete _0x56d7df['name'];}_0x296113['status'](_0x5169dd)[_0xf17d('0x24')](_0x56d7df);};}exports['index']=function(_0x23a802,_0x3838eb){var _0xd6d72c={},_0x72befe={},_0x36fc4f={'count':0x0,'rows':[]};var _0x5eabe8=_['map'](db[_0xf17d('0x25')][_0xf17d('0x26')],function(_0x2a1b25){return{'name':_0x2a1b25[_0xf17d('0x27')],'type':_0x2a1b25['type'][_0xf17d('0x28')]};});_0x72befe[_0xf17d('0x29')]=_[_0xf17d('0x1b')](_0x5eabe8,_0xf17d('0x1e'));_0x72befe[_0xf17d('0x2a')]=_[_0xf17d('0x2b')](_0x23a802['query']);_0x72befe[_0xf17d('0x2c')]=_[_0xf17d('0x2d')](_0x72befe[_0xf17d('0x29')],_0x72befe[_0xf17d('0x2a')]);_0xd6d72c[_0xf17d('0x2e')]=_[_0xf17d('0x2d')](_0x72befe[_0xf17d('0x29')],qs[_0xf17d('0x2f')](_0x23a802['query']['fields']));_0xd6d72c['attributes']=_0xd6d72c[_0xf17d('0x2e')][_0xf17d('0x30')]?_0xd6d72c[_0xf17d('0x2e')]:_0x72befe[_0xf17d('0x29')];if(!_0x23a802[_0xf17d('0x2a')][_0xf17d('0x31')]('nolimit')){_0xd6d72c[_0xf17d('0x10')]=qs[_0xf17d('0x10')](_0x23a802[_0xf17d('0x2a')][_0xf17d('0x10')]);_0xd6d72c['offset']=qs[_0xf17d('0xe')](_0x23a802[_0xf17d('0x2a')][_0xf17d('0xe')]);}_0xd6d72c[_0xf17d('0x32')]=qs[_0xf17d('0x33')](_0x23a802[_0xf17d('0x2a')][_0xf17d('0x33')]);_0xd6d72c[_0xf17d('0x34')]=qs['filters'](_['pick'](_0x23a802['query'],_0x72befe[_0xf17d('0x2c')]),_0x5eabe8);if(_0x23a802[_0xf17d('0x2a')][_0xf17d('0x35')]){_0xd6d72c[_0xf17d('0x34')]=_[_0xf17d('0x36')](_0xd6d72c['where'],{'$or':_['map'](_0x5eabe8,function(_0x1a706d){if(_0x1a706d[_0xf17d('0x1d')]!==_0xf17d('0x37')){var _0x28c9cd={};_0x28c9cd[_0x1a706d[_0xf17d('0x1e')]]={'$like':'%'+_0x23a802[_0xf17d('0x2a')][_0xf17d('0x35')]+'%'};return _0x28c9cd;}})});}_0xd6d72c=_['merge']({},_0xd6d72c,_0x23a802[_0xf17d('0x38')]);var _0x4b8725={'where':_0xd6d72c['where']};return db[_0xf17d('0x25')]['count'](_0x4b8725)[_0xf17d('0x13')](function(_0x2f2d9a){_0x36fc4f['count']=_0x2f2d9a;if(_0x23a802['query']['includeAll']){_0xd6d72c[_0xf17d('0x39')]=[{'all':!![]}];}return db[_0xf17d('0x25')][_0xf17d('0x3a')](_0xd6d72c);})[_0xf17d('0x13')](function(_0x1a90d3){_0x36fc4f['rows']=_0x1a90d3;return _0x36fc4f;})['then'](respondWithFilteredResult(_0x3838eb,_0xd6d72c))[_0xf17d('0x3b')](handleError(_0x3838eb,null));};exports[_0xf17d('0x3c')]=function(_0x59bfdd,_0x47b346){var _0x46597d={'raw':![],'where':{'id':_0x59bfdd[_0xf17d('0x3d')]['id']}},_0x538541={};_0x538541[_0xf17d('0x29')]=_[_0xf17d('0x2b')](db[_0xf17d('0x25')]['rawAttributes']);_0x538541['query']=_[_0xf17d('0x2b')](_0x59bfdd[_0xf17d('0x2a')]);_0x538541[_0xf17d('0x2c')]=_[_0xf17d('0x2d')](_0x538541[_0xf17d('0x29')],_0x538541[_0xf17d('0x2a')]);_0x46597d[_0xf17d('0x2e')]=_[_0xf17d('0x2d')](_0x538541[_0xf17d('0x29')],qs['fields'](_0x59bfdd['query'][_0xf17d('0x2f')]));_0x46597d['attributes']=_0x46597d['attributes'][_0xf17d('0x30')]?_0x46597d[_0xf17d('0x2e')]:_0x538541[_0xf17d('0x29')];if(_0x59bfdd['query'][_0xf17d('0x3e')]){_0x46597d[_0xf17d('0x39')]=[{'all':!![]}];}_0x46597d=_[_0xf17d('0x36')]({},_0x46597d,_0x59bfdd['options']);return db[_0xf17d('0x25')][_0xf17d('0x3f')](_0x46597d)['then'](handleEntityNotFound(_0x47b346,null))['then'](respondWithResult(_0x47b346,null))[_0xf17d('0x3b')](handleError(_0x47b346,null));};exports[_0xf17d('0x20')]=function(_0xb618b3,_0x1d1989){return db[_0xf17d('0x25')][_0xf17d('0x3f')]({'where':{'id':_0xb618b3[_0xf17d('0x3d')]['id']}})['then'](handleEntityNotFound(_0x1d1989,null))[_0xf17d('0x13')](removeEntity(_0x1d1989,null))['catch'](handleError(_0x1d1989,null));};exports['create']=function(_0xf5d338,_0x3fd78f){if(_0xf5d338[_0xf17d('0x40')]['type']===_0xf17d('0x18')&&!_[_0xf17d('0x41')](_0xf5d338[_0xf17d('0x40')]['exten'],'_')){_0xf5d338[_0xf17d('0x40')][_0xf17d('0x42')]='_'['concat'](_0xf5d338[_0xf17d('0x40')]['exten']);}return db['VoiceExtension'][_0xf17d('0x43')](_0xf5d338['body'],{'raw':![]})['then'](function(_0x3413d5){var _0x51b1ae=_0xf5d338['user'][_0xf17d('0x14')]({'plain':!![]});if(!_0x51b1ae)throw new Error('Unable\x20to\x20retrieve\x20the\x20current\x20user');if(_0x51b1ae[_0xf17d('0x44')]===_0xf17d('0x45')){var _0x2a71d7=_0x3413d5[_0xf17d('0x14')]({'plain':!![]});var _0x4fc206=[{'name':_0xf17d('0x15'),'value':'inbound'},{'name':_0xf17d('0x17'),'value':_0xf17d('0x18')},{'name':_0xf17d('0x19'),'value':'internal'}];var _0x44df96=_[_0xf17d('0x3f')](_0x4fc206,['value',_0x2a71d7['type']])[_0xf17d('0x1e')];return db['UserProfileSection']['find']({'where':{'name':_0x44df96,'userProfileId':_0x51b1ae[_0xf17d('0x46')]},'raw':!![]})[_0xf17d('0x13')](function(_0x1fe40){if(_0x1fe40&&_0x1fe40[_0xf17d('0x47')]===0x0){return db[_0xf17d('0x1f')][_0xf17d('0x43')]({'name':_0x2a71d7['name'],'resourceId':_0x2a71d7['id'],'type':_0x1fe40[_0xf17d('0x1e')],'sectionId':_0x1fe40['id']},{})['then'](function(){return _0x3413d5;});}else{return _0x3413d5;}})['catch'](function(_0x276665){logger['error']('Unable\x20to\x20associate\x20the\x20resource\x20to\x20the\x20User\x20Profile:\x20%s',_0x276665);throw _0x276665;});}return _0x3413d5;})[_0xf17d('0x13')](respondWithResult(_0x3fd78f,0xc9))[_0xf17d('0x3b')](handleError(_0x3fd78f,null));};exports[_0xf17d('0x48')]=function(_0x294d9f,_0x44a758){if(_0x294d9f[_0xf17d('0x40')]['id']){delete _0x294d9f[_0xf17d('0x40')]['id'];}return db['VoiceExtension'][_0xf17d('0x3f')]({'where':{'id':_0x294d9f[_0xf17d('0x3d')]['id']}})['then'](handleEntityNotFound(_0x44a758,null))[_0xf17d('0x13')](function(_0x15590b){if(_0x15590b){return db['VoiceExtension'][_0xf17d('0x48')](_[_0xf17d('0x49')](_0x294d9f[_0xf17d('0x40')],[_0xf17d('0x42'),'context','description',_0xf17d('0x4a'),_0xf17d('0x4b'),_0xf17d('0x4c'),_0xf17d('0x4d')]),{'where':{'context':_0x15590b[_0xf17d('0x4e')],'exten':_0x15590b[_0xf17d('0x42')]},'individualHooks':!![]})[_0xf17d('0x13')](function(_0x35cbec){return _0x35cbec;});}return null;})[_0xf17d('0x13')](respondWithStatusCode(_0x44a758,null))[_0xf17d('0x3b')](handleError(_0x44a758,null));};exports['addApplications']=function(_0x1ac178,_0x117367){var _0x549e42=_0x1ac178[_0xf17d('0x3d')]['id'];var _0x1c7838=_0x1ac178[_0xf17d('0x40')];var _0x372518=0xc8;var _0x8f4ab6=null;return db['sequelize']['transaction']({'isolationLevel':db[_0xf17d('0x4f')][_0xf17d('0x50')]['ISOLATION_LEVELS']['READ_COMMITTED']},function(_0x36f2c2){return db[_0xf17d('0x25')][_0xf17d('0x51')]({'where':{'id':_0x549e42},'transaction':_0x36f2c2})[_0xf17d('0x13')](function(_0x10a396){if(_0x10a396){return db[_0xf17d('0x25')]['destroy']({'where':{'VoiceExtensionId':_0x549e42},'transaction':_0x36f2c2})[_0xf17d('0x13')](function(){var _0x42fb2d=_[_0xf17d('0x1b')](_0x1c7838,function(_0x4306ea){_0x4306ea[_0xf17d('0x52')]=_0x549e42;_0x4306ea[_0xf17d('0x53')]=!![];return _0x4306ea;});return _0x10a396['createRouting'](_0x42fb2d);})[_0xf17d('0x13')](function(_0x5c59f4){return db[_0xf17d('0x25')][_0xf17d('0x54')](_0x5c59f4,{'transaction':_0x36f2c2});});}else{_0x372518=0x194;_0x8f4ab6=[];}});})[_0xf17d('0x13')](function(){if(_0x372518!==0x194){return db[_0xf17d('0x25')][_0xf17d('0x55')]({'where':{'isApp':!![],'VoiceExtensionId':_0x549e42},'order':_0xf17d('0x56')})['then'](function(_0x3c995e){_0x8f4ab6=_0x3c995e;});}})[_0xf17d('0x3b')](function(_0x54189d){_0x372518=0x1f4;logger[_0xf17d('0x22')](_0x54189d[_0xf17d('0x23')]);if(_0x54189d[_0xf17d('0x1e')]){delete _0x54189d[_0xf17d('0x1e')];}_0x8f4ab6=_0x54189d;})[_0xf17d('0x57')](function(){if(_0x8f4ab6===null){_0x117367[_0xf17d('0x21')](_0x372518);}else{if(_0x372518===0x1f4){_0x117367[_0xf17d('0xb')](_0x372518)[_0xf17d('0x24')](_0x8f4ab6);}else{_0x117367[_0xf17d('0xb')](_0x372518)[_0xf17d('0xd')](_0x8f4ab6);}}});};