Skip to content

Commit

Permalink
Resolving mentor issue 1.4
Browse files Browse the repository at this point in the history
  • Loading branch information
MOHAMMED1MEDHAT committed Sep 10, 2023
1 parent 0131ff9 commit 88fe604
Showing 1 changed file with 102 additions and 100 deletions.
202 changes: 102 additions & 100 deletions src/models/mentor.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,124 +4,126 @@ 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'
},
experience: [
{
type: String,
default: 'No experience'
}
],
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
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'
},
experience: [
{
type: String,
default: 'No experience'
}
],
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
},
workHoursRange: {
type: String,
default: '9:00-14:00'
{
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 experience courses onboarding_completed active role skill'
);
// this.find({ active: { $ne: false } });
next();
this.select(
'photo name email about experience courses onboarding_completed active role skill'
);
// this.find({ active: { $ne: false } });
next();
});
//-------------------------Export-----------------------//
const Mentor = mongoose.model('Mentor', mentorSchema);
Expand Down

0 comments on commit 88fe604

Please sign in to comment.