Skip to content

Commit 795addf

Browse files
Merge pull request #438 from Shubhangam333/devteam
[FEATURE]: [Added Validation for Exam]
2 parents ff1d374 + 80e0d14 commit 795addf

File tree

3 files changed

+41
-31
lines changed

3 files changed

+41
-31
lines changed

controller/exam.js

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,42 @@ import {
55
updateExamById,
66
} from "#services/exam";
77
import { logger } from "#util";
8+
import { isEntityIdValid } from "#middleware/entityIdValidation";
9+
import Infrastructure from "#models/infrastructure";
10+
import Course from "#models/course";
11+
import Faculty from "#models/faculty";
812

913
async function addExam(req, res) {
1014
const { date, startTime, duration, infrastructure, supervisor, course } =
1115
req.body;
16+
17+
// Checking if the provided IDs exist in the database
18+
const isInfrastructureValid = await isEntityIdValid(
19+
infrastructure,
20+
Infrastructure,
21+
);
22+
const isSupervisorValid = await isEntityIdValid(supervisor, Faculty);
23+
const isCourseValid = await isEntityIdValid(course, Course);
24+
1225
try {
13-
const exam = await createExam(
14-
date,
15-
startTime,
16-
duration,
17-
supervisor,
18-
infrastructure,
19-
course,
20-
);
21-
res.json({ res: `added exam ${exam.id}`, id: exam.id });
26+
if (!isInfrastructureValid || !isSupervisorValid || !isCourseValid) {
27+
res.status(400).json({
28+
error: `Invalid ID(s): Infra:${isInfrastructureValid} Supervisor:${isSupervisorValid} Course:${isCourseValid} `,
29+
});
30+
} else {
31+
const exam = await createExam(
32+
date,
33+
startTime,
34+
duration,
35+
supervisor,
36+
infrastructure,
37+
course,
38+
);
39+
res.json({ res: `added exam ${exam.id}`, id: exam.id });
40+
}
2241
} catch (error) {
2342
logger.error("Error while inserting", error);
24-
res.status(500);
25-
res.json({ err: "Error while inserting in DB" });
43+
res.status(500).json({ err: "Error while inserting in DB" });
2644
}
2745
}
2846

middleware/EntityIdValidation.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
export async function isEntityIdValid(entityIds, EntityModel) {
2+
// eslint-disable-next-line no-param-reassign
3+
if (!Array.isArray(entityIds)) entityIds = [entityIds];
4+
const filter = { _id: { $in: entityIds } };
5+
const entities = await EntityModel.read(filter);
6+
return entities.data.length === entityIds.length;
7+
}

test/routes/exam.test.js

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,9 @@
11
import { jest } from "@jest/globals"; // eslint-disable-line import/no-extraneous-dependencies
2-
import request from "supertest";
3-
import app from "#app";
42
import connector from "#models/databaseUtil";
53
import examModel from "#models/exam";
64

75
jest.mock("#util");
8-
9-
let server;
10-
let agent;
11-
12-
beforeAll((done) => {
13-
server = app.listen(null, () => {
14-
agent = request.agent(server);
15-
connector.set("debug", false);
16-
done();
17-
});
18-
});
6+
const { agent } = global;
197

208
function cleanUp(callback) {
219
examModel
@@ -25,10 +13,7 @@ function cleanUp(callback) {
2513
.then(() => {
2614
connector.disconnect((DBerr) => {
2715
if (DBerr) console.log("Database disconnect error: ", DBerr);
28-
server.close((serverErr) => {
29-
if (serverErr) console.log(serverErr);
30-
callback();
31-
});
16+
callback();
3217
});
3318
});
3419
}
@@ -43,9 +28,9 @@ describe("exam API", () => {
4328
date: "2023-06-18T14:11:30Z",
4429
startTime: "2023-06-18T14:11:30Z",
4530
duration: 5,
46-
supervisor: ["5f8778b54b553439ac49a03a"],
47-
infrastructure: ["5f8778b54b553439ac49a03a"],
48-
course: ["5f8778b54b553439ac49a03a"],
31+
supervisor: "5f8778b54b553439ac49a03a",
32+
infrastructure: "5f8778b54b553439ac49a03a",
33+
course: "5f8778b54b553439ac49a03a",
4934
});
5035
expect(response.headers["content-type"]).toMatch(/json/);
5136
expect(response.status).toBe(200);

0 commit comments

Comments
 (0)