feat(lstv2 move): moved lstv2 into this app to keep them combined and easier to maintain
This commit is contained in:
1
lstV2/server/services/prodUser/controller/addProdRole.ts
Normal file
1
lstV2/server/services/prodUser/controller/addProdRole.ts
Normal file
@@ -0,0 +1 @@
|
||||
export const addProdRole = async (data: any) => {};
|
||||
23
lstV2/server/services/prodUser/controller/getprodRoles.ts
Normal file
23
lstV2/server/services/prodUser/controller/getprodRoles.ts
Normal file
@@ -0,0 +1,23 @@
|
||||
import { db } from "../../../../database/dbclient.js";
|
||||
import { prodPermissions } from "../../../../database/schema/prodPermissions.js";
|
||||
import { tryCatch } from "../../../globalUtils/tryCatch.js";
|
||||
|
||||
export const getProdRoles = async () => {
|
||||
const { data, error } = (await tryCatch(
|
||||
db.select().from(prodPermissions)
|
||||
)) as any;
|
||||
|
||||
if (error) {
|
||||
return {
|
||||
success: false,
|
||||
message: "Error getting prod permissions",
|
||||
data: error,
|
||||
};
|
||||
}
|
||||
|
||||
return {
|
||||
success: true,
|
||||
message: "Current prod permissions",
|
||||
data: data,
|
||||
};
|
||||
};
|
||||
203
lstV2/server/services/prodUser/controller/produser.ts
Normal file
203
lstV2/server/services/prodUser/controller/produser.ts
Normal file
@@ -0,0 +1,203 @@
|
||||
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 {
|
||||
success: 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 {
|
||||
success: 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);
|
||||
}
|
||||
|
||||
//console.log(permRoleCheck);
|
||||
|
||||
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: {
|
||||
"X-API-Key": process.env.TEC_API_KEY || "",
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
})
|
||||
)) as any;
|
||||
|
||||
if (newE) {
|
||||
console.log(newE);
|
||||
return {
|
||||
success: false,
|
||||
message: `${data.username} encountered an error creating..`,
|
||||
data: newE.response.data,
|
||||
};
|
||||
}
|
||||
|
||||
return {
|
||||
success: 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("\\", "\\\\")
|
||||
) || ["Manufacturing\\IssueMaterial\\MaterialHandler"],
|
||||
rolesLegacy: JSON.parse(usercheck.data[0].legacyRoles) || [3],
|
||||
};
|
||||
|
||||
const { data: newU, error: newE } = (await tryCatch(
|
||||
axios.patch(revoke, revokePerms, {
|
||||
headers: {
|
||||
"X-API-Key": process.env.TEC_API_KEY || "",
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
})
|
||||
)) as any;
|
||||
|
||||
if (newE) {
|
||||
console.log("Revoke failed with: ", newE.response.data);
|
||||
|
||||
return {
|
||||
success: 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: {
|
||||
"X-API-Key": process.env.TEC_API_KEY || "",
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
})
|
||||
)) as any;
|
||||
|
||||
if (grante) {
|
||||
console.log(newE.response.data);
|
||||
return {
|
||||
success: false,
|
||||
message: `${data.username} encountered an error updating..`,
|
||||
data: newE.response.data,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
success: true,
|
||||
message: `${data.username} was just created or updated.`,
|
||||
data: [],
|
||||
};
|
||||
};
|
||||
|
||||
const deleteUser = async (data: any, permRoleCheck: any) => {
|
||||
const remove = await prodEndpointCreation(
|
||||
`/public/v1.0/Administration/User/${data.username}`
|
||||
);
|
||||
const newurl = await prodEndpointCreation(
|
||||
`/public/v1.0/Administration/User`
|
||||
);
|
||||
|
||||
const { data: removal, error: grante } = (await tryCatch(
|
||||
axios.delete(remove, {
|
||||
headers: {
|
||||
"X-API-Key": process.env.TEC_API_KEY || "",
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
})
|
||||
)) as any;
|
||||
|
||||
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: {
|
||||
"X-API-Key": process.env.TEC_API_KEY || "",
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
})
|
||||
)) as any;
|
||||
|
||||
if (newE) {
|
||||
console.log(newE);
|
||||
return {
|
||||
success: false,
|
||||
message: `${data.username} encountered an error creating..`,
|
||||
data: newE.response.data,
|
||||
};
|
||||
}
|
||||
|
||||
return {
|
||||
success: true,
|
||||
message: `${data.username} was just created.`,
|
||||
data: [],
|
||||
};
|
||||
};
|
||||
24
lstV2/server/services/prodUser/prodUser.ts
Normal file
24
lstV2/server/services/prodUser/prodUser.ts
Normal file
@@ -0,0 +1,24 @@
|
||||
import { OpenAPIHono } from "@hono/zod-openapi";
|
||||
import produser from "./routes/produser.js";
|
||||
import createProdRole from "./routes/addProdRole.js";
|
||||
import getRoles from "./routes/getProdRoles.js";
|
||||
import { prodRoles } from "./utils/prodRoles.js";
|
||||
|
||||
const app = new OpenAPIHono();
|
||||
const routes = [produser, createProdRole, getRoles] 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;
|
||||
43
lstV2/server/services/prodUser/routes/addProdRole.ts
Normal file
43
lstV2/server/services/prodUser/routes/addProdRole.ts
Normal 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.success,
|
||||
message: data.message,
|
||||
data: data.data,
|
||||
});
|
||||
}
|
||||
);
|
||||
export default app;
|
||||
36
lstV2/server/services/prodUser/routes/getProdRoles.ts
Normal file
36
lstV2/server/services/prodUser/routes/getProdRoles.ts
Normal file
@@ -0,0 +1,36 @@
|
||||
// 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";
|
||||
import { getProdRoles } from "../controller/getprodRoles.js";
|
||||
|
||||
const app = new OpenAPIHono({ strict: false });
|
||||
|
||||
app.openapi(
|
||||
createRoute({
|
||||
tags: ["admin"],
|
||||
summary: "Returns the current prod roles",
|
||||
method: "get",
|
||||
path: "/prodrole",
|
||||
responses: responses(),
|
||||
}),
|
||||
async (c) => {
|
||||
const { data, error } = await tryCatch(getProdRoles());
|
||||
apiHit(c, { endpoint: "/prodrole" });
|
||||
if (error) {
|
||||
return c.json({
|
||||
success: false,
|
||||
message: "Error getting new role.",
|
||||
});
|
||||
}
|
||||
|
||||
return c.json({
|
||||
success: data.success,
|
||||
message: data.message,
|
||||
data: data.data,
|
||||
});
|
||||
}
|
||||
);
|
||||
export default app;
|
||||
45
lstV2/server/services/prodUser/routes/produser.ts
Normal file
45
lstV2/server/services/prodUser/routes/produser.ts
Normal 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.success,
|
||||
message: data.message,
|
||||
data: data.data,
|
||||
});
|
||||
}
|
||||
);
|
||||
export default app;
|
||||
140
lstV2/server/services/prodUser/utils/prodRoles.ts
Normal file
140
lstV2/server/services/prodUser/utils/prodRoles.ts
Normal file
@@ -0,0 +1,140 @@
|
||||
/**
|
||||
* 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],
|
||||
},
|
||||
// production
|
||||
{
|
||||
name: "prodsupervisor",
|
||||
description:
|
||||
"Production supervisor, planning, labeling, and production contorlling.",
|
||||
roles: [
|
||||
"Manufacturing\\IssueMaterial\\MaterialHandler",
|
||||
"Manufacturing\\IssueMaterial\\ProcessAdmin",
|
||||
"Manufacturing\\ProductionLabelling\\ProcessAdmin",
|
||||
],
|
||||
rolesLegacy: [3, 11, 41, 48, 49],
|
||||
},
|
||||
// quality
|
||||
{
|
||||
name: "qualityTechB",
|
||||
description:
|
||||
"Quality tech with blocking, planning, stock warehouse leader, waste, quality waste.",
|
||||
roles: ["Quality\\Blocking\\ProcessAdmin"],
|
||||
rolesLegacy: [3, 14, 45],
|
||||
},
|
||||
{
|
||||
name: "qualityManager",
|
||||
description:
|
||||
"Quality manager with cch, blocking, planning, stock warehouse leader, waste, quality waste.",
|
||||
roles: [
|
||||
"Manufacturing\\ProductionControlling\\ProductionController",
|
||||
"Quality\\ComplaintHandling\\ProcessAdmin",
|
||||
"Quality\\Blocking\\ProcessAdmin",
|
||||
"Manufacturing\\IssueMaterial\\ProcessAdmin",
|
||||
"Manufacturing\\ProductionLabelling\\ProcessAdmin",
|
||||
],
|
||||
rolesLegacy: [105, 3, 14, 45],
|
||||
},
|
||||
// logistics
|
||||
|
||||
{
|
||||
name: "planner",
|
||||
description: "Planning role.",
|
||||
roles: [
|
||||
"Administration\\Scan\\ApiConsumer",
|
||||
"Administration\\Printing\\ApiConsumer",
|
||||
"Logistics\\Warehousing\\ProcessAdmin",
|
||||
"Manufacturing\\IssueMaterial\\ProcessAdmin",
|
||||
"Manufacturing\\ProductionLabelling\\ProcessAdmin",
|
||||
"DemandManagement\\Forecast\\ProcessAdmin",
|
||||
"DemandManagement\\Order\\ProcessAdmin",
|
||||
],
|
||||
rolesLegacy: [55, 95, 15, 105, 145, 9],
|
||||
},
|
||||
// plant manager
|
||||
{
|
||||
name: "plantManager",
|
||||
description:
|
||||
"Quality tech with blocking, planning, stock warehouse leader, waste, quality waste.",
|
||||
roles: [
|
||||
"Quality\\ComplaintHandling\\ProcessAdmin",
|
||||
"Manufacturing\\ProductionControlling\\ProcessAdmin",
|
||||
"MasterData\\Manufacturing\\ProcessAdmin",
|
||||
"Quality\\Blocking\\ProcessAdmin",
|
||||
"Logistics\\InboundDeliveries\\ApiConsumer",
|
||||
"MasterData\\Logistics\\ProcessAdmin",
|
||||
"DemandManagement\\Order\\ProcessAdmin",
|
||||
"Manufacturing\\IssueMaterial\\ProcessAdmin",
|
||||
"Logistics\\Warehousing\\ProcessAdmin",
|
||||
"Logistics\\OutboundDeliveries\\ProcessAdmin",
|
||||
"DemandManagement\\Forecast\\ProcessAdmin",
|
||||
"Manufacturing\\ProductionLabelling\\ProcessAdmin",
|
||||
],
|
||||
rolesLegacy: [3, 55, 145, 95, 45, 105, 65, 15, 125],
|
||||
},
|
||||
// regional
|
||||
{
|
||||
name: "ctoRegional",
|
||||
description: "admin for everything except planning.",
|
||||
roles: [
|
||||
"Manufacturing\\IssueMaterial\\ProcessAdmin",
|
||||
"MasterData\\Logistics\\ProcessAdmin",
|
||||
"MasterData\\Manufacturing\\ProcessAdmin",
|
||||
"Manufacturing\\IssueMaterial\\MaterialHandler",
|
||||
"Manufacturing\\ProductionLabelling\\ProcessAdmin",
|
||||
"DemandManagement\\Order\\ProcessAdmin",
|
||||
"Logistics\\Warehousing\\ProcessAdmin",
|
||||
"Quality\\Blocking\\BlockingContributor",
|
||||
"Quality\\Blocking\\ProcessAdmin",
|
||||
],
|
||||
rolesLegacy: [65, 95, 105, 55, 135, 45, 15, 125, 3, 9, 145],
|
||||
},
|
||||
];
|
||||
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