feat(produser): added the ability to add a prod user by default roles and update if already there
This commit is contained in:
13
database/migrations/0065_nappy_talos.sql
Normal file
13
database/migrations/0065_nappy_talos.sql
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
CREATE TABLE "prodPermissions" (
|
||||||
|
"prodPerm_id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
|
||||||
|
"name" text NOT NULL,
|
||||||
|
"description" text NOT NULL,
|
||||||
|
"roles" jsonb DEFAULT '[]'::jsonb,
|
||||||
|
"rolesLegacy" jsonb DEFAULT '[]'::jsonb,
|
||||||
|
"add_User" text DEFAULT 'LST_System' NOT NULL,
|
||||||
|
"add_Date" timestamp DEFAULT now(),
|
||||||
|
"upd_User" text DEFAULT 'LST_System' NOT NULL,
|
||||||
|
"upd_date" timestamp DEFAULT now()
|
||||||
|
);
|
||||||
|
--> statement-breakpoint
|
||||||
|
CREATE UNIQUE INDEX "prodPermName" ON "prodPermissions" USING btree ("name");
|
||||||
2113
database/migrations/meta/0065_snapshot.json
Normal file
2113
database/migrations/meta/0065_snapshot.json
Normal file
File diff suppressed because it is too large
Load Diff
@@ -456,6 +456,13 @@
|
|||||||
"when": 1748464203006,
|
"when": 1748464203006,
|
||||||
"tag": "0064_aberrant_blindfold",
|
"tag": "0064_aberrant_blindfold",
|
||||||
"breakpoints": true
|
"breakpoints": true
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"idx": 65,
|
||||||
|
"version": "7",
|
||||||
|
"when": 1749492130639,
|
||||||
|
"tag": "0065_nappy_talos",
|
||||||
|
"breakpoints": true
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
38
database/schema/prodPermissions.ts
Normal file
38
database/schema/prodPermissions.ts
Normal file
@@ -0,0 +1,38 @@
|
|||||||
|
import {
|
||||||
|
text,
|
||||||
|
pgTable,
|
||||||
|
timestamp,
|
||||||
|
uuid,
|
||||||
|
uniqueIndex,
|
||||||
|
jsonb,
|
||||||
|
} from "drizzle-orm/pg-core";
|
||||||
|
import { createInsertSchema, createSelectSchema } from "drizzle-zod";
|
||||||
|
import { z } from "zod";
|
||||||
|
|
||||||
|
export const prodPermissions = pgTable(
|
||||||
|
"prodPermissions",
|
||||||
|
{
|
||||||
|
prodPerm_id: uuid("prodPerm_id").defaultRandom().primaryKey(),
|
||||||
|
name: text("name").notNull(),
|
||||||
|
description: text("description").notNull(),
|
||||||
|
roles: jsonb("roles").default([]),
|
||||||
|
rolesLegacy: jsonb("rolesLegacy").default([]),
|
||||||
|
add_User: text("add_User").default("LST_System").notNull(),
|
||||||
|
add_Date: timestamp("add_Date").defaultNow(),
|
||||||
|
upd_user: text("upd_User").default("LST_System").notNull(),
|
||||||
|
upd_date: timestamp("upd_date").defaultNow(),
|
||||||
|
},
|
||||||
|
(table) => [
|
||||||
|
// uniqueIndex('emailUniqueIndex').on(sql`lower(${table.email})`),
|
||||||
|
uniqueIndex("prodPermName").on(table.name),
|
||||||
|
]
|
||||||
|
);
|
||||||
|
|
||||||
|
// Schema for inserting a user - can be used to validate API requests
|
||||||
|
// export const insertUsersSchema = createInsertSchema(prodPermissions, {
|
||||||
|
// name: z
|
||||||
|
// .string()
|
||||||
|
// .min(3, { message: "Role name must be longer than 3 characters" }),
|
||||||
|
// });
|
||||||
|
// Schema for selecting a Expenses - can be used to validate API responses
|
||||||
|
export const selectUsersSchema = createSelectSchema(prodPermissions);
|
||||||
@@ -28,6 +28,7 @@ import notify from "./services/notifications/notifyService.js";
|
|||||||
import eom from "./services/eom/eomService.js";
|
import eom from "./services/eom/eomService.js";
|
||||||
import dataMart from "./services/dataMart/dataMartService.js";
|
import dataMart from "./services/dataMart/dataMartService.js";
|
||||||
import qualityRequest from "./services/quality/qualityService.js";
|
import qualityRequest from "./services/quality/qualityService.js";
|
||||||
|
import produser from "./services/prodUser/prodUser.js";
|
||||||
|
|
||||||
// create the main prodlogin here
|
// create the main prodlogin here
|
||||||
const username = "lst_user";
|
const username = "lst_user";
|
||||||
@@ -106,6 +107,7 @@ const routes = [
|
|||||||
eom,
|
eom,
|
||||||
dataMart,
|
dataMart,
|
||||||
qualityRequest,
|
qualityRequest,
|
||||||
|
produser,
|
||||||
] as const;
|
] as const;
|
||||||
|
|
||||||
const appRoutes = routes.forEach((route) => {
|
const appRoutes = routes.forEach((route) => {
|
||||||
|
|||||||
1
server/services/prodUser/controller/addProdRole.ts
Normal file
1
server/services/prodUser/controller/addProdRole.ts
Normal file
@@ -0,0 +1 @@
|
|||||||
|
export const addProdRole = async (data: any) => {};
|
||||||
150
server/services/prodUser/controller/produser.ts
Normal file
150
server/services/prodUser/controller/produser.ts
Normal 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: [],
|
||||||
|
};
|
||||||
|
};
|
||||||
23
server/services/prodUser/prodUser.ts
Normal file
23
server/services/prodUser/prodUser.ts
Normal 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;
|
||||||
43
server/services/prodUser/routes/addProdRole.ts
Normal file
43
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.succes,
|
||||||
|
message: data.message,
|
||||||
|
data: data.data,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
);
|
||||||
|
export default app;
|
||||||
45
server/services/prodUser/routes/produser.ts
Normal file
45
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.succes,
|
||||||
|
message: data.message,
|
||||||
|
data: data.data,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
);
|
||||||
|
export default app;
|
||||||
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"
|
||||||
|
);
|
||||||
|
};
|
||||||
17
server/services/sqlServer/querys/prodUser/usercheck.ts
Normal file
17
server/services/sqlServer/querys/prodUser/usercheck.ts
Normal 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]'
|
||||||
|
`;
|
||||||
Reference in New Issue
Block a user