Skip to content

Commit fa62c03

Browse files
committed
fix(request): drop duplicate paper requests and validate the body
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 #550
1 parent 6e3aa11 commit fa62c03

3 files changed

Lines changed: 37 additions & 9 deletions

File tree

‎src/app/api/request/route.ts‎

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,35 @@
1+
import { z } from "zod";
12
import { success, failure } from "@/lib/utils/response";
23
import { createPaperRequest } from "@/lib/services/paper";
3-
import type { CreatePaperInputType } from "@/lib/services/paper";
4+
import { requestExams } from "@/db/paperRequest";
45
import { getPostHogClient } from "@/lib/posthog-server";
56
import { randomUUID } from "crypto";
67

8+
const paperRequestInput = z.object({
9+
subject: z.string().trim().min(1).max(200),
10+
exam: z.enum(requestExams),
11+
slot: z.string().trim().min(1).max(20),
12+
year: z.string().trim().min(1).max(20),
13+
});
14+
715
export async function POST(req: Request) {
816
try {
9-
const { subject, exam, slot, year } =
10-
(await req.json()) as CreatePaperInputType;
17+
const body: unknown = await req.json().catch(() => null);
18+
const input = paperRequestInput.safeParse(body);
1119

12-
if (!subject || !exam || !slot || !year) {
13-
return failure("All fields are required.", 400);
20+
if (!input.success) {
21+
return failure(
22+
`send a subject, slot and year, and one of ${requestExams.join(", ")} as exam`,
23+
400,
24+
);
1425
}
1526

16-
const newRequest = await createPaperRequest({ subject, exam, slot, year });
27+
const { subject, exam, slot, year } = input.data;
28+
const { request, alreadyRequested } = await createPaperRequest({ subject, exam, slot, year });
29+
30+
if (alreadyRequested) {
31+
return success({ message: "this paper has already been requested", request });
32+
}
1733

1834
try {
1935
const posthog = getPostHogClient();
@@ -43,7 +59,7 @@ export async function POST(req: Request) {
4359
}
4460

4561
return success(
46-
{ message: "Paper request submitted successfully!", request: newRequest },
62+
{ message: "Paper request submitted successfully!", request },
4763
"Created",
4864
201,
4965
);

‎src/db/paperRequest.ts‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import mongoose, { Schema, type Document, type Model } from "mongoose";
22

3+
export const requestExams = ["CAT-1", "CAT-2", "FAT", "Model CAT-1", "Model CAT-2", "Model FAT"] as const;
4+
35
export interface IPaperRequest extends Document {
46
subject: string;
57
exam: string;
@@ -10,12 +12,14 @@ export interface IPaperRequest extends Document {
1012

1113
const paperRequestSchema = new Schema<IPaperRequest>({
1214
subject: { type: String, required: true },
13-
exam: { type: String, required: true },
15+
exam: { type: String, enum: [...requestExams], required: true },
1416
slot: { type: String, required: true },
1517
year: { type: String, required: true },
1618
createdAt: { type: Date, default: Date.now }
1719
});
1820

21+
paperRequestSchema.index({ subject: 1, exam: 1, slot: 1, year: 1 }, { unique: true });
22+
1923
const PaperRequest: Model<IPaperRequest> =
2024
mongoose.models.PaperRequest ??
2125
mongoose.model<IPaperRequest>("PaperRequest", paperRequestSchema);

‎src/lib/services/paper.ts‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,15 @@ export async function getCourseCounts(){
6161

6262
export async function createPaperRequest({ subject, exam, slot, year} : CreatePaperInputType){
6363
await connectToDatabase();
64-
return await PaperRequest.create({ subject, exam, slot, year });
64+
const result = await PaperRequest.findOneAndUpdate(
65+
{ subject, exam, slot, year },
66+
{ $setOnInsert: { subject, exam, slot, year } },
67+
{ upsert: true, new: true, runValidators: true, includeResultMetadata: true },
68+
);
69+
return {
70+
request: result.value,
71+
alreadyRequested: result.lastErrorObject?.updatedExisting === true,
72+
};
6573
}
6674

6775
export async function getSelectedPapers() {

0 commit comments

Comments
 (0)