Skip to content

Commit

Permalink
Merge pull request #438 from Shubhangam333/devteam
Browse files Browse the repository at this point in the history
[FEATURE]: [Added Validation for Exam]
  • Loading branch information
TejasNair9977 authored Oct 29, 2023
2 parents ff1d374 + 80e0d14 commit 795addf
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 31 deletions.
40 changes: 29 additions & 11 deletions controller/exam.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,42 @@ import {
updateExamById,
} from "#services/exam";
import { logger } from "#util";
import { isEntityIdValid } from "#middleware/entityIdValidation";
import Infrastructure from "#models/infrastructure";
import Course from "#models/course";
import Faculty from "#models/faculty";

async function addExam(req, res) {
const { date, startTime, duration, infrastructure, supervisor, course } =
req.body;

// Checking if the provided IDs exist in the database
const isInfrastructureValid = await isEntityIdValid(
infrastructure,
Infrastructure,
);
const isSupervisorValid = await isEntityIdValid(supervisor, Faculty);
const isCourseValid = await isEntityIdValid(course, Course);

try {
const exam = await createExam(
date,
startTime,
duration,
supervisor,
infrastructure,
course,
);
res.json({ res: `added exam ${exam.id}`, id: exam.id });
if (!isInfrastructureValid || !isSupervisorValid || !isCourseValid) {
res.status(400).json({
error: `Invalid ID(s): Infra:${isInfrastructureValid} Supervisor:${isSupervisorValid} Course:${isCourseValid} `,
});
} else {
const exam = await createExam(
date,
startTime,
duration,
supervisor,
infrastructure,
course,
);
res.json({ res: `added exam ${exam.id}`, id: exam.id });
}
} catch (error) {
logger.error("Error while inserting", error);
res.status(500);
res.json({ err: "Error while inserting in DB" });
res.status(500).json({ err: "Error while inserting in DB" });
}
}

Expand Down
7 changes: 7 additions & 0 deletions middleware/EntityIdValidation.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export async function isEntityIdValid(entityIds, EntityModel) {
// eslint-disable-next-line no-param-reassign
if (!Array.isArray(entityIds)) entityIds = [entityIds];
const filter = { _id: { $in: entityIds } };
const entities = await EntityModel.read(filter);
return entities.data.length === entityIds.length;
}
25 changes: 5 additions & 20 deletions test/routes/exam.test.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,9 @@
import { jest } from "@jest/globals"; // eslint-disable-line import/no-extraneous-dependencies
import request from "supertest";
import app from "#app";
import connector from "#models/databaseUtil";
import examModel from "#models/exam";

jest.mock("#util");

let server;
let agent;

beforeAll((done) => {
server = app.listen(null, () => {
agent = request.agent(server);
connector.set("debug", false);
done();
});
});
const { agent } = global;

function cleanUp(callback) {
examModel
Expand All @@ -25,10 +13,7 @@ function cleanUp(callback) {
.then(() => {
connector.disconnect((DBerr) => {
if (DBerr) console.log("Database disconnect error: ", DBerr);
server.close((serverErr) => {
if (serverErr) console.log(serverErr);
callback();
});
callback();
});
});
}
Expand All @@ -43,9 +28,9 @@ describe("exam API", () => {
date: "2023-06-18T14:11:30Z",
startTime: "2023-06-18T14:11:30Z",
duration: 5,
supervisor: ["5f8778b54b553439ac49a03a"],
infrastructure: ["5f8778b54b553439ac49a03a"],
course: ["5f8778b54b553439ac49a03a"],
supervisor: "5f8778b54b553439ac49a03a",
infrastructure: "5f8778b54b553439ac49a03a",
course: "5f8778b54b553439ac49a03a",
});
expect(response.headers["content-type"]).toMatch(/json/);
expect(response.status).toBe(200);
Expand Down

0 comments on commit 795addf

Please sign in to comment.