Files
lstV2/server/services/prodUser/utils/prodRoles.ts

54 lines
1.8 KiB
TypeScript

/**
* check if the modules are in and if not add them.
* this will only run on a server start up
*/
import { sql } from "drizzle-orm";
import { db } from "../../../../database/dbclient.js";
import { prodPermissions } from "../../../../database/schema/prodPermissions.js";
import { createLog } from "../../logger/logger.js";
// "view", "technician", "supervisor","manager", "admin", "systemAdmin"
const newProdRoles: any = [
{
name: "planning",
description: "Planning viewer only",
roles: ["Manufacturing\\IssueMaterial\\MaterialHandler"],
rolesLegacy: [3],
},
];
export const prodRoles = async () => {
// get the roles
for (let i = 0; i < newProdRoles.length; i++) {
try {
const newRole = await db
.insert(prodPermissions)
.values(newProdRoles[i])
.onConflictDoUpdate({
target: prodPermissions.name,
set: {
name: newProdRoles[i].name,
description: newProdRoles[i].description,
roles: newProdRoles[i].roles,
rolesLegacy: newProdRoles[i].rolesLegacy,
upd_date: sql`NOW()`,
},
}) // this will only update the ones that are new :D
.returning({ name: prodPermissions.name });
} catch (error) {
console.log(error);
createLog(
"error",
"lst",
"server",
"There was an error adding new modules to the db"
);
}
}
createLog(
"info",
"lst",
"server",
"Modules were just added due to missing them on server startup"
);
};