← back to Govarbitrage

src/app/api/import/gsa/route.ts

27 lines

import { NextRequest, NextResponse } from "next/server";
import { fetchGsaAuctions } from "@/importers/gsa";
import { ingestMany } from "@/importers/ingest";
import { requireWrite } from "@/lib/auth";

export const dynamic = "force-dynamic";
export const maxDuration = 120;

// Pull live listings from the official GSA Auctions API and ingest them.
// Auth-gated (session or x-import-token). ?limit=N&ai=1 (ai defaults off for speed).
export async function POST(req: NextRequest) {
  const denied = await requireWrite(req);
  if (denied) return denied;
  try {
    const limit = Number(req.nextUrl.searchParams.get("limit") || 40);
    const useAI = req.nextUrl.searchParams.get("ai") === "1";
    const rows = await fetchGsaAuctions({ limit });
    const results = await ingestMany(rows, { useAI });
    const created = results.filter((r) => r.ok && r.created).length;
    const updated = results.filter((r) => r.ok && !r.created).length;
    const failed = results.filter((r) => !r.ok);
    return NextResponse.json({ source: "GSA_AUCTIONS", fetched: rows.length, created, updated, failed: failed.length });
  } catch (e) {
    return NextResponse.json({ error: (e as Error).message }, { status: 502 });
  }
}