2026-06-26 10:50:24 +02:00
|
|
|
|
/**
|
|
|
|
|
|
* Settings Routes – key/value application settings
|
|
|
|
|
|
*/
|
|
|
|
|
|
import type { FastifyInstance } from 'fastify';
|
|
|
|
|
|
import type { DatabaseInterface } from '../database/DatabaseInterface.js';
|
2026-07-04 16:43:55 +02:00
|
|
|
|
import { requireAuth } from '../auth/authMiddleware.js';
|
|
|
|
|
|
import type { AuthService } from '../auth/AuthService.js';
|
|
|
|
|
|
import { validateSettingsKey, validateSettingsValue } from '../utils/validation.js';
|
2026-06-26 10:50:24 +02:00
|
|
|
|
|
2026-07-04 16:43:55 +02:00
|
|
|
|
export function registerSettingsRoutes(fastify: FastifyInstance, db: DatabaseInterface, authService: AuthService) {
|
2026-06-26 10:50:24 +02:00
|
|
|
|
// Get a single setting by key
|
|
|
|
|
|
fastify.get('/api/settings/:key', async (request, reply) => {
|
2026-07-04 16:43:55 +02:00
|
|
|
|
if (!requireAuth(request, reply, authService)) return;
|
2026-06-26 10:50:24 +02:00
|
|
|
|
const { key } = request.params as { key: string };
|
2026-07-04 16:43:55 +02:00
|
|
|
|
const keyErr = validateSettingsKey(key);
|
|
|
|
|
|
if (keyErr) return reply.code(400).send({ error: keyErr });
|
2026-06-26 10:50:24 +02:00
|
|
|
|
const setting = db.getSetting(key);
|
|
|
|
|
|
if (!setting) return reply.code(404).send({ error: 'Setting not found' });
|
|
|
|
|
|
return setting;
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
// Create or update a setting (upsert)
|
2026-07-04 16:43:55 +02:00
|
|
|
|
fastify.put('/api/settings/:key', async (request, reply) => {
|
|
|
|
|
|
if (!requireAuth(request, reply, authService)) return;
|
2026-06-26 10:50:24 +02:00
|
|
|
|
const { key } = request.params as { key: string };
|
2026-07-04 16:43:55 +02:00
|
|
|
|
const keyErr = validateSettingsKey(key);
|
|
|
|
|
|
if (keyErr) return reply.code(400).send({ error: keyErr });
|
2026-06-26 10:50:24 +02:00
|
|
|
|
const body = request.body as { value?: string };
|
2026-07-04 16:43:55 +02:00
|
|
|
|
const valueErr = validateSettingsValue(body.value);
|
|
|
|
|
|
if (valueErr) return reply.code(400).send({ error: valueErr });
|
|
|
|
|
|
db.setSetting(key, body.value as string);
|
|
|
|
|
|
return { key, value: body.value as string, updated_at: new Date().toISOString() };
|
2026-06-26 10:50:24 +02:00
|
|
|
|
});
|
|
|
|
|
|
}
|