612ecec6d9
CI / go-lint (push) Has been skipped
CI / python-lint (push) Has been skipped
CI / nodejs-lint (push) Has been skipped
CI / test-go-school (push) Successful in 27s
CI / test-go-edu-search (push) Successful in 29s
CI / test-python-klausur (push) Failing after 3m18s
CI / test-python-agent-core (push) Successful in 20s
CI / test-nodejs-website (push) Successful in 23s
Frontend additions in studio-v2:
- types.ts adds TimetableSolution, TimetableLesson, SolutionStatus,
CreateTimetableSolution mirroring the Go models.
- lib/stundenplan/api.ts adds solutionsApi with list/get/create/remove/
lessons. Solve trigger is POST /timetable/solutions — school-service
forwards to the solver-service over the Docker network.
- _components/plan/SolutionList: table of past solves with status
badges, hard/soft score, Anzeigen + Loeschen buttons, and a
'Neuen Plan generieren' trigger. Auto-polls every 4 s while any
solution is pending/running, clears the interval otherwise.
- _components/plan/PlanView: Mo–Fr × period weekly grid. Three
perspectives (Klasse / Lehrer / Raum) toggleable via test-id'd
buttons; selector below lists every unique resource with at least
one lesson. Cells colour-coded by tt_subject.color.
- _components/plan/PlanHub orchestrates list + view; default tab in
page.tsx switches from 'klassen' to 'plan'.
Tests:
- mockSchoolApi helper extracted to e2e/_helpers.ts so the spec file
stays under 500 LOC. Helper now also mocks /solutions GET/POST/DELETE
and /solutions/:id/lessons; solutions kept in a closure so POST
appears in the next GET.
- 8 new tests across two suites: SolutionList empty state, list
render, completed-vs-failed Anzeigen visibility, solve trigger;
PlanView placeholder when no selection, grid render, perspective
switching.
- Existing Klassen CRUD tests now click the Klassen tab first
(Plan is the new default landing tab).
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
155 lines
7.9 KiB
TypeScript
155 lines
7.9 KiB
TypeScript
/**
|
|
* Stundenplan API client. All requests go through /api/school/* which proxies
|
|
* to the school-service Gin server (port 8084). Auth token, if available, is
|
|
* passed via Authorization: Bearer; for now no token = upstream 401.
|
|
*/
|
|
|
|
import type {
|
|
TimetableClass, TimetablePeriod, TimetableRoom, TimetableSubject,
|
|
TimetableTeacher, TimetableCurriculum, TimetableAssignment,
|
|
CreateTimetableClass, CreateTimetablePeriod, CreateTimetableRoom,
|
|
CreateTimetableSubject, CreateTimetableTeacher,
|
|
CreateTimetableCurriculum, CreateTimetableAssignment,
|
|
TeacherUnavailableDay, TeacherUnavailableWindow, TeacherMaxHoursDay,
|
|
TeacherMaxHoursWeek, TeacherExcludedSubject, TeacherExcludedRoom,
|
|
SubjectMinDayGap, SubjectMaxConsecutive, SubjectContiguousWhenRepeated,
|
|
SubjectPreferredPeriod, SubjectDoubleLesson,
|
|
ClassMaxHoursDay, ClassNoGaps,
|
|
RoomRequiresType, RoomUnavailable,
|
|
TimetableSolution, TimetableLesson, CreateTimetableSolution,
|
|
} from '@/app/stundenplan/types'
|
|
|
|
const TOKEN_KEY = 'bp_stundenplan_jwt'
|
|
|
|
export function setStundenplanToken(token: string): void {
|
|
if (typeof window !== 'undefined') localStorage.setItem(TOKEN_KEY, token)
|
|
}
|
|
|
|
export function getStundenplanToken(): string {
|
|
if (typeof window === 'undefined') return ''
|
|
return localStorage.getItem(TOKEN_KEY) || ''
|
|
}
|
|
|
|
async function apiFetch<T>(endpoint: string, options: RequestInit = {}): Promise<T> {
|
|
const headers: Record<string, string> = {
|
|
'Content-Type': 'application/json',
|
|
...(options.headers as Record<string, string> | undefined),
|
|
}
|
|
const token = getStundenplanToken()
|
|
if (token) headers['Authorization'] = `Bearer ${token}`
|
|
|
|
const res = await fetch(`/api/school${endpoint}`, { ...options, headers })
|
|
if (!res.ok) {
|
|
const errData = await res.json().catch(() => ({ error: 'Unknown error' }))
|
|
throw new Error(errData.error || errData.detail || `HTTP ${res.status}`)
|
|
}
|
|
if (res.status === 204) return undefined as T
|
|
return res.json()
|
|
}
|
|
|
|
// ---------- Stammdaten ----------
|
|
|
|
export const classesApi = {
|
|
list: () => apiFetch<TimetableClass[]>('/timetable/classes'),
|
|
create: (data: CreateTimetableClass) =>
|
|
apiFetch<TimetableClass>('/timetable/classes', { method: 'POST', body: JSON.stringify(data) }),
|
|
remove: (id: string) =>
|
|
apiFetch<void>(`/timetable/classes/${id}`, { method: 'DELETE' }),
|
|
}
|
|
|
|
export const periodsApi = {
|
|
list: () => apiFetch<TimetablePeriod[]>('/timetable/periods'),
|
|
create: (data: CreateTimetablePeriod) =>
|
|
apiFetch<TimetablePeriod>('/timetable/periods', { method: 'POST', body: JSON.stringify(data) }),
|
|
remove: (id: string) =>
|
|
apiFetch<void>(`/timetable/periods/${id}`, { method: 'DELETE' }),
|
|
}
|
|
|
|
export const roomsApi = {
|
|
list: () => apiFetch<TimetableRoom[]>('/timetable/rooms'),
|
|
create: (data: CreateTimetableRoom) =>
|
|
apiFetch<TimetableRoom>('/timetable/rooms', { method: 'POST', body: JSON.stringify(data) }),
|
|
remove: (id: string) =>
|
|
apiFetch<void>(`/timetable/rooms/${id}`, { method: 'DELETE' }),
|
|
}
|
|
|
|
export const subjectsApi = {
|
|
list: () => apiFetch<TimetableSubject[]>('/timetable/subjects'),
|
|
create: (data: CreateTimetableSubject) =>
|
|
apiFetch<TimetableSubject>('/timetable/subjects', { method: 'POST', body: JSON.stringify(data) }),
|
|
remove: (id: string) =>
|
|
apiFetch<void>(`/timetable/subjects/${id}`, { method: 'DELETE' }),
|
|
}
|
|
|
|
export const teachersApi = {
|
|
list: () => apiFetch<TimetableTeacher[]>('/timetable/teachers'),
|
|
create: (data: CreateTimetableTeacher) =>
|
|
apiFetch<TimetableTeacher>('/timetable/teachers', { method: 'POST', body: JSON.stringify(data) }),
|
|
remove: (id: string) =>
|
|
apiFetch<void>(`/timetable/teachers/${id}`, { method: 'DELETE' }),
|
|
}
|
|
|
|
export const curriculumApi = {
|
|
list: () => apiFetch<TimetableCurriculum[]>('/timetable/curriculum'),
|
|
create: (data: CreateTimetableCurriculum) =>
|
|
apiFetch<TimetableCurriculum>('/timetable/curriculum', { method: 'POST', body: JSON.stringify(data) }),
|
|
remove: (id: string) =>
|
|
apiFetch<void>(`/timetable/curriculum/${id}`, { method: 'DELETE' }),
|
|
}
|
|
|
|
export const assignmentsApi = {
|
|
list: () => apiFetch<TimetableAssignment[]>('/timetable/assignments'),
|
|
create: (data: CreateTimetableAssignment) =>
|
|
apiFetch<TimetableAssignment>('/timetable/assignments', { method: 'POST', body: JSON.stringify(data) }),
|
|
remove: (id: string) =>
|
|
apiFetch<void>(`/timetable/assignments/${id}`, { method: 'DELETE' }),
|
|
}
|
|
|
|
// ---------- Constraints ----------
|
|
|
|
/**
|
|
* Factory that builds a list/create/remove triple for a constraint endpoint.
|
|
* The 15 constraint tables share the same CRUD shape; only TItem differs.
|
|
*/
|
|
function constraintApi<TItem, TCreate>(path: string) {
|
|
return {
|
|
list: () => apiFetch<TItem[]>(`/timetable/constraints/${path}`),
|
|
create: (data: TCreate) =>
|
|
apiFetch<TItem>(`/timetable/constraints/${path}`, { method: 'POST', body: JSON.stringify(data) }),
|
|
remove: (id: string) =>
|
|
apiFetch<void>(`/timetable/constraints/${path}/${id}`, { method: 'DELETE' }),
|
|
}
|
|
}
|
|
|
|
export const teacherUnavailableDayApi = constraintApi<TeacherUnavailableDay, Omit<TeacherUnavailableDay, 'id' | 'created_by_user_id' | 'created_at'>>('teacher/unavailable-day')
|
|
export const teacherUnavailableWindowApi = constraintApi<TeacherUnavailableWindow, Omit<TeacherUnavailableWindow, 'id' | 'created_by_user_id' | 'created_at'>>('teacher/unavailable-window')
|
|
export const teacherMaxHoursDayApi = constraintApi<TeacherMaxHoursDay, Omit<TeacherMaxHoursDay, 'id' | 'created_by_user_id' | 'created_at'>>('teacher/max-hours-day')
|
|
export const teacherMaxHoursWeekApi = constraintApi<TeacherMaxHoursWeek, Omit<TeacherMaxHoursWeek, 'id' | 'created_by_user_id' | 'created_at'>>('teacher/max-hours-week')
|
|
export const teacherExcludedSubjectApi = constraintApi<TeacherExcludedSubject, Omit<TeacherExcludedSubject, 'id' | 'created_by_user_id' | 'created_at'>>('teacher/excluded-subject')
|
|
export const teacherExcludedRoomApi = constraintApi<TeacherExcludedRoom, Omit<TeacherExcludedRoom, 'id' | 'created_by_user_id' | 'created_at'>>('teacher/excluded-room')
|
|
|
|
export const subjectMinDayGapApi = constraintApi<SubjectMinDayGap, Omit<SubjectMinDayGap, 'id' | 'created_by_user_id' | 'created_at'>>('subject/min-day-gap')
|
|
export const subjectMaxConsecutiveApi = constraintApi<SubjectMaxConsecutive, Omit<SubjectMaxConsecutive, 'id' | 'created_by_user_id' | 'created_at'>>('subject/max-consecutive')
|
|
export const subjectContiguousWhenRepeatedApi = constraintApi<SubjectContiguousWhenRepeated, Omit<SubjectContiguousWhenRepeated, 'id' | 'created_by_user_id' | 'created_at'>>('subject/contiguous-when-repeated')
|
|
export const subjectPreferredPeriodApi = constraintApi<SubjectPreferredPeriod, Omit<SubjectPreferredPeriod, 'id' | 'created_by_user_id' | 'created_at'>>('subject/preferred-period')
|
|
export const subjectDoubleLessonApi = constraintApi<SubjectDoubleLesson, Omit<SubjectDoubleLesson, 'id' | 'created_by_user_id' | 'created_at'>>('subject/double-lesson')
|
|
|
|
export const classMaxHoursDayApi = constraintApi<ClassMaxHoursDay, Omit<ClassMaxHoursDay, 'id' | 'created_by_user_id' | 'created_at'>>('class/max-hours-day')
|
|
export const classNoGapsApi = constraintApi<ClassNoGaps, Omit<ClassNoGaps, 'id' | 'created_by_user_id' | 'created_at'>>('class/no-gaps')
|
|
|
|
export const roomRequiresTypeApi = constraintApi<RoomRequiresType, Omit<RoomRequiresType, 'id' | 'created_by_user_id' | 'created_at'>>('room/requires-type')
|
|
export const roomUnavailableApi = constraintApi<RoomUnavailable, Omit<RoomUnavailable, 'id' | 'created_by_user_id' | 'created_at'>>('room/unavailable')
|
|
|
|
// ---------- Solutions ----------
|
|
|
|
export const solutionsApi = {
|
|
list: () => apiFetch<TimetableSolution[]>('/timetable/solutions'),
|
|
get: (id: string) => apiFetch<TimetableSolution>(`/timetable/solutions/${id}`),
|
|
create: (data: CreateTimetableSolution) =>
|
|
apiFetch<TimetableSolution>('/timetable/solutions', { method: 'POST', body: JSON.stringify(data) }),
|
|
remove: (id: string) =>
|
|
apiFetch<void>(`/timetable/solutions/${id}`, { method: 'DELETE' }),
|
|
lessons: (id: string) =>
|
|
apiFetch<TimetableLesson[]>(`/timetable/solutions/${id}/lessons`),
|
|
}
|