diff --git a/lib/services/microsoft-calendar-sync.ts b/lib/services/microsoft-calendar-sync.ts index e088bc8..2294ab7 100644 --- a/lib/services/microsoft-calendar-sync.ts +++ b/lib/services/microsoft-calendar-sync.ts @@ -138,28 +138,22 @@ export async function fetchMicrosoftEvents( }; // Add date filter if provided - // Note: Microsoft Graph API filter syntax - // For timed events: start/dateTime - // For all-day events: start/date - // We need to handle both cases + // Note: Microsoft Graph API filter syntax is limited + // We can't easily filter both dateTime and date in one query + // So we'll filter by dateTime and handle all-day events separately if needed if (startDate && endDate) { // Format dates for Microsoft Graph API - // For dateTime: ISO 8601 format (e.g., 2026-01-14T21:00:00Z) - // For date: YYYY-MM-DD format - const startDateStr = startDate.toISOString().split('T')[0]; - const endDateStr = endDate.toISOString().split('T')[0]; + // Use ISO 8601 format for dateTime filter const startDateTimeStr = startDate.toISOString(); const endDateTimeStr = endDate.toISOString(); - // Microsoft Graph API filter: match events where start is within range - // This handles both timed events (dateTime) and all-day events (date) - // The filter checks if either dateTime OR date is within range - params.$filter = `(start/dateTime ge '${startDateTimeStr}' or start/date ge '${startDateStr}') and (start/dateTime le '${endDateTimeStr}' or start/date le '${endDateStr}')`; + // Microsoft Graph API filter: filter by start/dateTime + // This will match timed events. All-day events might need separate handling + // but Microsoft Graph usually returns all-day events with dateTime set to start of day + params.$filter = `start/dateTime ge '${startDateTimeStr}' and start/dateTime le '${endDateTimeStr}'`; logger.debug('Microsoft Graph API filter', { filter: params.$filter, - startDate: startDateStr, - endDate: endDateStr, startDateTime: startDateTimeStr, endDateTime: endDateTimeStr, }); @@ -214,13 +208,24 @@ export async function fetchMicrosoftEvents( } return events; - } catch (error) { - logger.error('Error fetching Microsoft events', { + } catch (error: any) { + // Log detailed error information for debugging + const errorDetails: any = { userId, email, calendarId, error: error instanceof Error ? error.message : String(error), - }); + }; + + if (error.response) { + errorDetails.status = error.response.status; + errorDetails.statusText = error.response.statusText; + errorDetails.data = error.response.data; + errorDetails.url = error.config?.url; + errorDetails.params = error.config?.params; + } + + logger.error('Error fetching Microsoft events', errorDetails); throw error; } }