feat(server): settings and module crud added in
This commit is contained in:
38
server/services/auth/utils/roleCheck.ts
Normal file
38
server/services/auth/utils/roleCheck.ts
Normal 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");
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user