Substitutions
Temporary teacher substitutions inside: data model, resolution rule, permissions, propagation, routes, tests and open items.
User-facing behaviour is in the help page Substitutions. This page is for the team. The source of truth is the code; the comments at the top of the files below say the same in Italian.
| Where | What |
|---|---|
libs/common/src/common/responsibles/responsibles.ts | the responsibles rule, activeGradingSubstitutions, coveredBySubstitution (the one scope of a substitution, used by the rule, the To-grade queue, the guard and the preview/create counts) |
libs/common/src/common/responsibles/substitutions.ts | pure functions: period checks, eventsToSubstitute, notices plan, substituteMay, substituteCanGrade |
apps/gateway/.../responsibles/substitutions.service.ts | preview, create, edit, cancel, list, badges, access checks |
apps/gateway/.../responsibles/substitute-access.guard.ts | SubstituteOrAbilityGuard |
apps/calendar/src/app/event-substitutions/ | rows, candidate events, fork carry (followFork) |
apps/education/src/app/responsibles/teacher-succession.service.ts | grading preview (pending submissions, exams due) |
Data model
Collection event_substitutions (calendar), one row per event and per course:
| Field | Meaning |
|---|---|
groupId | the substitution: all rows created together share it; edit, cancel and the list work by group |
eventId, courseId, calendarId, lectureId | the event and, for a lesson, that course's calendar and lecture. A lesson shared by several courses (one Event in several course calendars) has one row per course |
eventStart, eventEnd | the event times (kept in sync when the event moves) |
substituteId, originalId | substitute and usual teacher (titolare) |
subjectId, levelId | the scope: subject page (levelId empty) or level page |
from, to | the period: start of the first day, end of the last day |
includeGrading | "Also hand over exam grading". Default false: rows created before the field read as false, no migration |
notifyStudents, reason | notices; the reason is private (never to students, chat, badges) |
addedMember | the substitute was added to the event by this substitution (removed on cancel, never the usual teacher) |
status | active or cancelled (rows stay as history) |
Indexes: { eventId, status }, { subjectId, levelId, status, to }, { substituteId, status, to }. Nothing is copied onto submissions: grading is resolved on the spot.
Resolution rule
resolveResponsibles(target, directory):
- substitution layer
- an event: active rows of that event whose period contains the event start;
- a document to grade (no event,
gradingAt= now): groups withincludeGrading, whose period contains now and whose scope matches the document (inSubstitutionScope: from the subject any level of the subject, from the level that level). A document without subjects takes its lesson's subject; - in both cases the substitute replaces the usual teacher only if the usual teacher is responsible by the rule without substitutions, and the usual teacher stays in
covered("Covered by …");
- level teachers, if the level has any;
- subject teachers;
- course teachers;
- event manager.
The substitution follows the person: from the subject page the candidate events are all events of the subject at any level (eventsInPeriod), and eventsToSubstitute keeps those where the usual teacher is responsible without substitutions. A level with a different teacher drops out by itself; a level without teachers inherits from the subject. The grading preview (TeacherSuccessionService.preview with period) and the courses where the substitute may grade (personScopeCourseIds: subject courses plus its levels' courses) use the same rule.
Permissions
substituteMay(capability, substitution) and SubstituteOrAbilityGuard(ability, capability): the usual ability on the resource, or an active substitution.
| Capability | Granted when | Routes |
|---|---|---|
room-host | event membership with the usual teacher's role | the lesson room |
attendance | active row on the lectures in the request, period not over | PATCH /courses/:courseId/attendances |
lesson-materials | same | GET /courses/:courseId/chapters/:chapterId/lectures/:id |
grading | includeGrading, period running, submission still submitted, and the rule resolves it to the substitute now (substituteCanGrade) | PATCH /exams-in-courses/:courseId/attempts/:attemptId, GET …/session/review, PATCH …/session/grade, GET …/session/proctoring |
Everything else (course management, other submissions, the course-wide assessments view) stays refused. In To grade the substitute opens the single-submission review (SessionReviewDialog), never the course-wide dialog.
Propagation and forks
- Create: one row per (event, course); the substitute is added once per event with the usual teacher's role, members preview included.
- Cancel: future rows →
cancelledand the substitute removed where the substitution added them; past rows keep their history withto= now. - Edit: only "Also hand over exam grading" changed →
includeGradingupdated on the active rows (UPDATE_SUBSTITUTIONS); anything else → the group's rows are cancelled and recreated. - Fork (
EventSubstitutionsCarryService.followFork): "This calendar only", a course removed from sharing or a draft fork create a new Event; the active rows follow it, one per course of the new event (courseTargetsOf). - Moves (
refreshDates,refreshSeriesDates):eventStart/eventEndfollow the event; a lesson moved out of the period leaves the substitution. - End of the period: nothing runs; rows only apply inside their period.
Notices
TeacherSubstitutionChangedDto (queue subjects.teacher-substitution-changed):
- notification: substitute (
substitution-assigned), usual teacher (substitution-covered), on cancel both (substitution-cancelled), a substitute replaced in an edit (previousSubstitute→substitution-cancelled);withGradingplaceholder only for these; - students (
lessons-substituted): one per student across covered courses (planSubstitutionNotices), neverwithGrading, never the reason. They are personal notifications sent by the academy: in the academy context the bell shows them together with the academy's own (inboxFilterin the notification service); Students and Viewers of the academy see only these personal ones, never the academy-addressed admin notifications (onlyPersonalNotificationsFor, checked at every request); - chat: one system message per covered course channel, never grading;
- activity log:
metadata.includeGrading, entity name with(+ correzioni).
API
| Route | Purpose |
|---|---|
GET …/members/replace-preview?outgoingUserId&mode=temporary&from&to[&groupId] | lessons, courses, alreadyCovered, grading { pending, dueExams } |
POST …/members/replace { mode: 'temporary', outgoingUserId, incomingUserId, from, to, notifyStudents, reason?, includeGrading? } | create |
GET …/members/substitutions | current and scheduled groups (includeGrading for the "+ grading" chip) |
PATCH …/members/substitutions/:groupId | edit (also includeGrading alone) |
DELETE …/members/substitutions/:groupId | cancel from now |
POST /calendars/events/substitutions | badges of events or lectures |
GET /exams-in-courses/grading-queue?responsible= | To grade, with substitution on covered rows |
GET /exams-in-courses/mine | the student's assessments, graders with who graded by hand |
… is /subjects-areas/:areaId/subjects/:subjectId (subject page) or /subjects-areas/:areaId/subjects/:subjectId/levels/:levelId (level page).
Tests
Unit (backend/tests/unit/responsibles/, npx jest -c jest.unit.config.cjs tests/unit/responsibles):
substitutions.spec.ts: rule, period, events, notices, create, edit of grading only;substitution-grading.spec.ts: grading rule with an injected clock (before / during / after) for events, submissions and the guard, To grade queue, student feed;substitution-convergence.spec.ts: a lesson shared by 3 courses, replaced substitute, student push texts;substitution-person.spec.ts: the substitution follows the person (subject teacher also level teacher, level with inherited teacher, level with another teacher, from the level page).
End to end on Accademia Leonardo (web-next/e2e/help/flows/home/):
temporary-substitution.spec.mjs: the dialog on the next day with lessons, the grading checkbox, 375px light and dark;substitution-intensive.spec.mjs: events and submissions across all converging courses before / during / after cancel, permission probes (read-only), student side (one notice, no grading or reason, grader name, no 403), expiry step.SUBSTITUTION_FROM=subject|level(default subject).
Seeds and scripts (scripts/e2e/, Leonardo only, backups in _bak_202610_conv_*):
PHASE=plan|apply|cleanup node scripts/e2e/seed-convergence-events.mjs: converging lessons in a third course through the app's routes;bash scripts/e2e/mongosh.sh scripts/e2e/seed-convergence-demo.js PHASE=plan|apply|cleanup(on the server): one exam per course with pending submissions, plus an Italiano exam without level;node web-next/e2e/help/flows/run.mjs home/substitution-intensive [--shots];SUBSTITUTION_KEEP_GROUP=1 node web-next/e2e/help/flows/run.mjs home/substitution-intensive(prints the group id);bash scripts/e2e/mongosh.sh scripts/e2e/substitution-expire.js GROUP_ID=<id> PHASE=plan|apply(on the server);SUBSTITUTION_EXPIRE_STEP=1 node web-next/e2e/help/flows/run.mjs home/substitution-intensive(checks and cancels);- cleanup: step 1 and 2 with
PHASE=cleanup. Neversubstitution-expire.js PHASE=restoreafter a cancel: it would reactivate cancelled rows.
Open items
- G7: lessons created after the substitution (series extended, course added to sharing) are not covered; editing the days or the substitute recomputes.
- Calendars of cycle 2+ are rebuilt at publish without substitutions.
- Substitutions created before e219fc6396 on a lesson shared by several courses have a row for the first course only: cancel and recreate.
- Badges (
POST /calendars/events/substitutions) do not check that the caller can see the event (names only, never the reason).
See also docs/CALENDAR_PROPAGATION_MAP.md §9–10 in the repository.