Skip to content

Commit

Permalink
added mocks for even more models
Browse files Browse the repository at this point in the history
  • Loading branch information
TejasNair9977 committed Nov 6, 2023
1 parent 0b2046e commit 6233d2b
Show file tree
Hide file tree
Showing 15 changed files with 559 additions and 21 deletions.
10 changes: 10 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
PORT=4000
TOKEN_SECRET=mysecret
ENVIRONMENT=local
DB_URL=mongodb://mongo1:30001,mongo2:30002,mongo3:30003/?replicaSet=my-replica-set
EMAIL_HOST=
EMAIL_PORT=
EMAIL_USER=
EMAIL_PASS=

# get email stuff from mailtrap
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -130,4 +130,5 @@ dist
.pnp.*

# ingore genrated APIdocs
apidoc
apidoc
data/
65 changes: 64 additions & 1 deletion misc/initDB.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ import Tutorial from "#models/tutorial";
import Practical from "#models/practical";
import Semester from "#models/semester";
import Course from "#models/course";
import Faculty from "#models/faculty";
import Student from "#models/student";
import Attendance from "#models/attendance";
import Notification from "#models/notification";
import User from "#models/user";
import generateOrganizations from "#mockDB/orgMock";
import ACCREDS from "#mockDB/accredMock";
import TOPICS from "#mockDB/topicMock";
Expand All @@ -19,6 +24,11 @@ import generatePracticals from "#mockDB/pracMock";
import generateTutorials from "#mockDB/tutMock";
import generateSemesters from "#mockDB/semMock";
import generateCourses from "#mockDB/courseMock";
import generateFaculty from "#mockDB/facultyMock";
import generateStudents from "#mockDB/studentMock";
import generateAttendance from "#mockDB/attendanceMock";
import generateNotifications from "#mockDB/notificationMock";
import generateUsers from "#mockDB/userMock"; // eslint-disable-line no-unused-vars
/* eslint-disable no-underscore-dangle */
const createdAccreds = await Accreditation.createMultiple(ACCREDS);

Expand Down Expand Up @@ -86,6 +96,59 @@ const COURSES = generateCourses(
createdDepts.map((createdDept) => createdDept._id),
);

const createdCourses = await Course.createMultiple(COURSES); // eslint-disable-line no-unused-vars
const createdCourses = await Course.createMultiple(COURSES);

const FACULTY = generateFaculty(
createdDepts.map((createdDept) => createdDept._id),
createdCourses.map((createdCourse) => createdCourse._id),
);

const createdFaculty = await Faculty.createMultiple(FACULTY);

const STUDENTS = generateStudents(
createdDepts.map((createdDept) => createdDept._id),
createdCourses.map((createdCourse) => createdCourse._id),
);

const createdStudents = await Student.createMultiple(STUDENTS);

const studentCourseList = createdStudents.map((student) => {
const studentId = student._id.toString();
const coursesOpted = student.coursesOpted.map((courseId) =>
courseId.toString(),
);
return { studentId, coursesOpted };
});

const ATTENDANCE = generateAttendance(studentCourseList);

const createdAttendance = await Attendance.createMultiple(ATTENDANCE);

// const USERS = generateUsers( // TODO this takes forever bruhh
// createdStudents.map((createdStudent) => createdStudent.ERPID),
// createdFaculty.map((createdFaculty) => createdFaculty.ERPID),
// )

// const createdUsers = await User.createMultiple(USERS)

const createdUsers = await User.read(); // use this after you initialized Users at least once, or wait for years every time

const NOTIFS = generateNotifications(
createdUsers.data
.filter((user) => user.userType === "STUDENT")
.map((student) => student._id),
createdUsers.data
.filter((user) => user.userType === "FACULTY")
.map((faculty) => faculty._id),
createdUsers.data
.filter((user) => user.userType === "ADMIN")
.map((admin) => admin._id),
);

const createdNotifications = await Notification.createMultiple(NOTIFS);

console.log(createdNotifications);
console.log(createdAttendance);
console.log(createdFaculty);

process.exit(0);
34 changes: 34 additions & 0 deletions misc/mockDB/attendanceMock.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { faker } from "@faker-js/faker"; // eslint-disable-line import/no-extraneous-dependencies

const createRandomAttendance = (studentId, courseId) => {
const monthlyAttendedInt = faker.number.int({ min: 0, max: 25 });
return {
student: studentId,
course: courseId,
monthlyAttended: monthlyAttendedInt,
monthlyOccured: faker.number.int({ min: 25, max: 27 }),
cumulativeAttended:
monthlyAttendedInt + faker.number.int({ min: 0, max: 150 }),
cumulativeOccured:
monthlyAttendedInt + faker.number.int({ min: 150, max: 175 }),
};
};

const generateAttendance = (studentCourseList) => {
const attendance = [];
for (let i = 0, j = 0; i < studentCourseList.length; j += 1) {
attendance.push(
createRandomAttendance(
studentCourseList[i].studentId,
studentCourseList[i].coursesOpted[j],
),
);
if (j >= 6) {
i += 1;
j = 0;
}
}
return attendance;
};

export default generateAttendance;
97 changes: 97 additions & 0 deletions misc/mockDB/facultyMock.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
import { faker } from "@faker-js/faker"; // eslint-disable-line import/no-extraneous-dependencies

function getRandomLetter() {
const alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
const randomIndex = Math.floor(Math.random() * alphabet.length);
return alphabet[randomIndex];
}

function randomDate(start, end) {
return new Date(
start.getTime() + Math.random() * (end.getTime() - start.getTime()),
);
}

// Function to generate random 4-digit number
function getRandomNumber() {
return Math.floor(1000 + Math.random() * 9000);
}

const generatedIDs = [];

const createRandomFaculty = (departmentId, createdCourses) => {
let id;
do {
const letter = getRandomLetter();
const number = getRandomNumber();
id = `F${letter}${number}`;
} while (generatedIDs.includes(id));
generatedIDs.push(id);
const doj = faker.date.past({ years: 5 });
return {
ERPID: id,
dateOfJoining: doj,
dateOfLeaving: faker.datatype.boolean({ probability: 0.4 })
? randomDate(doj, new Date())
: null,
profileLink: faker.internet.url(),
qualifications: [
faker.lorem.word(),
faker.lorem.word(),
faker.lorem.word(),
],
totalExperience: faker.number.int({ min: 1, max: 20 }),
achievements: [
faker.lorem.words(),
faker.lorem.words(),
faker.lorem.words(),
],
areaOfSpecialization: [
faker.lorem.word(),
faker.lorem.word(),
faker.lorem.word(),
],
papersPublishedPG: faker.number.int({ min: 0, max: 50 }),
papersPublishedUG: faker.number.int({ min: 0, max: 50 }),
department: departmentId,
preferredSubjects: createdCourses,
designation: faker.helpers.arrayElements(
[
"HOD",
"Assistant Professor",
"Associate Professor",
"Activity Head",
"Professor",
"Director",
"T&P Officer",
"R&D Activity Head",
],
{ min: 1, max: 3 },
),
natureOfAssociation: faker.helpers.arrayElement([
"Regular",
"Contract",
"Adjunct",
]),
additionalResponsibilities: faker.lorem.words(),
};
};

const generateFaculty = (createdDepts, createdCourses) => {
const faculty = [];
let selectedCourses;
for (let i = 0, j = 1; i < createdDepts.length; j += 1) {
selectedCourses = faker.helpers.arrayElements(createdCourses, {
min: 4,
max: 8,
});
faculty.push(createRandomFaculty(createdDepts[i], selectedCourses));
if (j > 30) {
i += 1;
j = 0;
}
}
return faculty;
};

export default generateFaculty;
33 changes: 33 additions & 0 deletions misc/mockDB/notificationMock.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { faker } from "@faker-js/faker"; // eslint-disable-line import/no-extraneous-dependencies

const generateNotifications = (studentIds, facultyIds, adminIds) => {
const notifications = [];
const notificationTypes = ["Student", "Faculty"];

for (let i = 0; i < 300; i += 1) {
const notification = {
data: faker.lorem.lines({ min: 3, max: 7 }),
title: faker.lorem.sentence(),
from: faker.helpers.arrayElement([...facultyIds, ...adminIds]),
type: faker.helpers.arrayElement(notificationTypes),
filter: [],
};

if (notification.type === "Student") {
notification.filter = faker.helpers.arrayElements(studentIds, {
min: 5,
max: 100,
});
} else {
notification.filter = faker.helpers.arrayElements(
[...facultyIds, ...adminIds],
{ min: 5, max: 100 },
);
}
notifications.push(notification);
}

return notifications;
};

export default generateNotifications;
84 changes: 84 additions & 0 deletions misc/mockDB/studentMock.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
import { faker } from "@faker-js/faker"; // eslint-disable-line import/no-extraneous-dependencies

const departmentAbbrev = [
"ME",
"CE",
"CS",
"IT",
"ETE",
"ECS",
"AIDS",
"IOT",
"AIML",
"CSS",
"MEMM",
"AIDS",
"SD",
"AGD",
"DA",
];

function generateRandomYear() {
const currentYear = new Date().getFullYear();
const availableYears = Array.from(
{ length: 4 },
(_, index) => currentYear - index,
);
const randomIndex = Math.floor(Math.random() * availableYears.length);
return availableYears[randomIndex].toString().slice(-2);
}

function generateRandomThreeDigitNumber() {
return Math.floor(100 + Math.random() * 900);
}

function generateStudentID(i) {
const departmentInitials = departmentAbbrev[i];
const year = generateRandomYear();
const randomThreeDigitNumber = generateRandomThreeDigitNumber();
return `S${departmentInitials}${year}${randomThreeDigitNumber}`;
}

const generatedIDs = [];

const createRandomStudent = (department, createdCourses, k, j, i) => {
let id;
let divisionLetter;
do id = generateStudentID(i);
while (generatedIDs.includes(id));
generatedIDs.push(id);
if (j >= 120) divisionLetter = "C";
else if (j >= 60) divisionLetter = "B";
else divisionLetter = "A";
return {
ERPID: id,
name: faker.person.fullName(),
joiningYear: new Date().getFullYear() - k,
branch: department,
division: divisionLetter,
rollNo: (j % 60) + 1,
coursesOpted: createdCourses,
};
};

const generateStudents = (createdDepts, createdCourses) => {
const students = [];
let selectedCourses;
for (let i = 0, j = 0, k = 0; k < 4; j += 1) {
selectedCourses = faker.helpers.arrayElements(createdCourses, 7);
students.push(
createRandomStudent(createdDepts[i], selectedCourses, k, j, i),
);
if (j >= 179) {
i += 1;
j = -1;
}
if (i === createdDepts.length) {
i = 0;
k += 1;
}
}
return students;
};

export default generateStudents;
Loading

0 comments on commit 6233d2b

Please sign in to comment.