Skip to content

Commit

Permalink
Merge pull request #53 from Skill-Sync/SS-1.4-Medhat
Browse files Browse the repository at this point in the history
Ss 1.4 medhat
  • Loading branch information
MOHAMMED1MEDHAT authored Sep 12, 2023
2 parents 929a61f + 9648517 commit 2607efa
Show file tree
Hide file tree
Showing 4 changed files with 150 additions and 145 deletions.
5 changes: 4 additions & 1 deletion src/controllers/meeting.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -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));

Expand Down
84 changes: 43 additions & 41 deletions src/controllers/user.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
194 changes: 97 additions & 97 deletions src/models/mentor.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
12 changes: 6 additions & 6 deletions src/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -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!');
// });
});

0 comments on commit 2607efa

Please sign in to comment.