feat: banner and report viewer in admin panel

This commit is contained in:
trafficlunar 2025-05-02 22:05:17 +01:00
parent 419fcb4788
commit a8c83b9cb6
6 changed files with 206 additions and 27 deletions

View file

@ -0,0 +1,30 @@
import { NextRequest, NextResponse } from "next/server";
import { auth } from "@/lib/auth";
let bannerText: string | null = null;
export async function GET() {
return NextResponse.json({ success: true, message: bannerText });
}
export async function POST(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.text();
bannerText = body;
return NextResponse.json({ success: true });
}
export async function DELETE() {
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 });
bannerText = null;
return NextResponse.json({ success: true });
}

View file

@ -1,6 +1,6 @@
import { NextRequest, NextResponse } from "next/server";
import { z } from "zod";
import { ReportReason, ReportStatus, ReportType } from "@prisma/client";
import { ReportReason, ReportType } from "@prisma/client";
import { auth } from "@/lib/auth";
import { prisma } from "@/lib/prisma";
@ -15,10 +15,6 @@ const reportSchema = z.object({
notes: z.string().trim().max(256).optional(),
});
const getReportSchema = z.object({
status: z.enum(["open", "resolved", "dismissed"], { message: "Status must be either 'open', 'resolved', or 'dismissed'" }).default("open"),
});
export async function POST(request: NextRequest) {
const session = await auth();
if (!session) return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
@ -70,24 +66,3 @@ export async function POST(request: NextRequest) {
return rateLimit.sendResponse({ success: true });
}
export async function GET(request: NextRequest) {
const session = await auth();
if (!session) return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
// Check if user is an admin
if (Number(session.user.id) != Number(process.env.NEXT_PUBLIC_ADMIN_USER_ID))
return NextResponse.json({ error: "You're not an admin" }, { status: 403 });
const parsed = getReportSchema.safeParse(Object.fromEntries(request.nextUrl.searchParams));
if (!parsed.success) return NextResponse.json({ error: parsed.error.errors[0].message }, { status: 400 });
const { status } = parsed.data;
const reports = await prisma.report.findMany({
where: {
status: (status.toUpperCase() as ReportStatus) ?? "OPEN",
},
});
return NextResponse.json({ success: true, reports });
}