Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file modified .husky/pre-commit
100755 → 100644
Empty file.
47 changes: 34 additions & 13 deletions controller/department.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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);
Expand Down
59 changes: 40 additions & 19 deletions test/routes/department.test.js
Original file line number Diff line number Diff line change
@@ -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) {
Expand All @@ -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) => {
Expand All @@ -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);
});
Expand All @@ -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);
Expand All @@ -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;
});
Expand All @@ -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],
});
});

Expand All @@ -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();
});
Expand All @@ -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/);
Expand Down