diff --git a/.husky/pre-commit b/.husky/pre-commit old mode 100755 new mode 100644 diff --git a/controller/department.js b/controller/department.js index fa0bd42..2957d72 100644 --- a/controller/department.js +++ b/controller/department.js @@ -4,7 +4,10 @@ import { listdepartment, deletedepartment, } from "#services/department"; - +import { isEntityIdValid } from "#middleware/entityIdValidation"; +import Accreditation from "#models/accreditation"; +import Infrastructure from "#models/infrastructure"; +import Organization from "#models/organization"; import { logger } from "#util"; async function addDepartment(req, res) { @@ -16,19 +19,37 @@ async function addDepartment(req, res) { infrastructures, organization, } = req.body; + const isAccredationValid = await isEntityIdValid( + accreditations, + Accreditation, + ); + const isInfrastructureValid = await isEntityIdValid( + infrastructures, + Infrastructure, + ); + const isOrganizationValid = await isEntityIdValid(organization, Organization); + try { - const department = await createnewdepartment( - name, - acronym, - yearOfStarting, - accreditations, - infrastructures, - organization, - ); - res.json({ - res: `added Department successfully ${department.name}`, - id: department.id, - }); + if (!isAccredationValid && !isInfrastructureValid && !isOrganizationValid) { + const error = ""; + if (!isAccredationValid) error.concat("Invalid Accreditation"); + if (!isInfrastructureValid) error.concat(" Invalid Infrastruction"); + if (!isOrganizationValid) error.concat(" Invalid Organization"); + res.status(400).json({ err: error }); + } else { + const department = await createnewdepartment( + name, + acronym, + yearOfStarting, + accreditations, + infrastructures, + organization, + ); + res.json({ + res: `added Department successfully ${department.name}`, + id: department.id, + }); + } } catch (error) { logger.error("Error while inserting", error); res.status(500); diff --git a/test/routes/department.test.js b/test/routes/department.test.js index 18820f4..2444e75 100644 --- a/test/routes/department.test.js +++ b/test/routes/department.test.js @@ -1,11 +1,17 @@ import { jest } from "@jest/globals"; // eslint-disable-line import/no-extraneous-dependencies -import mongoose from "mongoose"; import departmentmodel from "#models/department"; import connector from "#models/databaseUtil"; +import accreditationModel from "#models/accreditation"; +import infrastructureModel from "#models/infrastructure"; +import organizationModel from "#models/organization"; jest.mock("#util"); const { agent } = global; +let accreditationIds; +let infrastructureIds; +let organizationIds; + // test case for deletion function cleanUp(callback) { @@ -14,9 +20,9 @@ function cleanUp(callback) { name: "Electronics", acronym: "COMPS", yearOfStarting: "2020-09-01T00:00:00.000Z", - accreditations: [mongoose.Types.ObjectId("5f8778b54b553439ac49a03a")], - infrastructures: [mongoose.Types.ObjectId("5f8778b54b553439ac49a03b")], - organization: [mongoose.Types.ObjectId("5f8778b54b553439ac49a03b")], + accreditations: [accreditationIds], + infrastructures: [infrastructureIds], + organization: [organizationIds], }) .then(() => { connector.disconnect((DBerr) => { @@ -26,6 +32,21 @@ function cleanUp(callback) { }); } +/* eslint-disable no-underscore-dangle */ +async function getIds(callback) { + accreditationIds = await accreditationModel.read({}, 1); + accreditationIds = accreditationIds.data[0]._id; + infrastructureIds = await infrastructureModel.read({}, 1); + infrastructureIds = infrastructureIds.data[0]._id; + organizationIds = await organizationModel.read({}, 1); + organizationIds = organizationIds.data[0]._id; + callback(); +} + +beforeAll((done) => { + getIds(done); +}); + afterAll((done) => { cleanUp(done); }); @@ -36,9 +57,9 @@ describe("Department CRUD", () => { name: "Computer", acronym: "COMPS", yearOfStarting: "2020-09-01T00:00:00.000Z", - accreditations: [mongoose.Types.ObjectId("5f8778b54b553439ac49a03a")], - infrastructures: [mongoose.Types.ObjectId("5f8778b54b553439ac49a03b")], - organization: [mongoose.Types.ObjectId("5f8778b54b553439ac49a03b")], + accreditations: [accreditationIds], + infrastructures: [infrastructureIds], + organization: [organizationIds], }); expect(response.status).toBe(200); @@ -52,9 +73,9 @@ describe("Department CRUD", () => { name: "Computer", acronym: "COMPS", yearOfStarting: "2020-09-01T00:00:00.000Z", - accreditations: [mongoose.Types.ObjectId("5f8778b54b553439ac49a03a")], - infrastructures: [mongoose.Types.ObjectId("5f8778b54b553439ac49a03b")], - organization: [mongoose.Types.ObjectId("5f8778b54b553439ac49a03b")], + accreditations: [accreditationIds], + infrastructures: [infrastructureIds], + organization: [organizationIds], }); id = JSON.parse(id.res.text).id; }); @@ -64,9 +85,9 @@ describe("Department CRUD", () => { name: "Computer", acronym: "COMPS", yearOfStarting: "2020-09-01T00:00:00.000Z", - accreditations: [mongoose.Types.ObjectId("5f8778b54b553439ac49a03a")], - infrastructures: [mongoose.Types.ObjectId("5f8778b54b553439ac49a03b")], - organization: [mongoose.Types.ObjectId("5f8778b54b553439ac49a03b")], + accreditations: [accreditationIds], + infrastructures: [infrastructureIds], + organization: [organizationIds], }); }); @@ -75,9 +96,9 @@ describe("Department CRUD", () => { name: "Computer", acronym: "COMPS", yearOfStarting: "2020-09-01T00:00:00.000Z", - accreditations: [mongoose.Types.ObjectId("5f8778b54b553439ac49a03a")], - infrastructures: [mongoose.Types.ObjectId("5f8778b54b553439ac49a03b")], - organization: [mongoose.Types.ObjectId("5f8778b54b553439ac49a03b")], + accreditations: [accreditationIds], + infrastructures: [infrastructureIds], + organization: [organizationIds], }); expect(response.body.res).not.toBeNull(); }); @@ -87,9 +108,9 @@ describe("Department CRUD", () => { name: "Electronics", acronym: "COMPS", yearOfStarting: "2020-09-01T00:00:00.000Z", - accreditations: [mongoose.Types.ObjectId("5f8778b54b553439ac49a03a")], - infrastructures: [mongoose.Types.ObjectId("5f8778b54b553439ac49a03b")], - organization: [mongoose.Types.ObjectId("5f8778b54b553439ac49a03b")], + accreditations: [accreditationIds], + infrastructures: [infrastructureIds], + organization: [organizationIds], }); expect(response.status).toBe(200); expect(response.body.res).toMatch(/department updated/);