From 6e3e6966d4dd0b767940bf9b2585e3969438c849 Mon Sep 17 00:00:00 2001 From: alma Date: Thu, 15 Jan 2026 17:41:10 +0100 Subject: [PATCH] Agenda refactor --- app/api/events/[id]/route.ts | 15 ++++++++++++ app/api/events/route.ts | 31 +++++++++++++++++++++++++ components/calendar/calendar-client.tsx | 25 +++++--------------- 3 files changed, 52 insertions(+), 19 deletions(-) diff --git a/app/api/events/[id]/route.ts b/app/api/events/[id]/route.ts index 21a8e54..2584263 100644 --- a/app/api/events/[id]/route.ts +++ b/app/api/events/[id]/route.ts @@ -128,6 +128,21 @@ export async function DELETE(req: NextRequest, props: { params: Promise<{ id: st where: { id: params.id }, }); + // Invalidate calendar cache so the deletion appears immediately + try { + const { invalidateCalendarCache } = await import('@/lib/redis'); + await invalidateCalendarCache(session.user.id); + logger.debug('[EVENTS] Invalidated calendar cache after event deletion', { + eventId: params.id, + userId: session.user.id, + }); + } catch (cacheError) { + // Log but don't fail the request if cache invalidation fails + logger.error('[EVENTS] Error invalidating calendar cache', { + error: cacheError instanceof Error ? cacheError.message : String(cacheError), + }); + } + return new NextResponse(null, { status: 204 }); } catch (error) { console.error("Erreur lors de la suppression de l'événement:", error); diff --git a/app/api/events/route.ts b/app/api/events/route.ts index ed49874..c028c30 100644 --- a/app/api/events/route.ts +++ b/app/api/events/route.ts @@ -4,6 +4,7 @@ import { authOptions } from "@/app/api/auth/options"; import { prisma } from "@/lib/prisma"; import { updateMicrosoftEvent, deleteMicrosoftEvent } from "@/lib/services/microsoft-calendar-sync"; import { logger } from "@/lib/logger"; +import { invalidateCalendarCache } from "@/lib/redis"; // Helper function to check if user can manage events in a mission calendar async function canManageMissionCalendar(userId: string, calendarId: string): Promise { @@ -96,6 +97,21 @@ export async function POST(req: NextRequest) { }, }); + // Invalidate calendar cache so the new event appears immediately + try { + await invalidateCalendarCache(session.user.id); + logger.debug('[EVENTS] Invalidated calendar cache after event creation', { + eventId: event.id, + calendarId, + userId: session.user.id, + }); + } catch (cacheError) { + // Log but don't fail the request if cache invalidation fails + logger.error('[EVENTS] Error invalidating calendar cache', { + error: cacheError instanceof Error ? cacheError.message : String(cacheError), + }); + } + console.log("Created event:", event); return NextResponse.json(event, { status: 201 }); } catch (error) { @@ -192,6 +208,21 @@ export async function PUT(req: NextRequest) { }, }); + // Invalidate calendar cache so the updated event appears immediately + try { + await invalidateCalendarCache(session.user.id); + logger.debug('[EVENTS] Invalidated calendar cache after event update', { + eventId: event.id, + calendarId, + userId: session.user.id, + }); + } catch (cacheError) { + // Log but don't fail the request if cache invalidation fails + logger.error('[EVENTS] Error invalidating calendar cache', { + error: cacheError instanceof Error ? cacheError.message : String(cacheError), + }); + } + // If event has externalEventId and calendar has Microsoft sync, update Microsoft too if (existingEvent.externalEventId && calendar.syncConfig && calendar.syncConfig.provider === 'microsoft' && calendar.syncConfig.syncEnabled) { const syncConfig = calendar.syncConfig; diff --git a/components/calendar/calendar-client.tsx b/components/calendar/calendar-client.tsx index ca28304..e303007 100644 --- a/components/calendar/calendar-client.tsx +++ b/components/calendar/calendar-client.tsx @@ -831,10 +831,11 @@ export function CalendarClient({ initialCalendars, userId, userProfile }: Calend setUpcomingEvents(upcoming); }; - const fetchCalendars = async () => { + const fetchCalendars = async (forceRefresh: boolean = false) => { try { setLoading(true); - const response = await fetch("/api/calendars"); + const url = forceRefresh ? "/api/calendars?refresh=true" : "/api/calendars"; + const response = await fetch(url); if (!response.ok) throw new Error("Failed to fetch calendars"); const data = await response.json(); console.log("Raw calendars data:", data); @@ -1052,23 +1053,9 @@ export function CalendarClient({ initialCalendars, userId, userProfile }: Calend setSelectedEvent(null); setError(null); - // Update calendars state with the new event - const updatedCalendars = calendars.map(cal => { - if (cal.id === eventData.calendarId) { - return { - ...cal, - events: [...cal.events, responseData] - }; - } - return cal; - }); - setCalendars(sortCalendars(updatedCalendars as typeof initialCalendars).map(cal => ({ - ...cal, - events: cal.events || [] - }))); - - // Fetch fresh data to ensure all calendars are up to date - await fetchCalendars(); + // Invalidate cache and fetch fresh data to ensure all calendars are up to date + // Force refresh by adding ?refresh=true to bypass cache + await fetchCalendars(true); } catch (error) { console.error("Error saving event:", error); setError(error instanceof Error ? error.message : "Failed to save event");