Agenda refactor

This commit is contained in:
alma 2026-01-14 21:22:31 +01:00
parent 2c68c00d9b
commit 59b0d52639
2 changed files with 64 additions and 12 deletions

View File

@ -594,13 +594,25 @@ export default async function CalendarPage() {
syncMicrosoftCalendar(syncConfig.id, true).then((result) => { syncMicrosoftCalendar(syncConfig.id, true).then((result) => {
console.log(`[AGENDA] Microsoft sync completed:`, { console.log(`[AGENDA] Microsoft sync completed:`, {
calendarSyncId: syncConfig.id, calendarSyncId: syncConfig.id,
calendarId: syncConfig.calendarId,
synced: result.synced, synced: result.synced,
created: result.created, created: result.created,
updated: result.updated, updated: result.updated,
deleted: result.deleted,
});
// Verify events were created by checking the database
prisma.event.count({
where: { calendarId: syncConfig.calendarId }
}).then((count) => {
console.log(`[AGENDA] Total events in calendar ${syncConfig.calendarId} after sync: ${count}`);
}).catch((err) => {
console.error('[AGENDA] Error counting events:', err);
}); });
}).catch((error) => { }).catch((error) => {
console.error('[AGENDA] Background sync failed for Microsoft calendar', { console.error('[AGENDA] Background sync failed for Microsoft calendar', {
calendarSyncId: syncConfig.id, calendarSyncId: syncConfig.id,
calendarId: syncConfig.calendarId,
error: error instanceof Error ? error.message : String(error), error: error instanceof Error ? error.message : String(error),
stack: error instanceof Error ? error.stack : undefined, stack: error instanceof Error ? error.stack : undefined,
}); });
@ -738,7 +750,16 @@ export default async function CalendarPage() {
const calendarsWithSync = calendars.filter(cal => cal.syncConfig); const calendarsWithSync = calendars.filter(cal => cal.syncConfig);
console.log(`[AGENDA] Total calendars with syncConfig: ${calendarsWithSync.length}`); console.log(`[AGENDA] Total calendars with syncConfig: ${calendarsWithSync.length}`);
calendarsWithSync.forEach(cal => { calendarsWithSync.forEach(cal => {
console.log(`[AGENDA] Calendar: ${cal.name}, provider: ${cal.syncConfig?.provider}, syncEnabled: ${cal.syncConfig?.syncEnabled}, hasMailCredential: ${!!cal.syncConfig?.mailCredential}`); const eventCount = cal.events?.length || 0;
console.log(`[AGENDA] Calendar: ${cal.name}, provider: ${cal.syncConfig?.provider}, syncEnabled: ${cal.syncConfig?.syncEnabled}, hasMailCredential: ${!!cal.syncConfig?.mailCredential}, events: ${eventCount}`);
if (eventCount > 0 && cal.syncConfig?.provider === 'microsoft') {
console.log(`[AGENDA] Microsoft calendar events:`, cal.events.slice(0, 5).map(e => ({
id: e.id,
title: e.title,
start: e.start,
end: e.end
})));
}
}); });
const now = new Date(); const now = new Date();

View File

@ -163,18 +163,28 @@ export async function fetchMicrosoftEvents(
}); });
// Get events from Microsoft Graph API // Get events from Microsoft Graph API
const response = await axios.get( const url = `https://graph.microsoft.com/v1.0/me/calendars/${calendarId}/events`;
`https://graph.microsoft.com/v1.0/me/calendars/${calendarId}/events`, logger.debug('Fetching Microsoft events', {
{ url,
headers: { params: JSON.stringify(params),
Authorization: `Bearer ${accessToken}`, });
'Content-Type': 'application/json',
}, const response = await axios.get(url, {
params, headers: {
} Authorization: `Bearer ${accessToken}`,
); 'Content-Type': 'application/json',
},
params,
});
return response.data.value || []; const events = response.data.value || [];
logger.debug('Microsoft Graph API response', {
calendarId,
eventCount: events.length,
hasValue: !!response.data.value,
});
return events;
} catch (error) { } catch (error) {
logger.error('Error fetching Microsoft events', { logger.error('Error fetching Microsoft events', {
userId, userId,
@ -307,6 +317,15 @@ export async function syncMicrosoftCalendar(
end: e.end.dateTime || e.end.date end: e.end.dateTime || e.end.date
})), })),
}); });
if (microsoftEvents.length === 0) {
logger.warn('No Microsoft events found', {
calendarSyncId,
email: creds.email,
externalCalendarId: syncConfig.externalCalendarId,
dateRange: { start: startDate.toISOString(), end: endDate.toISOString() },
});
}
// Convert Microsoft events to CalDAV-like format // Convert Microsoft events to CalDAV-like format
const caldavEvents = microsoftEvents.map(convertMicrosoftEventToCalDAV); const caldavEvents = microsoftEvents.map(convertMicrosoftEventToCalDAV);
@ -419,10 +438,22 @@ export async function syncMicrosoftCalendar(
logger.info('Microsoft calendar sync completed', { logger.info('Microsoft calendar sync completed', {
calendarSyncId, calendarSyncId,
calendarId: syncConfig.calendarId, calendarId: syncConfig.calendarId,
email: creds.email,
synced: caldavEvents.length, synced: caldavEvents.length,
created, created,
updated, updated,
deleted,
}); });
// Log summary of created/updated events
if (created > 0 || updated > 0) {
logger.info('Microsoft calendar sync summary', {
calendarSyncId,
newEventsCreated: created,
eventsUpdated: updated,
totalEventsInCalendar: caldavEvents.length,
});
}
return { return {
synced: caldavEvents.length, synced: caldavEvents.length,