diff --git a/_apidoc.js b/_apidoc.js index 271744f..4529f56 100644 --- a/_apidoc.js +++ b/_apidoc.js @@ -1,17 +1,161 @@ +// ------------------------------------------------------------------------------------------ +// General apiDoc documentation blocks and old history blocks. +// ------------------------------------------------------------------------------------------ +// ------------------------------------------------------------------------------------------ +// Current Success. +// ------------------------------------------------------------------------------------------ +// ------------------------------------------------------------------------------------------ +// Current Errors. +// ------------------------------------------------------------------------------------------ +// ------------------------------------------------------------------------------------------ +// Current Permissions. +// ------------------------------------------------------------------------------------------ +// ------------------------------------------------------------------------------------------ +// History. +// ------------------------------------------------------------------------------------------ + + +// ------------------------------------------------------------------------------------------ +// Index. +// ------------------------------------------------------------------------------------------ + +/** + * @api {get} / Retrieve Home Information + * @apiName GetIndex + * @apiGroup index + * + * @apiSuccess {String} res server working. + */ + +// ------------------------------------------------------------------------------------------ +// Auth. +// ------------------------------------------------------------------------------------------ + +/** + * @api {post} /auth Login User + * @apiName LoginUser + * @apiGroup Authentication + * + * @apiBody {String} id User ID. + * @apiBody {String} password User password. + * + * @apiSuccess {String} res Response message. + * @apiSuccess {Object} user User details. + * @apiSuccess {String} user.uid User ID. + * @apiSuccess {String} user.name User name. + * @apiSuccess {String} user.emailId User email ID. + * @apiSuccess {String} user.type User type. + * @apiSuccess {String} user.token User token. + * + * @apiSuccessExample Success Response: + * HTTP/1.1 200 OK + * { + * "res": "welcome", + * "user": { + * "uid": "123", + * "name": "Some User", + * "emailId": "someuser@example.com", + * "type": "user", + * "token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9..." + * } + * } + * + * @apiError (Error 403) UserDoesNotExist Incorrect ID or password. + * @apiError (Error 500) ServerError Something is wrong on our side. Try again. + */ + +/** + * @api {post} /auth/validateUser Validate User + * @apiName ValidateUser + * @apiGroup Authentication + * @apiDescription Validates the user's authentication token. + * + * @apiHeader {String} Authorization User's authentication token. + * + * @apiSuccess {Object} res User object. + * @apiSuccess {Object} res.user User details. + * @apiSuccess {String} res.user.uid User ID. + * @apiSuccess {String} res.user.name User name. + * @apiSuccess {String} res.user.emailId User email ID. + * @apiSuccess {String} res.user.type User type. + * + * @apiSuccessExample Success Response: + * HTTP/1.1 200 OK + * { + * "res": { + * "user": { + * "uid": "123", + * "name": "Some User", + * "emailId": "someuser@example.com", + * "type": "user" + * }, + * "msg": "user validated", + * "err": null + * } + * } + */ + +/** + * @api {post} /auth/sendOTP Send OTP + * @apiName SendOTP + * @apiGroup Authentication + * @apiDescription Sends an OTP (One-Time Password) to the user's email ID. + * + * @apiBody {String} uid User ID. + * @apiBody {String} emailId User email ID. + * + * @apiSuccess {String} res Response message. + * + * @apiSuccessExample Success Response: + * HTTP/1.1 200 OK + * { + * "res": "otp sent to emailID" + * } + * + * @apiError (Error) IncorrectUidOrEmail Incorrect UID or emailId. + */ + +/** + * @api {post} /auth/resetPassword Reset Password + * @apiName ResetPassword + * @apiGroup Authentication + * @apiDescription Resets the user's password using the provided OTP (One-Time Password). + * + * @apiBody {String} uid User ID. + * @apiBody {String} otp One-Time Password received by the user. + * @apiBody {String} password New password. + * + * @apiSuccess {String} res Response message. + * + * @apiSuccessExample Success Response: + * HTTP/1.1 200 OK + * { + * "res": "successfully updated password" + * } + * + * @apiError (Error) IncorrectOtp Incorrect OTP. + * @apiError (Error 500) UpdateError Something went wrong while updating password. + * @apiError (Error 500) ServerError Something went wrong. + */ + +// ------------------------------------------------------------------------------------------ +// Infrastructure. +// ------------------------------------------------------------------------------------------ + /** * @api {post} /infrastructure/add Add Infrastructure * @apiName AddInfrastructure * @apiGroup Infrastructure * - * @apiParam {String} name The name of the infrastructure. - * @apiParam {String} type The type of the infrastructure. - * @apiParam {String} wing The wing where the infrastructure is located. - * @apiParam {Number} floor The floor where the infrastructure is located. - * @apiParam {Number} capacity The capacity of the infrastructure. + * @apiBody {String} name The name of the infrastructure. + * @apiBody {String} type The type of the infrastructure. + * @apiBody {String} wing The wing where the infrastructure is located. + * @apiBody {Number} floor The floor where the infrastructure is located. + * @apiBody {Number} capacity The capacity of the infrastructure. * * @apiSuccess {String} res Success message with the ID of the added infrastructure. * * @apiError (Error 500) DatabaseError Error while inserting in the database. * * @apiDescription Adds a new infrastructure to the system. - */ \ No newline at end of file + */ diff --git a/controller/accreditation.js b/controller/accreditation.js index 8d1f285..b3a5209 100644 --- a/controller/accreditation.js +++ b/controller/accreditation.js @@ -1,4 +1,4 @@ -import { addNewAccreditation } from "#services/accreditation"; +import { addNewAccreditation, deleteAccreditationById } from "#services/accreditation"; import { logger } from "#util"; async function addAccreditation(req, res) { @@ -15,5 +15,15 @@ async function addAccreditation(req, res) { res.json({ err: "Error while inserting in DB" }); } } - -export default { addAccreditation }; +async function deleteAccreditation(req, res) { + const { accredationId} = req.params; + try { + await deleteAccreditationById(accredationId); + res.json({ res: "Accreditation deleted successfully" }); + } catch (error) { + logger.error("Error while deleting", error); + res.status(500); + res.json({ err: "Error while deleting from DB" }); + } +} +export default { addAccreditation, deleteAccreditation }; diff --git a/controller/auth.js b/controller/auth.js index 3db5753..c1be184 100644 --- a/controller/auth.js +++ b/controller/auth.js @@ -51,7 +51,7 @@ async function resetPassword(req, res) { await updatePassword(uid, password); res.json({ res: "successfully updated password" }); } catch (error) { - logger.log("Error while updating", error); + logger.error("Error while updating", error); res.status(500); if (error.name === "UpdateError") res.json({ err: "Something went wrong while updating password" }); else res.json({ err: "something went wrong" }); diff --git a/controller/infrastructure.js b/controller/infrastructure.js index 6ff9560..f49f27f 100644 --- a/controller/infrastructure.js +++ b/controller/infrastructure.js @@ -1,4 +1,4 @@ -import { createinfrastructure } from "#services/infrastructure"; +import { createInfrastructure } from "#services/infrastructure"; import { logger } from "#util"; async function addinfrastructure(req, res) { @@ -6,7 +6,7 @@ async function addinfrastructure(req, res) { name, type, wing, floor, capacity, } = req.body; try { - const newinfrastructure = await createinfrastructure(name, type, wing, floor, capacity); + const newinfrastructure = await createInfrastructure(name, type, wing, floor, capacity); res.json({ res: `added user ${newinfrastructure.id}` }); } catch (error) { logger.error("Error while inserting", error); diff --git a/models/accreditation.js b/models/accreditation.js index 26df3f1..2c81583 100644 --- a/models/accreditation.js +++ b/models/accreditation.js @@ -10,11 +10,14 @@ const accreditationSchema = { const Accreditation = connector.model("Accreditation", accreditationSchema); async function remove(filter) { - const res = await Accreditation.findOneAndDelete(filter); - return res; + const deleteResult = await Accreditation.deleteMany(filter); + return deleteResult.acknowledged; } -async function create(name, agencyName, dateofAccreditation, dateofExpiry) { +async function create(accreditationData) { + const { + name, agencyName, dateofAccreditation, dateofExpiry, + } = accreditationData; const accreditation = new Accreditation({ name, agencyName, @@ -26,13 +29,13 @@ async function create(name, agencyName, dateofAccreditation, dateofExpiry) { } async function read(filter, limit = 1) { - const accreditationData = await Accreditation.find(filter).limit(limit); - return accreditationData; + const accreditationDoc = await Accreditation.find(filter).limit(limit); + return accreditationDoc; } -async function update(filter, updateObject) { - const accreditation = await Accreditation.findOneAndUpdate(filter, updateObject, { new: true }); - return accreditation; +async function update(filter, updateObject, options = { multi: true }) { + const deleteResult = await Accreditation.updateMany(filter, { $set: updateObject }, options); + return deleteResult.acknowledged; } export default { diff --git a/models/assignment.js b/models/assignment.js index 8de36a2..9be376f 100644 --- a/models/assignment.js +++ b/models/assignment.js @@ -1,10 +1,11 @@ -import connector from '#models/databaseUtil'; +import connector from "#models/databaseUtil"; const assignmentSchema = { no: { type: Number, required: true }, title: { type: String, required: true }, - type: { type: String, required: true, enum: ['FA', 'RA'] }, + type: { type: String, required: true, enum: ["FA", "RA"] }, marks: { type: Number, required: true }, }; -const Assignment = connector.model('Assignment', assignmentSchema); +// eslint-disable-next-line no-unused-vars +const Assignment = connector.model("Assignment", assignmentSchema); diff --git a/models/attendance.js b/models/attendance.js index fd83f63..1fa5f49 100644 --- a/models/attendance.js +++ b/models/attendance.js @@ -1,4 +1,4 @@ -import connector from "./databaseUtil"; +import connector from "#models/databaseUtil"; connector.set("debug", true); @@ -13,52 +13,35 @@ const attendanceSchema = { const Attendance = connector.model("Attendance", attendanceSchema); -async function create(studentId, courseId) { - try { - const attendance = new Attendance({ - student: studentId, - course: courseId, - }); - const createAttendance = await attendance.save(); - return createAttendance; - } catch (error) { - console.error("Error creating attendance:", error); - return null; - } +async function create(attendanceData) { + const { + student, course, monthlyAttended, monthlyOccured, cumulativeAttended, cumulativeOccured, + } = attendanceData; + const attendance = new Attendance({ + student, + course, + monthlyAttended, + monthlyOccured, + cumulativeAttended, + cumulativeOccured, + }); + const attendanceDoc = await attendance.save(); + return attendanceDoc; } -async function read(attendanceId) { - try { - const attendance = await Attendance.findById(attendanceId); - return attendance; - } catch (error) { - console.error("error reading attendance", error); - return null; - } +async function read(filter, limit = 1) { + const attendanceDoc = await Attendance.find(filter).limit(limit); + return attendanceDoc; } -async function update(attendanceId, updateData) { - try { - const updatedAttendance = await Attendance.findByIdAndUpdate( - attendanceId, - updateData, - { new: true }, - ); - return updatedAttendance; - } catch (error) { - console.error("error updating attendance:", error); - return null; - } +async function update(filter, updateObject, options = { multi: true }) { + const updateResult = await Attendance.updateMany(filter, { $set: updateObject }, options); + return updateResult.acknowledged; } -async function remove(attendanceId) { - try { - const deletedAttendance = await Attendance.findByIdAndDelete(attendanceId); - return deletedAttendance; - } catch (error) { - console.error("error removing attendance", error); - return null; - } +async function remove(filter) { + const deleteResult = await Attendance.deleteMany(filter); + return deleteResult.acknowledged; } export default { create, remove, update, read, diff --git a/models/course.js b/models/course.js index ae5c6fe..413aaa0 100644 --- a/models/course.js +++ b/models/course.js @@ -1,4 +1,4 @@ -const { connector } = require('#models/databaseUtil') +const { connector } = require("#models/databaseUtil"); const courseSchema = { name: { type: String, required: true }, @@ -13,12 +13,12 @@ const courseSchema = { practicalMarks: { type: Number, required: true }, semester: { type: connector.Schema.Types.ObjectId, - ref: 'Semester', + ref: "Semester", required: true, }, subType: { type: String, - enum: ['open', 'professional', 'core'], + enum: ["open", "professional", "core"], required: true, }, // can be open, professional, or core prerequisites: { type: [String], required: true }, // array of strings @@ -29,55 +29,47 @@ const courseSchema = { RBTLevel: { type: [String] }, }, ], // this is the modules from syllabus - modules: [{ type: connector.Schema.Types.ObjectId, ref: 'Module' }], - practicals: [{ type: connector.Schema.Types.ObjectId, ref: 'Practical' }], - tutorials: [{ type: connector.Schema.Types.ObjectId, ref: 'Tutorial' }], - assignments: [{ type: connector.Schema.Types.ObjectId, ref: 'Assignment' }], + modules: [{ type: connector.Schema.Types.ObjectId, ref: "Module" }], + practicals: [{ type: connector.Schema.Types.ObjectId, ref: "Practical" }], + tutorials: [{ type: connector.Schema.Types.ObjectId, ref: "Tutorial" }], + assignments: [{ type: connector.Schema.Types.ObjectId, ref: "Assignment" }], reccTextbooks: { type: [String], required: true }, refBooks: { type: [String], required: true }, evalScheme: { type: [Number], required: true }, -} +}; // virtual for total hours -courseSchema.virtual('totalHours').get(function() { - return this.theoryHours + this.tutorialHours + this.practicalHours -}) +courseSchema.virtual("totalHours").get(() => this.theoryHours + this.tutorialHours + this.practicalHours); // virtual for theory marks -courseSchema.virtual('theoryMarks').get(function() { - return this.ISAMarks + this.ESEMarks -}) +courseSchema.virtual("theoryMarks").get(() => this.ISAMarks + this.ESEMarks); // virtual for total marks -courseSchema.virtual('totalMarks').get(function() { - return this.theoryMarks + this.tutorialMarks + this.practicalMarks -}) +courseSchema.virtual("totalMarks").get(() => this.theoryMarks + this.tutorialMarks + this.practicalMarks); -const Course = connector.model('Course', courseSchema) +const Course = connector.model("Course", courseSchema); /// CRUD operations /// async function create(courseData) { - const course = new Course(courseData) - const createdCourse = await course.save() - return createdCourse + const course = new Course(courseData); + const courseDoc = await course.save(); + return courseDoc; } async function read(filter, limit = 1) { - const courseData = await Course.find(filter).limit(limit) - return courseData + const courseDoc = await Course.find(filter).limit(limit); + return courseDoc; } -async function update(filter, updateObject) { - const updatedCourse = await Course.findOneAndUpdate(filter, updateObject, { - new: true, - }).exec() - return updatedCourse +async function update(filter, updateObject, options = { multi: true }) { + const updateResult = await Course.updateMany(filter, { $set: updateObject }, options); + return updateResult.acknowledged; } async function remove(filter) { - const deletedCourse = await Course.findOneAndDelete(filter).exec() - return deletedCourse + const deleteResult = await Course.deleteMany(filter).exec(); + return deleteResult.acknowledged; } export default { @@ -85,4 +77,4 @@ export default { read, update, remove, -} +}; diff --git a/models/department.js b/models/department.js index 0927a63..120bcf8 100644 --- a/models/department.js +++ b/models/department.js @@ -1,4 +1,4 @@ -import connector from '#models/databaseUtil'; +import connector from "#models/databaseUtil"; const departmentSchema = { name: { type: Number, required: true }, @@ -6,57 +6,52 @@ const departmentSchema = { yearOfStarting: { type: Date, immutable: true, required: true }, accreditations: [{ type: connector.Schema.Types.ObjectId, - ref: 'Accreditation', + ref: "Accreditation", required: true, }], infrastructures: [{ type: connector.Schema.Types.ObjectId, - ref: 'Infrastructure', + ref: "Infrastructure", required: true, }], }; -const Department = connector.model('Department', departmentSchema); +const Department = connector.model("Department", departmentSchema); -//for creating -async function create( - name, - acronym, - yearOfStarting, - accreditions, - infrastructures -){ - const department = new Department({ - name, - acronym, - yearOfStarting, - accreditions, - infrastructures - }); +// for creating +async function create(departmentData) { + const { + name, acronym, yearOfStarting, accreditions, infrastructures, + } = departmentData; + const department = new Department({ + name, + acronym, + yearOfStarting, + accreditions, + infrastructures, + }); const departmentDoc = await department.save(); return departmentDoc; - } +} - async function read(filter, limit = 1) { - const departmentData = await Department.find(filter).limit(limit); - return departmentData; - } +async function read(filter, limit = 1) { + const departmentDoc = await Department.find(filter).limit(limit); + return departmentDoc; +} + +async function update(filter, updateObject, options = { multi: true }) { + const updateResult = await Department.findOneAndUpdate(filter, { $set: updateObject }, options); + return updateResult.acknowledged; +} - async function update(filter, updateObject) { - const department = await Department.findOneAndUpdate(filter, updateObject, { - new: true, - }); - return department; - } +async function remove(filter) { + const deleteResult = await Department.findOneAndDelete(filter); + return deleteResult.acknowledged; +} - async function remove(filter) { - const res = await Department.findOneAndDelete(filter); - return res; - } - - export default { - create, - read, - update, - remove, - }; \ No newline at end of file +export default { + create, + read, + update, + remove, +}; diff --git a/models/faculty.js b/models/faculty.js index 54e6f72..9763213 100644 --- a/models/faculty.js +++ b/models/faculty.js @@ -1,94 +1,57 @@ -const { connector } = require("./databaseUtil"); +import connector from "#models/databaseUtil"; const facultySchema = { - name: { - type: String, - required: true, - }, - department: { - type: connector.Schema.Types.ObjectId, - ref: "Department", - required: true, - }, - empType: { - type: String, - required: true, - }, - emdUID: { - type: String, - required: true, - }, - preferredSubjects: { - type: [{ type: connector.Schema.Types.ObjectId, ref: "Subject" }], - required: true, - }, - profileLink: { - type: String, - required: true, - }, - designation: { - type: [String], - required: true, - }, - natureOfAssociation: { - type: String, - required: true, - }, - uniApprovalStatus: { - type: String, - required: true, - }, - qualifications: { - type: [String], - required: true, - }, - totalExperience: { - type: String, - required: true, - }, - additionalResponsibilities: { - type: String, - required: true, - }, - achievements: { - type: [String], - required: true, - }, - areaOfSpecialization: { - type: [String], - required: true, - }, - papersPublishedPG: { - type: Number, - required: true, - }, - papersPublishedUG: { - type: Number, - required: true, - }, - email: { - type: String, - required: true, - unique: true, - }, - phoneNumber: { - type: String, - required: true, - }, - office: { - type: String, - required: true, - }, - isTenured: { - type: Boolean, - default: false, - }, - joinedDate: { - type: Date, - default: Date.now, - }, + ERPID: { type: String, required: true }, + dateOfJoining: { type: Date, required: true, default: Date.now }, + dateOfLeaving: { type: Date, required: false }, + profileLink: { type: String, required: true }, + qualifications: { type: [String], required: true }, + totalExperience: { type: Number, required: true }, + achievements: { type: [String], required: true }, + areaOfSpecialization: { type: [String], required: true }, + papersPublishedPG: { type: Number, required: true }, + papersPublishedUG: { type: Number, required: true }, + department: { type: connector.Schema.Types.ObjectId, ref: "Department", required: true }, + preferredSubjects: [{ type: connector.Schema.Types.ObjectId, ref: "Course", required: true }], + designation: { type: [String], enum: ["HOD", "Assistant Professor", "Associate Professor", "Activity Head"], required: true }, + natureOfAssociation: { type: String, enum: ["Regular", "Contract", "Adjunct"], required: true }, + additionalResponsibilities: { type: String, required: true }, }; +facultySchema.virtual("tcetexperience").get(() => { + const currentDate = new Date(); + const joiningYear = this.dateOfJoining.getFullYear(); + const leavingYear = this.dateOfLeaving + ? this.dateOfLeaving.getFullYear + : currentDate.getFullYear; + return leavingYear - joiningYear; +}); + const Faculty = connector.model("Faculty", facultySchema); -module.exports = Faculty; +// CRUD Operations + +async function remove(filter) { + const deleteResult = await Faculty.deleteMany(filter); + return deleteResult.acknowledged; +} + +async function create(facultyData) { + const faculty = new Faculty(facultyData); + const facultyDoc = await faculty.save(); + return facultyDoc; +} + +async function read(filter, limit = 1) { + const facultyDoc = await Faculty.find(filter).limit(limit); + return facultyDoc; +} + +async function update(filter, updateObject, options = { multi: true }) { + const updateResult = await Faculty.updateMany(filter, { $set: updateObject }, options); + return updateResult.acknowledged; +} + +export default { + create, read, update, remove, +}; diff --git a/models/group.js b/models/group.js index 3f7a1d4..2f9f9f4 100644 --- a/models/group.js +++ b/models/group.js @@ -2,52 +2,31 @@ import connector from "#models/databaseUtil"; const groupSchema = { title: { type: String, required: true }, - students: [{ type: connector.Schema.Types.ObjectId, ref: "Student", required: true }] + students: [{ type: connector.Schema.Types.ObjectId, ref: "Student", required: true }], }; +const Group = connector.model("Group", groupSchema); -const groupModel = connector.model("group", groupSchema); - -async function create(title,student) { - try { - const newGroup = await groupModel.create(title , student); - return newGroup; - } catch (error) { - console.error("Error creating group:", error); - return null; - } +async function create(groupData) { + const { title, students } = groupData; + const groupDoc = await Group.create({ title, students }); + return groupDoc; } -async function read(groupId) { - try { - const group = await groupModel.findById(groupId); - return group; - } catch (error) { - console.error("Error retrieving group:", error); - return null; - } +async function read(filter, limit = 1) { + const groupDoc = await Group.find(filter).limit(limit); + return groupDoc; } -async function update(groupId, updateData) { - try { - const updatedGroup = await groupModel.findByIdAndUpdate(groupId, updateData, { new: true }); - return updatedGroup; - } catch (error) { - console.error("Error updating group:", error); - return null; - } +async function update(filter, updateObject, options = { multi: true }) { + const updateResult = await Group.updateManyupdateMany(filter, { $set: updateObject }, options); + return updateResult.acknowledged; } async function remove(groupId) { - try { - const deletedGroup = await groupModel.findByIdAndDelete(groupId); - return deletedGroup; - } catch (error) { - console.error("Error deleting group:", error); - return null; - } + const deleteResult = await Group.deleteMany(groupId); + return deleteResult.acknowledged; } - export default { create, read, diff --git a/models/infra.js b/models/infra.js deleted file mode 100644 index 0525c39..0000000 --- a/models/infra.js +++ /dev/null @@ -1,42 +0,0 @@ -import connector from "#models/databaseUtil"; - -const infrastructureSchema = { - name: { type: String, required: true }, - type: { type: String, required: true }, - wing: { type: String, required: true }, - floor: { type: Number, required: true }, - capacity: { type: Number, required: true }, -}; - -const Infrastructure = connector.model("Infrastructure", infrastructureSchema); - -async function remove(filter) { - const res = await Infrastructure.findOneAndDelete(filter); - return res; -} - -async function create(name, type, wing, floor, capacity) { - const infrastructure = new Infrastructure({ - name, - type, - wing, - floor, - capacity, - }); - const infrastructureDoc = await infrastructure.save(); - return infrastructureDoc; -} - -async function read(filter, limit = 1) { - const infrastructureData = await Infrastructure.find(filter).limit(limit); - return infrastructureData; -} - -async function update(filter, updateObject) { - const infrastructure = await Infrastructure.findOneAndUpdate(filter, updateObject, { new: true }); - return infrastructure; -} - -export default { - create, read, update, remove, -}; diff --git a/models/infrastructure.js b/models/infrastructure.js index 0525c39..b954f67 100644 --- a/models/infrastructure.js +++ b/models/infrastructure.js @@ -11,11 +11,14 @@ const infrastructureSchema = { const Infrastructure = connector.model("Infrastructure", infrastructureSchema); async function remove(filter) { - const res = await Infrastructure.findOneAndDelete(filter); - return res; + const deleteResult = await Infrastructure.deleteMany(filter); + return deleteResult.acknowledged; } -async function create(name, type, wing, floor, capacity) { +async function create(infrastructureData) { + const { + name, type, wing, floor, capacity, + } = infrastructureData; const infrastructure = new Infrastructure({ name, type, @@ -28,13 +31,13 @@ async function create(name, type, wing, floor, capacity) { } async function read(filter, limit = 1) { - const infrastructureData = await Infrastructure.find(filter).limit(limit); - return infrastructureData; + const infrastructureDoc = await Infrastructure.find(filter).limit(limit); + return infrastructureDoc; } -async function update(filter, updateObject) { - const infrastructure = await Infrastructure.findOneAndUpdate(filter, updateObject, { new: true }); - return infrastructure; +async function update(filter, updateObject, options = { multi: true }) { + const updateResult = await Infrastructure.updateMany(filter, { $set: updateObject }, options); + return updateResult.acknowledged; } export default { diff --git a/models/module.js b/models/module.js index 57155d8..8aa1a94 100644 --- a/models/module.js +++ b/models/module.js @@ -9,25 +9,21 @@ const moduleSchema = { cognitiveLevels: [{ type: String, required: true, - enum: ['L1', 'L2', 'L3', 'L4', 'L5', 'L6'], -}], + enum: ["L1", "L2", "L3", "L4", "L5", "L6"], + }], }; const Module = connector.model("Module", moduleSchema); async function remove(filter) { - const res = await Module.findOneAndDelete(filter); - return res; + const deleteResult = await Module.deleteMany(filter); + return deleteResult.acknowledged; } -async function create( - no, - name, - outcome, - contents, - hrsPerModule, - cognitiveLevels, -) { +async function create(moduleData) { + const { + no, name, outcome, contents, hrsPerModule, cognitiveLevels, + } = moduleData; const module = new Module({ no, name, @@ -41,15 +37,13 @@ async function create( } async function read(filter, limit = 1) { - const moduleData = await Module.find(filter).limit(limit); - return moduleData; + const moduleDoc = await Module.find(filter).limit(limit); + return moduleDoc; } -async function update(filter, updateObject) { - const module = await Module.findOneAndUpdate(filter, updateObject, { - new: true, - }); - return module; +async function update(filter, updateObject, options = { multi: true }) { + const updateResult = await Module.updateMany(filter, { $set: updateObject }, options); + return updateResult.acknowledged; } export default { diff --git a/models/organization.js b/models/organization.js index ebd8b4e..acd6bfe 100644 --- a/models/organization.js +++ b/models/organization.js @@ -1,39 +1,41 @@ -import connector from "./databaseUtil"; +import connector from "#models/databaseUtil"; const organizationSchema = { parent: { type: connector.Schema.Types.ObjectId, ref: "Organization", required: "true" }, startDate: { type: Date, required: true }, name: { type: String, required: true }, - accreditation: { type: connector.Schema.Types.ObjectId, ref: "Accrediation", required: "true" }, + accreditations: [{ type: connector.Schema.Types.ObjectId, ref: "Accrediation", required: "true" }], }; - const Organization = connector.model("Organization", organizationSchema); async function remove(filter) { - const res = await Organization.findOneAndDelete(filter); - return res; + const deleteResult = await Organization.deleteMany(filter); + return deleteResult.acknowledged; } -async function create(parent, startDate, name, accreditation) { - const org = new Organization({ +async function create(organizationData) { + const { + parent, startDate, name, accreditation, + } = organizationData; + const organization = new Organization({ parent, startDate, name, accreditation, }); - const orgDoc = await org.save(); - return orgDoc; + const organizationDoc = await organization.save(); + return organizationDoc; } async function read(filter, limit = 1) { - const orgData = await Organization.find(filter).limit(limit); - return orgData; + const organizationDoc = await Organization.find(filter).limit(limit); + return organizationDoc; } -async function update(filter, updateObject) { - const org = await Organization.findOneAndUpdate(filter, updateObject, { new: true }); - return org; +async function update(filter, updateObject, options = { multi: true }) { + const updateResult = await Organization.updateMany(filter, { $set: updateObject }, options); + return updateResult.acknowledged; } export default { diff --git a/models/otpStore.js b/models/otpStore.js index 6695e49..86ebb0d 100644 --- a/models/otpStore.js +++ b/models/otpStore.js @@ -8,11 +8,12 @@ const otpStoreSchema = { const OTPStore = connector.model("OTPStore", otpStoreSchema); async function remove(filter) { - const res = await OTPStore.findOneAndDelete(filter); - return res; + const deleteResult = await OTPStore.deleteMany(filter); + return deleteResult.acknowledged; } -async function create(uid, otp) { +async function create(otpData) { + const { uid, otp } = otpData; const otpStore = new OTPStore({ uid, otp, @@ -22,13 +23,13 @@ async function create(uid, otp) { } async function read(filter, limit = 1) { - const otpData = await OTPStore.find(filter).limit(limit); - return otpData; + const otpDoc = await OTPStore.find(filter).limit(limit); + return otpDoc; } -async function update(filter, updateObject) { - const otpDoc = await OTPStore.findOneAndUpdate(filter, updateObject, { upsert: true, new: true }); - return otpDoc; +async function update(filter, updateObject, options = { upsert: true }) { + const updateResult = await OTPStore.updateMany(filter, { $set: updateObject }, options); + return updateResult.acknowledged; } export default { diff --git a/models/practical.js b/models/practical.js index fbb8f7b..4df90d6 100644 --- a/models/practical.js +++ b/models/practical.js @@ -12,4 +12,5 @@ const practicalSchema = { }], }; +// eslint-disable-next-line no-unused-vars const Practical = connector.model("Practical", practicalSchema); diff --git a/models/topic.js b/models/topic.js new file mode 100644 index 0000000..a4f35e8 --- /dev/null +++ b/models/topic.js @@ -0,0 +1,7 @@ +import connector from "#models/databaseUtil"; + +const topicSchema = { + title: { type: String, required: true }, +}; +// eslint-disable-next-line no-unused-vars +const Topic = connector.model("topic", topicSchema); \ No newline at end of file diff --git a/models/tutorial.js b/models/tutorial.js index bfa9537..b32ed42 100644 --- a/models/tutorial.js +++ b/models/tutorial.js @@ -2,14 +2,14 @@ import connector from "#models/databaseUtil"; const tutorialSchema = { no: { type: Number, required: true }, - Title: { type: String, unique: true, required: true }, + title: { type: String, unique: true, required: true }, hours: { type: Number, required: true }, - cognitiveLevel:[{ - type:String, - enum:['L1','L2','L3','L4','L5','L6'], - default:'L1' - }] + cognitiveLevel: [{ + type: String, + enum: ["L1", "L2", "L3", "L4", "L5", "L6"], + default: "L1", + }], }; -const Tutorial= connector.model("Tutorial", tutorialSchema); - +// eslint-disable-next-line no-unused-vars +const Tutorial = connector.model("Tutorial", tutorialSchema); diff --git a/models/user.js b/models/user.js index 17e63cd..38ecbe6 100644 --- a/models/user.js +++ b/models/user.js @@ -13,15 +13,18 @@ const userSchema = { const User = connector.model("User", userSchema); async function remove(filter) { - const res = await User.findOneAndDelete(filter); - return res; + const deleteResult = await User.deleteMany(filter); + return deleteResult.acknowledged; } -async function create(name, pass, emailId, uid, userType) { - const password = await hashPassword(pass); +async function create(userData) { + const { + name, password, emailId, uid, userType, + } = userData; + const hashedPassword = await hashPassword(password); const user = new User({ name, - password, + password: hashedPassword, emailId, uid, userType, @@ -31,13 +34,13 @@ async function create(name, pass, emailId, uid, userType) { } async function read(filter, limit = 1) { - const userData = await User.find(filter).limit(limit); - return userData; + const userDoc = await User.find(filter).limit(limit); + return userDoc; } -async function update(filter, updateObject) { - const user = await User.findOneAndUpdate(filter, updateObject, { new: true }); - return user; +async function update(filter, updateObject, options = { multi: true }) { + const updateResult = await User.updateMany(filter, { $set: updateObject }, options); + return updateResult.acknowledged; } export default { diff --git a/routes/accreditation.js b/routes/accreditation.js index d8769c6..94e72c4 100644 --- a/routes/accreditation.js +++ b/routes/accreditation.js @@ -3,4 +3,5 @@ import accreditationController from "#controller/accreditation"; const router = express.Router(); router.post("/add", accreditationController.addAccreditation); +router.delete("/delete/:accredationId", accreditationController.deleteAccreditation); export default router; diff --git a/services/accreditation.js b/services/accreditation.js index 4563a25..0dc224e 100644 --- a/services/accreditation.js +++ b/services/accreditation.js @@ -2,14 +2,25 @@ import accreditation from "#models/accreditation"; import databaseError from "#error/database"; export async function addNewAccreditation(name, agencyName, dateofAccreditation, dateofExpiry) { - const newAccreditation = await accreditation.create( + const newAccreditation = await accreditation.create({ name, agencyName, dateofAccreditation, dateofExpiry, - ); + }); if (newAccreditation.name === name) { return newAccreditation; } throw new databaseError.DataEntryError("Accreditation"); } + +export async function deleteAccreditationById(accredationId){ + const deleted = await accreditation.remove({_id: accredationId}); + if (deleted) { + return deleted + } + throw new databaseError.DataDeleteError("Accreditation"); +} +export default { + deleteAccreditationById, addNewAccreditation +} \ No newline at end of file diff --git a/services/infrastructure.js b/services/infrastructure.js index 9ceb6c9..4e8247b 100644 --- a/services/infrastructure.js +++ b/services/infrastructure.js @@ -1,10 +1,12 @@ import infrastructure from "#models/infrastructure"; import databaseError from "#error/database"; -export async function createinfrastructure(name, type, wing, floor, capacity) { - const newinfrastructure = await infrastructure.create(name, type, wing, floor, capacity); - if (newinfrastructure.name === name) { - return newinfrastructure; +export async function createInfrastructure(name, type, wing, floor, capacity) { + const newInfrastructure = await infrastructure.create({ + name, type, wing, floor, capacity, + }); + if (newInfrastructure.name === name) { + return newInfrastructure; } throw new databaseError.DataEntryError("infrastructure"); } diff --git a/services/user.js b/services/user.js index b45fe34..1e6d737 100644 --- a/services/user.js +++ b/services/user.js @@ -19,8 +19,8 @@ export async function userExists(uid, email) { export async function updatePassword(uid, password) { const hashedPassword = await hashPassword(password); - const user = await User.update({ uid }, { password: hashedPassword }); - if (user.uid === uid) return user; + const updated = await User.update({ uid }, { password: hashedPassword }); + if (updated) return; throw new databaseError.UpdateError("User"); } @@ -30,7 +30,9 @@ export async function allUsers() { } export async function createUser(name, password, emailId, uid, userType) { - const newUser = await User.create(name, password, emailId, uid, userType); + const newUser = await User.create({ + name, password, emailId, uid, userType, + }); if (newUser.uid === uid) { return newUser; }