feat(lst): added update settings into the entire app

This commit is contained in:
2025-03-05 12:09:51 -06:00
parent 6fb615a743
commit 5fcadb9fc8
14 changed files with 601 additions and 29 deletions

View File

@@ -0,0 +1,37 @@
import {and, eq, sql} from "drizzle-orm";
import {db} from "../../../../../database/dbclient.js";
import {settings} from "../../../../../database/schema/settings.js";
import {log} from "../../../logger/logger.js";
import {userRoles} from "../../../../../database/schema/userRoles.js";
import {users} from "../../../../../database/schema/users.js";
export const updateSetting = async (data: any, user_id: string) => {
log.info(user_id, "Adding a new setting");
// make sure the user is a system admin before moving forward
const sysAdmin = await db
.select()
.from(userRoles)
.where(and(eq(userRoles.user_id, user_id), eq(userRoles.role, "systemAdmin")));
if (sysAdmin) {
log.info(`Setting ${data.name} is being updated`);
//get the username so we can update the correct field
const user = await db.select().from(users).where(eq(users.user_id, user_id));
try {
//const settingID = await db.select().from(settings).where(eq(settings.name, data.module));
const updateSetting = await db
.update(settings)
.set({value: data.value, upd_user: user[0].username, upd_date: sql`NOW()`})
.where(eq(settings.name, data.name));
} catch (error) {
log.error(error, "Error updating setting");
throw new Error("Error updating Setting");
}
} else {
log.info("This user cannot add new roles");
throw new Error("The user trying to add a setting dose not have the correct permissions");
}
};

View File

@@ -17,7 +17,7 @@ const AddSetting = z.object({
app.openapi(
createRoute({
tags: ["server"],
tags: ["server:settings"],
summary: "Add Setting",
method: "post",
path: "/settings",

View File

@@ -4,7 +4,7 @@ const app = new OpenAPIHono();
app.openapi(
createRoute({
tags: ["server"],
tags: ["server:settings"],
summary: "Returns all modules in the server",
method: "delete",
path: "/settings",

View File

@@ -8,7 +8,7 @@ const app = new OpenAPIHono();
app.openapi(
createRoute({
tags: ["server"],
tags: ["server:settings"],
summary: "Returns all settings based on your permissions",
method: "get",
path: "/settings",

View File

@@ -1,13 +1,29 @@
import {createRoute, OpenAPIHono, z} from "@hono/zod-openapi";
import type {User} from "../../../../types/users.js";
import {log} from "../../../logger/logger.js";
import {verify} from "hono/jwt";
import {updateSetting} from "../../controller/settings/updateSetting.js";
const app = new OpenAPIHono();
const UpdateSetting = z.object({
name: z.string().openapi({example: "server"}),
value: z.string().openapi({example: "localhost"}),
});
app.openapi(
createRoute({
tags: ["server"],
summary: "Returns all modules in the server",
method: "post",
path: "/",
tags: ["server:settings"],
summary: "Updates A setting",
method: "patch",
path: "/settings",
request: {
body: {
content: {
"application/json": {schema: UpdateSetting},
},
},
},
responses: {
200: {
content: {
@@ -20,10 +36,63 @@ app.openapi(
},
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) => {
return c.json({success: true, message: "Example"}, 200);
// make sure we have a vaid user being accessed thats really logged in
const authHeader = c.req.header("Authorization");
if (authHeader?.includes("Basic")) {
return c.json({message: "You are a Basic user! Please login to get a token"}, 401);
}
if (!authHeader) {
return c.json({message: "Unauthorized"}, 401);
}
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 updateSetting(data, user.user_id ?? "");
return c.json({success: true, message: "The Setting was just updated", data}, 200);
} catch (error) {
return c.json({message: "There was an error updating the settings.", error}, 400);
}
}
);
export default app;

View File

@@ -7,6 +7,7 @@ import updateModule from "./route/modules/updateModules.js";
import addModule from "./route/modules/addModule.js";
import addSetting from "./route/settings/addSetting.js";
import getSettings from "./route/settings/getSettings.js";
import updateSetting from "./route/settings/updateSetting.js";
areModulesIn();
const app = new OpenAPIHono();
@@ -18,6 +19,7 @@ const routes = [
// settings
addSetting,
getSettings,
updateSetting,
] as const;
// app.route("/server", modules);