2023-11-20 02:14:14 +00:00
|
|
|
import { Elysia, t } from "elysia";
|
2023-10-15 19:13:38 +00:00
|
|
|
import { info } from "std/log/mod.ts";
|
2023-11-20 02:14:14 +00:00
|
|
|
import { defaultParamsSchema, getConfig, setConfig } from "../app/config.ts";
|
|
|
|
import { withSessionAdmin } from "./getUser.ts";
|
2023-10-05 09:00:51 +00:00
|
|
|
|
2023-11-20 02:14:14 +00:00
|
|
|
export const paramsRoute = new Elysia()
|
|
|
|
.get(
|
|
|
|
"",
|
2023-10-05 09:00:51 +00:00
|
|
|
async () => {
|
|
|
|
const config = await getConfig();
|
2023-11-20 02:14:14 +00:00
|
|
|
return config.defaultParams;
|
2023-10-05 09:00:51 +00:00
|
|
|
},
|
|
|
|
{
|
2023-11-20 02:14:14 +00:00
|
|
|
response: {
|
|
|
|
200: defaultParamsSchema,
|
2023-10-05 09:00:51 +00:00
|
|
|
},
|
|
|
|
},
|
2023-11-20 02:14:14 +00:00
|
|
|
)
|
|
|
|
.patch(
|
|
|
|
"",
|
|
|
|
async ({ query, body, set }) => {
|
|
|
|
return withSessionAdmin({ query, set }, async (user) => {
|
2023-10-17 13:03:14 +00:00
|
|
|
const config = await getConfig();
|
2023-11-20 02:14:14 +00:00
|
|
|
info(`User ${user.first_name} updated default params: ${JSON.stringify(body)}`);
|
|
|
|
const defaultParams = { ...config.defaultParams, ...body };
|
2023-10-17 13:03:14 +00:00
|
|
|
await setConfig({ defaultParams });
|
2023-11-20 02:14:14 +00:00
|
|
|
return config.defaultParams;
|
2023-10-23 00:39:01 +00:00
|
|
|
});
|
2023-10-05 09:00:51 +00:00
|
|
|
},
|
2023-11-20 02:14:14 +00:00
|
|
|
{
|
|
|
|
query: t.Object({ sessionId: t.String() }),
|
|
|
|
body: defaultParamsSchema,
|
|
|
|
response: {
|
|
|
|
200: defaultParamsSchema,
|
|
|
|
401: t.Literal("Must be logged in"),
|
|
|
|
403: t.Literal("Must be an admin"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
);
|