Repository navigation
Conversation
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
left a comment
There was a problem hiding this comment.
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 |
dc96217 to
fa62c03
Compare
closes #550
duplicates
validation
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) } }))