| 1 | import { NextResponse, type NextRequest } from "next/server"; |
| 2 | import { parsePaging, type ItemList } from "@/lib/db"; |
| 3 | import { getBills, getBillsCount, type BillListItem } from "@/lib/feed"; |
| 4 | |
| 5 | export const dynamic = "force-dynamic"; |
| 6 | |
| 7 | export async function GET(req: NextRequest) { |
| 8 | const sp = req.nextUrl.searchParams; |
| 9 | const { limit, offset } = parsePaging(sp); |
| 10 | const congressRaw = parseInt(sp.get("congress") ?? "", 10); |
| 11 | |
| 12 | const filter = { |
| 13 | q: sp.get("q") || undefined, |
| 14 | state: sp.get("state") || undefined, |
| 15 | status: sp.get("status") || undefined, |
| 16 | congress: Number.isFinite(congressRaw) ? congressRaw : undefined, |
| 17 | policyArea: sp.get("policyArea") || undefined, |
| 18 | sort: ( |
| 19 | sp.get("sort") === "action" ? "action" : |
| 20 | sp.get("sort") === "congress" ? "congress" : |
| 21 | sp.get("sort") === "status" ? "status" : |
| 22 | "updated" |
| 23 | ) as "updated" | "action" | "congress" | "status", |
| 24 | }; |
| 25 | |
| 26 | const items = getBills(filter, limit, offset); |
| 27 | const total = getBillsCount(filter); |
| 28 | const body: ItemList<BillListItem> = { items, total, limit, offset }; |
| 29 | return NextResponse.json(body); |
| 30 | } |