From 4e531e5af0fe1ff5f1d0c66a8112706add16cbfc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Burak=20Boduro=C4=9Flu?= <80620802+burakboduroglu@users.noreply.github.com> Date: Tue, 28 Jul 2026 14:22:40 +0300 Subject: [PATCH] =?UTF-8?q?feat(api):=20=F0=9F=94=8C=20align=20backend=20c?= =?UTF-8?q?ontracts?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - add runtime limits and complete meal endpoint clients - align course, homework, report, and settings payloads - drive auth validation from server limits --- src/api/__tests__/courses/courses.test.ts | 6 + src/api/__tests__/homework/homework.test.ts | 6 + src/api/__tests__/limits/limits.test.ts | 19 ++ src/api/__tests__/meals/meals.test.ts | 79 +++++++ src/api/__tests__/reports/reports.test.ts | 6 + src/api/__tests__/settings/settings.test.ts | 17 ++ src/api/client/types.ts | 205 ++++++++++++++++++- src/api/courses/getCourses.ts | 25 ++- src/api/homework/getHomework.ts | 12 +- src/api/limits/getLimits.ts | 21 ++ src/api/limits/index.ts | 1 + src/api/meals/deleteMealBookingById.ts | 6 + src/api/meals/deleteMealDishById.ts | 5 + src/api/meals/deleteMealMenuById.ts | 5 + src/api/meals/getDietaryProfileByUserId.ts | 6 + src/api/meals/getMealAttendanceByUserId.ts | 19 ++ src/api/meals/getMealBalanceByUserId.ts | 6 + src/api/meals/getMealLedgerByUserId.ts | 12 ++ src/api/meals/getMealMenuBookings.ts | 15 ++ src/api/meals/getMealMenuById.ts | 6 + src/api/meals/getMealMenus.ts | 14 ++ src/api/meals/getMealServiceRoster.ts | 15 ++ src/api/meals/getMyDietaryProfile.ts | 6 + src/api/meals/getMyMealBalance.ts | 6 + src/api/meals/getMyMealBookings.ts | 8 + src/api/meals/index.ts | 22 ++ src/api/meals/patchDietaryProfileByUserId.ts | 9 + src/api/meals/patchMealDishById.ts | 7 + src/api/meals/patchMealMenuById.ts | 6 + src/api/meals/postMealAttendance.ts | 9 + src/api/meals/postMealBooking.ts | 9 + src/api/meals/postMealCredit.ts | 11 + src/api/meals/postMealDish.ts | 8 + src/api/meals/postMealMenu.ts | 6 + src/api/reports/getMyCourses.ts | 7 +- src/api/settings/patchSettings.ts | 8 +- src/pages/login-page.tsx | 17 +- src/pages/register-page.tsx | 21 +- 38 files changed, 629 insertions(+), 37 deletions(-) create mode 100644 src/api/__tests__/limits/limits.test.ts create mode 100644 src/api/__tests__/meals/meals.test.ts create mode 100644 src/api/limits/getLimits.ts create mode 100644 src/api/limits/index.ts create mode 100644 src/api/meals/deleteMealBookingById.ts create mode 100644 src/api/meals/deleteMealDishById.ts create mode 100644 src/api/meals/deleteMealMenuById.ts create mode 100644 src/api/meals/getDietaryProfileByUserId.ts create mode 100644 src/api/meals/getMealAttendanceByUserId.ts create mode 100644 src/api/meals/getMealBalanceByUserId.ts create mode 100644 src/api/meals/getMealLedgerByUserId.ts create mode 100644 src/api/meals/getMealMenuBookings.ts create mode 100644 src/api/meals/getMealMenuById.ts create mode 100644 src/api/meals/getMealMenus.ts create mode 100644 src/api/meals/getMealServiceRoster.ts create mode 100644 src/api/meals/getMyDietaryProfile.ts create mode 100644 src/api/meals/getMyMealBalance.ts create mode 100644 src/api/meals/getMyMealBookings.ts create mode 100644 src/api/meals/index.ts create mode 100644 src/api/meals/patchDietaryProfileByUserId.ts create mode 100644 src/api/meals/patchMealDishById.ts create mode 100644 src/api/meals/patchMealMenuById.ts create mode 100644 src/api/meals/postMealAttendance.ts create mode 100644 src/api/meals/postMealBooking.ts create mode 100644 src/api/meals/postMealCredit.ts create mode 100644 src/api/meals/postMealDish.ts create mode 100644 src/api/meals/postMealMenu.ts diff --git a/src/api/__tests__/courses/courses.test.ts b/src/api/__tests__/courses/courses.test.ts index 3258d0d..5c21a97 100644 --- a/src/api/__tests__/courses/courses.test.ts +++ b/src/api/__tests__/courses/courses.test.ts @@ -60,6 +60,12 @@ describe("courses API", () => { expect(init?.method).toBe("GET"); }); + it("getCourses sends server-side kind, search, and term filters", async () => { + mockFetchSuccess({ items: [], total: 0 }); + await getCourses({ limit: 12, offset: 24, kind: "course", q: "math", term_id: "none" }); + expect(lastFetchCall()[0]).toBe("/api/courses?limit=12&offset=24&kind=course&q=math&term_id=none"); + }); + it("getCourseById calls /courses/:id", async () => { const mockCourse = { id: "c1", title: "Course" }; mockFetchSuccess(mockCourse); diff --git a/src/api/__tests__/homework/homework.test.ts b/src/api/__tests__/homework/homework.test.ts index 363ffb0..5348943 100644 --- a/src/api/__tests__/homework/homework.test.ts +++ b/src/api/__tests__/homework/homework.test.ts @@ -37,6 +37,12 @@ describe("homework API", () => { expect(init?.method).toBe("GET"); }); + it("getHomework sends due_after", async () => { + mockFetchSuccess({ items: [], total: 0 }); + await getHomework({ limit: 1, due_after: 1_700_000_000_000 }); + expect(lastFetchCall()[0]).toBe("/api/homework?limit=1&due_after=1700000000000"); + }); + it("getHomeworkById calls /homework/:id", async () => { const mockHomework = { id: "hw1" }; mockFetchSuccess(mockHomework); diff --git a/src/api/__tests__/limits/limits.test.ts b/src/api/__tests__/limits/limits.test.ts new file mode 100644 index 0000000..f50dc79 --- /dev/null +++ b/src/api/__tests__/limits/limits.test.ts @@ -0,0 +1,19 @@ +import { afterEach, describe, expect, it, vi } from "vitest"; +import { getLimits, setCachedLimits } from "../../limits"; +import { lastFetchCall, mockFetchSuccess } from "../helpers/mock-fetch"; + +describe("limits API", () => { + afterEach(() => { + vi.restoreAllMocks(); + setCachedLimits(null); + }); + + it("loads /limits once and caches it", async () => { + mockFetchSuccess({ user: { min_username_len: 3 } }); + await getLimits(); + expect(lastFetchCall()[0]).toBe("/api/limits"); + vi.mocked(globalThis.fetch).mockClear(); + await getLimits(); + expect(globalThis.fetch).not.toHaveBeenCalled(); + }); +}); diff --git a/src/api/__tests__/meals/meals.test.ts b/src/api/__tests__/meals/meals.test.ts new file mode 100644 index 0000000..723b376 --- /dev/null +++ b/src/api/__tests__/meals/meals.test.ts @@ -0,0 +1,79 @@ +import { afterEach, describe, expect, it, vi } from "vitest"; +import { + deleteMealBookingById, + deleteMealDishById, + deleteMealMenuById, + getDietaryProfileByUserId, + getMealAttendanceByUserId, + getMealBalanceByUserId, + getMealLedgerByUserId, + getMealMenuBookings, + getMealMenuById, + getMealMenus, + getMealServiceRoster, + getMyDietaryProfile, + getMyMealBalance, + getMyMealBookings, + patchDietaryProfileByUserId, + patchMealDishById, + patchMealMenuById, + postMealAttendance, + postMealBooking, + postMealCredit, + postMealDish, + postMealMenu, +} from "../../meals"; +import { lastFetchCall, mockFetch204, mockFetchSuccess } from "../helpers/mock-fetch"; + +describe("meals API", () => { + afterEach(() => vi.restoreAllMocks()); + const json = () => mockFetchSuccess({}); + const page = () => mockFetchSuccess({ items: [], total: 0 }); + + it("lists menus with dates and pagination", async () => { + page(); await getMealMenus({ from: "2026-07-28", to: "2026-08-01", limit: 12, offset: 12 }); + expect(lastFetchCall()[0]).toBe("/api/meals/menus?limit=12&offset=12&from=2026-07-28&to=2026-08-01"); + }); + it("gets a menu", async () => { json(); await getMealMenuById("m/1"); expect(lastFetchCall()[0]).toBe("/api/meals/menus/m%2F1"); }); + it("publishes a menu", async () => { + json(); const body = { date: "2026-07-28", slot: "lunch", capacity: 10 }; await postMealMenu(body); + expect(lastFetchCall()).toMatchObject(["/api/meals/menus", { method: "POST", body: JSON.stringify(body) }]); + }); + it("patches a menu", async () => { + json(); await patchMealMenuById("m1", { capacity: null }); + expect(lastFetchCall()).toMatchObject(["/api/meals/menus/m1", { method: "PATCH", body: JSON.stringify({ capacity: null }) }]); + }); + it("deletes a menu", async () => { mockFetch204(); await deleteMealMenuById("m1"); expect(lastFetchCall()).toMatchObject(["/api/meals/menus/m1", { method: "DELETE" }]); }); + it("adds a dish", async () => { + json(); const body = { name: "Soup", description: null, price_minor: 4500, tags: ["vegan"] }; await postMealDish("m1", body); + expect(lastFetchCall()).toMatchObject(["/api/meals/menus/m1/dishes", { method: "POST", body: JSON.stringify(body) }]); + }); + it("patches a dish", async () => { json(); await patchMealDishById("d1", { price_minor: 5000 }); expect(lastFetchCall()).toMatchObject(["/api/meals/dishes/d1", { method: "PATCH", body: JSON.stringify({ price_minor: 5000 }) }]); }); + it("deletes a dish", async () => { mockFetch204(); await deleteMealDishById("d1"); expect(lastFetchCall()).toMatchObject(["/api/meals/dishes/d1", { method: "DELETE" }]); }); + it("gets own profile", async () => { json(); await getMyDietaryProfile(); expect(lastFetchCall()[0]).toBe("/api/meals/profiles/me"); }); + it("gets a student profile", async () => { json(); await getDietaryProfileByUserId("u1"); expect(lastFetchCall()[0]).toBe("/api/meals/profiles/u1"); }); + it("patches a student profile", async () => { + json(); const body = { tags: ["nuts"], note: null }; await patchDietaryProfileByUserId("u1", body); + expect(lastFetchCall()).toMatchObject(["/api/meals/profiles/u1", { method: "PATCH", body: JSON.stringify(body) }]); + }); + it("books a child seat", async () => { + json(); await postMealBooking("m1", "u1"); + expect(lastFetchCall()).toMatchObject(["/api/meals/menus/m1/bookings", { method: "POST", body: JSON.stringify({ student_id: "u1" }) }]); + }); + it("lists own bookings", async () => { page(); await getMyMealBookings({ limit: 20 }); expect(lastFetchCall()[0]).toBe("/api/meals/bookings/me?limit=20"); }); + it("cancels a booking", async () => { json(); await deleteMealBookingById("b1"); expect(lastFetchCall()).toMatchObject(["/api/meals/bookings/b1", { method: "DELETE" }]); }); + it("lists booking audit", async () => { page(); await getMealMenuBookings("m1", { limit: 20 }); expect(lastFetchCall()[0]).toBe("/api/meals/menus/m1/bookings?limit=20"); }); + it("lists the service roster", async () => { page(); await getMealServiceRoster("m1", { limit: 20 }); expect(lastFetchCall()[0]).toBe("/api/meals/menus/m1/service-roster?limit=20"); }); + it("marks service", async () => { + json(); await postMealAttendance("m1", "u1", "served"); + expect(lastFetchCall()).toMatchObject(["/api/meals/menus/m1/attendance", { method: "POST", body: JSON.stringify({ student_id: "u1", status: "served" }) }]); + }); + it("gets attendance history", async () => { page(); await getMealAttendanceByUserId("u1", { from: "2026-07-01", limit: 20 }); expect(lastFetchCall()[0]).toBe("/api/meals/attendance/u1?limit=20&from=2026-07-01"); }); + it("gets own balance", async () => { json(); await getMyMealBalance(); expect(lastFetchCall()[0]).toBe("/api/meals/balance/me"); }); + it("gets student balance", async () => { json(); await getMealBalanceByUserId("u1"); expect(lastFetchCall()[0]).toBe("/api/meals/balance/u1"); }); + it("gets student ledger", async () => { page(); await getMealLedgerByUserId("u1", { limit: 20 }); expect(lastFetchCall()[0]).toBe("/api/meals/ledger/u1?limit=20"); }); + it("records append-only credit", async () => { + json(); const body = { student_id: "u1", amount_minor: 10000, method: "cash", note: "receipt" }; await postMealCredit(body); + expect(lastFetchCall()).toMatchObject(["/api/meals/credits", { method: "POST", body: JSON.stringify(body) }]); + }); +}); diff --git a/src/api/__tests__/reports/reports.test.ts b/src/api/__tests__/reports/reports.test.ts index b9cc977..4505a50 100644 --- a/src/api/__tests__/reports/reports.test.ts +++ b/src/api/__tests__/reports/reports.test.ts @@ -23,6 +23,12 @@ describe("reports API", () => { expect(init?.method).toBe("GET"); }); + it("getMyCourses sends course filters", async () => { + mockFetchSuccess({ items: [], total: 0 }); + await getMyCourses({ kind: "club", q: "robot", term_id: "t1" }); + expect(lastFetchCall()[0]).toBe("/api/courses/me?kind=club&q=robot&term_id=t1"); + }); + it("getMyAttendance calls /attendance/me", async () => { const mockAttendance = { courses: [] }; mockFetchSuccess(mockAttendance); diff --git a/src/api/__tests__/settings/settings.test.ts b/src/api/__tests__/settings/settings.test.ts index 9907164..52d6a23 100644 --- a/src/api/__tests__/settings/settings.test.ts +++ b/src/api/__tests__/settings/settings.test.ts @@ -46,4 +46,21 @@ describe("settings API", () => { expect(cached).toEqual(mockSettings); expect(globalThis.fetch).not.toHaveBeenCalled(); }); + + it("patchSettings preserves nullable meal cutoff and expanded policy fields", async () => { + mockFetchSuccess({ meal_cancel_cutoff_minutes: null }); + const updates = { + chatbot_history_turns: 8, + max_chatbot_threads: 20, + max_chatbot_message_len: 3000, + meal_slots: [{ name: "lunch", serving_minute: 540 }], + dietary_tags: ["nuts"], + meal_cancel_cutoff_minutes: null, + }; + await patchSettings(updates); + const [url, init] = lastFetchCall(); + expect(url).toBe("/api/settings"); + expect(init?.method).toBe("PATCH"); + expect(init?.body).toBe(JSON.stringify(updates)); + }); }); diff --git a/src/api/client/types.ts b/src/api/client/types.ts index 832e533..1e545d5 100644 --- a/src/api/client/types.ts +++ b/src/api/client/types.ts @@ -437,24 +437,139 @@ export type GradeBand = { label: string; }; +export type MealSlot = { + name: string; + serving_minute: number | null; +}; + export type SchoolSettings = { exam_kinds: ExamKindSetting[]; attendance_statuses: string[]; grade_bands: GradeBand[]; max_file_bytes: number; + chatbot_history_turns: number; + max_chatbot_threads: number; + max_chatbot_message_len: number; + meal_slots: MealSlot[]; + dietary_tags: string[]; + meal_cancel_cutoff_minutes: number | null; +}; + +export type Limits = { + user: { + min_username_len: number; + max_username_len: number; + username_separators: string[]; + reserved_usernames: string[]; + min_password_len: number; + max_password_len: number; + max_name_len: number; + max_email_len: number; + min_phone_digits: number; + max_phone_digits: number; + roles: Role[]; + themes: UserTheme[]; + languages: UserLanguage[]; + session_duration_days: number; + }; + note: { max_title_len: number; max_content_len: number; max_files: number }; + file: { + max_name_len: number; + max_content_type_len: number; + min_max_file_bytes: number; + max_max_file_bytes: number; + default_max_file_bytes: number; + image_content_types: string[]; + }; + message: { max_subject_len: number; max_body_len: number; max_label_len: number }; + event: { max_title_len: number; max_description_len: number }; + course: { + max_title_len: number; + max_description_len: number; + kinds: CourseKind[]; + max_subject_name_len: number; + max_subject_description_len: number; + max_session_topic_len: number; + max_term_name_len: number; + }; + exam: { + max_title_len: number; + max_description_len: number; + modes: ExamMode[]; + min_duration_ms: number; + max_duration_ms: number; + max_attempts: number; + unlimited_attempts: number; + question_kinds: QuestionKind[]; + max_question_text_len: number; + min_question_points: number; + max_question_points: number; + min_question_choices: number; + max_question_choices: number; + max_choice_text_len: number; + max_answer_text_len: number; + min_mark: number; + max_mark: number; + ws_tick_secs: number; + ws_max_question_id_len: number; + }; + homework: { + max_title_len: number; + max_description_len: number; + max_text_len: number; + max_files_per_submission: number; + max_assigned: number; + statuses: string[]; + }; + question_pool: { max_title_len: number; max_body_len: number; max_solution_body_len: number }; + appointment: { max_note_len: number; max_reason_len: number; max_slot_occurrences: number }; + meal: { + max_dish_name_len: number; + max_dish_description_len: number; + max_dishes_per_menu: number; + max_dish_tags: number; + max_menu_capacity: number; + max_dietary_tags: number; + max_dietary_note_len: number; + max_dish_price_minor: number; + max_ledger_amount_minor: number; + max_ledger_method_len: number; + max_ledger_note_len: number; + max_cancel_cutoff_minutes: number; + max_serving_minute: number; + booking_statuses: string[]; + attendance_statuses: string[]; + ledger_kinds: string[]; + }; + chatbot: { + max_message_len: number; + max_thread_title_len: number; + min_max_message_len: number; + max_max_message_len: number; + default_max_message_len: number; + min_history_turns: number; + max_history_turns: number; + default_history_turns: number; + min_max_threads: number; + max_max_threads: number; + default_max_threads: number; + }; + settings: { + max_list_len: number; + max_item_len: number; + min_exam_kind_weight: number; + max_exam_kind_weight: number; + max_grade_bands: number; + max_grade_label_len: number; + required_attendance_statuses: string[]; + }; + request: { max_page_limit: number; schedule_past_grace_ms: number; request_timeout_secs: number }; + rate: { window_secs: number; auth_per_minute: number; api_per_minute: number; chatbot_per_minute: number }; }; -export const EXAM_KINDS: KnownExamKind[] = [ - "homework", - "quiz", - "midterm", - "final", - "project", - "oral", -]; - +// Fallbacks keep forms usable when the unauthenticated metadata request fails. +export const EXAM_KINDS: KnownExamKind[] = ["homework", "quiz", "midterm", "final", "project", "oral"]; export const EXAM_MODES: ExamMode[] = ["sync", "async", "open"]; - export const QUESTION_KINDS: QuestionKind[] = ["choice", "text"]; export type MessageFolder = "inbox" | "sent" | "archive" | "trash"; @@ -534,3 +649,73 @@ export type HomeworkReportEntry = { missing: boolean; result: HomeworkResult | null; }; + +export type MealDish = { + id: string; + name: string; + description: string | null; + price_minor: number; + tags: string[]; + conflicts: string[]; + created_at: number; +}; + +export type MealMenu = { + id: string; + date: string; + slot: string; + capacity: number | null; + dishes: MealDish[]; + created_by: PersonRef; + created_at: number; +}; + +export type MealBooking = { + id: string; + menu_id: string; + student: PersonRef; + booked_by: PersonRef; + status: string; + cancelled_at: number | null; + created_at: number; +}; + +export type MealAttendance = { + id: string; + menu_id: string; + student: PersonRef; + status: string; + marked_by: PersonRef; + marked_at: number; +}; + +export type MealServiceRosterEntry = { + student: PersonRef; + booking: MealBooking | null; + attendance: MealAttendance | null; +}; + +export type DietaryProfile = { + student: PersonRef; + tags: string[]; + note: string | null; + updated_by: PersonRef | null; + updated_at: number | null; +}; + +export type MealBalance = { + student: PersonRef; + balance_minor: number; +}; + +export type MealLedgerEntry = { + id: string; + student: PersonRef; + kind: string; + amount_minor: number; + source: string | null; + method: string | null; + note: string | null; + recorded_by: PersonRef; + created_at: number; +}; diff --git a/src/api/courses/getCourses.ts b/src/api/courses/getCourses.ts index 8ea0377..0d75887 100644 --- a/src/api/courses/getCourses.ts +++ b/src/api/courses/getCourses.ts @@ -1,8 +1,25 @@ import { client } from "../client"; -import { normalizePage, pageQuery, type Page, type PageParams } from "../client"; -import type { Course } from "../client"; +import { appendPageParams, normalizePage, type Page, type PageParams } from "../client"; +import type { Course, CourseKind } from "../client"; -export async function getCourses(params?: PageParams, signal?: AbortSignal): Promise> { - const data = await client(`/courses${pageQuery(params)}`, { signal }); +export type CourseListParams = PageParams & { + kind?: CourseKind; + q?: string; + term_id?: string; +}; + +export function courseListQuery(params?: CourseListParams): string { + if (!params) return ""; + const query = new URLSearchParams(); + appendPageParams(query, params); + if (params.kind) query.set("kind", params.kind); + if (params.q) query.set("q", params.q); + if (params.term_id) query.set("term_id", params.term_id); + const value = query.toString(); + return value ? `?${value}` : ""; +} + +export async function getCourses(params?: CourseListParams, signal?: AbortSignal): Promise> { + const data = await client(`/courses${courseListQuery(params)}`, { signal }); return normalizePage(data); } diff --git a/src/api/homework/getHomework.ts b/src/api/homework/getHomework.ts index b24c64f..0c11ea3 100644 --- a/src/api/homework/getHomework.ts +++ b/src/api/homework/getHomework.ts @@ -1,8 +1,14 @@ import { client } from "../client"; -import { normalizePage, pageQuery, type Page, type PageParams } from "../client"; +import { appendPageParams, normalizePage, type Page, type PageParams } from "../client"; import type { Homework } from "../client"; -export async function getHomework(params?: PageParams, signal?: AbortSignal): Promise> { - const data = await client(`/homework${pageQuery(params)}`, { signal }); +export type HomeworkListParams = PageParams & { due_after?: number }; + +export async function getHomework(params?: HomeworkListParams, signal?: AbortSignal): Promise> { + const query = new URLSearchParams(); + appendPageParams(query, params); + if (params?.due_after != null) query.set("due_after", String(params.due_after)); + const suffix = query.size ? `?${query}` : ""; + const data = await client(`/homework${suffix}`, { signal }); return normalizePage(data); } diff --git a/src/api/limits/getLimits.ts b/src/api/limits/getLimits.ts new file mode 100644 index 0000000..f3872ad --- /dev/null +++ b/src/api/limits/getLimits.ts @@ -0,0 +1,21 @@ +import { client } from "../client"; +import type { Limits } from "../client"; + +let cachedLimits: Limits | null = null; +let pendingLimits: Promise | null = null; + +export function getLimits(signal?: AbortSignal): Promise { + if (cachedLimits) return Promise.resolve(cachedLimits); + if (pendingLimits) return pendingLimits; + pendingLimits = client("/limits", { signal }) + .then((limits) => (cachedLimits = limits)) + .finally(() => { + pendingLimits = null; + }); + return pendingLimits; +} + +export function setCachedLimits(limits: Limits | null): void { + cachedLimits = limits; + pendingLimits = null; +} diff --git a/src/api/limits/index.ts b/src/api/limits/index.ts new file mode 100644 index 0000000..1d07f03 --- /dev/null +++ b/src/api/limits/index.ts @@ -0,0 +1 @@ +export * from "./getLimits"; diff --git a/src/api/meals/deleteMealBookingById.ts b/src/api/meals/deleteMealBookingById.ts new file mode 100644 index 0000000..a541f3e --- /dev/null +++ b/src/api/meals/deleteMealBookingById.ts @@ -0,0 +1,6 @@ +import { client } from "../client"; +import type { MealBooking } from "../client"; + +export function deleteMealBookingById(id: string): Promise { + return client(`/meals/bookings/${encodeURIComponent(id)}`, { method: "DELETE" }); +} diff --git a/src/api/meals/deleteMealDishById.ts b/src/api/meals/deleteMealDishById.ts new file mode 100644 index 0000000..c637491 --- /dev/null +++ b/src/api/meals/deleteMealDishById.ts @@ -0,0 +1,5 @@ +import { client } from "../client"; + +export function deleteMealDishById(id: string): Promise { + return client(`/meals/dishes/${encodeURIComponent(id)}`, { method: "DELETE" }); +} diff --git a/src/api/meals/deleteMealMenuById.ts b/src/api/meals/deleteMealMenuById.ts new file mode 100644 index 0000000..5769306 --- /dev/null +++ b/src/api/meals/deleteMealMenuById.ts @@ -0,0 +1,5 @@ +import { client } from "../client"; + +export function deleteMealMenuById(id: string): Promise { + return client(`/meals/menus/${encodeURIComponent(id)}`, { method: "DELETE" }); +} diff --git a/src/api/meals/getDietaryProfileByUserId.ts b/src/api/meals/getDietaryProfileByUserId.ts new file mode 100644 index 0000000..57d18bc --- /dev/null +++ b/src/api/meals/getDietaryProfileByUserId.ts @@ -0,0 +1,6 @@ +import { client } from "../client"; +import type { DietaryProfile } from "../client"; + +export function getDietaryProfileByUserId(userId: string, signal?: AbortSignal): Promise { + return client(`/meals/profiles/${encodeURIComponent(userId)}`, { signal }); +} diff --git a/src/api/meals/getMealAttendanceByUserId.ts b/src/api/meals/getMealAttendanceByUserId.ts new file mode 100644 index 0000000..5d72be5 --- /dev/null +++ b/src/api/meals/getMealAttendanceByUserId.ts @@ -0,0 +1,19 @@ +import { client } from "../client"; +import { appendPageParams, normalizePage, type Page, type PageParams } from "../client"; +import type { MealAttendance } from "../client"; + +export async function getMealAttendanceByUserId( + userId: string, + params?: PageParams & { from?: string; to?: string }, + signal?: AbortSignal, +): Promise> { + const query = new URLSearchParams(); + appendPageParams(query, params); + if (params?.from) query.set("from", params.from); + if (params?.to) query.set("to", params.to); + const data = await client( + `/meals/attendance/${encodeURIComponent(userId)}${query.size ? `?${query}` : ""}`, + { signal }, + ); + return normalizePage(data); +} diff --git a/src/api/meals/getMealBalanceByUserId.ts b/src/api/meals/getMealBalanceByUserId.ts new file mode 100644 index 0000000..f95470e --- /dev/null +++ b/src/api/meals/getMealBalanceByUserId.ts @@ -0,0 +1,6 @@ +import { client } from "../client"; +import type { MealBalance } from "../client"; + +export function getMealBalanceByUserId(userId: string, signal?: AbortSignal): Promise { + return client(`/meals/balance/${encodeURIComponent(userId)}`, { signal }); +} diff --git a/src/api/meals/getMealLedgerByUserId.ts b/src/api/meals/getMealLedgerByUserId.ts new file mode 100644 index 0000000..3876e39 --- /dev/null +++ b/src/api/meals/getMealLedgerByUserId.ts @@ -0,0 +1,12 @@ +import { client } from "../client"; +import { normalizePage, pageQuery, type Page, type PageParams } from "../client"; +import type { MealLedgerEntry } from "../client"; + +export async function getMealLedgerByUserId( + userId: string, + params?: PageParams, + signal?: AbortSignal, +): Promise> { + const data = await client(`/meals/ledger/${encodeURIComponent(userId)}${pageQuery(params)}`, { signal }); + return normalizePage(data); +} diff --git a/src/api/meals/getMealMenuBookings.ts b/src/api/meals/getMealMenuBookings.ts new file mode 100644 index 0000000..70168bd --- /dev/null +++ b/src/api/meals/getMealMenuBookings.ts @@ -0,0 +1,15 @@ +import { client } from "../client"; +import { normalizePage, pageQuery, type Page, type PageParams } from "../client"; +import type { MealBooking } from "../client"; + +export async function getMealMenuBookings( + menuId: string, + params?: PageParams, + signal?: AbortSignal, +): Promise> { + const data = await client( + `/meals/menus/${encodeURIComponent(menuId)}/bookings${pageQuery(params)}`, + { signal }, + ); + return normalizePage(data); +} diff --git a/src/api/meals/getMealMenuById.ts b/src/api/meals/getMealMenuById.ts new file mode 100644 index 0000000..c8bb1e3 --- /dev/null +++ b/src/api/meals/getMealMenuById.ts @@ -0,0 +1,6 @@ +import { client } from "../client"; +import type { MealMenu } from "../client"; + +export function getMealMenuById(id: string, signal?: AbortSignal): Promise { + return client(`/meals/menus/${encodeURIComponent(id)}`, { signal }); +} diff --git a/src/api/meals/getMealMenus.ts b/src/api/meals/getMealMenus.ts new file mode 100644 index 0000000..e8fda8f --- /dev/null +++ b/src/api/meals/getMealMenus.ts @@ -0,0 +1,14 @@ +import { client } from "../client"; +import { appendPageParams, normalizePage, type Page, type PageParams } from "../client"; +import type { MealMenu } from "../client"; + +export type MealMenuParams = PageParams & { from?: string; to?: string }; + +export async function getMealMenus(params?: MealMenuParams, signal?: AbortSignal): Promise> { + const query = new URLSearchParams(); + appendPageParams(query, params); + if (params?.from) query.set("from", params.from); + if (params?.to) query.set("to", params.to); + const data = await client(`/meals/menus${query.size ? `?${query}` : ""}`, { signal }); + return normalizePage(data); +} diff --git a/src/api/meals/getMealServiceRoster.ts b/src/api/meals/getMealServiceRoster.ts new file mode 100644 index 0000000..abaeb23 --- /dev/null +++ b/src/api/meals/getMealServiceRoster.ts @@ -0,0 +1,15 @@ +import { client } from "../client"; +import { normalizePage, pageQuery, type Page, type PageParams } from "../client"; +import type { MealServiceRosterEntry } from "../client"; + +export async function getMealServiceRoster( + menuId: string, + params?: PageParams, + signal?: AbortSignal, +): Promise> { + const data = await client( + `/meals/menus/${encodeURIComponent(menuId)}/service-roster${pageQuery(params)}`, + { signal }, + ); + return normalizePage(data); +} diff --git a/src/api/meals/getMyDietaryProfile.ts b/src/api/meals/getMyDietaryProfile.ts new file mode 100644 index 0000000..cbe7507 --- /dev/null +++ b/src/api/meals/getMyDietaryProfile.ts @@ -0,0 +1,6 @@ +import { client } from "../client"; +import type { DietaryProfile } from "../client"; + +export function getMyDietaryProfile(signal?: AbortSignal): Promise { + return client("/meals/profiles/me", { signal }); +} diff --git a/src/api/meals/getMyMealBalance.ts b/src/api/meals/getMyMealBalance.ts new file mode 100644 index 0000000..2889076 --- /dev/null +++ b/src/api/meals/getMyMealBalance.ts @@ -0,0 +1,6 @@ +import { client } from "../client"; +import type { MealBalance } from "../client"; + +export function getMyMealBalance(signal?: AbortSignal): Promise { + return client("/meals/balance/me", { signal }); +} diff --git a/src/api/meals/getMyMealBookings.ts b/src/api/meals/getMyMealBookings.ts new file mode 100644 index 0000000..1a89022 --- /dev/null +++ b/src/api/meals/getMyMealBookings.ts @@ -0,0 +1,8 @@ +import { client } from "../client"; +import { normalizePage, pageQuery, type Page, type PageParams } from "../client"; +import type { MealBooking } from "../client"; + +export async function getMyMealBookings(params?: PageParams, signal?: AbortSignal): Promise> { + const data = await client(`/meals/bookings/me${pageQuery(params)}`, { signal }); + return normalizePage(data); +} diff --git a/src/api/meals/index.ts b/src/api/meals/index.ts new file mode 100644 index 0000000..ba1faa5 --- /dev/null +++ b/src/api/meals/index.ts @@ -0,0 +1,22 @@ +export * from "./deleteMealBookingById"; +export * from "./deleteMealDishById"; +export * from "./deleteMealMenuById"; +export * from "./getDietaryProfileByUserId"; +export * from "./getMealAttendanceByUserId"; +export * from "./getMealBalanceByUserId"; +export * from "./getMealLedgerByUserId"; +export * from "./getMealMenuById"; +export * from "./getMealMenuBookings"; +export * from "./getMealMenus"; +export * from "./getMealServiceRoster"; +export * from "./getMyDietaryProfile"; +export * from "./getMyMealBalance"; +export * from "./getMyMealBookings"; +export * from "./patchDietaryProfileByUserId"; +export * from "./patchMealDishById"; +export * from "./patchMealMenuById"; +export * from "./postMealAttendance"; +export * from "./postMealBooking"; +export * from "./postMealCredit"; +export * from "./postMealDish"; +export * from "./postMealMenu"; diff --git a/src/api/meals/patchDietaryProfileByUserId.ts b/src/api/meals/patchDietaryProfileByUserId.ts new file mode 100644 index 0000000..d733d19 --- /dev/null +++ b/src/api/meals/patchDietaryProfileByUserId.ts @@ -0,0 +1,9 @@ +import { client } from "../client"; +import type { DietaryProfile } from "../client"; + +export function patchDietaryProfileByUserId( + userId: string, + body: { tags?: string[]; note?: string | null }, +): Promise { + return client(`/meals/profiles/${encodeURIComponent(userId)}`, { method: "PATCH", body }); +} diff --git a/src/api/meals/patchMealDishById.ts b/src/api/meals/patchMealDishById.ts new file mode 100644 index 0000000..7021a31 --- /dev/null +++ b/src/api/meals/patchMealDishById.ts @@ -0,0 +1,7 @@ +import { client } from "../client"; +import type { MealDish } from "../client"; +import type { MealDishBody } from "./postMealDish"; + +export function patchMealDishById(id: string, body: Partial): Promise { + return client(`/meals/dishes/${encodeURIComponent(id)}`, { method: "PATCH", body }); +} diff --git a/src/api/meals/patchMealMenuById.ts b/src/api/meals/patchMealMenuById.ts new file mode 100644 index 0000000..f00c45f --- /dev/null +++ b/src/api/meals/patchMealMenuById.ts @@ -0,0 +1,6 @@ +import { client } from "../client"; +import type { MealMenu } from "../client"; + +export function patchMealMenuById(id: string, body: { capacity?: number | null }): Promise { + return client(`/meals/menus/${encodeURIComponent(id)}`, { method: "PATCH", body }); +} diff --git a/src/api/meals/postMealAttendance.ts b/src/api/meals/postMealAttendance.ts new file mode 100644 index 0000000..338bd79 --- /dev/null +++ b/src/api/meals/postMealAttendance.ts @@ -0,0 +1,9 @@ +import { client } from "../client"; +import type { MealAttendance } from "../client"; + +export function postMealAttendance(menuId: string, studentId: string, status: string): Promise { + return client(`/meals/menus/${encodeURIComponent(menuId)}/attendance`, { + method: "POST", + body: { student_id: studentId, status }, + }); +} diff --git a/src/api/meals/postMealBooking.ts b/src/api/meals/postMealBooking.ts new file mode 100644 index 0000000..5310c07 --- /dev/null +++ b/src/api/meals/postMealBooking.ts @@ -0,0 +1,9 @@ +import { client } from "../client"; +import type { MealBooking } from "../client"; + +export function postMealBooking(menuId: string, studentId?: string): Promise { + return client(`/meals/menus/${encodeURIComponent(menuId)}/bookings`, { + method: "POST", + body: studentId ? { student_id: studentId } : {}, + }); +} diff --git a/src/api/meals/postMealCredit.ts b/src/api/meals/postMealCredit.ts new file mode 100644 index 0000000..ce2ffd6 --- /dev/null +++ b/src/api/meals/postMealCredit.ts @@ -0,0 +1,11 @@ +import { client } from "../client"; +import type { MealLedgerEntry } from "../client"; + +export function postMealCredit(body: { + student_id: string; + amount_minor: number; + method?: string; + note?: string; +}): Promise { + return client("/meals/credits", { method: "POST", body }); +} diff --git a/src/api/meals/postMealDish.ts b/src/api/meals/postMealDish.ts new file mode 100644 index 0000000..0754c4d --- /dev/null +++ b/src/api/meals/postMealDish.ts @@ -0,0 +1,8 @@ +import { client } from "../client"; +import type { MealDish } from "../client"; + +export type MealDishBody = { name: string; description?: string | null; price_minor: number; tags: string[] }; + +export function postMealDish(menuId: string, body: MealDishBody): Promise { + return client(`/meals/menus/${encodeURIComponent(menuId)}/dishes`, { method: "POST", body }); +} diff --git a/src/api/meals/postMealMenu.ts b/src/api/meals/postMealMenu.ts new file mode 100644 index 0000000..f3bc2dd --- /dev/null +++ b/src/api/meals/postMealMenu.ts @@ -0,0 +1,6 @@ +import { client } from "../client"; +import type { MealMenu } from "../client"; + +export function postMealMenu(body: { date: string; slot: string; capacity?: number | null }): Promise { + return client("/meals/menus", { method: "POST", body }); +} diff --git a/src/api/reports/getMyCourses.ts b/src/api/reports/getMyCourses.ts index 817a123..1ef1c1b 100644 --- a/src/api/reports/getMyCourses.ts +++ b/src/api/reports/getMyCourses.ts @@ -1,8 +1,9 @@ import { client } from "../client"; -import { normalizePage, pageQuery, type Page, type PageParams } from "../client"; +import { normalizePage, type Page } from "../client"; import type { Course } from "../client"; +import { courseListQuery, type CourseListParams } from "../courses/getCourses"; -export async function getMyCourses(params?: PageParams, signal?: AbortSignal): Promise> { - const data = await client(`/courses/me${pageQuery(params)}`, { signal }); +export async function getMyCourses(params?: CourseListParams, signal?: AbortSignal): Promise> { + const data = await client(`/courses/me${courseListQuery(params)}`, { signal }); return normalizePage(data); } diff --git a/src/api/settings/patchSettings.ts b/src/api/settings/patchSettings.ts index 6ed5165..c53373e 100644 --- a/src/api/settings/patchSettings.ts +++ b/src/api/settings/patchSettings.ts @@ -1,12 +1,18 @@ import { client } from "../client"; import { setCachedSettings } from "./getSettings"; -import type { ExamKindSetting, GradeBand, SchoolSettings } from "../client"; +import type { ExamKindSetting, GradeBand, MealSlot, SchoolSettings } from "../client"; export type PatchSettingsBody = { exam_kinds?: ExamKindSetting[]; attendance_statuses?: string[]; grade_bands?: GradeBand[]; max_file_bytes?: number; + chatbot_history_turns?: number; + max_chatbot_threads?: number; + max_chatbot_message_len?: number; + meal_slots?: MealSlot[]; + dietary_tags?: string[]; + meal_cancel_cutoff_minutes?: number | null; }; export function patchSettings(body: PatchSettingsBody): Promise { diff --git a/src/pages/login-page.tsx b/src/pages/login-page.tsx index aa0c521..9146b11 100644 --- a/src/pages/login-page.tsx +++ b/src/pages/login-page.tsx @@ -1,6 +1,7 @@ import { Link, useNavigate } from "@tanstack/solid-router"; -import { createSignal, Show } from "solid-js"; +import { createResource, createSignal, Show } from "solid-js"; import { postLogin } from "@/api/auth"; +import { getLimits } from "@/api/limits"; import { formatApiError } from "@/api/client"; import { Button } from "@/components/ui/button"; import { Input } from "@/components/ui/input"; @@ -27,16 +28,18 @@ function LoginForm() { const [showPassword, setShowPassword] = createSignal(false); const [error, setError] = createSignal(""); const [pending, setPending] = createSignal(false); + const [limits] = createResource(() => getLimits().catch(() => null)); const handleSubmit = async (e: SubmitEvent) => { e.preventDefault(); const u = username().trim(); const p = password(); - if (u.length < 3 || u.length > 32) { + const userLimits = limits()?.user; + if (userLimits && (u.length < userLimits.min_username_len || u.length > userLimits.max_username_len)) { setError(t("auth.usernameHint")); return; } - if (p.length < 6 || p.length > 128) { + if (userLimits && (p.length < userLimits.min_password_len || p.length > userLimits.max_password_len)) { setError(t("auth.passwordHint")); return; } @@ -68,8 +71,8 @@ function LoginForm() { id="login-username" class="h-11" autocomplete="username" - minlength={3} - maxlength={32} + minlength={limits()?.user.min_username_len} + maxlength={limits()?.user.max_username_len} required value={username()} onInput={(e) => setUsername(e.currentTarget.value)} @@ -84,8 +87,8 @@ function LoginForm() { class="h-11 pr-10" type={showPassword() ? "text" : "password"} autocomplete="current-password" - minlength={6} - maxlength={128} + minlength={limits()?.user.min_password_len} + maxlength={limits()?.user.max_password_len} required value={password()} onInput={(e) => setPassword(e.currentTarget.value)} diff --git a/src/pages/register-page.tsx b/src/pages/register-page.tsx index 9a38c56..683a32f 100644 --- a/src/pages/register-page.tsx +++ b/src/pages/register-page.tsx @@ -1,6 +1,7 @@ import { Link, useNavigate } from "@tanstack/solid-router"; -import { createSignal, Show } from "solid-js"; +import { createResource, createSignal, Show } from "solid-js"; import { postRegister } from "@/api/auth"; +import { getLimits } from "@/api/limits"; import { formatApiError } from "@/api/client"; import { Button } from "@/components/ui/button"; import { Input } from "@/components/ui/input"; @@ -26,17 +27,19 @@ function RegisterForm() { const [showPassword, setShowPassword] = createSignal(false); const [error, setError] = createSignal(""); const [pending, setPending] = createSignal(false); + const [limits] = createResource(() => getLimits().catch(() => null)); const handleSubmit = async (e: SubmitEvent) => { e.preventDefault(); const u = username().trim(); const p = password(); const cp = confirmPassword(); - if (u.length < 3 || u.length > 32) { + const userLimits = limits()?.user; + if (userLimits && (u.length < userLimits.min_username_len || u.length > userLimits.max_username_len)) { setError(t("auth.usernameHint")); return; } - if (p.length < 6 || p.length > 128) { + if (userLimits && (p.length < userLimits.min_password_len || p.length > userLimits.max_password_len)) { setError(t("auth.passwordHint")); return; } @@ -71,8 +74,8 @@ function RegisterForm() { id="register-username" class="h-11" autocomplete="username" - minlength={3} - maxlength={32} + minlength={limits()?.user.min_username_len} + maxlength={limits()?.user.max_username_len} required value={username()} onInput={(e) => setUsername(e.currentTarget.value)} @@ -87,8 +90,8 @@ function RegisterForm() { class="h-11 pr-10" type={showPassword() ? "text" : "password"} autocomplete="new-password" - minlength={6} - maxlength={128} + minlength={limits()?.user.min_password_len} + maxlength={limits()?.user.max_password_len} required value={password()} onInput={(e) => setPassword(e.currentTarget.value)} @@ -113,8 +116,8 @@ function RegisterForm() { class="h-11" type={showPassword() ? "text" : "password"} autocomplete="new-password" - minlength={6} - maxlength={128} + minlength={limits()?.user.min_password_len} + maxlength={limits()?.user.max_password_len} required value={confirmPassword()} onInput={(e) => setConfirmPassword(e.currentTarget.value)}