Spaces:
Sleeping
Sleeping
Download backend/src/utils/eventsUtils.js from edichat/edi.chat.test: direct link, hf CLI and curl.
- Browser
- Download file 13.8 kB
-
https://huggingface.co/spaces/edichat/edi.chat.test/resolve/main/backend/src/utils/eventsUtils.js
- Command line
-
hf download hf://spaces/edichat/edi.chat.test/backend/src/utils/eventsUtils.js
-
curl -L -o eventsUtils.js https://huggingface.co/spaces/edichat/edi.chat.test/resolve/main/backend/src/utils/eventsUtils.js
13.8 kB
| const logger = require('./logger'); | |
| class EventsUtils { | |
| // Validate event data before saving | |
| static validateEventData(eventData) { | |
| const errors = []; | |
| // Required fields | |
| if (!eventData.title || eventData.title.trim().length === 0) { | |
| errors.push('Title is required'); | |
| } | |
| if (!eventData.startDate || isNaN(new Date(eventData.startDate).getTime())) { | |
| errors.push('Valid start date is required'); | |
| } | |
| // Validate end date if provided | |
| if (eventData.endDate) { | |
| const startDate = new Date(eventData.startDate); | |
| const endDate = new Date(eventData.endDate); | |
| if (isNaN(endDate.getTime())) { | |
| errors.push('End date must be a valid date'); | |
| } else if (endDate < startDate) { | |
| errors.push('End date must be after start date'); | |
| } | |
| } | |
| // Validate event type | |
| const validEventTypes = [ | |
| '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' | |
| ]; | |
| if (!eventData.eventType || !validEventTypes.includes(eventData.eventType)) { | |
| errors.push('Valid event type is required'); | |
| } | |
| // Validate category | |
| const validCategories = [ | |
| 'academics', 'admissions', 'athletics', 'arts', 'career-services', | |
| 'dining', 'greek-life', 'housing', 'international', 'libraries', | |
| 'recreation', 'research', 'service-learning', 'student-affairs', | |
| 'student-organizations', 'technology', 'transportation', 'wellness', 'other' | |
| ]; | |
| if (!eventData.category || !validCategories.includes(eventData.category)) { | |
| errors.push('Valid category is required'); | |
| } | |
| return { | |
| isValid: errors.length === 0, | |
| errors: errors | |
| }; | |
| } | |
| // Format event for API response | |
| static formatEventForAPI(event) { | |
| return { | |
| id: event._id, | |
| title: event.title, | |
| description: event.description, | |
| shortDescription: event.shortDescription, | |
| eventType: event.eventType, | |
| category: event.category, | |
| startDate: event.startDate, | |
| endDate: event.endDate, | |
| allDay: event.allDay, | |
| timezone: event.timezone, | |
| location: event.location, | |
| audience: event.audience, | |
| academic: event.academic, | |
| priority: event.priority, | |
| isImportantDate: event.isImportantDate, | |
| isFeatured: event.isFeatured, | |
| organizer: event.organizer, | |
| contact: event.contact, | |
| images: event.images, | |
| links: event.links, | |
| tags: event.tags, | |
| keywords: event.keywords, | |
| status: event.status, | |
| visibility: event.visibility, | |
| analytics: event.analytics, | |
| createdAt: event.createdAt, | |
| updatedAt: event.updatedAt, | |
| metadata: { | |
| source: event.metadata?.source, | |
| lastSynced: event.metadata?.lastSynced | |
| } | |
| }; | |
| } | |
| // Create event summary for dashboard | |
| static createEventSummary(event) { | |
| return { | |
| id: event._id, | |
| title: event.title, | |
| shortDescription: event.shortDescription || | |
| (event.description ? event.description.substring(0, 100) + '...' : ''), | |
| eventType: event.eventType, | |
| category: event.category, | |
| startDate: event.startDate, | |
| location: event.location?.name || 'TBD', | |
| priority: event.priority, | |
| isImportantDate: event.isImportantDate, | |
| audience: event.audience | |
| }; | |
| } | |
| // Generate calendar-friendly event format | |
| static formatForCalendar(event) { | |
| return { | |
| id: event._id.toString(), | |
| title: event.title, | |
| start: event.startDate.toISOString(), | |
| end: event.endDate ? event.endDate.toISOString() : null, | |
| allDay: event.allDay, | |
| description: event.description, | |
| location: event.location?.name, | |
| category: event.category, | |
| priority: event.priority, | |
| url: event.links?.find(link => link.type === 'website')?.url, | |
| backgroundColor: this.getCategoryColor(event.category), | |
| borderColor: this.getPriorityColor(event.priority) | |
| }; | |
| } | |
| // Get color coding for categories | |
| static getCategoryColor(category) { | |
| const categoryColors = { | |
| 'academics': '#007bff', // Blue | |
| 'athletics': '#28a745', // Green | |
| 'arts': '#dc3545', // Red | |
| 'career-services': '#fd7e14', // Orange | |
| 'dining': '#6f42c1', // Purple | |
| 'student-affairs': '#20c997', // Teal | |
| 'housing': '#ffc107', // Yellow | |
| 'other': '#6c757d' // Gray | |
| }; | |
| return categoryColors[category] || categoryColors['other']; | |
| } | |
| // Get color coding for priority levels | |
| static getPriorityColor(priority) { | |
| const priorityColors = { | |
| 'critical': '#dc3545', // Red | |
| 'high': '#fd7e14', // Orange | |
| 'medium': '#ffc107', // Yellow | |
| 'low': '#28a745' // Green | |
| }; | |
| return priorityColors[priority] || priorityColors['medium']; | |
| } | |
| // Calculate time until event | |
| static getTimeUntilEvent(eventDate) { | |
| const now = new Date(); | |
| const timeDiff = new Date(eventDate) - now; | |
| if (timeDiff < 0) { | |
| return { passed: true, message: 'Event has passed' }; | |
| } | |
| 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)); | |
| if (days > 0) { | |
| return { | |
| passed: false, | |
| message: `${days} day${days > 1 ? 's' : ''} ${hours > 0 ? `and ${hours} hour${hours > 1 ? 's' : ''}` : ''}`, | |
| days, hours, minutes | |
| }; | |
| } else if (hours > 0) { | |
| return { | |
| passed: false, | |
| message: `${hours} hour${hours > 1 ? 's' : ''} ${minutes > 0 ? `and ${minutes} minute${minutes > 1 ? 's' : ''}` : ''}`, | |
| days: 0, hours, minutes | |
| }; | |
| } else { | |
| return { | |
| passed: false, | |
| message: `${minutes} minute${minutes > 1 ? 's' : ''}`, | |
| days: 0, hours: 0, minutes | |
| }; | |
| } | |
| } | |
| // Check if event is happening now | |
| static isEventHappening(event) { | |
| const now = new Date(); | |
| const startDate = new Date(event.startDate); | |
| const endDate = event.endDate ? new Date(event.endDate) : startDate; | |
| return now >= startDate && now <= endDate; | |
| } | |
| // Filter events by multiple criteria | |
| static filterEvents(events, filters = {}) { | |
| return events.filter(event => { | |
| // Filter by event types | |
| if (filters.eventTypes && filters.eventTypes.length > 0) { | |
| if (!filters.eventTypes.includes(event.eventType)) { | |
| return false; | |
| } | |
| } | |
| // Filter by categories | |
| if (filters.categories && filters.categories.length > 0) { | |
| if (!filters.categories.includes(event.category)) { | |
| return false; | |
| } | |
| } | |
| // Filter by audience | |
| if (filters.audience && filters.audience.length > 0) { | |
| const hasMatchingAudience = filters.audience.some(aud => | |
| event.audience && event.audience.includes(aud) | |
| ); | |
| if (!hasMatchingAudience) { | |
| return false; | |
| } | |
| } | |
| // Filter by priority | |
| if (filters.priority && filters.priority.length > 0) { | |
| if (!filters.priority.includes(event.priority)) { | |
| return false; | |
| } | |
| } | |
| // Filter by date range | |
| if (filters.startDate || filters.endDate) { | |
| const eventStart = new Date(event.startDate); | |
| if (filters.startDate && eventStart < new Date(filters.startDate)) { | |
| return false; | |
| } | |
| if (filters.endDate && eventStart > new Date(filters.endDate)) { | |
| return false; | |
| } | |
| } | |
| // Filter by important dates only | |
| if (filters.importantOnly && !event.isImportantDate) { | |
| return false; | |
| } | |
| // Filter by featured events only | |
| if (filters.featuredOnly && !event.isFeatured) { | |
| return false; | |
| } | |
| // Text search in title and description | |
| if (filters.searchText) { | |
| const searchLower = filters.searchText.toLowerCase(); | |
| const titleMatch = event.title.toLowerCase().includes(searchLower); | |
| const descMatch = event.description && event.description.toLowerCase().includes(searchLower); | |
| const locationMatch = event.location?.name?.toLowerCase().includes(searchLower); | |
| if (!titleMatch && !descMatch && !locationMatch) { | |
| return false; | |
| } | |
| } | |
| return true; | |
| }); | |
| } | |
| // Sort events by multiple criteria | |
| static sortEvents(events, sortOptions = {}) { | |
| const { field = 'startDate', direction = 'asc' } = sortOptions; | |
| return events.sort((a, b) => { | |
| let aValue, bValue; | |
| switch (field) { | |
| case 'startDate': | |
| aValue = new Date(a.startDate); | |
| bValue = new Date(b.startDate); | |
| break; | |
| case 'title': | |
| aValue = a.title.toLowerCase(); | |
| bValue = b.title.toLowerCase(); | |
| break; | |
| case 'priority': | |
| const priorityOrder = { 'critical': 4, 'high': 3, 'medium': 2, 'low': 1 }; | |
| aValue = priorityOrder[a.priority] || 2; | |
| bValue = priorityOrder[b.priority] || 2; | |
| break; | |
| case 'category': | |
| aValue = a.category; | |
| bValue = b.category; | |
| break; | |
| default: | |
| aValue = a[field]; | |
| bValue = b[field]; | |
| } | |
| if (aValue < bValue) { | |
| return direction === 'asc' ? -1 : 1; | |
| } | |
| if (aValue > bValue) { | |
| return direction === 'asc' ? 1 : -1; | |
| } | |
| return 0; | |
| }); | |
| } | |
| // Pagination helper | |
| static paginateEvents(events, page = 1, limit = 20) { | |
| const startIndex = (page - 1) * limit; | |
| const endIndex = startIndex + limit; | |
| return { | |
| events: events.slice(startIndex, endIndex), | |
| pagination: { | |
| currentPage: page, | |
| totalPages: Math.ceil(events.length / limit), | |
| totalEvents: events.length, | |
| eventsPerPage: limit, | |
| hasNextPage: endIndex < events.length, | |
| hasPrevPage: page > 1 | |
| } | |
| }; | |
| } | |
| // Generate suggested events based on user preferences | |
| static suggestEvents(allEvents, userPreferences = {}) { | |
| const { | |
| interests = [], | |
| classification = 'all-students', | |
| preferredCategories = [], | |
| excludeEventTypes = [] | |
| } = userPreferences; | |
| let suggestions = allEvents.filter(event => { | |
| // Only include future events | |
| if (new Date(event.startDate) <= new Date()) { | |
| return false; | |
| } | |
| // Exclude certain event types | |
| if (excludeEventTypes.includes(event.eventType)) { | |
| return false; | |
| } | |
| // Match audience | |
| if (!event.audience.includes(classification) && !event.audience.includes('all-students')) { | |
| return false; | |
| } | |
| return true; | |
| }); | |
| // Score events based on preferences | |
| suggestions = suggestions.map(event => { | |
| let score = 0; | |
| // Category preference scoring | |
| if (preferredCategories.includes(event.category)) { | |
| score += 10; | |
| } | |
| // Interest-based scoring | |
| const eventText = `${event.title} ${event.description || ''}`.toLowerCase(); | |
| interests.forEach(interest => { | |
| if (eventText.includes(interest.toLowerCase())) { | |
| score += 5; | |
| } | |
| }); | |
| // Priority scoring | |
| const priorityScores = { 'critical': 8, 'high': 6, 'medium': 4, 'low': 2 }; | |
| score += priorityScores[event.priority] || 4; | |
| // Featured event bonus | |
| if (event.isFeatured) { | |
| score += 3; | |
| } | |
| // Important date bonus | |
| if (event.isImportantDate) { | |
| score += 2; | |
| } | |
| return { ...event, suggestionScore: score }; | |
| }); | |
| // Sort by score and return top suggestions | |
| return suggestions | |
| .sort((a, b) => b.suggestionScore - a.suggestionScore) | |
| .slice(0, 10); | |
| } | |
| // Create event digest/summary | |
| static createEventDigest(events, options = {}) { | |
| const { includeImportant = true, includeFeatured = true, daysAhead = 7 } = options; | |
| const endDate = new Date(); | |
| endDate.setDate(endDate.getDate() + daysAhead); | |
| const upcomingEvents = events.filter(event => { | |
| const eventDate = new Date(event.startDate); | |
| return eventDate >= new Date() && eventDate <= endDate; | |
| }); | |
| const digest = { | |
| period: `Next ${daysAhead} days`, | |
| totalEvents: upcomingEvents.length, | |
| importantDates: [], | |
| featuredEvents: [], | |
| eventsByCategory: {}, | |
| eventsByDay: {} | |
| }; | |
| // Group events by category and day | |
| upcomingEvents.forEach(event => { | |
| // By category | |
| if (!digest.eventsByCategory[event.category]) { | |
| digest.eventsByCategory[event.category] = []; | |
| } | |
| digest.eventsByCategory[event.category].push(this.createEventSummary(event)); | |
| // By day | |
| const dayKey = event.startDate.toISOString().split('T')[0]; | |
| if (!digest.eventsByDay[dayKey]) { | |
| digest.eventsByDay[dayKey] = []; | |
| } | |
| digest.eventsByDay[dayKey].push(this.createEventSummary(event)); | |
| // Important dates | |
| if (includeImportant && event.isImportantDate) { | |
| digest.importantDates.push(this.createEventSummary(event)); | |
| } | |
| // Featured events | |
| if (includeFeatured && event.isFeatured) { | |
| digest.featuredEvents.push(this.createEventSummary(event)); | |
| } | |
| }); | |
| return digest; | |
| } | |
| // Log event interaction | |
| static logEventInteraction(eventId, interactionType, metadata = {}) { | |
| logger.info('Event interaction', { | |
| eventId, | |
| interactionType, // 'view', 'click', 'share', 'register' | |
| timestamp: new Date().toISOString(), | |
| ...metadata | |
| }); | |
| } | |
| } | |
| module.exports = EventsUtils; |