Naalya Handbook
Academic-year scoping

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:

  • year must not match a live row. create checks for an existing row with the same year and throws 409 Academic year {year} already exists. The check skips soft-deleted rows, and there is no unique index on year, so a deleted 2025 can be created again.
  • order is a unique, monotonic integer. Omit it and the service assigns nextOrder(), the highest order plus one. Supply it and a clash is 409 Academic year order {order} is already in use. The database index UQ_academic_year_school_id_order is 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's order to the current year's. There used to be an isFuture boolean: 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-local startDate/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:

apps/server/src/app/academic-year/campus-academic-year.service.ts
// 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 lockedAt and lockedById.
  • 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 with Academic year is locked. It flips the flag only when it was closed. Only the call that flips it queues the OpenYearEnrolment job 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 change openForEnrolment. The guardian enrol flow only lists years that are open and unlocked (guardian-enrollment.service.ts), and staff create calls assertNotLocked, so a locked year takes no new enrolments either way.

  • Close (PATCH .../:id/close-enrolment) is always allowed. It also clears enrolmentOpensAt, 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.

apps/server/src/app/academic-year/academic-year-attachment.helper.ts
/** 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

EndpointDoesGuard rails
POST academic-yearCreate global yearyear unique incl. soft-deleted; order auto or validated
PATCH academic-year/:id/set-currentMove the global current pointerSingle global current
POST .../start-currentBind campus to global current + set currentIdempotent 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 yearJSONB snapshots; born unlocked, not current
PATCH .../:id/set-currentSwitch campus current yearUnlocked only; one transaction
PATCH .../:id/lock / unlockSeal / reopenSets/clears lockedAt + lockedById. Lock closes live documents, best effort
PATCH .../:id/open-enrolment / close-enrolmentLet families enrol, or stop themOpen needs an unlocked row and notifies once. Close always works
PATCH .../:id / DELETE .../:idEdit / soft-delete bindingUnlocked only; delete attachment-gated
GET campus/:id/academic-contextCurrent binding + current term in one callDerived, 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

RouteOperation idDoesWho
POST /academic-yearcreateAcademicYearCreate yearcreate on academic_year
GET /academic-yearlistAcademicYearsList academic yearslist on academic_year
GET /academic-year/currentgetCurrentAcademicYearGet current yearread on academic_year
GET /academic-year/:idgetAcademicYearGet academic yearread on academic_year
PATCH /academic-year/:idupdateAcademicYearUpdate yearupdate on academic_year
PATCH /academic-year/:id/set-currentsetCurrentAcademicYearSet current yearupdate on academic_year
DELETE /academic-year/:iddeleteAcademicYearDelete yeardelete on academic_year

A campus's year

RouteOperation idDoesWho
POST /campus/:campusId/academic-yearcreateCampusAcademicYearAssign yearcreate on academic_year
GET /campus/:campusId/academic-yearlistCampusAcademicYearsList academic yearslist on academic_year
GET /campus/:campusId/academic-year/:idgetCampusAcademicYearGet academic yearread on academic_year
PATCH /campus/:campusId/academic-year/:idupdateCampusAcademicYearUpdate yearupdate on academic_year
PATCH /campus/:campusId/academic-year/:id/set-currentsetCurrentCampusAcademicYearSet current yearupdate on academic_year
PATCH /campus/:campusId/academic-year/:id/locklockCampusAcademicYearLock yearupdate on academic_year
PATCH /campus/:campusId/academic-year/:id/unlockunlockCampusAcademicYearUnlock yearupdate on academic_year
PATCH /campus/:campusId/academic-year/:id/open-enrolmentopenCampusAcademicYearEnrolmentOpen enrolmentupdate on academic_year
PATCH /campus/:campusId/academic-year/:id/close-enrolmentcloseCampusAcademicYearEnrolmentClose enrolmentupdate on academic_year
POST /campus/:campusId/academic-year/start-currentstartCurrentCampusAcademicYearStart current yearcreate on academic_year
DELETE /campus/:campusId/academic-year/:iddeleteCampusAcademicYearRemove yeardelete 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

On this page