test(auth): testing auth stuff
This commit is contained in:
42
server/services/server/controller/module/updateSubModule.ts
Normal file
42
server/services/server/controller/module/updateSubModule.ts
Normal file
@@ -0,0 +1,42 @@
|
|||||||
|
import { eq } from "drizzle-orm";
|
||||||
|
import { db } from "../../../../../database/dbclient.js";
|
||||||
|
import { createLog } from "../../../logger/logger.js";
|
||||||
|
import { subModules } from "../../../../../database/schema/subModules.js";
|
||||||
|
|
||||||
|
type Data = {
|
||||||
|
active: boolean;
|
||||||
|
};
|
||||||
|
export const updateSubModule = async (data: Data, subModuleID: string) => {
|
||||||
|
createLog("info", "lst", "server", "Module being updated");
|
||||||
|
let module;
|
||||||
|
|
||||||
|
console.log(data);
|
||||||
|
|
||||||
|
if (typeof data.active !== "boolean") {
|
||||||
|
createLog(
|
||||||
|
"error",
|
||||||
|
"lst",
|
||||||
|
"server",
|
||||||
|
"Invalid data type: 'active' must be a boolean"
|
||||||
|
);
|
||||||
|
throw new Error("'active' must be a boolean");
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
module = await db
|
||||||
|
.update(subModules)
|
||||||
|
.set({ active: data.active })
|
||||||
|
.where(eq(subModules.submodule_id, subModuleID))
|
||||||
|
.returning({ name: subModules.name });
|
||||||
|
//.where(sql`${userRole} = ANY(roles)`);
|
||||||
|
} catch (error) {
|
||||||
|
createLog(
|
||||||
|
"error",
|
||||||
|
"lst",
|
||||||
|
"server",
|
||||||
|
"There was an error updating the module"
|
||||||
|
);
|
||||||
|
throw new Error("There was an error updating the module");
|
||||||
|
}
|
||||||
|
return module;
|
||||||
|
};
|
||||||
134
server/services/server/route/modules/updateSubModules.ts
Normal file
134
server/services/server/route/modules/updateSubModules.ts
Normal file
@@ -0,0 +1,134 @@
|
|||||||
|
import { z, createRoute, OpenAPIHono } from "@hono/zod-openapi";
|
||||||
|
import type { User } from "../../../../types/users.js";
|
||||||
|
import { verify } from "hono/jwt";
|
||||||
|
|
||||||
|
import { authMiddleware } from "../../../auth/middleware/authMiddleware.js";
|
||||||
|
import { updateSubModule } from "../../controller/module/updateSubModule.js";
|
||||||
|
|
||||||
|
// Define the response schema
|
||||||
|
const responseSchema = z.object({
|
||||||
|
message: z.string().optional(),
|
||||||
|
module_id: z
|
||||||
|
.string()
|
||||||
|
.openapi({ example: "6c922c6c-7de3-4ec4-acb0-f068abdc" })
|
||||||
|
.optional(),
|
||||||
|
name: z.string().openapi({ example: "Production" }).optional(),
|
||||||
|
active: z.boolean().openapi({ example: true }).optional(),
|
||||||
|
roles: z
|
||||||
|
.string()
|
||||||
|
.openapi({ example: `["viewer","technician"]` })
|
||||||
|
.optional(),
|
||||||
|
});
|
||||||
|
|
||||||
|
const app = new OpenAPIHono();
|
||||||
|
|
||||||
|
const ParamsSchema = z.object({
|
||||||
|
id: z
|
||||||
|
.string()
|
||||||
|
.min(3)
|
||||||
|
.openapi({
|
||||||
|
param: {
|
||||||
|
name: "id",
|
||||||
|
in: "path",
|
||||||
|
},
|
||||||
|
example: "1212121",
|
||||||
|
}),
|
||||||
|
});
|
||||||
|
|
||||||
|
app.openapi(
|
||||||
|
createRoute({
|
||||||
|
tags: ["server"],
|
||||||
|
summary: "Updates submodule",
|
||||||
|
method: "patch",
|
||||||
|
path: "/submodules/{id}",
|
||||||
|
middleware: authMiddleware,
|
||||||
|
request: {
|
||||||
|
params: ParamsSchema,
|
||||||
|
},
|
||||||
|
responses: {
|
||||||
|
200: {
|
||||||
|
content: {
|
||||||
|
"application/json": { schema: responseSchema },
|
||||||
|
},
|
||||||
|
description: "Response message",
|
||||||
|
},
|
||||||
|
400: {
|
||||||
|
content: {
|
||||||
|
"application/json": {
|
||||||
|
schema: z.object({
|
||||||
|
message: z
|
||||||
|
.string()
|
||||||
|
.optional()
|
||||||
|
.openapi({ example: "Internal Server error" }),
|
||||||
|
}),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
description: "Internal Server Error",
|
||||||
|
},
|
||||||
|
401: {
|
||||||
|
content: {
|
||||||
|
"application/json": {
|
||||||
|
schema: z.object({
|
||||||
|
message: z
|
||||||
|
.string()
|
||||||
|
.optional()
|
||||||
|
.openapi({ example: "Unauthenticated" }),
|
||||||
|
}),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
description: "Unauthorized",
|
||||||
|
},
|
||||||
|
500: {
|
||||||
|
content: {
|
||||||
|
"application/json": {
|
||||||
|
schema: z.object({
|
||||||
|
message: z
|
||||||
|
.string()
|
||||||
|
.optional()
|
||||||
|
.openapi({ example: "Internal Server error" }),
|
||||||
|
}),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
description: "Internal Server Error",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}),
|
||||||
|
async (c) => {
|
||||||
|
const { id } = c.req.valid("param");
|
||||||
|
|
||||||
|
// make sure we have a vaid user being accessed thats really logged in
|
||||||
|
const authHeader = c.req.header("Authorization");
|
||||||
|
|
||||||
|
const token = authHeader?.split("Bearer ")[1] || "";
|
||||||
|
let user: User;
|
||||||
|
|
||||||
|
try {
|
||||||
|
const payload = await verify(token, process.env.JWT_SECRET!);
|
||||||
|
user = payload.user as User;
|
||||||
|
} catch (error) {
|
||||||
|
return c.json({ message: "Unauthorized" }, 401);
|
||||||
|
}
|
||||||
|
|
||||||
|
// now pass all the data over to update the user info
|
||||||
|
try {
|
||||||
|
const data = await c?.req.json();
|
||||||
|
await updateSubModule(data, id ?? "");
|
||||||
|
return c.json({ success: true, message: "Module Updated" }, 200);
|
||||||
|
} catch (error) {
|
||||||
|
return c.json(
|
||||||
|
{
|
||||||
|
message: "Please make sure you are not missing your data.",
|
||||||
|
error,
|
||||||
|
},
|
||||||
|
400
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
return c.json({
|
||||||
|
message: `Module has been updated`,
|
||||||
|
data: id,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
export default app;
|
||||||
@@ -16,6 +16,7 @@ import { setPerms } from "./utils/testServerPerms.js";
|
|||||||
import serviceControl from "./route/servers/serverContorl.js";
|
import serviceControl from "./route/servers/serverContorl.js";
|
||||||
import { areSubModulesIn } from "./utils/subModuleCheck.js";
|
import { areSubModulesIn } from "./utils/subModuleCheck.js";
|
||||||
import getSubmodules from "./route/modules/getSubModules.js";
|
import getSubmodules from "./route/modules/getSubModules.js";
|
||||||
|
import updateSubModule from "./route/modules/updateSubModules.js";
|
||||||
|
|
||||||
// making sure all modules are in properly
|
// making sure all modules are in properly
|
||||||
setTimeout(async () => {
|
setTimeout(async () => {
|
||||||
@@ -33,6 +34,7 @@ const routes = [
|
|||||||
updateModule,
|
updateModule,
|
||||||
addModule,
|
addModule,
|
||||||
getSubmodules,
|
getSubmodules,
|
||||||
|
updateSubModule,
|
||||||
// settings
|
// settings
|
||||||
addSetting,
|
addSetting,
|
||||||
getSettings,
|
getSettings,
|
||||||
|
|||||||
@@ -207,6 +207,14 @@ const newSettings = [
|
|||||||
serviceBelowsTo: "ocp",
|
serviceBelowsTo: "ocp",
|
||||||
roleToChange: "admin",
|
roleToChange: "admin",
|
||||||
},
|
},
|
||||||
|
// temp settings can be deleted at a later date once that code is removed
|
||||||
|
{
|
||||||
|
name: "siloAdjMigrations",
|
||||||
|
value: `0`,
|
||||||
|
description: "Migrates the old silo adjustments to lst v2.",
|
||||||
|
serviceBelowsTo: "admin",
|
||||||
|
roleToChange: "admin",
|
||||||
|
},
|
||||||
];
|
];
|
||||||
export const areSettingsIn = async () => {
|
export const areSettingsIn = async () => {
|
||||||
// get the roles
|
// get the roles
|
||||||
|
|||||||
@@ -101,7 +101,14 @@ const newSubModules = [
|
|||||||
link: "/modules",
|
link: "/modules",
|
||||||
icon: "Settings",
|
icon: "Settings",
|
||||||
newWindow: false,
|
newWindow: false,
|
||||||
isActive: false,
|
isActive: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "Sub Modules",
|
||||||
|
link: "/subModules",
|
||||||
|
icon: "Settings",
|
||||||
|
newWindow: false,
|
||||||
|
isActive: true,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "Swagger",
|
name: "Swagger",
|
||||||
|
|||||||
Reference in New Issue
Block a user