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) => {
console.log(`[AGENDA] Microsoft sync completed:`, {
calendarSyncId: syncConfig.id,
calendarId: syncConfig.calendarId,
synced: result.synced,
created: result.created,
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) => {
console.error('[AGENDA] Background sync failed for Microsoft calendar', {
calendarSyncId: syncConfig.id,
calendarId: syncConfig.calendarId,
error: error instanceof Error ? error.message : String(error),
stack: error instanceof Error ? error.stack : undefined,
});
@ -738,7 +750,16 @@ export default async function CalendarPage() {
const calendarsWithSync = calendars.filter(cal => cal.syncConfig);
console.log(`[AGENDA] Total calendars with syncConfig: ${calendarsWithSync.length}`);
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();

View File

@ -163,18 +163,28 @@ export async function fetchMicrosoftEvents(
});
// Get events from Microsoft Graph API
const response = await axios.get(
`https://graph.microsoft.com/v1.0/me/calendars/${calendarId}/events`,
{
headers: {
Authorization: `Bearer ${accessToken}`,
'Content-Type': 'application/json',
},
params,
}
);
const url = `https://graph.microsoft.com/v1.0/me/calendars/${calendarId}/events`;
logger.debug('Fetching Microsoft events', {
url,
params: JSON.stringify(params),
});
const response = await axios.get(url, {
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) {
logger.error('Error fetching Microsoft events', {
userId,
@ -307,6 +317,15 @@ export async function syncMicrosoftCalendar(
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
const caldavEvents = microsoftEvents.map(convertMicrosoftEventToCalDAV);
@ -419,10 +438,22 @@ export async function syncMicrosoftCalendar(
logger.info('Microsoft calendar sync completed', {
calendarSyncId,
calendarId: syncConfig.calendarId,
email: creds.email,
synced: caldavEvents.length,
created,
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 {
synced: caldavEvents.length,