Skip to content

fix(contests,sync): prune obsolete scheduled contests and enforce unique externalId index in DsaContest - #332

Merged
VarshithReddy2006 merged 2 commits into
VarshithReddy2006:mainfrom
Trunal2005:fix/issue-328-contest-sync-pruning
Nov 16, 2026
Merged

VarshithReddy2006 merged 2 commits into
VarshithReddy2006:mainfrom
Trunal2005:fix/issue-328-contest-sync-pruning

Conversation

@Trunal2005

Copy link
Copy Markdown
Contributor

Description

Fixes #328

In backend/src/modules/platform-sync/contests/contestRefresh.service.ts and backend/src/db/models/dsaContest.model.ts:

  1. When external platform contests were refreshed via refreshPlatformContests, only active incoming contests were upserted into DsaContest. If a future scheduled contest was cancelled, rescheduled, or removed by upstream providers (Codeforces, LeetCode, AtCoder, CodeChef), it remained in MongoDB indefinitely and was returned as a phantom scheduled contest in fallback/database queries.
  2. In dsaContest.model.ts, the index { platform: 1, externalId: 1, kind: 1 } lacked the unique: true constraint. Under concurrent worker runs of refreshPlatformContests, parallel upsert calls could produce duplicate documents with identical (platform, externalId, kind).

Key Changes

  • Compound Unique Partial Index: Added { unique: true, partialFilterExpression: { kind: 'scheduled', externalId: { $type: 'string' } } } to { platform: 1, externalId: 1, kind: 1 } in DsaContest. This enforces strict uniqueness across scheduled contests while accommodating non-scheduled documents.
  • Obsolete Future Contest Pruning: After upserting incoming contests in refreshPlatformContests, all scheduled contests for that platform with endTime >= now whose externalId is not present in the newly fetched batch are automatically pruned via DsaContest.deleteMany.
  • Regression Tests:
    • Added test verifying that attempting to insert duplicate scheduled contests with identical (platform, externalId, kind) is rejected by the unique compound index.
    • Added test verifying that cancelled or removed upstream contests are properly pruned from MongoDB while preserving active contests and contests belonging to other platforms.

Verification

  • npm test --prefix backend -- src/__tests__/modules/platform-sync/contest-sources.test.ts --run (15/15 tests passed)
  • node scripts/verify-dco.js --range upstream/main..HEAD (DCO signed-off)

Please add the labels nsoc26, level3, bug to both the issue (#328) and this PR.

…que externalId index in DsaContest

Signed-off-by: Trunal Patil <patiltrunal06@gmail.com>
@VarshithReddy2006

Copy link
Copy Markdown
Owner

Hi @Trunal2005,

Following the recent merge of PR #320 into \main, this pull request currently has a merge conflict in:

  • \�ackend/src/db/models/dsaContest.model.ts\ (indexes section)

Could you please sync your branch with \main\ and resolve the conflict?

\\�ash
git fetch origin
git merge origin/main # or git rebase origin/main

Resolve the conflict in backend/src/db/models/dsaContest.model.ts and verify tests pass

git push origin fix/issue-328-contest-sync-pruning
\\

Once the conflict is resolved and CI passes, we will proceed with the review and merge. Thank you!

@Trunal2005

Copy link
Copy Markdown
Contributor Author

Hi @VarshithReddy2006 👋 Thank you for the update! I am checking the merge conflict in backend/src/db/models/dsaContest.model.ts to sync with latest main. Please let me know if any other improvements or changes are needed in this PR. Thanks!

@VarshithReddy2006
VarshithReddy2006 merged commit 958283e into VarshithReddy2006:main Nov 16, 2026
2 checks passed
@VarshithReddy2006

Copy link
Copy Markdown
Owner

Resolved \dsaContest.model.ts\ indexes merge conflict against main and squash-merged! Thanks @Trunal2005.

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

Labels

bug Something isn't working level3 10 points - core logic, database changes, and complex features. nsoc26

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fix(contests,sync): prune obsolete scheduled contests and enforce unique externalId index in DsaContest

2 participants