Built motion from commit f0b1811d.|2.6.6
[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 _0xf159=['show','params','options','catch','create','startsWith','exten','body','concat','user','Unable\x20to\x20retrieve\x20the\x20current\x20user','role','OutboundRoutes','userProfileId','autoAssociation','error','Unable\x20to\x20associate\x20the\x20resource\x20to\x20the\x20User\x20Profile:\x20%s','update','context','description','recordingFormat','cutdigits','mandatoryDisposition','alias','addApplications','sequelize','Transaction','READ_COMMITTED','findOne','VoiceExtensionId','isApp','createRouting','findAndCountAll','cast(priority\x20as\x20unsigned)','send','lodash','ioredis','../../components/parsers/qs','../../mysqldb','redis','defaults','localhost','socket.io-emitter','./voiceExtension.socket','sendStatus','status','end','json','offset','undefined','limit','count','set','Content-Range','destroy','get','InboundRoutes','inbound','outbound','InternalRoutes','internal','filter','map','value','includes','type','find','name','UserProfileResource','then','stack','index','VoiceExtension','rawAttributes','fieldName','key','model','keys','query','filters','attributes','intersection','fields','length','hasOwnProperty','nolimit','order','sort','where','pick','merge','VIRTUAL','includeAll','include','findAll','rows'];(function(_0x4dd6d4,_0x31a217){var _0x3d3177=function(_0x405a1a){while(--_0x405a1a){_0x4dd6d4['push'](_0x4dd6d4['shift']());}};_0x3d3177(++_0x31a217);}(_0xf159,0xe3));var _0x9f15=function(_0x1f019a,_0x2abe70){_0x1f019a=_0x1f019a-0x0;var _0x1864ec=_0xf159[_0x1f019a];return _0x1864ec;};'use strict';var _=require(_0x9f15('0x0'));var Redis=require(_0x9f15('0x1'));var qs=require(_0x9f15('0x2'));var logger=require('../../config/logger')('api');var config=require('../../config/environment');var db=require(_0x9f15('0x3'))['db'];config[_0x9f15('0x4')]=_[_0x9f15('0x5')](config[_0x9f15('0x4')],{'host':_0x9f15('0x6'),'port':0x18eb});var socket=require(_0x9f15('0x7'))(new Redis(config['redis']));require(_0x9f15('0x8'))['register'](socket);function respondWithStatusCode(_0x2cea3f,_0x55b075){_0x55b075=_0x55b075||0xcc;return function(_0x381678){if(_0x381678){return _0x2cea3f[_0x9f15('0x9')](_0x55b075);}return _0x2cea3f[_0x9f15('0xa')](_0x55b075)[_0x9f15('0xb')]();};}function respondWithResult(_0x1b7c6a,_0x1b7f46){_0x1b7f46=_0x1b7f46||0xc8;return function(_0xccf9b5){if(_0xccf9b5){return _0x1b7c6a[_0x9f15('0xa')](_0x1b7f46)[_0x9f15('0xc')](_0xccf9b5);}};}function respondWithFilteredResult(_0x2d5bf2,_0x121937){return function(_0x473f76){if(_0x473f76){var _0x1b50de=typeof _0x121937[_0x9f15('0xd')]===_0x9f15('0xe')&&typeof _0x121937[_0x9f15('0xf')]===_0x9f15('0xe');var _0x30f844=_0x473f76[_0x9f15('0x10')];var _0x3774d4=_0x1b50de?0x0:_0x121937[_0x9f15('0xd')];var _0xb81243=_0x1b50de?_0x473f76[_0x9f15('0x10')]:_0x121937[_0x9f15('0xd')]+_0x121937[_0x9f15('0xf')];var _0x333bf6;if(_0xb81243>=_0x30f844){_0xb81243=_0x30f844;_0x333bf6=0xc8;}else{_0x333bf6=0xce;}_0x2d5bf2[_0x9f15('0xa')](_0x333bf6);return _0x2d5bf2[_0x9f15('0x11')](_0x9f15('0x12'),_0x3774d4+'-'+_0xb81243+'/'+_0x30f844)[_0x9f15('0xc')](_0x473f76);}return null;};}function removeEntity(_0x110187){return function(_0xb3d373){if(_0xb3d373){return _0xb3d373[_0x9f15('0x13')]()['then'](function(){var _0x19c294=_0xb3d373[_0x9f15('0x14')]({'plain':!![]});var _0x481760=[{'name':_0x9f15('0x15'),'value':_0x9f15('0x16'),'ignore':![]},{'name':'OutboundRoutes','value':_0x9f15('0x17'),'ignore':![]},{'name':_0x9f15('0x18'),'value':_0x9f15('0x19'),'ignore':![]}];var _0x94062b=_(_0x481760)[_0x9f15('0x1a')]('ignore')[_0x9f15('0x1b')](_0x9f15('0x1c'))[_0x9f15('0x1c')]();if(_[_0x9f15('0x1d')](_0x94062b,_0x19c294[_0x9f15('0x1e')])){return;}var _0x3b2562=_[_0x9f15('0x1f')](_0x481760,[_0x9f15('0x1c'),_0x19c294[_0x9f15('0x1e')]])[_0x9f15('0x20')];return db[_0x9f15('0x21')]['destroy']({'where':{'type':_0x3b2562,'resourceId':_0x19c294['id']}})[_0x9f15('0x22')](function(){return _0xb3d373;});})[_0x9f15('0x22')](function(){_0x110187['status'](0xcc)[_0x9f15('0xb')]();});}};}function handleEntityNotFound(_0xb42c54){return function(_0x20b73e){if(!_0x20b73e){_0xb42c54['sendStatus'](0x194);}return _0x20b73e;};}function handleError(_0x2eb268,_0x102f40){_0x102f40=_0x102f40||0x1f4;return function(_0x3ecf98){logger['error'](_0x3ecf98[_0x9f15('0x23')]);if(_0x3ecf98[_0x9f15('0x20')]){delete _0x3ecf98['name'];}_0x2eb268[_0x9f15('0xa')](_0x102f40)['send'](_0x3ecf98);};}exports[_0x9f15('0x24')]=function(_0x133459,_0x4785eb){var _0x1757d7={},_0x3d6329={},_0x59d24b={'count':0x0,'rows':[]};var _0x23562b=_['map'](db[_0x9f15('0x25')][_0x9f15('0x26')],function(_0x53c10b){return{'name':_0x53c10b[_0x9f15('0x27')],'type':_0x53c10b['type'][_0x9f15('0x28')]};});_0x3d6329[_0x9f15('0x29')]=_[_0x9f15('0x1b')](_0x23562b,'name');_0x3d6329['query']=_[_0x9f15('0x2a')](_0x133459[_0x9f15('0x2b')]);_0x3d6329[_0x9f15('0x2c')]=_['intersection'](_0x3d6329[_0x9f15('0x29')],_0x3d6329[_0x9f15('0x2b')]);_0x1757d7[_0x9f15('0x2d')]=_[_0x9f15('0x2e')](_0x3d6329['model'],qs['fields'](_0x133459['query'][_0x9f15('0x2f')]));_0x1757d7[_0x9f15('0x2d')]=_0x1757d7['attributes'][_0x9f15('0x30')]?_0x1757d7['attributes']:_0x3d6329[_0x9f15('0x29')];if(!_0x133459[_0x9f15('0x2b')][_0x9f15('0x31')](_0x9f15('0x32'))){_0x1757d7['limit']=qs[_0x9f15('0xf')](_0x133459[_0x9f15('0x2b')][_0x9f15('0xf')]);_0x1757d7[_0x9f15('0xd')]=qs[_0x9f15('0xd')](_0x133459['query'][_0x9f15('0xd')]);}_0x1757d7[_0x9f15('0x33')]=qs[_0x9f15('0x34')](_0x133459['query'][_0x9f15('0x34')]);_0x1757d7[_0x9f15('0x35')]=qs[_0x9f15('0x2c')](_[_0x9f15('0x36')](_0x133459[_0x9f15('0x2b')],_0x3d6329[_0x9f15('0x2c')]),_0x23562b);if(_0x133459['query'][_0x9f15('0x1a')]){_0x1757d7[_0x9f15('0x35')]=_[_0x9f15('0x37')](_0x1757d7[_0x9f15('0x35')],{'$or':_['map'](_0x23562b,function(_0x2b2517){if(_0x2b2517[_0x9f15('0x1e')]!==_0x9f15('0x38')){var _0x26903c={};_0x26903c[_0x2b2517[_0x9f15('0x20')]]={'$like':'%'+_0x133459[_0x9f15('0x2b')][_0x9f15('0x1a')]+'%'};return _0x26903c;}})});}_0x1757d7=_['merge']({},_0x1757d7,_0x133459['options']);var _0x4a14d8={'where':_0x1757d7['where']};return db[_0x9f15('0x25')][_0x9f15('0x10')](_0x4a14d8)[_0x9f15('0x22')](function(_0x2d8b76){_0x59d24b['count']=_0x2d8b76;if(_0x133459[_0x9f15('0x2b')][_0x9f15('0x39')]){_0x1757d7[_0x9f15('0x3a')]=[{'all':!![]}];}return db['VoiceExtension'][_0x9f15('0x3b')](_0x1757d7);})[_0x9f15('0x22')](function(_0x136b69){_0x59d24b[_0x9f15('0x3c')]=_0x136b69;return _0x59d24b;})[_0x9f15('0x22')](respondWithFilteredResult(_0x4785eb,_0x1757d7))['catch'](handleError(_0x4785eb,null));};exports[_0x9f15('0x3d')]=function(_0x364df6,_0x2580e0){var _0x15af88={'raw':![],'where':{'id':_0x364df6[_0x9f15('0x3e')]['id']}},_0x29098a={};_0x29098a[_0x9f15('0x29')]=_[_0x9f15('0x2a')](db[_0x9f15('0x25')]['rawAttributes']);_0x29098a['query']=_['keys'](_0x364df6[_0x9f15('0x2b')]);_0x29098a[_0x9f15('0x2c')]=_[_0x9f15('0x2e')](_0x29098a[_0x9f15('0x29')],_0x29098a['query']);_0x15af88[_0x9f15('0x2d')]=_[_0x9f15('0x2e')](_0x29098a['model'],qs[_0x9f15('0x2f')](_0x364df6['query']['fields']));_0x15af88[_0x9f15('0x2d')]=_0x15af88[_0x9f15('0x2d')][_0x9f15('0x30')]?_0x15af88['attributes']:_0x29098a[_0x9f15('0x29')];if(_0x364df6[_0x9f15('0x2b')]['includeAll']){_0x15af88[_0x9f15('0x3a')]=[{'all':!![]}];}_0x15af88=_['merge']({},_0x15af88,_0x364df6[_0x9f15('0x3f')]);return db[_0x9f15('0x25')][_0x9f15('0x1f')](_0x15af88)[_0x9f15('0x22')](handleEntityNotFound(_0x2580e0,null))[_0x9f15('0x22')](respondWithResult(_0x2580e0,null))[_0x9f15('0x40')](handleError(_0x2580e0,null));};exports[_0x9f15('0x13')]=function(_0x5129bb,_0x5ce6ed){return db['VoiceExtension'][_0x9f15('0x1f')]({'where':{'id':_0x5129bb[_0x9f15('0x3e')]['id']}})[_0x9f15('0x22')](handleEntityNotFound(_0x5ce6ed,null))['then'](removeEntity(_0x5ce6ed,null))[_0x9f15('0x40')](handleError(_0x5ce6ed,null));};exports[_0x9f15('0x41')]=function(_0x12de16,_0x3239f0){if(_0x12de16['body'][_0x9f15('0x1e')]===_0x9f15('0x17')&&!_[_0x9f15('0x42')](_0x12de16['body'][_0x9f15('0x43')],'_')){_0x12de16[_0x9f15('0x44')][_0x9f15('0x43')]='_'[_0x9f15('0x45')](_0x12de16['body'][_0x9f15('0x43')]);}return db[_0x9f15('0x25')][_0x9f15('0x41')](_0x12de16[_0x9f15('0x44')],{'raw':![]})[_0x9f15('0x22')](function(_0x362bff){var _0x363353=_0x12de16[_0x9f15('0x46')][_0x9f15('0x14')]({'plain':!![]});if(!_0x363353)throw new Error(_0x9f15('0x47'));if(_0x363353[_0x9f15('0x48')]===_0x9f15('0x46')){var _0x231393=_0x362bff[_0x9f15('0x14')]({'plain':!![]});var _0x388db0=[{'name':'InboundRoutes','value':_0x9f15('0x16')},{'name':_0x9f15('0x49'),'value':_0x9f15('0x17')},{'name':_0x9f15('0x18'),'value':_0x9f15('0x19')}];var _0xddd90=_[_0x9f15('0x1f')](_0x388db0,[_0x9f15('0x1c'),_0x231393[_0x9f15('0x1e')]])[_0x9f15('0x20')];return db['UserProfileSection']['find']({'where':{'name':_0xddd90,'userProfileId':_0x363353[_0x9f15('0x4a')]},'raw':!![]})['then'](function(_0x4c085d){if(_0x4c085d&&_0x4c085d[_0x9f15('0x4b')]===0x0){return db[_0x9f15('0x21')][_0x9f15('0x41')]({'name':_0x231393[_0x9f15('0x20')],'resourceId':_0x231393['id'],'type':_0x4c085d[_0x9f15('0x20')],'sectionId':_0x4c085d['id']},{})['then'](function(){return _0x362bff;});}else{return _0x362bff;}})[_0x9f15('0x40')](function(_0x4602d3){logger[_0x9f15('0x4c')](_0x9f15('0x4d'),_0x4602d3);throw _0x4602d3;});}return _0x362bff;})['then'](respondWithResult(_0x3239f0,0xc9))['catch'](handleError(_0x3239f0,null));};exports[_0x9f15('0x4e')]=function(_0x461220,_0x3b9e38){if(_0x461220[_0x9f15('0x44')]['id']){delete _0x461220[_0x9f15('0x44')]['id'];}return db['VoiceExtension'][_0x9f15('0x1f')]({'where':{'id':_0x461220['params']['id']}})[_0x9f15('0x22')](handleEntityNotFound(_0x3b9e38,null))[_0x9f15('0x22')](function(_0x364680){if(_0x364680){return db[_0x9f15('0x25')][_0x9f15('0x4e')](_['pick'](_0x461220['body'],[_0x9f15('0x43'),_0x9f15('0x4f'),_0x9f15('0x50'),_0x9f15('0x51'),_0x9f15('0x52'),_0x9f15('0x53'),_0x9f15('0x54')]),{'where':{'context':_0x364680['context'],'exten':_0x364680[_0x9f15('0x43')]},'individualHooks':!![]})['then'](function(_0x3aab81){return _0x3aab81;});}return null;})[_0x9f15('0x22')](respondWithStatusCode(_0x3b9e38,null))['catch'](handleError(_0x3b9e38,null));};exports[_0x9f15('0x55')]=function(_0x1846a8,_0x521017){var _0x326e59=_0x1846a8[_0x9f15('0x3e')]['id'];var _0x288c3d=_0x1846a8[_0x9f15('0x44')];var _0x5541ba=0xc8;var _0x50e6b7=null;return db[_0x9f15('0x56')]['transaction']({'isolationLevel':db['sequelize'][_0x9f15('0x57')]['ISOLATION_LEVELS'][_0x9f15('0x58')]},function(_0x299aec){return db['VoiceExtension'][_0x9f15('0x59')]({'where':{'id':_0x326e59},'transaction':_0x299aec})['then'](function(_0x1eafa5){if(_0x1eafa5){return db[_0x9f15('0x25')][_0x9f15('0x13')]({'where':{'VoiceExtensionId':_0x326e59},'transaction':_0x299aec})[_0x9f15('0x22')](function(){var _0x8fe3f6=_[_0x9f15('0x1b')](_0x288c3d,function(_0x20651c){_0x20651c[_0x9f15('0x5a')]=_0x326e59;_0x20651c[_0x9f15('0x5b')]=!![];return _0x20651c;});return _0x1eafa5[_0x9f15('0x5c')](_0x8fe3f6);})['then'](function(_0x1cd654){return db[_0x9f15('0x25')]['bulkCreate'](_0x1cd654,{'transaction':_0x299aec});});}else{_0x5541ba=0x194;_0x50e6b7=[];}});})['then'](function(){if(_0x5541ba!==0x194){return db[_0x9f15('0x25')][_0x9f15('0x5d')]({'where':{'isApp':!![],'VoiceExtensionId':_0x326e59},'order':_0x9f15('0x5e')})[_0x9f15('0x22')](function(_0x579040){_0x50e6b7=_0x579040;});}})[_0x9f15('0x40')](function(_0x45b78b){_0x5541ba=0x1f4;logger[_0x9f15('0x4c')](_0x45b78b[_0x9f15('0x23')]);if(_0x45b78b[_0x9f15('0x20')]){delete _0x45b78b['name'];}_0x50e6b7=_0x45b78b;})['finally'](function(){if(_0x50e6b7===null){_0x521017['sendStatus'](_0x5541ba);}else{if(_0x5541ba===0x1f4){_0x521017[_0x9f15('0xa')](_0x5541ba)[_0x9f15('0x5f')](_0x50e6b7);}else{_0x521017[_0x9f15('0xa')](_0x5541ba)['json'](_0x50e6b7);}}});};