import { NextRequest, NextResponse } from "next/server"; import { z } from "zod"; import { prisma } from "@/lib/db"; import { toPlanInput, getOwnedPlan } from "@/lib/queries"; import { getCurrentUserId } from "@/lib/session"; import { computePlan } from "@/lib/calculations"; import { planProfileSchema, validatePersonsForType } from "@/app/api/plans/route"; export async function GET( _request: NextRequest, { params }: { params: Promise<{ planId: string }> } ) { const userId = await getCurrentUserId(); if (!userId) return NextResponse.json({ error: "Nicht authentifiziert." }, { status: 401 }); const { planId } = await params; const plan = await getOwnedPlan(planId, userId); if (!plan) return NextResponse.json({ error: "Plan nicht gefunden." }, { status: 404 }); const planInput = toPlanInput(plan); const computed = computePlan(planInput); return NextResponse.json({ plan: planInput, computed }); } // Name allein aendern ODER das ganze Plan-Profil (Haushaltsform/Personen/Inflation). const patchSchema = z.union([ z.object({ name: z.string().min(1).max(120) }), planProfileSchema.extend({ name: z.string().min(1).max(120).optional() }), ]); export async function PATCH( request: NextRequest, { params }: { params: Promise<{ planId: string }> } ) { const userId = await getCurrentUserId(); if (!userId) return NextResponse.json({ error: "Nicht authentifiziert." }, { status: 401 }); const { planId } = await params; const plan = await prisma.plan.findFirst({ where: { id: planId, userId } }); if (!plan) return NextResponse.json({ error: "Plan nicht gefunden." }, { status: 404 }); const body = await request.json(); const parsed = patchSchema.safeParse(body); if (!parsed.success) return NextResponse.json({ error: "Ungueltige Eingabe." }, { status: 400 }); const data = parsed.data; const hasProfile = "householdType" in data; if (hasProfile) { const error = validatePersonsForType(data); if (error) return NextResponse.json({ error }, { status: 400 }); const updated = await prisma.$transaction(async (tx) => { await tx.person.deleteMany({ where: { planId: plan.id } }); return tx.plan.update({ where: { id: plan.id }, data: { name: data.name ?? undefined, householdType: data.householdType, inflationRateDefault: data.inflationRateDefault, persons: { create: data.persons }, }, }); }); return NextResponse.json({ plan: { id: updated.id, name: updated.name } }); } const updated = await prisma.plan.update({ where: { id: plan.id }, data: { name: data.name }, }); return NextResponse.json({ plan: { id: updated.id, name: updated.name } }); } export async function DELETE( _request: NextRequest, { params }: { params: Promise<{ planId: string }> } ) { const userId = await getCurrentUserId(); if (!userId) return NextResponse.json({ error: "Nicht authentifiziert." }, { status: 401 }); const { planId } = await params; const plan = await prisma.plan.findFirst({ where: { id: planId, userId } }); if (!plan) return NextResponse.json({ error: "Plan nicht gefunden." }, { status: 404 }); await prisma.plan.delete({ where: { id: plan.id } }); return NextResponse.json({ ok: true }); }