Repository Public
1import type { FastifyInstance } from 'fastify';2import { UserSettings } from '@cr/shared';3import { authOf, parse, type Ctx } from '../../http.js';4import { getSettings, putSettings } from './store.js';56/** NOTE: `notifications.webPush` is stored but Web Push delivery is not implemented in this milestone. */7export default async function settingsPlugin(app: FastifyInstance, ctx: Ctx): Promise<void> {8 app.get('/settings', { config: { doc: { summary: 'User settings (defaults merged)', response: UserSettings, responseName: 'UserSettings' } } }, (req) =>9 getSettings(ctx.db, authOf(req).userId),10 );11 app.put('/settings', { config: { doc: { summary: 'Replace user settings', body: UserSettings, bodyName: 'UserSettings', response: UserSettings, responseName: 'UserSettings' } } }, async (req) => {12 const a = authOf(req);13 const settings = parse(UserSettings, req.body);14 await putSettings(ctx.db, a.userId, settings);15 await ctx.audit.req(req, 'settings.update');16 return settings;17 });18}19