forked from pinks/eris
1
0
Fork 0
eris/api/paramsRoute.ts

40 lines
1.1 KiB
TypeScript
Raw Normal View History

import { Elysia, t } from "elysia";
2023-10-15 19:13:38 +00:00
import { info } from "std/log/mod.ts";
import { defaultParamsSchema, getConfig, setConfig } from "../app/config.ts";
import { withSessionAdmin } from "./getUser.ts";
2023-10-05 09:00:51 +00:00
export const paramsRoute = new Elysia()
.get(
"",
2023-10-05 09:00:51 +00:00
async () => {
const config = await getConfig();
return config.defaultParams;
2023-10-05 09:00:51 +00:00
},
{
response: {
200: defaultParamsSchema,
2023-10-05 09:00:51 +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();
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 });
return config.defaultParams;
2023-10-23 00:39:01 +00:00
});
2023-10-05 09:00:51 +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"),
},
},
);