import { NextResponse } from "next/server"; import { auth } from "@/lib/auth"; import { prisma } from "@/lib/prisma"; export async function PATCH( req: Request, { params }: { params: Promise<{ id: string }> } ) { const session = await auth(); if (!session?.user?.id) return NextResponse.json({ error: "Non autorisé" }, { status: 401 }); const { id } = await params; const body = await req.json(); const familyId = (session.user as { familyId?: string }).familyId; if (!familyId) return NextResponse.json({ error: "Non autorisé" }, { status: 401 }); const owned = await prisma.medicationReminder.findFirst({ where: { id, baby: { familyId } } }); if (!owned) return NextResponse.json({ error: "Introuvable" }, { status: 404 }); const reminder = await prisma.medicationReminder.update({ where: { id }, data: { ...(body.name !== undefined ? { name: body.name } : {}), ...(body.dose !== undefined ? { dose: body.dose } : {}), ...(body.unit !== undefined ? { unit: body.unit } : {}), ...(body.intervalHours !== undefined ? { intervalHours: parseFloat(body.intervalHours) } : {}), ...(body.startAt !== undefined ? { startAt: new Date(body.startAt) } : {}), ...(body.enabled !== undefined ? { enabled: body.enabled } : {}), }, }); return NextResponse.json(reminder); } export async function DELETE( _req: Request, { params }: { params: Promise<{ id: string }> } ) { const session = await auth(); if (!session?.user?.id) return NextResponse.json({ error: "Non autorisé" }, { status: 401 }); const { id } = await params; const delFamilyId = (session.user as { familyId?: string }).familyId; if (!delFamilyId) return NextResponse.json({ error: "Non autorisé" }, { status: 401 }); const delOwned = await prisma.medicationReminder.findFirst({ where: { id, baby: { familyId: delFamilyId } } }); if (!delOwned) return NextResponse.json({ error: "Introuvable" }, { status: 404 }); await prisma.medicationReminder.delete({ where: { id } }); return new NextResponse(null, { status: 204 }); }