From 2e9de0749dcbf7f79c46fdb5e9f20f418a052239 Mon Sep 17 00:00:00 2001 From: Ryan Mwangi Date: Wed, 13 Nov 2024 02:54:42 +0300 Subject: [PATCH] fix:handle timebased events --- src/calendarUtil.js | 34 +++++++++----- test/calendar.test.js | 52 ++++++++++------------ test/test_calendars/work_task_calendar.ics | 8 +--- 3 files changed, 48 insertions(+), 46 deletions(-) diff --git a/src/calendarUtil.js b/src/calendarUtil.js index e7c35c5..b8976b9 100644 --- a/src/calendarUtil.js +++ b/src/calendarUtil.js @@ -44,24 +44,34 @@ export function addEventsToCalendar(calendarComponent, results) { component.getAllSubcomponents('vevent').forEach((event) => { const vevent = new ICAL.Event(event); const newEvent = new ICAL.Component('vevent'); - + // Use ICAL.Time to handle dates correctly const startDate = vevent.startDate; const endDate = vevent.endDate; // Create new ICAL.Time objects for start and end dates - const startTime = new ICAL.Time(); - startTime.year = startDate.year; - startTime.month = startDate.month; - startTime.day = startDate.day; - startTime.isDate = true; // Set as all-day event - - const endTime = new ICAL.Time(); - endTime.year = endDate.year; - endTime.month = endDate.month; - endTime.day = endDate.day; - endTime.isDate = true; // Set as all-day event + const startTime = new ICAL.Time({ + year: startDate.year, + month: startDate.month, + day: startDate.day, + hour: startDate.isDate ? null : startDate.hour, + minute: startDate.isDate ? null : startDate.minute, + second: startDate.isDate ? null : startDate.second, + zone: startDate.zone + }); + startTime.isDate = startDate.isDate; + const endTime = new ICAL.Time({ + year: endDate.year, + month: endDate.month, + day: endDate.day, + hour: endDate.isDate ? null : endDate.hour, + minute: endDate.isDate ? null : endDate.minute, + second: endDate.isDate ? null : endDate.second, + zone: endDate.zone + }); + endTime.isDate = endDate.isDate; + // Retain the existing DTSTAMP from vevent const dtstampProperty = event.getFirstProperty('dtstamp'); const dtstamp = dtstampProperty ? dtstampProperty.getFirstValue() : null; diff --git a/test/calendar.test.js b/test/calendar.test.js index e94936b..89ec26d 100644 --- a/test/calendar.test.js +++ b/test/calendar.test.js @@ -92,37 +92,33 @@ describe('Calendar Merging API', () => { // expect(actualOutput).toBe(expectedOutput); // }); - // test('Merge time-based calendar', async () => { - // const response = await request(server) - // .post('/merge') - // .send({ - // linkGroupName: 'Time Based Calendar', - // calendars: [ - // { - // url: loadCalendarFile('team_meeting_calendar.ics'), - // prefix: 'team_meeting_calendar', - // override: false, - // }, - // { - // url: loadCalendarFile('work_task_calendar.ics'), - // prefix: 'work_task', - // override: false, - // }, - // ], - // }); + test('Merge time-based calendar', async () => { + const input = loadCalendarFile('work_task_calendar.ics'); + const response = await request(server) + .post('/merge') + .send({ + linkGroupName: 'Time Based Calendar', + calendars: [ + { + url: input, + prefix: 'work_task', + override: false, + }, + ], + }); - // expect(response.status).toBe(200); - // expect(response.body.url).toMatch(/calendar\/Time_Based_Calendar/); + expect(response.status).toBe(200); + expect(response.body.url).toMatch(/calendar\/Time_Based_Calendar/); - // // Check if the file was created in the test directory - // const filePath = path.join(CALENDARS_DIR, 'Time_Based_Calendar.ics'); - // expect(fs.existsSync(filePath)).toBe(true); + // Check if the file was created in the test directory + const filePath = path.join(CALENDARS_DIR, 'Time_Based_Calendar.ics'); + expect(fs.existsSync(filePath)).toBe(true); - // // Load expected output and compare - // const expectedOutput = loadExpectedOutput('Time_Based_Calendar.ics'); - // const actualOutput = fs.readFileSync (filePath, 'utf8'); - // expect(actualOutput).toBe(expectedOutput); - // }); + // Load expected output and compare + const expectedOutput = fs.readFileSync(input, 'utf8'); + const actualOutput = fs.readFileSync(filePath, 'utf8'); + expect(actualOutput).toBe(expectedOutput); + }); // test('Merge calendar without prefix', async () => { // const response = await request(server) diff --git a/test/test_calendars/work_task_calendar.ics b/test/test_calendars/work_task_calendar.ics index 6ca9a59..1c6bc00 100644 --- a/test/test_calendars/work_task_calendar.ics +++ b/test/test_calendars/work_task_calendar.ics @@ -1,15 +1,11 @@ BEGIN:VCALENDAR +NAME:Time Based Calendar VERSION:2.0 -PRODID:-//Example Corp//NONSGML Event//EN BEGIN:VEVENT UID:20231108T090000-001@example.com DTSTAMP:20231108T090000Z DTSTART:20231108T090000Z DTEND:20231108T100000Z SUMMARY:Work Task -DESCRIPTION:Time-based work task event. -LOCATION:Office -STATUS:CONFIRMED -SEQUENCE:0 END:VEVENT -END:VCALENDAR +END:VCALENDAR \ No newline at end of file