feat(server): settings and module crud added in

This commit is contained in:
2025-03-04 16:43:45 -06:00
parent 5b9cadb76e
commit 2ad1dcc55b
19 changed files with 701 additions and 14 deletions

View File

@@ -0,0 +1,114 @@
import {z, createRoute, OpenAPIHono} from "@hono/zod-openapi";
import {modules} from "../../../../../database/schema/modules.js";
import {db} from "../../../../../database/dbclient.js";
import {log} from "../../../logger/logger.js";
import type {User} from "../../../../types/users.js";
import {verify} from "hono/jwt";
import {updateModule} from "../../controller/module/updateModule.js";
import {authMiddleware} from "../../../auth/middleware/authMiddleware.js";
import {addModule} from "../../controller/module/addModule.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",
// }),
// });
const AddModule = z.object({
name: z.string().openapi({example: "production"}),
});
app.openapi(
createRoute({
tags: ["server"],
summary: "Adds a new module",
method: "post",
path: "/modules",
middleware: authMiddleware,
request: {
body: {
content: {
"application/json": {schema: AddModule},
},
},
},
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) {
log.error(error, "Failed session check, user must be logged out");
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 addModule(data, user.user_id ?? "");
return c.json({success: true, message: "New setting was added"}, 200);
} catch (error) {
return c.json({message: "Please make sure you are not missing your data.", error}, 400);
}
}
);
export default app;

View File

@@ -0,0 +1,67 @@
import {z, createRoute, OpenAPIHono} from "@hono/zod-openapi";
import {modules} from "../../../../../database/schema/modules.js";
import {db} from "../../../../../database/dbclient.js";
import {log} from "../../../logger/logger.js";
// Define the request body schema
const requestSchema = z.object({
ip: z.string().optional(),
endpoint: z.string().optional(),
action: z.string().optional(),
stats: z.string().optional(),
});
// 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();
app.openapi(
createRoute({
tags: ["server"],
summary: "Returns all modules in the server",
method: "get",
path: "/modules",
responses: {
200: {
content: {
"application/json": {schema: responseSchema},
},
description: "Response message",
},
},
}),
async (c) => {
//console.log("system modules");
let module: any = [];
try {
module = await db.select().from(modules); // .where(eq(modules.active, true));
} catch (error) {
console.log(error);
module = [];
}
// parse the roles
const updateModules = module.map((m: any) => {
if (m.roles) {
return {...m, roles: m?.roles};
}
return m;
}); //JSON.parse(module[0]?.roles);
// Return response with the received data
return c.json({
message: `All active modules`,
data: module,
});
}
);
export default app;

View File

@@ -0,0 +1,110 @@
import {z, createRoute, OpenAPIHono} from "@hono/zod-openapi";
import {modules} from "../../../../../database/schema/modules.js";
import {db} from "../../../../../database/dbclient.js";
import {log} from "../../../logger/logger.js";
import type {User} from "../../../../types/users.js";
import {verify} from "hono/jwt";
import {updateModule} from "../../controller/module/updateModule.js";
import {authMiddleware} from "../../../auth/middleware/authMiddleware.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 module",
method: "patch",
path: "/modules/{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) {
log.error(error, "Failed session check, user must be logged out");
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 updateModule(data, id ?? "");
return c.json({success: true, message: "New setting was added"}, 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;