From e91ec5bab2a37ab8d2d8115305539f9b7e8c58fa Mon Sep 17 00:00:00 2001 From: vikrantsingh22 Date: Thu, 14 Dec 2023 14:37:30 +0530 Subject: [PATCH] 200-Addressed-461-Use-of-Session-and-Transaction-to-maintain-acid-edited-test-case-for-student --- controller/student.js | 395 ++++++++++++++++++++++++++++++-- models/student.js | 2 +- models/student/stdBank.js | 2 +- models/student/stdCollege.js | 4 +- models/student/stdEduHistory.js | 304 ++++++++++++------------ models/student/stdMedHistory.js | 4 +- models/student/stdPersonal.js | 2 +- services/student.js | 380 +++++++++++++++++++++++++++++- test/config/globalSetup.js | 4 +- test/routes/student.test.js | 368 ++++++++++++++++++++++++++--- 10 files changed, 1254 insertions(+), 211 deletions(-) diff --git a/controller/student.js b/controller/student.js index 7bef762..e501d6f 100644 --- a/controller/student.js +++ b/controller/student.js @@ -1,4 +1,10 @@ +import mongoose from "mongoose"; import { + createStdBank, + createStdCollege, + createStdEduHistory, + createStdMedHistory, + createStdPersonal, createStudent, deleteStudentById, studentList, @@ -10,26 +16,387 @@ import Department from "#models/department"; import Course from "#models/course"; async function addStudent(req, res) { - const { ERPID, name, joiningYear, branch, division, rollNo, coursesOpted } = - req.body; + const { + ERPID, + name, + joiningYear, + branch, + division, + rollNo, + coursesOpted, + uid, + /* Model stdBAnkdetails */ + bankName, + bankAccount, + bankBranch, + bankIfsc, + bankMicr, + /* Model stdCollege */ + /* uid, */ + admissionYear, + studentCode, + /* rollNo, + //it rollNo is repeated as uid is being repeated */ + admissionStatus, + admissionPattern, + admissionCategory, + seatDesc, + quotaType, + isBoarderStudent, + seatType, + seatSubType, + eligibilityNo, + enrollmentNo, + /* Model stdEduHistory */ + /* uid, */ + // tenth: { + marks, + percentage, + seatNumber, + examName, + examBoard, + msOms, + meritNumberInQualifyingExam, + admittedNumber, + // }, + // cetHscDetails: { + cetRollNo, + cetMarks, + qualifyingExamForAdmission, + stdType, + streamOpted, + mediumOfInstruction, + aggTotalMarks, + totalMarksOutOf, + percentOfMarks, + attemptNo, + passingMonth, + passingYear, + institutionName, + educBoardName, + pcmPercent, + pbmPercent, + stuQualifyingExam, + marksObtained, + stateRank, + prevExamSeatNumber, + prevTcNumber, + hscPassedSchoolName, + boardPattern, + scholarshipName, + scholarshipType, + dteSeatType, + dteUserPassword, + dteUserId, + // }, + // graduationDetails: { + graduationInstitute, + graduationBranch, + graduationDegree, + graduationMarksPct, + graduationsPassingYear, + urbanRural, + scholarshipNumber, + lastSchoolCollegeAttended, + // }, + /* Model stdMedHistory */ + /* uid, */ + bloodGroup, + pastMedicalHistory, + immunisationHistory, + chronicMedicalConditions, + parentsEmailId, + parentsContact, + relativeContacts, + /* Model stdPersonal */ + /* uid, */ + title, + firstName, + middleName, + motherName, + gender, + dob, + age, + birthPlace, + nationality, + motherTongue, + domicileState, + religion, + castCategory, + maharashtraKarnatakaBorderCandidate, + castDescription, + subCasteDescription, + nonCreamyLayerCertificateAttached, + hobby, + passportNo, + /* bloodGroup, */ + physicallyHandicapped, + studentMobNo, + studentMail, + parentMobNo, + parentMail, + perAddrDescr, + perPlotNo, + perStreetName, + perStuAddr1, + perStuAddr2, + city, + percellphone, + perpincode, + perresiphone, + permailaddress, + country, + state, + district, + tahsil, + correspondanceAddrDescr, + correspondancePlotNo, + correspondanceStreetName, + correspondanceStuAddr1, + correspondanceStuAddr2, + correspondanceCity, + correspondanceCellPhone, + correspondancePincode, + correspondanceResiPhone, + correspondanceMailAddress, + correspondanceCountry, + correspondanceState, + correspondanceDistrict, + correspondanceTahsil, + fatherDetails, + fathersOccupation, + parentsFirstName, + parentsMiddleName, + parentsLastName, + guardianMobNo, + guardianMailId, + nameAsPerTc, + casteAsPerTc, + birthStatus, + maritalStatus, + panCardNo, + passportExpiry, + drivingLicNo, + drivingLicValidTo, + aadharCardNo, + electionCardNo, + motherMobNo, + motherEmailId, + parentIncome, + photoUploaded, + signUploaded, + thumbUploaded, + noOfDocumentsUploaded, + } = req.body; try { const isBranchValid = await isEntityIdValid(branch, Department); const isCourseValid = await isEntityIdValid(coursesOpted, Course); if (isBranchValid && isCourseValid) { - const newStudent = await createStudent( - ERPID, - name, - joiningYear, - branch, - division, - rollNo, - coursesOpted, - ); - res.json({ res: `added user ${newStudent.id}`, id: newStudent.id }); + const session = await mongoose.startSession(); + session.startTransaction(); + + try { + const newStudent = await createStudent( + { ERPID, name, joiningYear, branch, division, rollNo, coursesOpted }, + session, + ); + const newStdBank = await createStdBank( + { + uid, + bankName, + bankAccount, + bankBranch, + bankIfsc, + bankMicr, + }, + session, + ); + const newstdCollege = await createStdCollege( + { + uid, + admissionYear, + studentCode, + rollNo, + admissionStatus, + admissionPattern, + admissionCategory, + seatDesc, + quotaType, + isBoarderStudent, + seatType, + seatSubType, + eligibilityNo, + enrollmentNo, + }, + session, + ); + const newStdEduHistory = await createStdEduHistory( + { + uid, + // tenth: { + marks, + percentage, + seatNumber, + examName, + examBoard, + msOms, + meritNumberInQualifyingExam, + admittedNumber, + // }, + // cetHscDetails: { + cetRollNo, + cetMarks, + qualifyingExamForAdmission, + stdType, + streamOpted, + mediumOfInstruction, + aggTotalMarks, + totalMarksOutOf, + percentOfMarks, + attemptNo, + passingMonth, + passingYear, + institutionName, + educBoardName, + pcmPercent, + pbmPercent, + stuQualifyingExam, + marksObtained, + stateRank, + prevExamSeatNumber, + prevTcNumber, + hscPassedSchoolName, + boardPattern, + scholarshipName, + scholarshipType, + dteSeatType, + dteUserPassword, + dteUserId, + // }, + // graduationDetails: { + graduationInstitute, + graduationBranch, + graduationDegree, + graduationMarksPct, + graduationsPassingYear, + urbanRural, + scholarshipNumber, + lastSchoolCollegeAttended, + // }, + }, + session, + ); + const newStdMedHistory = await createStdMedHistory( + { + uid, + bloodGroup, + pastMedicalHistory, + immunisationHistory, + chronicMedicalConditions, + parentsEmailId, + parentsContact, + relativeContacts, + }, + session, + ); + const newStdPersonal = await createStdPersonal( + { + uid, + title, + firstName, + middleName, + motherName, + gender, + dob, + age, + birthPlace, + nationality, + motherTongue, + domicileState, + religion, + castCategory, + maharashtraKarnatakaBorderCandidate, + castDescription, + subCasteDescription, + nonCreamyLayerCertificateAttached, + hobby, + passportNo, + bloodGroup, + physicallyHandicapped, + studentMobNo, + studentMail, + parentMobNo, + parentMail, + perAddrDescr, + perPlotNo, + perStreetName, + perStuAddr1, + perStuAddr2, + city, + percellphone, + perpincode, + perresiphone, + permailaddress, + country, + state, + district, + tahsil, + correspondanceAddrDescr, + correspondancePlotNo, + correspondanceStreetName, + correspondanceStuAddr1, + correspondanceStuAddr2, + correspondanceCity, + correspondanceCellPhone, + correspondancePincode, + correspondanceResiPhone, + correspondanceMailAddress, + correspondanceCountry, + correspondanceState, + correspondanceDistrict, + correspondanceTahsil, + fatherDetails, + fathersOccupation, + parentsFirstName, + parentsMiddleName, + parentsLastName, + guardianMobNo, + guardianMailId, + nameAsPerTc, + casteAsPerTc, + birthStatus, + maritalStatus, + panCardNo, + passportExpiry, + drivingLicNo, + drivingLicValidTo, + aadharCardNo, + electionCardNo, + motherMobNo, + motherEmailId, + parentIncome, + photoUploaded, + signUploaded, + thumbUploaded, + noOfDocumentsUploaded, + }, + session, + ); + await session.commitTransaction(); + res.json({ + res: `added user ${newStudent.id} ${newStdBank.bankAccount} ,${newstdCollege.enrollmentNo}, ${newStdEduHistory.uid},${newStdMedHistory.uid},${newStdPersonal.uid}`, + id: newStudent.id, + }); + } catch (err) { + await session.abortTransaction(); + console.log(`aborted by catch + ${err}`); + } finally { + session.endSession(); + } } else { let error = ""; // eslint-disable-line prefer-const - if (!isBranchValid) error.concat("Invalid branch"); - if (!isCourseValid) error.concat(" Invalid course opted"); + if (!isBranchValid) error = error.concat("Invalid branch"); + if (!isCourseValid) error = error.concat(" Invalid course opted"); res.status(400).json({ err: error }); } } catch (caughtError) { diff --git a/models/student.js b/models/student.js index f72d06d..c02c1b0 100644 --- a/models/student.js +++ b/models/student.js @@ -39,7 +39,7 @@ async function create(studentData) { rollNo, coursesOpted, }); - const studentDoc = await student.save(); + const studentDoc = await student.save({ session: studentData.session }); return studentDoc; } diff --git a/models/student/stdBank.js b/models/student/stdBank.js index f998afb..df9d7c3 100644 --- a/models/student/stdBank.js +++ b/models/student/stdBank.js @@ -49,7 +49,7 @@ async function create(studentBankData) { bankMicr, }); - const stdBankDoc = await stdBank.save(); + const stdBankDoc = await stdBank.save({ session: studentBankData.session }); return stdBankDoc; } diff --git a/models/student/stdCollege.js b/models/student/stdCollege.js index 8ef0fba..be5c212 100644 --- a/models/student/stdCollege.js +++ b/models/student/stdCollege.js @@ -93,7 +93,9 @@ async function create(studentCollegeData) { enrollmentNo, }); - const stdCollegeDoc = await stdCollege.save(); + const stdCollegeDoc = await stdCollege.save({ + session: studentCollegeData.session, + }); return stdCollegeDoc; } diff --git a/models/student/stdEduHistory.js b/models/student/stdEduHistory.js index 9fca7c0..5de7f3f 100644 --- a/models/student/stdEduHistory.js +++ b/models/student/stdEduHistory.js @@ -3,56 +3,56 @@ import connector from "#models/databaseUtil"; const studentEducationSchema = { uid: { type: String, required: true }, // tenth_details - tenth: { - marks: { type: String, required: true }, - percentage: { type: Number, required: true }, - seatNumber: { type: String, required: true }, - examName: { type: String, required: true }, - examBoard: { type: String, required: true }, - msOms: { type: String, required: true }, - meritNumberInQualifyingExam: { type: String, required: true }, - admittedNumber: { type: String, required: true }, - }, - cetHscDetails: { - cetRollNo: { type: String, required: true }, - cetMarks: { type: String, required: true }, - qualifyingExamForAdmission: { type: String, required: true }, - stdType: { type: String, required: true }, - streamOpted: { type: String, required: true }, - mediumOfInstruction: { type: String, required: true }, - aggTotalMarks: { type: Number, required: true }, - totalMarksOutOf: { type: Number, required: true }, - percentOfMarks: { type: String, required: true }, - attemptNo: { type: String, required: true }, - passingMonth: { type: String, required: true }, - passingYear: { type: String, required: true }, - institutionName: { type: String, required: true }, - educBoardName: { type: String, required: true }, - pcmPercent: { type: String, required: true }, - pbmPercent: { type: String, required: true }, - stuQualifyingExam: { type: String, required: true }, - marksObtained: { type: String, required: true }, - stateRank: { type: String, required: true }, - prevExamSeatNumber: { type: String, required: false }, - prevTcNumber: { type: String, required: false }, - hscPassedSchoolName: { type: String, required: true }, - boardPattern: { type: String, required: true }, - scholarshipName: { type: String, required: false }, - scholarshipType: { type: String, required: false }, - dteSeatType: { type: String, required: true }, - dteUserPassword: { type: String, required: true }, - dteUserId: { type: String, required: true }, - }, - graduationDetails: { - graduationInstitute: { type: String, required: true }, - graduationBranch: { type: String, required: true }, - graduationDegree: { type: String, required: true }, - graduationMarksPct: { type: Number, required: true }, - graduationsPassingYear: { type: String, required: true }, - urbanRural: { type: String, required: true }, - scholarshipNumber: { type: String, required: false }, - lastSchoolCollegeAttended: { type: String, required: true }, - }, + // tenth: { + marks: { type: String, required: true }, + percentage: { type: Number, required: true }, + seatNumber: { type: String, required: true }, + examName: { type: String, required: true }, + examBoard: { type: String, required: true }, + msOms: { type: String, required: true }, + meritNumberInQualifyingExam: { type: String, required: true }, + admittedNumber: { type: String, required: true }, + // }, + // cetHscDetails: { + cetRollNo: { type: String, required: true }, + cetMarks: { type: String, required: true }, + qualifyingExamForAdmission: { type: String, required: true }, + stdType: { type: String, required: true }, + streamOpted: { type: String, required: true }, + mediumOfInstruction: { type: String, required: true }, + aggTotalMarks: { type: Number, required: true }, + totalMarksOutOf: { type: Number, required: true }, + percentOfMarks: { type: String, required: true }, + attemptNo: { type: String, required: true }, + passingMonth: { type: String, required: true }, + passingYear: { type: String, required: true }, + institutionName: { type: String, required: true }, + educBoardName: { type: String, required: true }, + pcmPercent: { type: String, required: true }, + pbmPercent: { type: String, required: true }, + stuQualifyingExam: { type: String, required: true }, + marksObtained: { type: String, required: true }, + stateRank: { type: String, required: true }, + prevExamSeatNumber: { type: String, required: false }, + prevTcNumber: { type: String, required: false }, + hscPassedSchoolName: { type: String, required: true }, + boardPattern: { type: String, required: true }, + scholarshipName: { type: String, required: false }, + scholarshipType: { type: String, required: false }, + dteSeatType: { type: String, required: true }, + dteUserPassword: { type: String, required: true }, + dteUserId: { type: String, required: true }, + // }, + // graduationDetails: { + graduationInstitute: { type: String, required: true }, + graduationBranch: { type: String, required: true }, + graduationDegree: { type: String, required: true }, + graduationMarksPct: { type: Number, required: true }, + graduationsPassingYear: { type: String, required: true }, + urbanRural: { type: String, required: true }, + scholarshipNumber: { type: String, required: false }, + lastSchoolCollegeAttended: { type: String, required: true }, + // }, }; const StudentEducation = connector.model( @@ -63,110 +63,110 @@ const StudentEducation = connector.model( async function create(studentEducationData) { const { uid, - tenth: { - marks, - percentage, - seatNumber, - examName, - examBoard, - msOms, - meritNumberInQualifyingExam, - admittedNumber, - }, - cetHscDetails: { - cetRollNo, - cetMarks, - qualifyingExamForAdmission, - stdType, - streamOpted, - mediumOfInstruction, - aggTotalMarks, - totalMarksOutOf, - percentOfMarks, - attemptNo, - passingMonth, - passingYear, - institutionName, - educBoardName, - pcmPercent, - pbmPercent, - stuQualifyingExam, - marksObtained, - stateRank, - prevExamSeatNumber, - prevTcNumber, - hscPassedSchoolName, - boardPattern, - scholarshipName, - scholarshipType, - dteSeatType, - dteUserPassword, - dteUserId, - }, - graduationDetails: { - graduationInstitute, - graduationBranch, - graduationDegree, - graduationMarksPct, - graduationsPassingYear, - urbanRural, - scholarshipNumber, - lastSchoolCollegeAttended, - }, + // tenth: { + marks, + percentage, + seatNumber, + examName, + examBoard, + msOms, + meritNumberInQualifyingExam, + admittedNumber, + // }, + // cetHscDetails: { + cetRollNo, + cetMarks, + qualifyingExamForAdmission, + stdType, + streamOpted, + mediumOfInstruction, + aggTotalMarks, + totalMarksOutOf, + percentOfMarks, + attemptNo, + passingMonth, + passingYear, + institutionName, + educBoardName, + pcmPercent, + pbmPercent, + stuQualifyingExam, + marksObtained, + stateRank, + prevExamSeatNumber, + prevTcNumber, + hscPassedSchoolName, + boardPattern, + scholarshipName, + scholarshipType, + dteSeatType, + dteUserPassword, + dteUserId, + // }, + // graduationDetails: { + graduationInstitute, + graduationBranch, + graduationDegree, + graduationMarksPct, + graduationsPassingYear, + urbanRural, + scholarshipNumber, + lastSchoolCollegeAttended, + // }, } = studentEducationData; const stdEducation = new StudentEducation({ uid, - tenth: { - marks, - percentage, - seatNumber, - examName, - examBoard, - msOms, - meritNumberInQualifyingExam, - admittedNumber, - }, - cetHscDetails: { - cetRollNo, - cetMarks, - qualifyingExamForAdmission, - stdType, - streamOpted, - mediumOfInstruction, - aggTotalMarks, - totalMarksOutOf, - percentOfMarks, - attemptNo, - passingMonth, - passingYear, - institutionName, - educBoardName, - pcmPercent, - pbmPercent, - stuQualifyingExam, - marksObtained, - stateRank, - prevExamSeatNumber, - prevTcNumber, - hscPassedSchoolName, - boardPattern, - scholarshipName, - scholarshipType, - dteSeatType, - dteUserPassword, - dteUserId, - }, - graduationDetails: { - graduationInstitute, - graduationBranch, - graduationDegree, - graduationMarksPct, - graduationsPassingYear, - urbanRural, - scholarshipNumber, - lastSchoolCollegeAttended, - }, + // tenth: { + marks, + percentage, + seatNumber, + examName, + examBoard, + msOms, + meritNumberInQualifyingExam, + admittedNumber, + // }, + // cetHscDetails: { + cetRollNo, + cetMarks, + qualifyingExamForAdmission, + stdType, + streamOpted, + mediumOfInstruction, + aggTotalMarks, + totalMarksOutOf, + percentOfMarks, + attemptNo, + passingMonth, + passingYear, + institutionName, + educBoardName, + pcmPercent, + pbmPercent, + stuQualifyingExam, + marksObtained, + stateRank, + prevExamSeatNumber, + prevTcNumber, + hscPassedSchoolName, + boardPattern, + scholarshipName, + scholarshipType, + dteSeatType, + dteUserPassword, + dteUserId, + // }, + // graduationDetails: { + graduationInstitute, + graduationBranch, + graduationDegree, + graduationMarksPct, + graduationsPassingYear, + urbanRural, + scholarshipNumber, + lastSchoolCollegeAttended, + // }, }); const stdEducationDoc = await stdEducation.save(); return stdEducationDoc; @@ -183,7 +183,7 @@ async function read(filter, limit = 0, page = 1) { } async function update(filter, updateObject, options = { multi: true }) { - const updateResult = await studentEducationSchema.updateMany( + const updateResult = await StudentEducation.updateMany( filter, { $set: updateObject }, options, @@ -192,7 +192,7 @@ async function update(filter, updateObject, options = { multi: true }) { } async function remove(stdEducationId) { - const deleteResult = await studentEducationSchema.deleteMany(stdEducationId); + const deleteResult = await StudentEducation.deleteMany(stdEducationId); return deleteResult.acknowledged; } diff --git a/models/student/stdMedHistory.js b/models/student/stdMedHistory.js index 4d3f63a..9d14998 100644 --- a/models/student/stdMedHistory.js +++ b/models/student/stdMedHistory.js @@ -41,7 +41,9 @@ async function create(studentMedicalData) { parentsContact, relativeContacts, }); - const medicalHistoryDoc = await medicalHistory.save(); + const medicalHistoryDoc = await medicalHistory.save({ + session: studentMedicalData.session, + }); return medicalHistoryDoc; } diff --git a/models/student/stdPersonal.js b/models/student/stdPersonal.js index 3659109..cdb9a4e 100644 --- a/models/student/stdPersonal.js +++ b/models/student/stdPersonal.js @@ -248,7 +248,7 @@ async function create(studentData) { thumbUploaded, noOfDocumentsUploaded, }); - const studentDoc = await student.save(); + const studentDoc = await student.save({ session: studentData.session }); return studentDoc; } diff --git a/services/student.js b/services/student.js index 235706b..5cbfab3 100644 --- a/services/student.js +++ b/services/student.js @@ -1,15 +1,14 @@ import Student from "#models/student"; import databaseError from "#error/database"; +import stdBank from "#models/student/stdBank"; +import stdCollege from "#models/student/stdCollege"; +import stdEduHistory from "#models/student/stdEduHistory"; +import stdMedHistory from "#models/student/stdMedHistory"; +import stdPersonal from "#models/student/stdPersonal"; -export async function createStudent( - ERPID, - name, - joiningYear, - branch, - division, - rollNo, - coursesOpted, -) { +export async function createStudent(studentDetails, session) { + const { ERPID, name, joiningYear, branch, division, rollNo, coursesOpted } = + studentDetails; const newStudent = await Student.create({ ERPID, name, @@ -18,6 +17,7 @@ export async function createStudent( division, rollNo, coursesOpted, + session, }); if (newStudent.name === name) { return newStudent; @@ -25,6 +25,368 @@ export async function createStudent( throw new databaseError.DataEntryError("student"); } +export async function createStdBank(studentBankdetails, session) { + const { uid, bankName, bankAccount, bankBranch, bankIfsc, bankMicr } = + studentBankdetails; + const newStdBank = await stdBank.create({ + uid, + bankName, + bankAccount, + bankBranch, + bankIfsc, + bankMicr, + session, + }); + if (newStdBank.uid === uid) { + return newStdBank; + } + throw new databaseError.DataEntryError("stdBank"); +} + +export async function createStdCollege(studentCollegeDetails, session) { + const { + uid, + admissionYear, + studentCode, + rollNo, + admissionStatus, + admissionPattern, + admissionCategory, + seatDesc, + quotaType, + isBoarderStudent, + seatType, + seatSubType, + eligibilityNo, + enrollmentNo, + } = studentCollegeDetails; + const newstdCollege = await stdCollege.create({ + uid, + admissionYear, + studentCode, + rollNo, + admissionStatus, + admissionPattern, + admissionCategory, + seatDesc, + quotaType, + isBoarderStudent, + seatType, + seatSubType, + eligibilityNo, + enrollmentNo, + session, + }); + if (newstdCollege.uid === uid) { + return newstdCollege; + } + throw new databaseError.DataEntryError("stdCollege"); +} + +export async function createStdEduHistory(studentEducationHistory, session) { + const { + uid, + marks, + percentage, + seatNumber, + examName, + examBoard, + msOms, + meritNumberInQualifyingExam, + admittedNumber, + cetRollNo, + cetMarks, + qualifyingExamForAdmission, + stdType, + streamOpted, + mediumOfInstruction, + aggTotalMarks, + totalMarksOutOf, + percentOfMarks, + attemptNo, + passingMonth, + passingYear, + institutionName, + educBoardName, + pcmPercent, + pbmPercent, + stuQualifyingExam, + marksObtained, + stateRank, + prevExamSeatNumber, + prevTcNumber, + hscPassedSchoolName, + boardPattern, + scholarshipName, + scholarshipType, + dteSeatType, + dteUserPassword, + dteUserId, + graduationInstitute, + graduationBranch, + graduationDegree, + graduationMarksPct, + graduationsPassingYear, + urbanRural, + scholarshipNumber, + lastSchoolCollegeAttended, + } = studentEducationHistory; + const newStdEduHistory = await stdEduHistory.create({ + uid, + // tenth: { + marks, + percentage, + seatNumber, + examName, + examBoard, + msOms, + meritNumberInQualifyingExam, + admittedNumber, + // }, + // cetHscDetails: { + cetRollNo, + cetMarks, + qualifyingExamForAdmission, + stdType, + streamOpted, + mediumOfInstruction, + aggTotalMarks, + totalMarksOutOf, + percentOfMarks, + attemptNo, + passingMonth, + passingYear, + institutionName, + educBoardName, + pcmPercent, + pbmPercent, + stuQualifyingExam, + marksObtained, + stateRank, + prevExamSeatNumber, + prevTcNumber, + hscPassedSchoolName, + boardPattern, + scholarshipName, + scholarshipType, + dteSeatType, + dteUserPassword, + dteUserId, + // }, + // graduationDetails: { + graduationInstitute, + graduationBranch, + graduationDegree, + graduationMarksPct, + graduationsPassingYear, + urbanRural, + scholarshipNumber, + lastSchoolCollegeAttended, + // }, + session, + }); + if (newStdEduHistory.uid === uid) { + return newStdEduHistory; + } + throw new databaseError.DataEntryError("stdEduHistory"); +} + +export async function createStdMedHistory(studentMedicalHistory, session) { + const { + uid, + bloodGroup, + pastMedicalHistory, + immunisationHistory, + chronicMedicalConditions, + parentsEmailId, + parentsContact, + relativeContacts, + } = studentMedicalHistory; + const newStdMedHistory = await stdMedHistory.create({ + uid, + bloodGroup, + pastMedicalHistory, + immunisationHistory, + chronicMedicalConditions, + parentsEmailId, + parentsContact, + relativeContacts, + session, + }); + if (newStdMedHistory.uid === uid) { + return newStdMedHistory; + } + throw new databaseError.DataEntryError("stdMedHistory"); +} + +export async function createStdPersonal(studentPersonalDetails, session) { + const { + uid, + title, + firstName, + middleName, + motherName, + gender, + dob, + age, + birthPlace, + nationality, + motherTongue, + domicileState, + religion, + castCategory, + maharashtraKarnatakaBorderCandidate, + castDescription, + subCasteDescription, + nonCreamyLayerCertificateAttached, + hobby, + passportNo, + bloodGroup, + physicallyHandicapped, + studentMobNo, + studentMail, + parentMobNo, + parentMail, + perAddrDescr, + perPlotNo, + perStreetName, + perStuAddr1, + perStuAddr2, + city, + percellphone, + perpincode, + perresiphone, + permailaddress, + country, + state, + district, + tahsil, + correspondanceAddrDescr, + correspondancePlotNo, + correspondanceStreetName, + correspondanceStuAddr1, + correspondanceStuAddr2, + correspondanceCity, + correspondanceCellPhone, + correspondancePincode, + correspondanceResiPhone, + correspondanceMailAddress, + correspondanceCountry, + correspondanceState, + correspondanceDistrict, + correspondanceTahsil, + fatherDetails, + fathersOccupation, + parentsFirstName, + parentsMiddleName, + parentsLastName, + guardianMobNo, + guardianMailId, + nameAsPerTc, + casteAsPerTc, + birthStatus, + maritalStatus, + panCardNo, + passportExpiry, + drivingLicNo, + drivingLicValidTo, + aadharCardNo, + electionCardNo, + motherMobNo, + motherEmailId, + parentIncome, + photoUploaded, + signUploaded, + thumbUploaded, + noOfDocumentsUploaded, + } = studentPersonalDetails; + const newStdPersonal = await stdPersonal.create({ + uid, + title, + firstName, + middleName, + motherName, + gender, + dob, + age, + birthPlace, + nationality, + motherTongue, + domicileState, + religion, + castCategory, + maharashtraKarnatakaBorderCandidate, + castDescription, + subCasteDescription, + nonCreamyLayerCertificateAttached, + hobby, + passportNo, + bloodGroup, + physicallyHandicapped, + studentMobNo, + studentMail, + parentMobNo, + parentMail, + perAddrDescr, + perPlotNo, + perStreetName, + perStuAddr1, + perStuAddr2, + city, + percellphone, + perpincode, + perresiphone, + permailaddress, + country, + state, + district, + tahsil, + correspondanceAddrDescr, + correspondancePlotNo, + correspondanceStreetName, + correspondanceStuAddr1, + correspondanceStuAddr2, + correspondanceCity, + correspondanceCellPhone, + correspondancePincode, + correspondanceResiPhone, + correspondanceMailAddress, + correspondanceCountry, + correspondanceState, + correspondanceDistrict, + correspondanceTahsil, + fatherDetails, + fathersOccupation, + parentsFirstName, + parentsMiddleName, + parentsLastName, + guardianMobNo, + guardianMailId, + nameAsPerTc, + casteAsPerTc, + birthStatus, + maritalStatus, + panCardNo, + passportExpiry, + drivingLicNo, + drivingLicValidTo, + aadharCardNo, + electionCardNo, + motherMobNo, + motherEmailId, + parentIncome, + photoUploaded, + signUploaded, + thumbUploaded, + noOfDocumentsUploaded, + session, + }); + if (newStdPersonal.uid === uid) { + return newStdPersonal; + } + throw new databaseError.DataEntryError("stdPersonal"); +} + export async function updateStudentById(id, data) { const updated = await Student.update({ _id: id }, data); if (updated) { diff --git a/test/config/globalSetup.js b/test/config/globalSetup.js index 4ae3e37..6ff8211 100644 --- a/test/config/globalSetup.js +++ b/test/config/globalSetup.js @@ -1,5 +1,5 @@ import { spawn } from "child_process"; -import { MongoMemoryServer } from "mongodb-memory-server"; +import { MongoMemoryReplSet } from "mongodb-memory-server"; import { config } from "./config.js"; // eslint-disable-line import/extensions function runChildProcessWithTimeout(command, args, timeout) { @@ -36,7 +36,7 @@ export default async function globalSetup() { if (config.Memory) { // Config to decided if an mongodb-memory-server instance should be used // it"s needed in global space, because we don"t want to create a new instance every test-suite - const instance = await MongoMemoryServer.create(); + const instance = await MongoMemoryReplSet.create({ replSet: { count: 4 } }); const uri = instance.getUri(); global.MONGOINSTANCE = instance; process.env.DB_URL = uri.slice(0, uri.lastIndexOf("/")); diff --git a/test/routes/student.test.js b/test/routes/student.test.js index a5e6a84..6c6c1a1 100644 --- a/test/routes/student.test.js +++ b/test/routes/student.test.js @@ -3,6 +3,11 @@ import connector from "#models/databaseUtil"; import studentModel from "#models/student"; import courseModel from "#models/course"; // Update this import import departmentModel from "#models/department"; +import stdBank from "#models/student/stdBank"; +import stdCollege from "#models/student/stdCollege"; +import stdEduHistory from "#models/student/stdEduHistory"; +import stdMedHistory from "#models/student/stdMedHistory"; +import stdPersonal from "#models/student/stdPersonal"; jest.mock("#util"); const { agent } = global; @@ -10,16 +15,17 @@ const { agent } = global; let courseIds; let branchId; function cleanUp(callback) { - studentModel - .remove({ - branch: branchId, - }) - .then(() => { - connector.disconnect((DBerr) => { - if (DBerr) console.log("Database disconnect error : ", DBerr); - callback(); - }); + stdBank.remove({ uid: "USR25123456445" }); + stdCollege.remove({ uid: "USR25123456445" }); + stdEduHistory.remove({ uid: "USR25123456445" }); + stdMedHistory.remove({ uid: "USR25123456445" }); + stdPersonal.remove({ uid: "USR25123456445" }); + studentModel.remove({ branch: branchId }).then(() => { + connector.disconnect((DBerr) => { + if (DBerr) console.log("Database disconnect error : ", DBerr); + callback(); }); + }); } /* eslint-disable no-underscore-dangle */ @@ -42,44 +48,348 @@ afterAll((done) => { describe("Student API", () => { it("should create student", async () => { const response = await agent.post("/student/create").send({ - ERPID: "S1032220999", - name: "Arya", - joiningYear: 2020, + ERPID: "ST14556245", + name: "John", + joiningYear: 2022, branch: branchId, - division: "A", - rollNo: 12, + division: "B", + rollNo: 101, coursesOpted: courseIds, + uid: "USR25123456445", + bankName: "Example Bank", + bankAccount: "001234567890", + bankBranch: "Main Branch", + bankIfsc: "ABCD0123456", + bankMicr: "012345678", + admissionYear: "2022", + studentCode: "SC123", + admissionStatus: "Admitted", + admissionPattern: "Regular", + admissionCategory: "General", + seatDesc: "Engineering", + quotaType: "Government", + isBoarderStudent: true, + seatType: "Regular", + seatSubType: "Computer Science", + eligibilityNo: "EL12345", + enrollmentNo: "EN123456", + marks: "500", + percentage: 80.5, + seatNumber: "S123", + examName: "SSC", + examBoard: "State Board", + msOms: "OMS123", + meritNumberInQualifyingExam: "M123", + admittedNumber: "A123", + cetRollNo: "CET123", + cetMarks: "120", + qualifyingExamForAdmission: "HSC", + stdType: "Science", + streamOpted: "Engineering", + mediumOfInstruction: "English", + aggTotalMarks: 450, + totalMarksOutOf: 500, + percentOfMarks: "90%", + attemptNo: "1", + passingMonth: "May", + passingYear: "2022", + institutionName: "XYZ School", + educBoardName: "State Board", + pcmPercent: "85%", + pbmPercent: "88%", + stuQualifyingExam: "HSC", + marksObtained: "450", + stateRank: "10", + prevExamSeatNumber: "PREV123", + prevTcNumber: "TC123", + hscPassedSchoolName: "ABC College", + boardPattern: "State Board", + scholarshipName: "Merit Scholarship", + scholarshipType: "Merit", + dteSeatType: "Government", + dteUserPassword: "password123", + dteUserId: "user123", + graduationInstitute: "XYZ University", + graduationBranch: "Computer Science", + graduationDegree: "B.Tech", + graduationMarksPct: 75.5, + graduationsPassingYear: "2026", + urbanRural: "Urban", + scholarshipNumber: "SCHOLAR123", + lastSchoolCollegeAttended: "ABC School", + bloodGroup: "A+", + pastMedicalHistory: "No significant past medical history.", + immunisationHistory: "Up-to-date immunizations.", + chronicMedicalConditions: "None", + parentsEmailId: "parent@example.com", + parentsContact: 1234567890, + relativeContacts: 9876543210, + title: "Mr", + firstName: "John", + middleName: "Doe", + motherName: "Jane Doe", + gender: "Male", + dob: "2023-05-07T06:10:19.426+00:00", + age: 32, + birthPlace: "Cityville", + nationality: "Indian", + motherTongue: "English", + domicileState: "StateXYZ", + religion: "Hindu", + castCategory: "General", + maharashtraKarnatakaBorderCandidate: true, + castDescription: "CasteXYZ", + subCasteDescription: "SubCasteXYZ", + nonCreamyLayerCertificateAttached: true, + hobby: "Reading", + passportNo: 1234567890, + physicallyHandicapped: false, + studentMobNo: 9876543210, + studentMail: "john.doe@example.com", + parentMobNo: 7890123456, + parentMail: "parent.doe@example.com", + perAddrDescr: "Permanent Address", + perPlotNo: 10, + perStreetName: "StreetXYZ", + perStuAddr1: "Address Line 1", + perStuAddr2: "Address Line 2", + city: "CityXYZ", + percellphone: 9876543210, + perpincode: 560001, + perresiphone: 7890123456, + permailaddress: "john.doe@home.com", + country: "India", + state: "StateXYZ", + district: "DistrictXYZ", + tahsil: "TahsilXYZ", + correspondanceAddrDescr: "Correspondence Address", + correspondancePlotNo: 20, + correspondanceStreetName: "Correspondence Street", + correspondanceStuAddr1: "Correspondence Address Line 1", + correspondanceStuAddr2: "Correspondence Address Line 2", + correspondanceCity: "Correspondence City", + correspondanceCellPhone: 9876543210, + correspondancePincode: 560002, + correspondanceResiPhone: 7890123456, + correspondanceMailAddress: "john.doe@correspondence.com", + correspondanceCountry: "India", + correspondanceState: "StateXYZ", + correspondanceDistrict: "DistrictXYZ", + correspondanceTahsil: "TahsilXYZ", + fatherDetails: "FatherXYZ", + fathersOccupation: "OccupationXYZ", + parentsFirstName: "ParentFirst", + parentsMiddleName: "ParentMiddle", + parentsLastName: "ParentLast", + guardianMobNo: 8765432109, + guardianMailId: "guardian@example.com", + nameAsPerTc: "John Doe", + casteAsPerTc: "CasteXYZ", + birthStatus: "Alive", + maritalStatus: true, + panCardNo: 987654321, + passportExpiry: "2023-05-07T06:10:19.426+00:00", + drivingLicNo: 123456789, + drivingLicValidTo: "2023-05-07T06:10:19.426+00:00", + aadharCardNo: 123456789012, + electionCardNo: 9876543210, + motherMobNo: 8765432109, + motherEmailId: "mother.doe@example.com", + parentIncome: 100000, + photoUploaded: true, + signUploaded: true, + thumbUploaded: true, + noOfDocumentsUploaded: 5, }); expect(response.status).toBe(200); expect(response.body.res).toMatch(/added user/); - }); + await Promise.all([ + stdBank.remove({ uid: "USR25123456445" }), + stdCollege.remove({ uid: "USR25123456445" }), + stdEduHistory.remove({ uid: "USR25123456445" }), + stdMedHistory.remove({ uid: "USR25123456445" }), + stdPersonal.remove({ uid: "USR25123456445" }), + ]); + }, 40000); describe("after adding student", () => { let id; beforeEach(async () => { id = await agent.post("/student/create").send({ - ERPID: "S1032220999", - name: "Arya", - joiningYear: 2020, + ERPID: "ST14556245", + name: "John", + joiningYear: 2022, branch: branchId, - division: "A", - rollNo: 12, + division: "B", + rollNo: 101, coursesOpted: courseIds, + uid: "USR25123456445", + bankName: "Example Bank", + bankAccount: "001234567890", + bankBranch: "Main Branch", + bankIfsc: "ABCD0123456", + bankMicr: "012345678", + admissionYear: "2022", + studentCode: "SC123", + admissionStatus: "Admitted", + admissionPattern: "Regular", + admissionCategory: "General", + seatDesc: "Engineering", + quotaType: "Government", + isBoarderStudent: true, + seatType: "Regular", + seatSubType: "Computer Science", + eligibilityNo: "EL12345", + enrollmentNo: "EN123456", + marks: "500", + percentage: 80.5, + seatNumber: "S123", + examName: "SSC", + examBoard: "State Board", + msOms: "OMS123", + meritNumberInQualifyingExam: "M123", + admittedNumber: "A123", + cetRollNo: "CET123", + cetMarks: "120", + qualifyingExamForAdmission: "HSC", + stdType: "Science", + streamOpted: "Engineering", + mediumOfInstruction: "English", + aggTotalMarks: 450, + totalMarksOutOf: 500, + percentOfMarks: "90%", + attemptNo: "1", + passingMonth: "May", + passingYear: "2022", + institutionName: "XYZ School", + educBoardName: "State Board", + pcmPercent: "85%", + pbmPercent: "88%", + stuQualifyingExam: "HSC", + marksObtained: "450", + stateRank: "10", + prevExamSeatNumber: "PREV123", + prevTcNumber: "TC123", + hscPassedSchoolName: "ABC College", + boardPattern: "State Board", + scholarshipName: "Merit Scholarship", + scholarshipType: "Merit", + dteSeatType: "Government", + dteUserPassword: "password123", + dteUserId: "user123", + graduationInstitute: "XYZ University", + graduationBranch: "Computer Science", + graduationDegree: "B.Tech", + graduationMarksPct: 75.5, + graduationsPassingYear: "2026", + urbanRural: "Urban", + scholarshipNumber: "SCHOLAR123", + lastSchoolCollegeAttended: "ABC School", + bloodGroup: "A+", + pastMedicalHistory: "No significant past medical history.", + immunisationHistory: "Up-to-date immunizations.", + chronicMedicalConditions: "None", + parentsEmailId: "parent@example.com", + parentsContact: 1234567890, + relativeContacts: 9876543210, + title: "Mr", + firstName: "John", + middleName: "Doe", + motherName: "Jane Doe", + gender: "Male", + dob: "2023-05-07T06:10:19.426+00:00", + age: 32, + birthPlace: "Cityville", + nationality: "Indian", + motherTongue: "English", + domicileState: "StateXYZ", + religion: "Hindu", + castCategory: "General", + maharashtraKarnatakaBorderCandidate: true, + castDescription: "CasteXYZ", + subCasteDescription: "SubCasteXYZ", + nonCreamyLayerCertificateAttached: true, + hobby: "Reading", + passportNo: 1234567890, + physicallyHandicapped: false, + studentMobNo: 9876543210, + studentMail: "john.doe@example.com", + parentMobNo: 7890123456, + parentMail: "parent.doe@example.com", + perAddrDescr: "Permanent Address", + perPlotNo: 10, + perStreetName: "StreetXYZ", + perStuAddr1: "Address Line 1", + perStuAddr2: "Address Line 2", + city: "CityXYZ", + percellphone: 9876543210, + perpincode: 560001, + perresiphone: 7890123456, + permailaddress: "john.doe@home.com", + country: "India", + state: "StateXYZ", + district: "DistrictXYZ", + tahsil: "TahsilXYZ", + correspondanceAddrDescr: "Correspondence Address", + correspondancePlotNo: 20, + correspondanceStreetName: "Correspondence Street", + correspondanceStuAddr1: "Correspondence Address Line 1", + correspondanceStuAddr2: "Correspondence Address Line 2", + correspondanceCity: "Correspondence City", + correspondanceCellPhone: 9876543210, + correspondancePincode: 560002, + correspondanceResiPhone: 7890123456, + correspondanceMailAddress: "john.doe@correspondence.com", + correspondanceCountry: "India", + correspondanceState: "StateXYZ", + correspondanceDistrict: "DistrictXYZ", + correspondanceTahsil: "TahsilXYZ", + fatherDetails: "FatherXYZ", + fathersOccupation: "OccupationXYZ", + parentsFirstName: "ParentFirst", + parentsMiddleName: "ParentMiddle", + parentsLastName: "ParentLast", + guardianMobNo: 8765432109, + guardianMailId: "guardian@example.com", + nameAsPerTc: "John Doe", + casteAsPerTc: "CasteXYZ", + birthStatus: "Alive", + maritalStatus: true, + panCardNo: 987654321, + passportExpiry: "2023-05-07T06:10:19.426+00:00", + drivingLicNo: 123456789, + drivingLicValidTo: "2023-05-07T06:10:19.426+00:00", + aadharCardNo: 123456789012, + electionCardNo: 9876543210, + motherMobNo: 8765432109, + motherEmailId: "mother.doe@example.com", + parentIncome: 100000, + photoUploaded: true, + signUploaded: true, + thumbUploaded: true, + noOfDocumentsUploaded: 5, }); id = JSON.parse(id.res.text).id; }); afterEach(async () => { - await studentModel.remove({ - ERPID: "S1032220999", - name: "Arya", - joiningYear: 2020, - branch: branchId, - division: "A", - rollNo: 12, - coursesOpted: courseIds, - }); + await Promise.all([ + stdBank.remove({ uid: "USR25123456445" }), + stdCollege.remove({ uid: "USR25123456445" }), + stdEduHistory.remove({ uid: "USR25123456445" }), + stdMedHistory.remove({ uid: "USR25123456445" }), + stdPersonal.remove({ uid: "USR25123456445" }), + studentModel.remove({ + ERPID: "ST14556245", + name: "John", + joiningYear: 2022, + branch: branchId, + division: "B", + rollNo: 101, + coursesOpted: courseIds, + }), + ]); }); it("should read student", async () => {