feat(produser): added the ability to add a prod user by default roles and update if already there

This commit is contained in:
2025-06-09 16:42:49 -05:00
parent ed44b11e5c
commit ad4c9502fa
12 changed files with 2505 additions and 0 deletions

View File

@@ -0,0 +1 @@
export const addProdRole = async (data: any) => {};

View File

@@ -0,0 +1,150 @@
import axios from "axios";
import { db } from "../../../../database/dbclient.js";
import { prodPermissions } from "../../../../database/schema/prodPermissions.js";
import { tryCatch } from "../../../globalUtils/tryCatch.js";
import { query } from "../../sqlServer/prodSqlServer.js";
import { userCheck } from "../../sqlServer/querys/prodUser/usercheck.js";
import { prodEndpointCreation } from "../../../globalUtils/createUrl.js";
export const prodUser = async (data: any) => {
// get the prodPermissions so we can make sure we have one in here
const { data: prodPerm, error: pe } = await tryCatch(
db.select().from(prodPermissions)
);
// create url
const grantUrl = await prodEndpointCreation(
`/public/v1.0/Administration/User/${data.username}/Grant`
);
const newurl = await prodEndpointCreation(
`/public/v1.0/Administration/User`
);
const revoke = await prodEndpointCreation(
`/public/v1.0/Administration/User/${data.username}/Revoke`
);
if (pe) {
console.log(pe);
return {
succes: false,
message: "There was an error getting the base prod permissions",
data: pe,
};
}
// check if we sent over a valid permissions fole over
const permRoleCheck = prodPerm.filter((n: any) => n.name === data.role);
if (permRoleCheck.length === 0) {
return {
succes: false,
message: `Role: ${data.role}, dose note exist please check the role you have selected and try again.`,
data: [],
};
}
// dose this user already exist?
const quc = userCheck.replace("[userName]", data.username);
const { data: usercheck, error: userError } = (await tryCatch(
query(quc, "Checks for existing user")
)) as any;
if (userError) {
console.log(userError);
}
if (usercheck?.data.length === 0) {
// create the user
const newUser: any = {
userId: data.username,
remark: data.remark,
languageCode: "en",
active: true,
roles: permRoleCheck[0].roles,
rolesLegacy: permRoleCheck[0].rolesLegacy,
};
const { data: newU, error: newE } = await tryCatch(
axios.post(newurl, newUser, {
headers: {
Authorization: `Basic ${btoa(
`matthes01:99Monsters200Scary!`
)}`,
"Content-Type": "application/json",
},
})
);
if (newE) {
console.log(newE);
}
return {
succes: true,
message: `${data.username} was just created or updated.`,
data: [],
};
} else {
// revoke and readd
const revokePerms: any = {
roles:
JSON.parse(usercheck.data[0].roles.replaceAll("\\", "\\\\")) ||
[],
rolesLegacy: JSON.parse(usercheck.data[0].legacyRoles) || [],
};
const { data: newU, error: newE } = (await tryCatch(
axios.patch(revoke, revokePerms, {
headers: {
Authorization: `Basic ${btoa(
`matthes01:99Monsters200Scary!`
)}`,
"Content-Type": "application/json",
},
})
)) as any;
if (newE) {
console.log(newE.response.data);
return {
succes: false,
message: `${data.username} encountered an error updating..`,
data: newE.response.data,
};
}
// add the new roles to the user.
const grantRole: any = {
roles: permRoleCheck[0].roles,
rolesLegacy: permRoleCheck[0].rolesLegacy,
};
const { data: grant, error: grante } = (await tryCatch(
axios.patch(grantUrl, grantRole, {
headers: {
Authorization: `Basic ${btoa(
`matthes01:99Monsters200Scary!`
)}`,
"Content-Type": "application/json",
},
})
)) as any;
if (grante) {
console.log(newE.response.data);
return {
succes: false,
message: `${data.username} encountered an error updating..`,
data: newE.response.data,
};
}
}
return {
succes: true,
message: `${data.username} was just created or updated.`,
data: [],
};
};

View File

@@ -0,0 +1,23 @@
import { OpenAPIHono } from "@hono/zod-openapi";
import produser from "./routes/produser.js";
import createProdRole from "./routes/addProdRole.js";
import { prodRoles } from "./utils/prodRoles.js";
const app = new OpenAPIHono();
const routes = [produser, createProdRole] as const;
const appRoutes = routes.forEach((route) => {
app.route("/produser", route);
});
app.all("/produser/*", (c) => {
return c.json({
success: false,
message: "You have encounters a prodUser route that dose not exist.",
});
});
setTimeout(() => {
prodRoles();
}, 2000);
export default app;

View File

@@ -0,0 +1,43 @@
// an external way to creating logs
import { createRoute, OpenAPIHono, z } from "@hono/zod-openapi";
import { responses } from "../../../globalUtils/routeDefs/responses.js";
import { tryCatch } from "../../../globalUtils/tryCatch.js";
import { apiHit } from "../../../globalUtils/apiHits.js";
import { prodUser } from "../controller/produser.js";
const app = new OpenAPIHono({ strict: false });
app.openapi(
createRoute({
tags: ["admin"],
summary: "Creates a new prod role",
method: "post",
path: "/prodrole",
responses: responses(),
}),
async (c) => {
const { data: body, error: be } = await tryCatch(c.req.json());
if (be) {
return c.json({
success: false,
message: "Missing data.",
});
}
const { data, error } = await tryCatch(prodUser(body));
apiHit(c, { endpoint: "/prodrole" });
if (error) {
return c.json({
success: false,
message: "Error creating new role.",
});
}
return c.json({
success: data.succes,
message: data.message,
data: data.data,
});
}
);
export default app;

View File

@@ -0,0 +1,45 @@
// an external way to creating logs
import { createRoute, OpenAPIHono, z } from "@hono/zod-openapi";
import { responses } from "../../../globalUtils/routeDefs/responses.js";
import { tryCatch } from "../../../globalUtils/tryCatch.js";
import { apiHit } from "../../../globalUtils/apiHits.js";
import { prodUser } from "../controller/produser.js";
const app = new OpenAPIHono({ strict: false });
app.openapi(
createRoute({
tags: ["admin"],
summary:
"Runs a full crud on the user plus added icons if pc name provided and is online",
method: "post",
path: "/produser",
responses: responses(),
}),
async (c) => {
const { data: body, error: be } = await tryCatch(c.req.json());
if (be) {
return c.json({
success: false,
message: "Missing data.",
});
}
const { data, error } = await tryCatch(prodUser(body));
apiHit(c, { endpoint: "/newuser" });
if (error) {
console.log(error);
return c.json({
success: false,
message: "Error processing create user.",
});
}
return c.json({
success: data.succes,
message: data.message,
data: data.data,
});
}
);
export default app;

View 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"
);
};

View File

@@ -0,0 +1,17 @@
export const userCheck = `
SELECT *,
'[' + STUFF((
SELECT ',' + '"' + REPLACE(REPLACE(ur.name, '\', '\\'), '"', '\"') + '"'
FROM [test1_AlplaPROD2.0_Read].[user].[Roles] (nolock) ur
WHERE ur.userid = u.id
FOR XML PATH(''), TYPE).value('.', 'NVARCHAR(MAX)'
), 1, 1, '') + ']' AS roles,
'[' + STUFF((
SELECT ',' + cast(ulr.roleid as nvarchar(max))
FROM [test1_AlplaPROD2.0_Read].[user].[LegacyRoles] (nolock) ulr
WHERE ulr.userid = u.id
FOR XML PATH(''), TYPE).value('.', 'NVARCHAR(MAX)'
), 1, 1, '') + ']' AS legacyRoles
FROM [test1_AlplaPROD2.0_Read].[user].[User] (nolock) u
where Loginname = '[userName]'
`;