refactor(calendarUtil): addEventsToCalendar variable naming

This commit is contained in:
xeruf 2024-11-25 13:41:28 +01:00
parent 1706b9cad3
commit ff595a9f70
5 changed files with 33 additions and 114 deletions

View File

@ -1,7 +1,7 @@
import ICAL from './lib/ical.timezones.js';
import fs from 'fs';
import path from 'path';
import axios from 'axios';
import ICAL from './lib/ical.timezones.js'
import fs from 'fs'
import path from 'path'
import axios from 'axios'
export const MERGED_CALENDARS_DIR = path.join(process.cwd(), 'calendar');
@ -26,81 +26,6 @@ export async function fetchCalendarData(calendar) {
}
}
// Helper function to check if TZID exists in the raw property string
function hasTZID(rawProperty) {
return rawProperty.includes('TZID=');
}
// Function to process DTSTART/DTEND
function processDateTimeProperty(event, propertyName, newEvent) {
const rawProperty = event.getFirstProperty(propertyName)?.toICALString();
if (!rawProperty) {
console.log(`No raw property found for ${propertyName}`);
return;
}
console.log(`Raw property: ${rawProperty}`); // Log the raw property
// Check if it's a date-based event (VALUE=DATE)
if (rawProperty.includes('VALUE=DATE')) {
console.log(`Date-based event detected for ${propertyName}: ${rawProperty}`);
// Split to get the date part (should be in the format YYYYMMDD)
const dateOnly = rawProperty.split(':')[1]; // e.g., "20231225"
console.log(`Extracted date string: ${dateOnly}`);
if (!dateOnly) {
console.error(`Error: Could not extract date from ${rawProperty}`);
return;
}
// Ensure the date string is valid (no dashes, just YYYYMMDD)
const year = dateOnly.slice(0, 4);
const month = dateOnly.slice(4, 6);
const day = dateOnly.slice(6, 8);
console.log(`Parsed date: ${year}-${month}-${day}`);
// Check if the date is valid
if (!year || !month || !day || isNaN(new Date(`${year}-${month}-${day}`))) {
console.error(`Invalid date parsed from raw property: ${rawProperty}`);
return;
}
const formattedDate = dateOnly; // Use the date string as is (YYYYMMDD format)
console.log(`Formatted date: ${formattedDate}`);
// Log before adding the property to ensure it's correct
console.log(`Adding date-based property with value: ${propertyName};VALUE=DATE:${formattedDate}`);
// Correct property name usage (DTSTART not dtstart)
const property = new ICAL.Property(propertyName.toUpperCase(), newEvent); // Use uppercase "DTSTART"
property.setValue(`VALUE=DATE:${formattedDate}`);
// Log the property object before adding it to ensure everything is correct
console.log(`Property to add:`, property);
newEvent.addProperty(property);
} else {
console.log(`Time-based event detected for ${propertyName}: ${rawProperty}`);
// Time-based event processing (existing logic)
const dateTime = event.getFirstPropertyValue(propertyName);
const dateTimeString = dateTime.toString();
const property = new ICAL.Property(propertyName, newEvent);
property.setValue(dateTimeString);
if (hasTZID(rawProperty)) {
// If raw property includes TZID, add it
property.setParameter('TZID', dateTime.zone.tzid);
}
newEvent.addProperty(property);
}
}
// Create a top-level VCALENDAR component
export function createCalendarComponent(name) {
const calendarComponent = new ICAL.Component(['vcalendar', [], []]);
@ -112,77 +37,71 @@ export function createCalendarComponent(name) {
}
// Add events to the calendar component
export function addEventsToCalendar(calendarComponent, calendars, overrideFlag = false) {
export function addEventsToCalendar(newCalendar, calendars, overrideFlag = false) {
let defaultTimeZone = null; // To store the first found X-WR-TIMEZONE
calendars.forEach((calendar) => {
calendars.forEach((calendarRaw) => {
try {
const parsed = ICAL.parse(calendar.data);
const component = new ICAL.Component(parsed);
const calendar = new ICAL.Component(ICAL.parse(calendarRaw.data));
// Extract METHOD from the parsed data (if available)
const method = component.getFirstPropertyValue('method');
const method = calendar.getFirstPropertyValue('method');
if (method) {
console.log(`Extracted METHOD: ${method}`);
// Only add the METHOD property once
if (!calendarComponent.getFirstPropertyValue('method')) {
calendarComponent.updatePropertyWithValue('method', method.toUpperCase());
if (!newCalendar.getFirstPropertyValue('method')) {
newCalendar.updatePropertyWithValue('method', method.toUpperCase());
}
}
// Extract X-WR-TIMEZONE if available
const wrTimeZone = component.getFirstPropertyValue('x-wr-timezone');
const wrTimeZone = calendar.getFirstPropertyValue('x-wr-timezone');
if (wrTimeZone) {
console.log(`Extracted X-WR-TIMEZONE: ${wrTimeZone}`);
// Set it as the default if not already set
if (!defaultTimeZone) {
defaultTimeZone = wrTimeZone;
if (!calendarComponent.getFirstPropertyValue('x-wr-timezone')) {
calendarComponent.updatePropertyWithValue('x-wr-timezone', defaultTimeZone);
if (!newCalendar.getFirstPropertyValue('x-wr-timezone')) {
newCalendar.updatePropertyWithValue('x-wr-timezone', defaultTimeZone);
}
}
}
// Extract and add VTIMEZONE components
const timezones = component.getAllSubcomponents('vtimezone');
const timezones = calendar.getAllSubcomponents('vtimezone');
timezones.forEach((timezone) => {
const tzid = timezone.getFirstPropertyValue('tzid');
if (!calendarComponent.getFirstSubcomponent((comp) => comp.name === 'vtimezone' && comp.getFirstPropertyValue('tzid') === tzid)) {
calendarComponent.addSubcomponent(timezone);
if (!newCalendar.getFirstSubcomponent((comp) => comp.name === 'vtimezone' && comp.getFirstPropertyValue('tzid') === tzid)) {
newCalendar.addSubcomponent(timezone);
}
});
// Process VEVENT components
component.getAllSubcomponents('vevent').forEach((event) => {
const vevent = new ICAL.Event(event);
const newEvent = new ICAL.Event(newEvent);
calendar.getAllSubcomponents('vevent').forEach((vevent) => {
const event = new ICAL.Event(vevent);
const newEvent = new ICAL.Event();
newEvent.startDate = vevent.startDate
newEvent.endDate = vevent.endDate
newEvent.uid = event.uid;
newEvent.startDate = event.startDate
newEvent.endDate = event.endDate
// 3. Copy DTSTAMP
const dtstamp = event.getFirstPropertyValue('dtstamp');
const dtstamp = vevent.getFirstPropertyValue('dtstamp');
if (dtstamp) newEvent.component.updatePropertyWithValue('dtstamp', dtstamp);
// 4. Copy UID
newEvent.uid = vevent.uid;
// 5. Add LOCATION (conditionally included)
if (overrideFlag) {
newEvent.summary = 'Busy'
} else {
newEvent.summary = vevent.summary;
if (vevent.location) newEvent.location = vevent.location;
newEvent.summary = event.summary;
if (event.location) newEvent.location = event.location;
}
const rrule = event.getFirstPropertyValue('rrule');
const rrule = vevent.getFirstPropertyValue('rrule');
if (rrule) newEvent.component.updatePropertyWithValue('rrule', rrule);
// Add the VEVENT to the calendar
calendarComponent.addSubcomponent(newEvent.component);
newCalendar.addSubcomponent(newEvent.component);
});
console.log(`Processed VEVENT components for calendar: ${calendar.name}`);
} catch (error) {
console.error(`Error processing calendar:`, calendar, error);
console.error(`Error processing calendar:`, calendarRaw, error);
}
});
}

View File

@ -4,10 +4,10 @@ PRODID:-//CalMerge//Calendar Merger 1.0//EN
VERSION:2.0
CALSCALE:GREGORIAN
BEGIN:VEVENT
UID:20231225T000000-001@example.com
DTSTART;VALUE=DATE:20231225
DTEND;VALUE=DATE:20231226
DTSTAMP:20231225T000000Z
UID:20231225T000000-001@example.com
SUMMARY:Christmas Day
END:VEVENT
END:VCALENDAR

View File

@ -6,10 +6,10 @@ CALSCALE:GREGORIAN
METHOD:PUBLISH
X-WR-TIMEZONE:Africa/Nairobi
BEGIN:VEVENT
UID:6tbrvsitniuu72li7kk15gou2b@google.com
DTSTART:20241003T190000Z
DTEND:20241003T200000Z
DTSTAMP:20241119T115316Z
UID:6tbrvsitniuu72li7kk15gou2b@google.com
SUMMARY:progodessey
END:VEVENT
END:VCALENDAR

View File

@ -30,10 +30,10 @@ SUMMARY:progodessey
TRANSP:OPAQUE
END:VEVENT
BEGIN:VEVENT
UID:7l7n9nltrudluv65gfgll2q930@google.com
DTSTART:20241009T173000Z
DTEND:20241009T183000Z
DTSTAMP:20241119T115316Z
UID:7l7n9nltrudluv65gfgll2q930@google.com
CREATED:20241010T123337Z
LAST-MODIFIED:20241010T123337Z
SEQUENCE:0

View File

@ -21,10 +21,10 @@ RRULE:FREQ=YEARLY;BYMONTH=10;BYDAY=-1SU
END:STANDARD
END:VTIMEZONE
BEGIN:VEVENT
UID:5f4ad965-16a8-48eb-8233-78bf93a8b35e
DTSTART;TZID=Europe/Berlin:20241120T211500
DTEND;TZID=Europe/Berlin:20241120T215000
DTSTAMP:20241113T212909Z
UID:5f4ad965-16a8-48eb-8233-78bf93a8b35e
SUMMARY:JR Weekly Check-In
LOCATION:FaceTime
RRULE:FREQ=WEEKLY;BYDAY=WE