Agenda refactor
This commit is contained in:
parent
71e97b3d72
commit
f9ecffd78c
@ -741,13 +741,88 @@ export async function syncMicrosoftCalendar(
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// If no events found, try to discover which calendar actually has events
|
||||||
if (microsoftEvents.length === 0) {
|
if (microsoftEvents.length === 0) {
|
||||||
logger.warn('No Microsoft events found in date range', {
|
logger.warn('No Microsoft events found in specified calendar, discovering available calendars', {
|
||||||
calendarSyncId,
|
calendarSyncId,
|
||||||
email: creds.email,
|
email: creds.email,
|
||||||
externalCalendarId: syncConfig.externalCalendarId,
|
externalCalendarId: calendarIdToUse,
|
||||||
dateRange: { start: startDate.toISOString(), end: endDate.toISOString() },
|
dateRange: { start: startDate.toISOString(), end: endDate.toISOString() },
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// Discover all available calendars
|
||||||
|
const availableCalendars = await discoverMicrosoftCalendars(
|
||||||
|
syncConfig.calendar.userId,
|
||||||
|
creds.email
|
||||||
|
);
|
||||||
|
|
||||||
|
logger.info('Discovered Microsoft calendars', {
|
||||||
|
calendarSyncId,
|
||||||
|
calendarsCount: availableCalendars.length,
|
||||||
|
calendarNames: availableCalendars.map(cal => cal.name),
|
||||||
|
});
|
||||||
|
|
||||||
|
// Try to find a calendar with events
|
||||||
|
let calendarWithEvents = null;
|
||||||
|
for (const cal of availableCalendars) {
|
||||||
|
try {
|
||||||
|
const testEvents = await fetchMicrosoftEvents(
|
||||||
|
syncConfig.calendar.userId,
|
||||||
|
creds.email,
|
||||||
|
cal.id,
|
||||||
|
startDate,
|
||||||
|
endDate
|
||||||
|
);
|
||||||
|
|
||||||
|
if (testEvents.length > 0) {
|
||||||
|
calendarWithEvents = { calendar: cal, events: testEvents };
|
||||||
|
logger.info('Found calendar with events', {
|
||||||
|
calendarSyncId,
|
||||||
|
calendarId: cal.id,
|
||||||
|
calendarName: cal.name,
|
||||||
|
eventCount: testEvents.length,
|
||||||
|
});
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
logger.debug('Error checking calendar for events', {
|
||||||
|
calendarId: cal.id,
|
||||||
|
calendarName: cal.name,
|
||||||
|
error: error instanceof Error ? error.message : String(error),
|
||||||
|
});
|
||||||
|
// Continue to next calendar
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// If we found a calendar with events, update the sync config
|
||||||
|
if (calendarWithEvents) {
|
||||||
|
const newCalendarId = calendarWithEvents.calendar.id;
|
||||||
|
logger.info('Updating sync config to use calendar with events', {
|
||||||
|
calendarSyncId,
|
||||||
|
oldCalendarId: calendarIdToUse,
|
||||||
|
newCalendarId: newCalendarId,
|
||||||
|
newCalendarName: calendarWithEvents.calendar.name,
|
||||||
|
eventCount: calendarWithEvents.events.length,
|
||||||
|
});
|
||||||
|
|
||||||
|
await prisma.calendarSync.update({
|
||||||
|
where: { id: calendarSyncId },
|
||||||
|
data: {
|
||||||
|
externalCalendarId: newCalendarId,
|
||||||
|
lastSyncError: null,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
// Use the events from the calendar we found
|
||||||
|
microsoftEvents = calendarWithEvents.events;
|
||||||
|
} else {
|
||||||
|
logger.warn('No Microsoft events found in any calendar', {
|
||||||
|
calendarSyncId,
|
||||||
|
email: creds.email,
|
||||||
|
calendarsChecked: availableCalendars.length,
|
||||||
|
dateRange: { start: startDate.toISOString(), end: endDate.toISOString() },
|
||||||
|
});
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
// Log events in the future to help debug
|
// Log events in the future to help debug
|
||||||
const now = new Date();
|
const now = new Date();
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user