Authoring and access
Subject-scoped exam creation, the draft-publish-close lifecycle, per-stream assignment rules, and the relationship-based CbtAccessService.
Everything a teacher does to an exam before students see it: create it under a subject, build questions, publish, and assign it to streams. Each step has guard rails worth understanding rather than memorizing: most of them exist because an exam is a shared artifact (several teachers, several streams) sitting inside a locked-down year.
Creating an exam
POST /cbt/examinations (CbtExaminationService.create) is subject-scoped. The body needs title, subjectId, termId, campusAcademicYearId and timingMode (whole_exam or per_question); durationMinutes is optional (at least 1). The campus comes from the subject, never the body. The exam gets a unique reference and starts as draft. Streams are assigned separately (below), so one exam can serve several streams.
There is no @RequirePermissions: the caller must teach the subject, or pass create on cbt_exam for the campus. The API doesn't require a duration for whole_exam, though the DTO text says so; the Hub form enforces it.
| Step | When | Message |
|---|---|---|
| Create | No staff profile, unknown subject | Staff profile not found, Subject not found |
| Doesn't teach the subject and lacks the campus permission | You do not teach this subject | |
| Year locked, or term from another year | Academic year is locked, Term does not belong to the selected campus academic year | |
| Reference mint failed | Failed to generate examination reference | |
| Update | Not a teacher of the subject for the class | You do not teach this subject for this class |
| Not a draft, or missing | Only draft examinations can be edited, Examination not found | |
| Publish | No questions | Add at least one question before publishing |
| A question with 0 marks | Every question must carry marks greater than zero | |
per_question timing with a missing limit | Per-question timing requires a time limit on every question |
Swagger on these routes documents few of these messages. The Hub edits details through EditCbtExamDetails (see Portal routes).
The lifecycle
- Draft-only editing.
update, question create/update/delete/reorder: all callassertDraft. Once published, the paper is frozen; students may already be sitting it. - Every write also checks the year (
assertExamYearIsCurrent): update, publish, close, delete, and question edits all reject when the exam's year is no longer current/unlocked. A teacher can't quietly rewrite last year's exam. - Publish validates the whole paper: at least one question, every question
marks > 0, and, forper_questiontiming, atimeLimitSecondson every question. Passing stampstotalMarks(Σ marks) onto the exam. - Delete is a soft-delete, blocked while any attempt is
in_progressorsubmitted. It callsreleaseExamLinks(nullingsourceCbtExaminationIdon linked gradebook assessments) before the soft-delete: that ordering fixes an orphaned-link bug; keep it if you touch this code.
Questions
Question types, validated in CbtQuestionService.validateQuestionPayload; per-type grading data is normalized into the config jsonb by buildConfig:
| Type | Payload rules |
|---|---|
mcq_single | ≥ 2 options, exactly 1 correct |
mcq_multi | ≥ 2 options, ≥ 1 correct |
short_text | ≥ 1 accepted answer; match mode exact / case_insensitive / trimmed |
long_text, math | Free response: manually graded (math's canonical-expression grading is "manual until then") |
totalMarks is recomputed on every question change (recomputeTotalMarks): never write it.
Stream assignment
CbtExaminationStreamService.assign is where an exam meets its audience, with four gates in order:
- Exam not closed, year current.
- Caller may assign (
assertCanAssignStream: owner, teaches the subject for that stream, or campusMANAGE). (examinationId, streamId)unique: assigning twice is a409.- Window validity (
assertWindowValid):availableTo > availableFrom, and forwhole_examtiming the exam'sdurationMinutesmust actually fit inside the window, a 90-minute exam can't be offered in a 60-minute slot.
maxAttempts defaults to 1 and lives on the assignment, so Stream A can get two tries while Stream B gets one. Unassigning is blocked once any actively-enrolled student in the stream has a non-voided attempt: you can't pull an exam out from under students who already sat it.
Who may do what: CbtAccessService
There are no @RequirePermissions decorators on CBT routes. Authorization is relationship-based in code (cbt-access.service.ts, mirroring ClassroomAccessService): a teaching assignment is authorization, and the campus-scoped CBT_EXAM permission is the admin fallback, not the primary path.
| Action | Allowed for |
|---|---|
| Create for a subject | Teaches that subject (any subject_x_teacher row), or campus CREATE |
| Manage an exam | Teaching relationship (stream/class/subject), or campus MANAGE |
| Read an exam | Owner ∪ teaches the subject ∪ class-teacher of an assigned stream ∪ campus READ |
| Assign a stream | Owner, or teaches the subject for that stream, or campus MANAGE |
The class-teacher branch is the interesting one: a class teacher who doesn't teach the subject still sees exams assigned to their stream, oversight without authorship. The staff list endpoint composes the same three OR-branches (owner ∪ taught subjects ∪ class-teacher streams), and its streamId filter is join-backed: it pre-resolves exam ids from cbt_examination_x_stream first, short-circuiting to an empty page when there are none.
Controllers
| Controller | Base route | Guard |
|---|---|---|
CbtExaminationController | cbt/examinations | @RequireUserType(STAFF) |
CbtExaminationStreamController | cbt/examinations/:examinationId/streams | STAFF |
CbtQuestionController | cbt/examinations/:examinationId/questions | STAFF |
CbtStudentController | cbt/student | @RequireUserType(STUDENT) |
Audit snapshots exclude answer keys
Every CBT write is audited, but the snapshot functions deliberately omit the sensitive halves: exam snapshots exclude questions, question snapshots exclude prompt/options/config, attempt snapshots exclude answers and scores. An audit reader can see that a question changed, never what the answer is. Keep that property when adding audited CBT endpoints.