Agenda refactor
This commit is contained in:
parent
c0d848395d
commit
533c42bff4
@ -352,37 +352,74 @@ export default async function CalendarPage() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// For each account, keep only the most recent enabled sync, disable or delete others
|
// For each account, keep only the most recent enabled sync, disable or delete others
|
||||||
|
// IMPORTANT: Only process if there are actually multiple ENABLED syncs to avoid disabling active syncs
|
||||||
for (const [key, syncs] of syncsByAccount.entries()) {
|
for (const [key, syncs] of syncsByAccount.entries()) {
|
||||||
if (syncs.length > 1) {
|
if (syncs.length > 1) {
|
||||||
// Sort by syncEnabled first (enabled first), then by createdAt (newest first)
|
// Count how many are actually enabled
|
||||||
syncs.sort((a, b) => {
|
const enabledSyncs = syncs.filter(s => s.syncEnabled === true);
|
||||||
if (a.syncEnabled !== b.syncEnabled) {
|
|
||||||
return a.syncEnabled ? -1 : 1;
|
|
||||||
}
|
|
||||||
return new Date(b.createdAt).getTime() - new Date(a.createdAt).getTime();
|
|
||||||
});
|
|
||||||
|
|
||||||
const keepSync = syncs[0];
|
// Only clean up if there are multiple ENABLED syncs
|
||||||
const duplicates = syncs.slice(1);
|
// If there's only one enabled sync, don't touch it even if there are disabled duplicates
|
||||||
|
if (enabledSyncs.length > 1) {
|
||||||
|
console.log(`[AGENDA] Found ${enabledSyncs.length} enabled syncs for ${key}, cleaning up duplicates`);
|
||||||
|
|
||||||
// Disable or delete duplicate syncs
|
// Sort by syncEnabled first (enabled first), then by lastSyncAt (most recently synced first), then by createdAt (newest first)
|
||||||
for (const duplicate of duplicates) {
|
syncs.sort((a, b) => {
|
||||||
if (duplicate.syncEnabled) {
|
if (a.syncEnabled !== b.syncEnabled) {
|
||||||
// Disable the duplicate sync
|
return a.syncEnabled ? -1 : 1;
|
||||||
await prisma.calendarSync.update({
|
}
|
||||||
where: { id: duplicate.id },
|
// Among enabled syncs, prefer the one with most recent sync
|
||||||
data: { syncEnabled: false }
|
if (a.syncEnabled && b.syncEnabled) {
|
||||||
});
|
if (a.lastSyncAt && b.lastSyncAt) {
|
||||||
}
|
return new Date(b.lastSyncAt).getTime() - new Date(a.lastSyncAt).getTime();
|
||||||
// Delete the calendar if it has no events
|
}
|
||||||
const eventCount = await prisma.event.count({
|
if (a.lastSyncAt && !b.lastSyncAt) return -1;
|
||||||
where: { calendarId: duplicate.calendarId }
|
if (!a.lastSyncAt && b.lastSyncAt) return 1;
|
||||||
|
}
|
||||||
|
return new Date(b.createdAt).getTime() - new Date(a.createdAt).getTime();
|
||||||
});
|
});
|
||||||
if (eventCount === 0) {
|
|
||||||
await prisma.calendar.delete({
|
const keepSync = syncs[0];
|
||||||
where: { id: duplicate.calendarId }
|
const duplicates = syncs.slice(1);
|
||||||
});
|
|
||||||
|
// Additional safety check: only disable duplicates if the sync to keep is actually valid
|
||||||
|
// (has events or has been synced recently)
|
||||||
|
const keepSyncEventCount = await prisma.event.count({
|
||||||
|
where: { calendarId: keepSync.calendarId }
|
||||||
|
});
|
||||||
|
const keepSyncIsValid = keepSyncEventCount > 0 || keepSync.lastSyncAt;
|
||||||
|
|
||||||
|
if (!keepSyncIsValid) {
|
||||||
|
console.log(`[AGENDA] WARNING: Sync to keep ${keepSync.id} has no events and no sync history - skipping cleanup to avoid disabling valid syncs`);
|
||||||
|
continue; // Skip this group to avoid disabling a potentially valid sync
|
||||||
}
|
}
|
||||||
|
|
||||||
|
console.log(`[AGENDA] Keeping sync ${keepSync.id} (calendar: ${keepSync.calendar.name}, events: ${keepSyncEventCount}), disabling ${duplicates.length} duplicates`);
|
||||||
|
|
||||||
|
// Disable or delete duplicate syncs
|
||||||
|
for (const duplicate of duplicates) {
|
||||||
|
if (duplicate.syncEnabled) {
|
||||||
|
// Disable the duplicate sync
|
||||||
|
console.log(`[AGENDA] Disabling duplicate sync ${duplicate.id} (calendar: ${duplicate.calendar.name})`);
|
||||||
|
await prisma.calendarSync.update({
|
||||||
|
where: { id: duplicate.id },
|
||||||
|
data: { syncEnabled: false }
|
||||||
|
});
|
||||||
|
}
|
||||||
|
// Delete the calendar if it has no events
|
||||||
|
const eventCount = await prisma.event.count({
|
||||||
|
where: { calendarId: duplicate.calendarId }
|
||||||
|
});
|
||||||
|
if (eventCount === 0) {
|
||||||
|
console.log(`[AGENDA] Deleting empty calendar ${duplicate.calendar.name} (${duplicate.calendarId})`);
|
||||||
|
await prisma.calendar.delete({
|
||||||
|
where: { id: duplicate.calendarId }
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else if (enabledSyncs.length === 1 && syncs.length > 1) {
|
||||||
|
// There's only one enabled sync but multiple disabled ones - this is fine, don't touch anything
|
||||||
|
console.log(`[AGENDA] Found 1 enabled sync and ${syncs.length - 1} disabled syncs for ${key} - no cleanup needed`);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user