Skip to article
Documentation

src/app/api/exports/route.ts

Linked repository file from this build.

import { serverTiming } from "@/lib/server-timing";
import { apiRoute } from "@/lib/api-route";
import { getActor } from "@/lib/auth/server";
import { RuleError } from "@/lib/errors";
import { queryExportMonths, exportReviews } from "@/lib/db/export-store";
import { assertExportRole, generateMonthExport } from "@/features/exports/month-export";
export const dynamic = "force-dynamic";
export const GET = apiRoute("Export failed. Please retry.", async (request) => {
  const timing = serverTiming();
  const actor = await timing.measure("auth", () => getActor(request));
  assertExportRole(actor);
  const params = new URL(request.url).searchParams;
  const kind = params.get("kind");
  if (kind !== null && kind !== "fns" && kind !== "epp")
    throw new RuleError("Choose FNS or EPP export.");
  if (!kind)
    return timing.json({ months: await timing.measure("data", () => queryExportMonths(actor)) });
  assertExportRole(actor, kind);
  const months = params.getAll("month");
  const reviews = await timing.measure("data", () => exportReviews(actor, months));
  return timing.json(generateMonthExport(reviews, actor, kind, months));
});