import { Course, CourseAttendeeRole, CourseEventDetails, CourseEventSummary } from '@shared/types/course' //@ts-ignore const addr = import.meta.env.VITE_APIHOST; export async function getTrainingReports(sortMode: string, search: string): Promise { const res = await fetch(`${addr}/courseEvent?sort=${sortMode}&search=${search}`); if (res.ok) { return await res.json() as Promise; } else { console.error("Something went wrong"); throw new Error("Failed to load training reports"); } } export async function getTrainingReport(id: number): Promise { const res = await fetch(`${addr}/courseEvent/${id}`); if (res.ok) { return await res.json() as Promise; } else { console.error("Something went wrong"); throw new Error("Failed to load training reports"); } } export async function getAllTrainings(): Promise { const res = await fetch(`${addr}/course`); if (res.ok) { return await res.json() as Promise; } else { console.error("Something went wrong"); throw new Error("Failed to load training list"); } } export async function getAllAttendeeRoles(): Promise { const res = await fetch(`${addr}/course/roles`); if (res.ok) { return await res.json() as Promise; } else { console.error("Something went wrong"); throw new Error("Failed to load attendee roles"); } } export async function postTrainingReport(report: CourseEventDetails) { const res = await fetch(`${addr}/courseEvent`, { method: "POST", headers: { "Content-Type": "application/json", }, body: JSON.stringify(report), credentials: 'include', }); if (!res.ok) { const errorText = await res.text(); throw new Error(`Failed to post training report: ${res.status} ${errorText}`); } return res.json(); // expected to return the inserted report or new ID }