calendar 29
This commit is contained in:
parent
0a982dd218
commit
d85df3e8ce
@ -9,37 +9,43 @@ export async function DELETE(
|
||||
) {
|
||||
const session = await getServerSession(authOptions);
|
||||
|
||||
if (!session?.user?.username) {
|
||||
if (!session?.user?.id) {
|
||||
return NextResponse.json({ error: "Non authentifié" }, { status: 401 });
|
||||
}
|
||||
|
||||
try {
|
||||
// Verify event ownership through calendar
|
||||
const event = await prisma.event.findFirst({
|
||||
where: {
|
||||
id: params.id,
|
||||
},
|
||||
include: {
|
||||
calendar: true
|
||||
}
|
||||
// First, find the event and its associated calendar
|
||||
const event = await prisma.event.findUnique({
|
||||
where: { id: params.id },
|
||||
include: { calendar: true },
|
||||
});
|
||||
|
||||
if (!event || event.calendar.userId !== session.user.username) {
|
||||
if (!event) {
|
||||
return NextResponse.json(
|
||||
{ error: "Événement non trouvé ou non autorisé" },
|
||||
{ error: "Événement non trouvé" },
|
||||
{ status: 404 }
|
||||
);
|
||||
}
|
||||
|
||||
// Verify that the user owns the calendar
|
||||
if (event.calendar.userId !== session.user.id) {
|
||||
return NextResponse.json(
|
||||
{ error: "Non autorisé" },
|
||||
{ status: 403 }
|
||||
);
|
||||
}
|
||||
|
||||
// Delete the event
|
||||
await prisma.event.delete({
|
||||
where: {
|
||||
id: params.id,
|
||||
},
|
||||
where: { id: params.id },
|
||||
});
|
||||
|
||||
return NextResponse.json({ success: true });
|
||||
return new NextResponse(null, { status: 204 });
|
||||
} catch (error) {
|
||||
console.error("Erreur lors de la suppression de l'événement:", error);
|
||||
return NextResponse.json({ error: "Erreur serveur" }, { status: 500 });
|
||||
return NextResponse.json(
|
||||
{ error: "Erreur serveur" },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
@ -495,7 +495,9 @@ export function CalendarClient({ initialCalendars, userId, userProfile }: Calend
|
||||
const response = await fetch("/api/calendars");
|
||||
if (!response.ok) throw new Error("Failed to fetch calendars");
|
||||
const data = await response.json();
|
||||
console.log("Fetched calendars with events:", data);
|
||||
console.log("Fetched calendars:", data);
|
||||
|
||||
let calendarsData = data;
|
||||
|
||||
// Create principal calendar if it doesn't exist
|
||||
if (data.length === 0 || !data.some((cal: Calendar) => cal.name === "Calendrier principal")) {
|
||||
@ -513,13 +515,23 @@ export function CalendarClient({ initialCalendars, userId, userProfile }: Calend
|
||||
});
|
||||
|
||||
if (principalCalendar.ok) {
|
||||
const newData = await principalCalendar.json();
|
||||
data.push(newData);
|
||||
const newCalendar = await principalCalendar.json();
|
||||
calendarsData = [newCalendar, ...data];
|
||||
}
|
||||
} else {
|
||||
// Remove any duplicate "Calendrier principal" and "Default" calendars
|
||||
const seenPrincipal = false;
|
||||
calendarsData = data.filter((cal: Calendar) => {
|
||||
if (cal.name === "Calendrier principal") {
|
||||
if (seenPrincipal) return false;
|
||||
return true;
|
||||
}
|
||||
return cal.name !== "Default";
|
||||
});
|
||||
}
|
||||
|
||||
// Ensure events array exists for each calendar and convert dates
|
||||
const calendarsWithEvents = data.map((cal: Calendar & { events: Event[] }) => ({
|
||||
const calendarsWithEvents = calendarsData.map((cal: Calendar & { events: Event[] }) => ({
|
||||
...cal,
|
||||
events: (cal.events || []).map(event => ({
|
||||
...event,
|
||||
@ -531,16 +543,16 @@ export function CalendarClient({ initialCalendars, userId, userProfile }: Calend
|
||||
console.log("Setting calendars with processed events:", calendarsWithEvents);
|
||||
setCalendars(calendarsWithEvents);
|
||||
|
||||
if (calendarsWithEvents.length > 0 && !selectedCalendarId) {
|
||||
setSelectedCalendarId(calendarsWithEvents[0].id);
|
||||
// Set initial selected calendar
|
||||
if (calendarsWithEvents.length > 0) {
|
||||
const principalCalendar = calendarsWithEvents.find(cal => cal.name === "Calendrier principal");
|
||||
setSelectedCalendarId(principalCalendar?.id || calendarsWithEvents[0].id);
|
||||
}
|
||||
|
||||
// Update visible calendars if not set
|
||||
if (visibleCalendarIds.length === 0) {
|
||||
setVisibleCalendarIds(calendarsWithEvents.map(cal => cal.id));
|
||||
}
|
||||
// Update visible calendars
|
||||
setVisibleCalendarIds(calendarsWithEvents.map(cal => cal.id));
|
||||
|
||||
// Force calendar refresh after updating state
|
||||
// Force calendar refresh
|
||||
if (calendarRef.current) {
|
||||
const calendarApi = calendarRef.current.getApi();
|
||||
calendarApi.refetchEvents();
|
||||
|
||||
Loading…
Reference in New Issue
Block a user