const mongoose = require('mongoose'); const locationSchema = new mongoose.Schema({ name: { type: String, required: true, trim: true }, address: { type: String, trim: true }, building: { type: String, trim: true }, room: { type: String, trim: true }, coordinates: { latitude: Number, longitude: Number }, virtualLink: { type: String, trim: true }, isVirtual: { type: Boolean, default: false } }, { _id: false }); const contactInfoSchema = new mongoose.Schema({ name: { type: String, trim: true }, email: { type: String, trim: true, lowercase: true }, phone: { type: String, trim: true }, department: { type: String, trim: true } }, { _id: false }); const registrationSchema = new mongoose.Schema({ required: { type: Boolean, default: false }, url: { type: String, trim: true }, deadline: Date, cost: { type: Number, min: 0 }, capacity: { type: Number, min: 0 }, spotsRemaining: { type: Number, min: 0 } }, { _id: false }); const recurrenceSchema = new mongoose.Schema({ type: { type: String, enum: ['none', 'daily', 'weekly', 'monthly', 'yearly', 'custom'], default: 'none' }, interval: { type: Number, min: 1, default: 1 }, daysOfWeek: [{ type: Number, min: 0, max: 6 }], endDate: Date, exceptions: [Date] }, { _id: false }); const ugaEventSchema = new mongoose.Schema({ // Basic Event Information title: { type: String, required: true, trim: true, index: true }, description: { type: String, trim: true }, shortDescription: { type: String, trim: true, maxlength: 200 }, // Event Type and Category eventType: { type: String, required: true, enum: [ 'academic-calendar', 'registration-deadline', 'campus-event', 'student-activity', 'sports', 'arts-culture', 'lecture-seminar', 'workshop', 'meeting', 'orientation', 'commencement', 'holiday', 'break', 'exam-period', 'dining-special', 'career-fair', 'information-session', 'deadline', 'announcement', 'emergency', 'other' ], index: true }, category: { type: String, required: true, enum: [ 'academics', 'admissions', 'athletics', 'arts', 'career-services', 'dining', 'greek-life', 'housing', 'international', 'libraries', 'recreation', 'research', 'service-learning', 'student-affairs', 'student-organizations', 'technology', 'transportation', 'wellness', 'other' ], index: true }, // Date and Time Information startDate: { type: Date, required: true, index: true }, endDate: { type: Date, index: true }, allDay: { type: Boolean, default: false }, timezone: { type: String, default: 'America/New_York' }, // Recurrence recurrence: recurrenceSchema, // Location Information location: locationSchema, // Target Audience audience: [{ type: String, enum: [ 'all-students', 'undergraduate', 'graduate', 'doctoral', 'freshman', 'sophomore', 'junior', 'senior', 'faculty', 'staff', 'alumni', 'prospective-students', 'international-students', 'student-athletes', 'greek-members', 'commuter-students', 'distance-learners', 'public' ] }], // Academic Information academic: { semester: { type: String, enum: ['fall', 'spring', 'summer', 'all-year'], index: true }, year: { type: Number, index: true }, colleges: [{ type: String, trim: true }], departments: [{ type: String, trim: true }], courses: [{ code: String, name: String }] }, // Priority and Importance priority: { type: String, enum: ['low', 'medium', 'high', 'critical'], default: 'medium', index: true }, isImportantDate: { type: Boolean, default: false, index: true }, isFeatured: { type: Boolean, default: false, index: true }, // Registration and Cost registration: registrationSchema, // Contact Information organizer: contactInfoSchema, contact: contactInfoSchema, // Media and Links images: [{ url: String, alt: String, caption: String }], links: [{ url: String, text: String, type: { type: String, enum: ['website', 'registration', 'tickets', 'social-media', 'other'] } }], // Tags and Keywords tags: [{ type: String, trim: true, lowercase: true }], keywords: [{ type: String, trim: true, lowercase: true }], // Data Source Information metadata: { source: { type: String, enum: ['localist-api', 'registrar-scrape', 'manual-entry', 'uga-calendar', 'department-feed'], default: 'manual-entry' }, sourceUrl: String, sourceId: String, lastUpdated: { type: Date, default: Date.now }, lastSynced: Date, dataHash: String, importBatch: String }, // Status and Visibility status: { type: String, enum: ['draft', 'published', 'cancelled', 'postponed', 'completed'], default: 'published', index: true }, visibility: { type: String, enum: ['public', 'students-only', 'faculty-staff', 'private'], default: 'public', index: true }, // Notification Settings notifications: { enabled: { type: Boolean, default: false }, reminderDays: [{ type: Number, min: 0 }], channels: [{ type: String, enum: ['email', 'push', 'sms', 'calendar'] }] }, // Analytics and Engagement analytics: { views: { type: Number, default: 0 }, clicks: { type: Number, default: 0 }, shares: { type: Number, default: 0 }, registrations: { type: Number, default: 0 } } }, { timestamps: true }); // Compound Indexes for efficient querying ugaEventSchema.index({ startDate: 1, eventType: 1 }); ugaEventSchema.index({ eventType: 1, category: 1, status: 1 }); ugaEventSchema.index({ 'academic.semester': 1, 'academic.year': 1 }); ugaEventSchema.index({ audience: 1, startDate: 1 }); ugaEventSchema.index({ priority: 1, isImportantDate: 1, startDate: 1 }); ugaEventSchema.index({ tags: 1, keywords: 1 }); ugaEventSchema.index({ 'metadata.source': 1, 'metadata.lastSynced': 1 }); // Text search index ugaEventSchema.index({ title: 'text', description: 'text', 'location.name': 'text', tags: 'text', keywords: 'text' }); // Geospatial index for location-based queries ugaEventSchema.index({ 'location.coordinates': '2dsphere' }); // Pre-save middleware ugaEventSchema.pre('save', function(next) { // Ensure endDate is after startDate if provided if (this.endDate && this.endDate < this.startDate) { this.endDate = this.startDate; } // Set academic year based on start date if not provided if (!this.academic.year && this.startDate) { const startMonth = this.startDate.getMonth(); const startYear = this.startDate.getFullYear(); // Academic year starts in August (month 7) this.academic.year = startMonth >= 7 ? startYear : startYear - 1; } // Auto-detect semester if not provided if (!this.academic.semester && this.startDate) { const month = this.startDate.getMonth(); if (month >= 7 && month <= 11) { this.academic.semester = 'fall'; } else if (month >= 0 && month <= 4) { this.academic.semester = 'spring'; } else { this.academic.semester = 'summer'; } } // Update metadata this.metadata.lastUpdated = new Date(); next(); }); // Static methods for common queries ugaEventSchema.statics.findUpcoming = function(limit = 50, eventTypes = []) { const query = { startDate: { $gte: new Date() }, status: { $in: ['published'] }, visibility: 'public' }; if (eventTypes.length > 0) { query.eventType = { $in: eventTypes }; } return this.find(query) .sort({ startDate: 1, priority: -1 }) .limit(limit); }; ugaEventSchema.statics.findByDateRange = function(startDate, endDate, options = {}) { const query = { $or: [ { startDate: { $gte: startDate, $lte: endDate } }, { endDate: { $gte: startDate, $lte: endDate } }, { startDate: { $lte: startDate }, endDate: { $gte: endDate } } ], status: { $in: ['published'] } }; if (options.eventTypes) { query.eventType = { $in: options.eventTypes }; } if (options.categories) { query.category = { $in: options.categories }; } if (options.audience) { query.audience = { $in: options.audience }; } return this.find(query).sort({ startDate: 1 }); }; ugaEventSchema.statics.findImportantDates = function(semester, year) { const query = { isImportantDate: true, status: 'published' }; if (semester) { query['academic.semester'] = semester; } if (year) { query['academic.year'] = year; } return this.find(query).sort({ startDate: 1 }); }; ugaEventSchema.statics.findByAudience = function(audience, limit = 20) { return this.find({ audience: { $in: [audience, 'all-students'] }, startDate: { $gte: new Date() }, status: 'published' }) .sort({ startDate: 1, priority: -1 }) .limit(limit); }; ugaEventSchema.statics.searchEvents = function(query, options = {}) { const searchQuery = { $text: { $search: query }, status: 'published' }; if (options.startDate) { searchQuery.startDate = { $gte: options.startDate }; } if (options.eventTypes) { searchQuery.eventType = { $in: options.eventTypes }; } return this.find(searchQuery, { score: { $meta: 'textScore' } }) .sort({ score: { $meta: 'textScore' }, startDate: 1 }); }; // Instance methods ugaEventSchema.methods.isHappening = function() { const now = new Date(); return this.startDate <= now && (this.endDate ? this.endDate >= now : this.startDate >= now); }; ugaEventSchema.methods.hasPassedRegistrationDeadline = function() { if (!this.registration || !this.registration.deadline) { return false; } return new Date() > this.registration.deadline; }; ugaEventSchema.methods.getTimeUntilEvent = function() { const now = new Date(); const timeDiff = this.startDate - now; if (timeDiff < 0) { return null; // Event has already started } const days = Math.floor(timeDiff / (1000 * 60 * 60 * 24)); const hours = Math.floor((timeDiff % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60)); const minutes = Math.floor((timeDiff % (1000 * 60 * 60)) / (1000 * 60)); return { days, hours, minutes }; }; ugaEventSchema.methods.incrementView = function() { this.analytics.views += 1; return this.save(); }; ugaEventSchema.methods.incrementClick = function() { this.analytics.clicks += 1; return this.save(); }; module.exports = mongoose.model('UGAEvent', ugaEventSchema);