From a6c49741fac720c1b8b1a2fe7014861415aa3bfc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Fri, 18 Sep 2026 02:33:38 +0300 Subject: [PATCH] =?UTF-8?q?fix:=20=F0=9F=8E=93=20open=20the=20class=20the?= =?UTF-8?q?=20create=20envelope=20names?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - POST /classes answers an envelope — { class, skipped, stocked_from } — but postClass was typed as the bare ClassGroup, so a create navigated to /management/classes/undefined and the route 404'd on a class that existed - CreateClassResponse joins the client types; the page reads created.class.id - a create whose grade's blueprint left pairs behind now reports them first: the amber skipped alert plus the skipped-pairs panel, with the course titles read once up front, and closing the panel opens the new class - the classes API test mocks the real envelope and asserts it comes back - the live contract suite read the same response as a bare class in two fixtures, so both now assert `skipped` is an array and take the id from `class` — the suite was outside the src/ client sweep - a new page test proves the create handler navigates to the created id, and holds the page while the skip report is open --- src/api/__tests__/classes/classes.test.ts | 13 ++- .../contract/bank-questions.contract.test.ts | 7 +- .../contract/learning.contract.test.ts | 7 +- src/api/classes/postClass.ts | 9 +- src/api/client/types.ts | 7 ++ src/pages/classes-page.test.tsx | 88 +++++++++++++++++++ src/pages/classes-page.tsx | 58 +++++++++++- 7 files changed, 176 insertions(+), 13 deletions(-) create mode 100644 src/pages/classes-page.test.tsx diff --git a/src/api/__tests__/classes/classes.test.ts b/src/api/__tests__/classes/classes.test.ts index da5f2a8..dbe84b9 100644 --- a/src/api/__tests__/classes/classes.test.ts +++ b/src/api/__tests__/classes/classes.test.ts @@ -28,10 +28,15 @@ describe("classes API", () => { vi.restoreAllMocks(); }); - it("postClass POSTs /classes with the body", async () => { - mockFetchSuccess({ id: "c1" }); - const body = { name: "9-A", grade: "9", term_id: "t1" }; - await postClass(body); + // POST /classes answers with an envelope, not the bare class: the created + // row plus the pairs its grade's blueprint could not attach. + it("postClass POSTs /classes and returns the create envelope", async () => { + const envelope = { class: { id: "c1" }, skipped: [], stocked_from: null }; + mockFetchSuccess(envelope); + const body = { name: "9-A", grade: "9", year: "y1" }; + const created = await postClass(body); + expect(created).toEqual(envelope); + expect(created.class.id).toBe("c1"); const [url, init] = lastFetchCall(); expect(url).toBe("/api/classes"); expect(init?.method).toBe("POST"); diff --git a/src/api/__tests__/contract/bank-questions.contract.test.ts b/src/api/__tests__/contract/bank-questions.contract.test.ts index 7139455..137affd 100644 --- a/src/api/__tests__/contract/bank-questions.contract.test.ts +++ b/src/api/__tests__/contract/bank-questions.contract.test.ts @@ -28,11 +28,14 @@ describe.skipIf(!isLive)(`bank-questions contract @ ${contractBaseUrl}`, () => { body: { name: tag, year: yearId, starts_at: Date.now(), ends_at: Date.now() + 120 * 24 * 60 * 60 * 1000 }, }); termId = term.id; - const klass = await json<{ id: string }>("/classes", { + // POST /classes answers with a create envelope, not the bare şube: its + // `class` is the row, and `skipped` is the pair report (empty here). + const created = await json<{ class: { id: string }; skipped: unknown[] }>("/classes", { method: "POST", body: { name: tag, grade: "9", year: yearId }, }); - classId = klass.id; + classId = created.class.id; + expect(Array.isArray(created.skipped)).toBe(true); const course = await json("/courses", { method: "POST", diff --git a/src/api/__tests__/contract/learning.contract.test.ts b/src/api/__tests__/contract/learning.contract.test.ts index 3fccb5d..fde504f 100644 --- a/src/api/__tests__/contract/learning.contract.test.ts +++ b/src/api/__tests__/contract/learning.contract.test.ts @@ -30,11 +30,14 @@ describe.skipIf(!isLive)(`learning contract @ ${contractBaseUrl}`, () => { termId = term.id; expect(term.year).toBe(yearId); - const klass = await json<{ id: string }>("/classes", { + // POST /classes answers with a create envelope, not the bare şube: its + // `class` is the row, and `skipped` is the pair report (empty here). + const created = await json<{ class: { id: string }; skipped: unknown[] }>("/classes", { method: "POST", body: { name: tag, grade: "9", year: yearId }, }); - classId = klass.id; + classId = created.class.id; + expect(Array.isArray(created.skipped)).toBe(true); const course = await json<{ id: string; title: string; kind: string }>("/courses", { method: "POST", diff --git a/src/api/classes/postClass.ts b/src/api/classes/postClass.ts index 4a2423e..aa9c2e4 100644 --- a/src/api/classes/postClass.ts +++ b/src/api/classes/postClass.ts @@ -1,10 +1,13 @@ import { client } from "../client"; -import type { ClassGroup } from "../client"; +import type { CreateClassResponse } from "../client"; // `year` is the academic year (`getAcademicYears`) — what binds the şube to a // karne and to the rollover. A şube with no year takes no exam. export type CreateClassBody = { name: string; grade?: string; year?: string; teacher_id?: string }; -export function postClass(body: CreateClassBody): Promise { - return client("/classes", { method: "POST", body }); +// The 201 is a create envelope, not the bare class: its `class` is what the +// route navigates to, and its `skipped` is the blueprint fallout the caller +// has to report. +export function postClass(body: CreateClassBody): Promise { + return client("/classes", { method: "POST", body }); } diff --git a/src/api/client/types.ts b/src/api/client/types.ts index f9be285..ebbe5de 100644 --- a/src/api/client/types.ts +++ b/src/api/client/types.ts @@ -403,6 +403,13 @@ export type ClassGroup = { teacher: PersonRef | null; }; +// What POST /classes answers with: the created class, the (class, course) +// pairs its grade's blueprint could not attach, and the grade of the blueprint +// that stocked it. `skipped` is empty when the template took every course, and +// always empty when no template covered the grade; `stocked_from` is null in +// that second case. +export type CreateClassResponse = { class: ClassGroup; skipped: BlueprintSkip[]; stocked_from: string | null }; + export type ClassMember = { id: string; class: string; diff --git a/src/pages/classes-page.test.tsx b/src/pages/classes-page.test.tsx new file mode 100644 index 0000000..0e89f43 --- /dev/null +++ b/src/pages/classes-page.test.tsx @@ -0,0 +1,88 @@ +import { cleanup, fireEvent, render, screen, waitFor } from "@solidjs/testing-library"; +import type { JSX } from "solid-js"; +import { afterEach, expect, test, vi } from "vitest"; +import ClassesPage from "@/pages/classes-page"; +import { PreferencesProvider } from "@/stores/preferences-context"; + +const { getClasses, getClassMembers, postClass, getCourses, navigate } = vi.hoisted(() => ({ + getClasses: vi.fn(), + getClassMembers: vi.fn(), + postClass: vi.fn(), + getCourses: vi.fn(), + navigate: vi.fn(), +})); + +vi.mock("@tanstack/solid-router", () => ({ + Link: (props: { to: string; children: JSX.Element; class?: string }) => {props.children}, + Navigate: () => null, + useNavigate: () => navigate, +})); +vi.mock("@/stores/auth-context", () => ({ + useAuth: () => ({ + user: () => ({ id: "u-1", username: "boss", role: "manager" }), + loading: () => false, + error: () => null, + refresh: vi.fn(), + }), +})); +vi.mock("@/api/classes", () => ({ getClasses, getClassMembers, postClass })); +vi.mock("@/api/academic-years", () => ({ getAcademicYears: vi.fn(async () => ({ items: [] })) })); +vi.mock("@/api/limits", () => ({ + getLimits: vi.fn(async () => ({ course: { max_class_name_len: 60, max_class_grade_len: 10 } })), +})); +vi.mock("@/api/courses", () => ({ getCourses })); +vi.mock("@/api/users", () => ({ getUserSearch: vi.fn(async () => ({ items: [] })) })); + +const createdClass = { id: "c-new", creator: null, name: "9-A", grade: "9", year: null, teacher: null }; + +afterEach(() => { + cleanup(); + vi.clearAllMocks(); +}); + +async function openCreateForm() { + getClasses.mockResolvedValue({ items: [] }); + getClassMembers.mockResolvedValue({ items: [], total: 0 }); + render(() => ); + + fireEvent.click(await screen.findByRole("button", { name: /New class/ })); + fireEvent.input(await screen.findByLabelText(/^Name/), { target: { value: "9-A" } }); + fireEvent.click(screen.getByRole("button", { name: "Create" })); +} + +// The 201 is an envelope, not the class: opening the route with `undefined` +// 404s the class it just made. +test("creating a class opens the class the server made", async () => { + postClass.mockResolvedValue({ class: createdClass, skipped: [], stocked_from: null }); + + await openCreateForm(); + + await waitFor(() => + expect(navigate).toHaveBeenCalledWith({ to: "/management/classes/$id", params: { id: "c-new" } }), + ); + expect(postClass).toHaveBeenCalledWith({ name: "9-A", grade: undefined, year: undefined, teacher_id: undefined }); +}); + +test("a create whose blueprint left pairs behind holds the page until the report closes", async () => { + postClass.mockResolvedValue({ + class: createdClass, + skipped: [{ class: "c-new", class_name: "9-A", course: "k1", reason: "the class is already at its course ceiling" }], + stocked_from: "9", + }); + getCourses.mockResolvedValue({ items: [{ id: "k1", title: "Algebra" }] }); + + await openCreateForm(); + + expect(await screen.findByText(/could not be attached/)).toBeTruthy(); + expect(navigate).not.toHaveBeenCalled(); + + fireEvent.click(screen.getByRole("button", { name: "See details" })); + expect(await screen.findByText("Not attached")).toBeTruthy(); + expect(await screen.findByText("Algebra")).toBeTruthy(); + + fireEvent.click(screen.getByRole("button", { name: "Close" })); + + await waitFor(() => + expect(navigate).toHaveBeenCalledWith({ to: "/management/classes/$id", params: { id: "c-new" } }), + ); +}); diff --git a/src/pages/classes-page.tsx b/src/pages/classes-page.tsx index d1d5d9b..5a0a36e 100644 --- a/src/pages/classes-page.tsx +++ b/src/pages/classes-page.tsx @@ -3,8 +3,10 @@ import { createResource } from "@/lib/create-resource"; import { useNavigate } from "@tanstack/solid-router"; import { getClasses, getClassMembers, postClass } from "@/api/classes"; import { getAcademicYears } from "@/api/academic-years"; +import { getCourses } from "@/api/courses"; import { getLimits } from "@/api/limits"; -import { formatApiError, type ClassGroup } from "@/api/client"; +import { formatApiError, type BlueprintSkip, type ClassGroup } from "@/api/client"; +import { BlueprintSkippedReport } from "@/components/classes/blueprint-skipped-report"; import { RouteGuard } from "@/components/layout/route-guard"; import { DataTableSearch } from "@/components/ui/data-table-search"; import { DataSection } from "@/components/ui/data-section"; @@ -56,11 +58,18 @@ function ClassesContent() { const [teacherId, setTeacherId] = createSignal(""); const [error, setError] = createSignal(""); const [pending, setPending] = createSignal(false); + // A create whose grade's blueprint left pairs behind reports them here before + // it opens the class: the response is the only place those rows ever appear. + const [createdClass, setCreatedClass] = createSignal(null); + const [skipped, setSkipped] = createSignal([]); + const [skippedCourseTitles, setSkippedCourseTitles] = createSignal>({}); + const [reportOpen, setReportOpen] = createSignal(false); const [flash, setFlash] = createFlash(); const [years] = createResource(async () => (await getAcademicYears({ limit: 100 })).items); const [limits] = createResource(() => canManage() ? getLimits() : null); const [list, { refetch }] = createResource(async () => (await getClasses()).items); + const courseTitle = (id: string) => skippedCourseTitles()[id] ?? id; const listData = () => list.latest ?? list() ?? []; const yearName = (id: string | null) => years.latest?.find((year) => year.id === id)?.name ?? (id || t("academicYears.unassigned")); @@ -125,7 +134,23 @@ function ClassesContent() { // The list reloads on the next visit; the class was created. } setFlash(t("common.created")); - void navigate({ to: "/management/classes/$id", params: { id: created.id } }); + if (created.skipped.length > 0) { + // The class exists either way; its grade's template just could not + // take every course. The report names each skipped course, so the one + // catalog read happens before the panel can open; a failed read leaves + // the ids in place rather than holding the report back. + try { + const courses = (await getCourses({ limit: 200 })).items; + setSkippedCourseTitles(Object.fromEntries(courses.map((course) => [course.id, course.title]))); + } catch { + // The report still opens; the rows fall back to course ids. + } + setCreatedClass(created.class); + setSkipped(created.skipped); + setReportOpen(true); + return; + } + void navigate({ to: "/management/classes/$id", params: { id: created.class.id } }); } catch (err) { setError(formatApiError(err)); } finally { @@ -133,6 +158,19 @@ function ClassesContent() { } }; + // Closing the report is what opens the class, however it closes (button, + // Escape, backdrop): the create already happened, so there is nowhere else + // to go back to. + const closeSkippedReport = (open: boolean) => { + setReportOpen(open); + if (open) return; + const created = createdClass(); + setCreatedClass(null); + setSkipped([]); + setSkippedCourseTitles({}); + if (created) void navigate({ to: "/management/classes/$id", params: { id: created.id } }); + }; + return (
@@ -150,6 +188,22 @@ function ClassesContent() { {flash()} + 0}> + + {t("classBlueprints.skippedSummary", { count: skipped().length })} + + + + + + {/* Every blueprint endpoint is manager+, so a teacher must not be shown a tab that would 403 the moment it opens. */}