import { NextRequest, NextResponse } from "next/server"; import { getServerSession } from "next-auth/next"; import { authOptions } from "@/app/api/auth/[...nextauth]/route"; import { prisma } from "@/lib/prisma"; /** * Handles the GET request to retrieve calendars for the authenticated user. * * @param {NextRequest} req - The incoming request object. * @returns {Promise} - A promise that resolves to a JSON response containing the calendars or an error message. * * The function performs the following steps: * 1. Retrieves the server session using `getServerSession`. * 2. Checks if the user is authenticated by verifying the presence of `session.user.username`. * - If not authenticated, returns a 401 response with an error message. * 3. Attempts to fetch the calendars associated with the authenticated user from the database. * - If successful, returns the calendars in a JSON response. * - If an error occurs during the database query, logs the error and returns a 500 response with an error message. */ export async function GET(req: NextRequest) { const session = await getServerSession(authOptions); if (!session?.user?.username) { return NextResponse.json({ error: "Non authentifié" }, { status: 401 }); } try { const calendars = await prisma.calendar.findMany({ where: { userId: session.user.username, }, orderBy: { createdAt: "desc", }, }); return NextResponse.json(calendars); } catch (error) { console.error("Erreur lors de la récupération des calendriers:", error); return NextResponse.json({ error: "Erreur serveur" }, { status: 500 }); } } /** * Handles the POST request to create a new calendar. * * @param {NextRequest} req - The incoming request object. * @returns {Promise} The response object containing the created calendar or an error message. * * @throws {Error} If there is an issue with the request or server. * * The function performs the following steps: * 1. Retrieves the server session using `getServerSession`. * 2. Checks if the user is authenticated by verifying the presence of `session.user.username`. * 3. Parses the request body to extract `name`, `color`, and `description`. * 4. Validates that the `name` field is provided. * 5. Creates a new calendar entry in the database using Prisma. * 6. Returns the created calendar with a 201 status code. * 7. Catches and logs any errors, returning a 500 status code with an error message. */ export async function POST(req: NextRequest) { const session = await getServerSession(authOptions); if (!session?.user?.username) { return NextResponse.json({ error: "Non authentifié" }, { status: 401 }); } try { const { name, color, description } = await req.json(); // Validation if (!name) { return NextResponse.json( { error: "Le nom du calendrier est requis" }, { status: 400 } ); } const calendar = await prisma.calendar.create({ data: { name, color: color || "#0082c9", description, userId: session.user.username, }, }); return NextResponse.json(calendar, { status: 201 }); } catch (error) { console.error("Erreur lors de la création du calendrier:", error); return NextResponse.json({ error: "Erreur serveur" }, { status: 500 }); } }