Agenda refactor
This commit is contained in:
parent
d3b63edefe
commit
c0d848395d
@ -242,11 +242,56 @@ export async function DELETE(request: Request) {
|
||||
if (!account) {
|
||||
return NextResponse.json({ error: 'Account not found' }, { status: 404 });
|
||||
}
|
||||
|
||||
// Find all calendar sync configs linked to this mail credential
|
||||
const syncConfigs = await prisma.calendarSync.findMany({
|
||||
where: {
|
||||
mailCredentialId: accountId,
|
||||
},
|
||||
include: {
|
||||
calendar: true,
|
||||
},
|
||||
});
|
||||
|
||||
// Delete calendars and sync configs associated with this account
|
||||
// This prevents orphaned calendars when a mail account is deleted
|
||||
for (const syncConfig of syncConfigs) {
|
||||
console.log(`[COURRIER] Deleting calendar ${syncConfig.calendar.name} (${syncConfig.calendar.id}) associated with deleted account ${account.email}`);
|
||||
|
||||
// Delete the calendar (events will be cascade deleted)
|
||||
await prisma.calendar.delete({
|
||||
where: { id: syncConfig.calendarId },
|
||||
});
|
||||
|
||||
// Sync config is already deleted by cascade, but we can also delete it explicitly
|
||||
try {
|
||||
await prisma.calendarSync.delete({
|
||||
where: { id: syncConfig.id },
|
||||
});
|
||||
} catch (error) {
|
||||
// Ignore if already deleted by cascade
|
||||
}
|
||||
}
|
||||
|
||||
// Delete from database
|
||||
await prisma.mailCredentials.delete({ where: { id: accountId } });
|
||||
|
||||
// Invalidate cache
|
||||
await invalidateFolderCache(session.user.id, account.email, '*');
|
||||
return NextResponse.json({ success: true, message: 'Account deleted' });
|
||||
|
||||
// Invalidate calendar cache for this user
|
||||
try {
|
||||
const { invalidateCalendarCache } = await import('@/lib/redis');
|
||||
await invalidateCalendarCache(session.user.id);
|
||||
} catch (error) {
|
||||
console.error('Error invalidating calendar cache:', error);
|
||||
}
|
||||
|
||||
return NextResponse.json({
|
||||
success: true,
|
||||
message: 'Account deleted',
|
||||
deletedCalendars: syncConfigs.length,
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('Error deleting account:', error);
|
||||
return NextResponse.json({ error: 'Failed to delete account', details: error instanceof Error ? error.message : 'Unknown error' }, { status: 500 });
|
||||
|
||||
Loading…
Reference in New Issue
Block a user