Spaces:
Sleeping
Sleeping
Download backend/src/models/User.js from edichat/edi.chat.test: direct link, hf CLI and curl.
- Browser
- Download file 6.63 kB
-
https://huggingface.co/spaces/edichat/edi.chat.test/resolve/main/backend/src/models/User.js
- Command line
-
hf download hf://spaces/edichat/edi.chat.test/backend/src/models/User.js
-
curl -L -o User.js https://huggingface.co/spaces/edichat/edi.chat.test/resolve/main/backend/src/models/User.js
6.63 kB
| const mongoose = require('mongoose'); | |
| const bcrypt = require('bcryptjs'); | |
| const crypto = require('crypto'); | |
| const userSchema = new mongoose.Schema({ | |
| // UGA-specific identifiers (optional for local accounts) | |
| ugaId: { | |
| type: String, | |
| required: false, | |
| unique: true, | |
| sparse: true, // Allow null values to be unique | |
| trim: true | |
| }, | |
| myId: { | |
| type: String, | |
| required: false, | |
| unique: true, | |
| sparse: true, // Allow null values to be unique | |
| trim: true | |
| }, | |
| email: { | |
| type: String, | |
| required: true, | |
| unique: true, | |
| lowercase: true, | |
| trim: true, | |
| validate: { | |
| validator: function(email) { | |
| // Basic email validation | |
| const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/; | |
| return emailRegex.test(email); | |
| }, | |
| message: 'Must be a valid email address' | |
| } | |
| }, | |
| // Personal Information | |
| firstName: { | |
| type: String, | |
| required: true, | |
| trim: true | |
| }, | |
| lastName: { | |
| type: String, | |
| required: true, | |
| trim: true | |
| }, | |
| displayName: { | |
| type: String, | |
| trim: true | |
| }, | |
| // Role and Academic Information | |
| role: { | |
| type: String, | |
| enum: ['student', 'faculty', 'staff', 'admin'], | |
| required: true | |
| }, | |
| college: { | |
| type: String, | |
| trim: true | |
| }, | |
| department: { | |
| type: String, | |
| trim: true | |
| }, | |
| major: { | |
| type: String, | |
| trim: true | |
| }, | |
| classification: { | |
| type: String, | |
| enum: ['freshman', 'sophomore', 'junior', 'senior', 'graduate', 'doctoral', 'faculty', 'staff', 'other'], | |
| trim: true | |
| }, | |
| // Authentication | |
| passwordHash: { | |
| type: String, | |
| required: function() { | |
| // Password required for local accounts | |
| return this.authProvider === 'local'; | |
| } | |
| }, | |
| authProvider: { | |
| type: String, | |
| enum: ['myid', 'local'], | |
| default: 'myid' | |
| }, | |
| lastLogin: { | |
| type: Date, | |
| default: Date.now | |
| }, | |
| // Chatbot Preferences | |
| preferences: { | |
| theme: { | |
| type: String, | |
| enum: ['light', 'dark', 'auto'], | |
| default: 'auto' | |
| }, | |
| language: { | |
| type: String, | |
| default: 'en' | |
| }, | |
| notifications: { | |
| email: { type: Boolean, default: true }, | |
| push: { type: Boolean, default: false }, | |
| sms: { type: Boolean, default: false } | |
| }, | |
| chatHistory: { | |
| type: Boolean, | |
| default: true | |
| } | |
| }, | |
| // Usage Analytics | |
| stats: { | |
| totalChats: { type: Number, default: 0 }, | |
| totalQuestions: { type: Number, default: 0 }, | |
| favoriteTopics: [String], | |
| lastActive: { type: Date, default: Date.now } | |
| }, | |
| // Account Status | |
| isActive: { | |
| type: Boolean, | |
| default: true | |
| }, | |
| isVerified: { | |
| type: Boolean, | |
| default: false | |
| }, | |
| // Password Reset | |
| passwordResetToken: { | |
| type: String, | |
| default: null | |
| }, | |
| passwordResetExpires: { | |
| type: Date, | |
| default: null | |
| }, | |
| // Email Verification | |
| emailVerificationToken: { | |
| type: String, | |
| default: null | |
| }, | |
| emailVerificationExpires: { | |
| type: Date, | |
| default: null | |
| }, | |
| // Integration tokens (encrypted) | |
| integrationTokens: { | |
| elc: String, | |
| athena: String, | |
| // Add other UGA system tokens as needed | |
| } | |
| }, { | |
| timestamps: true, | |
| toJSON: { | |
| transform: function(doc, ret) { | |
| delete ret.passwordHash; | |
| delete ret.integrationTokens; | |
| return ret; | |
| } | |
| } | |
| }); | |
| // Indexes for performance (unique fields automatically indexed) | |
| userSchema.index({ role: 1 }); | |
| userSchema.index({ 'stats.lastActive': -1 }); | |
| // Virtual for full name | |
| userSchema.virtual('fullName').get(function() { | |
| return `${this.firstName} ${this.lastName}`; | |
| }); | |
| // Hash password before saving | |
| userSchema.pre('save', async function(next) { | |
| // Only hash the password if it has been modified (or is new) | |
| if (!this.isModified('passwordHash') || !this.passwordHash) { | |
| return next(); | |
| } | |
| try { | |
| const salt = await bcrypt.genSalt(12); | |
| this.passwordHash = await bcrypt.hash(this.passwordHash, salt); | |
| next(); | |
| } catch (error) { | |
| next(error); | |
| } | |
| }); | |
| // Method to check password | |
| userSchema.methods.comparePassword = async function(candidatePassword) { | |
| if (!this.passwordHash) { | |
| return false; | |
| } | |
| return bcrypt.compare(candidatePassword, this.passwordHash); | |
| }; | |
| // Method to update last activity | |
| userSchema.methods.updateActivity = function() { | |
| this.stats.lastActive = new Date(); | |
| this.lastLogin = new Date(); | |
| return this.save(); | |
| }; | |
| // Method to increment chat statistics | |
| userSchema.methods.incrementChatStats = function(topic = null) { | |
| this.stats.totalChats += 1; | |
| this.stats.totalQuestions += 1; | |
| if (topic) { | |
| if (!this.stats.favoriteTopics.includes(topic)) { | |
| this.stats.favoriteTopics.push(topic); | |
| } | |
| } | |
| this.stats.lastActive = new Date(); | |
| return this.save(); | |
| }; | |
| // Static method to find users by role | |
| userSchema.statics.findByRole = function(role) { | |
| return this.find({ role, isActive: true }); | |
| }; | |
| // Static method to find recent active users | |
| userSchema.statics.findRecentlyActive = function(days = 30) { | |
| const date = new Date(); | |
| date.setDate(date.getDate() - days); | |
| return this.find({ | |
| 'stats.lastActive': { $gte: date }, | |
| isActive: true | |
| }).sort({ 'stats.lastActive': -1 }); | |
| }; | |
| // Method to generate password reset token | |
| userSchema.methods.generatePasswordResetToken = function() { | |
| const resetToken = crypto.randomBytes(32).toString('hex'); | |
| this.passwordResetToken = crypto.createHash('sha256').update(resetToken).digest('hex'); | |
| this.passwordResetExpires = new Date(Date.now() + 10 * 60 * 1000); // 10 minutes | |
| return resetToken; | |
| }; | |
| // Method to generate email verification token | |
| userSchema.methods.generateEmailVerificationToken = function() { | |
| const verificationToken = crypto.randomBytes(32).toString('hex'); | |
| this.emailVerificationToken = crypto.createHash('sha256').update(verificationToken).digest('hex'); | |
| this.emailVerificationExpires = new Date(Date.now() + 24 * 60 * 60 * 1000); // 24 hours | |
| return verificationToken; | |
| }; | |
| // Static method to find user by password reset token | |
| userSchema.statics.findByPasswordResetToken = function(token) { | |
| const hashedToken = crypto.createHash('sha256').update(token).digest('hex'); | |
| return this.findOne({ | |
| passwordResetToken: hashedToken, | |
| passwordResetExpires: { $gt: Date.now() } | |
| }); | |
| }; | |
| // Static method to find user by email verification token | |
| userSchema.statics.findByEmailVerificationToken = function(token) { | |
| const hashedToken = crypto.createHash('sha256').update(token).digest('hex'); | |
| return this.findOne({ | |
| emailVerificationToken: hashedToken, | |
| emailVerificationExpires: { $gt: Date.now() } | |
| }); | |
| }; | |
| module.exports = mongoose.model('User', userSchema); |