Agenda refactor
This commit is contained in:
parent
8e775ceca7
commit
6e3e6966d4
@ -128,6 +128,21 @@ export async function DELETE(req: NextRequest, props: { params: Promise<{ id: st
|
|||||||
where: { id: params.id },
|
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 });
|
return new NextResponse(null, { status: 204 });
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error("Erreur lors de la suppression de l'événement:", error);
|
console.error("Erreur lors de la suppression de l'événement:", error);
|
||||||
|
|||||||
@ -4,6 +4,7 @@ import { authOptions } from "@/app/api/auth/options";
|
|||||||
import { prisma } from "@/lib/prisma";
|
import { prisma } from "@/lib/prisma";
|
||||||
import { updateMicrosoftEvent, deleteMicrosoftEvent } from "@/lib/services/microsoft-calendar-sync";
|
import { updateMicrosoftEvent, deleteMicrosoftEvent } from "@/lib/services/microsoft-calendar-sync";
|
||||||
import { logger } from "@/lib/logger";
|
import { logger } from "@/lib/logger";
|
||||||
|
import { invalidateCalendarCache } from "@/lib/redis";
|
||||||
|
|
||||||
// Helper function to check if user can manage events in a mission calendar
|
// Helper function to check if user can manage events in a mission calendar
|
||||||
async function canManageMissionCalendar(userId: string, calendarId: string): Promise<boolean> {
|
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);
|
console.log("Created event:", event);
|
||||||
return NextResponse.json(event, { status: 201 });
|
return NextResponse.json(event, { status: 201 });
|
||||||
} catch (error) {
|
} 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 event has externalEventId and calendar has Microsoft sync, update Microsoft too
|
||||||
if (existingEvent.externalEventId && calendar.syncConfig && calendar.syncConfig.provider === 'microsoft' && calendar.syncConfig.syncEnabled) {
|
if (existingEvent.externalEventId && calendar.syncConfig && calendar.syncConfig.provider === 'microsoft' && calendar.syncConfig.syncEnabled) {
|
||||||
const syncConfig = calendar.syncConfig;
|
const syncConfig = calendar.syncConfig;
|
||||||
|
|||||||
@ -831,10 +831,11 @@ export function CalendarClient({ initialCalendars, userId, userProfile }: Calend
|
|||||||
setUpcomingEvents(upcoming);
|
setUpcomingEvents(upcoming);
|
||||||
};
|
};
|
||||||
|
|
||||||
const fetchCalendars = async () => {
|
const fetchCalendars = async (forceRefresh: boolean = false) => {
|
||||||
try {
|
try {
|
||||||
setLoading(true);
|
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");
|
if (!response.ok) throw new Error("Failed to fetch calendars");
|
||||||
const data = await response.json();
|
const data = await response.json();
|
||||||
console.log("Raw calendars data:", data);
|
console.log("Raw calendars data:", data);
|
||||||
@ -1052,23 +1053,9 @@ export function CalendarClient({ initialCalendars, userId, userProfile }: Calend
|
|||||||
setSelectedEvent(null);
|
setSelectedEvent(null);
|
||||||
setError(null);
|
setError(null);
|
||||||
|
|
||||||
// Update calendars state with the new event
|
// Invalidate cache and fetch fresh data to ensure all calendars are up to date
|
||||||
const updatedCalendars = calendars.map(cal => {
|
// Force refresh by adding ?refresh=true to bypass cache
|
||||||
if (cal.id === eventData.calendarId) {
|
await fetchCalendars(true);
|
||||||
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();
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error("Error saving event:", error);
|
console.error("Error saving event:", error);
|
||||||
setError(error instanceof Error ? error.message : "Failed to save event");
|
setError(error instanceof Error ? error.message : "Failed to save event");
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user