intial setting and auth intergrated
This commit is contained in:
44
backend/system/settings.route.ts
Normal file
44
backend/system/settings.route.ts
Normal file
@@ -0,0 +1,44 @@
|
||||
import { type Response, Router } from "express";
|
||||
import { db } from "../db/db.controller.js";
|
||||
import { settings } from "../db/schema/settings.schema.js";
|
||||
|
||||
import { apiReturn } from "../utils/returnHelper.utils.js";
|
||||
import { tryCatch } from "../utils/trycatch.utils.js";
|
||||
|
||||
// export const updateSetting = async (setting: Setting) => {
|
||||
// // TODO: when the setting is a feature setting we will need to have it run each kill switch on the crons well just stop them and during a reset it just wont start them
|
||||
// // TODO: when the setting is a system we will need to force an app restart
|
||||
// // TODO: when the setting is standard we don't do anything.
|
||||
// };
|
||||
|
||||
const r = Router();
|
||||
|
||||
r.get("/", async (_, res: Response) => {
|
||||
const { data: sName, error: sError } = await tryCatch(
|
||||
db.select().from(settings),
|
||||
);
|
||||
|
||||
if (sError) {
|
||||
return apiReturn(res, {
|
||||
success: false,
|
||||
level: "error",
|
||||
module: "system",
|
||||
subModule: "settings",
|
||||
message: `There was an error getting the settings `,
|
||||
data: [sError],
|
||||
status: 400,
|
||||
});
|
||||
}
|
||||
|
||||
return apiReturn(res, {
|
||||
success: true,
|
||||
level: "info",
|
||||
module: "system",
|
||||
subModule: "settings",
|
||||
message: `All current settings`,
|
||||
data: sName ?? [],
|
||||
status: 200,
|
||||
});
|
||||
});
|
||||
|
||||
export default r;
|
||||
0
backend/system/settingsBase.controller.ts
Normal file
0
backend/system/settingsBase.controller.ts
Normal file
@@ -1,7 +0,0 @@
|
||||
import type { Setting } from "../db/schema/settings.schema.js";
|
||||
|
||||
export const updateSetting = async (setting: Setting) => {
|
||||
// TODO: when the setting is a feature setting we will need to have it run each kill switch on the crons well just stop them and during a reset it just wont start them
|
||||
// TODO: when the setting is a system we will need to force an app restart
|
||||
// TODO: when the setting is standard we don't do anything.
|
||||
};
|
||||
93
backend/system/settingsUpdate.route.ts
Normal file
93
backend/system/settingsUpdate.route.ts
Normal file
@@ -0,0 +1,93 @@
|
||||
import { eq, sql } from "drizzle-orm";
|
||||
import { type Request, type Response, Router } from "express";
|
||||
import { db } from "../db/db.controller.js";
|
||||
import { settings } from "../db/schema/settings.schema.js";
|
||||
|
||||
import { apiReturn } from "../utils/returnHelper.utils.js";
|
||||
import { tryCatch } from "../utils/trycatch.utils.js";
|
||||
|
||||
// export const updateSetting = async (setting: Setting) => {
|
||||
// // TODO: when the setting is a feature setting we will need to have it run each kill switch on the crons well just stop them and during a reset it just wont start them
|
||||
// // TODO: when the setting is a system we will need to force an app restart
|
||||
// // TODO: when the setting is standard we don't do anything.
|
||||
// };
|
||||
|
||||
const r = Router();
|
||||
|
||||
r.patch("/:name", async (req: Request, res: Response) => {
|
||||
const { name } = req.params;
|
||||
const updates: Record<string, unknown | null> = {};
|
||||
// lets see if we even have a setting name
|
||||
|
||||
const { data: sName, error: sError } = await tryCatch(
|
||||
db
|
||||
.select()
|
||||
.from(settings)
|
||||
.where(eq(settings.name, name ?? "")),
|
||||
);
|
||||
|
||||
if (sError) {
|
||||
return apiReturn(res, {
|
||||
success: false,
|
||||
level: "error",
|
||||
module: "system",
|
||||
subModule: "settings",
|
||||
message: `There was an error checking the name of the setting`,
|
||||
data: [sError],
|
||||
status: 400,
|
||||
});
|
||||
}
|
||||
|
||||
// if (sName?.length === 0) {
|
||||
// return apiReturn(res, {
|
||||
// success: false,
|
||||
// level: "error",
|
||||
// module: "system",
|
||||
// subModule: "settings",
|
||||
// message: `The setting "${name}" dose not appear to be a valid setting please check the name and try again. `,
|
||||
// data: [],
|
||||
// status: 400,
|
||||
// });
|
||||
// }
|
||||
|
||||
// manage the actual setting. we will still do an upsert just in case we strangely get past everything
|
||||
|
||||
if (req.body?.value !== undefined) {
|
||||
updates.value = req.body.value;
|
||||
}
|
||||
|
||||
if (req.body?.description !== undefined) {
|
||||
updates.description = req.body.description;
|
||||
}
|
||||
|
||||
if (req.body?.moduleName !== undefined) {
|
||||
updates.moduleName = req.body.moduleName;
|
||||
}
|
||||
|
||||
if (req.body?.active !== undefined) {
|
||||
updates.active = req.body.active === "true";
|
||||
}
|
||||
|
||||
if (req.body?.roles !== undefined) {
|
||||
updates.roles = req.body.roles;
|
||||
}
|
||||
|
||||
if (req.body?.settingType !== undefined) {
|
||||
updates.settingType = req.body.settingType;
|
||||
}
|
||||
|
||||
updates.upd_user = req.user?.username || "lst_user";
|
||||
updates.upd_date = sql`NOW()`;
|
||||
|
||||
return apiReturn(res, {
|
||||
success: true,
|
||||
level: "info",
|
||||
module: "system",
|
||||
subModule: "settings",
|
||||
message: `Setting "${name}" Was just updated. `,
|
||||
data: [updates],
|
||||
status: 400,
|
||||
});
|
||||
});
|
||||
|
||||
export default r;
|
||||
@@ -1,9 +1,14 @@
|
||||
import { requireAuth } from "backend/middleware/auth.middleware.js";
|
||||
import type { Express } from "express";
|
||||
import getSettings from "./settings.route.js";
|
||||
import updSetting from "./settingsUpdate.route.js";
|
||||
import stats from "./stats.route.js";
|
||||
|
||||
export const setupSystemRoutes = (baseUrl: string, app: Express) => {
|
||||
//stats will be like this as we dont need to change this
|
||||
app.use(`${baseUrl}/api/stats`, stats);
|
||||
app.use(`${baseUrl}/api/settings`, getSettings);
|
||||
app.use(`${baseUrl}/api/settings`, requireAuth, updSetting);
|
||||
|
||||
// all other system should be under /api/system/*
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user