Locking and lifecycle
Creating, starting, and sealing academic years, the lock write-gate, transactional set-current, order-derived phase, and delete protection.
A year's life: created globally once for the whole school, started per campus, pointed at as current, and eventually locked, the one state that blocks writes. This page walks each transition and the guard rails around it.
Create the global year
POST academic-year creates the school-wide row: year, name, startDate, endDate. Two guard rails:
yearmust not match a live row.createchecks for an existing row with the sameyearand throws409Academic year {year} already exists. The check skips soft-deleted rows, and there is no unique index onyear, so a deleted 2025 can be created again.orderis a unique, monotonic integer. Omit it and the service assignsnextOrder(), the highestorderplus one. Supply it and a clash is409Academic year order {order} is already in use. The database indexUQ_academic_year_school_id_orderis unique per school among rows that are not deleted. This one column replaces every stored time flag: past / current / future is always derived by comparing a year'sorderto the current year's. There used to be anisFutureboolean: it was removed precisely because stored flags drift; never reintroduce one.
PATCH academic-year/:id/set-current moves the single global current pointer. toggleField clears isCurrent on every other year in the school and sets it on this one, in one transaction. It does not touch any campus binding.
Start it on a campus
Two doors into a campus_x_academic_year binding, both under campus/:campusId/academic-year:
POST .../start-current: the everyday path. Finds the global current year, upserts the binding (idempotent: clicking twice is safe), and sets it current for the campus. This is the "Start 2026 Academic Year" button in the staff hub.POST ...(plain create): bind the campus to any year with campus-localstartDate/endDate. Used for future years and catch-up setups. The binding snapshots both campus and year into JSONB columns at creation, starts unlocked and not current.
Set-current is one transaction
Switching the campus's current year touches three rows, and they must move together:
// Flag and campus pointer must move together or not at all: the staff
// hub reads both and treats disagreement as "no current year".
await this.dataSource.transaction(async (manager) => {
await manager.update(CampusXAcademicYearEntity,
{ campusId, isCurrent: true }, { isCurrent: false }); // 1. unset all siblings
await manager.update(CampusXAcademicYearEntity,
{ id }, { isCurrent: true }); // 2. set the target
await manager.update(CampusEntity,
{ id: campusId }, { currentAcademicYearId: id }); // 3. move the denormalised pointer
});That comment is the why: the frontend reads both the flag and campus.currentAcademicYearId and treats disagreement as "no current year", a crash between separate updates would blank the whole hub. The row must also be unlocked to be set current (assertRowUnlocked), the same rule that guards update and remove on the binding itself.
Lock and unlock
PATCH .../:id/lock stamps lockedAt + lockedById; unlock clears them. That timestamp is the entire write-gate mechanism: every year-scoped mutation anywhere in the API funnels through assertNotLocked, which checks nothing but this column (see Request Scoping). isCurrent never gates anything: a school can lock the current year during an exam-board freeze and the whole campus goes read-only while still displaying normally.
What lock and unlock do not check:
- Neither refuses a row that is already in the requested state. Locking a locked year writes a new
lockedAtandlockedById. - Neither looks at
isCurrent. A school can lock its current year.
After the row is written, lock asks CollabService.closeYearDocuments to close every live document for that year, so open schemes and plans stop accepting edits. That step is best effort. If it fails, the lock still succeeds and a warning Failed to close collab documents after year lock is logged.
Open and close enrolment
openForEnrolment decides whether families can enrol into a campus year. It is separate from the lock.
-
Open (
PATCH .../:id/open-enrolment) refuses a locked row withAcademic year is locked. It flips the flag only when it was closed. Only the call that flips it queues theOpenYearEnrolmentjob on the academic sweep queue, so a double-click or a race with the scheduled sweep does not notify families twice. If queuing fails, the flag stays open and the error is logged. Locking does not changeopenForEnrolment. The guardian enrol flow only lists years that are open and unlocked (guardian-enrollment.service.ts), and staff create callsassertNotLocked, so a locked year takes no new enrolments either way. -
Close (
PATCH .../:id/close-enrolment) is always allowed. It also clearsenrolmentOpensAt, so a scheduled opening cannot reopen it on the sweep's next tick.
Delete protection
Deleting a campus binding first counts every table registered in CAMPUS_YEAR_RESOURCES for that binding. The binding must also be unlocked.
/** Year-scoped tables counted before soft-deleting a campus academic year. */
const CAMPUS_YEAR_RESOURCES = [
{ resource: 'campus_term', entity: CampusTermEntity },
{ resource: 'enrollment', entity: EnrollmentEntity },
// ...sixteen in total
] as const;The sixteen are campus_term, enrollment, payment_item, payment_charge, cbt_examination, assessment, scheme_of_work, lesson_plan, record_of_work, scheme_group, grade, report_card, grading_config, report_card_config, election, and timetable.
Deleting a global year is stricter. listSchoolYearAttachments counts the year's campus bindings and its school_report_run rows, then the sixteen tables across every binding. So a global year cannot be deleted while any campus is still bound to it. Delete the bindings first.
Any rows at all → ConflictException (409) whose body carries a per-resource attachment report (enrollment: 214, grade: 1893, ...) in the standard error envelope's attachments field, the frontend renders it as "why you can't delete this". Registering your entity in this array is step 2 of making it year-scoped; forget it and your data becomes silently deletable-around.
The endpoint map
| Endpoint | Does | Guard rails |
|---|---|---|
POST academic-year | Create global year | year unique incl. soft-deleted; order auto or validated |
PATCH academic-year/:id/set-current | Move the global current pointer | Single global current |
POST .../start-current | Bind campus to global current + set current | Idempotent upsert. No current academic year has been set when there is no global current. A locked binding fails at set-current with Academic year is locked |
POST ... | Bind campus to any year | JSONB snapshots; born unlocked, not current |
PATCH .../:id/set-current | Switch campus current year | Unlocked only; one transaction |
PATCH .../:id/lock / unlock | Seal / reopen | Sets/clears lockedAt + lockedById. Lock closes live documents, best effort |
PATCH .../:id/open-enrolment / close-enrolment | Let families enrol, or stop them | Open needs an unlocked row and notifies once. Close always works |
PATCH .../:id / DELETE .../:id | Edit / soft-delete binding | Unlocked only; delete attachment-gated |
GET campus/:id/academic-context | Current binding + current term in one call | Derived, never stored: the staff hub's default view |
Every route
Generated from academic-year.controller.ts and campus-academic-year.controller.ts at API fbb63d51. Who is the permission each route checks. Prefix every path with /api/v1.
The global year
| Route | Operation id | Does | Who |
|---|---|---|---|
POST /academic-year | createAcademicYear | Create year | create on academic_year |
GET /academic-year | listAcademicYears | List academic years | list on academic_year |
GET /academic-year/current | getCurrentAcademicYear | Get current year | read on academic_year |
GET /academic-year/:id | getAcademicYear | Get academic year | read on academic_year |
PATCH /academic-year/:id | updateAcademicYear | Update year | update on academic_year |
PATCH /academic-year/:id/set-current | setCurrentAcademicYear | Set current year | update on academic_year |
DELETE /academic-year/:id | deleteAcademicYear | Delete year | delete on academic_year |
A campus's year
| Route | Operation id | Does | Who |
|---|---|---|---|
POST /campus/:campusId/academic-year | createCampusAcademicYear | Assign year | create on academic_year |
GET /campus/:campusId/academic-year | listCampusAcademicYears | List academic years | list on academic_year |
GET /campus/:campusId/academic-year/:id | getCampusAcademicYear | Get academic year | read on academic_year |
PATCH /campus/:campusId/academic-year/:id | updateCampusAcademicYear | Update year | update on academic_year |
PATCH /campus/:campusId/academic-year/:id/set-current | setCurrentCampusAcademicYear | Set current year | update on academic_year |
PATCH /campus/:campusId/academic-year/:id/lock | lockCampusAcademicYear | Lock year | update on academic_year |
PATCH /campus/:campusId/academic-year/:id/unlock | unlockCampusAcademicYear | Unlock year | update on academic_year |
PATCH /campus/:campusId/academic-year/:id/open-enrolment | openCampusAcademicYearEnrolment | Open enrolment | update on academic_year |
PATCH /campus/:campusId/academic-year/:id/close-enrolment | closeCampusAcademicYearEnrolment | Close enrolment | update on academic_year |
POST /campus/:campusId/academic-year/start-current | startCurrentCampusAcademicYear | Start current year | create on academic_year |
DELETE /campus/:campusId/academic-year/:id | deleteCampusAcademicYear | Remove year | delete on academic_year |
Term routes are on Terms.
Current limitations: say them out loud
No rollover automation. start-current activates a binding, but nothing carries students, enrollments, or terms into the new year: promotion is manual re-enrollment today. That gap was not re-tested by running a year change.
Both year controllers require an academic year permission on every method that was read. AcademicYearController uses create, list, read, update, and delete for create, list, get current, get one, update, set-current, and delete. CampusAcademicYearController uses the same resource for assign, list, get, update, set-current, lock, unlock, open enrolment, close enrolment, start-current, and remove. An older sentence on this page said those controllers were JWT-only. That is no longer what the decorators say.
Term routes split the resource. Create, update, set-current, release-reports, and delete on campus/:campusId/academic-year/:campusAcademicYearId/term require academic year. List and get require campus term. A locked campus year refuses those writes with Academic year is locked. The Hub terms tab hides Add term and Set as current while the year is locked. The date check, the derived name, and the delete rule are on Create an academic year.
The decorators were read at API a5dff0c208d5193c274d3d1f87787b55344d5d30 in academic-year.controller.ts and campus-academic-year.controller.ts. The service bodies were read at fbb63d5178bcfe171292daebb19b6cbdac025a44: academic-year.service.ts (create, update, remove, toggleCurrent, nextOrder), campus-academic-year.service.ts (create, update, remove, lock, unlock, openEnrolment, closeEnrolment, toggleCurrent, startCurrentYear), academic-year-attachment.helper.ts, BaseRepository.exists and toggleField, the academic_year entity, and the UQ_academic_year_school_id_order index in prod_init. Read in code. No year was created, locked, or deleted.
Where to go next
Request Scoping
How the lock is enforced on every write: assertNotLocked and the request-year vs row-year split.
Scoped Entities
The tables counted by delete protection, and what is deliberately not year-scoped.
Error Handling and Logging
The error envelope the attachments report rides in.
Auditing
Lock, unlock, and set-current are audited writes: how the audit trail works.
Request scoping
How campusAcademicYearId travels on every year-scoped request, the required-DTO rule, the one exemption, and the scope service behind every write.
Terms
Campus terms inside a campus academic year. The routes, who may read them, what a locked year refuses, report release, and what the Hub forms add.