Agenda refactor
This commit is contained in:
parent
ac4617a874
commit
c549e34c8e
@ -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
|
// Clean up duplicate calendars for the same mailCredentialId
|
||||||
// Keep only the most recent one with syncEnabled=true, delete others
|
// Keep only the most recent one with syncEnabled=true, delete others
|
||||||
const allSyncs = await prisma.calendarSync.findMany({
|
const allSyncs = await prisma.calendarSync.findMany({
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user