import { NextResponse } from "next/server"; import { auth } from "@/lib/auth"; import { prisma } from "@/lib/prisma"; export async function GET(req: Request) { const session = await auth(); if (!session?.user?.id) return NextResponse.json({ error: "Non autorisé" }, { status: 401 }); const { searchParams } = new URL(req.url); const babyId = searchParams.get("babyId"); if (!babyId) return NextResponse.json({ error: "babyId requis" }, { status: 400 }); const notes = await prisma.doctorNote.findMany({ where: { babyId }, orderBy: { date: "desc" }, include: { user: { select: { id: true, name: true } } }, }); return NextResponse.json(notes); } export async function POST(req: Request) { const session = await auth(); if (!session?.user?.id) return NextResponse.json({ error: "Non autorisé" }, { status: 401 }); const userRole = (session.user as { role?: string }).role; if (userRole !== "DOCTOR") { return NextResponse.json({ error: "Réservé aux médecins" }, { status: 403 }); } const body = await req.json(); const { babyId, content, date } = body; if (!babyId || !content || !date) { return NextResponse.json({ error: "Champs requis manquants" }, { status: 400 }); } const noteDate = new Date(date); noteDate.setHours(12, 0, 0, 0); const note = await prisma.doctorNote.create({ data: { babyId, userId: session.user.id, content, date: noteDate, }, include: { user: { select: { id: true, name: true } } }, }); return NextResponse.json(note, { status: 201 }); }