src/app/api/reports/route.ts
Linked repository file from this build.
import { serverTiming } from "@/lib/server-timing";
import { NextResponse } from "next/server";
import { z } from "zod";
import { parseSampleMonth } from "@/features/reports/sample-month";
import { apiRoute } from "@/lib/api-route";
import { queryReport } from "@/lib/db/report-store";
import { getActor } from "@/lib/auth/server";
export const dynamic = "force-dynamic";
const month = z
.string()
.optional()
.refine((value) => !value || parseSampleMonth(value) !== null)
.transform((value) => (value ? parseSampleMonth(value)! : undefined));
const filtersSchema = z
.object({
from: month,
through: month,
examiner: z.string().max(200).optional(),
county: z.string().max(200).optional(),
})
.refine((value) => !value.from || !value.through || value.from <= value.through);
export const GET = apiRoute("Unable to load reports. Please try Refresh.", async (request) => {
const timing = serverTiming();
const actor = await timing.measure("auth", () => getActor(request));
const filters = filtersSchema.safeParse(Object.fromEntries(new URL(request.url).searchParams));
if (!filters.success)
return NextResponse.json({ error: "Invalid report filters." }, { status: 400 });
return timing.json(await timing.measure("data", () => queryReport(actor, filters.data)));
});