Spaces:
Sleeping
Sleeping
File size: 3,563 Bytes
7851df6 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 | const mongoose = require('mongoose');
const bcrypt = require('bcryptjs');
const userSchema = new mongoose.Schema({
// Basic info
username: {
type: String,
required: true,
unique: true,
trim: true,
minlength: 3,
maxlength: 30
},
email: {
type: String,
required: true,
unique: true,
trim: true,
lowercase: true
},
password: {
type: String,
required: true,
minlength: 6
},
// Profile info
profile: {
firstName: { type: String, trim: true },
lastName: { type: String, trim: true },
ugaId: { type: String, trim: true }, // Student/Faculty ID
year: { type: String, enum: ['Freshman', 'Sophomore', 'Junior', 'Senior', 'Graduate', 'Faculty', 'Staff'] },
major: { type: String, trim: true },
college: { type: String, trim: true },
avatar: { type: String }, // URL or base64
bio: { type: String, maxlength: 500 },
phoneNumber: { type: String, trim: true },
emergencyContact: {
name: String,
phone: String,
relationship: String
}
},
// Academic info
courses: [{
courseCode: String, // e.g., "CSCI 1301"
courseName: String,
instructor: String,
semester: String,
year: Number,
grade: String
}],
// Settings
settings: {
theme: { type: String, enum: ['light', 'dark', 'auto'], default: 'light' },
notifications: {
email: { type: Boolean, default: true },
push: { type: Boolean, default: true },
assignments: { type: Boolean, default: true },
events: { type: Boolean, default: true }
},
privacy: {
profileVisible: { type: Boolean, default: true },
showOnlineStatus: { type: Boolean, default: true }
},
ai: {
preferredModel: { type: String, default: 'llama3' },
webSearchEnabled: { type: Boolean, default: true },
contextLength: { type: Number, default: 4000 }
}
},
// Activity tracking
lastLogin: { type: Date, default: Date.now },
lastActive: { type: Date, default: Date.now },
loginCount: { type: Number, default: 0 },
chatCount: { type: Number, default: 0 },
// Account status
isActive: { type: Boolean, default: true },
isVerified: { type: Boolean, default: false },
role: { type: String, enum: ['student', 'faculty', 'staff', 'admin'], default: 'student' }
}, {
timestamps: true
});
// Password hashing middleware
userSchema.pre('save', async function(next) {
if (!this.isModified('password')) return next();
try {
const salt = await bcrypt.genSalt(12);
this.password = await bcrypt.hash(this.password, salt);
next();
} catch (error) {
next(error);
}
});
// Password comparison method
userSchema.methods.comparePassword = async function(candidatePassword) {
return bcrypt.compare(candidatePassword, this.password);
};
// Virtual for full name
userSchema.virtual('fullName').get(function() {
if (this.profile.firstName && this.profile.lastName) {
return `${this.profile.firstName} ${this.profile.lastName}`;
}
return this.username;
});
// Virtual for current courses
userSchema.virtual('currentCourses').get(function() {
const currentYear = new Date().getFullYear();
const currentSemester = new Date().getMonth() < 6 ? 'Spring' : 'Fall';
return this.courses.filter(course =>
course.year === currentYear &&
course.semester === currentSemester
);
});
// Index for search
userSchema.index({ username: 'text', email: 'text', 'profile.firstName': 'text', 'profile.lastName': 'text' });
module.exports = mongoose.model('User', userSchema); |