From b5c15964e7828af0d45e2c58d6ee9f76a341bd97 Mon Sep 17 00:00:00 2001 From: alma Date: Thu, 15 Jan 2026 16:49:29 +0100 Subject: [PATCH] Agenda refactor --- lib/services/microsoft-calendar-sync.ts | 29 +++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/lib/services/microsoft-calendar-sync.ts b/lib/services/microsoft-calendar-sync.ts index a49f543..e4e4ee4 100644 --- a/lib/services/microsoft-calendar-sync.ts +++ b/lib/services/microsoft-calendar-sync.ts @@ -1082,6 +1082,35 @@ export async function syncMicrosoftCalendar( } } + // Delete events that are no longer in Microsoft calendar + // Build a set of all Microsoft event IDs we just synced + const syncedMicrosoftIds = new Set(); + for (const caldavEvent of caldavEvents) { + if (caldavEvent.uid) { + syncedMicrosoftIds.add(caldavEvent.uid); + } + } + + // Find events in DB that have externalEventId but are not in the synced list + // Only delete events that have an externalEventId (to avoid deleting manually created events) + for (const existingEvent of existingEvents) { + const externalId = (existingEvent as any).externalEventId; + if (externalId && !syncedMicrosoftIds.has(externalId)) { + // This event exists in DB but not in Microsoft - it was deleted in Microsoft + logger.info('Deleting event that no longer exists in Microsoft', { + eventId: existingEvent.id, + title: existingEvent.title, + externalEventId: externalId, + }); + + await prisma.event.delete({ + where: { id: existingEvent.id }, + }); + + deleted++; + } + } + // Update sync timestamp await prisma.calendarSync.update({ where: { id: calendarSyncId },