This commit is contained in:
@@ -0,0 +1,50 @@
|
||||
import { NextRequest, NextResponse } from "next/server";
|
||||
import { z } from "zod";
|
||||
import { prisma } from "@/lib/db";
|
||||
import { phaseInclude } from "@/lib/queries";
|
||||
|
||||
const createPhaseSchema = z.object({
|
||||
name: z.string().min(1).max(120),
|
||||
durationYears: z.number().int().min(1).max(80),
|
||||
inflationRate: z.number().min(-20).max(50).nullable().optional(),
|
||||
incomeMode: z.enum(["PER_PERSON", "HOUSEHOLD"]).default("HOUSEHOLD"),
|
||||
});
|
||||
|
||||
// Fuegt eine neue Lebensabschnittsphase am Ende der Phasenkette eines Plans an
|
||||
// (TDD Kapitel 3: Phasen werden chronologisch aneinandergereiht).
|
||||
export async function POST(
|
||||
request: NextRequest,
|
||||
{ params }: { params: Promise<{ planId: string }> }
|
||||
) {
|
||||
const { planId } = await params;
|
||||
const body = await request.json();
|
||||
const parsed = createPhaseSchema.safeParse(body);
|
||||
if (!parsed.success) {
|
||||
return NextResponse.json({ error: parsed.error.flatten() }, { status: 400 });
|
||||
}
|
||||
|
||||
const plan = await prisma.plan.findUnique({ where: { id: planId } });
|
||||
if (!plan) {
|
||||
return NextResponse.json({ error: "Plan nicht gefunden." }, { status: 404 });
|
||||
}
|
||||
|
||||
const lastPhase = await prisma.phase.findFirst({
|
||||
where: { planId },
|
||||
orderBy: { sequenceNumber: "desc" },
|
||||
});
|
||||
const nextSequence = (lastPhase?.sequenceNumber ?? 0) + 1;
|
||||
|
||||
const phase = await prisma.phase.create({
|
||||
data: {
|
||||
planId,
|
||||
sequenceNumber: nextSequence,
|
||||
name: parsed.data.name,
|
||||
durationYears: parsed.data.durationYears,
|
||||
inflationRate: parsed.data.inflationRate ?? null,
|
||||
incomeMode: parsed.data.incomeMode,
|
||||
},
|
||||
include: phaseInclude,
|
||||
});
|
||||
|
||||
return NextResponse.json({ phase }, { status: 201 });
|
||||
}
|
||||
Reference in New Issue
Block a user