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,95 @@
import {createRoute, OpenAPIHono, z} from "@hono/zod-openapi";
import {addSetting} from "../../controller/settings/addSetting.js";
import {log} from "../../../logger/logger.js";
import {verify} from "hono/jwt";
import type {User} from "../../../../types/users.js";
import {authMiddleware} from "../../../auth/middleware/authMiddleware.js";
const app = new OpenAPIHono();
const AddSetting = z.object({
name: z.string().openapi({example: "server"}),
value: z.string().openapi({example: "localhost"}),
description: z.string().openapi({example: "The server we are going to connect to"}),
roles: z.string().openapi({example: "admin"}),
module: z.string().openapi({example: "production"}),
});
app.openapi(
createRoute({
tags: ["server"],
summary: "Add Setting",
method: "post",
path: "/settings",
middleware: authMiddleware,
request: {
body: {
content: {
"application/json": {schema: AddSetting},
},
},
},
responses: {
200: {
content: {
"application/json": {
schema: z.object({
success: z.boolean().openapi({example: true}),
message: z.string().openapi({example: "Starter"}),
}),
},
},
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) => {
// 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 addSetting(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;