Skip to content

fix(request): drop duplicate paper requests and validate the body - #571

Open
Dx-Alz-xD wants to merge 1 commit into
CodeChefVIT:stagingfrom
Dx-Alz-xD:krish/dedupe-paper-requests
Open

Dx-Alz-xD wants to merge 1 commit into
CodeChefVIT:stagingfrom
Dx-Alz-xD:krish/dedupe-paper-requests

Conversation

@Dx-Alz-xD

Copy link
Copy Markdown
Contributor

closes #550

duplicates

  • unique index on subject + exam + slot + year in PaperRequest
  • createPaperRequest now upserts, so a repeat request returns 200 with "this paper has already been requested" instead of adding a row. new requests still return 201 and are the only ones sent to posthog

validation

  • the body is checked with zod before the db call. bad json, null, missing or blank fields, or a field that isn't a string now return 400 instead of 500
  • exam has to be one of CAT-1, CAT-2, FAT, Model CAT-1, Model CAT-2, Model FAT. PaperRequest had no enum before, so an unknown exam like "midterm" was saved. the schema now has the same enum

tested against an in-memory mongodb by calling the route directly: repeat and padded-subject requests store one row, 20 identical requests sent at once give one 201 and one row, and every bad body above gets 400. on staging the same requests gave 201s for duplicates and unknown exams and 500s for non-string fields and bad json

before deploying: if the db already has duplicate requests, the unique index won't build until they're removed. the route still works and still dedupes in the meantime, mongoose just logs the failed index build. this keeps the oldest of each and removes the rest:

db.paperrequests.aggregate([
{ $sort: { createdAt: 1 } },
{ $group: { _id: { subject: "$subject", exam: "$exam", slot: "$slot", year: "$year" }, ids: { $push: "$_id" } } },
{ $match: { "ids.1": { $exists: true } } }
]).forEach(group => db.paperrequests.deleteMany({ _id: { $in: group.ids.slice(1) } }))

the same subject, exam, slot and year could be requested any number of
times, each one a new row. a unique index on those four fields and an
upsert in createPaperRequest keep one row per paper, and a repeat request
gets a 200 saying it was already requested instead of a 201

the body is now checked with zod before it reaches the database, so bad
json, null, or a field that is not a string returns 400 instead of 500.
exam has to be one of the six exam types, which PaperRequest now also
enforces, before this an unknown exam like "midterm" was saved

refs CodeChefVIT#550

@YOGESH-08 YOGESH-08 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm, will be merged later, as prod already has duplicate paper requests, Mongo will fail to build this unique index

@Dx-Alz-xD

Copy link
Copy Markdown
Contributor Author

lgtm, will be merged later, as prod already has duplicate paper requests, Mongo will fail to build this unique index

tru

added scripts/merge-duplicate-paper-requests.mjs (npm run merge-duplicate-requests). it lists every paper requested more than once and keeps the first request of each. with --delete it saves the rows it removes to a json file, deletes them, and creates the unique index. running npm run merge-duplicate-requests -- --delete against prod before or right after deploying should let the index build. if the old code adds a new duplicate in between, running it again picks that up

@Dx-Alz-xD
Dx-Alz-xD force-pushed the krish/dedupe-paper-requests branch from dc96217 to fa62c03 Compare December 1, 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

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Improve /api/request: deduplicate requests, add input validation

2 participants