58 lines
1.6 KiB
TypeScript
58 lines
1.6 KiB
TypeScript
import { NextRequest, NextResponse } from "next/server"
|
|
import { prisma } from "@/lib/db"
|
|
import { auth } from "@/lib/auth"
|
|
|
|
export async function GET(request: NextRequest) {
|
|
const session = await auth.api.getSession({ headers: request.headers })
|
|
if (!session?.user?.id) {
|
|
return NextResponse.json({ error: "No autorizado" }, { status: 401 })
|
|
}
|
|
|
|
const parent = await prisma.parent.findUnique({
|
|
where: { id: session.user.id },
|
|
include: {
|
|
family: {
|
|
include: { children: true },
|
|
},
|
|
},
|
|
})
|
|
|
|
if (!parent || !parent.family) {
|
|
return NextResponse.json({ error: "Parent or family not found" }, { status: 404 })
|
|
}
|
|
|
|
return NextResponse.json(parent.family.children)
|
|
}
|
|
|
|
export async function POST(request: Request) {
|
|
const session = await auth.api.getSession({ headers: request.headers })
|
|
if (!session?.user?.id) {
|
|
return NextResponse.json({ error: "No autorizado" }, { status: 401 })
|
|
}
|
|
|
|
const { name, birthdate, profileNotes } = await request.json()
|
|
if (!name) {
|
|
return NextResponse.json({ error: "name required" }, { status: 400 })
|
|
}
|
|
|
|
const parent = await prisma.parent.findUnique({
|
|
where: { id: session.user.id },
|
|
select: { familyId: true },
|
|
})
|
|
|
|
if (!parent || !parent.familyId) {
|
|
return NextResponse.json({ error: "Parent has no family" }, { status: 400 })
|
|
}
|
|
|
|
const child = await prisma.child.create({
|
|
data: {
|
|
name,
|
|
birthdate: birthdate ? new Date(birthdate) : undefined,
|
|
profileNotes,
|
|
familyId: parent.familyId,
|
|
},
|
|
})
|
|
|
|
return NextResponse.json(child, { status: 201 })
|
|
}
|