text: import mongoose from 'mongoose'
  import crypto from 'crypto'
  const UserSchema = new mongoose.Schema({
    name: {
      type: String,
      trim: true,
      required: 'Name is required'
    },
    studentIDNum: {
      type: String,
      trim: true,
      required: 'Identity Number is required',
      unique: 'User with the same Identity Number already exists'
    },
    studentNum: {
      type: Number,
      trim: true,
      required: 'Please enter a student number',
      unique: 'Student number already exists'
    },
    email: {
      type: String,
      trim: true,
      unique: 'Email already exists',
      match: [/.+\@.+\..+/, 'Please fill a valid email address'],
      required: 'Email is required'
    },
    hashed_password: {
      type: String,
      required: "Password is required"
    },
    nowDue: {
      type: Number,
      required: 'Please enter now due value'
    },
    admin: {
      type: Boolean,
      default: false
    },
    salt: String,
    updated: Date,
    created: {
      type: Date,
      default: Date.now
    }
  })
  
  UserSchema
    .virtual('password')
    .set(function(password) {
      this._password = password
      this.salt = this.makeSalt()
      this.hashed_password = this.encryptPassword(password)
    })
    .get(function() {
      return this._password
    })
  
  UserSchema.path('hashed_password').validate(function(v) {
    if (this._password && this._password.length < 6) {
      this.invalidate('password', 'Password must be at least 6 characters.')
    }
    if (this.isNew && !this._password) {
      this.invalidate('password', 'Password is required')
    }
  }, null)
  
  UserSchema.methods = {
    authenticate: function(plainText) {
      return this.encryptPassword(plainText) === this.hashed_password
    },
    encryptPassword: function(password) {
      if (!password) return ''
      try {
        return crypto
          .createHmac('sha1', this.salt)
          .update(password)
          .digest('hex')
      } catch (err) {
        return ''
      }
    },
    makeSalt: function() {
      return Math.round((new Date().valueOf() * Math.random())) + ''
    }
  }
  
  export default mongoose.model('User', UserSchema)
  