Agenda refactor
This commit is contained in:
parent
33b14a1e32
commit
2c68c00d9b
@ -138,13 +138,29 @@ 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
|
||||||
|
// For all-day events, start/dateTime might not exist, so we use start/date instead
|
||||||
if (startDate && endDate) {
|
if (startDate && endDate) {
|
||||||
// Microsoft Graph API expects ISO format for date filters
|
// Microsoft Graph API expects ISO format for date filters
|
||||||
// For all-day events, we need to handle date-only format
|
// For all-day events, use start/date (YYYY-MM-DD format)
|
||||||
const startFilter = startDate.toISOString();
|
// For timed events, use start/dateTime (ISO format)
|
||||||
const endFilter = endDate.toISOString();
|
const startDateStr = startDate.toISOString().split('T')[0]; // YYYY-MM-DD
|
||||||
params.$filter = `start/dateTime ge '${startFilter}' and start/dateTime le '${endFilter}'`;
|
const endDateStr = endDate.toISOString().split('T')[0]; // YYYY-MM-DD
|
||||||
|
const startDateTimeStr = startDate.toISOString();
|
||||||
|
const endDateTimeStr = endDate.toISOString();
|
||||||
|
|
||||||
|
// Filter: events where start is within range
|
||||||
|
// Handle both timed events (start/dateTime) and all-day events (start/date)
|
||||||
|
params.$filter = `(start/dateTime ge '${startDateTimeStr}' or start/date ge '${startDateStr}') and (start/dateTime le '${endDateTimeStr}' or start/date le '${endDateStr}')`;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
logger.debug('Fetching Microsoft events with params', {
|
||||||
|
email,
|
||||||
|
calendarId,
|
||||||
|
startDate: startDate?.toISOString(),
|
||||||
|
endDate: endDate?.toISOString(),
|
||||||
|
filter: params.$filter,
|
||||||
|
});
|
||||||
|
|
||||||
// Get events from Microsoft Graph API
|
// Get events from Microsoft Graph API
|
||||||
const response = await axios.get(
|
const response = await axios.get(
|
||||||
@ -283,7 +299,13 @@ export async function syncMicrosoftCalendar(
|
|||||||
logger.info('Fetched Microsoft events', {
|
logger.info('Fetched Microsoft events', {
|
||||||
calendarSyncId,
|
calendarSyncId,
|
||||||
eventCount: microsoftEvents.length,
|
eventCount: microsoftEvents.length,
|
||||||
events: microsoftEvents.slice(0, 5).map(e => ({ id: e.id, subject: e.subject, start: e.start.dateTime })),
|
events: microsoftEvents.slice(0, 10).map(e => ({
|
||||||
|
id: e.id,
|
||||||
|
subject: e.subject,
|
||||||
|
start: e.start.dateTime || e.start.date,
|
||||||
|
isAllDay: e.isAllDay,
|
||||||
|
end: e.end.dateTime || e.end.date
|
||||||
|
})),
|
||||||
});
|
});
|
||||||
|
|
||||||
// Convert Microsoft events to CalDAV-like format
|
// Convert Microsoft events to CalDAV-like format
|
||||||
@ -349,12 +371,23 @@ export async function syncMicrosoftCalendar(
|
|||||||
data: eventData,
|
data: eventData,
|
||||||
});
|
});
|
||||||
updated++;
|
updated++;
|
||||||
|
logger.debug('Updated event', {
|
||||||
|
eventId: existingEvent.id,
|
||||||
|
title: caldavEvent.summary,
|
||||||
|
microsoftId,
|
||||||
|
});
|
||||||
} else {
|
} else {
|
||||||
// Create new event
|
// Create new event
|
||||||
await prisma.event.create({
|
const newEvent = await prisma.event.create({
|
||||||
data: eventData,
|
data: eventData,
|
||||||
});
|
});
|
||||||
created++;
|
created++;
|
||||||
|
logger.debug('Created new event', {
|
||||||
|
eventId: newEvent.id,
|
||||||
|
title: caldavEvent.summary,
|
||||||
|
microsoftId,
|
||||||
|
start: caldavEvent.start.toISOString(),
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -367,6 +400,22 @@ export async function syncMicrosoftCalendar(
|
|||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// Invalidate cache for this user's calendars so new events appear immediately
|
||||||
|
try {
|
||||||
|
const { invalidateCalendarCache } = await import('@/lib/redis');
|
||||||
|
await invalidateCalendarCache(syncConfig.calendar.userId);
|
||||||
|
logger.info('Invalidated calendar cache after sync', {
|
||||||
|
userId: syncConfig.calendar.userId,
|
||||||
|
calendarId: syncConfig.calendarId,
|
||||||
|
});
|
||||||
|
} catch (cacheError) {
|
||||||
|
// Don't fail sync if cache invalidation fails
|
||||||
|
logger.warn('Failed to invalidate calendar cache', {
|
||||||
|
userId: syncConfig.calendar.userId,
|
||||||
|
error: cacheError instanceof Error ? cacheError.message : String(cacheError),
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
logger.info('Microsoft calendar sync completed', {
|
logger.info('Microsoft calendar sync completed', {
|
||||||
calendarSyncId,
|
calendarSyncId,
|
||||||
calendarId: syncConfig.calendarId,
|
calendarId: syncConfig.calendarId,
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user