text: import mongoose from 'mongoose';
  import bcrypt from 'bcrypt';
  
  const SALT_WORK_FACTOR = 10;
  
  
  /**
   * @description This is Users schema
   */
  const userSchema = new mongoose.Schema({
    name: { type: String },
    password: { type: String, required: true },
    email: { type: String, required: true, unique: true },
    createdAt: { type: Date, default: Date.now },
    updatedAt: { type: Date, default: Date.now },
  });
  
  
  userSchema.pre('save', function (next) {
    const user = this;
    if (!user.isModified('password')) return next();
    bcrypt.genSalt(SALT_WORK_FACTOR, function (err, salt) {
      if (err) return next(err);
      bcrypt.hash(user.password, salt, function (err, hash) {
        if (err) return next(err);
        user.password = hash;
        next();
      });
    });
  });
  
  userSchema.methods.comparePassword = (candidatePassword, cb) => {
    bcrypt.compare(candidatePassword, this.password, (err, isMatch) => {
      if (err) return cb(err);
      return cb(null, isMatch);
    });
  };
  
  const Users = mongoose.model('Users', userSchema);
  
  export default Users;
  