Agenda refactor
This commit is contained in:
parent
e5763d7ab0
commit
35799463f5
@ -752,13 +752,27 @@ export default async function CalendarPage() {
|
|||||||
calendarsWithSync.forEach(cal => {
|
calendarsWithSync.forEach(cal => {
|
||||||
const eventCount = cal.events?.length || 0;
|
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}`);
|
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') {
|
if (cal.syncConfig?.provider === 'microsoft') {
|
||||||
console.log(`[AGENDA] Microsoft calendar events:`, cal.events.slice(0, 5).map(e => ({
|
// Log all Microsoft events with details
|
||||||
|
console.log(`[AGENDA] Microsoft calendar ${cal.id} events (${eventCount}):`, cal.events.map(e => ({
|
||||||
id: e.id,
|
id: e.id,
|
||||||
title: e.title,
|
title: e.title,
|
||||||
start: e.start,
|
start: e.start,
|
||||||
end: e.end
|
end: e.end,
|
||||||
|
isAllDay: e.isAllDay,
|
||||||
|
description: e.description ? e.description.substring(0, 50) : null
|
||||||
})));
|
})));
|
||||||
|
|
||||||
|
// Also check directly in DB to see if there are more events
|
||||||
|
prisma.event.count({
|
||||||
|
where: { calendarId: cal.id }
|
||||||
|
}).then((dbCount) => {
|
||||||
|
if (dbCount !== eventCount) {
|
||||||
|
console.log(`[AGENDA] WARNING: Calendar ${cal.id} has ${eventCount} events in query but ${dbCount} in DB`);
|
||||||
|
}
|
||||||
|
}).catch((err) => {
|
||||||
|
console.error('[AGENDA] Error counting events in DB:', err);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@ -138,20 +138,34 @@ export async function fetchMicrosoftEvents(
|
|||||||
};
|
};
|
||||||
|
|
||||||
// Add date filter if provided
|
// Add date filter if provided
|
||||||
// Note: Microsoft Graph API filter for events needs to handle both timed and all-day events
|
// Note: Microsoft Graph API filter syntax
|
||||||
// For all-day events, start/dateTime might not exist, so we use start/date instead
|
// For timed events: start/dateTime
|
||||||
|
// For all-day events: start/date
|
||||||
|
// We need to handle both cases
|
||||||
if (startDate && endDate) {
|
if (startDate && endDate) {
|
||||||
// Microsoft Graph API expects ISO format for date filters
|
// Format dates for Microsoft Graph API
|
||||||
// For all-day events, use start/date (YYYY-MM-DD format)
|
// For dateTime: ISO 8601 format (e.g., 2026-01-14T21:00:00Z)
|
||||||
// For timed events, use start/dateTime (ISO format)
|
// For date: YYYY-MM-DD format
|
||||||
const startDateStr = startDate.toISOString().split('T')[0]; // YYYY-MM-DD
|
const startDateStr = startDate.toISOString().split('T')[0];
|
||||||
const endDateStr = endDate.toISOString().split('T')[0]; // YYYY-MM-DD
|
const endDateStr = endDate.toISOString().split('T')[0];
|
||||||
const startDateTimeStr = startDate.toISOString();
|
const startDateTimeStr = startDate.toISOString();
|
||||||
const endDateTimeStr = endDate.toISOString();
|
const endDateTimeStr = endDate.toISOString();
|
||||||
|
|
||||||
// Filter: events where start is within range
|
// Microsoft Graph API filter: match events where start is within range
|
||||||
// Handle both timed events (start/dateTime) and all-day events (start/date)
|
// 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}')`;
|
params.$filter = `(start/dateTime ge '${startDateTimeStr}' or start/date ge '${startDateStr}') and (start/dateTime le '${endDateTimeStr}' or start/date le '${endDateStr}')`;
|
||||||
|
|
||||||
|
logger.debug('Microsoft Graph API filter', {
|
||||||
|
filter: params.$filter,
|
||||||
|
startDate: startDateStr,
|
||||||
|
endDate: endDateStr,
|
||||||
|
startDateTime: startDateTimeStr,
|
||||||
|
endDateTime: endDateTimeStr,
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
// If no date filter, get all events (might be too many, but useful for debugging)
|
||||||
|
logger.warn('No date filter provided, fetching all events (this might be slow)');
|
||||||
}
|
}
|
||||||
|
|
||||||
logger.debug('Fetching Microsoft events with params', {
|
logger.debug('Fetching Microsoft events with params', {
|
||||||
@ -178,12 +192,27 @@ export async function fetchMicrosoftEvents(
|
|||||||
});
|
});
|
||||||
|
|
||||||
const events = response.data.value || [];
|
const events = response.data.value || [];
|
||||||
logger.debug('Microsoft Graph API response', {
|
logger.info('Microsoft Graph API response', {
|
||||||
calendarId,
|
calendarId,
|
||||||
eventCount: events.length,
|
eventCount: events.length,
|
||||||
hasValue: !!response.data.value,
|
hasValue: !!response.data.value,
|
||||||
|
status: response.status,
|
||||||
|
// Log first few event IDs to verify they're being returned
|
||||||
|
eventIds: events.slice(0, 5).map(e => e.id),
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// Log if we got fewer events than expected
|
||||||
|
if (events.length === 0 && startDate && endDate) {
|
||||||
|
logger.warn('No events returned from Microsoft Graph API', {
|
||||||
|
calendarId,
|
||||||
|
dateRange: {
|
||||||
|
start: startDate.toISOString(),
|
||||||
|
end: endDate.toISOString(),
|
||||||
|
},
|
||||||
|
filter: params.$filter,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
return events;
|
return events;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
logger.error('Error fetching Microsoft events', {
|
logger.error('Error fetching Microsoft events', {
|
||||||
@ -309,6 +338,7 @@ export async function syncMicrosoftCalendar(
|
|||||||
endDate
|
endDate
|
||||||
);
|
);
|
||||||
|
|
||||||
|
// Log all events, not just first 10
|
||||||
logger.info('Fetched Microsoft events', {
|
logger.info('Fetched Microsoft events', {
|
||||||
calendarSyncId,
|
calendarSyncId,
|
||||||
eventCount: microsoftEvents.length,
|
eventCount: microsoftEvents.length,
|
||||||
@ -316,7 +346,7 @@ export async function syncMicrosoftCalendar(
|
|||||||
start: startDate.toISOString(),
|
start: startDate.toISOString(),
|
||||||
end: endDate.toISOString()
|
end: endDate.toISOString()
|
||||||
},
|
},
|
||||||
events: microsoftEvents.slice(0, 10).map(e => ({
|
allEvents: microsoftEvents.map(e => ({
|
||||||
id: e.id,
|
id: e.id,
|
||||||
subject: e.subject,
|
subject: e.subject,
|
||||||
start: e.start.dateTime || e.start.date,
|
start: e.start.dateTime || e.start.date,
|
||||||
@ -458,6 +488,11 @@ export async function syncMicrosoftCalendar(
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Verify events were actually saved to DB
|
||||||
|
const eventsInDb = await prisma.event.count({
|
||||||
|
where: { calendarId: syncConfig.calendarId }
|
||||||
|
});
|
||||||
|
|
||||||
logger.info('Microsoft calendar sync completed', {
|
logger.info('Microsoft calendar sync completed', {
|
||||||
calendarSyncId,
|
calendarSyncId,
|
||||||
calendarId: syncConfig.calendarId,
|
calendarId: syncConfig.calendarId,
|
||||||
@ -466,6 +501,7 @@ export async function syncMicrosoftCalendar(
|
|||||||
created,
|
created,
|
||||||
updated,
|
updated,
|
||||||
deleted,
|
deleted,
|
||||||
|
eventsInDb,
|
||||||
});
|
});
|
||||||
|
|
||||||
// Log summary of created/updated events
|
// Log summary of created/updated events
|
||||||
@ -475,6 +511,16 @@ export async function syncMicrosoftCalendar(
|
|||||||
newEventsCreated: created,
|
newEventsCreated: created,
|
||||||
eventsUpdated: updated,
|
eventsUpdated: updated,
|
||||||
totalEventsInCalendar: caldavEvents.length,
|
totalEventsInCalendar: caldavEvents.length,
|
||||||
|
eventsInDbAfterSync: eventsInDb,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// Log warning if events count doesn't match
|
||||||
|
if (caldavEvents.length > 0 && eventsInDb === 0) {
|
||||||
|
logger.error('WARNING: Events were fetched but none were saved to DB', {
|
||||||
|
calendarSyncId,
|
||||||
|
fetchedCount: caldavEvents.length,
|
||||||
|
dbCount: eventsInDb,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user