Files
Grow/src/app/api/milestones/route.ts
T
arnaudne 169309af05 security: enforce family ownership on all baby-data API routes
All collection routes (events, growth, doctor-notes, journal, milestones,
vaccinations, milk, reminders, teeth, search, export) now verify the requested
babyId belongs to the authenticated user's family before querying or writing.
All [id] mutation routes verify record ownership via nested baby→familyId
before any PATCH/DELETE. Additional fixes: admin config masks sensitive
secrets in GET response, invite send-email enforces PARENT role, photo
serving requires authentication, baby PATCH restricted to own-family PARENT.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-06-15 23:22:44 +02:00

60 lines
2.1 KiB
TypeScript

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 familyId = (session.user as { familyId?: string }).familyId;
if (!familyId) return NextResponse.json({ error: "Non autorisé" }, { status: 401 });
const ownedBaby = await prisma.baby.findFirst({ where: { id: babyId, familyId } });
if (!ownedBaby) return NextResponse.json({ error: "Bébé introuvable" }, { status: 404 });
const milestones = await prisma.milestone.findMany({
where: { babyId },
orderBy: { date: "desc" },
});
return NextResponse.json(milestones);
}
export async function POST(req: Request) {
const session = await auth();
if (!session?.user?.id) return NextResponse.json({ error: "Non autorisé" }, { status: 401 });
const role = (session.user as { role?: string }).role;
if (role === "READONLY") {
return NextResponse.json({ error: "Accès en lecture seule" }, { status: 403 });
}
const body = await req.json();
const { babyId, title, date, notes, icon } = body;
if (!babyId || !title || !date) {
return NextResponse.json({ error: "Champs requis manquants" }, { status: 400 });
}
const postFamilyId = (session.user as { familyId?: string }).familyId;
if (!postFamilyId) return NextResponse.json({ error: "Non autorisé" }, { status: 401 });
const postBaby = await prisma.baby.findFirst({ where: { id: babyId, familyId: postFamilyId } });
if (!postBaby) return NextResponse.json({ error: "Bébé introuvable" }, { status: 404 });
const milestone = await prisma.milestone.create({
data: {
babyId,
title,
date: new Date(date),
notes: notes ?? null,
icon: icon ?? null,
},
});
return NextResponse.json(milestone, { status: 201 });
}