import { apiError, apiJson, withApiHandler } from "@/lib/api-handler"; import { createEventForScope } from "@/modules/calendar/server/actions"; import { eventInput } from "@/modules/calendar/server/schemas"; import { listEventsForScope } from "@/modules/calendar/server/queries"; function parseCalendarIds(value: string | null): "all" | string[] { if (!value || value === "all") return "all"; return value .split(",") .map((id) => id.trim()) .filter(Boolean); } export async function GET(request: Request) { return withApiHandler(request, async (scope, req) => { const url = new URL(req.url); const from = url.searchParams.get("from"); const to = url.searchParams.get("to"); const calendarIds = parseCalendarIds(url.searchParams.get("calendarIds")); if (!from || !to) { return apiError("from and to query parameters are required", 400); } const events = await listEventsForScope(scope, { from, to, calendarIds }); return apiJson(events); }); } export async function POST(request: Request) { return withApiHandler(request, async (scope, req) => { const body: unknown = await req.json(); const parsed = eventInput.parse(body); const event = await createEventForScope(scope, parsed); return apiJson(event, 201); }); }