V3: 3 Farbschemata, Grundprofil auf Plan-Ebene, Erstellungs-Popups, integer-Zahlenfelder mit Beschleunigungs-Spinner, Carry-Forward des Zielwerts, gefuehrter Uebergang
Deploy App / deploy (push) Successful in 1m51s

- Theming: semantische CSS-Tokens + 3 waehlbare Schemata (Hell/Dunkel/Warm/Sunset), Umschalter im Profil-Menue, FOUC-frei via Inline-Script, localStorage; Klassen-Sweep aller Komponenten, Recharts aus Tokens
- Datenmodell: Household entfaellt; Plan traegt Haushaltsform/Personen/Inflation selbst (Person -> planId, Plan -> userId); destruktive Migration (TRUNCATE); Onboarding/HouseholdSettings entfernt; Plan-Erstellung & -Einstellungen mit Profilfeldern
- Popups: Element-Erstellung mit Inline-Feldern (geteilte ElementPhaseFields/ElementTransitionFields), Phase- und Plan-Popups mit Direkteingabe
- Zahlenfelder: 1'000er-Runden entfernt (floorToThousand/roundToHundred weg), integer MoneyInput mit beschleunigendem Press-and-Hold-Spinner, 0-Bug-Fix, harte Live-Caps
- Quote: Amortisation + Tilgung neu quotenwirksam; Restquote sichtbar (sinkt beim Verteilen); Invest-Deckel = verfuegbares Kapital + fortgeschriebener Zielwert
- Carry-Forward: Startwert der Folgephase = Zielwert der Vorphase minus Uebergangs-Bezug (live abgeleitet); optionale Zusatzinvestition aus verfuegbarem Kapital
- Matrix: Zelle zeigt Start -> Ziel; Uebergangs-Spaltenkopf mit "n offen"-Badge + gefuehrtem Pruef-Panel

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
2026-07-13 14:49:42 +02:00
parent b775ab77cb
commit 32adfb4476
32 changed files with 1706 additions and 1290 deletions
+21 -33
View File
@@ -1,7 +1,7 @@
import { NextRequest, NextResponse } from "next/server";
import { z } from "zod";
import { prisma } from "@/lib/db";
import { getHouseholdOrNull, getOwnedPlan, toHouseholdInput, toPlanInput } from "@/lib/queries";
import { getOwnedPlan, toPlanInput } from "@/lib/queries";
import { getCurrentUserId } from "@/lib/session";
import { Prisma } from "@/generated/prisma/client";
import { computePlan, maxPhaseDuration } from "@/lib/calculations";
@@ -10,11 +10,12 @@ import { num, type PhaseData } from "@/lib/elements";
const createPhaseSchema = z.object({
name: z.string().min(1).max(120).optional(),
durationYears: z.number().int().min(1).max(80).optional(),
inflationRate: z.number().min(-20).max(50).nullable().optional(),
});
// Legt eine neue Lebensphase am Ende der Kette an. Die Dauer wird ans naechste
// Pensionsereignis gekappt. Fuer bestehende Elemente werden die Werte 1:1 bzw. mit
// den fortgeschriebenen Endbestaenden aus der Vorphase vorbelegt.
// Pensionsereignis gekappt. Fuer bestehende Elemente werden die editierbaren Felder
// vorbelegt; die Startwerte werden in der Berechnung live aus der Vorphase fortgeschrieben.
export async function POST(
request: NextRequest,
{ params }: { params: Promise<{ planId: string }> }
@@ -23,8 +24,6 @@ export async function POST(
if (!userId) return NextResponse.json({ error: "Nicht authentifiziert." }, { status: 401 });
const { planId } = await params;
const household = await getHouseholdOrNull(userId);
if (!household) return NextResponse.json({ error: "Kein Haushalt vorhanden." }, { status: 400 });
const plan = await getOwnedPlan(planId, userId);
if (!plan) return NextResponse.json({ error: "Plan nicht gefunden." }, { status: 404 });
@@ -32,10 +31,9 @@ export async function POST(
const parsed = createPhaseSchema.safeParse(body);
if (!parsed.success) return NextResponse.json({ error: "Ungueltige Eingabe." }, { status: 400 });
const householdInput = toHouseholdInput(household);
const planInput = toPlanInput(plan);
const yearsBefore = planInput.phases.reduce((s, p) => s + p.durationYears, 0);
const cap = maxPhaseDuration(household.persons, planInput, yearsBefore);
const cap = maxPhaseDuration(planInput.persons, yearsBefore);
let duration = parsed.data.durationYears ?? (cap ?? 10);
if (cap != null) duration = Math.min(duration, cap);
@@ -44,18 +42,15 @@ export async function POST(
const nextSequence = planInput.phases.length + 1;
// Phasentyp der neuen Phase fuer den Default-Namen bestimmen.
const anyRetiredAtStart = household.persons.some((p) => {
const ra = p.role === "PERSON_A" ? planInput.retirementAgeA ?? p.retirementAge : planInput.retirementAgeB ?? p.retirementAge;
return p.age + yearsBefore >= ra;
});
const anyRetiredAtStart = planInput.persons.some((p) => p.age + yearsBefore >= p.retirementAge);
const defaultName =
parsed.data.name ?? (nextSequence === 1 ? "Erste Lebensphase" : anyRetiredAtStart ? "Pensionsphase" : "Erwerbsphase");
// Endbestaende der bisher letzten Phase (fuer Carry-Vorbelegung).
const prevComputed = planInput.phases.length > 0 ? computePlan(planInput, householdInput) : null;
// Status der Elemente in der bisher letzten Phase (verkauft/getilgt nicht fortfuehren).
const prevComputed = planInput.phases.length > 0 ? computePlan(planInput) : null;
const lastPhaseId = planInput.phases.at(-1)?.id;
const prevPhase = prevComputed?.phases.find((p) => p.id === lastPhaseId) ?? null;
const prevElemById = new Map((prevPhase?.elements ?? []).map((e) => [e.elementId, e]));
const prevStatusById = new Map((prevPhase?.elements ?? []).map((e) => [e.elementId, e.status]));
const phase = await prisma.$transaction(async (tx) => {
const created = await tx.phase.create({
@@ -64,15 +59,15 @@ export async function POST(
sequenceNumber: nextSequence,
name: defaultName,
durationYears: duration,
inflationRate: parsed.data.inflationRate ?? undefined,
},
});
// Carry-Vorbelegung fuer bestehende Elemente.
// Vorbelegung der editierbaren Felder bestehender Elemente.
for (const e of planInput.elements) {
const prev = prevElemById.get(e.id);
if (prev && prev.status !== "ACTIVE") continue; // verkauft/getilgt -> nicht mehr fortfuehren
if ((prevStatusById.get(e.id) ?? "ACTIVE") !== "ACTIVE") continue; // verkauft/getilgt
const prevData: PhaseData = e.phaseValues[lastPhaseId ?? ""] ?? {};
const data: PhaseData = buildCarryData(e.category, prevData, prev?.endValue);
const data: PhaseData = buildCarryData(e.category, prevData);
await tx.elementPhaseValue.create({
data: { elementId: e.id, phaseId: created.id, data: data as Prisma.InputJsonValue },
});
@@ -84,12 +79,10 @@ export async function POST(
return NextResponse.json({ phase: { id: phase.id } }, { status: 201 });
}
function buildCarryData(
category: string,
prev: PhaseData,
prevEndValue: number | undefined
): PhaseData {
const endVal = Math.max(0, Math.round(prevEndValue ?? 0));
// Vorbelegung fuer eine neue Phase: nur die editierbaren Felder werden uebernommen. Start-
// bzw. Restwerte (PK/3a/Vermoegen/Hypothek/Schuld) werden in der Berechnung live aus der
// Vorphase fortgeschrieben und deshalb hier NICHT als Snapshot gespeichert.
function buildCarryData(category: string, prev: PhaseData): PhaseData {
switch (category) {
case "INCOME":
case "EXPENSE":
@@ -98,18 +91,13 @@ function buildCarryData(
return { gapYears: 0 };
case "PENSION_FUND":
case "PILLAR_3A":
return { currentValue: endVal, annualContribution: num(prev.annualContribution), expectedReturn: num(prev.expectedReturn) };
case "OTHER_ASSET":
return { startValue: endVal, annualContribution: num(prev.annualContribution), expectedReturn: num(prev.expectedReturn) };
return { annualContribution: num(prev.annualContribution), expectedReturn: num(prev.expectedReturn) };
case "REAL_ESTATE":
// endValue = purchase - mortgage; Hypothek fortschreiben ueber purchasePrice - endValue.
return {
purchasePrice: num(prev.purchasePrice),
mortgage: Math.max(0, num(prev.purchasePrice) - endVal),
amortization: num(prev.amortization),
};
// purchasePrice + amortization bleiben; die Resthypothek wird live fortgeschrieben.
return { purchasePrice: num(prev.purchasePrice), amortization: num(prev.amortization) };
case "OTHER_DEBT":
return { startValue: Math.abs(endVal), annualRepayment: num(prev.annualRepayment) };
return { annualRepayment: num(prev.annualRepayment) };
default:
return {};
}