refactor(auth): added module and submodule access to the user

This commit is contained in:
2025-04-09 17:47:02 -05:00
parent ded074a1ce
commit efc630f5f8
10 changed files with 181 additions and 142 deletions

View File

@@ -1,24 +1,42 @@
import { db } from "../../../../../database/dbclient.js";
import { userRoles } from "../../../../../database/schema/userRoles.js";
import { users } from "../../../../../database/schema/users.js";
import { returnRes } from "../../../../globalUtils/routeDefs/returnRes.js";
import { tryCatch } from "../../../../globalUtils/tryCatch.js";
import { createLog } from "../../../logger/logger.js";
export const getAllUsers = async () => {
/**
* returns all users that are in lst
*/
createLog("info", "apiAuthedRoute", "auth", "Get all users");
const { data, error } = await tryCatch(db.select().from(users));
/**
* returns all users that are in lst
*/
createLog("info", "apiAuthedRoute", "auth", "Get all users");
if (error) {
returnRes(
false,
"There was an error getting users",
new Error("No user exists.")
);
}
// get all users
const { data, error } = await tryCatch(db.select().from(users));
returnRes(true, "All users.", data);
return { success: true, message: "All users", data };
// add there modules they are in
const { data: m, error: em } = await tryCatch(db.select().from(userRoles));
const user: any = data;
const userData = user.map((i: any) => {
// module in
const module = m?.filter((x: any) => x.user_id === i.user_id);
if (module) {
return { ...i, moduleRoles: module };
} else {
return;
}
});
if (error) {
returnRes(
false,
"There was an error getting users",
new Error("No user exists.")
);
}
//returnRes(true, "All users.", data);
return { success: true, message: "All users", data: userData };
};

View File

@@ -54,12 +54,18 @@ export const setUserAccess = async (
// set the user
try {
const userRole = await db.insert(userRoles).values({
user_id: user[0].user_id,
role_id: role[0].role_id,
module_id: module[0].module_id,
role: roleName,
});
const userRole = await db
.insert(userRoles)
.values({
user_id: user[0].user_id,
role_id: role[0].role_id,
module_id: module[0].module_id,
role: roleName,
})
.onConflictDoUpdate({
target: userRoles.user_id,
set: { role_id: role[0].role_id, role: roleName },
});
//.returning({user: users.username, email: users.email});
// return c.json({message: "User Registered", user}, 200);

View File

@@ -59,7 +59,7 @@ app.openapi(
{
success: access.success,
message: access.message,
data: access.data,
data: [], //access?.data,
},
200
);