Agenda refactor

This commit is contained in:
alma 2026-01-14 17:10:08 +01:00
parent 340e346071
commit 39fd43f995

View File

@ -161,6 +161,8 @@ export default async function CalendarPage() {
});
} else {
// Try to discover calendars to verify if credentials are now valid
// But if discovery fails and we have an existing URL, re-enable sync anyway
// The existing URL might still work even if discovery fails
try {
const { discoverInfomaniakCalendars } = await import('@/lib/services/caldav-sync');
const externalCalendars = await discoverInfomaniakCalendars(
@ -169,7 +171,7 @@ export default async function CalendarPage() {
);
if (externalCalendars.length > 0) {
// Credentials are now valid, re-enable sync
// Credentials are now valid, re-enable sync with discovered calendar
await prisma.calendarSync.update({
where: { id: existingSync.id },
data: {
@ -179,16 +181,40 @@ export default async function CalendarPage() {
externalCalendarUrl: externalCalendars[0].url
}
});
} else if (existingSync.externalCalendarUrl) {
// Discovery succeeded but no calendars found, but we have an existing URL
// Re-enable sync with existing URL - it might still work
await prisma.calendarSync.update({
where: { id: existingSync.id },
data: {
syncEnabled: true,
lastSyncError: 'Aucun calendrier trouvé lors de la découverte, utilisation de l\'URL existante'
}
});
}
} catch (error) {
// Credentials are still invalid, keep sync disabled
// Discovery failed, but if we have an existing URL, re-enable sync anyway
// The existing URL might still work even if discovery fails (e.g., due to network issues)
const errorMessage = error instanceof Error ? error.message : 'Unknown error';
await prisma.calendarSync.update({
where: { id: existingSync.id },
data: {
lastSyncError: `Identifiants invalides ou expirés (${errorMessage}). Veuillez vérifier vos identifiants Infomaniak dans la page courrier.`
}
});
if (existingSync.externalCalendarUrl) {
// We have an existing URL, re-enable sync - it worked before, might still work
await prisma.calendarSync.update({
where: { id: existingSync.id },
data: {
syncEnabled: true,
lastSyncError: `Découverte CalDAV échouée (${errorMessage}), mais réactivation du sync avec l'URL existante`
}
});
} else {
// No existing URL, keep sync disabled
await prisma.calendarSync.update({
where: { id: existingSync.id },
data: {
lastSyncError: `Identifiants invalides ou expirés (${errorMessage}). Veuillez vérifier vos identifiants Infomaniak dans la page courrier.`
}
});
}
}
}
continue; // Skip to next account
@ -488,9 +514,9 @@ export default async function CalendarPage() {
// Log for debugging if Infomaniak calendar is missing
if (isPrivateOrDefault && cal.syncConfig?.provider === 'infomaniak') {
if (!hasActiveSync) {
console.log(`Infomaniak calendar found but sync is disabled: ${cal.id}, syncEnabled: ${cal.syncConfig?.syncEnabled}, error: ${cal.syncConfig?.lastSyncError || 'none'}`);
console.log(`[AGENDA] Infomaniak calendar found but sync is disabled: ${cal.id}, syncEnabled: ${cal.syncConfig?.syncEnabled}, hasMailCredential: ${!!cal.syncConfig?.mailCredential}, error: ${cal.syncConfig?.lastSyncError || 'none'}`);
} else {
console.log(`Infomaniak calendar is active: ${cal.id}, email: ${cal.syncConfig?.mailCredential?.email}`);
console.log(`[AGENDA] Infomaniak calendar is active and will be displayed: ${cal.id}, email: ${cal.syncConfig?.mailCredential?.email}`);
}
}
@ -499,6 +525,10 @@ export default async function CalendarPage() {
}
return true;
});
// Log final count of Infomaniak calendars
const infomaniakCalendars = calendars.filter(cal => cal.syncConfig?.provider === 'infomaniak');
console.log(`[AGENDA] Final Infomaniak calendars count: ${infomaniakCalendars.length}`);
const now = new Date();
const nextWeek = add(now, { days: 7 });