ce5f83823f
Deploy App / deploy (push) Successful in 1m52s
Haushaltsform, Personen (Name/Alter) und Startjahr wandern vom Szenario auf den Plan. Das Pensionsalter bleibt szenario-eigen -- es ist der Kern jedes Frueh-/Spaetpensionierungs-Szenarios. Neue Tabelle PlanPerson; Person behaelt nur Rolle + Pensionsalter; Plan bekommt householdType und startYear. Der Rechenkern bleibt unberuehrt: toPlanInput fuegt beide Ebenen wieder zu einem unveraenderten PlanInput zusammen. 43 Golden Tests unveraendert. Nebeneffekt: Ein Ist-Satz trifft jetzt in ALLEN Szenarien dasselbe Planjahr -- vorher war das nicht garantiert. Wiederherstellen einer Version setzt nur noch Szenario-Eigenes zurueck. Profil-Dialog kennzeichnet plan-weite vs. szenario-eigene Felder. Zwei Tests spielen echte V6-Daten ein und pruefen die Uebernahme. Spezifikation 0.23 (210 -> 212). Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
98 lines
3.6 KiB
TypeScript
98 lines
3.6 KiB
TypeScript
import { NextRequest, NextResponse } from "next/server";
|
|
import { z } from "zod";
|
|
import { prisma } from "@/lib/db";
|
|
import { getCurrentUserId } from "@/lib/session";
|
|
import { touchScenario } from "@/lib/versioning-db";
|
|
|
|
const personSchema = z.object({
|
|
role: z.enum(["PERSON_A", "PERSON_B"]),
|
|
name: z.string().max(60).nullish(),
|
|
age: z.number().int().min(0).max(120),
|
|
retirementAge: z.number().int().min(30).max(100),
|
|
});
|
|
|
|
// Das Grundprofil liegt am SZENARIO (nicht am Plan) -- dadurch kann ein Szenario z. B. ein
|
|
// anderes Pensionsalter tragen als das Basisszenario (Frühpensionierungs-Szenario).
|
|
export const scenarioProfileSchema = z.object({
|
|
householdType: z.enum(["SINGLE", "COUPLE"]),
|
|
inflationRateDefault: z.number().min(-20).max(50),
|
|
startYear: z.number().int().min(1900).max(2200).nullish(),
|
|
persons: z.array(personSchema).min(1).max(2),
|
|
});
|
|
|
|
const createPlanSchema = z.object({ name: z.string().min(1).max(120) }).and(scenarioProfileSchema);
|
|
|
|
export function validatePersonsForType(data: z.infer<typeof scenarioProfileSchema>): string | null {
|
|
if (data.householdType === "SINGLE" && data.persons.length !== 1) {
|
|
return "Einzelperson-Plan benötigt genau eine Person.";
|
|
}
|
|
if (data.householdType === "COUPLE" && data.persons.length !== 2) {
|
|
return "Paar-Plan benötigt genau zwei Personen (Person A und Person B).";
|
|
}
|
|
return null;
|
|
}
|
|
|
|
// Liste der Pläne mit ihrem Szenario-Baum (nur Kopfdaten).
|
|
export async function GET() {
|
|
const userId = await getCurrentUserId();
|
|
if (!userId) return NextResponse.json({ error: "Nicht authentifiziert." }, { status: 401 });
|
|
|
|
const plans = await prisma.plan.findMany({
|
|
where: { userId },
|
|
orderBy: { createdAt: "asc" },
|
|
select: {
|
|
id: true,
|
|
name: true,
|
|
createdAt: true,
|
|
scenarios: {
|
|
orderBy: [{ isBase: "desc" }, { createdAt: "asc" }],
|
|
select: { id: true, planId: true, name: true, isBase: true, parentScenarioId: true },
|
|
},
|
|
},
|
|
});
|
|
return NextResponse.json({ plans });
|
|
}
|
|
|
|
// Legt einen Plan an -- zusammen mit seinem Basisszenario, das das Grundprofil trägt.
|
|
export async function POST(request: NextRequest) {
|
|
const userId = await getCurrentUserId();
|
|
if (!userId) return NextResponse.json({ error: "Nicht authentifiziert." }, { status: 401 });
|
|
|
|
const body = await request.json();
|
|
const parsed = createPlanSchema.safeParse(body);
|
|
if (!parsed.success) return NextResponse.json({ error: parsed.error.flatten() }, { status: 400 });
|
|
const error = validatePersonsForType(parsed.data);
|
|
if (error) return NextResponse.json({ error }, { status: 400 });
|
|
|
|
// Haushaltsform, Personen und Startjahr am PLAN; das Pensionsalter je Szenario.
|
|
const plan = await prisma.plan.create({
|
|
data: {
|
|
userId,
|
|
name: parsed.data.name,
|
|
householdType: parsed.data.householdType,
|
|
startYear: parsed.data.startYear ?? new Date().getFullYear(),
|
|
persons: {
|
|
create: parsed.data.persons.map((p) => ({ role: p.role, name: p.name ?? null, age: p.age })),
|
|
},
|
|
scenarios: {
|
|
create: {
|
|
name: "Basisszenario",
|
|
isBase: true,
|
|
inflationRateDefault: parsed.data.inflationRateDefault,
|
|
persons: {
|
|
create: parsed.data.persons.map((p) => ({ role: p.role, retirementAge: p.retirementAge })),
|
|
},
|
|
},
|
|
},
|
|
},
|
|
include: { scenarios: true },
|
|
});
|
|
|
|
// Das frisch angelegte Basisszenario startet mit Version 1.0.
|
|
await touchScenario(plan.scenarios[0].id, userId);
|
|
return NextResponse.json(
|
|
{ plan: { id: plan.id }, scenario: { id: plan.scenarios[0].id } },
|
|
{ status: 201 }
|
|
);
|
|
}
|