Built motion from commit 3c2ce842.|2.6.31
[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 _0x165c=['get','inbound','UserProfileSection','userProfileId','autoAssociation','Unable\x20to\x20associate\x20the\x20resource\x20to\x20the\x20User\x20Profile:\x20%s','update','context','description','recordingFormat','cutdigits','alias','addApplications','sequelize','ISOLATION_LEVELS','findOne','VoiceExtensionId','createRouting','findAndCountAll','send','lodash','ioredis','../../components/parsers/qs','../../config/logger','api','../../config/environment','../../mysqldb','redis','defaults','localhost','./voiceExtension.socket','register','sendStatus','end','status','json','offset','limit','undefined','count','set','Content-Range','then','InboundRoutes','OutboundRoutes','InternalRoutes','internal','ignore','value','includes','type','find','UserProfileResource','destroy','error','stack','name','index','map','VoiceExtension','fieldName','key','model','query','intersection','attributes','fields','length','order','where','filters','pick','filter','merge','VIRTUAL','options','includeAll','findAll','rows','show','params','rawAttributes','keys','include','catch','create','body','outbound','exten','user','Unable\x20to\x20retrieve\x20the\x20current\x20user','role'];(function(_0xd765e7,_0x29faa0){var _0x1d6ded=function(_0x29ea60){while(--_0x29ea60){_0xd765e7['push'](_0xd765e7['shift']());}};_0x1d6ded(++_0x29faa0);}(_0x165c,0x128));var _0xc165=function(_0x31d97a,_0x13b7b9){_0x31d97a=_0x31d97a-0x0;var _0x314a7f=_0x165c[_0x31d97a];return _0x314a7f;};'use strict';var _=require(_0xc165('0x0'));var Redis=require(_0xc165('0x1'));var qs=require(_0xc165('0x2'));var logger=require(_0xc165('0x3'))(_0xc165('0x4'));var config=require(_0xc165('0x5'));var db=require(_0xc165('0x6'))['db'];config[_0xc165('0x7')]=_[_0xc165('0x8')](config['redis'],{'host':_0xc165('0x9'),'port':0x18eb});var socket=require('socket.io-emitter')(new Redis(config[_0xc165('0x7')]));require(_0xc165('0xa'))[_0xc165('0xb')](socket);function respondWithStatusCode(_0x35a4b4,_0x41f08b){_0x41f08b=_0x41f08b||0xcc;return function(_0x5b8c6c){if(_0x5b8c6c){return _0x35a4b4[_0xc165('0xc')](_0x41f08b);}return _0x35a4b4['status'](_0x41f08b)[_0xc165('0xd')]();};}function respondWithResult(_0x35a36f,_0x16c37e){_0x16c37e=_0x16c37e||0xc8;return function(_0x57a2d4){if(_0x57a2d4){return _0x35a36f[_0xc165('0xe')](_0x16c37e)[_0xc165('0xf')](_0x57a2d4);}};}function respondWithFilteredResult(_0x5f44a2,_0x3e4fc6){return function(_0x1415ef){if(_0x1415ef){var _0x1add6c=typeof _0x3e4fc6[_0xc165('0x10')]==='undefined'&&typeof _0x3e4fc6[_0xc165('0x11')]===_0xc165('0x12');var _0x176b13=_0x1415ef[_0xc165('0x13')];var _0x5570c4=_0x1add6c?0x0:_0x3e4fc6[_0xc165('0x10')];var _0x341ae7=_0x1add6c?_0x1415ef[_0xc165('0x13')]:_0x3e4fc6[_0xc165('0x10')]+_0x3e4fc6[_0xc165('0x11')];var _0x156664;if(_0x341ae7>=_0x176b13){_0x341ae7=_0x176b13;_0x156664=0xc8;}else{_0x156664=0xce;}_0x5f44a2[_0xc165('0xe')](_0x156664);return _0x5f44a2[_0xc165('0x14')](_0xc165('0x15'),_0x5570c4+'-'+_0x341ae7+'/'+_0x176b13)[_0xc165('0xf')](_0x1415ef);}return null;};}function removeEntity(_0x16c572){return function(_0x4c8166){if(_0x4c8166){return _0x4c8166['destroy']()[_0xc165('0x16')](function(){var _0x5d9213=_0x4c8166['get']({'plain':!![]});var _0x1ac326=[{'name':_0xc165('0x17'),'value':'inbound','ignore':![]},{'name':_0xc165('0x18'),'value':'outbound','ignore':![]},{'name':_0xc165('0x19'),'value':_0xc165('0x1a'),'ignore':![]}];var _0x272c8c=_(_0x1ac326)['filter'](_0xc165('0x1b'))['map'](_0xc165('0x1c'))[_0xc165('0x1c')]();if(_[_0xc165('0x1d')](_0x272c8c,_0x5d9213[_0xc165('0x1e')])){return;}var _0x2a5fba=_[_0xc165('0x1f')](_0x1ac326,[_0xc165('0x1c'),_0x5d9213[_0xc165('0x1e')]])['name'];return db[_0xc165('0x20')][_0xc165('0x21')]({'where':{'type':_0x2a5fba,'resourceId':_0x5d9213['id']}})[_0xc165('0x16')](function(){return _0x4c8166;});})[_0xc165('0x16')](function(){_0x16c572[_0xc165('0xe')](0xcc)[_0xc165('0xd')]();});}};}function handleEntityNotFound(_0x5a05dc){return function(_0x23f554){if(!_0x23f554){_0x5a05dc[_0xc165('0xc')](0x194);}return _0x23f554;};}function handleError(_0x44cee5,_0x55eed1){_0x55eed1=_0x55eed1||0x1f4;return function(_0x16c577){logger[_0xc165('0x22')](_0x16c577[_0xc165('0x23')]);if(_0x16c577[_0xc165('0x24')]){delete _0x16c577[_0xc165('0x24')];}_0x44cee5[_0xc165('0xe')](_0x55eed1)['send'](_0x16c577);};}exports[_0xc165('0x25')]=function(_0x581a0a,_0x9e3467){var _0x552ca1={},_0x1ff0fb={},_0x326539={'count':0x0,'rows':[]};var _0x5b7d8b=_[_0xc165('0x26')](db[_0xc165('0x27')]['rawAttributes'],function(_0xd0d887){return{'name':_0xd0d887[_0xc165('0x28')],'type':_0xd0d887[_0xc165('0x1e')][_0xc165('0x29')]};});_0x1ff0fb[_0xc165('0x2a')]=_[_0xc165('0x26')](_0x5b7d8b,_0xc165('0x24'));_0x1ff0fb[_0xc165('0x2b')]=_['keys'](_0x581a0a[_0xc165('0x2b')]);_0x1ff0fb['filters']=_[_0xc165('0x2c')](_0x1ff0fb['model'],_0x1ff0fb['query']);_0x552ca1[_0xc165('0x2d')]=_[_0xc165('0x2c')](_0x1ff0fb[_0xc165('0x2a')],qs[_0xc165('0x2e')](_0x581a0a[_0xc165('0x2b')]['fields']));_0x552ca1[_0xc165('0x2d')]=_0x552ca1[_0xc165('0x2d')][_0xc165('0x2f')]?_0x552ca1['attributes']:_0x1ff0fb[_0xc165('0x2a')];if(!_0x581a0a[_0xc165('0x2b')]['hasOwnProperty']('nolimit')){_0x552ca1[_0xc165('0x11')]=qs[_0xc165('0x11')](_0x581a0a['query']['limit']);_0x552ca1[_0xc165('0x10')]=qs[_0xc165('0x10')](_0x581a0a[_0xc165('0x2b')][_0xc165('0x10')]);}_0x552ca1[_0xc165('0x30')]=qs['sort'](_0x581a0a[_0xc165('0x2b')]['sort']);_0x552ca1[_0xc165('0x31')]=qs[_0xc165('0x32')](_[_0xc165('0x33')](_0x581a0a[_0xc165('0x2b')],_0x1ff0fb[_0xc165('0x32')]),_0x5b7d8b);if(_0x581a0a['query'][_0xc165('0x34')]){_0x552ca1[_0xc165('0x31')]=_[_0xc165('0x35')](_0x552ca1[_0xc165('0x31')],{'$or':_[_0xc165('0x26')](_0x5b7d8b,function(_0x34e85f){if(_0x34e85f['type']!==_0xc165('0x36')){var _0x1471ff={};_0x1471ff[_0x34e85f['name']]={'$like':'%'+_0x581a0a[_0xc165('0x2b')][_0xc165('0x34')]+'%'};return _0x1471ff;}})});}_0x552ca1=_[_0xc165('0x35')]({},_0x552ca1,_0x581a0a[_0xc165('0x37')]);var _0x295dde={'where':_0x552ca1[_0xc165('0x31')]};return db[_0xc165('0x27')][_0xc165('0x13')](_0x295dde)[_0xc165('0x16')](function(_0x2a17df){_0x326539[_0xc165('0x13')]=_0x2a17df;if(_0x581a0a[_0xc165('0x2b')][_0xc165('0x38')]){_0x552ca1['include']=[{'all':!![]}];}return db[_0xc165('0x27')][_0xc165('0x39')](_0x552ca1);})['then'](function(_0x20202f){_0x326539[_0xc165('0x3a')]=_0x20202f;return _0x326539;})[_0xc165('0x16')](respondWithFilteredResult(_0x9e3467,_0x552ca1))['catch'](handleError(_0x9e3467,null));};exports[_0xc165('0x3b')]=function(_0x1f50fc,_0x6d0d65){var _0x3e9b7e={'raw':![],'where':{'id':_0x1f50fc[_0xc165('0x3c')]['id']}},_0x2484ff={};_0x2484ff[_0xc165('0x2a')]=_['keys'](db[_0xc165('0x27')][_0xc165('0x3d')]);_0x2484ff[_0xc165('0x2b')]=_[_0xc165('0x3e')](_0x1f50fc[_0xc165('0x2b')]);_0x2484ff[_0xc165('0x32')]=_['intersection'](_0x2484ff['model'],_0x2484ff['query']);_0x3e9b7e[_0xc165('0x2d')]=_[_0xc165('0x2c')](_0x2484ff[_0xc165('0x2a')],qs[_0xc165('0x2e')](_0x1f50fc[_0xc165('0x2b')][_0xc165('0x2e')]));_0x3e9b7e['attributes']=_0x3e9b7e[_0xc165('0x2d')][_0xc165('0x2f')]?_0x3e9b7e[_0xc165('0x2d')]:_0x2484ff['model'];if(_0x1f50fc['query'][_0xc165('0x38')]){_0x3e9b7e[_0xc165('0x3f')]=[{'all':!![]}];}_0x3e9b7e=_[_0xc165('0x35')]({},_0x3e9b7e,_0x1f50fc[_0xc165('0x37')]);return db[_0xc165('0x27')][_0xc165('0x1f')](_0x3e9b7e)[_0xc165('0x16')](handleEntityNotFound(_0x6d0d65,null))[_0xc165('0x16')](respondWithResult(_0x6d0d65,null))['catch'](handleError(_0x6d0d65,null));};exports['destroy']=function(_0x1cf32c,_0x121805){return db[_0xc165('0x27')][_0xc165('0x1f')]({'where':{'id':_0x1cf32c[_0xc165('0x3c')]['id']}})['then'](handleEntityNotFound(_0x121805,null))['then'](removeEntity(_0x121805,null))[_0xc165('0x40')](handleError(_0x121805,null));};exports[_0xc165('0x41')]=function(_0x152baa,_0x4e9f34){if(_0x152baa[_0xc165('0x42')][_0xc165('0x1e')]===_0xc165('0x43')&&!_['startsWith'](_0x152baa['body'][_0xc165('0x44')],'_')){_0x152baa[_0xc165('0x42')][_0xc165('0x44')]='_'['concat'](_0x152baa['body'][_0xc165('0x44')]);}return db[_0xc165('0x27')]['create'](_0x152baa[_0xc165('0x42')],{'raw':![]})[_0xc165('0x16')](function(_0xb5139c){var _0x56f00a=_0x152baa[_0xc165('0x45')]['get']({'plain':!![]});if(!_0x56f00a)throw new Error(_0xc165('0x46'));if(_0x56f00a[_0xc165('0x47')]===_0xc165('0x45')){var _0x5794f9=_0xb5139c[_0xc165('0x48')]({'plain':!![]});var _0x20085c=[{'name':_0xc165('0x17'),'value':_0xc165('0x49')},{'name':_0xc165('0x18'),'value':'outbound'},{'name':_0xc165('0x19'),'value':_0xc165('0x1a')}];var _0x315f82=_[_0xc165('0x1f')](_0x20085c,['value',_0x5794f9[_0xc165('0x1e')]])['name'];return db[_0xc165('0x4a')][_0xc165('0x1f')]({'where':{'name':_0x315f82,'userProfileId':_0x56f00a[_0xc165('0x4b')]},'raw':!![]})['then'](function(_0x4a3edd){if(_0x4a3edd&&_0x4a3edd[_0xc165('0x4c')]===0x0){return db[_0xc165('0x20')][_0xc165('0x41')]({'name':_0x5794f9[_0xc165('0x24')],'resourceId':_0x5794f9['id'],'type':_0x4a3edd[_0xc165('0x24')],'sectionId':_0x4a3edd['id']},{})['then'](function(){return _0xb5139c;});}else{return _0xb5139c;}})[_0xc165('0x40')](function(_0x1a91f5){logger[_0xc165('0x22')](_0xc165('0x4d'),_0x1a91f5);throw _0x1a91f5;});}return _0xb5139c;})['then'](respondWithResult(_0x4e9f34,0xc9))['catch'](handleError(_0x4e9f34,null));};exports['update']=function(_0x52a90a,_0x1134c9){if(_0x52a90a['body']['id']){delete _0x52a90a[_0xc165('0x42')]['id'];}return db['VoiceExtension'][_0xc165('0x1f')]({'where':{'id':_0x52a90a[_0xc165('0x3c')]['id']}})[_0xc165('0x16')](handleEntityNotFound(_0x1134c9,null))[_0xc165('0x16')](function(_0x27c789){if(_0x27c789){return db[_0xc165('0x27')][_0xc165('0x4e')](_[_0xc165('0x33')](_0x52a90a[_0xc165('0x42')],['exten',_0xc165('0x4f'),_0xc165('0x50'),_0xc165('0x51'),_0xc165('0x52'),'mandatoryDisposition',_0xc165('0x53')]),{'where':{'context':_0x27c789[_0xc165('0x4f')],'exten':_0x27c789[_0xc165('0x44')]},'individualHooks':!![]})[_0xc165('0x16')](function(_0x3613e2){return _0x3613e2;});}return null;})['then'](respondWithStatusCode(_0x1134c9,null))[_0xc165('0x40')](handleError(_0x1134c9,null));};exports[_0xc165('0x54')]=function(_0xc568c6,_0x246e6a){var _0x1f7ca1=_0xc568c6['params']['id'];var _0x228fc7=_0xc568c6[_0xc165('0x42')];var _0x5b4932=0xc8;var _0x1f96df=null;return db['sequelize']['transaction']({'isolationLevel':db[_0xc165('0x55')]['Transaction'][_0xc165('0x56')]['READ_COMMITTED']},function(_0x23ba4b){return db[_0xc165('0x27')][_0xc165('0x57')]({'where':{'id':_0x1f7ca1},'transaction':_0x23ba4b})[_0xc165('0x16')](function(_0x4e701a){if(_0x4e701a){return db[_0xc165('0x27')][_0xc165('0x21')]({'where':{'VoiceExtensionId':_0x1f7ca1},'transaction':_0x23ba4b})[_0xc165('0x16')](function(){var _0x5c328e=_[_0xc165('0x26')](_0x228fc7,function(_0x3ad605){_0x3ad605[_0xc165('0x58')]=_0x1f7ca1;_0x3ad605['isApp']=!![];return _0x3ad605;});return _0x4e701a[_0xc165('0x59')](_0x5c328e);})[_0xc165('0x16')](function(_0x3859d0){return db[_0xc165('0x27')]['bulkCreate'](_0x3859d0,{'transaction':_0x23ba4b});});}else{_0x5b4932=0x194;_0x1f96df=[];}});})[_0xc165('0x16')](function(){if(_0x5b4932!==0x194){return db[_0xc165('0x27')][_0xc165('0x5a')]({'where':{'isApp':!![],'VoiceExtensionId':_0x1f7ca1},'order':'cast(priority\x20as\x20unsigned)'})[_0xc165('0x16')](function(_0x888d97){_0x1f96df=_0x888d97;});}})[_0xc165('0x40')](function(_0x35c9af){_0x5b4932=0x1f4;logger[_0xc165('0x22')](_0x35c9af[_0xc165('0x23')]);if(_0x35c9af['name']){delete _0x35c9af[_0xc165('0x24')];}_0x1f96df=_0x35c9af;})['finally'](function(){if(_0x1f96df===null){_0x246e6a[_0xc165('0xc')](_0x5b4932);}else{if(_0x5b4932===0x1f4){_0x246e6a['status'](_0x5b4932)[_0xc165('0x5b')](_0x1f96df);}else{_0x246e6a[_0xc165('0xe')](_0x5b4932)[_0xc165('0xf')](_0x1f96df);}}});};