const mongoose = require('mongoose')
const CryptoJS = require('crypto-js')
const CourseSchema = require('./course').CourseSchema
const CurriculumClassSchema = require('./curriculum').CurriculumClassSchema
const config = require('../config')
const POSITIONS = ['undergraduate', 'master', 'doctor', 'teacher', 'unknown']

const userInfoSchema = new mongoose.Schema({
  studentNumber: String,
  department: String,
  realName: String,
  email: String,
  position: {
    type: String,
    enum: POSITIONS
  }
}, {
  toObject: {
    transform: (doc, ret) => {
      delete ret._id
    }
  }
})

const userSchema = new mongoose.Schema({
  username: {
    type: String,
    index: true
  },
  password: String,
  info: userInfoSchema,
  courses: [CourseSchema],
  curriculum: [CurriculumClassSchema]
})

userSchema.pre('save', function (next) {
  if (this.isNew) {
    const rawPass = this.password
    this.password = CryptoJS.AES.encrypt(rawPass, config.secretkey).toString()
  }

  next()
})

userSchema.methods.getPassword = function () {
  return CryptoJS.AES.decrypt(this.password, config.secretkey).toString(CryptoJS.enc.Utf8)
}

const User = mongoose.model('User', userSchema)

module.exports = User

const mongoose = require('mongoose')
const CryptoJS = require('crypto-js')
const CourseSchema = require('./course').CourseSchema
const CurriculumClassSchema = require('./curriculum').CurriculumClassSchema
const WeekSchedule = require('./schedule').WeekScedule
const config = require('../config')
const POSITIONS = ['undergraduate', 'master', 'doctor', 'teacher', 'unknown']

const userInfoSchema = new mongoose.Schema({
  studentNumber: String,
  department: String,
  realName: String,
  email: String,
  position: {
    type: String,
    enum: POSITIONS
  }
}, {
  toObject: {
    transform: (doc, ret) => {
      delete ret._id
    }
  }
})

const userSchema = new mongoose.Schema({
  username: {
    type: String,
    index: true
  },
  password: String,
  info: userInfoSchema,
  courses: [CourseSchema],
  curriculum: [CurriculumClassSchema],
  schedule: [WeekSchedule]
})

userSchema.pre('save', function (next) {
  if (this.isNew) {
    const rawPass = this.password
    this.password = CryptoJS.AES.encrypt(rawPass, config.secretkey).toString()
  }

  next()
})

userSchema.methods.getPassword = function () {
  return CryptoJS.AES.decrypt(this.password, config.secretkey).toString(CryptoJS.enc.Utf8)
}

const User = mongoose.model('User', userSchema)

module.exports = User

Add Schedule models to user