fix(v2): Remove custom hooks; fix(v1): Remove company from JWT context gen

This commit is contained in:
Josepablo C
2024-08-14 10:14:25 -06:00
parent 41ae6354fa
commit 55f155af03
6 changed files with 53 additions and 55 deletions

View File

@@ -12,7 +12,7 @@ async function middleware( req, res, next ){
user : await usersModel.findById( userID , { password : 0 , session_token : 0 , session_token_exp : 0 } ).populate('company') user : await usersModel.findById( userID , { password : 0 , session_token : 0 , session_token_exp : 0 } ).populate('company')
} }
req.context.userId = req.context.user.id; req.context.userId = req.context.user.id;
req.context.companyId = req.context.user.company.id || null; req.context.companyId = req.context.user.company?.id || null;
req.context.job_role = req.context.user.job_role || null; req.context.job_role = req.context.user.job_role || null;
req.context.permissions = req.context.user.permissions || null; req.context.permissions = req.context.user.permissions || null;
next(); next();

View File

@@ -135,7 +135,7 @@ module.exports = {
AppInit, AppInit,
hooks : { hooks : {
before: { before: {
/**Array of middleware functions*/ /**Array of middleware functions or objects lile { endpoint, middleware }*/
all : [], all : [],
del : [], del : [],
get : [], get : [],
@@ -145,17 +145,7 @@ module.exports = {
}, },
after: { after: {
/**Array of middleware functions*/ /**Array of middleware functions or objects lile { endpoint, middleware }*/
all : [],
del : [],
get : [],
patch : [],
post : [],
put : []
},
custom: {
/**Array of objects like { endpoint, middleware }*/
all : [ all : [
{ '/register': dummy_middleware } { '/register': dummy_middleware }
], ],
@@ -174,6 +164,6 @@ module.exports = {
{ '/recover' : post_recover } { '/recover' : post_recover }
], ],
put : [] put : []
} },
} }
}; };

View File

@@ -16,7 +16,7 @@ module.exports = {
AppInit, AppInit,
hooks : { hooks : {
before: { before: {
/**Array of middleware functions*/ /**Array of middleware functions or objects lile { endpoint, middleware }*/
all : [], all : [],
del : [], del : [],
get : [], get : [],
@@ -26,23 +26,13 @@ module.exports = {
}, },
after: { after: {
/**Array of middleware functions*/ /**Array of middleware functions or objects lile { endpoint, middleware }*/
all : [],
del : [],
get : [],
patch : [],
post : [],
put : []
},
custom: {
/**Array of objects like { endpoint, middleware }*/
all : [ { "/test" : dummy_middleware } ], all : [ { "/test" : dummy_middleware } ],
del : [], del : [],
get : [], get : [],
patch : [], patch : [],
post : [], post : [],
put : [] put : []
} },
} }
}; };

View File

@@ -9,13 +9,10 @@ class GenericController{
} }
init(){ init(){
const { before, after, custom } = this.hooks; const { before, after } = this.hooks;
if( before ){ if( before ){
this.populate_hooks( before ); this.populate_hooks( before );
} }
if( custom ){
this.populate_custom_hooks( custom );
}
if( after ){ if( after ){
this.populate_hooks( after ); this.populate_hooks( after );
} }
@@ -25,30 +22,31 @@ class GenericController{
populate_hooks( hooks ){ populate_hooks( hooks ){
if( (hooks.all) && (hooks.all.length > 0) ){ if( (hooks.all) && (hooks.all.length > 0) ){
this.router.use( hooks.all ); this.populate_hooks_by_verb( 'all' , hooks.all );
} }
if( (hooks.delete) && (hooks.delete.length > 0) ){ if( (hooks.delete) && (hooks.delete.length > 0) ){
this.router.delete( hooks.del ); this.populate_hooks_by_verb( 'del', hooks.del );
} }
if( (hooks.get) && (hooks.get.length > 0) ){ if( (hooks.get) && (hooks.get.length > 0) ){
this.router.get( hooks.get ); this.populate_hooks_by_verb( 'get', hooks.get );
} }
if( (hooks.patch) && (hooks.patch.length > 0) ){ if( (hooks.patch) && (hooks.patch.length > 0) ){
this.router.patch( hooks.patch ); this.populate_hooks_by_verb( 'patch', hooks.patch );
} }
if( (hooks.post) && (hooks.post.length > 0) ){ if( (hooks.post) && (hooks.post.length > 0) ){
this.router.post( hooks.post ); this.populate_hooks_by_verb( 'post', hooks.post );
} }
if( (hooks.put) && (hooks.put.length > 0) ){ if( (hooks.put) && (hooks.put.length > 0) ){
this.router.put( hooks.put ); this.populate_hooks_by_verb( 'put', hooks.put );
} }
} }
add_custom_hook( http_verb, endpoint, middleware ){ add_hook_with_endpoint( http_verb, endpoint, middleware ){
switch( http_verb ){ switch( http_verb ){
case 'all': case 'all':
this.router.use( endpoint , middleware ); this.router.use( endpoint , middleware );
@@ -73,16 +71,45 @@ class GenericController{
} }
} }
populate_custom_hooks( hooks ){ add_simple_hook( http_verb, middleware ){
for (let http_verb in hooks ){ switch( http_verb ){
for (let custom_hook of hooks[ http_verb ]){ case 'all':
this.router.use( middleware );
break;
case 'del':
this.router.delete( middleware );
break;
case 'get':
this.router.get( middleware );
break;
case 'patch':
this.router.patch( middleware );
break;
case 'post':
this.router.post( middleware );
break;
case 'put':
this.router.put( middleware );
break;
default:
return;
}
}
populate_hooks_by_verb( http_verb, hook_list ){
for (let custom_hook of hook_list ){
if( typeof custom_hook === "object" ){
for (let entry of Object.entries(custom_hook) ){ for (let entry of Object.entries(custom_hook) ){
const endpoint = entry[0]; const endpoint = entry[0];
const callback = entry[1]; const callback = entry[1];
this.add_custom_hook( http_verb, endpoint, callback ); /// Add this hook as an (endpoint, middleware function) pair
this.add_hook_with_endpoint( http_verb, endpoint, callback );
} }
}else{
/// Add this hook as a middleware function
this.add_simple_hook( http_verb, custom_hook );
} }
} }
} }
} }

View File

@@ -19,7 +19,7 @@ module.exports = {
AppInit : initEvents, /// Dummy App Init AppInit : initEvents, /// Dummy App Init
hooks : { hooks : {
before: { before: {
/**Array of middleware functions*/ /**Array of middleware functions or objects lile { endpoint, middleware }*/
all : [], all : [],
del : [], del : [],
get : [ test ], get : [ test ],
@@ -29,17 +29,7 @@ module.exports = {
}, },
after: { after: {
/**Array of middleware functions*/ /**Array of middleware functions or objects lile { endpoint, middleware }*/
all : [],
del : [],
get : [],
patch : [],
post : [],
put : []
},
custom: {
/**Array of objects like { endpoint, middleware }*/
all : [], all : [],
del : [], del : [],
get : [], get : [],

View File

@@ -42,6 +42,7 @@ class SystemServices {
async disconnect(){ async disconnect(){
this.knex.destroy(); this.knex.destroy();
console.log("SQL Disconnected");
this.SystemServiceState = OFFLINE; this.SystemServiceState = OFFLINE;
} }