44 lines
1.4 KiB
TypeScript
44 lines
1.4 KiB
TypeScript
/**
|
|
* 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 {createLog} 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});
|
|
createLog(
|
|
"info",
|
|
"lst",
|
|
"auth",
|
|
`${JSON.stringify(newRole)}, "Roles were just added due to missing them on server startup"`
|
|
);
|
|
} catch (error) {
|
|
createLog("error", "lst", "auth", "There was an error adding new roles to the db");
|
|
}
|
|
}
|
|
} catch (error) {
|
|
createLog("error", "lst", "auth", "There was an error getting or adding new roles");
|
|
}
|
|
};
|