import { Schema as _Schema, model } from 'mongoose' import { hashPassword } from '../utils/hashUtils' const Schema = _Schema const UserSchema = new Schema({ name: { type: String, required: true, maxlength: 100 }, gender: { type: String, enum: ['male', 'female', 'other'], maxlength: 20 }, birth: { type: Date }, avatar: { type: String, maxlength: 100 }, account: { type: String, required: true, unique: true, maxlength: 100, index: true }, password: { type: String, required: true, maxlength: 100 }, email: { type: String, maxlength: 255, index: true }, phone: { type: String, maxlength: 11 }, registerDate: { type: Date, default: Date.now }, lastLoginDate: { type: Date, default: Date.now }, status: { type: String, enum: ['active', 'inactive', 'pending'], default: 'pending' } }) UserSchema.pre('save', async function (next) { const user = this if (!user.isModified('password')) return next() try { console.log(user.password) user.password = await hashPassword(user.password) next() } catch (error) { next(error) } }) export default model('User', UserSchema)