Skip to content

refactor: wrap course progress updates in MongoDB transactions for atomicity (#1427) - #1468

Open
jayshreerathoreai32-hue wants to merge 2 commits into
JiyaBatra:mainfrom
jayshreerathoreai32-hue:feat/atomic-progress-transactions-1427
Open

refactor: wrap course progress updates in MongoDB transactions for atomicity (#1427)#1468
jayshreerathoreai32-hue wants to merge 2 commits into
JiyaBatra:mainfrom
jayshreerathoreai32-hue:feat/atomic-progress-transactions-1427

Conversation

@jayshreerathoreai32-hue

Copy link
Copy Markdown
Contributor

Resolves #1427

Description

This PR refactors the lesson completion workflow to make all related database operations atomic and consistent.

Previously, completing a lesson triggered multiple independent database writes across the Progress, Analytics, and Notification collections without an atomicity guarantee. If any operation failed midway, earlier writes could remain persisted, leaving the user in a partially updated or inconsistent state.

This PR introduces a centralized transaction-based progress service using MongoDB sessions and multi-document transactions. All lesson completion operations now succeed or fail together, ensuring that progress, analytics, XP, streaks, badges, and notifications remain consistent.

Problem Statement

Lesson completion previously triggered four independent database writes:

  • Progress update
  • Analytics entry
  • Notification entry
  • Additional notification/reward-related write

Because these operations were executed independently, a failure during any intermediate operation could leave the system in an inconsistent state.

For example, the Progress record could be updated successfully while an Analytics or Notification operation failed. This could result in:

  • Progress being recorded without corresponding analytics
  • XP or streak updates without the expected notification
  • Notifications being created for incomplete operations
  • Incorrect reward state
  • Partial database updates that are difficult to recover from

The new implementation ensures that all related operations are handled within a single transaction.

Changes Included

1. New Progress Service

Added:

server/services/progressService.js

The new service centralizes lesson-completion business logic through the exported function:

recordLessonCompletion

The function is responsible for coordinating all progress-related database operations within a single MongoDB transaction.

Key changes:

  • Starts a MongoDB session using mongoose.startSession().
  • Wraps all operations inside session.withTransaction().
  • Uses the same session for every database write.
  • Commits all changes only when every operation succeeds.
  • Automatically rolls back the transaction if any operation fails.
  • Ensures session.endSession() is always called inside a finally block.
  • Keeps streak, XP, level, and badge calculations inside the transaction boundary.

2. Atomic Database Writes

The following operations now share the same MongoDB transaction:

  • Progress upsert
  • Analytics creation
  • Notification creation
  • Streak updates
  • XP calculations
  • Level calculations
  • Badge calculations

The overall flow is:

Lesson Completion Request
        ↓
Start MongoDB Session
        ↓
Begin Transaction
        ↓
Update Progress
        ↓
Calculate Streak / XP / Level / Badges
        ↓
Create Analytics Record
        ↓
Create Notifications
        ↓
All operations successful?
      ↙       ↘
    YES        NO
     ↓          ↓
  COMMIT     ROLLBACK
     ↓          ↓
End Session in finally

@vercel

vercel Bot commented Aug 12, 2026

Copy link
Copy Markdown

@jayshreerathoreai32-hue is attempting to deploy a commit to the jiya's projects Team on Vercel.

A member of the Team first needs to authorize it.

@netlify

netlify Bot commented Aug 12, 2026

Copy link
Copy Markdown

Deploy Preview for codevibeforyou ready!

Name Link
🔨 Latest commit 75fcdb7
🔍 Latest deploy log https://app.netlify.com/projects/codevibeforyou/deploys/6a7cd87f9dc9ae0008e2aa8b
😎 Deploy Preview https://deploy-preview-1468--codevibeforyou.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Refactor]: Use MongoDB Transactions for Atomic Course Progress Updates

1 participant