2025-06-26 19:32:53 -07:00
|
|
|
import { RequestHandler } from "express";
|
2025-07-17 05:04:30 -07:00
|
|
|
import { config, syncConfigWithDatabase } from "@/src/services/configService";
|
2025-06-26 19:32:53 -07:00
|
|
|
import { getAccountForRequest, isAdministrator } from "@/src/services/loginService";
|
2025-07-05 06:23:10 -07:00
|
|
|
import { saveConfig } from "@/src/services/configWriterService";
|
2025-07-18 15:36:10 -07:00
|
|
|
import { sendWsBroadcastEx } from "@/src/services/wsService";
|
2025-06-26 19:32:53 -07:00
|
|
|
|
|
|
|
export const getConfigController: RequestHandler = async (req, res) => {
|
|
|
|
const account = await getAccountForRequest(req);
|
|
|
|
if (isAdministrator(account)) {
|
|
|
|
const responseData: Record<string, boolean | string | number | null> = {};
|
|
|
|
for (const id of req.body as string[]) {
|
|
|
|
const [obj, idx] = configIdToIndexable(id);
|
|
|
|
responseData[id] = obj[idx] ?? null;
|
|
|
|
}
|
|
|
|
res.json(responseData);
|
|
|
|
} else {
|
|
|
|
res.status(401).end();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
export const setConfigController: RequestHandler = async (req, res) => {
|
|
|
|
const account = await getAccountForRequest(req);
|
|
|
|
if (isAdministrator(account)) {
|
|
|
|
for (const [id, value] of Object.entries(req.body as Record<string, boolean | string | number>)) {
|
|
|
|
const [obj, idx] = configIdToIndexable(id);
|
|
|
|
obj[idx] = value;
|
|
|
|
}
|
2025-07-18 15:36:10 -07:00
|
|
|
sendWsBroadcastEx({ config_reloaded: true }, undefined, parseInt(String(req.query.wsid)));
|
2025-07-11 21:16:07 -07:00
|
|
|
syncConfigWithDatabase();
|
2025-06-26 19:32:53 -07:00
|
|
|
await saveConfig();
|
|
|
|
res.end();
|
|
|
|
} else {
|
|
|
|
res.status(401).end();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const configIdToIndexable = (id: string): [Record<string, boolean | string | number | undefined>, string] => {
|
|
|
|
let obj = config as unknown as Record<string, never>;
|
|
|
|
const arr = id.split(".");
|
|
|
|
while (arr.length > 1) {
|
2025-07-06 20:14:05 -07:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
|
|
|
|
obj[arr[0]] ??= {} as never;
|
2025-06-26 19:32:53 -07:00
|
|
|
obj = obj[arr[0]];
|
|
|
|
arr.splice(0, 1);
|
|
|
|
}
|
|
|
|
return [obj, arr[0]];
|
|
|
|
};
|