| 1 | import { NextResponse, type NextRequest } from "next/server"; |
| 2 | import { parsePaging, type ItemList } from "@/lib/db"; |
| 3 | import { |
| 4 | getCalendarEvents, |
| 5 | getCalendarDaySummary, |
| 6 | type CalendarEvent, |
| 7 | type CalendarDayKind, |
| 8 | } from "@/lib/feed"; |
| 9 | |
| 10 | export const dynamic = "force-dynamic"; |
| 11 | |
| 12 | |
| 13 | export async function GET(req: NextRequest) { |
| 14 | const sp = req.nextUrl.searchParams; |
| 15 | const from = sp.get("from") || undefined; |
| 16 | const to = sp.get("to") || undefined; |
| 17 | const kind = sp.get("kind") || undefined; |
| 18 | |
| 19 | if (sp.get("summary")) { |
| 20 | const items = getCalendarDaySummary(from ?? "", to ?? ""); |
| 21 | const body: ItemList<CalendarDayKind> = { |
| 22 | items, |
| 23 | total: items.length, |
| 24 | limit: items.length, |
| 25 | offset: 0, |
| 26 | }; |
| 27 | return NextResponse.json(body); |
| 28 | } |
| 29 | |
| 30 | const { limit, offset } = parsePaging(sp); |
| 31 | const items = getCalendarEvents({ from, to, kind }); |
| 32 | const total = items.length; |
| 33 | const paged = items.slice(offset, offset + limit); |
| 34 | const body: ItemList<CalendarEvent> = { items: paged, total, limit, offset }; |
| 35 | return NextResponse.json(body); |
| 36 | } |