5778fded846302f0a55d71489054c9a5d3fa53fc
[motion-next.git] / server / api / trunk / trunk.controller.js
1 const a643_0xaeb0=['chan_sip.so','../../config/environment','Trunk,\x20%s,\x20%s,\x20%s','order','params','code','filters','fields','body','model','Reload','key','client','destroy','createdAt','sort','root','/etc/asterisk/sip_xcally_trunks.conf','query','clone','findAll','nolimit','lodash','like','userProfileId','includeAll','writeFileSync','update','updatedAt','include','../../config/logger','sendStatus','attributes','../../mysqldb','Unable\x20to\x20retrieve\x20the\x20current\x20user','utf8','create','where','role','render','Content-Range','limit','map','rawAttributes','status','user','jayson/promise','info','send','VIRTUAL','/etc/asterisk/sip_xcally_registers.conf','catch','merge','end','join','Trunks','debug','stringify','readFileSync','pick','UserProfileResource','message','autoAssociation','omit','then','undefined','request\x20sent','findOne','length','set','registry','Trunk','filter','options','Trunk,\x20%s,\x20%s','keys','offset','get','name','Unable\x20to\x20associate\x20the\x20resource\x20to\x20the\x20User\x20Profile:\x20%s','files/templates/trunk.ejs','index','intersection','../../components/parsers/qs','request','error','path','rows','result','count'];(function(_0x1977ec,_0xaeb073){const _0x2ddbe1=function(_0x2b9704){while(--_0x2b9704){_0x1977ec['push'](_0x1977ec['shift']());}};_0x2ddbe1(++_0xaeb073);}(a643_0xaeb0,0xba));const a643_0x2ddb=function(_0x1977ec,_0xaeb073){_0x1977ec=_0x1977ec-0x0;let _0x2ddbe1=a643_0xaeb0[_0x1977ec];return _0x2ddbe1;};const _0x287c2a=a643_0x2ddb;'use strict';const path=require(_0x287c2a('0x50')),ejs=require('ejs'),fs=require('fs'),_=require(_0x287c2a('0x10')),qs=require(_0x287c2a('0x4d')),logger=require(_0x287c2a('0x18'))('api'),config=require(_0x287c2a('0x55')),db=require(_0x287c2a('0x1b'))['db'],{Op}=require('sequelize'),jayson=require(_0x287c2a('0x28')),client=jayson[_0x287c2a('0x6')]['http']({'port':0x232a});function respondWithRpcPromise(_0x2b9704,_0x5b6ba1,_0x130065,_0x3e3e3d){return new Promise(function(_0x14aef2,_0x11cc89){const _0x40a1a0=a643_0x2ddb,_0x5537fc=_0x3e3e3d||client;return _0x5537fc[_0x40a1a0('0x4e')](_0x2b9704,_0x130065)['then'](function(_0x2714f6){const _0x29a044=_0x40a1a0;logger[_0x29a044('0x29')](_0x29a044('0x44'),_0x5b6ba1,_0x29a044('0x3c')),logger[_0x29a044('0x32')](_0x29a044('0x56'),_0x5b6ba1,_0x29a044('0x3c'),JSON[_0x29a044('0x33')](_0x2714f6));if(_0x2714f6[_0x29a044('0x4f')]){if(_0x2714f6[_0x29a044('0x4f')][_0x29a044('0x59')]===0x1f4)return logger[_0x29a044('0x4f')](_0x29a044('0x44'),_0x5b6ba1,_0x2714f6['error']['message']),_0x11cc89(_0x2714f6[_0x29a044('0x4f')][_0x29a044('0x37')]);return logger[_0x29a044('0x4f')](_0x29a044('0x44'),_0x5b6ba1,_0x2714f6[_0x29a044('0x4f')][_0x29a044('0x37')]),_0x14aef2(_0x2714f6[_0x29a044('0x4f')][_0x29a044('0x37')]);}else logger[_0x29a044('0x29')](_0x29a044('0x44'),_0x5b6ba1,_0x29a044('0x3c')),_0x14aef2(_0x2714f6[_0x29a044('0x52')][_0x29a044('0x37')]);})[_0x40a1a0('0x2d')](function(_0x3d9c03){const _0x5ce052=_0x40a1a0;logger[_0x5ce052('0x4f')](_0x5ce052('0x44'),_0x5b6ba1,_0x3d9c03),_0x11cc89(_0x3d9c03);});});}function respondWithResult(_0x529300,_0x72bf3f){return _0x72bf3f=_0x72bf3f||0xc8,function(_0x4708d7){const _0x2e9ff5=a643_0x2ddb;if(_0x4708d7)return _0x529300[_0x2e9ff5('0x26')](_0x72bf3f)['json'](_0x4708d7);};}function respondWithFilteredResult(_0x52a5d1,_0x11a8d2){return function(_0x5a7069){const _0x2e9f6e=a643_0x2ddb;if(_0x5a7069){const _0x4bdffe=_0x5a7069[_0x2e9f6e('0x53')],_0x373ffe=_0x11a8d2[_0x2e9f6e('0x46')];let _0x530fb2=_0x11a8d2[_0x2e9f6e('0x46')]+_0x11a8d2['limit'],_0x3cd2d5;return _0x530fb2>=_0x4bdffe?(_0x530fb2=_0x4bdffe,_0x3cd2d5=0xc8):_0x3cd2d5=0xce,_0x52a5d1[_0x2e9f6e('0x26')](_0x3cd2d5),_0x52a5d1[_0x2e9f6e('0x3f')](_0x2e9f6e('0x22'),_0x373ffe+'-'+_0x530fb2+'/'+_0x4bdffe)['json'](_0x5a7069);}return null;};}function saveUpdates(_0x1130cf){return function(_0xecdc9){const _0x4ca3c6=a643_0x2ddb;if(_0xecdc9)return _0xecdc9[_0x4ca3c6('0x15')](_0x1130cf)[_0x4ca3c6('0x3a')](function(_0x377504){return _0x377504;});return null;};}function removeEntity(_0x593f01){return function(_0x1d2c56){const _0x259f4e=a643_0x2ddb;if(_0x1d2c56)return _0x1d2c56['destroy']()[_0x259f4e('0x3a')](function(){const _0x5d6f5e=_0x259f4e,_0x362885=_0x1d2c56[_0x5d6f5e('0x47')]({'plain':!![]}),_0x256690=_0x5d6f5e('0x31');return db['UserProfileResource']['destroy']({'where':{'type':_0x256690,'resourceId':_0x362885['id']}})['then'](function(){return _0x1d2c56;});})[_0x259f4e('0x3a')](function(){const _0x587e7a=_0x259f4e;_0x593f01[_0x587e7a('0x26')](0xcc)[_0x587e7a('0x2f')]();});};}function handleEntityNotFound(_0x4357e7){return function(_0x5ec123){const _0xe653a3=a643_0x2ddb;return!_0x5ec123&&_0x4357e7[_0xe653a3('0x19')](0x194),_0x5ec123;};}function handleError(_0x116aa8,_0x39c066){return _0x39c066=_0x39c066||0x1f4,function(_0x487c37){const _0x30903b=a643_0x2ddb;logger[_0x30903b('0x4f')](_0x487c37['stack']),_0x487c37[_0x30903b('0x48')]&&delete _0x487c37['name'],_0x116aa8[_0x30903b('0x26')](_0x39c066)[_0x30903b('0x2a')](_0x487c37);};}exports[_0x287c2a('0x4b')]=function(_0x5d7247,_0x21864c){const _0x11e1ac=_0x287c2a;let _0x42d433={};const _0xbeb1c3={},_0x28b895={'count':0x0,'rows':[]},_0x473b4b=_[_0x11e1ac('0x24')](db[_0x11e1ac('0x41')][_0x11e1ac('0x25')],function(_0x58eda5){const _0x105b5b=_0x11e1ac;return{'name':_0x58eda5['fieldName'],'type':_0x58eda5['type'][_0x105b5b('0x5')]};});_0xbeb1c3[_0x11e1ac('0x3')]=_[_0x11e1ac('0x24')](_0x473b4b,'name'),_0xbeb1c3[_0x11e1ac('0xc')]=_[_0x11e1ac('0x45')](_0x5d7247[_0x11e1ac('0xc')]),_0xbeb1c3[_0x11e1ac('0x0')]=_[_0x11e1ac('0x4c')](_0xbeb1c3[_0x11e1ac('0x3')],_0xbeb1c3[_0x11e1ac('0xc')]),_0x42d433['attributes']=_[_0x11e1ac('0x4c')](_0xbeb1c3[_0x11e1ac('0x3')],qs[_0x11e1ac('0x1')](_0x5d7247[_0x11e1ac('0xc')]['fields'])),_0x42d433[_0x11e1ac('0x1a')]=_0x42d433[_0x11e1ac('0x1a')][_0x11e1ac('0x3e')]?_0x42d433[_0x11e1ac('0x1a')]:_0xbeb1c3[_0x11e1ac('0x3')];typeof _0x5d7247[_0x11e1ac('0xc')][_0x11e1ac('0xf')]===_0x11e1ac('0x3b')&&(_0x42d433[_0x11e1ac('0x23')]=qs[_0x11e1ac('0x23')](_0x5d7247[_0x11e1ac('0xc')]['limit']),_0x42d433[_0x11e1ac('0x46')]=qs[_0x11e1ac('0x46')](_0x5d7247[_0x11e1ac('0xc')]['offset']));_0x42d433[_0x11e1ac('0x57')]=qs[_0x11e1ac('0x9')](_0x5d7247[_0x11e1ac('0xc')][_0x11e1ac('0x9')]),_0x42d433[_0x11e1ac('0x1f')]=qs[_0x11e1ac('0x0')](_[_0x11e1ac('0x35')](_0x5d7247[_0x11e1ac('0xc')],_0xbeb1c3[_0x11e1ac('0x0')]),_0x473b4b);_0x5d7247[_0x11e1ac('0xc')][_0x11e1ac('0x42')]&&(_0x42d433[_0x11e1ac('0x1f')]=_[_0x11e1ac('0x2e')](_0x42d433[_0x11e1ac('0x1f')],{[Op['or']]:_['map'](_0x473b4b,function(_0x1ec709){const _0x3ba46b=_0x11e1ac;if(_0x1ec709['type']!==_0x3ba46b('0x2b')){const _0x2059d0={};return _0x2059d0[_0x1ec709[_0x3ba46b('0x48')]]={[Op[_0x3ba46b('0x11')]]:'%'+_0x5d7247['query'][_0x3ba46b('0x42')]+'%'},_0x2059d0;}})}));_0x42d433=_[_0x11e1ac('0x2e')]({},_0x42d433,_0x5d7247[_0x11e1ac('0x43')]);const _0x273d8c={'where':_0x42d433['where']};return db[_0x11e1ac('0x41')][_0x11e1ac('0x53')](_0x273d8c)[_0x11e1ac('0x3a')](function(_0x563efc){const _0x49cf2a=_0x11e1ac;return _0x28b895[_0x49cf2a('0x53')]=_0x563efc,_0x5d7247[_0x49cf2a('0xc')]['includeAll']&&(_0x42d433[_0x49cf2a('0x17')]=[{'all':!![]}]),db[_0x49cf2a('0x41')][_0x49cf2a('0xe')](_0x42d433);})[_0x11e1ac('0x3a')](function(_0x3e7e88){const _0x97cc71=_0x11e1ac;return _0x28b895[_0x97cc71('0x51')]=_0x3e7e88,_0x28b895;})['then'](respondWithFilteredResult(_0x21864c,_0x42d433))['catch'](handleError(_0x21864c,null));},exports['show']=function(_0x27f1bc,_0x47cf27){const _0x58a3c7=_0x287c2a;let _0x1e7a3b={'raw':![],'where':{'id':_0x27f1bc[_0x58a3c7('0x58')]['id']}};const _0x1c92d4={};return _0x1c92d4[_0x58a3c7('0x3')]=_['keys'](db[_0x58a3c7('0x41')][_0x58a3c7('0x25')]),_0x1c92d4[_0x58a3c7('0xc')]=_[_0x58a3c7('0x45')](_0x27f1bc['query']),_0x1c92d4[_0x58a3c7('0x0')]=_['intersection'](_0x1c92d4[_0x58a3c7('0x3')],_0x1c92d4['query']),_0x1e7a3b[_0x58a3c7('0x1a')]=_[_0x58a3c7('0x4c')](_0x1c92d4[_0x58a3c7('0x3')],qs['fields'](_0x27f1bc[_0x58a3c7('0xc')][_0x58a3c7('0x1')])),_0x1e7a3b[_0x58a3c7('0x1a')]=_0x1e7a3b['attributes']['length']?_0x1e7a3b['attributes']:_0x1c92d4['model'],_0x27f1bc[_0x58a3c7('0xc')]['includeAll']&&(_0x1e7a3b[_0x58a3c7('0x17')]=[{'all':!![]}]),_0x1e7a3b=_['merge']({},_0x1e7a3b,_0x27f1bc[_0x58a3c7('0x43')]),db[_0x58a3c7('0x41')][_0x58a3c7('0x3d')](_0x1e7a3b)[_0x58a3c7('0x3a')](handleEntityNotFound(_0x47cf27,null))[_0x58a3c7('0x3a')](respondWithResult(_0x47cf27,null))['catch'](handleError(_0x47cf27,null));},exports[_0x287c2a('0xd')]=function(_0xdc7926,_0x4f8327){const _0x5e7ee4=_0x287c2a;let _0x166d83={'where':{'id':_0xdc7926['params']['id']}};const _0x19b669={};return _0x19b669[_0x5e7ee4('0x3')]=_['keys'](db[_0x5e7ee4('0x41')]['rawAttributes']),_0x166d83['attributes']=_[_0x5e7ee4('0x4c')](_0x19b669[_0x5e7ee4('0x3')],qs[_0x5e7ee4('0x1')](_0xdc7926[_0x5e7ee4('0xc')][_0x5e7ee4('0x1')])),_0x166d83[_0x5e7ee4('0x1a')]=_0x166d83['attributes'][_0x5e7ee4('0x3e')]?_0x166d83[_0x5e7ee4('0x1a')]:_0x19b669[_0x5e7ee4('0x3')],_0xdc7926[_0x5e7ee4('0xc')][_0x5e7ee4('0x13')]&&(_0x166d83[_0x5e7ee4('0x17')]=[{'all':!![]}]),_0x166d83=_[_0x5e7ee4('0x2e')]({},_0x166d83,_0xdc7926[_0x5e7ee4('0x43')]),db['Trunk']['findOne'](_0x166d83)[_0x5e7ee4('0x3a')](handleEntityNotFound(_0x4f8327,null))[_0x5e7ee4('0x3a')](function(_0x33e572){const _0x33dd05=_0x5e7ee4;if(_0x33e572){let _0x6869a5=_0x33e572[_0x33dd05('0x47')]({'plain':!![]});return _0x6869a5=qs['omit'](_0x6869a5,['id','createdAt','updatedAt']),_0xdc7926['body']=_[_0x33dd05('0x39')](_0xdc7926['body'],['id',_0x33dd05('0x8'),_0x33dd05('0x16')]),db[_0x33dd05('0x41')][_0x33dd05('0x1e')](_[_0x33dd05('0x2e')](_0x6869a5,_0xdc7926[_0x33dd05('0x2')]),{'include':_0xdc7926['query']['includeAll']?[{'all':!![]}]:undefined})[_0x33dd05('0x3a')](function(_0x2c585d){const _0x1f8aec=_0x33dd05,_0x597f2c=_0xdc7926[_0x1f8aec('0x27')][_0x1f8aec('0x47')]({'plain':!![]});if(!_0x597f2c)throw new Error('Unable\x20to\x20retrieve\x20the\x20current\x20user');if(_0x597f2c[_0x1f8aec('0x20')]==='user'){const _0x302f7c=_0x2c585d[_0x1f8aec('0x47')]({'plain':!![]}),_0x7c9e1b=_0x1f8aec('0x31');return db['UserProfileSection'][_0x1f8aec('0x3d')]({'where':{'name':_0x7c9e1b,'userProfileId':_0x597f2c[_0x1f8aec('0x12')]},'raw':!![]})[_0x1f8aec('0x3a')](function(_0x1bd4ba){const _0x1e0b4a=_0x1f8aec;return _0x1bd4ba&&_0x1bd4ba['autoAssociation']===0x0?db[_0x1e0b4a('0x36')][_0x1e0b4a('0x1e')]({'name':_0x302f7c['name'],'resourceId':_0x302f7c['id'],'type':_0x1bd4ba['name'],'sectionId':_0x1bd4ba['id']},{})['then'](function(){return _0x2c585d;}):_0x2c585d;})[_0x1f8aec('0x2d')](function(_0x51e269){const _0x4c84d6=_0x1f8aec;logger['error'](_0x4c84d6('0x49'),_0x51e269);throw _0x51e269;});}return _0x2c585d;});}})['then'](respondWithResult(_0x4f8327,0xc9))[_0x5e7ee4('0x2d')](handleError(_0x4f8327,null));},exports[_0x287c2a('0x1e')]=function(_0x4374ca,_0x246f40){const _0x390340=_0x287c2a;let _0x32b779;return db[_0x390340('0x41')][_0x390340('0x1e')](_0x4374ca[_0x390340('0x2')],{'raw':!![]})[_0x390340('0x3a')](function(_0xe84082){const _0x864d80=_0x390340;return _0x32b779=_0xe84082,db[_0x864d80('0x41')]['findAll']({'raw':!![]});})[_0x390340('0x3a')](function(_0x2fb1c0){const _0x1ce63d=_0x390340,_0x60a186=fs[_0x1ce63d('0x34')](path[_0x1ce63d('0x30')](config[_0x1ce63d('0xa')],_0x1ce63d('0x4a')),_0x1ce63d('0x1d')),_0x596666=fs['readFileSync'](path[_0x1ce63d('0x30')](config[_0x1ce63d('0xa')],'files/templates/registry.ejs'),'utf8'),_0x1d6d71=ejs[_0x1ce63d('0x21')](_0x60a186,{'trunks':_0x2fb1c0||[]});fs[_0x1ce63d('0x14')](_0x1ce63d('0xb'),_0x1d6d71);if(_0x32b779[_0x1ce63d('0x40')]){const _0x4c265f=ejs['render'](_0x596666,{'trunks':_0x2fb1c0||[]});fs[_0x1ce63d('0x14')](_0x1ce63d('0x2c'),_0x4c265f);}})['then'](function(){const _0x2676f1=_0x390340;return respondWithRpcPromise(_0x2676f1('0x4'),_0x2676f1('0x1e'),{'module':_0x2676f1('0x54')});})['then'](function(){const _0x4d858c=_0x390340,_0x206025=_0x4374ca[_0x4d858c('0x27')][_0x4d858c('0x47')]({'plain':!![]});if(!_0x206025)throw new Error(_0x4d858c('0x1c'));if(_0x206025[_0x4d858c('0x20')]===_0x4d858c('0x27')){const _0x4dc018=_0x32b779['get']({'plain':!![]});return db['UserProfileSection'][_0x4d858c('0x3d')]({'where':{'name':'Trunks','userProfileId':_0x206025[_0x4d858c('0x12')]},'raw':!![]})[_0x4d858c('0x3a')](function(_0x54f2fc){const _0x6c7150=_0x4d858c;return _0x54f2fc&&_0x54f2fc[_0x6c7150('0x38')]===0x0?db[_0x6c7150('0x36')][_0x6c7150('0x1e')]({'name':_0x4dc018[_0x6c7150('0x48')],'resourceId':_0x4dc018['id'],'type':_0x54f2fc[_0x6c7150('0x48')],'sectionId':_0x54f2fc['id']},{})[_0x6c7150('0x3a')](function(){return _0x32b779;}):_0x32b779;})[_0x4d858c('0x2d')](function(_0x2e57b3){logger['error']('Unable\x20to\x20associate\x20the\x20resource\x20to\x20the\x20User\x20Profile:\x20%s',_0x2e57b3);throw _0x2e57b3;});}return _0x32b779;})['then'](respondWithResult(_0x246f40,0xc9))[_0x390340('0x2d')](handleError(_0x246f40,null));},exports[_0x287c2a('0x15')]=function(_0x4201f9,_0x3aaeb7){const _0x4e5f70=_0x287c2a;let _0xc3b8a6;return db[_0x4e5f70('0x41')][_0x4e5f70('0x3d')]({'where':{'id':_0x4201f9[_0x4e5f70('0x58')]['id']}})[_0x4e5f70('0x3a')](handleEntityNotFound(_0x3aaeb7,null))[_0x4e5f70('0x3a')](saveUpdates(_0x4201f9[_0x4e5f70('0x2')],null))[_0x4e5f70('0x3a')](function(_0x531b2b){const _0x23d6a6=_0x4e5f70;if(_0x531b2b)return _0xc3b8a6=_0x531b2b,db['Trunk'][_0x23d6a6('0xe')]({'raw':!![]});})[_0x4e5f70('0x3a')](function(_0x24e8b3){const _0x761539=_0x4e5f70;if(_0x24e8b3){const _0x44e065=fs[_0x761539('0x34')](path['join'](config['root'],_0x761539('0x4a')),_0x761539('0x1d')),_0x403c5e=fs[_0x761539('0x34')](path['join'](config['root'],'files/templates/registry.ejs'),_0x761539('0x1d')),_0xe36e5d=ejs[_0x761539('0x21')](_0x44e065,{'trunks':_0x24e8b3||[]});fs[_0x761539('0x14')](_0x761539('0xb'),_0xe36e5d);const _0x8cf6a2=ejs[_0x761539('0x21')](_0x403c5e,{'trunks':_0x24e8b3||[]});return fs[_0x761539('0x14')](_0x761539('0x2c'),_0x8cf6a2),respondWithRpcPromise(_0x761539('0x4'),_0x761539('0x15'),{'module':_0x761539('0x54')});}return null;})[_0x4e5f70('0x3a')](function(_0x19dc33){if(_0x19dc33)return _0xc3b8a6;})['then'](respondWithResult(_0x3aaeb7,null))[_0x4e5f70('0x2d')](handleError(_0x3aaeb7,null));},exports[_0x287c2a('0x7')]=function(_0x1434eb,_0x168b3f){const _0x55f90a=_0x287c2a;return db[_0x55f90a('0x41')][_0x55f90a('0x3d')]({'where':{'id':_0x1434eb['params']['id']}})['then'](handleEntityNotFound(_0x168b3f,null))['then'](removeEntity(_0x168b3f,null))[_0x55f90a('0x3a')](function(){const _0xf88ecb=_0x55f90a;return db[_0xf88ecb('0x41')][_0xf88ecb('0xe')]({'raw':!![]});})[_0x55f90a('0x3a')](function(_0x1bc6df){const _0x51e383=_0x55f90a,_0x5bb46f=fs[_0x51e383('0x34')](path[_0x51e383('0x30')](config[_0x51e383('0xa')],_0x51e383('0x4a')),_0x51e383('0x1d')),_0x49c5fa=fs[_0x51e383('0x34')](path[_0x51e383('0x30')](config[_0x51e383('0xa')],'files/templates/registry.ejs'),'utf8'),_0x22eb6b=ejs['render'](_0x5bb46f,{'trunks':_0x1bc6df}),_0x31e9ef=ejs[_0x51e383('0x21')](_0x49c5fa,{'trunks':_0x1bc6df});return fs['writeFileSync'](_0x51e383('0xb'),_0x22eb6b),fs[_0x51e383('0x14')](_0x51e383('0x2c'),_0x31e9ef),respondWithRpcPromise(_0x51e383('0x4'),_0x51e383('0x7'),{'module':_0x51e383('0x54')});})[_0x55f90a('0x2d')](handleError(_0x168b3f,null));};