From 96f9e475683757746c0eed795f032ef5b3ea7d12 Mon Sep 17 00:00:00 2001 From: Aastha Rai Date: Tue, 19 Dec 2023 23:19:07 +0530 Subject: [PATCH] created logic for erpId of student and faculty --- controller/faculty.js | 10 +- controller/student.js | 13 ++- misc/constant.js | 35 ++++++ misc/mockDB/deptMock.js | 37 +------ test/routes/faculty.test.js | 206 +++++++++++++++++------------------- test/routes/student.test.js | 5 +- 6 files changed, 158 insertions(+), 148 deletions(-) diff --git a/controller/faculty.js b/controller/faculty.js index 696ddfe6..3c129b47 100644 --- a/controller/faculty.js +++ b/controller/faculty.js @@ -8,7 +8,6 @@ import { logger } from "#util"; async function addFaculty(req, res) { const { - ERPID, dateOfJoining, dateOfLeaving, profileLink, @@ -25,6 +24,15 @@ async function addFaculty(req, res) { additionalResponsibilities, } = req.body; try { + const alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; + const randomIndex = Math.floor(Math.random() * alphabet.length); + const randomLetter = alphabet[randomIndex]; + let randomNumber = Math.floor(Math.random() * 1000).toString(); + if (randomNumber.length === 2) { + randomNumber = `0${ randomNumber}`; + } + const ERPID = `F${ randomLetter }${randomNumber}`; + const newFaculty = await createFaculty( ERPID, dateOfJoining, diff --git a/controller/student.js b/controller/student.js index 7bef7628..19309883 100644 --- a/controller/student.js +++ b/controller/student.js @@ -8,14 +8,25 @@ import { logger } from "#util"; import { isEntityIdValid } from "#middleware/entityIdValidation"; import Department from "#models/department"; import Course from "#models/course"; +import { departmentAbbrev, departmentNames } from "#constant"; async function addStudent(req, res) { - const { ERPID, name, joiningYear, branch, division, rollNo, coursesOpted } = + const { name, joiningYear, branch, division, rollNo, coursesOpted } = req.body; try { const isBranchValid = await isEntityIdValid(branch, Department); const isCourseValid = await isEntityIdValid(coursesOpted, Course); if (isBranchValid && isCourseValid) { + const departmentData = await Department.read({ _id: branch }); + const departmentName = departmentData.data[0].name; + const abbrev = departmentAbbrev[departmentNames.indexOf(departmentName)]; + const year = joiningYear.toString().slice(-2); + let randomNumber = Math.floor(Math.random() * 1000).toString(); + if (randomNumber.length === 2) { + randomNumber = `0${ randomNumber}`; + } + const ERPID = `S${ abbrev }${year }${randomNumber}`; + const newStudent = await createStudent( ERPID, name, diff --git a/misc/constant.js b/misc/constant.js index 2308b955..3cc1d6d8 100644 --- a/misc/constant.js +++ b/misc/constant.js @@ -3,3 +3,38 @@ export const logLevel = { dev: "debug", prod: "info", }; + +export const departmentNames = [ + "Mechanical Engineering", + "Civil Engineering", + "Computer Engineering", + "Information Technology", + "Electronics and Telecommunication Engineering", + "Electronics and Computer Science", + "Artificial Intelligence and Data Science", + "Internet of Things", + "Artificial Intelligence and Machine Learning", + "Computer Science and Engineering (Cyber Security)", + "Mechanical and Mechatronics Engineering (Additive Manufacturing)", + "Artificial Intelligence and Data Science", + "Software Development", + "Animation & Graphic Designing", + "Data Analytics", +]; +export const departmentAbbrev = [ + "ME", + "CE", + "CS", + "IT", + "ETE", + "ECS", + "AIDS", + "IOT", + "AIML", + "CSS", + "MEMM", + "AIDS", + "SD", + "AGD", + "DA", +]; diff --git a/misc/mockDB/deptMock.js b/misc/mockDB/deptMock.js index fff6cc9b..1746ae31 100644 --- a/misc/mockDB/deptMock.js +++ b/misc/mockDB/deptMock.js @@ -1,40 +1,5 @@ import { faker } from "@faker-js/faker"; // eslint-disable-line import/no-extraneous-dependencies - -/* eslint-disable no-underscore-dangle */ -const departmentNames = [ - "Mechanical Engineering", - "Civil Engineering", - "Computer Engineering", - "Information Technology", - "Electronics and Telecommunication Engineering", - "Electronics and Computer Science", - "Artificial Intelligence and Data Science", - "Internet of Things", - "Artificial Intelligence and Machine Learning", - "Computer Science and Engineering (Cyber Security)", - "Mechanical and Mechatronics Engineering (Additive Manufacturing)", - "Artificial Intelligence and Data Science", - "Software Development", - "Animation & Graphic Designing", - "Data Analytics", -]; -const departmentAbbrev = [ - "ME", - "CE", - "CS", - "IT", - "ETE", - "ECS", - "AIDS", - "IOT", - "AIML", - "CSS", - "MEMM", - "AIDS", - "SD", - "AGD", - "DA", -]; +import { departmentNames, departmentAbbrev } from "#constant"; const createRandomDepartment = ( i, diff --git a/test/routes/faculty.test.js b/test/routes/faculty.test.js index 5e30440b..2a37a1e3 100644 --- a/test/routes/faculty.test.js +++ b/test/routes/faculty.test.js @@ -3,126 +3,120 @@ import connector from "#models/databaseUtil"; import facultyModel from "#models/faculty"; jest.mock("#util"); -const {agent} = global; +const { agent } = global; // test case for deletion function cleanUp(callback) { - facultyModel.remove( - { - ERPID: "test123", - dateOfJoining: "2023-06-18T14:11:30Z", - dateOfLeaving: "2023-07-18T14:11:30Z", - profileLink: "Sanika", - qualifications: ["Ph.D.", "M.Sc."], - totalExperience: 5, - achievements: ["Award 1", "Award 2"], - areaOfSpecialization: ["Specialization 1", "Specialization 2"], - papersPublishedPG: 10, - papersPublishedUG: 5, - department: ["5f7b75a5c69e2d4f0c285e52"], - preferredSubjects: ["5f7b75a5c69e2d4f0c285e53"], - designation: "Assistant Professor", - natureOfAssociation: "Regular", - additionalResponsibilities: "Teaching and Research", - }, - ) - .then(() => { - connector.disconnect((DBerr) => { - if (DBerr) console.log("Database disconnect error: ", DBerr); - callback(); - }); - }); + facultyModel + .remove({ + ERPID: "test123", + dateOfJoining: "2023-06-18T14:11:30Z", + dateOfLeaving: "2023-07-18T14:11:30Z", + profileLink: "Sanika", + qualifications: ["Ph.D.", "M.Sc."], + totalExperience: 5, + achievements: ["Award 1", "Award 2"], + areaOfSpecialization: ["Specialization 1", "Specialization 2"], + papersPublishedPG: 10, + papersPublishedUG: 5, + department: ["5f7b75a5c69e2d4f0c285e52"], + preferredSubjects: ["5f7b75a5c69e2d4f0c285e53"], + designation: "Assistant Professor", + natureOfAssociation: "Regular", + additionalResponsibilities: "Teaching and Research", + }) + .then(() => { + connector.disconnect((DBerr) => { + if (DBerr) console.log("Database disconnect error: ", DBerr); + callback(); + }); + }); } afterAll((done) => { - cleanUp(done); + cleanUp(done); }); describe("Faculty API", () => { - it("should create faculty", async () => { - const response = await agent.post("/faculty/create").send({ - ERPID: "test123", - dateOfJoining: "2023-06-18T14:11:30Z", - dateOfLeaving: "2023-07-18T14:11:30Z", - profileLink: "xyz", - qualifications: ["Ph.D.", "M.Sc."], - totalExperience: 5, - achievements: ["Award 1", "Award 2"], - areaOfSpecialization: ["Specialization 1", "Specialization 2"], - papersPublishedPG: 10, - papersPublishedUG: 5, - department: ["5f7b75a5c69e2d4f0c285e52"], - preferredSubjects: ["5f7b75a5c69e2d4f0c285e53"], - designation: "Assistant Professor", - natureOfAssociation: "Regular", - additionalResponsibilities: "Teaching and Research", - }); - - expect(response.status).toBe(200); - expect(response.body.res).toMatch(/added faculty/); + it("should create faculty", async () => { + const response = await agent.post("/faculty/create").send({ + dateOfJoining: "2023-06-18T14:11:30Z", + dateOfLeaving: "2023-07-18T14:11:30Z", + profileLink: "xyz", + qualifications: ["Ph.D.", "M.Sc."], + totalExperience: 5, + achievements: ["Award 1", "Award 2"], + areaOfSpecialization: ["Specialization 1", "Specialization 2"], + papersPublishedPG: 10, + papersPublishedUG: 5, + department: ["5f7b75a5c69e2d4f0c285e52"], + preferredSubjects: ["5f7b75a5c69e2d4f0c285e53"], + designation: "Assistant Professor", + natureOfAssociation: "Regular", + additionalResponsibilities: "Teaching and Research", }); - describe("after adding faculty", () => { - let id; - beforeEach(async () => { - id = await agent.post("/faculty/create").send( - { - ERPID: "test123", - dateOfJoining: "2023-06-18T14:11:30Z", - dateOfLeaving: "2023-07-18T14:11:30Z", - profileLink: "xyz", - qualifications: ["Ph.D.", "M.Sc."], - totalExperience: 5, - achievements: ["Award 1", "Award 2"], - areaOfSpecialization: ["Specialization 1", "Specialization 2"], - papersPublishedPG: 10, - papersPublishedUG: 5, - department: ["5f7b75a5c69e2d4f0c285e52"], - preferredSubjects: ["5f7b75a5c69e2d4f0c285e53"], - designation: "Assistant Professor", - natureOfAssociation: "Regular", - additionalResponsibilities: "Teaching and Research", - }); + expect(response.status).toBe(200); + expect(response.body.res).toMatch(/added faculty/); + }); - id = JSON.parse(id.res.text).id; - }); + describe("after adding faculty", () => { + let id; + beforeEach(async () => { + id = await agent.post("/faculty/create").send({ + dateOfJoining: "2023-06-18T14:11:30Z", + dateOfLeaving: "2023-07-18T14:11:30Z", + profileLink: "xyz", + qualifications: ["Ph.D.", "M.Sc."], + totalExperience: 5, + achievements: ["Award 1", "Award 2"], + areaOfSpecialization: ["Specialization 1", "Specialization 2"], + papersPublishedPG: 10, + papersPublishedUG: 5, + department: ["5f7b75a5c69e2d4f0c285e52"], + preferredSubjects: ["5f7b75a5c69e2d4f0c285e53"], + designation: "Assistant Professor", + natureOfAssociation: "Regular", + additionalResponsibilities: "Teaching and Research", + }); - afterEach(async () => { - await facultyModel.remove( - { - ERPID: "test123", - dateOfJoining: "2023-06-18T14:11:30Z", - dateOfLeaving: "2023-07-18T14:11:30Z", - profileLink: "xyz", - qualifications: ["Ph.D.", "M.Sc."], - totalExperience: 5, - achievements: ["Award 1", "Award 2"], - areaOfSpecialization: ["Specialization 1", "Specialization 2"], - papersPublishedPG: 10, - papersPublishedUG: 5, - department: ["5f7b75a5c69e2d4f0c285e52"], - preferredSubjects: ["5f7b75a5c69e2d4f0c285e53"], - designation: "Assistant Professor", - natureOfAssociation: "Regular", - additionalResponsibilities: "Teaching and Research", - }); - }); + id = JSON.parse(id.res.text).id; + }); - it("should read faculty", async () => { - const response = await agent - .get("/faculty/list") - .send({ERPID: "test123"}); + afterEach(async () => { + await facultyModel.remove({ + dateOfJoining: "2023-06-18T14:11:30Z", + dateOfLeaving: "2023-07-18T14:11:30Z", + profileLink: "xyz", + qualifications: ["Ph.D.", "M.Sc."], + totalExperience: 5, + achievements: ["Award 1", "Award 2"], + areaOfSpecialization: ["Specialization 1", "Specialization 2"], + papersPublishedPG: 10, + papersPublishedUG: 5, + department: ["5f7b75a5c69e2d4f0c285e52"], + preferredSubjects: ["5f7b75a5c69e2d4f0c285e53"], + designation: "Assistant Professor", + natureOfAssociation: "Regular", + additionalResponsibilities: "Teaching and Research", + }); + }); - expect(response.status).toBe(200); - expect(response.body.res).toBeDefined(); - }); + it("should read faculty", async () => { + const response = await agent + .get("/faculty/list") + .send({ profileLink: "xyz" }); - it("should update faculty", async () => { - const response = await agent - .post(`/faculty/update/${id}`) - .send({ ERPID: "test123" }, { totalExperience: 10 }); - expect(response.status).toBe(200); - expect(response.body.res).toMatch(/updated faculty/); - }); + expect(response.status).toBe(200); + expect(response.body.res).toBeDefined(); }); -}); \ No newline at end of file + + it("should update faculty", async () => { + const response = await agent + .post(`/faculty/update/${id}`) + .send({ profileLink: "xyz" }, { totalExperience: 10 }); + expect(response.status).toBe(200); + expect(response.body.res).toMatch(/updated faculty/); + }); + }); +}); diff --git a/test/routes/student.test.js b/test/routes/student.test.js index a5e6a849..dad4c40d 100644 --- a/test/routes/student.test.js +++ b/test/routes/student.test.js @@ -42,7 +42,6 @@ afterAll((done) => { describe("Student API", () => { it("should create student", async () => { const response = await agent.post("/student/create").send({ - ERPID: "S1032220999", name: "Arya", joiningYear: 2020, branch: branchId, @@ -59,7 +58,6 @@ describe("Student API", () => { let id; beforeEach(async () => { id = await agent.post("/student/create").send({ - ERPID: "S1032220999", name: "Arya", joiningYear: 2020, branch: branchId, @@ -72,7 +70,6 @@ describe("Student API", () => { afterEach(async () => { await studentModel.remove({ - ERPID: "S1032220999", name: "Arya", joiningYear: 2020, branch: branchId, @@ -91,7 +88,7 @@ describe("Student API", () => { it("should update student", async () => { const response = await agent .post(`/student/update/${id}`) - .send({ ERPID: "S1032220999" }, { joiningYear: 2021 }); + .send({ name: "Arya" }, { joiningYear: 2021 }); expect(response.status).toBe(200); expect(response.body.res).toMatch(`updated Student with id ${id}`);