diff --git a/app/agenda/page.tsx b/app/agenda/page.tsx index 54c4c15..4f757c8 100644 --- a/app/agenda/page.tsx +++ b/app/agenda/page.tsx @@ -448,50 +448,6 @@ export default async function CalendarPage() { } } - // Clean up orphaned syncs (syncs with mailCredentialId that no longer exists) - // This can happen when a user deletes and re-adds an email account - const allMailCredentialIds = new Set([ - ...infomaniakAccounts.map(acc => acc.id), - ...microsoftAccounts.map(acc => acc.id) - ]); - - const orphanedSyncs = await prisma.calendarSync.findMany({ - where: { - calendar: { - userId: session?.user?.id || '' - }, - mailCredentialId: { - not: null - } - }, - include: { - calendar: true, - mailCredential: true - } - }); - - // Delete syncs where mailCredential no longer exists - for (const sync of orphanedSyncs) { - if (sync.mailCredentialId && !allMailCredentialIds.has(sync.mailCredentialId)) { - console.log(`[AGENDA] Deleting orphaned sync for non-existent mailCredentialId: ${sync.mailCredentialId}`); - // Delete the calendar if it has no events - const eventCount = await prisma.event.count({ - where: { calendarId: sync.calendarId } - }); - if (eventCount === 0) { - await prisma.calendar.delete({ - where: { id: sync.calendarId } - }); - } else { - // Just disable the sync, keep the calendar - await prisma.calendarSync.update({ - where: { id: sync.id }, - data: { syncEnabled: false } - }); - } - } - } - // Clean up duplicate calendars for the same mailCredentialId // Keep only the most recent one with syncEnabled=true, delete others const allSyncs = await prisma.calendarSync.findMany({