Files
wfh/src/app/api/admin/settings/route.ts

38 lines
1.1 KiB
TypeScript
Raw Normal View History

2026-03-12 17:09:22 -05:00
import { NextResponse } from "next/server";
import { getServerSession } from "next-auth/next";
import { authOptions } from "@/app/api/auth/[...nextauth]/route";
import { prisma } from "@/lib/prisma";
// GET /api/admin/settings - Fetch global settings
export async function GET() {
const session = await getServerSession(authOptions);
if (!session || session.user.role !== "ADMIN") {
return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
}
const settings = await prisma.setting.findMany();
const settingsMap = settings.reduce((acc: any, curr: any) => ({ ...acc, [curr.key]: curr.value }), {});
return NextResponse.json(settingsMap);
}
// POST /api/admin/settings - Update or create setting
export async function POST(req: Request) {
const session = await getServerSession(authOptions);
if (!session || session.user.role !== "ADMIN") {
return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
}
const { key, value } = await req.json();
const setting = await prisma.setting.upsert({
where: { key },
update: { value },
create: { key, value },
});
return NextResponse.json(setting);
}