Spaces:
Sleeping
Sleeping
Download backend/src/services/calendarExportService.js from edichat/edi.chat.test: direct link, hf CLI and curl.
- Browser
- Download file 8.84 kB
-
https://huggingface.co/spaces/edichat/edi.chat.test/resolve/main/backend/src/services/calendarExportService.js
- Command line
-
hf download hf://spaces/edichat/edi.chat.test/backend/src/services/calendarExportService.js
-
curl -L -o calendarExportService.js https://huggingface.co/spaces/edichat/edi.chat.test/resolve/main/backend/src/services/calendarExportService.js
8.84 kB
| const ical = require('ical-generator'); | |
| const logger = require('../utils/logger'); | |
| class CalendarExportService { | |
| async generateICalendar(dueDates, options = {}) { | |
| try { | |
| const calendar = ical({ | |
| domain: 'uga-chatbot.edu', | |
| name: options.calendarName || 'UGA Academic Calendar', | |
| description: options.description || 'Due dates and assignments extracted from course documents', | |
| timezone: options.timezone || 'America/New_York', | |
| ttl: 60 * 60 * 24 // 24 hours | |
| }); | |
| // Add each due date as an event | |
| dueDates.forEach(dueDate => { | |
| this.addDueDateToCalendar(calendar, dueDate, options); | |
| }); | |
| logger.info('Calendar generated successfully', { | |
| eventCount: dueDates.length, | |
| calendarName: options.calendarName | |
| }); | |
| return calendar.toString(); | |
| } catch (error) { | |
| logger.error('Calendar generation failed:', error); | |
| throw new Error('Failed to generate calendar file'); | |
| } | |
| } | |
| addDueDateToCalendar(calendar, dueDate, options = {}) { | |
| try { | |
| const event = calendar.createEvent({ | |
| uid: `duedate-${dueDate._id || Math.random().toString(36)}@uga-chatbot.edu`, | |
| start: this.calculateEventStart(dueDate.dueDate), | |
| end: this.calculateEventEnd(dueDate.dueDate), | |
| summary: this.formatEventSummary(dueDate), | |
| description: this.formatEventDescription(dueDate), | |
| location: options.defaultLocation || 'University of Georgia', | |
| categories: [dueDate.type || 'assignment'], | |
| priority: this.calculatePriority(dueDate) | |
| }); | |
| // Add reminders based on assignment type | |
| this.addReminders(event, dueDate); | |
| return event; | |
| } catch (error) { | |
| logger.warn('Failed to add due date to calendar:', { dueDate: dueDate._id, error: error.message }); | |
| } | |
| } | |
| calculateEventStart(dueDate) { | |
| // For assignments, create an all-day event or set to 11:59 PM if time is not specified | |
| const date = new Date(dueDate); | |
| // If no time is specified (00:00:00), assume it's due at 11:59 PM | |
| if (date.getHours() === 0 && date.getMinutes() === 0 && date.getSeconds() === 0) { | |
| date.setHours(23, 59, 0, 0); | |
| } | |
| return date; | |
| } | |
| calculateEventEnd(dueDate) { | |
| // End time is same as start time for due dates (point-in-time events) | |
| return this.calculateEventStart(dueDate); | |
| } | |
| formatEventSummary(dueDate) { | |
| const coursePrefix = dueDate.course ? `[${dueDate.course}] ` : ''; | |
| const typePrefix = dueDate.type && dueDate.type !== 'other' ? `${this.capitalizeFirst(dueDate.type)}: ` : ''; | |
| return `${coursePrefix}${typePrefix}${dueDate.title}`; | |
| } | |
| formatEventDescription(dueDate) { | |
| let description = `Due Date: ${dueDate.title}\n\n`; | |
| if (dueDate.description) { | |
| description += `Description: ${dueDate.description}\n\n`; | |
| } | |
| if (dueDate.course) { | |
| description += `Course: ${dueDate.course}\n`; | |
| } | |
| if (dueDate.type) { | |
| description += `Type: ${this.capitalizeFirst(dueDate.type)}\n`; | |
| } | |
| if (dueDate.confidence !== undefined) { | |
| description += `Confidence: ${Math.round(dueDate.confidence * 100)}%\n`; | |
| } | |
| if (dueDate.extractedText) { | |
| description += `\nExtracted from: "${dueDate.extractedText}"`; | |
| } | |
| description += '\n\nGenerated by UGA Chatbot Knowledge Base'; | |
| return description; | |
| } | |
| calculatePriority(dueDate) { | |
| // Priority based on type and confidence | |
| const typePriority = { | |
| 'exam': 1, // Highest priority | |
| 'quiz': 2, | |
| 'project': 3, | |
| 'assignment': 4, | |
| 'presentation': 5, | |
| 'reading': 6, | |
| 'other': 7 // Lowest priority | |
| }; | |
| let priority = typePriority[dueDate.type] || 5; | |
| // Adjust based on confidence (higher confidence = higher priority) | |
| if (dueDate.confidence > 0.8) { | |
| priority = Math.max(1, priority - 1); | |
| } else if (dueDate.confidence < 0.5) { | |
| priority = Math.min(9, priority + 1); | |
| } | |
| return priority; | |
| } | |
| addReminders(event, dueDate) { | |
| // Add different reminders based on assignment type | |
| const reminders = []; | |
| switch (dueDate.type) { | |
| case 'exam': | |
| reminders.push( | |
| { trigger: 60 * 24 * 7 }, // 1 week before | |
| { trigger: 60 * 24 * 3 }, // 3 days before | |
| { trigger: 60 * 24 }, // 1 day before | |
| { trigger: 60 * 2 } // 2 hours before | |
| ); | |
| break; | |
| case 'project': | |
| reminders.push( | |
| { trigger: 60 * 24 * 7 }, // 1 week before | |
| { trigger: 60 * 24 * 3 }, // 3 days before | |
| { trigger: 60 * 24 } // 1 day before | |
| ); | |
| break; | |
| case 'assignment': | |
| reminders.push( | |
| { trigger: 60 * 24 * 3 }, // 3 days before | |
| { trigger: 60 * 24 } // 1 day before | |
| ); | |
| break; | |
| case 'quiz': | |
| reminders.push( | |
| { trigger: 60 * 24 }, // 1 day before | |
| { trigger: 60 * 2 } // 2 hours before | |
| ); | |
| break; | |
| default: | |
| reminders.push( | |
| { trigger: 60 * 24 } // 1 day before | |
| ); | |
| } | |
| // Only add reminders for future dates | |
| const now = new Date(); | |
| const dueDateTime = new Date(dueDate.dueDate); | |
| reminders.forEach(reminder => { | |
| const reminderTime = new Date(dueDateTime.getTime() - (reminder.trigger * 60 * 1000)); | |
| if (reminderTime > now) { | |
| event.createAlarm({ | |
| type: 'display', | |
| trigger: reminder.trigger * 60, // Convert minutes to seconds | |
| description: `Reminder: ${dueDate.title} is due in ${this.formatTimeUntilDue(reminder.trigger)}` | |
| }); | |
| } | |
| }); | |
| } | |
| formatTimeUntilDue(minutes) { | |
| if (minutes < 60) { | |
| return `${minutes} minutes`; | |
| } else if (minutes < 60 * 24) { | |
| const hours = Math.floor(minutes / 60); | |
| return `${hours} hour${hours !== 1 ? 's' : ''}`; | |
| } else { | |
| const days = Math.floor(minutes / (60 * 24)); | |
| return `${days} day${days !== 1 ? 's' : ''}`; | |
| } | |
| } | |
| capitalizeFirst(str) { | |
| return str.charAt(0).toUpperCase() + str.slice(1); | |
| } | |
| // Generate calendar for specific filters | |
| async generateFilteredCalendar(dueDates, filters = {}) { | |
| let filteredDueDates = dueDates; | |
| // Filter by course | |
| if (filters.course) { | |
| filteredDueDates = filteredDueDates.filter(dd => | |
| dd.course && dd.course.toLowerCase().includes(filters.course.toLowerCase()) | |
| ); | |
| } | |
| // Filter by type | |
| if (filters.type) { | |
| filteredDueDates = filteredDueDates.filter(dd => dd.type === filters.type); | |
| } | |
| // Filter by date range | |
| if (filters.startDate || filters.endDate) { | |
| const startDate = filters.startDate ? new Date(filters.startDate) : new Date(); | |
| const endDate = filters.endDate ? new Date(filters.endDate) : new Date(Date.now() + 365 * 24 * 60 * 60 * 1000); | |
| filteredDueDates = filteredDueDates.filter(dd => { | |
| const dueDate = new Date(dd.dueDate); | |
| return dueDate >= startDate && dueDate <= endDate; | |
| }); | |
| } | |
| // Filter by minimum confidence | |
| if (filters.minConfidence) { | |
| filteredDueDates = filteredDueDates.filter(dd => | |
| dd.confidence >= parseFloat(filters.minConfidence) | |
| ); | |
| } | |
| const calendarOptions = { | |
| calendarName: this.generateCalendarName(filters), | |
| description: this.generateCalendarDescription(filters), | |
| ...filters | |
| }; | |
| return this.generateICalendar(filteredDueDates, calendarOptions); | |
| } | |
| generateCalendarName(filters) { | |
| let name = 'UGA Academic Calendar'; | |
| if (filters.course) { | |
| name += ` - ${filters.course}`; | |
| } | |
| if (filters.type) { | |
| name += ` - ${this.capitalizeFirst(filters.type)}s`; | |
| } | |
| return name; | |
| } | |
| generateCalendarDescription(filters) { | |
| let description = 'Due dates and assignments extracted from course documents'; | |
| const filterDescriptions = []; | |
| if (filters.course) { | |
| filterDescriptions.push(`Course: ${filters.course}`); | |
| } | |
| if (filters.type) { | |
| filterDescriptions.push(`Type: ${this.capitalizeFirst(filters.type)}`); | |
| } | |
| if (filters.startDate || filters.endDate) { | |
| const start = filters.startDate ? new Date(filters.startDate).toDateString() : 'All dates'; | |
| const end = filters.endDate ? new Date(filters.endDate).toDateString() : 'onwards'; | |
| filterDescriptions.push(`Date range: ${start} to ${end}`); | |
| } | |
| if (filterDescriptions.length > 0) { | |
| description += ` (Filtered: ${filterDescriptions.join(', ')})`; | |
| } | |
| return description; | |
| } | |
| } | |
| module.exports = new CalendarExportService(); |