diff --git a/README.md b/README.md index 31e4d3f..aa6fa7e 100644 --- a/README.md +++ b/README.md @@ -830,7 +830,7 @@ window filtering, before paging; negative values are a `400` naming the field. | GET | `/courses/me` | student | The caller's **enrolled** courses · paged | | GET | `/courses/{id}` | student | Get course (enrolled, creator, assigned teacher, or manager+) | | PATCH | `/courses/{id}` | teacher | Edit course incl. `kind` and `capacity` (`null` lifts the cap) (course manager; `409` if the `term_id` it moves off changed since the read — nothing written, re-read and retry) | -| DELETE | `/courses/{id}` | teacher | Delete course + its exams, results, subjects, sessions, and homework (submissions, files, and grades included) (creator, or manager+ — **not** an assigned teacher; `409` while anyone is still enrolled) | +| DELETE | `/courses/{id}` | teacher | Delete course + its exams, results, subjects, sessions, and homework (submissions, files, and grades included); its class attachments and its id in every grade blueprint go with it (creator, or manager+ — **not** an assigned teacher; `409` while anyone is still enrolled) | | POST | `/courses/{id}/teachers` | manager | `{user_id}` — assign a **teacher+** to run the course (idempotent; returns the course; `409` + undo if that account is demoted mid-request) | | DELETE | `/courses/{id}/teachers/{user}` | manager | Unassign a teacher (`404` if they weren't assigned) | | POST | `/courses/{id}/enrollments` | teacher | `{user_id}` — enroll a **student** (idempotent upsert; course manager; only students can be enrolled; `409` once a capped course is full); enrolling a class-pumped student clears the row's `source`, so a class sweep can no longer take them back | @@ -2946,10 +2946,10 @@ at one grade, and the caller is asking about all of them. A course a human attached by hand **counts as carried**. The template asks for the course, not for the pump's tag, and a course already on the class is a no-op for a pump whoever attached it — a status read that disagreed would send -managers chasing rows no pump will ever write. A course in the template that no -longer exists is the one deliberate piece of noise: it reads as missing from -every section, because that is the truth about the section and this route may -not write, so the dangling id stays until the next pump prunes it. +managers chasing rows no pump will ever write. `missing` only ever names +courses that **still exist**: deleting a course takes its id out of every +template holding it (see below), so there is no dangling id left for this read +to report. **Finishing a partial pump** is one of two idempotent calls, and neither needs any new machinery: @@ -2979,10 +2979,11 @@ A skip's `reason` is a **machine code**, not a sentence: the client owns the wording (and the language), the same id-plus-client-label shape roles and course kinds have. The set is closed, and each code names the record that actually failed: `class_deleted` (the section vanished mid-pump), -`course_deleted` (the course was deleted; the pump also removes the dangling id -from the template, so the template shrinks and the skip is reported once for the -whole grade — the sections after the first one are not asked again — and never -again on a later pump), `class_at_course_ceiling`, `class_roster_too_large` +`course_deleted` (the course was deleted **while the pump ran** — one deleted +before it started is already out of the template; the pump removes that id too, +so the skip is reported once for the whole grade — the sections after the first +one are not asked again — and never again on a later pump), +`class_at_course_ceiling`, `class_roster_too_large` (the section holds more students than one attach may enroll at once), `course_full` (no free seat for the whole section), and @@ -3029,6 +3030,16 @@ class first exactly as a manual detach does), and a course a human attached to that class by hand carries no tag and is left exactly where it is. Deleting a blueprint applies that to its whole list. +**Deleting the course itself takes it out of every template naming it**, in the +same transaction that detaches it from the sections. A template holds its +courses as a list on its own row, so nothing else could reach them, and the id +would otherwise be permanent rather than merely stale: the pump prunes a dead id +only while walking a section, so a grade with no sections could never drop one, +and `PATCH`ing the template back as it stands — the repair this section points +at — is a `400` for naming a course that does not exist. The sweep is a scan of +`class_blueprint`, which is deliberately unindexed: the table holds one row per +grade label the school uses, and a course delete is rare. + `DELETE /classes/blueprints/{grade}` removes the row **first** and then sweeps by that tag, rather than by the list the call read: an edit that adds a course and pumps it while the delete runs would otherwise leave rows tagged with a diff --git a/src/domain/class_blueprint.rs b/src/domain/class_blueprint.rs index 238b775..1f6b009 100644 --- a/src/domain/class_blueprint.rs +++ b/src/domain/class_blueprint.rs @@ -531,8 +531,10 @@ impl ClassBlueprint { /// A course id the blueprint holds but that no longer exists reads as /// missing from every section. It is the truth about the section, and this /// read cannot prune it the way a pump does ([`Self::prune`]) without - /// writing; the next pump — `PATCH`ing the same list back — drops the id - /// and this noise with it. + /// writing — but it is no longer a state a course delete leaves behind: the + /// delete's own cascade takes the id out of every template naming it, so + /// only a row written before that cascade existed, or one the pump's own + /// window ([`Self::prune`]) is about to clear, can still show it. /// /// Unpaged, like the [`ClassGroup::list_for_grade`] it is built on: the set /// is the şube one school runs at one grade, and the caller is asking about @@ -575,14 +577,25 @@ impl ClassBlueprint { /// Drop a course that no longer exists out of this blueprint's list. /// - /// [`crate::domain::course::Course::delete`] takes the `class_course` links - /// a course had, but nothing it can reach names the blueprints holding its - /// id — so a deleted course stays in the list and every future pump refuses - /// it again, on every class, forever. A skip a manager cannot act on is - /// noise, so the pump that *finds* the dangling id also removes it: once - /// for the run that found it (later classes in the same [`Self::pump`] skip - /// it by its dead-course set rather than re-attempting and re-pruning it), - /// and never again afterwards, because the list no longer holds it. + /// The delete does this itself now: + /// [`crate::domain::course::Course::delete`] sweeps `class_blueprint` in + /// the same cascade that takes the course's `class_course` links, so a + /// template stops naming a course the instant that course goes. This used + /// to be the *only* thing that could remove such an id, and that was the + /// bug: it fires only while walking a section, so a grade with no sections + /// could never reach it, and the repair every doc surface pointed at + /// (`PATCH` the list back as it stands) is a `400` for naming a course that + /// does not exist. A permanent dangling id, and no call that could clear + /// it. + /// + /// What is left for this to do is the **window** the sweep cannot cover: a + /// pump walks a snapshot of the list it read, so a course deleted after + /// that read is still attempted, and the delete's sweep has already run + /// past a row it will not visit again. The skip is then reported once for + /// the run that found it (later classes in the same [`Self::pump`] skip it + /// by its dead-course set rather than re-attempting and re-pruning it), and + /// this write is a no-op against a list the cascade already trimmed. It + /// also clears ids written before that cascade existed. /// /// Not a compare-and-set, unlike [`Self::set_courses`]: "a course that does /// not exist is not in this list" holds for every version of the list, so diff --git a/src/domain/course.rs b/src/domain/course.rs index 05968db..6336d66 100644 --- a/src/domain/course.rs +++ b/src/domain/course.rs @@ -400,6 +400,21 @@ impl Course { /// homework-file *blobs* are the web layer's to remove — it collects /// their names before calling this. /// + /// The **grade blueprints** naming it are swept in that same transaction. + /// Nothing else can reach them — a blueprint holds its courses as a list on + /// its own row, not as link rows this cascade could delete — and an id left + /// behind is permanent rather than merely stale: the pump's own + /// [`crate::domain::class_blueprint::ClassBlueprint::prune`] fires only + /// while walking a section, so a grade with no sections can never drop one, + /// and every `PATCH` of that template is refused for naming a course that + /// does not exist, which is precisely the call documented as the repair. + /// + /// It is a table scan, deliberately unindexed: `class_blueprint` holds one + /// row per grade label the school uses (a dozen), a course delete is rare, + /// and the per-element index the `WHERE` would want + /// (`FIELDS courses[*]`) is the one this store answers with **no rows at + /// all** — the trap [`Self::list_for_teacher`] documents. + /// /// The marks going with it give their exam kinds' references back, counted /// per kind inside this same transaction — the mirror of `Exam::delete`. /// Skipping it would leave every kind the course graded under counted @@ -442,6 +457,7 @@ impl Course { UPDATE $row.class SET {CLASS_COURSE_COUNT_FIELD} = \ math::max([({CLASS_COURSE_COUNT_FIELD} ?? 0) - 1, 0]); }}; + UPDATE class_blueprint SET courses -= $course WHERE $course IN courses; DELETE session_attendance WHERE course = $course; DELETE course_session WHERE course = $course; DELETE enrollment WHERE course = $course; diff --git a/src/lib.rs b/src/lib.rs index 4ef1b61..1c5099c 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=