33 lines
960 B
TypeScript
33 lines
960 B
TypeScript
import type { PageData } from './$types';
|
|
import type { RequestHandler } from '@sveltejs/kit';
|
|
import { getSession } from '$lib/server/session';
|
|
import { Permissions } from '$lib/permissions';
|
|
import { Settings } from '$lib/server/database';
|
|
|
|
export const POST = (async ({ request, cookies }) => {
|
|
if (getSession(cookies, { permissions: [Permissions.SettingsWrite] }) == null) {
|
|
return new Response(null, {
|
|
status: 401
|
|
});
|
|
}
|
|
|
|
const settings: PageData['settings'] = await request.json();
|
|
|
|
for (const [group, entries] of Object.entries(settings)) {
|
|
for (const [key, value] of Object.entries(entries)) {
|
|
const setting = await Settings.findOne({ where: { key: `${group}.${key}` } });
|
|
if (setting) {
|
|
setting.value = JSON.stringify(value);
|
|
await setting.save();
|
|
} else {
|
|
await Settings.create({
|
|
key: `${group}.${key}`,
|
|
value: JSON.stringify(value)
|
|
});
|
|
}
|
|
}
|
|
}
|
|
|
|
return new Response();
|
|
}) satisfies RequestHandler;
|