/** * Schulkalender API client. Re-uses the same /api/school/* proxy + the JWT * helper from stundenplan so we don't fork the auth flow. */ import { getStundenplanToken } from '@/lib/stundenplan/api' import type { PublicEvent, SchoolCalendarConfig, UpsertSchoolCalendarConfig, SchoolEvent, CreateSchoolEvent, SchoolYearRolloverResult, ParentInviteListItem, InviteParentRequest, InviteParentResponse, } from '@/app/schulkalender/types' async function apiFetch(endpoint: string, options: RequestInit = {}): Promise { const headers: Record = { 'Content-Type': 'application/json', ...(options.headers as Record | undefined), } const token = getStundenplanToken() if (token) headers['Authorization'] = `Bearer ${token}` const res = await fetch(`/api/school${endpoint}`, { ...options, headers }) if (!res.ok) { const err = await res.json().catch(() => ({ error: 'Unknown error' })) throw new Error(err.error || err.detail || `HTTP ${res.status}`) } if (res.status === 204) return undefined as T return res.json() } export const calendarApi = { listHolidays: (region: string, from: string, to: string) => apiFetch(`/calendar/holidays?region=${encodeURIComponent(region)}&from=${from}&to=${to}`), getConfig: () => apiFetch('/calendar/config'), upsertConfig: (data: UpsertSchoolCalendarConfig) => apiFetch('/calendar/config', { method: 'PUT', body: JSON.stringify(data) }), // School events listEvents: (from: string, to: string) => apiFetch(`/calendar/events?from=${from}&to=${to}`), createEvent: (data: CreateSchoolEvent) => apiFetch('/calendar/events', { method: 'POST', body: JSON.stringify(data) }), deleteEvent: (id: string) => apiFetch(`/calendar/events/${id}`, { method: 'DELETE' }), rolloverSchoolYear: (newYearStart?: string, newYearEnd?: string) => apiFetch('/calendar/school-year-rollover', { method: 'POST', body: JSON.stringify({ new_year_start: newYearStart, new_year_end: newYearEnd, }), }), // Phase 9c: parent invitations listParents: () => apiFetch('/calendar/parents'), inviteParent: (data: InviteParentRequest) => apiFetch('/calendar/parents/invite', { method: 'POST', body: JSON.stringify(data) }), deleteParentChild: (childId: string) => apiFetch(`/calendar/parents/children/${childId}`, { method: 'DELETE' }), }