Agenda refactor

This commit is contained in:
alma 2026-01-15 17:41:10 +01:00
parent 8e775ceca7
commit 6e3e6966d4
3 changed files with 52 additions and 19 deletions

View File

@ -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);

View File

@ -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<boolean> {
@ -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;

View File

@ -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");