diff --git a/lib/services/microsoft-calendar-sync.ts b/lib/services/microsoft-calendar-sync.ts index a9939f3..e380c7f 100644 --- a/lib/services/microsoft-calendar-sync.ts +++ b/lib/services/microsoft-calendar-sync.ts @@ -285,10 +285,13 @@ export async function syncMicrosoftCalendar( } // Fetch events from Microsoft Graph API + // Sync from 1 month ago to 6 months in the future to catch all events const startDate = new Date(); - startDate.setMonth(startDate.getMonth() - 1); // Sync last month to next 3 months + startDate.setMonth(startDate.getMonth() - 1); + startDate.setHours(0, 0, 0, 0); // Start of day const endDate = new Date(); - endDate.setMonth(endDate.getMonth() + 3); + endDate.setMonth(endDate.getMonth() + 6); + endDate.setHours(23, 59, 59, 999); // End of day logger.info('Starting Microsoft calendar sync', { calendarSyncId, @@ -309,6 +312,10 @@ export async function syncMicrosoftCalendar( logger.info('Fetched Microsoft events', { calendarSyncId, eventCount: microsoftEvents.length, + dateRange: { + start: startDate.toISOString(), + end: endDate.toISOString() + }, events: microsoftEvents.slice(0, 10).map(e => ({ id: e.id, subject: e.subject, @@ -319,12 +326,28 @@ export async function syncMicrosoftCalendar( }); if (microsoftEvents.length === 0) { - logger.warn('No Microsoft events found', { + logger.warn('No Microsoft events found in date range', { calendarSyncId, email: creds.email, externalCalendarId: syncConfig.externalCalendarId, dateRange: { start: startDate.toISOString(), end: endDate.toISOString() }, }); + } else { + // Log events in the future to help debug + const now = new Date(); + const futureEvents = microsoftEvents.filter(e => { + const eventStart = e.start.dateTime || e.start.date; + return new Date(eventStart) > now; + }); + logger.info('Microsoft events in the future', { + calendarSyncId, + futureEventCount: futureEvents.length, + futureEvents: futureEvents.slice(0, 5).map(e => ({ + id: e.id, + subject: e.subject, + start: e.start.dateTime || e.start.date, + })), + }); } // Convert Microsoft events to CalDAV-like format