Skip to content

Commit 8eed790

Browse files
authored
Merge pull request #466 from tcet-opensource/415-feat-allotting-uids-to-students-and-faculty-on-add
[FEAT] Creating ERPID for Student and Faculty
2 parents 44b10b5 + e02f643 commit 8eed790

File tree

6 files changed

+67
-51
lines changed

6 files changed

+67
-51
lines changed

controller/faculty.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ import { logger } from "#util";
3333

3434
async function addFaculty(req, res) {
3535
const {
36-
ERPID,
3736
dateOfJoining,
3837
dateOfLeaving,
3938
profileLink,
@@ -56,6 +55,15 @@ async function addFaculty(req, res) {
5655
const session = await mongoose.startSession();
5756
session.startTransaction();
5857
try {
58+
const alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
59+
const randomIndex = Math.floor(Math.random() * alphabet.length);
60+
const randomLetter = alphabet[randomIndex];
61+
let randomNumber = Math.floor(Math.random() * 1000).toString();
62+
if (randomNumber.length === 2) {
63+
randomNumber = `0${ randomNumber}`;
64+
}
65+
const ERPID = `F${ randomLetter }${randomNumber}`;
66+
5967
const newFaculty = await createFaculty(
6068
ERPID,
6169
dateOfJoining,

controller/student.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@ import { logger } from "#util";
1414
import { isEntityIdValid } from "#middleware/entityIdValidation";
1515
import Department from "#models/department";
1616
import Course from "#models/course";
17+
import { departmentAbbrev, departmentNames } from "#constant";
1718

1819
async function addStudent(req, res) {
1920
const {
20-
ERPID,
2121
name,
2222
joiningYear,
2323
branch,
@@ -192,6 +192,16 @@ async function addStudent(req, res) {
192192
const isBranchValid = await isEntityIdValid(branch, Department);
193193
const isCourseValid = await isEntityIdValid(coursesOpted, Course);
194194
if (isBranchValid && isCourseValid) {
195+
const departmentData = await Department.read({ _id: branch });
196+
const departmentName = departmentData.data[0].name;
197+
const abbrev = departmentAbbrev[departmentNames.indexOf(departmentName)];
198+
const year = joiningYear.toString().slice(-2);
199+
let randomNumber = Math.floor(Math.random() * 1000).toString();
200+
if (randomNumber.length === 2) {
201+
randomNumber = `0${ randomNumber}`;
202+
}
203+
const ERPID = `S${ abbrev }${year }${randomNumber}`;
204+
195205
const session = await mongoose.startSession();
196206
session.startTransaction();
197207

misc/constant.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,38 @@ export const logLevel = {
33
dev: "debug",
44
prod: "info",
55
};
6+
7+
export const departmentNames = [
8+
"Mechanical Engineering",
9+
"Civil Engineering",
10+
"Computer Engineering",
11+
"Information Technology",
12+
"Electronics and Telecommunication Engineering",
13+
"Electronics and Computer Science",
14+
"Artificial Intelligence and Data Science",
15+
"Internet of Things",
16+
"Artificial Intelligence and Machine Learning",
17+
"Computer Science and Engineering (Cyber Security)",
18+
"Mechanical and Mechatronics Engineering (Additive Manufacturing)",
19+
"Artificial Intelligence and Data Science",
20+
"Software Development",
21+
"Animation & Graphic Designing",
22+
"Data Analytics",
23+
];
24+
export const departmentAbbrev = [
25+
"ME",
26+
"CE",
27+
"CS",
28+
"IT",
29+
"ETE",
30+
"ECS",
31+
"AIDS",
32+
"IOT",
33+
"AIML",
34+
"CSS",
35+
"MEMM",
36+
"AIDS",
37+
"SD",
38+
"AGD",
39+
"DA",
40+
];

misc/mockDB/deptMock.js

Lines changed: 1 addition & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,5 @@
11
import { faker } from "@faker-js/faker"; // eslint-disable-line import/no-extraneous-dependencies
2-
3-
/* eslint-disable no-underscore-dangle */
4-
const departmentNames = [
5-
"Mechanical Engineering",
6-
"Civil Engineering",
7-
"Computer Engineering",
8-
"Information Technology",
9-
"Electronics and Telecommunication Engineering",
10-
"Electronics and Computer Science",
11-
"Artificial Intelligence and Data Science",
12-
"Internet of Things",
13-
"Artificial Intelligence and Machine Learning",
14-
"Computer Science and Engineering (Cyber Security)",
15-
"Mechanical and Mechatronics Engineering (Additive Manufacturing)",
16-
"Artificial Intelligence and Data Science",
17-
"Software Development",
18-
"Animation & Graphic Designing",
19-
"Data Analytics",
20-
];
21-
const departmentAbbrev = [
22-
"ME",
23-
"CE",
24-
"CS",
25-
"IT",
26-
"ETE",
27-
"ECS",
28-
"AIDS",
29-
"IOT",
30-
"AIML",
31-
"CSS",
32-
"MEMM",
33-
"AIDS",
34-
"SD",
35-
"AGD",
36-
"DA",
37-
];
2+
import { departmentNames, departmentAbbrev } from "#constant";
383

394
const createRandomDepartment = (
405
i,

test/routes/faculty.test.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ const { agent } = global;
1111

1212
// test case for deletion
1313
function cleanUp(callback) {
14+
1415
facultyBankModel.remove({ uid: "aaaaa" });
1516
facultyCurrentModel.remove({ uid: "aaaaa" });
1617
facultyEducationModel.remove({ uid: "aaaaa" });
@@ -48,7 +49,6 @@ afterAll((done) => {
4849
describe("Faculty API", () => {
4950
it("should create faculty", async () => {
5051
const response = await agent.post("/faculty/create").send({
51-
ERPID: "test123",
5252
dateOfJoining: "2023-06-18T14:11:30Z",
5353
dateOfLeaving: "2023-07-18T14:11:30Z",
5454
profileLink: "xyz",
@@ -277,6 +277,7 @@ describe("Faculty API", () => {
277277
]);
278278
});
279279

280+
280281
describe("after adding faculty", () => {
281282
let id;
282283
beforeEach(async () => {

test/routes/student.test.js

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,8 @@ afterAll((done) => {
4848
describe("Student API", () => {
4949
it("should create student", async () => {
5050
const response = await agent.post("/student/create").send({
51-
ERPID: "ST14556245",
52-
name: "John",
53-
joiningYear: 2022,
51+
name: "Arya",
52+
joiningYear: 2020,
5453
branch: branchId,
5554
division: "B",
5655
rollNo: 101,
@@ -217,9 +216,8 @@ describe("Student API", () => {
217216
let id;
218217
beforeEach(async () => {
219218
id = await agent.post("/student/create").send({
220-
ERPID: "ST14556245",
221-
name: "John",
222-
joiningYear: 2022,
219+
name: "Arya",
220+
joiningYear: 2020,
223221
branch: branchId,
224222
division: "B",
225223
rollNo: 101,
@@ -381,12 +379,11 @@ describe("Student API", () => {
381379
stdMedHistory.remove({ uid: "USR25123456445" }),
382380
stdPersonal.remove({ uid: "USR25123456445" }),
383381
studentModel.remove({
384-
ERPID: "ST14556245",
385-
name: "John",
386-
joiningYear: 2022,
382+
name: "Arya",
383+
joiningYear: 2020,
387384
branch: branchId,
388-
division: "B",
389-
rollNo: 101,
385+
division: "A",
386+
rollNo: 12,
390387
coursesOpted: courseIds,
391388
}),
392389
]);
@@ -401,7 +398,7 @@ describe("Student API", () => {
401398
it("should update student", async () => {
402399
const response = await agent
403400
.post(`/student/update/${id}`)
404-
.send({ ERPID: "S1032220999" }, { joiningYear: 2021 });
401+
.send({ name: "Arya" }, { joiningYear: 2021 });
405402

406403
expect(response.status).toBe(200);
407404
expect(response.body.res).toMatch(`updated Student with id ${id}`);

0 commit comments

Comments
 (0)