Agenda refactor

This commit is contained in:
alma 2026-01-14 21:13:41 +01:00
parent 52b0b041eb
commit 108fa3fb8c
2 changed files with 31 additions and 2 deletions

View File

@ -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)`);
}
}

View File

@ -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