diff --git a/AGENTS.md b/AGENTS.md index 8653c7c..9f89e22 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -38,7 +38,7 @@ instructions. Always written and read in English. `components/events/`, `components/exams/`, `components/users/`, `components/attendance/`, `components/courses/`, `components/marks/`, `components/sessions/`, `components/messages/`, `components/homework/`, - `components/layout/`, `components/ui/`. + `components/appointments/`, `components/layout/`, `components/ui/`. - Cross-cutting app state lives in `src/stores/` (`auth-context.tsx`, `preferences-context.tsx`) — context providers only, never domain components. School-policy lists (exam kinds, attendance statuses, grade bands) come from diff --git a/docs/role-scope-matrix.md b/docs/role-scope-matrix.md index ae7b3ad..44d4bf8 100644 --- a/docs/role-scope-matrix.md +++ b/docs/role-scope-matrix.md @@ -16,6 +16,7 @@ | Live monitor `/exams/:id/live` | ❌ | ✅ | ✅ | ✅ | | Events `/events` | ✅ | ✅ | ✅ | ✅ | | Event detail `/events/:id` | ✅ | ✅ | ✅ | ✅ | +| Appointments `/appointments` | ✅ (book + own) | ✅ (publish/manage own) | ✅ (manage all) | ✅ (manage all) | | **Report card `/marks`** | ✅ | ❌ (FE) | ❌ (FE) | ❌ (FE) | | My attendance `/attendance` | ✅ | ✅ | ✅ | ✅ | | Student marks lookup `/management/student-marks` | ❌ | ✅ | ✅ | ✅ | @@ -64,6 +65,9 @@ | Mark own attendance on events | ✅ | ✅ | ✅ | ✅ | | Mark other users' event attendance | ❌ | ✅ | ✅ | ✅ | | Create / edit / delete events | ❌ | ✅ (creator or manager+) | ✅ | ✅ | +| Publish / delete appointment slots | ❌ | ✅ (own slots) | ✅ (any) | ✅ (any) | +| Approve / reject / reschedule / cancel bookings | ❌ | ✅ (own slots) | ✅ (any) | ✅ (any) | +| Book an appointment (+ cancel / accept reschedule) | ✅ (student & parent) | ❌ | ❌ | ❌ | | Enroll students / unenroll any enrollment | ❌ | ✅ (course creator) | ✅ | ✅ | | Edit / delete courses | ❌ | ✅ (course creator) | ✅ | ✅ | | Create / edit / delete course sessions | ❌ | ✅ (course manager) | ✅ | ✅ | diff --git a/src/api/__tests__/appointments/appointments.test.ts b/src/api/__tests__/appointments/appointments.test.ts new file mode 100644 index 0000000..67472fa --- /dev/null +++ b/src/api/__tests__/appointments/appointments.test.ts @@ -0,0 +1,190 @@ +import { afterEach, describe, expect, it, vi } from "vitest"; +import { postSlots } from "../../appointments"; +import { getSlots } from "../../appointments"; +import { deleteSlotById } from "../../appointments"; +import { deleteSlotSeries } from "../../appointments"; +import { postAppointment } from "../../appointments"; +import { getAppointments } from "../../appointments"; +import { patchApproveAppointment } from "../../appointments"; +import { patchRejectAppointment } from "../../appointments"; +import { patchCancelAppointment } from "../../appointments"; +import { patchRescheduleAppointment } from "../../appointments"; +import { patchAcceptReschedule } from "../../appointments"; +import { patchDeclineReschedule } from "../../appointments"; +import { lastFetchCall, mockFetch204, mockFetchSuccess } from "../helpers/mock-fetch"; + +describe("appointments API", () => { + afterEach(() => { + vi.restoreAllMocks(); + }); + + it("postSlots calls /appointments/slots with data", async () => { + const mockSlots = [{ id: "s1" }]; + mockFetchSuccess(mockSlots); + + const data = { starts_at: 1000, ends_at: 2000, note: "hi", repeat_weekly: true, until: 5000 }; + const result = await postSlots(data); + expect(result).toEqual(mockSlots); + + const [url, init] = lastFetchCall(); + expect(url).toBe("/api/appointments/slots"); + expect(init?.method).toBe("POST"); + expect(init?.body).toBe(JSON.stringify(data)); + }); + + it("getSlots calls /appointments/slots with pagination", async () => { + const mockPage = { items: [{ id: "s1" }], total: 1 }; + mockFetchSuccess(mockPage); + + const result = await getSlots({ limit: 10 }); + expect(result).toEqual({ ...mockPage, limit: null, offset: 0 }); + + const [url, init] = lastFetchCall(); + expect(url).toBe("/api/appointments/slots?limit=10"); + expect(init?.method).toBe("GET"); + }); + + it("deleteSlotById calls /appointments/slots/:id", async () => { + mockFetch204(); + + await deleteSlotById("s1"); + + const [url, init] = lastFetchCall(); + expect(url).toBe("/api/appointments/slots/s1"); + expect(init?.method).toBe("DELETE"); + }); + + it("deleteSlotSeries calls /appointments/slots/series/:series", async () => { + mockFetch204(); + + await deleteSlotSeries("ser1"); + + const [url, init] = lastFetchCall(); + expect(url).toBe("/api/appointments/slots/series/ser1"); + expect(init?.method).toBe("DELETE"); + }); + + it("postAppointment calls /appointments with data", async () => { + const mockAppt = { id: "a1" }; + mockFetchSuccess(mockAppt); + + const data = { slot: "s1", reason: "need help" }; + const result = await postAppointment(data); + expect(result).toEqual(mockAppt); + + const [url, init] = lastFetchCall(); + expect(url).toBe("/api/appointments"); + expect(init?.method).toBe("POST"); + expect(init?.body).toBe(JSON.stringify(data)); + }); + + it("getAppointments calls /appointments with pagination", async () => { + const mockPage = { items: [{ id: "a1" }], total: 1 }; + mockFetchSuccess(mockPage); + + const result = await getAppointments({ limit: 10 }); + expect(result).toEqual({ ...mockPage, limit: null, offset: 0 }); + + const [url, init] = lastFetchCall(); + expect(url).toBe("/api/appointments?limit=10"); + expect(init?.method).toBe("GET"); + }); + + it("patchApproveAppointment calls /appointments/:id/approve", async () => { + const mockAppt = { id: "a1", status: "approved" }; + mockFetchSuccess(mockAppt); + + const result = await patchApproveAppointment("a1"); + expect(result).toEqual(mockAppt); + + const [url, init] = lastFetchCall(); + expect(url).toBe("/api/appointments/a1/approve"); + expect(init?.method).toBe("PATCH"); + }); + + it("patchRejectAppointment calls /appointments/:id/reject", async () => { + const mockAppt = { id: "a1", status: "rejected" }; + mockFetchSuccess(mockAppt); + + const result = await patchRejectAppointment("a1"); + expect(result).toEqual(mockAppt); + + const [url, init] = lastFetchCall(); + expect(url).toBe("/api/appointments/a1/reject"); + expect(init?.method).toBe("PATCH"); + expect(init?.body).toBeUndefined(); + }); + + it("patchRejectAppointment sends an optional reason body", async () => { + mockFetchSuccess({ id: "a1", status: "rejected" }); + + await patchRejectAppointment("a1", { reason: "slot no longer available" }); + + const [url, init] = lastFetchCall(); + expect(url).toBe("/api/appointments/a1/reject"); + expect(init?.method).toBe("PATCH"); + expect(init?.body).toBe(JSON.stringify({ reason: "slot no longer available" })); + }); + + it("patchCancelAppointment calls /appointments/:id/cancel", async () => { + const mockAppt = { id: "a1", status: "cancelled" }; + mockFetchSuccess(mockAppt); + + const result = await patchCancelAppointment("a1"); + expect(result).toEqual(mockAppt); + + const [url, init] = lastFetchCall(); + expect(url).toBe("/api/appointments/a1/cancel"); + expect(init?.method).toBe("PATCH"); + expect(init?.body).toBeUndefined(); + }); + + it("patchCancelAppointment sends an optional reason body", async () => { + mockFetchSuccess({ id: "a1", status: "cancelled" }); + + await patchCancelAppointment("a1", { reason: "double booked" }); + + const [url, init] = lastFetchCall(); + expect(url).toBe("/api/appointments/a1/cancel"); + expect(init?.method).toBe("PATCH"); + expect(init?.body).toBe(JSON.stringify({ reason: "double booked" })); + }); + + it("patchRescheduleAppointment calls /appointments/:id/reschedule with data", async () => { + const mockAppt = { id: "a1" }; + mockFetchSuccess(mockAppt); + + const data = { starts_at: 3000, ends_at: 4000 }; + const result = await patchRescheduleAppointment("a1", data); + expect(result).toEqual(mockAppt); + + const [url, init] = lastFetchCall(); + expect(url).toBe("/api/appointments/a1/reschedule"); + expect(init?.method).toBe("PATCH"); + expect(init?.body).toBe(JSON.stringify(data)); + }); + + it("patchAcceptReschedule calls /appointments/:id/reschedule/accept", async () => { + const mockAppt = { id: "a1" }; + mockFetchSuccess(mockAppt); + + const result = await patchAcceptReschedule("a1"); + expect(result).toEqual(mockAppt); + + const [url, init] = lastFetchCall(); + expect(url).toBe("/api/appointments/a1/reschedule/accept"); + expect(init?.method).toBe("PATCH"); + }); + + it("patchDeclineReschedule calls /appointments/:id/reschedule/decline", async () => { + const mockAppt = { id: "a1" }; + mockFetchSuccess(mockAppt); + + const result = await patchDeclineReschedule("a1"); + expect(result).toEqual(mockAppt); + + const [url, init] = lastFetchCall(); + expect(url).toBe("/api/appointments/a1/reschedule/decline"); + expect(init?.method).toBe("PATCH"); + }); +}); diff --git a/src/api/appointments/deleteSlotById.ts b/src/api/appointments/deleteSlotById.ts new file mode 100644 index 0000000..cd83331 --- /dev/null +++ b/src/api/appointments/deleteSlotById.ts @@ -0,0 +1,5 @@ +import { client } from "../client"; + +export function deleteSlotById(id: string): Promise { + return client(`/appointments/slots/${id}`, { method: "DELETE" }); +} diff --git a/src/api/appointments/deleteSlotSeries.ts b/src/api/appointments/deleteSlotSeries.ts new file mode 100644 index 0000000..b39e1e9 --- /dev/null +++ b/src/api/appointments/deleteSlotSeries.ts @@ -0,0 +1,5 @@ +import { client } from "../client"; + +export function deleteSlotSeries(series: string): Promise { + return client(`/appointments/slots/series/${series}`, { method: "DELETE" }); +} diff --git a/src/api/appointments/getAppointments.ts b/src/api/appointments/getAppointments.ts new file mode 100644 index 0000000..493fc88 --- /dev/null +++ b/src/api/appointments/getAppointments.ts @@ -0,0 +1,8 @@ +import { client } from "../client"; +import { normalizePage, pageQuery, type Page, type PageParams } from "../client"; +import type { Appointment } from "../client"; + +export async function getAppointments(params?: PageParams, signal?: AbortSignal): Promise> { + const data = await client(`/appointments${pageQuery(params)}`, { signal }); + return normalizePage(data); +} diff --git a/src/api/appointments/getSlots.ts b/src/api/appointments/getSlots.ts new file mode 100644 index 0000000..1b2878b --- /dev/null +++ b/src/api/appointments/getSlots.ts @@ -0,0 +1,8 @@ +import { client } from "../client"; +import { normalizePage, pageQuery, type Page, type PageParams } from "../client"; +import type { AppointmentSlot } from "../client"; + +export async function getSlots(params?: PageParams, signal?: AbortSignal): Promise> { + const data = await client(`/appointments/slots${pageQuery(params)}`, { signal }); + return normalizePage(data); +} diff --git a/src/api/appointments/index.ts b/src/api/appointments/index.ts new file mode 100644 index 0000000..be1fb3f --- /dev/null +++ b/src/api/appointments/index.ts @@ -0,0 +1,12 @@ +export * from "./postSlots"; +export * from "./getSlots"; +export * from "./deleteSlotById"; +export * from "./deleteSlotSeries"; +export * from "./postAppointment"; +export * from "./getAppointments"; +export * from "./patchApproveAppointment"; +export * from "./patchRejectAppointment"; +export * from "./patchCancelAppointment"; +export * from "./patchRescheduleAppointment"; +export * from "./patchAcceptReschedule"; +export * from "./patchDeclineReschedule"; diff --git a/src/api/appointments/patchAcceptReschedule.ts b/src/api/appointments/patchAcceptReschedule.ts new file mode 100644 index 0000000..3f2322a --- /dev/null +++ b/src/api/appointments/patchAcceptReschedule.ts @@ -0,0 +1,6 @@ +import { client } from "../client"; +import type { Appointment } from "../client"; + +export function patchAcceptReschedule(id: string): Promise { + return client(`/appointments/${id}/reschedule/accept`, { method: "PATCH" }); +} diff --git a/src/api/appointments/patchApproveAppointment.ts b/src/api/appointments/patchApproveAppointment.ts new file mode 100644 index 0000000..8979fd3 --- /dev/null +++ b/src/api/appointments/patchApproveAppointment.ts @@ -0,0 +1,6 @@ +import { client } from "../client"; +import type { Appointment } from "../client"; + +export function patchApproveAppointment(id: string): Promise { + return client(`/appointments/${id}/approve`, { method: "PATCH" }); +} diff --git a/src/api/appointments/patchCancelAppointment.ts b/src/api/appointments/patchCancelAppointment.ts new file mode 100644 index 0000000..9e9ee7f --- /dev/null +++ b/src/api/appointments/patchCancelAppointment.ts @@ -0,0 +1,11 @@ +import { client } from "../client"; +import type { Appointment } from "../client"; + +export type CancelAppointmentBody = { + /** Optional free-text reason; blank/omitted records no reason. */ + reason?: string; +}; + +export function patchCancelAppointment(id: string, body?: CancelAppointmentBody): Promise { + return client(`/appointments/${id}/cancel`, { method: "PATCH", body }); +} diff --git a/src/api/appointments/patchDeclineReschedule.ts b/src/api/appointments/patchDeclineReschedule.ts new file mode 100644 index 0000000..0da8bb5 --- /dev/null +++ b/src/api/appointments/patchDeclineReschedule.ts @@ -0,0 +1,6 @@ +import { client } from "../client"; +import type { Appointment } from "../client"; + +export function patchDeclineReschedule(id: string): Promise { + return client(`/appointments/${id}/reschedule/decline`, { method: "PATCH" }); +} diff --git a/src/api/appointments/patchRejectAppointment.ts b/src/api/appointments/patchRejectAppointment.ts new file mode 100644 index 0000000..5a34348 --- /dev/null +++ b/src/api/appointments/patchRejectAppointment.ts @@ -0,0 +1,11 @@ +import { client } from "../client"; +import type { Appointment } from "../client"; + +export type RejectAppointmentBody = { + /** Optional free-text reason; blank/omitted records no reason. */ + reason?: string; +}; + +export function patchRejectAppointment(id: string, body?: RejectAppointmentBody): Promise { + return client(`/appointments/${id}/reject`, { method: "PATCH", body }); +} diff --git a/src/api/appointments/patchRescheduleAppointment.ts b/src/api/appointments/patchRescheduleAppointment.ts new file mode 100644 index 0000000..a44e918 --- /dev/null +++ b/src/api/appointments/patchRescheduleAppointment.ts @@ -0,0 +1,11 @@ +import { client } from "../client"; +import type { Appointment } from "../client"; + +export type RescheduleBody = { + starts_at: number; + ends_at: number; +}; + +export function patchRescheduleAppointment(id: string, body: RescheduleBody): Promise { + return client(`/appointments/${id}/reschedule`, { method: "PATCH", body }); +} diff --git a/src/api/appointments/postAppointment.ts b/src/api/appointments/postAppointment.ts new file mode 100644 index 0000000..0b0dd74 --- /dev/null +++ b/src/api/appointments/postAppointment.ts @@ -0,0 +1,11 @@ +import { client } from "../client"; +import type { Appointment } from "../client"; + +export type BookAppointmentBody = { + slot: string; + reason: string; +}; + +export function postAppointment(body: BookAppointmentBody): Promise { + return client("/appointments", { method: "POST", body }); +} diff --git a/src/api/appointments/postSlots.ts b/src/api/appointments/postSlots.ts new file mode 100644 index 0000000..c155809 --- /dev/null +++ b/src/api/appointments/postSlots.ts @@ -0,0 +1,14 @@ +import { client } from "../client"; +import type { AppointmentSlot } from "../client"; + +export type PublishSlotsBody = { + starts_at: number; + ends_at: number; + note?: string | null; + repeat_weekly?: boolean; + until?: number | null; +}; + +export function postSlots(body: PublishSlotsBody): Promise { + return client("/appointments/slots", { method: "POST", body }); +} diff --git a/src/api/client/types.ts b/src/api/client/types.ts index 83ca367..78e3783 100644 --- a/src/api/client/types.ts +++ b/src/api/client/types.ts @@ -88,6 +88,43 @@ export type EventRegistration = { registered_by: PersonRef; }; +export type AppointmentStatus = "pending" | "approved" | "rejected" | "cancelled"; + +export type AppointmentSlot = { + id: string; + teacher: PersonRef; + starts_at: number; // unix ms UTC + ends_at: number; // unix ms UTC, half-open + note: string | null; + series: string | null; // null = one-off; shared id across a weekly series + created_at: number; +}; + +export type Appointment = { + id: string; + slot: string; + teacher: PersonRef | null; // null only if slot vanished + requester: PersonRef; + status: AppointmentStatus; + reason: string; + starts_at: number | null; // effective window (proposal if accepted, else slot's) + ends_at: number | null; + proposed_starts_at: number | null; + proposed_ends_at: number | null; + proposed_by: PersonRef | null; + decided_by: PersonRef | null; // null while pending + cancelled_by: PersonRef | null; // set only when status === "cancelled" + cancel_reason: string | null; // optional reason, null when none given + reject_reason: string | null; // optional reason when status === "rejected" (rejecter is decided_by) + created_at: number; +}; + +export const APPOINTMENT_LIMITS = { + noteMaxLen: 500, // MAX_APPOINTMENT_NOTE_LEN, empty allowed + reasonMaxLen: 1000, // MAX_APPOINTMENT_REASON_LEN, non-blank required + maxSlotOccurrences: 52, // MAX_SLOT_OCCURRENCES for repeat_weekly +} as const; + export type Course = { id: string; creator: PersonRef; diff --git a/src/components/appointments/book-appointment-form.tsx b/src/components/appointments/book-appointment-form.tsx new file mode 100644 index 0000000..57d5bcb --- /dev/null +++ b/src/components/appointments/book-appointment-form.tsx @@ -0,0 +1,82 @@ +import { createSignal } from "solid-js"; +import { APPOINTMENT_LIMITS, formatApiError, type AppointmentSlot } from "@/api/client"; +import { Button } from "@/components/ui/button"; +import { Label } from "@/components/ui/label"; +import { Textarea } from "@/components/ui/textarea"; +import { formatDateTime } from "@/lib/format"; +import { personLabel } from "@/lib/person"; +import { usePreferences, useT } from "@/stores/preferences-context"; + +export function BookAppointmentForm(props: { + slot: AppointmentSlot; + onSubmit: (reason: string) => Promise; + onCancel?: () => void; +}) { + const t = useT(); + const { locale } = usePreferences(); + const [reason, setReason] = createSignal(""); + const [error, setError] = createSignal(""); + const [pending, setPending] = createSignal(false); + + const handleSubmit = async (e: SubmitEvent) => { + e.preventDefault(); + const value = reason().trim(); + if (!value) { + setError(t("appointments.reasonRequired")); + return; + } + if (value.length > APPOINTMENT_LIMITS.reasonMaxLen) { + setError(t("form.descriptionMax")); + return; + } + setError(""); + setPending(true); + try { + await props.onSubmit(value); + } catch (err) { + setError(formatApiError(err)); + } finally { + setPending(false); + } + }; + + return ( +
+
+

+ {t("appointments.teacher")}:{" "} + {personLabel(props.slot.teacher)} +

+

+ {formatDateTime(props.slot.starts_at, locale())} — {formatDateTime(props.slot.ends_at, locale())} +

+
+ +
+ +