diff --git a/README.md b/README.md index 40e5e6c..8e563ba 100644 --- a/README.md +++ b/README.md @@ -2984,18 +2984,35 @@ the run**: it says nothing about the (section, course) pair it names, so every remaining section would only repeat it. It is reported once, the sections already stocked stay stocked, and the call still succeeds. -**The manual attaches answer the same vocabulary.** A `409` from -`POST /classes/{id}/members` or `POST /classes/{id}/courses` is +**The manual attaches answer the same vocabulary**, each route with its own +set. A `409` from either is `{"error": "", "code": ""}` — the prose unchanged, the -code out of the set above plus two a pump never reports: `duplicate` (already a -member / already attached) and `linked_course_missing` (another course already -attached to that section no longer exists — detach it first), which only -`POST /classes/{id}/members` can meet, since a member add is the one attach -that walks the section's existing course links while a pump attaches a course -it has just proved alive. One cause therefore reads the same whether a manager -hit it by hand or a pump hit it in bulk, which is what lets a bilingual client -branch and word it once. `code` is published on those two routes only and is simply **absent** -from every other error body. +code out of: + +| route | codes | +| --- | --- | +| `POST /classes/{id}/courses` | `duplicate`, `class_at_course_ceiling`, `class_roster_too_large`, `course_full` | +| `POST /classes/{id}/members` | `duplicate`, `class_at_roster_ceiling`, `class_course_list_too_large`, `course_full`, `linked_course_missing` | + +The three *deleted* codes are a pump's alone: on a manual attach a deleted class +or course is a `404`, and `blueprint_deleted` needs a blueprint nobody handed +it. `duplicate` (already a member / already attached) is a refusal on either +route and no skip for a pump, which asked for a row that is already in place, +and `linked_course_missing` (another course already attached to that section no +longer exists — detach it first) only `POST /classes/{id}/members` can meet, +since a member add is the one attach that walks the section's existing course +links while a pump attaches a course it has just proved alive. + +The **ceiling** pair differs by route because the ceiling does: a member add is +refused by a full roster (`class_at_roster_ceiling`, the section already holds +`max_class_members`) or by a course list longer than one add may enroll at once +(`class_course_list_too_large`); a course attach by the mirror of both +(`class_at_course_ceiling`, `class_roster_too_large`) — which is also why the +pump, which only ever attaches courses, reports that second pair. One cause +therefore reads the same whether a manager hit it by hand or a pump hit it in +bulk, which is what lets a bilingual client branch and word it once. `code` is +published on those two routes only and is simply **absent** from every other +error body. **Removal spares what a human placed.** Every attachment a blueprint makes is tagged with it. Dropping a course from the list detaches it only where the diff --git a/src/domain/class_blueprint.rs b/src/domain/class_blueprint.rs index 665b631..238b775 100644 --- a/src/domain/class_blueprint.rs +++ b/src/domain/class_blueprint.rs @@ -167,10 +167,14 @@ struct Held { /// blueprint asks for, so re-running a pump must report nothing — while a *hand* /// attach's duplicate is a genuine refusal (`duplicate`), because that call /// asked for the row and did not get it. +/// +/// Always the course axis: a pump attaches courses to sections, so the two +/// ceiling codes are read on that side ([`Attached::refusal_code`] takes the +/// axis because they differ per axis). fn skip_reason(landed: &Attached) -> Option<&'static str> { match landed { Attached::Duplicate => None, - other => other.refusal_code(), + other => other.refusal_code(&Axis::Course), } } @@ -1100,7 +1104,7 @@ mod tests { // row — the arm itself is the only thing to check for it. assert!(skip_reason(&Attached::Duplicate).is_none()); assert_eq!( - Attached::::Duplicate.refusal_code(), + Attached::::Duplicate.refusal_code(&Axis::Course), Some("duplicate"), "a hand attach's duplicate is a refusal even though a pump's is not" ); @@ -1122,10 +1126,50 @@ mod tests { "{refusal:?} must be reported as its own code, not swallowed or reworded" ); assert_eq!( - refusal.refusal_code(), + refusal.refusal_code(&Axis::Course), skip_reason(&refusal), "{refusal:?} must read the same on a manual 409 as in a pump's skip list" ); } } + + /// The two ceiling refusals mean the *opposite* ceiling on the two axes — + /// `ClassFull` is the axis being attached, `ClassOverloaded` the other one — + /// so the member add's pair must be the member add's own, and a code that + /// reads the same on both axes is the bug this pins: the roster being full + /// was published as `class_at_course_ceiling`. + #[test] + fn each_axis_names_the_ceiling_it_actually_hit() { + for (refusal, course_axis, member_axis) in [ + ( + Attached::::ClassFull, + "class_at_course_ceiling", + "class_at_roster_ceiling", + ), + ( + Attached::ClassOverloaded, + "class_roster_too_large", + "class_course_list_too_large", + ), + ] { + assert_eq!(refusal.refusal_code(&Axis::Course), Some(course_axis)); + assert_eq!(refusal.refusal_code(&Axis::Member), Some(member_axis)); + } + // Every other code is axis-free, and must stay that way: they name a + // record, not a ceiling. + for refusal in [ + Attached::::Duplicate, + Attached::Gone, + Attached::PivotGone, + Attached::Full("course:algebra".into()), + Attached::CourseGone("course:algebra".into()), + Attached::SourceGone, + ] { + assert_eq!( + refusal.refusal_code(&Axis::Course), + refusal.refusal_code(&Axis::Member), + "{refusal:?} names a record, so it must read the same on both axes" + ); + } + } } diff --git a/src/domain/class_course.rs b/src/domain/class_course.rs index fa4b43f..0c50f19 100644 --- a/src/domain/class_course.rs +++ b/src/domain/class_course.rs @@ -97,7 +97,7 @@ impl ClassCourse { // Read off the refusal, never respelled here: this route and a // blueprint pump answer one vocabulary. `Made` is the only `None`, and // it takes the `Ok` arm below. - let code = landed.refusal_code().unwrap_or_default(); + let code = landed.refusal_code(&Axis::Course).unwrap_or_default(); match landed { Attached::Made(saved) => Ok(saved), Attached::Duplicate => Err(AppError::ConflictCoded { @@ -126,8 +126,14 @@ impl ClassCourse { code, message: format!("{full} cannot hold the whole class"), }), - // Another of the class's links points at a deleted course. This - // axis claims its own pivot, so it is never *this* course. + // Unreachable on this axis, and kept because the match is + // exhaustive: the pair loop enrolls the roster into `$pivot` and + // nothing else, and the pivot claim already proved *that* course + // alive inside the same transaction. Only a member add walks a + // class's existing course links, so `linked_course_missing` is that + // route's alone — which is why this route's `409` does not publish + // it. Left mapped rather than folded into an `Internal`, so a future + // axis change is a wrong-looking 409, not a 500. Attached::CourseGone(course) => Err(AppError::ConflictCoded { code, message: format!("{course} no longer exists — detach it from this class first"), diff --git a/src/domain/class_member.rs b/src/domain/class_member.rs index f729396..4cc1d71 100644 --- a/src/domain/class_member.rs +++ b/src/domain/class_member.rs @@ -102,7 +102,7 @@ impl ClassMember { // Read off the refusal, never respelled here: this route and a // blueprint pump answer one vocabulary. `Made` is the only `None`, and // it takes the `Ok` arm below. - let code = landed.refusal_code().unwrap_or_default(); + let code = landed.refusal_code(&Axis::Member).unwrap_or_default(); match landed { Attached::Made(saved) => Ok(saved), Attached::Duplicate => Err(AppError::ConflictCoded { diff --git a/src/domain/class_pump.rs b/src/domain/class_pump.rs index 8ca44b4..7180ccf 100644 --- a/src/domain/class_pump.rs +++ b/src/domain/class_pump.rs @@ -149,14 +149,21 @@ impl Attached { /// detached before this attach can be retried. `blueprint_deleted` is a /// pump losing the template itself mid-run — the only refusal that says /// nothing about the (class, course) pair it names. - pub(crate) fn refusal_code(&self) -> Option<&'static str> { + /// + /// The `axis` is a parameter because two of these answers name a *different + /// ceiling* on each of them: [`Attached::ClassFull`] is the ceiling of the + /// axis being attached and [`Attached::ClassOverloaded`] the other axis's, + /// so a member add's "full" is a full roster where a course attach's is a + /// full course list ([`Axis::at_ceiling_code`]). Read blind, the same code + /// worded a full roster as a full course list. + pub(crate) fn refusal_code(&self, axis: &Axis) -> Option<&'static str> { match self { Attached::Made(_) => None, Attached::Duplicate => Some("duplicate"), Attached::Gone => Some("class_deleted"), Attached::PivotGone => Some("course_deleted"), - Attached::ClassFull => Some("class_at_course_ceiling"), - Attached::ClassOverloaded => Some("class_roster_too_large"), + Attached::ClassFull => Some(axis.at_ceiling_code()), + Attached::ClassOverloaded => Some(axis.other().over_ceiling_code()), Attached::Full(_) => Some("course_full"), Attached::CourseGone(_) => Some("linked_course_missing"), Attached::SourceGone => Some("blueprint_deleted"), @@ -272,6 +279,28 @@ impl Axis { } } + /// The refusal code for "the class is *at* this axis's ceiling" + /// ([`Attached::ClassFull`]), taken off the axis beside the `cap` it + /// reports, because that ceiling and its name are one fact. + fn at_ceiling_code(&self) -> &'static str { + match self { + Axis::Member => "class_at_roster_ceiling", + Axis::Course => "class_at_course_ceiling", + } + } + + /// The refusal code for "the class stands *above* this axis's ceiling", so + /// the other axis's attach would run a write loop longer than a transaction + /// may be ([`Attached::ClassOverloaded`]). Answered off the axis that is + /// over, never the one being attached — the two are always different axes, + /// which is exactly what the blind version got wrong. + fn over_ceiling_code(&self) -> &'static str { + match self { + Axis::Member => "class_roster_too_large", + Axis::Course => "class_course_list_too_large", + } + } + /// The axis whose link rows this one's write loop iterates — its counter is /// the length of that loop, and its ceiling is therefore the second half of /// the bound. diff --git a/src/lib.rs b/src/lib.rs index 192c8d5..4ef1b61 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -50,7 +50,7 @@ use crate::state::AppState; (name = "appointments", description = "Teacher availability slots and the meetings booked on them. A teacher+ publishes windows (`POST /appointments/slots`), optionally repeating weekly up to an `until` date (at most 52 occurrences, all sharing a `series` id that deletes as one). A published window may not overlap another of the *same teacher's* (`409`): the check is half-open, so 10:00–10:30 and 10:30–11:00 are two slots and not a collision, and a weekly publish is all-or-nothing — one colliding occurrence refuses the whole series. Students and parents book one with a required reason — a parent books for themselves, this is the parent-teacher conference — and the request lands `pending`: publishing availability is not consent to a person and a topic. The teacher approves, rejects, or counter-proposes another time; a counter-proposal sends the booking back to `pending` and clears `decided_by`, and the requester either accepts it (approval at the new time) or declines, which cancels the booking. A window that has already opened is closed on every side (409): such a slot cannot be booked, an underway time cannot be approved, accepted, or counter-proposed, and the requester may cancel — declining included, since a decline *is* a cancel — only until the meeting's effective window starts. Cancelling is the **requester's** route alone (`403` for anyone else, the slot's teacher and a manager/admin included, since the guard compares ids and not roles): a teacher ends a booking by rejecting it while it is `pending`, and an approved one by counter-proposing another time (back to `pending`) and then rejecting it, or simply by rescheduling. Both the reject and the cancel take an optional `reason`, and the row records who settled it (`decided_by`, `cancelled_by`) with that text (`reject_reason`, `cancel_reason`) — visible only to the requester and the slot's teacher, since that is who a booking is ever rendered to. One live booking holds a slot (rejecting or cancelling frees it), no approved meeting may overlap another for the teacher *or* the requester (409), and a slot with a live booking refuses deletion (409). A teacher demoted after publishing leaves inert slots: they drop out of the bookable list and booking one is refused"), (name = "events", description = "Calendar events and attendance. An event's `audience` is its expected-attendee roster, not a visibility wall: everyone sees every event. Aim it at the whole school, one role, a course's enrollment, a class section's (şube) roster, or a hand-built registration list. Rosters resolve live, so role changes, (un)enrollments and class-roster changes move people in and out by themselves. Attendance: teacher+ marks people in the audience (students never mark, not even themselves), and the roster report joins the expected list with the marks to show who missed. Registration lists are filled seat by seat (teachers place students, staff place only themselves), optionally capped by `capacity`, and close once the event starts. `GET /events` takes an optional `?starts_after=&ends_after=` schedule window (unix-millis) that keeps only upcoming/unfinished events and lists them soonest-first; without it the list stays newest-first"), (name = "courses", description = "Courses — kind `course` (a regular taught course), `study` (etüt, a supervised study session), or `club` (kulüp, a student club; same behavior, different label) — optional enroll-time seat `capacity`, enrollment (students only), course exams, and course sessions. A course is run by its creator plus any teachers a manager assigned to it (`POST /courses/{id}/teachers`): assigned teachers manage everything inside the course but cannot delete it or change the assignment list. Both rights are re-checked against the caller's live role on every request: `creator` is a historical column no demotion sweeps, so an account that falls below `teacher` keeps neither management nor deletion of a course it made (the course stays reachable to its assigned teachers and to manager+), and the course drops out of their `/courses`, `/exams` and `/homework` catalogs unless they are enrolled in it. A session's `teacher` column carries the same floor: teaching a session grants neither its roll call nor a view of it once the account falls below `teacher`. A roster is also filled in bulk by attaching the course to a class (see `classes`), which writes the same ordinary enrollment rows one at a time. A course with anyone still enrolled refuses deletion (409) — empty the roster first"), - (name = "classes", description = "Class sections (şube): a named set of students the school moves as one. A class is bulk enrollment, not a second kind of membership — adding a student enrolls them into every course the class carries, attaching a course enrolls the whole roster into it, and the rows written are ordinary enrollments, each tagged with the class that pumped it (a row with no such tag was placed individually). A course with too few seats takes none of the class — the whole call is a 409 naming the course that ran out, and not one of the seats claimed earlier in the same run is kept — and a student already enrolled by hand keeps their own row (never adopted, never swept). Removing a member or detaching a course takes back only the rows that class pumped, and even then repairs before it deletes: a row a *second* class still claims (it holds the same student and the same course) is re-tagged to that class instead of being dropped, so nobody loses a seat someone is still responsible for. Unenrolling a pumped student by hand is allowed and permanent — the class does not put them back; staff re-enroll them or detach and re-attach the course. A class may link an academic term (which then refuses deletion while it does), and a class still holding students or courses refuses deletion itself (409). A class may also name a homeroom teacher (sınıf öğretmeni) with `teacher_id` — any teacher+ account, a pure label that grants nothing, cleared on every class it held the moment that account is demoted below `teacher`. The class itself and its roster are manager+, every read is teacher+, and attaching or detaching a course takes management rights on *that course*, since it writes that course's roster. Two reads sit outside that bar so a student can learn their own section: `GET /classes/me` (any authenticated role — staff, never members, get an empty page) and `GET /classes/user/{user}` (teacher+, or a parent linked to that student); both hide the class's `creator` (always a manager or admin) from a caller below teacher+, since the office's account names are otherwise unreachable at that level. Naming a homeroom teacher who is demoted while the request runs is a `409`: the assignment is taken back, and a create is rolled back whole. A grade may also carry a **blueprint** (`/classes/blueprints`): the list of courses every section at that grade takes. It reaches sections in both directions — creating or editing one retro-pumps every class already at the grade, and a class created afterwards at a blueprinted grade is stocked from it by `POST /classes` itself, whose `201` therefore answers `{class, skipped, stocked_from}` (`stocked_from` is the grade label of the template that stocked it, `null` when none covers the grade, which is what tells that apart from a template that applied cleanly; a stocking that could not be carried out reads `null` too, and the retry for both is the idempotent `POST /classes/{id}/blueprint`). Pumping is best-effort — a section that cannot take a course (it is at its course ceiling, or the course has no free seat for the whole section) is skipped and returned in `skipped`, while every other section is still stocked, so a blueprint edit can leave a deliberately partial state. The same body carries `matched`, the number of sections the pump actually reached: a grade label is free text matched exactly, so `\"9 \"` and `\"9-A\"` are not `\"9\"`, and `matched: 0` with an empty `skipped` is a saved template that stocked nothing rather than one that stocked everything. `GET /classes?grade=