69559bdfbe1aad070d7fb37a1d38c697bec0e198
[motion-next.git] / server / api / trunk / trunk.controller.js
1 const a643_0x4a51=['update','set','key','createdAt','lodash','VIRTUAL','order','sequelize','filter','UserProfileResource','Trunk','join','/etc/asterisk/sip_xcally_registers.conf','chan_sip.so','nolimit','destroy','merge','stack','omit','count','role','result','keys','Content-Range','render','files/templates/trunk.ejs','../../components/parsers/qs','info','request\x20sent','files/templates/registry.ejs','where','autoAssociation','length','filters','clone','json','Unable\x20to\x20retrieve\x20the\x20current\x20user','Unable\x20to\x20associate\x20the\x20resource\x20to\x20the\x20User\x20Profile:\x20%s','/etc/asterisk/sip_xcally_trunks.conf','body','rawAttributes','Trunk,\x20%s,\x20%s','http','catch','registry','pick','end','user','model','get','create','sendStatus','include','intersection','fieldName','includeAll','readFileSync','attributes','request','name','root','error','params','code','type','show','send','message','UserProfileSection','userProfileId','Trunks','sort','findOne','then','limit','index','query','api','map','updatedAt','fields','client','utf8','findAll','../../config/logger','status','options','writeFileSync','jayson/promise','offset','Reload'];(function(_0x52234e,_0x4a51e8){const _0x2b477f=function(_0x130079){while(--_0x130079){_0x52234e['push'](_0x52234e['shift']());}};_0x2b477f(++_0x4a51e8);}(a643_0x4a51,0x1e4));const a643_0x2b47=function(_0x52234e,_0x4a51e8){_0x52234e=_0x52234e-0x0;let _0x2b477f=a643_0x4a51[_0x52234e];return _0x2b477f;};const _0x5939dd=a643_0x2b47;'use strict';const path=require('path'),ejs=require('ejs'),fs=require('fs'),_=require(_0x5939dd('0x42')),qs=require(_0x5939dd('0x58')),logger=require(_0x5939dd('0x37'))(_0x5939dd('0x30')),config=require('../../config/environment'),db=require('../../mysqldb')['db'],{Op}=require(_0x5939dd('0x45')),jayson=require(_0x5939dd('0x3b')),client=jayson[_0x5939dd('0x34')][_0x5939dd('0xd')]({'port':0x232a});function respondWithRpcPromise(_0x130079,_0x570007,_0x4a4773,_0x5c08f1){return new Promise(function(_0x4ab911,_0x5094a2){const _0x4c4591=a643_0x2b47,_0x2e4081=_0x5c08f1||client;return _0x2e4081[_0x4c4591('0x1d')](_0x130079,_0x4a4773)[_0x4c4591('0x2c')](function(_0x569153){const _0x58a384=_0x4c4591;logger[_0x58a384('0x59')](_0x58a384('0xc'),_0x570007,_0x58a384('0x5a')),logger['debug']('Trunk,\x20%s,\x20%s,\x20%s',_0x570007,_0x58a384('0x5a'),JSON['stringify'](_0x569153));if(_0x569153[_0x58a384('0x20')]){if(_0x569153[_0x58a384('0x20')][_0x58a384('0x22')]===0x1f4)return logger[_0x58a384('0x20')](_0x58a384('0xc'),_0x570007,_0x569153[_0x58a384('0x20')][_0x58a384('0x26')]),_0x5094a2(_0x569153[_0x58a384('0x20')][_0x58a384('0x26')]);return logger[_0x58a384('0x20')](_0x58a384('0xc'),_0x570007,_0x569153[_0x58a384('0x20')][_0x58a384('0x26')]),_0x4ab911(_0x569153[_0x58a384('0x20')]['message']);}else logger[_0x58a384('0x59')](_0x58a384('0xc'),_0x570007,'request\x20sent'),_0x4ab911(_0x569153[_0x58a384('0x53')][_0x58a384('0x26')]);})['catch'](function(_0x1f68a9){const _0x5d6507=_0x4c4591;logger[_0x5d6507('0x20')]('Trunk,\x20%s,\x20%s',_0x570007,_0x1f68a9),_0x5094a2(_0x1f68a9);});});}function respondWithResult(_0x1d6517,_0x55062d){return _0x55062d=_0x55062d||0xc8,function(_0x268a0b){const _0x329a65=a643_0x2b47;if(_0x268a0b)return _0x1d6517[_0x329a65('0x38')](_0x55062d)['json'](_0x268a0b);};}function respondWithFilteredResult(_0x2f5283,_0x3ec915){return function(_0x1a99f9){const _0x3332a7=a643_0x2b47;if(_0x1a99f9){const _0x3fb2cb=_0x1a99f9[_0x3332a7('0x51')],_0x15b893=_0x3ec915['offset'];let _0x4f923a=_0x3ec915[_0x3332a7('0x3c')]+_0x3ec915[_0x3332a7('0x2d')],_0xe44b78;return _0x4f923a>=_0x3fb2cb?(_0x4f923a=_0x3fb2cb,_0xe44b78=0xc8):_0xe44b78=0xce,_0x2f5283[_0x3332a7('0x38')](_0xe44b78),_0x2f5283[_0x3332a7('0x3f')](_0x3332a7('0x55'),_0x15b893+'-'+_0x4f923a+'/'+_0x3fb2cb)[_0x3332a7('0x6')](_0x1a99f9);}return null;};}function saveUpdates(_0x41bd12){return function(_0x44450a){const _0x379a7e=a643_0x2b47;if(_0x44450a)return _0x44450a[_0x379a7e('0x3e')](_0x41bd12)[_0x379a7e('0x2c')](function(_0x38fc3a){return _0x38fc3a;});return null;};}function removeEntity(_0x464f49){return function(_0x50908a){const _0x344a8d=a643_0x2b47;if(_0x50908a)return _0x50908a['destroy']()[_0x344a8d('0x2c')](function(){const _0x54fff4=_0x344a8d,_0x294939=_0x50908a['get']({'plain':!![]}),_0x48953b=_0x54fff4('0x29');return db[_0x54fff4('0x47')]['destroy']({'where':{'type':_0x48953b,'resourceId':_0x294939['id']}})[_0x54fff4('0x2c')](function(){return _0x50908a;});})['then'](function(){const _0x351164=_0x344a8d;_0x464f49[_0x351164('0x38')](0xcc)[_0x351164('0x11')]();});};}function handleEntityNotFound(_0x4ca906){return function(_0xd836d3){const _0x4f9a12=a643_0x2b47;return!_0xd836d3&&_0x4ca906[_0x4f9a12('0x16')](0x194),_0xd836d3;};}function handleError(_0x585426,_0x2f42cd){return _0x2f42cd=_0x2f42cd||0x1f4,function(_0x11e453){const _0x5ef470=a643_0x2b47;logger[_0x5ef470('0x20')](_0x11e453[_0x5ef470('0x4f')]),_0x11e453['name']&&delete _0x11e453[_0x5ef470('0x1e')],_0x585426[_0x5ef470('0x38')](_0x2f42cd)[_0x5ef470('0x25')](_0x11e453);};}exports[_0x5939dd('0x2e')]=function(_0x2f6365,_0x5c5879){const _0x446529=_0x5939dd;let _0x1a5078={};const _0x46ff24={},_0x585b06={'count':0x0,'rows':[]},_0x4f8f0c=_['map'](db[_0x446529('0x48')]['rawAttributes'],function(_0x483e3){const _0x3848ba=_0x446529;return{'name':_0x483e3[_0x3848ba('0x19')],'type':_0x483e3[_0x3848ba('0x23')][_0x3848ba('0x40')]};});_0x46ff24['model']=_[_0x446529('0x31')](_0x4f8f0c,_0x446529('0x1e')),_0x46ff24['query']=_[_0x446529('0x54')](_0x2f6365[_0x446529('0x2f')]),_0x46ff24[_0x446529('0x4')]=_[_0x446529('0x18')](_0x46ff24[_0x446529('0x13')],_0x46ff24[_0x446529('0x2f')]),_0x1a5078[_0x446529('0x1c')]=_[_0x446529('0x18')](_0x46ff24[_0x446529('0x13')],qs['fields'](_0x2f6365[_0x446529('0x2f')][_0x446529('0x33')])),_0x1a5078[_0x446529('0x1c')]=_0x1a5078[_0x446529('0x1c')][_0x446529('0x3')]?_0x1a5078[_0x446529('0x1c')]:_0x46ff24[_0x446529('0x13')];typeof _0x2f6365[_0x446529('0x2f')][_0x446529('0x4c')]==='undefined'&&(_0x1a5078[_0x446529('0x2d')]=qs[_0x446529('0x2d')](_0x2f6365[_0x446529('0x2f')][_0x446529('0x2d')]),_0x1a5078[_0x446529('0x3c')]=qs[_0x446529('0x3c')](_0x2f6365[_0x446529('0x2f')][_0x446529('0x3c')]));_0x1a5078[_0x446529('0x44')]=qs[_0x446529('0x2a')](_0x2f6365[_0x446529('0x2f')][_0x446529('0x2a')]),_0x1a5078[_0x446529('0x1')]=qs[_0x446529('0x4')](_[_0x446529('0x10')](_0x2f6365[_0x446529('0x2f')],_0x46ff24[_0x446529('0x4')]),_0x4f8f0c);_0x2f6365[_0x446529('0x2f')]['filter']&&(_0x1a5078['where']=_['merge'](_0x1a5078[_0x446529('0x1')],{[Op['or']]:_[_0x446529('0x31')](_0x4f8f0c,function(_0x125d92){const _0x3ff1ae=_0x446529;if(_0x125d92[_0x3ff1ae('0x23')]!==_0x3ff1ae('0x43')){const _0x550855={};return _0x550855[_0x125d92[_0x3ff1ae('0x1e')]]={[Op['like']]:'%'+_0x2f6365['query'][_0x3ff1ae('0x46')]+'%'},_0x550855;}})}));_0x1a5078=_[_0x446529('0x4e')]({},_0x1a5078,_0x2f6365[_0x446529('0x39')]);const _0x131bc2={'where':_0x1a5078[_0x446529('0x1')]};return db['Trunk'][_0x446529('0x51')](_0x131bc2)[_0x446529('0x2c')](function(_0x29acf8){const _0x3521fe=_0x446529;return _0x585b06[_0x3521fe('0x51')]=_0x29acf8,_0x2f6365[_0x3521fe('0x2f')][_0x3521fe('0x1a')]&&(_0x1a5078[_0x3521fe('0x17')]=[{'all':!![]}]),db[_0x3521fe('0x48')][_0x3521fe('0x36')](_0x1a5078);})[_0x446529('0x2c')](function(_0x1545f3){return _0x585b06['rows']=_0x1545f3,_0x585b06;})['then'](respondWithFilteredResult(_0x5c5879,_0x1a5078))['catch'](handleError(_0x5c5879,null));},exports[_0x5939dd('0x24')]=function(_0x4a84ab,_0x47cec1){const _0x4769f6=_0x5939dd;let _0x503e83={'raw':![],'where':{'id':_0x4a84ab[_0x4769f6('0x21')]['id']}};const _0x1854fb={};return _0x1854fb[_0x4769f6('0x13')]=_[_0x4769f6('0x54')](db[_0x4769f6('0x48')][_0x4769f6('0xb')]),_0x1854fb[_0x4769f6('0x2f')]=_[_0x4769f6('0x54')](_0x4a84ab[_0x4769f6('0x2f')]),_0x1854fb[_0x4769f6('0x4')]=_[_0x4769f6('0x18')](_0x1854fb[_0x4769f6('0x13')],_0x1854fb[_0x4769f6('0x2f')]),_0x503e83['attributes']=_[_0x4769f6('0x18')](_0x1854fb['model'],qs[_0x4769f6('0x33')](_0x4a84ab[_0x4769f6('0x2f')]['fields'])),_0x503e83[_0x4769f6('0x1c')]=_0x503e83['attributes'][_0x4769f6('0x3')]?_0x503e83['attributes']:_0x1854fb[_0x4769f6('0x13')],_0x4a84ab[_0x4769f6('0x2f')][_0x4769f6('0x1a')]&&(_0x503e83[_0x4769f6('0x17')]=[{'all':!![]}]),_0x503e83=_[_0x4769f6('0x4e')]({},_0x503e83,_0x4a84ab['options']),db['Trunk'][_0x4769f6('0x2b')](_0x503e83)[_0x4769f6('0x2c')](handleEntityNotFound(_0x47cec1,null))[_0x4769f6('0x2c')](respondWithResult(_0x47cec1,null))[_0x4769f6('0xe')](handleError(_0x47cec1,null));},exports[_0x5939dd('0x5')]=function(_0x284b37,_0x1a5930){const _0x4dea30=_0x5939dd;let _0x4e9c30={'where':{'id':_0x284b37[_0x4dea30('0x21')]['id']}};const _0x2565ab={};return _0x2565ab[_0x4dea30('0x13')]=_[_0x4dea30('0x54')](db[_0x4dea30('0x48')][_0x4dea30('0xb')]),_0x4e9c30['attributes']=_[_0x4dea30('0x18')](_0x2565ab[_0x4dea30('0x13')],qs[_0x4dea30('0x33')](_0x284b37[_0x4dea30('0x2f')][_0x4dea30('0x33')])),_0x4e9c30[_0x4dea30('0x1c')]=_0x4e9c30[_0x4dea30('0x1c')][_0x4dea30('0x3')]?_0x4e9c30['attributes']:_0x2565ab[_0x4dea30('0x13')],_0x284b37[_0x4dea30('0x2f')]['includeAll']&&(_0x4e9c30[_0x4dea30('0x17')]=[{'all':!![]}]),_0x4e9c30=_[_0x4dea30('0x4e')]({},_0x4e9c30,_0x284b37[_0x4dea30('0x39')]),db['Trunk'][_0x4dea30('0x2b')](_0x4e9c30)['then'](handleEntityNotFound(_0x1a5930,null))[_0x4dea30('0x2c')](function(_0xb70b5c){const _0x224224=_0x4dea30;if(_0xb70b5c){let _0x44c6c8=_0xb70b5c['get']({'plain':!![]});return _0x44c6c8=qs['omit'](_0x44c6c8,['id','createdAt',_0x224224('0x32')]),_0x284b37[_0x224224('0xa')]=_[_0x224224('0x50')](_0x284b37[_0x224224('0xa')],['id',_0x224224('0x41'),'updatedAt']),db[_0x224224('0x48')][_0x224224('0x15')](_['merge'](_0x44c6c8,_0x284b37[_0x224224('0xa')]),{'include':_0x284b37['query'][_0x224224('0x1a')]?[{'all':!![]}]:undefined})[_0x224224('0x2c')](function(_0x3857fc){const _0x2b389e=_0x224224,_0x4a0685=_0x284b37[_0x2b389e('0x12')]['get']({'plain':!![]});if(!_0x4a0685)throw new Error(_0x2b389e('0x7'));if(_0x4a0685[_0x2b389e('0x52')]===_0x2b389e('0x12')){const _0x382c01=_0x3857fc[_0x2b389e('0x14')]({'plain':!![]}),_0x1a31d0=_0x2b389e('0x29');return db[_0x2b389e('0x27')][_0x2b389e('0x2b')]({'where':{'name':_0x1a31d0,'userProfileId':_0x4a0685[_0x2b389e('0x28')]},'raw':!![]})[_0x2b389e('0x2c')](function(_0x87a436){const _0x2dfd2e=_0x2b389e;return _0x87a436&&_0x87a436[_0x2dfd2e('0x2')]===0x0?db['UserProfileResource'][_0x2dfd2e('0x15')]({'name':_0x382c01[_0x2dfd2e('0x1e')],'resourceId':_0x382c01['id'],'type':_0x87a436['name'],'sectionId':_0x87a436['id']},{})['then'](function(){return _0x3857fc;}):_0x3857fc;})[_0x2b389e('0xe')](function(_0x2b46ee){const _0x3a50de=_0x2b389e;logger[_0x3a50de('0x20')](_0x3a50de('0x8'),_0x2b46ee);throw _0x2b46ee;});}return _0x3857fc;});}})[_0x4dea30('0x2c')](respondWithResult(_0x1a5930,0xc9))[_0x4dea30('0xe')](handleError(_0x1a5930,null));},exports[_0x5939dd('0x15')]=function(_0x2385bb,_0x38fb2f){const _0x326bb9=_0x5939dd;let _0x3eb722;return db[_0x326bb9('0x48')][_0x326bb9('0x15')](_0x2385bb[_0x326bb9('0xa')],{'raw':!![]})[_0x326bb9('0x2c')](function(_0x2211f7){const _0x33ed22=_0x326bb9;return _0x3eb722=_0x2211f7,db[_0x33ed22('0x48')][_0x33ed22('0x36')]({'raw':!![]});})['then'](function(_0x4bd1c0){const _0x4a5a5c=_0x326bb9,_0xba81cb=fs[_0x4a5a5c('0x1b')](path[_0x4a5a5c('0x49')](config[_0x4a5a5c('0x1f')],_0x4a5a5c('0x57')),_0x4a5a5c('0x35')),_0x5c103a=fs[_0x4a5a5c('0x1b')](path[_0x4a5a5c('0x49')](config[_0x4a5a5c('0x1f')],_0x4a5a5c('0x0')),'utf8'),_0x35561a=ejs[_0x4a5a5c('0x56')](_0xba81cb,{'trunks':_0x4bd1c0||[]});fs[_0x4a5a5c('0x3a')]('/etc/asterisk/sip_xcally_trunks.conf',_0x35561a);if(_0x3eb722[_0x4a5a5c('0xf')]){const _0x2a6372=ejs[_0x4a5a5c('0x56')](_0x5c103a,{'trunks':_0x4bd1c0||[]});fs[_0x4a5a5c('0x3a')]('/etc/asterisk/sip_xcally_registers.conf',_0x2a6372);}})['then'](function(){const _0x14a2e4=_0x326bb9;return respondWithRpcPromise(_0x14a2e4('0x3d'),_0x14a2e4('0x15'),{'module':_0x14a2e4('0x4b')});})[_0x326bb9('0x2c')](function(){const _0x1d0c5f=_0x326bb9,_0x2acb39=_0x2385bb[_0x1d0c5f('0x12')][_0x1d0c5f('0x14')]({'plain':!![]});if(!_0x2acb39)throw new Error(_0x1d0c5f('0x7'));if(_0x2acb39[_0x1d0c5f('0x52')]===_0x1d0c5f('0x12')){const _0x1742f7=_0x3eb722[_0x1d0c5f('0x14')]({'plain':!![]});return db[_0x1d0c5f('0x27')]['findOne']({'where':{'name':_0x1d0c5f('0x29'),'userProfileId':_0x2acb39[_0x1d0c5f('0x28')]},'raw':!![]})['then'](function(_0xb26141){const _0x12baf2=_0x1d0c5f;return _0xb26141&&_0xb26141['autoAssociation']===0x0?db[_0x12baf2('0x47')]['create']({'name':_0x1742f7[_0x12baf2('0x1e')],'resourceId':_0x1742f7['id'],'type':_0xb26141[_0x12baf2('0x1e')],'sectionId':_0xb26141['id']},{})[_0x12baf2('0x2c')](function(){return _0x3eb722;}):_0x3eb722;})[_0x1d0c5f('0xe')](function(_0x28d5cb){const _0x690d52=_0x1d0c5f;logger['error'](_0x690d52('0x8'),_0x28d5cb);throw _0x28d5cb;});}return _0x3eb722;})[_0x326bb9('0x2c')](respondWithResult(_0x38fb2f,0xc9))['catch'](handleError(_0x38fb2f,null));},exports[_0x5939dd('0x3e')]=function(_0x16bc93,_0x44ab67){const _0x2996a6=_0x5939dd;let _0x160f03;return db[_0x2996a6('0x48')][_0x2996a6('0x2b')]({'where':{'id':_0x16bc93['params']['id']}})[_0x2996a6('0x2c')](handleEntityNotFound(_0x44ab67,null))[_0x2996a6('0x2c')](saveUpdates(_0x16bc93[_0x2996a6('0xa')],null))[_0x2996a6('0x2c')](function(_0x5b0791){const _0x209d62=_0x2996a6;if(_0x5b0791)return _0x160f03=_0x5b0791,db[_0x209d62('0x48')][_0x209d62('0x36')]({'raw':!![]});})[_0x2996a6('0x2c')](function(_0x16f32a){const _0x2cf252=_0x2996a6;if(_0x16f32a){const _0x2e5e2d=fs['readFileSync'](path[_0x2cf252('0x49')](config[_0x2cf252('0x1f')],_0x2cf252('0x57')),_0x2cf252('0x35')),_0x8bd666=fs[_0x2cf252('0x1b')](path[_0x2cf252('0x49')](config['root'],'files/templates/registry.ejs'),_0x2cf252('0x35')),_0x2dd57c=ejs[_0x2cf252('0x56')](_0x2e5e2d,{'trunks':_0x16f32a||[]});fs['writeFileSync'](_0x2cf252('0x9'),_0x2dd57c);const _0x5da0d9=ejs['render'](_0x8bd666,{'trunks':_0x16f32a||[]});return fs[_0x2cf252('0x3a')](_0x2cf252('0x4a'),_0x5da0d9),respondWithRpcPromise(_0x2cf252('0x3d'),_0x2cf252('0x3e'),{'module':_0x2cf252('0x4b')});}return null;})[_0x2996a6('0x2c')](function(_0x36fa7e){if(_0x36fa7e)return _0x160f03;})[_0x2996a6('0x2c')](respondWithResult(_0x44ab67,null))[_0x2996a6('0xe')](handleError(_0x44ab67,null));},exports['destroy']=function(_0x2cfd8c,_0x5f391e){const _0x517f49=_0x5939dd;return db[_0x517f49('0x48')][_0x517f49('0x2b')]({'where':{'id':_0x2cfd8c['params']['id']}})[_0x517f49('0x2c')](handleEntityNotFound(_0x5f391e,null))[_0x517f49('0x2c')](removeEntity(_0x5f391e,null))[_0x517f49('0x2c')](function(){const _0x544590=_0x517f49;return db[_0x544590('0x48')][_0x544590('0x36')]({'raw':!![]});})['then'](function(_0x1f7faa){const _0x47da96=_0x517f49,_0x3f9257=fs[_0x47da96('0x1b')](path[_0x47da96('0x49')](config['root'],_0x47da96('0x57')),_0x47da96('0x35')),_0x1d7161=fs[_0x47da96('0x1b')](path['join'](config[_0x47da96('0x1f')],'files/templates/registry.ejs'),'utf8'),_0x530c52=ejs['render'](_0x3f9257,{'trunks':_0x1f7faa}),_0x53cde7=ejs[_0x47da96('0x56')](_0x1d7161,{'trunks':_0x1f7faa});return fs[_0x47da96('0x3a')](_0x47da96('0x9'),_0x530c52),fs[_0x47da96('0x3a')](_0x47da96('0x4a'),_0x53cde7),respondWithRpcPromise('Reload',_0x47da96('0x4d'),{'module':'chan_sip.so'});})[_0x517f49('0xe')](handleError(_0x5f391e,null));};