feat: add settings route for managing forum and bug feature toggles
This commit is contained in:
44
nest-backend/src/routes/settings.ts
Normal file
44
nest-backend/src/routes/settings.ts
Normal file
@@ -0,0 +1,44 @@
|
||||
import { Router } from 'express';
|
||||
import type { Request, Response } from 'express';
|
||||
import prisma from '../lib/prisma.js';
|
||||
import { authenticate, requireAdmin } from '../middleware/auth.js';
|
||||
|
||||
const router = Router();
|
||||
|
||||
function getOrCreateSettings() {
|
||||
return prisma.siteSettings.upsert({
|
||||
where: { id: 1 },
|
||||
update: {},
|
||||
create: { id: 1 },
|
||||
});
|
||||
}
|
||||
|
||||
// GET /api/settings — public
|
||||
router.get('/', async (_req: Request, res: Response): Promise<void> => {
|
||||
const settings = await getOrCreateSettings();
|
||||
res.json({ forumEnabled: settings.forumEnabled, bugsEnabled: settings.bugsEnabled });
|
||||
});
|
||||
|
||||
// PATCH /api/settings — admin only
|
||||
router.patch('/', authenticate, requireAdmin, async (req: Request, res: Response): Promise<void> => {
|
||||
const { forumEnabled, bugsEnabled } = req.body as { forumEnabled?: unknown; bugsEnabled?: unknown };
|
||||
|
||||
const data: { forumEnabled?: boolean; bugsEnabled?: boolean } = {};
|
||||
if (typeof forumEnabled === 'boolean') data.forumEnabled = forumEnabled;
|
||||
if (typeof bugsEnabled === 'boolean') data.bugsEnabled = bugsEnabled;
|
||||
|
||||
if (Object.keys(data).length === 0) {
|
||||
res.status(400).json({ error: 'No valid fields to update' });
|
||||
return;
|
||||
}
|
||||
|
||||
const settings = await prisma.siteSettings.upsert({
|
||||
where: { id: 1 },
|
||||
update: data,
|
||||
create: { id: 1, ...data },
|
||||
});
|
||||
|
||||
res.json({ forumEnabled: settings.forumEnabled, bugsEnabled: settings.bugsEnabled });
|
||||
});
|
||||
|
||||
export default router;
|
||||
Reference in New Issue
Block a user