Szenario-Hierarchie: Plan als Behaelter, Diff-Markierung (V6)
Deploy App / deploy (push) Successful in 1m43s
Deploy App / deploy (push) Successful in 1m43s
Groesste Umstrukturierung bisher. Der PLAN ist neu ein schlanker Behaelter ohne Finanzdaten; die berechenbare Einheit ist das SZENARIO. Modell: - Jeder Plan bekommt beim Anlegen automatisch ein Basisszenario (isBase). - Das Grundprofil liegt am Szenario, nicht am Plan -- nur so sind Szenarien mit abweichendem PENSIONSALTER moeglich (Fruehpensionierung), das in Person steckt. - Neue Szenarien sind vollstaendige Kopien eines BELIEBIGEN Szenarios und haengen als Baum darunter (parentScenarioId); die Seitenleiste rueckt sie ein. - Kopierte Phasen/Elemente tragen Herkunfts-Verweise (sourcePhaseId, sourceElementId). Ueber den Namen zu matchen waere fragil gewesen. Abweichungs-Markierung (Diff gegen das Eltern-Szenario, live): - geaendert = gelb, neu = gruen + Badge, entfernt = graue Geisterzeile. - Markiert: Phasen-/Uebergangszellen, Element-Zeilen, Phasenkoepfe, Cash-Anfangswert, Cash-Uebergaenge, Grundprofil. Zaehler ueber der Matrix. - Eigene Theme-Tokens fuer Hell/Dunkel/Warm -- ein fester Gelbwert waere im Dunkelschema unbrauchbar. Charts vergleichen neu die Geschwister-Szenarien statt fremder Plaene. Datenmodell/Migration: - Neue Tabelle Plan; bisheriger Plan -> Scenario (IDs erhalten, damit alle Kind-Fremdschluessel gueltig bleiben); planId -> scenarioId in Person/Phase/ FinancialElement. Bestehende Szenarien werden per rekursivem CTE demselben Behaelter zugeordnet, auch mehrfach verschachtelte. - Migration VOR dem Deploy gegen echtes PostgreSQL verifiziert (PGlite, in-process), inkl. verschachtelter Szenarien und Cascade. Der Test ist als migrations.test.ts committet und sichert kuenftige Migrationen ab. API neu unter /api/scenarios/*. 10 neue Tests (48 -> 58). Spezifikation auf v0.8. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
+28
-24
@@ -10,18 +10,17 @@ const personSchema = z.object({
|
||||
retirementAge: z.number().int().min(30).max(100),
|
||||
});
|
||||
|
||||
// Ein Plan traegt sein eigenes Grundprofil (Haushaltsform, Personen, Inflation).
|
||||
export const planProfileSchema = z.object({
|
||||
// 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),
|
||||
persons: z.array(personSchema).min(1).max(2),
|
||||
});
|
||||
|
||||
const createPlanSchema = z
|
||||
.object({ name: z.string().min(1).max(120) })
|
||||
.and(planProfileSchema);
|
||||
const createPlanSchema = z.object({ name: z.string().min(1).max(120) }).and(scenarioProfileSchema);
|
||||
|
||||
export function validatePersonsForType(data: z.infer<typeof planProfileSchema>): string | null {
|
||||
export function validatePersonsForType(data: z.infer<typeof scenarioProfileSchema>): string | null {
|
||||
if (data.householdType === "SINGLE" && data.persons.length !== 1) {
|
||||
return "Einzelperson-Plan benoetigt genau eine Person.";
|
||||
}
|
||||
@@ -31,40 +30,35 @@ export function validatePersonsForType(data: z.infer<typeof planProfileSchema>):
|
||||
return null;
|
||||
}
|
||||
|
||||
// Liste der Plaene mit ihrem Szenario-Baum (nur Kopfdaten).
|
||||
export async function GET() {
|
||||
const userId = await getCurrentUserId();
|
||||
if (!userId) {
|
||||
return NextResponse.json({ error: "Nicht authentifiziert." }, { status: 401 });
|
||||
}
|
||||
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,
|
||||
parentPlanId: true,
|
||||
branchFromPhaseId: true,
|
||||
createdAt: true,
|
||||
phases: {
|
||||
select: { id: true, name: true, sequenceNumber: true },
|
||||
orderBy: { sequenceNumber: "asc" },
|
||||
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 traegt.
|
||||
export async function POST(request: NextRequest) {
|
||||
const userId = await getCurrentUserId();
|
||||
if (!userId) {
|
||||
return NextResponse.json({ error: "Nicht authentifiziert." }, { status: 401 });
|
||||
}
|
||||
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 });
|
||||
}
|
||||
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 });
|
||||
|
||||
@@ -72,11 +66,21 @@ export async function POST(request: NextRequest) {
|
||||
data: {
|
||||
userId,
|
||||
name: parsed.data.name,
|
||||
householdType: parsed.data.householdType,
|
||||
inflationRateDefault: parsed.data.inflationRateDefault,
|
||||
persons: { create: parsed.data.persons },
|
||||
scenarios: {
|
||||
create: {
|
||||
name: "Basisszenario",
|
||||
isBase: true,
|
||||
householdType: parsed.data.householdType,
|
||||
inflationRateDefault: parsed.data.inflationRateDefault,
|
||||
persons: { create: parsed.data.persons },
|
||||
},
|
||||
},
|
||||
},
|
||||
include: { scenarios: true },
|
||||
});
|
||||
|
||||
return NextResponse.json({ plan: { id: plan.id } }, { status: 201 });
|
||||
return NextResponse.json(
|
||||
{ plan: { id: plan.id }, scenario: { id: plan.scenarios[0].id } },
|
||||
{ status: 201 }
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user