4a07fe9f99595a54c2bb55984d9b183c16052248
[motion-next.git] / server / api / trunk / trunk.controller.js
1 const a643_0x1fbe=['keys','options','writeFileSync','server/files/templates/registry.ejs','findOne','rows','intersection','count','Trunk,\x20%s,\x20%s','offset','chan_sip.so','end','error','join','fields','json','Trunk','user','code','readFileSync','limit','Reload','path','include','order','Trunks','VIRTUAL','map','stack','key','like','api','server/files/templates/trunk.ejs','destroy','render','get','index','filter','createdAt','set','type','fieldName','/etc/asterisk/sip_xcally_registers.conf','http','sort','rawAttributes','query','request\x20sent','length','update','../../components/parsers/qs','client','model','Trunk,\x20%s,\x20%s,\x20%s','pick','role','includeAll','findAll','userProfileId','Unable\x20to\x20associate\x20the\x20resource\x20to\x20the\x20User\x20Profile:\x20%s','nolimit','undefined','root','show','body','Content-Range','name','info','message','utf8','stringify','status','jayson/promise','/etc/asterisk/sip_xcally_trunks.conf','Unable\x20to\x20retrieve\x20the\x20current\x20user','../../config/logger','omit','merge','catch','attributes','where','UserProfileSection','params','UserProfileResource','sequelize','../../config/environment','filters','sendStatus','../../mysqldb','autoAssociation','create','then'];(function(_0x2e80dd,_0x1fbe5d){const _0x49da9f=function(_0xa1bf49){while(--_0xa1bf49){_0x2e80dd['push'](_0x2e80dd['shift']());}};_0x49da9f(++_0x1fbe5d);}(a643_0x1fbe,0x153));const a643_0x49da=function(_0x2e80dd,_0x1fbe5d){_0x2e80dd=_0x2e80dd-0x0;let _0x49da9f=a643_0x1fbe[_0x2e80dd];return _0x49da9f;};const _0x2dffb5=a643_0x49da;'use strict';const path=require(_0x2dffb5('0x33')),ejs=require('ejs'),fs=require('fs'),_=require('lodash'),qs=require(_0x2dffb5('0x4f')),logger=require(_0x2dffb5('0xc'))(_0x2dffb5('0x3c')),config=require(_0x2dffb5('0x16')),db=require(_0x2dffb5('0x19'))['db'],{Op}=require(_0x2dffb5('0x15')),jayson=require(_0x2dffb5('0x9')),client=jayson[_0x2dffb5('0x50')][_0x2dffb5('0x48')]({'port':0x232a});function respondWithRpcPromise(_0xa1bf49,_0x336465,_0x1f94f3,_0x331445){return new Promise(function(_0x45bd81,_0x1a7f81){const _0x486d39=a643_0x49da,_0x363ffe=_0x331445||client;return _0x363ffe['request'](_0xa1bf49,_0x1f94f3)[_0x486d39('0x1c')](function(_0x749107){const _0x2a405b=_0x486d39;logger[_0x2a405b('0x4')](_0x2a405b('0x25'),_0x336465,'request\x20sent'),logger['debug'](_0x2a405b('0x52'),_0x336465,_0x2a405b('0x4c'),JSON[_0x2a405b('0x7')](_0x749107));if(_0x749107[_0x2a405b('0x29')]){if(_0x749107[_0x2a405b('0x29')][_0x2a405b('0x2f')]===0x1f4)return logger['error'](_0x2a405b('0x25'),_0x336465,_0x749107[_0x2a405b('0x29')]['message']),_0x1a7f81(_0x749107[_0x2a405b('0x29')][_0x2a405b('0x5')]);return logger[_0x2a405b('0x29')](_0x2a405b('0x25'),_0x336465,_0x749107[_0x2a405b('0x29')][_0x2a405b('0x5')]),_0x45bd81(_0x749107['error'][_0x2a405b('0x5')]);}else logger[_0x2a405b('0x4')](_0x2a405b('0x25'),_0x336465,_0x2a405b('0x4c')),_0x45bd81(_0x749107['result'][_0x2a405b('0x5')]);})['catch'](function(_0x2914a8){const _0xb1b35c=_0x486d39;logger[_0xb1b35c('0x29')](_0xb1b35c('0x25'),_0x336465,_0x2914a8),_0x1a7f81(_0x2914a8);});});}function respondWithResult(_0x5d8dac,_0x406b1e){return _0x406b1e=_0x406b1e||0xc8,function(_0x4b27d3){const _0x1715c7=a643_0x49da;if(_0x4b27d3)return _0x5d8dac['status'](_0x406b1e)[_0x1715c7('0x2c')](_0x4b27d3);};}function respondWithFilteredResult(_0x2064e1,_0x5e5528){return function(_0x3a278d){const _0x33b3d6=a643_0x49da;if(_0x3a278d){const _0x5908b2=_0x3a278d[_0x33b3d6('0x24')],_0x1c5cda=_0x5e5528['offset'];let _0x48a2c5=_0x5e5528[_0x33b3d6('0x26')]+_0x5e5528['limit'],_0x558825;return _0x48a2c5>=_0x5908b2?(_0x48a2c5=_0x5908b2,_0x558825=0xc8):_0x558825=0xce,_0x2064e1[_0x33b3d6('0x8')](_0x558825),_0x2064e1[_0x33b3d6('0x44')](_0x33b3d6('0x2'),_0x1c5cda+'-'+_0x48a2c5+'/'+_0x5908b2)[_0x33b3d6('0x2c')](_0x3a278d);}return null;};}function saveUpdates(_0x1663fe){return function(_0x38e5d1){if(_0x38e5d1)return _0x38e5d1['update'](_0x1663fe)['then'](function(_0x44441f){return _0x44441f;});return null;};}function removeEntity(_0x2ffd54){return function(_0x3f3bea){const _0x1abb58=a643_0x49da;if(_0x3f3bea)return _0x3f3bea[_0x1abb58('0x3e')]()[_0x1abb58('0x1c')](function(){const _0x494222=_0x1abb58,_0x43993c=_0x3f3bea['get']({'plain':!![]}),_0x152a99=_0x494222('0x36');return db['UserProfileResource'][_0x494222('0x3e')]({'where':{'type':_0x152a99,'resourceId':_0x43993c['id']}})[_0x494222('0x1c')](function(){return _0x3f3bea;});})[_0x1abb58('0x1c')](function(){const _0x68d306=_0x1abb58;_0x2ffd54[_0x68d306('0x8')](0xcc)[_0x68d306('0x28')]();});};}function handleEntityNotFound(_0x177bb2){return function(_0x499a29){const _0x5c29f7=a643_0x49da;return!_0x499a29&&_0x177bb2[_0x5c29f7('0x18')](0x194),_0x499a29;};}function handleError(_0x1431bf,_0x358a94){return _0x358a94=_0x358a94||0x1f4,function(_0x2def0d){const _0x3eb119=a643_0x49da;logger['error'](_0x2def0d[_0x3eb119('0x39')]),_0x2def0d[_0x3eb119('0x3')]&&delete _0x2def0d[_0x3eb119('0x3')],_0x1431bf[_0x3eb119('0x8')](_0x358a94)['send'](_0x2def0d);};}exports[_0x2dffb5('0x41')]=function(_0x25890b,_0xdada72){const _0x9bedba=_0x2dffb5;let _0x335948={};const _0x2ea5fc={},_0x140dbc={'count':0x0,'rows':[]},_0x21068f=_[_0x9bedba('0x38')](db[_0x9bedba('0x2d')]['rawAttributes'],function(_0x2a93fe){const _0x36127e=_0x9bedba;return{'name':_0x2a93fe[_0x36127e('0x46')],'type':_0x2a93fe['type'][_0x36127e('0x3a')]};});_0x2ea5fc[_0x9bedba('0x51')]=_[_0x9bedba('0x38')](_0x21068f,_0x9bedba('0x3')),_0x2ea5fc[_0x9bedba('0x4b')]=_['keys'](_0x25890b[_0x9bedba('0x4b')]),_0x2ea5fc[_0x9bedba('0x17')]=_['intersection'](_0x2ea5fc[_0x9bedba('0x51')],_0x2ea5fc[_0x9bedba('0x4b')]),_0x335948['attributes']=_[_0x9bedba('0x23')](_0x2ea5fc[_0x9bedba('0x51')],qs[_0x9bedba('0x2b')](_0x25890b[_0x9bedba('0x4b')][_0x9bedba('0x2b')])),_0x335948['attributes']=_0x335948[_0x9bedba('0x10')]['length']?_0x335948[_0x9bedba('0x10')]:_0x2ea5fc[_0x9bedba('0x51')];typeof _0x25890b[_0x9bedba('0x4b')][_0x9bedba('0x59')]===_0x9bedba('0x5a')&&(_0x335948['limit']=qs[_0x9bedba('0x31')](_0x25890b[_0x9bedba('0x4b')][_0x9bedba('0x31')]),_0x335948[_0x9bedba('0x26')]=qs[_0x9bedba('0x26')](_0x25890b[_0x9bedba('0x4b')][_0x9bedba('0x26')]));_0x335948[_0x9bedba('0x35')]=qs[_0x9bedba('0x49')](_0x25890b[_0x9bedba('0x4b')][_0x9bedba('0x49')]),_0x335948[_0x9bedba('0x11')]=qs['filters'](_[_0x9bedba('0x53')](_0x25890b['query'],_0x2ea5fc[_0x9bedba('0x17')]),_0x21068f);_0x25890b[_0x9bedba('0x4b')][_0x9bedba('0x42')]&&(_0x335948['where']=_['merge'](_0x335948['where'],{[Op['or']]:_[_0x9bedba('0x38')](_0x21068f,function(_0x301723){const _0x1bfc46=_0x9bedba;if(_0x301723[_0x1bfc46('0x45')]!==_0x1bfc46('0x37')){const _0x36f5cd={};return _0x36f5cd[_0x301723[_0x1bfc46('0x3')]]={[Op[_0x1bfc46('0x3b')]]:'%'+_0x25890b['query']['filter']+'%'},_0x36f5cd;}})}));_0x335948=_['merge']({},_0x335948,_0x25890b['options']);const _0x607fa9={'where':_0x335948['where']};return db[_0x9bedba('0x2d')][_0x9bedba('0x24')](_0x607fa9)['then'](function(_0x2f2fb3){const _0x956cfd=_0x9bedba;return _0x140dbc[_0x956cfd('0x24')]=_0x2f2fb3,_0x25890b[_0x956cfd('0x4b')][_0x956cfd('0x55')]&&(_0x335948[_0x956cfd('0x34')]=[{'all':!![]}]),db[_0x956cfd('0x2d')]['findAll'](_0x335948);})['then'](function(_0x323453){const _0x2d5b10=_0x9bedba;return _0x140dbc[_0x2d5b10('0x22')]=_0x323453,_0x140dbc;})[_0x9bedba('0x1c')](respondWithFilteredResult(_0xdada72,_0x335948))[_0x9bedba('0xf')](handleError(_0xdada72,null));},exports[_0x2dffb5('0x0')]=function(_0x254f10,_0x4ce320){const _0x8172ac=_0x2dffb5;let _0x348849={'raw':![],'where':{'id':_0x254f10[_0x8172ac('0x13')]['id']}};const _0xfeaebd={};return _0xfeaebd[_0x8172ac('0x51')]=_[_0x8172ac('0x1d')](db['Trunk'][_0x8172ac('0x4a')]),_0xfeaebd[_0x8172ac('0x4b')]=_[_0x8172ac('0x1d')](_0x254f10[_0x8172ac('0x4b')]),_0xfeaebd[_0x8172ac('0x17')]=_[_0x8172ac('0x23')](_0xfeaebd['model'],_0xfeaebd[_0x8172ac('0x4b')]),_0x348849[_0x8172ac('0x10')]=_['intersection'](_0xfeaebd[_0x8172ac('0x51')],qs[_0x8172ac('0x2b')](_0x254f10[_0x8172ac('0x4b')][_0x8172ac('0x2b')])),_0x348849[_0x8172ac('0x10')]=_0x348849[_0x8172ac('0x10')][_0x8172ac('0x4d')]?_0x348849[_0x8172ac('0x10')]:_0xfeaebd[_0x8172ac('0x51')],_0x254f10[_0x8172ac('0x4b')][_0x8172ac('0x55')]&&(_0x348849[_0x8172ac('0x34')]=[{'all':!![]}]),_0x348849=_[_0x8172ac('0xe')]({},_0x348849,_0x254f10[_0x8172ac('0x1e')]),db[_0x8172ac('0x2d')]['findOne'](_0x348849)[_0x8172ac('0x1c')](handleEntityNotFound(_0x4ce320,null))[_0x8172ac('0x1c')](respondWithResult(_0x4ce320,null))[_0x8172ac('0xf')](handleError(_0x4ce320,null));},exports['clone']=function(_0x3af645,_0x2c9863){const _0x428a88=_0x2dffb5;let _0x1573f0={'where':{'id':_0x3af645[_0x428a88('0x13')]['id']}};const _0x3a43b8={};return _0x3a43b8[_0x428a88('0x51')]=_['keys'](db[_0x428a88('0x2d')][_0x428a88('0x4a')]),_0x1573f0['attributes']=_[_0x428a88('0x23')](_0x3a43b8[_0x428a88('0x51')],qs[_0x428a88('0x2b')](_0x3af645['query'][_0x428a88('0x2b')])),_0x1573f0['attributes']=_0x1573f0[_0x428a88('0x10')][_0x428a88('0x4d')]?_0x1573f0[_0x428a88('0x10')]:_0x3a43b8['model'],_0x3af645[_0x428a88('0x4b')]['includeAll']&&(_0x1573f0[_0x428a88('0x34')]=[{'all':!![]}]),_0x1573f0=_['merge']({},_0x1573f0,_0x3af645[_0x428a88('0x1e')]),db['Trunk']['findOne'](_0x1573f0)['then'](handleEntityNotFound(_0x2c9863,null))[_0x428a88('0x1c')](function(_0x2de666){const _0x3b51c8=_0x428a88;if(_0x2de666){let _0x4a0a19=_0x2de666[_0x3b51c8('0x40')]({'plain':!![]});return _0x4a0a19=qs['omit'](_0x4a0a19,['id',_0x3b51c8('0x43'),'updatedAt']),_0x3af645[_0x3b51c8('0x1')]=_[_0x3b51c8('0xd')](_0x3af645['body'],['id',_0x3b51c8('0x43'),'updatedAt']),db['Trunk'][_0x3b51c8('0x1b')](_[_0x3b51c8('0xe')](_0x4a0a19,_0x3af645[_0x3b51c8('0x1')]),{'include':_0x3af645[_0x3b51c8('0x4b')][_0x3b51c8('0x55')]?[{'all':!![]}]:undefined})[_0x3b51c8('0x1c')](function(_0x268f88){const _0x5d5261=_0x3b51c8,_0x451654=_0x3af645[_0x5d5261('0x2e')][_0x5d5261('0x40')]({'plain':!![]});if(!_0x451654)throw new Error(_0x5d5261('0xb'));if(_0x451654[_0x5d5261('0x54')]==='user'){const _0x82c21d=_0x268f88[_0x5d5261('0x40')]({'plain':!![]}),_0x3e696e=_0x5d5261('0x36');return db[_0x5d5261('0x12')][_0x5d5261('0x21')]({'where':{'name':_0x3e696e,'userProfileId':_0x451654['userProfileId']},'raw':!![]})[_0x5d5261('0x1c')](function(_0x443823){const _0x509b52=_0x5d5261;return _0x443823&&_0x443823[_0x509b52('0x1a')]===0x0?db[_0x509b52('0x14')][_0x509b52('0x1b')]({'name':_0x82c21d[_0x509b52('0x3')],'resourceId':_0x82c21d['id'],'type':_0x443823[_0x509b52('0x3')],'sectionId':_0x443823['id']},{})['then'](function(){return _0x268f88;}):_0x268f88;})[_0x5d5261('0xf')](function(_0x2793fc){const _0x50c493=_0x5d5261;logger[_0x50c493('0x29')](_0x50c493('0x58'),_0x2793fc);throw _0x2793fc;});}return _0x268f88;});}})['then'](respondWithResult(_0x2c9863,0xc9))[_0x428a88('0xf')](handleError(_0x2c9863,null));},exports[_0x2dffb5('0x1b')]=function(_0x9cf4bc,_0xc87816){const _0xe4ae7d=_0x2dffb5;let _0x14059a;return db[_0xe4ae7d('0x2d')][_0xe4ae7d('0x1b')](_0x9cf4bc[_0xe4ae7d('0x1')],{'raw':!![]})[_0xe4ae7d('0x1c')](function(_0x38da85){const _0x3d391a=_0xe4ae7d;return _0x14059a=_0x38da85,db[_0x3d391a('0x2d')][_0x3d391a('0x56')]({'raw':!![]});})[_0xe4ae7d('0x1c')](function(_0x44e284){const _0x13af6e=_0xe4ae7d,_0x58ab9a=fs[_0x13af6e('0x30')](path[_0x13af6e('0x2a')](config[_0x13af6e('0x5b')],_0x13af6e('0x3d')),_0x13af6e('0x6')),_0x3ecd6e=fs[_0x13af6e('0x30')](path[_0x13af6e('0x2a')](config['root'],_0x13af6e('0x20')),_0x13af6e('0x6')),_0x29ccc7=ejs[_0x13af6e('0x3f')](_0x58ab9a,{'trunks':_0x44e284||[]});fs['writeFileSync'](_0x13af6e('0xa'),_0x29ccc7);if(_0x14059a['registry']){const _0x181433=ejs['render'](_0x3ecd6e,{'trunks':_0x44e284||[]});fs['writeFileSync']('/etc/asterisk/sip_xcally_registers.conf',_0x181433);}})[_0xe4ae7d('0x1c')](function(){const _0x51c617=_0xe4ae7d;return respondWithRpcPromise('Reload',_0x51c617('0x1b'),{'module':_0x51c617('0x27')});})[_0xe4ae7d('0x1c')](function(){const _0x366504=_0xe4ae7d,_0x54c678=_0x9cf4bc[_0x366504('0x2e')][_0x366504('0x40')]({'plain':!![]});if(!_0x54c678)throw new Error('Unable\x20to\x20retrieve\x20the\x20current\x20user');if(_0x54c678[_0x366504('0x54')]===_0x366504('0x2e')){const _0x72dff4=_0x14059a['get']({'plain':!![]});return db[_0x366504('0x12')][_0x366504('0x21')]({'where':{'name':_0x366504('0x36'),'userProfileId':_0x54c678[_0x366504('0x57')]},'raw':!![]})[_0x366504('0x1c')](function(_0x4f07f5){const _0x588501=_0x366504;return _0x4f07f5&&_0x4f07f5['autoAssociation']===0x0?db[_0x588501('0x14')][_0x588501('0x1b')]({'name':_0x72dff4[_0x588501('0x3')],'resourceId':_0x72dff4['id'],'type':_0x4f07f5[_0x588501('0x3')],'sectionId':_0x4f07f5['id']},{})['then'](function(){return _0x14059a;}):_0x14059a;})['catch'](function(_0x5c80ee){const _0x2d4610=_0x366504;logger[_0x2d4610('0x29')](_0x2d4610('0x58'),_0x5c80ee);throw _0x5c80ee;});}return _0x14059a;})[_0xe4ae7d('0x1c')](respondWithResult(_0xc87816,0xc9))[_0xe4ae7d('0xf')](handleError(_0xc87816,null));},exports['update']=function(_0x5b9c64,_0x1b4245){const _0x2cda95=_0x2dffb5;let _0x436c05;return db[_0x2cda95('0x2d')][_0x2cda95('0x21')]({'where':{'id':_0x5b9c64[_0x2cda95('0x13')]['id']}})[_0x2cda95('0x1c')](handleEntityNotFound(_0x1b4245,null))[_0x2cda95('0x1c')](saveUpdates(_0x5b9c64[_0x2cda95('0x1')],null))[_0x2cda95('0x1c')](function(_0x394585){const _0x1d28c1=_0x2cda95;if(_0x394585)return _0x436c05=_0x394585,db[_0x1d28c1('0x2d')][_0x1d28c1('0x56')]({'raw':!![]});})[_0x2cda95('0x1c')](function(_0x5ca732){const _0x1e5274=_0x2cda95;if(_0x5ca732){const _0x5669ad=fs[_0x1e5274('0x30')](path[_0x1e5274('0x2a')](config['root'],'server/files/templates/trunk.ejs'),'utf8'),_0x490410=fs[_0x1e5274('0x30')](path[_0x1e5274('0x2a')](config['root'],_0x1e5274('0x20')),'utf8'),_0x3d47e1=ejs['render'](_0x5669ad,{'trunks':_0x5ca732||[]});fs['writeFileSync'](_0x1e5274('0xa'),_0x3d47e1);const _0x125f6e=ejs[_0x1e5274('0x3f')](_0x490410,{'trunks':_0x5ca732||[]});return fs[_0x1e5274('0x1f')](_0x1e5274('0x47'),_0x125f6e),respondWithRpcPromise(_0x1e5274('0x32'),_0x1e5274('0x4e'),{'module':_0x1e5274('0x27')});}return null;})[_0x2cda95('0x1c')](function(_0x2cb4d7){if(_0x2cb4d7)return _0x436c05;})[_0x2cda95('0x1c')](respondWithResult(_0x1b4245,null))[_0x2cda95('0xf')](handleError(_0x1b4245,null));},exports[_0x2dffb5('0x3e')]=function(_0x332582,_0x1d655d){const _0x530ffc=_0x2dffb5;return db[_0x530ffc('0x2d')][_0x530ffc('0x21')]({'where':{'id':_0x332582[_0x530ffc('0x13')]['id']}})['then'](handleEntityNotFound(_0x1d655d,null))[_0x530ffc('0x1c')](removeEntity(_0x1d655d,null))['then'](function(){return db['Trunk']['findAll']({'raw':!![]});})[_0x530ffc('0x1c')](function(_0x458e86){const _0x115764=_0x530ffc,_0x36fa8d=fs[_0x115764('0x30')](path[_0x115764('0x2a')](config[_0x115764('0x5b')],_0x115764('0x3d')),_0x115764('0x6')),_0x38e8cb=fs[_0x115764('0x30')](path[_0x115764('0x2a')](config[_0x115764('0x5b')],_0x115764('0x20')),'utf8'),_0x545cd0=ejs['render'](_0x36fa8d,{'trunks':_0x458e86}),_0x5c723a=ejs[_0x115764('0x3f')](_0x38e8cb,{'trunks':_0x458e86});return fs[_0x115764('0x1f')](_0x115764('0xa'),_0x545cd0),fs[_0x115764('0x1f')](_0x115764('0x47'),_0x5c723a),respondWithRpcPromise(_0x115764('0x32'),_0x115764('0x3e'),{'module':_0x115764('0x27')});})[_0x530ffc('0xf')](handleError(_0x1d655d,null));};