fb70781e5b
Deploy App / deploy (push) Successful in 1m45s
Sidebar zweistufig: pro Plan die Unterpunkte Szenarien / Effektive Werte / Analysen; Klick auf Plan-Name oeffnet ein Plan-Dashboard. Plan-Dashboard: Kennzahlen, gerechnete Werte ausdruecklich "laut Basisszenario", Ist-Abweichung falls erfasst. Szenario-Liste: Version, Elementzahl, Endvermoegen, Ruinalter + Aktionen Historie und Matrix. Baum in der Sidebar bleibt. Analysen: vier umklappende Kacheln (auch per Antippen). Grafiken oeffnen neu mit Auswahl EINER Grafik. Szenario-Vergleich zu den Grafiken, CSV-Export auf die Matrix. Gespeicherte Analysen: Grafik/MC/Einflussfaktoren als ZAHLEN einfrieren (read-only, nichts wird neu gerechnet) -- druckfaehig fuer den spaeteren PDF-Bericht, ohne finalWealthSorted. Einheitliche generische Ergebnisform. Neue Tabelle SavedAnalysis, Endpunkte /analyses und /dashboard, Module analyses.ts, Komponenten PlanViews/SavedAnalysisView/SaveAnalysisButton. Kein Eingriff in den Rechenkern. Spezifikation 0.24 (3.10 und 9.30 neu). Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
103 lines
3.9 KiB
TypeScript
103 lines
3.9 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,
|
|
householdType: true,
|
|
startYear: true,
|
|
persons: { select: { role: true, name: true, age: true }, orderBy: { role: "asc" } },
|
|
scenarios: {
|
|
orderBy: [{ isBase: "desc" }, { createdAt: "asc" }],
|
|
select: { id: true, planId: true, name: true, isBase: true, parentScenarioId: true },
|
|
},
|
|
// Zähler für die Übersicht und das Plan-Dashboard -- ohne die Datensätze selbst zu laden.
|
|
_count: { select: { actuals: true, analyses: 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 }
|
|
);
|
|
}
|