feat(server): settings and module crud added in

This commit is contained in:
2025-03-04 16:43:45 -06:00
parent 5b9cadb76e
commit 2ad1dcc55b
19 changed files with 701 additions and 14 deletions

View File

@@ -7,18 +7,22 @@ import session from "./routes/session.js";
import getAccess from "./routes/userRoles/getUserRoles.js";
import setAccess from "./routes/userRoles/setUserRoles.js";
import profile from "./routes/user/profileUpdate.js";
import {areRolesIn} from "./utils/roleCheck.js";
const app = new OpenAPIHono();
// run the role check
areRolesIn();
app.route("auth/login", login);
app.route("auth/register", register);
app.route("auth/session", session);
// required to login
/* User area just needs to be logged in to enter here */
app.route("/auth/profileUpdate", profile);
app.route("auth/profileUpdate", profile);
/* will need to increase to make sure the person coming here has the correct permissions */
app.route("/auth/getuseraccess", getAccess);
app.route("/auth/setuseraccess", setAccess);
app.route("auth/getuseraccess", getAccess);
app.route("auth/setuseraccess", setAccess);
export default app;

View File

@@ -12,7 +12,15 @@ export const roleCheck = async (user_id: string | undefined) => {
throw Error("Missing user_id");
}
// get the user roles by the user_id
const roles = await db.select().from(userRoles).where(eq(userRoles.user_id, user_id));
const roles = await db
.select({
user_id: userRoles.user_id,
role_id: userRoles.role_id,
module_id: userRoles.module_id,
role: userRoles.role,
})
.from(userRoles)
.where(eq(userRoles.user_id, user_id));
return roles;
};

View File

@@ -0,0 +1,38 @@
/**
* check if the roles are in and if not add them.
* this will only run on a server start up
*/
import {db} from "../../../../database/dbclient.js";
import {roles} from "../../../../database/schema/roles.js";
import {log} from "../../logger/logger.js";
// "view", "technician", "supervisor","manager", "admin", "systemAdmin"
const newRoles = [
{name: "viewer"},
{name: "technician"},
{name: "supervisor"},
{name: "manager"},
{name: "admin"},
{name: "systemAdmin"},
];
export const areRolesIn = async () => {
// get the roles
try {
const roleCheck = await db.select().from(roles);
if (roleCheck.length !== newRoles.length) {
try {
const newRole = await db
.insert(roles)
.values(newRoles)
.onConflictDoNothing() // this will only update the ones that are new :D
.returning({name: roles.name});
log.info(newRole, "Roles were just added due to missing them on server startup");
} catch (error) {
log.error(error, "There was an error adding new roles to the db");
}
}
} catch (error) {
log.error(error, "There was an error getting or adding new roles");
}
};