feat(produser): added the ability to add a prod user by default roles and update if already there
This commit is contained in:
53
server/services/prodUser/utils/prodRoles.ts
Normal file
53
server/services/prodUser/utils/prodRoles.ts
Normal file
@@ -0,0 +1,53 @@
|
||||
/**
|
||||
* 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: ["OperationsPlanning\\ProductionScheduling\\ProductionViewer"],
|
||||
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"
|
||||
);
|
||||
};
|
||||
Reference in New Issue
Block a user