feat: allow admins to disable submissions

This commit is contained in:
trafficlunar 2025-05-09 22:05:50 +01:00
parent 769dd0b863
commit c5437ed3e7
5 changed files with 104 additions and 0 deletions

View file

@ -0,0 +1,23 @@
import { NextRequest, NextResponse } from "next/server";
import { z } from "zod";
import { auth } from "@/lib/auth";
let canSubmit = true;
export async function GET() {
return NextResponse.json({ success: true, value: canSubmit });
}
export async function PATCH(request: NextRequest) {
const session = await auth();
if (!session) return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
if (Number(session.user.id) !== Number(process.env.NEXT_PUBLIC_ADMIN_USER_ID)) return NextResponse.json({ error: "Forbidden" }, { status: 403 });
const body = await request.json();
const validatedCanSubmit = z.boolean().safeParse(body);
if (!validatedCanSubmit.success) return NextResponse.json({ error: "Failed to validate body" }, { status: 400 });
canSubmit = validatedCanSubmit.data;
return NextResponse.json({ success: true });
}

View file

@ -41,6 +41,11 @@ export async function POST(request: NextRequest) {
const check = await rateLimit.handle();
if (check) return check;
const response = await fetch(`${process.env.BASE_URL}/api/admin/can-submit`);
const { value } = await response.json();
if (!value) return rateLimit.sendResponse({ error: "Submissions are disabled" }, 409);
// Parse data
const formData = await request.formData();
let rawTags: string[];