Skip to content

feat: add score reconciliation job (#478) - #495

Merged
Mkalbani merged 1 commit into
MindFlowInteractive:mainfrom
AwesomeuncleB:feat/478-score-reconciliation-job
Nov 20, 2026
Merged

Mkalbani merged 1 commit into
MindFlowInteractive:mainfrom
AwesomeuncleB:feat/478-score-reconciliation-job

Conversation

@AwesomeuncleB

Copy link
Copy Markdown
Contributor

Closes #478

What does this PR do?

Adds a scheduled job that compares queued score submissions with the stored score records. It restores missing records, flags mismatches for review without overwriting anything, and saves a report for every run.

Why?

Closes #478

Type of change

  • Bug fix (non-breaking change that fixes an issue)
  • New feature (non-breaking change that adds functionality)
  • Breaking change (fix or feature that would cause existing behavior to change)
  • Refactor (no behavior change)
  • Docs / CI / tooling

How was it tested?

  • Unit tests added/updated
  • E2E test added/updated
  • Manual verification steps below

npx jest src/score-reconciliation: 24 tests pass.

What's included

New module src/score-reconciliation/ (full details in its README.md):

  • Queue and record tables: score_submissions (queued submission keyed by idempotency key, with status pending / confirmed / failed) and score_records (the final stored result, one per idempotency key). Stage 1 (Score Submission Queue with Retry #477) has not landed, so this PR adds the queue table in the shape the reconciliation job needs. The submission queue can write to it directly.
  • Report format (score_reconciliation_reports): one row per run with the window, counts (checked, matched, restored, conflictsFlagged, alreadyFlagged, errors), capped key lists in details, and a failureReason for failed runs.
  • Run logic:
    • Checks submissions from now - lookback to now - grace, so in-flight retries are left alone.
    • Runs in batches.
    • A missing record is restored from the queue and the submission is marked confirmed. A record written by someone else mid-run is handled.
    • A mismatch opens a conflict (score_reconciliation_conflicts) with field-level differences and marks the record needsReview. Its values are never overwritten, and a key never gets a second open conflict.
    • One bad submission is counted as an error and the run continues.
    • A report is saved for every run, including failed ones.
  • Scheduling: hourly cron, plus a manual run. Runs never overlap: the scheduled run skips, and a manual run returns 409.
  • Review: admins list conflicts and resolve them with a note. The review flag is cleared once a record has no open conflicts.

Settings (all optional): SCORE_RECONCILIATION_ENABLED, SCORE_RECONCILIATION_LOOKBACK_HOURS (72), SCORE_RECONCILIATION_GRACE_MINUTES (10), SCORE_RECONCILIATION_BATCH_SIZE (500).

Method Path Access
POST /score-reconciliation/run Admin
GET /score-reconciliation/reports Admin
GET /score-reconciliation/reports/:reportId Admin
GET /score-reconciliation/conflicts?status=open Admin
PATCH /score-reconciliation/conflicts/:conflictId/resolve Admin

Acceptance criteria

  • Missing scores restored automatically
  • Conflicts flagged, never silently overwritten
  • Report generated each run

Checklist

  • npm run lint passes for the new files
  • npm run format:check passes
  • npm run typecheck: no new errors (main already has 149 existing errors; this PR adds none)
  • npm test passes for the new suite
  • I added/updated tests for the change
  • I updated relevant docs (module README with the report format)
  • I added a changeset / migration if schema changed
  • I read CONTRIBUTING.md

Risk & rollout

Additive only. The job does nothing until submissions exist in score_submissions, and it can be turned off with SCORE_RECONCILIATION_ENABLED=false. Production needs a migration for the new tables.

Closes #478

- Add ScoreSubmission (queued submission keyed by idempotency key) and
  ScoreRecord (final stored result) entities
- Add ScoreConflict to hold field-level mismatches for review, and
  ScoreReconciliationReport with a documented per-run report format
- ScoreReconciliationService:
  - hourly cron plus manual run, never overlapping
  - checks submissions between now - lookback and now - grace, in
    batches, against records by idempotency key
  - restores missing records from the queue and confirms the
    submission; handles a record written concurrently
  - flags mismatches as conflicts and marks the record needsReview
    without changing its values; no duplicate open conflicts
  - one failing submission is counted and does not stop the run
  - saves a report every run, including failed runs, with capped
    key lists in details
  - conflict resolution clears the review flag when none stay open
- Admin-only controller for run, reports and conflicts
- README describing the run, report format, endpoints and settings
- Register ScoreReconciliationModule in AppModule
- 24 unit tests

Closes MindFlowInteractive#478
@drips-wave

drips-wave Bot commented Nov 19, 2026

Copy link
Copy Markdown

@AwesomeuncleB Great news! 🎉 Based on an automated assessment of this PR, the linked Wave issue(s) no longer count against your application limits.

You can now already apply to more issues while waiting for a review of this PR. Keep up the great work! 🚀

Learn more about application limits

@Mkalbani
Mkalbani merged commit b04eeb3 into MindFlowInteractive:main Nov 20, 2026
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.

Score Reconciliation Job

2 participants