diff --git a/src/controllers/meeting.controller.js b/src/controllers/meeting.controller.js index c82cea2..3544f4b 100644 --- a/src/controllers/meeting.controller.js +++ b/src/controllers/meeting.controller.js @@ -78,7 +78,10 @@ exports.createMeeting = catchAsyncError(async (req, res, next) => { exports.getMeeting = catchAsyncError(async (req, res, next) => { const meeting = await Meeting.find({ mentor: req.params.id - }); + }) + .populate({ path: 'user', select: 'name email' }) + .populate({ path: 'mentor', select: 'name email' }); + if (!meeting) return next(new AppError('No meeting found with that ID', 404)); diff --git a/src/controllers/user.controller.js b/src/controllers/user.controller.js index 6d0f5db..c40a09c 100644 --- a/src/controllers/user.controller.js +++ b/src/controllers/user.controller.js @@ -6,61 +6,63 @@ const AppError = require('../utils/appErrorsClass'); const catchAsyncError = require('../utils/catchAsyncErrors'); //------------handler functions ------------// const filterObj = (obj, ...allowedFields) => { - const returnedFiled = {}; - Object.keys(obj).forEach(key => { - if (allowedFields.includes(key)) returnedFiled[key] = obj[key]; - }); - return returnedFiled; + const returnedFiled = {}; + Object.keys(obj).forEach(key => { + if (allowedFields.includes(key)) returnedFiled[key] = obj[key]; + }); + return returnedFiled; }; // ---------- User Operations ---------// exports.getMe = (req, res, next) => { - req.params.id = res.locals.userId; - next(); + req.params.id = res.locals.userId; + next(); }; exports.UpdateMe = catchAsyncError(async (req, res, next) => { - if (req.body.pass || req.body.passConfirm) { - return next(new AppError('This route is not for password updates.', 400)); - } + if (req.body.pass || req.body.passConfirm) { + return next( + new AppError('This route is not for password updates.', 400) + ); + } - const filteredBody = filterObj( - req.body, - 'name', - 'email', - 'about', - 'isEmployed', - 'skillsToLearn', - 'skillsLearned' - ); + const filteredBody = filterObj( + req.body, + 'name', + 'email', + 'about', + 'isEmployed', + 'skillsToLearn', + 'skillsLearned' + ); - const updatedUser = await User.findById(req.params.id); - Object.keys(filteredBody).forEach(key => { - updatedUser[key] = filteredBody[key]; - }); - await updatedUser.save({ runValidators: true }); + const updatedUser = await User.findById(req.params.id); + Object.keys(filteredBody).forEach(key => { + updatedUser[key] = filteredBody[key]; + }); + await updatedUser.save({ runValidators: true }); - res.status(res.locals.statusCode || 200).json({ - status: 'success', - data: updatedUser - }); + res.status(res.locals.statusCode || 200).json({ + status: 'success', + data: updatedUser + }); }); exports.getRelevantMentors = catchAsyncError(async (req, res, next) => { - const user = await User.findById(res.locals.userId); + const user = await User.findById(res.locals.userId); - const mentors = await Mentor.find({ - skill: { - $in: user.skillsToLearn.map(skill => skill._id) - } - }).populate({ - path: 'skill', - select: 'name' - }); + const mentors = await Mentor.find({ + skill: { + $in: user.skillsToLearn.map(skill => skill._id) + } + }).populate({ + path: 'skill', + select: 'name' + }); - res.status(res.locals.statusCode || 200).json({ - status: 'success', - data: mentors - }); + res.status(res.locals.statusCode || 200).json({ + status: 'success', + data: mentors + }); }); // exports.deactivateUser = factory.deactivateOne(User); diff --git a/src/models/mentor.model.js b/src/models/mentor.model.js index 365e8cd..75e9503 100644 --- a/src/models/mentor.model.js +++ b/src/models/mentor.model.js @@ -4,120 +4,120 @@ const mongoose = require('mongoose'); const validator = require('validator'); //------------------------------------------// const mentorSchema = new mongoose.Schema( - { - role: { - type: String, - default: 'mentor' - }, - name: { - type: String, - required: [true, 'A user must have a name'] - }, - email: { - type: String, - unique: true, - lowercase: true, - validate: { - validator: validator.isEmail, - message: 'Please provide a valid email' - }, - required: [true, 'A user must have an email'] - }, - pass: { - type: String, - required: [true, 'A user must have a password'], - minlength: 8, - select: false - }, - passConfirm: { - type: String, - validate: { - validator: function(el) { - return el === this.pass; + { + role: { + type: String, + default: 'mentor' }, - message: 'Passwords are not the same!' - }, - required: [true, 'A user must have a password confirmation'] - }, - identityCard: { - type: String - }, - photo: { - type: String, - default: 'default.jpg' - }, - about: { - type: String, - default: 'No description' - }, - skill: { - type: mongoose.Schema.Types.ObjectId, - ref: 'Skill' - }, - requestLetter: { - type: String, - trim: true, - default: 'No request letter' - }, - onboarding_completed: { - type: Boolean, - default: true - }, - isVerified: { - type: Boolean, - default: false - }, - active: { - type: Boolean, - default: false, - select: false - }, - workHoursRange: { - type: String, - default: '9:00-14:00' + name: { + type: String, + required: [true, 'A user must have a name'] + }, + email: { + type: String, + unique: true, + lowercase: true, + validate: { + validator: validator.isEmail, + message: 'Please provide a valid email' + }, + required: [true, 'A user must have an email'] + }, + pass: { + type: String, + required: [true, 'A user must have a password'], + minlength: 8, + select: false + }, + passConfirm: { + type: String, + validate: { + validator: function(el) { + return el === this.pass; + }, + message: 'Passwords are not the same!' + }, + required: [true, 'A user must have a password confirmation'] + }, + identityCard: { + type: String + }, + photo: { + type: String, + default: 'default.jpg' + }, + about: { + type: String, + default: 'No description' + }, + skill: { + type: mongoose.Schema.Types.ObjectId, + ref: 'Skill' + }, + requestLetter: { + type: String, + trim: true, + default: 'No request letter' + }, + onboarding_completed: { + type: Boolean, + default: true + }, + isVerified: { + type: Boolean, + default: false + }, + active: { + type: Boolean, + default: false, + select: false + }, + workHoursRange: { + type: String, + default: '9:00-14:00' + }, + passwordResetToken: String, + passwordResetExpires: Date }, - passwordResetToken: String, - passwordResetExpires: Date - }, - { - toJSON: { virtuals: true }, - toObject: { virtuals: true } - } + { + toJSON: { virtuals: true }, + toObject: { virtuals: true } + } ); //-------------------Instance Methods-------------------// mentorSchema.methods.correctPassword = async function(loginPass, userPass) { - return await bcrypt.compare(loginPass, userPass); + return await bcrypt.compare(loginPass, userPass); }; mentorSchema.methods.createPasswordResetToken = function() { - const resetToken = crypto.randomBytes(32).toString('hex'); - this.passwordResetToken = crypto - .createHash('sha256') - .update(resetToken) - .digest('hex'); - this.passwordResetExpires = Date.now() + 5 * 60 * 1000; - return resetToken; + const resetToken = crypto.randomBytes(32).toString('hex'); + this.passwordResetToken = crypto + .createHash('sha256') + .update(resetToken) + .digest('hex'); + this.passwordResetExpires = Date.now() + 5 * 60 * 1000; + return resetToken; }; //-------------------Document Middleware-----------------// mentorSchema.pre('save', function(next) { - if (this.isNew) return next(); - this.onboarding_completed = true; - next(); + if (this.isNew) return next(); + this.onboarding_completed = true; + next(); }); mentorSchema.pre('save', async function(next) { - // Only run this function only when password got modified (or created) - if (!this.isModified('pass')) return next(); - this.pass = await bcrypt.hash(this.pass, 12); - this.passConfirm = undefined; + // Only run this function only when password got modified (or created) + if (!this.isModified('pass')) return next(); + this.pass = await bcrypt.hash(this.pass, 12); + this.passConfirm = undefined; }); //-------------------Query Middleware-------------------// mentorSchema.pre(/^find/, function(next) { - this.select( - 'photo name email about courses onboarding_completed active role skill' - ); - // this.find({ active: { $ne: false } }); - next(); + this.select( + 'photo name email about courses onboarding_completed active role skill' + ); + // this.find({ active: { $ne: false } }); + next(); }); //-------------------------Export-----------------------// const Mentor = mongoose.model('Mentor', mentorSchema); diff --git a/src/server.js b/src/server.js index 16db8ba..fd7089f 100644 --- a/src/server.js +++ b/src/server.js @@ -44,9 +44,9 @@ process.on('unhandledRejection', err => { process.exit(1); }); }); -// process.on('SIGTERM', () => { -// console.log('👋 SIGTERM RECEIVED. Shutting down gracefully'); -// server.close(() => { -// console.log('💥 Process terminated!'); -// }); -// }); +process.on('SIGTERM', () => { + console.log('👋 SIGTERM RECEIVED. Shutting down gracefully'); + // server.close(() => { + // console.log('💥 Process terminated!'); + // }); +});