Skip to content

fix: keep course progress consistent everywhere and make it easier to… - #2384

Open
its-gabo wants to merge 1 commit into
stagingfrom
gs_fix_course_progress_consistency
Open

its-gabo wants to merge 1 commit into
stagingfrom
gs_fix_course_progress_consistency

Conversation

@its-gabo

@its-gabo its-gabo commented Dec 4, 2026 •

Copy link
Copy Markdown
Contributor

Overview

Makes learner progress easier to maintain and makes "completed" mean the same thing on every screen. First the test coverage was raised, then the 1,282-line StudentLessonProgressService was split into focused services and repositories. The new coverage exposed two bugs, fixed here. Course completion is now one stored fact that every screen reads, and recounting learners after content is removed runs in the background.

Test coverage (written against the old code first): 33 new end-to-end tests in src/studentLessonProgress/__tests__, plus 12 unit tests. Previously, reopening a lesson, recounting after a lesson was deleted, chapter completion and its events, and completing a course after its content changed had little or no coverage. New coverage:

  • Completing a lesson when another lesson in the chapter is open, when another chapter is open, and when it is the last lesson: chapter rows, course progress, completion date and language, and exact event counts.
  • Reopening and resetting a lesson for every combination of not started, started and completed: message key, lesson row, change event, and the chapter and course reopening.
  • Which lesson types are marked started when opened, and that starting a completed lesson changes nothing.
  • Recounting after a lesson or chapter is removed, including enrolled learners with no progress and a learner whose only finished chapter was removed.
  • A completed course stays completed at 100% after a chapter is added.
  • Learning-mode users without an enrollment are enrolled; quiz and AI mentor lessons are refused without a result; the acting learner is looked up when no actor is passed; AI mentor results are updated in place.
  • One full pipeline test through the outbox: a repeated completion leaves exactly one "completed chapter" activity-log entry.

Bug fixes:

  • Completing an already-completed lesson published UserChapterFinishedEvent again each time. Each repeat wrote another activity-log entry and could send the learner another "chapter finished" email. It is now published only when the chapter is newly finished.
  • Since chore: adjust enrolled students filtering and course learning requirements #1558, progress is only tracked for enrolled learners or learners in learning mode, so the freemium completion branch and three "lesson access denied" checks could no longer run. That dead code is removed. The completed_as_freemium column stays because certificates still read it.
  • Course cards, the course page button, the certificate card, the lesson sidebar and the dashboard each used their own rule for "completed". Some compared the stored finished-chapter count with the current chapter count, using === or >= inconsistently. Others read the completion date. They disagreed as soon as an author added or removed a chapter, and nothing recounted learners when chapters were removed or a course copy was synced.

Completion rule:

  • A course is completed when the learner has finished every chapter. That fact is stored as the enrollment's completion date, and CourseCompletionService is the only code that writes it.
  • Only a lesson the learner reopens (for example a SCORM reset) withdraws a completion. Completing lessons, deleting lessons and deleting chapters only update the stored count.
  • When an author adds a chapter to a course a learner has completed, the course stays completed, the certificate stays valid, progress shows 100%, and the new chapter shows as not finished.
  • When chapters are removed, learners who have now finished everything that remains are recognised as having completed the course.
  • During a repeat, the course page and lesson sidebar show the repeat's own progress, and the earlier completion and certificate stay in place.

Background recount: removing a lesson or chapter, or a course-copy sync that removes chapters, used to recount every affected learner synchronously. For lesson deletion that happened inside the admin's HTTP request, so a large course could time out the request or run past the outbox's 5-minute claim.

  • The outbox now only enqueues one course-progress-recount BullMQ job per event, keyed by the event ID.
  • The worker validates the job data with TypeBox and pages through learners 200 at a time, with one short transaction per learner.
  • One learner's failure doesn't stop the others. If any fail, their IDs are saved on the job and the BullMQ retry recounts only them.
  • Recounts name the learner, not the admin, as the actor of their own progress events.

Refactor: studentLessonProgress.service.ts (1,282 → 491 lines) is now a facade with the same public methods, so none of its ~12 callers change. Behind it:

  • LessonProgressAccessService: the access check (previews, drafts, tracking rules, learning-mode enrollment) that was copied into three methods, plus resolving the acting user.
  • CourseCompletionService: chapter recount and course completion, including repetition, certificates and statistics.
  • CourseProgressRecountService and CourseProgressRecountWorker: the background recount.
  • LessonProgressRepository, LessonProgressAccessRepository and CourseProgressRepository hold the data access, one query or write per method. Services make no direct database calls.
  • Branching uses match(...).exhaustive(), shared contracts live in studentLessonProgress.type.ts, and the controller's response type is in schemas/.

Contracts:

  • Additive API change: course lists, the course detail and the student dashboard return isCompleted and progressPercentage. Both come from one set of SQL helpers in src/courses/utils/course-progress-sql.ts. The schema and web client were regenerated through the existing scripts. The web app reads these fields instead of comparing chapter counts; isCourseAttemptCompleted covers repeats in progress.
  • New outbox event CourseProgressRecountRequestedEvent { courseId, chapterId | null } with a TypeBox payload schema.
  • New queue course-progress-recount.
  • No existing event payloads and no database schema changed.

Notes:

  • The regenerated api-schema.json also picks up an onboarding-status parameter difference that was already out of date on staging.
  • certificate-issuing.e2e-spec.ts › "returns the learner's active certificate instead of issuing another" fails intermittently with a Postgres deadlock. It fails on unmodified staging too (one in three runs), so it is not caused by this change.

Business Value

Learners and HR see one completion status and one progress figure on every screen, and adding content to a finished course no longer quietly un-finishes it or contradicts an issued certificate. Learners stop receiving duplicate "chapter finished" notifications, and activity logs stop recording the same chapter completion several times. Removing content from large courses no longer risks timing out the admin's request or stalling progress updates, and learners who have finished everything that remains are recognised automatically. Learner progress now follows the same structure as the rest of the backend and is covered by end-to-end tests, which makes future progress and completion work cheaper and safer.

PR flow video

Recorded at 0eebab20cc8b.

  • Completed course stays completed after an author adds a chapter
  • Removing the remaining unfinished chapter marks the course completed
video.mp4

@its-gabo
its-gabo requested a review from Japrolol December 4, 2026 14:49
@its-gabo its-gabo self-assigned this Dec 4, 2026
@its-gabo its-gabo added the review me 👀 PR is ready to be reviewed label Dec 4, 2026
@its-gabo
its-gabo force-pushed the gs_fix_course_progress_consistency branch from d2119c2 to 0eebab2 Compare December 4, 2026 14:54
@its-gabo
its-gabo marked this pull request as ready for review December 4, 2026 14:56

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

review me 👀 PR is ready to be reviewed

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant