/** /api/admin/inquiries — 留言箱:列表 / 状态更新 / 删除。 */ import { NextResponse } from "next/server"; import { getSession, verifyCsrf } from "@/lib/auth"; import { deleteInquiry, listInquiries, updateInquiryStatus, type InquiryRow, } from "@/lib/siteConfig"; export const runtime = "nodejs"; export async function GET() { if (!(await getSession())) { return NextResponse.json({ error: "未登录" }, { status: 401 }); } return NextResponse.json({ inquiries: listInquiries(200) }); } export async function PATCH(req: Request) { if (!(await verifyCsrf())) { return NextResponse.json({ error: "未登录" }, { status: 401 }); } let body: { id?: number; status?: InquiryRow["status"] }; try { body = await req.json(); } catch { return NextResponse.json({ error: "请求格式错误" }, { status: 400 }); } const valid: InquiryRow["status"][] = ["new", "read", "replied", "archived"]; if ( !Number.isInteger(body.id) || !body.status || !valid.includes(body.status) ) { return NextResponse.json({ error: "参数错误" }, { status: 400 }); } updateInquiryStatus(body.id as number, body.status); return NextResponse.json({ ok: true }); } export async function DELETE(req: Request) { if (!(await verifyCsrf())) { return NextResponse.json({ error: "未登录" }, { status: 401 }); } const { searchParams } = new URL(req.url); const id = Number(searchParams.get("id")); if (!Number.isInteger(id)) { return NextResponse.json({ error: "无效 ID" }, { status: 400 }); } deleteInquiry(id); return NextResponse.json({ ok: true }); }