Agenda refactor
This commit is contained in:
parent
cb967a0ca6
commit
ace4e5ca3a
@ -146,12 +146,51 @@ export default async function CalendarPage() {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
// If sync exists but is disabled, re-enable it instead of creating a new calendar
|
// If sync exists but is disabled, check if it's due to invalid credentials
|
||||||
|
// Don't re-enable if the last error was 401 (invalid credentials)
|
||||||
if (existingSync && !existingSync.syncEnabled) {
|
if (existingSync && !existingSync.syncEnabled) {
|
||||||
await prisma.calendarSync.update({
|
const isAuthError = existingSync.lastSyncError?.includes('401') ||
|
||||||
where: { id: existingSync.id },
|
existingSync.lastSyncError?.includes('Unauthorized') ||
|
||||||
data: { syncEnabled: true }
|
existingSync.lastSyncError?.includes('invalid');
|
||||||
});
|
|
||||||
|
if (!isAuthError) {
|
||||||
|
// Only re-enable if it's not an authentication error
|
||||||
|
await prisma.calendarSync.update({
|
||||||
|
where: { id: existingSync.id },
|
||||||
|
data: { syncEnabled: true }
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
// Try to discover calendars to verify if credentials are now valid
|
||||||
|
try {
|
||||||
|
const { discoverInfomaniakCalendars } = await import('@/lib/services/caldav-sync');
|
||||||
|
const externalCalendars = await discoverInfomaniakCalendars(
|
||||||
|
account.email,
|
||||||
|
account.password!
|
||||||
|
);
|
||||||
|
|
||||||
|
if (externalCalendars.length > 0) {
|
||||||
|
// Credentials are now valid, re-enable sync
|
||||||
|
await prisma.calendarSync.update({
|
||||||
|
where: { id: existingSync.id },
|
||||||
|
data: {
|
||||||
|
syncEnabled: true,
|
||||||
|
lastSyncError: null,
|
||||||
|
externalCalendarId: externalCalendars[0].id,
|
||||||
|
externalCalendarUrl: externalCalendars[0].url
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
// Credentials are still invalid, keep sync disabled
|
||||||
|
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.`
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
continue; // Skip to next account
|
continue; // Skip to next account
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -211,7 +250,30 @@ export default async function CalendarPage() {
|
|||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
// Log error but don't fail the page - account may not have calendar access or credentials may be invalid
|
// Log error but don't fail the page - account may not have calendar access or credentials may be invalid
|
||||||
console.log(`Infomaniak calendar sync not available for ${account.email} - ${error instanceof Error ? error.message : 'Unknown error'}`);
|
const errorMessage = error instanceof Error ? error.message : 'Unknown error';
|
||||||
|
console.log(`Infomaniak calendar sync not available for ${account.email} - ${errorMessage}`);
|
||||||
|
|
||||||
|
// If it's a 401 error, the credentials are likely invalid - update lastSyncError in existing sync if any
|
||||||
|
if (errorMessage.includes('401') || errorMessage.includes('Unauthorized')) {
|
||||||
|
// Check if there's a disabled sync for this account
|
||||||
|
const disabledSync = await prisma.calendarSync.findFirst({
|
||||||
|
where: {
|
||||||
|
mailCredentialId: account.id,
|
||||||
|
provider: 'infomaniak',
|
||||||
|
syncEnabled: false
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
if (disabledSync) {
|
||||||
|
// Update the error message
|
||||||
|
await prisma.calendarSync.update({
|
||||||
|
where: { id: disabledSync.id },
|
||||||
|
data: {
|
||||||
|
lastSyncError: `Identifiants invalides ou expirés (401 Unauthorized). Veuillez vérifier vos identifiants Infomaniak dans la page courrier.`
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
// Continue with other accounts even if one fails
|
// Continue with other accounts even if one fails
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -423,9 +485,13 @@ export default async function CalendarPage() {
|
|||||||
const hasActiveSync = cal.syncConfig?.syncEnabled === true && cal.syncConfig?.mailCredential;
|
const hasActiveSync = cal.syncConfig?.syncEnabled === true && cal.syncConfig?.mailCredential;
|
||||||
|
|
||||||
// Exclude "Privée"/"Default" calendars that are not actively synced
|
// Exclude "Privée"/"Default" calendars that are not actively synced
|
||||||
// Also log for debugging if Infomaniak calendar is missing
|
// Log for debugging if Infomaniak calendar is missing
|
||||||
if (isPrivateOrDefault && cal.syncConfig?.provider === 'infomaniak' && !hasActiveSync) {
|
if (isPrivateOrDefault && cal.syncConfig?.provider === 'infomaniak') {
|
||||||
console.log(`Infomaniak calendar found but sync is disabled: ${cal.id}, syncEnabled: ${cal.syncConfig?.syncEnabled}`);
|
if (!hasActiveSync) {
|
||||||
|
console.log(`Infomaniak calendar found but sync is disabled: ${cal.id}, syncEnabled: ${cal.syncConfig?.syncEnabled}, error: ${cal.syncConfig?.lastSyncError || 'none'}`);
|
||||||
|
} else {
|
||||||
|
console.log(`Infomaniak calendar is active: ${cal.id}, email: ${cal.syncConfig?.mailCredential?.email}`);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (isPrivateOrDefault && !hasActiveSync) {
|
if (isPrivateOrDefault && !hasActiveSync) {
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user