Agenda refactor

This commit is contained in:
alma 2026-01-14 17:26:05 +01:00
parent 5a4e746bf4
commit ac4617a874

View File

@ -232,60 +232,61 @@ export default async function CalendarPage() {
data: { syncEnabled: true } data: { syncEnabled: true }
}); });
} else { } else {
// Try to discover calendars to verify if credentials are now valid // 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 // But if discovery fails and we have an existing URL, re-enable sync anyway
// The existing URL might still work even if discovery fails // The existing URL might still work even if discovery fails
try { try {
const { discoverInfomaniakCalendars } = await import('@/lib/services/caldav-sync'); const { discoverInfomaniakCalendars } = await import('@/lib/services/caldav-sync');
const externalCalendars = await discoverInfomaniakCalendars( const externalCalendars = await discoverInfomaniakCalendars(
account.email, account.email,
account.password! account.password!
); );
if (externalCalendars.length > 0) { if (externalCalendars.length > 0) {
// Credentials are now valid, re-enable sync with discovered calendar // Credentials are now valid, re-enable sync with discovered calendar
await prisma.calendarSync.update({ await prisma.calendarSync.update({
where: { id: existingSync.id }, where: { id: existingSync.id },
data: { data: {
syncEnabled: true, syncEnabled: true,
lastSyncError: null, lastSyncError: null,
externalCalendarId: externalCalendars[0].id, externalCalendarId: externalCalendars[0].id,
externalCalendarUrl: externalCalendars[0].url externalCalendarUrl: externalCalendars[0].url
} }
}); });
} else if (existingSync.externalCalendarUrl) { } else if (existingSync.externalCalendarUrl) {
// Discovery succeeded but no calendars found, but we have an existing URL // Discovery succeeded but no calendars found, but we have an existing URL
// Re-enable sync with existing URL - it might still work // Re-enable sync with existing URL - it might still work
await prisma.calendarSync.update({ await prisma.calendarSync.update({
where: { id: existingSync.id }, where: { id: existingSync.id },
data: { data: {
syncEnabled: true, syncEnabled: true,
lastSyncError: 'Aucun calendrier trouvé lors de la découverte, utilisation de l\'URL existante' lastSyncError: 'Aucun calendrier trouvé lors de la découverte, utilisation de l\'URL existante'
} }
}); });
} }
} catch (error) { } catch (error) {
// Discovery failed, but if we have an existing URL, re-enable sync anyway // 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) // 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'; const errorMessage = error instanceof Error ? error.message : 'Unknown error';
if (existingSync.externalCalendarUrl) { if (existingSync.externalCalendarUrl) {
// We have an existing URL, re-enable sync - it worked before, might still work // We have an existing URL, re-enable sync - it worked before, might still work
await prisma.calendarSync.update({ await prisma.calendarSync.update({
where: { id: existingSync.id }, where: { id: existingSync.id },
data: { data: {
syncEnabled: true, syncEnabled: true,
lastSyncError: `Découverte CalDAV échouée (${errorMessage}), mais réactivation du sync avec l'URL existante` lastSyncError: `Découverte CalDAV échouée (${errorMessage}), mais réactivation du sync avec l'URL existante`
} }
}); });
} else { } else {
// No existing URL, keep sync disabled // No existing URL, keep sync disabled
await prisma.calendarSync.update({ await prisma.calendarSync.update({
where: { id: existingSync.id }, where: { id: existingSync.id },
data: { data: {
lastSyncError: `Identifiants invalides ou expirés (${errorMessage}). Veuillez vérifier vos identifiants Infomaniak dans la page courrier.` lastSyncError: `Identifiants invalides ou expirés (${errorMessage}). Veuillez vérifier vos identifiants Infomaniak dans la page courrier.`
} }
}); });
}
} }
} }
} }