diff --git a/controller/faculty.js b/controller/faculty.js index 9343765..5e00b33 100644 --- a/controller/faculty.js +++ b/controller/faculty.js @@ -33,7 +33,6 @@ import { logger } from "#util"; async function addFaculty(req, res) { const { - ERPID, dateOfJoining, dateOfLeaving, profileLink, @@ -56,6 +55,15 @@ async function addFaculty(req, res) { const session = await mongoose.startSession(); session.startTransaction(); 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 e501d6f..510ff28 100644 --- a/controller/student.js +++ b/controller/student.js @@ -14,10 +14,10 @@ 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, @@ -192,6 +192,16 @@ async function addStudent(req, res) { 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 session = await mongoose.startSession(); session.startTransaction(); diff --git a/misc/constant.js b/misc/constant.js index 2308b95..3cc1d6d 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 fff6cc9..1746ae3 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 64708bf..b19efe0 100644 --- a/test/routes/faculty.test.js +++ b/test/routes/faculty.test.js @@ -11,6 +11,7 @@ const { agent } = global; // test case for deletion function cleanUp(callback) { + facultyBankModel.remove({ uid: "aaaaa" }); facultyCurrentModel.remove({ uid: "aaaaa" }); facultyEducationModel.remove({ uid: "aaaaa" }); @@ -48,7 +49,6 @@ afterAll((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", @@ -277,6 +277,7 @@ describe("Faculty API", () => { ]); }); + describe("after adding faculty", () => { let id; beforeEach(async () => { diff --git a/test/routes/student.test.js b/test/routes/student.test.js index 6c6c1a1..4a30004 100644 --- a/test/routes/student.test.js +++ b/test/routes/student.test.js @@ -48,9 +48,8 @@ afterAll((done) => { describe("Student API", () => { it("should create student", async () => { const response = await agent.post("/student/create").send({ - ERPID: "ST14556245", - name: "John", - joiningYear: 2022, + name: "Arya", + joiningYear: 2020, branch: branchId, division: "B", rollNo: 101, @@ -217,9 +216,8 @@ describe("Student API", () => { let id; beforeEach(async () => { id = await agent.post("/student/create").send({ - ERPID: "ST14556245", - name: "John", - joiningYear: 2022, + name: "Arya", + joiningYear: 2020, branch: branchId, division: "B", rollNo: 101, @@ -381,12 +379,11 @@ describe("Student API", () => { stdMedHistory.remove({ uid: "USR25123456445" }), stdPersonal.remove({ uid: "USR25123456445" }), studentModel.remove({ - ERPID: "ST14556245", - name: "John", - joiningYear: 2022, + name: "Arya", + joiningYear: 2020, branch: branchId, - division: "B", - rollNo: 101, + division: "A", + rollNo: 12, coursesOpted: courseIds, }), ]); @@ -401,7 +398,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}`);