const mongoose = require('mongoose'); const crypto = require('crypto') const libs = `${process.cwd()}/libs`; const log = require(`${libs}/log`)(module); const Schema = mongoose.Schema; // set up a mongoose model var UserSchema = new Schema({ email: { type: String, unique: true, required: [true, 'O campo Email é obrigatório.'] }, hashedPassword: { type: String, required: [true, 'O campo Senha é obrigatório.'] }, salt: { type: String, required: true }, name: { type: String, required: [true, 'O campo Nome é obrigatório.'] }, cpf:{ type: String, unique: true, required: [true, 'O campo CPF é obrigatório.'] }, schooling: { type: String, required: [true, 'O campo Escolaridade é obrigatório.'] }, course: { type: String, }, segment: { type: String, required: [true, 'O campo Segmento é obrigatório.'] }, role: { type: String, required: [true, 'O campo Função é obrigatório.'] }, institution_name: { type: String, required: [true, 'O campo Instituição em que trabalha é obrigatório.'] }, state: { type: String, required: [true, 'O campo Estado é obrigatório.'] }, city: { type: String, required: [true, 'O campo Cidade é obrigatório.'] }, receive_emails: { type: Boolean }, createdAt: { type: Date, default: Date.now }, origin: { type: String, enum: ['LDE', 'SimCAQ'], required: true } }); UserSchema.methods.encryptPassword = (password) => { return crypto.pbkdf2Sync(password, this.salt, 10000, 512); }; UserSchema.virtual('password').set((password) => { this._plainPassword = password; this.salt = crypto.randomBytes(128).toString('hex'); this.hashedPassword = this.encryptPassword(password); }).get(() => { return this._plainPassword; }); module.exports = mongoose.model('User', UserSchema);