● Zion Boggan
repos/Docket/app/api/lobbying/route.ts
zionboggan.com ↗
35 lines · typescript
History for this file →
1
import { NextResponse, type NextRequest } from "next/server";
2
import { safeAll, parsePaging, type ItemList } from "@/lib/db";
3
 
4
export const dynamic = "force-dynamic";
5
 
6
export async function GET(req: NextRequest) {
7
  const sp = req.nextUrl.searchParams;
8
  const { limit, offset } = parsePaging(sp);
9
  const bill = sp.get("bill");
10
 
11
  const items = safeAll(
12
    `SELECT rowid, registrant, client, amount_range, period, source_url
13
     FROM lobbying
14
     ${bill ? "WHERE covered_bill_id = :bill" : ""}
15
     ORDER BY period DESC`,
16
    bill ? { bill } : {},
17
    (r) => ({
18
      id: `lda:${r.rowid}`,
19
      registrant: String(r.registrant ?? ""),
20
      client: String(r.client ?? ""),
21
      amountRange: String(r.amount_range ?? ""),
22
      period: String(r.period ?? ""),
23
      sourceUrl: String(r.source_url ?? "https://lda.gov/"),
24
      href: "/who-pays",
25
    })
26
  );
27
  const total = items.length;
28
  const body: ItemList<typeof items[number]> = {
29
    items: items.slice(offset, offset + limit),
30
    total,
31
    limit,
32
    offset,
33
  };
34
  return NextResponse.json(body);
35
}