diff --git a/app/agenda/page.tsx b/app/agenda/page.tsx index 4e12c95..5351589 100644 --- a/app/agenda/page.tsx +++ b/app/agenda/page.tsx @@ -579,16 +579,25 @@ export default async function CalendarPage() { if (needsSync) { console.log(`[AGENDA] Triggering background sync for Microsoft calendar ${syncConfig.id}`); // Trigger sync in background (don't await to avoid blocking page load) + // The sync will update the database, and the next page load will show the events import('@/lib/services/microsoft-calendar-sync').then(({ syncMicrosoftCalendar }) => { syncMicrosoftCalendar(syncConfig.id, false).then((result) => { - console.log(`[AGENDA] Microsoft sync completed:`, result); + console.log(`[AGENDA] Microsoft sync completed:`, { + calendarSyncId: syncConfig.id, + synced: result.synced, + created: result.created, + updated: result.updated, + }); }).catch((error) => { - console.error('Background sync failed for Microsoft calendar', { + console.error('[AGENDA] Background sync failed for Microsoft calendar', { calendarSyncId: syncConfig.id, error: error instanceof Error ? error.message : String(error), + stack: error instanceof Error ? error.stack : undefined, }); }); }); + } else { + console.log(`[AGENDA] Microsoft sync skipped - too soon since last sync (${syncConfig.syncFrequency} min)`); } } diff --git a/lib/services/microsoft-calendar-sync.ts b/lib/services/microsoft-calendar-sync.ts index 351277c..4a73f85 100644 --- a/lib/services/microsoft-calendar-sync.ts +++ b/lib/services/microsoft-calendar-sync.ts @@ -264,6 +264,14 @@ export async function syncMicrosoftCalendar( const endDate = new Date(); endDate.setMonth(endDate.getMonth() + 3); + logger.info('Starting Microsoft calendar sync', { + calendarSyncId, + calendarId: syncConfig.calendarId, + email: creds.email, + externalCalendarId: syncConfig.externalCalendarId, + dateRange: { start: startDate.toISOString(), end: endDate.toISOString() }, + }); + const microsoftEvents = await fetchMicrosoftEvents( syncConfig.calendar.userId, creds.email, @@ -272,6 +280,12 @@ export async function syncMicrosoftCalendar( endDate ); + logger.info('Fetched Microsoft events', { + calendarSyncId, + eventCount: microsoftEvents.length, + events: microsoftEvents.slice(0, 5).map(e => ({ id: e.id, subject: e.subject, start: e.start.dateTime })), + }); + // Convert Microsoft events to CalDAV-like format const caldavEvents = microsoftEvents.map(convertMicrosoftEventToCalDAV); @@ -286,6 +300,12 @@ export async function syncMicrosoftCalendar( let updated = 0; let deleted = 0; + logger.info('Syncing events to database', { + calendarSyncId, + existingEventsCount: existingEvents.length, + newEventsCount: caldavEvents.length, + }); + // Sync events: create or update for (const caldavEvent of caldavEvents) { // Store Microsoft ID in description with a special prefix for matching