Fundament: Element-Stammdaten, Fixpunkte, Horizont in Jahren, PK-Bezugsalter
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
@@ -5,18 +5,17 @@ import { getOwnedScenario, toPlanInput } from "@/lib/queries";
|
||||
import { getCurrentUserId } from "@/lib/session";
|
||||
import { touchScenario } from "@/lib/versioning-db";
|
||||
import { planHorizonChange } from "@/lib/retirement";
|
||||
import { MAX_PLANNING_HORIZON_AGE, MIN_PLANNING_HORIZON_AGE } from "@/lib/constants";
|
||||
import { MAX_PLANNING_HORIZON_YEARS, MIN_PLANNING_HORIZON_YEARS } from "@/lib/constants";
|
||||
|
||||
// Planungshorizont setzen: bis zu welchem Alter gerechnet wird.
|
||||
// Planungshorizont setzen: wie viele JAHRE die Planung umfasst.
|
||||
//
|
||||
// Wie beim Pensionsalter gilt: Zahl stellen, Struktur folgt. Die LETZTE Lebensphase wird so
|
||||
// verlängert oder gekürzt, dass der Plan genau bis zum Horizont läuft. Vorher ergab sich das
|
||||
// Planende stillschweigend aus der Summe der Phasendauern -- zwei Szenarien konnten dadurch
|
||||
// unbemerkt verschieden weit rechnen und waren nicht vergleichbar.
|
||||
// verlängert oder gekürzt, dass der Plan genau bis zum Horizont läuft. Gibt es noch keine
|
||||
// Phasen, wird nur die Zahl gespeichert -- sie ist dann die Grundlage, auf der der Assistent
|
||||
// die Zeitachse aufspannt.
|
||||
|
||||
const bodySchema = z.object({
|
||||
role: z.enum(["PERSON_A", "PERSON_B"]),
|
||||
horizonAge: z.number().int().min(MIN_PLANNING_HORIZON_AGE).max(MAX_PLANNING_HORIZON_AGE),
|
||||
horizonYears: z.number().int().min(MIN_PLANNING_HORIZON_YEARS).max(MAX_PLANNING_HORIZON_YEARS),
|
||||
});
|
||||
|
||||
export async function POST(request: NextRequest, { params }: { params: Promise<{ scenarioId: string }> }) {
|
||||
@@ -29,27 +28,19 @@ export async function POST(request: NextRequest, { params }: { params: Promise<{
|
||||
|
||||
const parsed = bodySchema.safeParse(await request.json().catch(() => ({})));
|
||||
if (!parsed.success) return NextResponse.json({ error: "Ungültige Eingabe." }, { status: 400 });
|
||||
const { role, horizonAge } = parsed.data;
|
||||
const { horizonYears } = parsed.data;
|
||||
|
||||
const planInput = toPlanInput(scenario);
|
||||
const person = planInput.persons.find((p) => p.role === role);
|
||||
if (!person) return NextResponse.json({ error: "Diese Person gibt es in diesem Szenario nicht." }, { status: 400 });
|
||||
if (horizonAge <= person.retirementAge) {
|
||||
return NextResponse.json(
|
||||
{ error: "Der Planungshorizont muss nach der Pensionierung liegen." },
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
|
||||
const change = planHorizonChange(planInput, horizonAge, role);
|
||||
if (!change) return NextResponse.json({ error: "Es gibt keine Lebensphase, die sich anpassen liesse." }, { status: 400 });
|
||||
if (change.blocked) return NextResponse.json({ error: change.blocked }, { status: 400 });
|
||||
const change = planInput.phases.length > 0 ? planHorizonChange(planInput, horizonYears) : null;
|
||||
if (change?.blocked) return NextResponse.json({ error: change.blocked }, { status: 400 });
|
||||
|
||||
await prisma.$transaction([
|
||||
prisma.person.update({ where: { id: person.id }, data: { planningHorizonAge: horizonAge } }),
|
||||
prisma.phase.update({ where: { id: change.lastPhaseId }, data: { durationYears: change.newDuration } }),
|
||||
prisma.scenario.update({ where: { id: scenarioId }, data: { planningHorizonYears: horizonYears } }),
|
||||
...(change
|
||||
? [prisma.phase.update({ where: { id: change.lastPhaseId }, data: { durationYears: change.newDuration } })]
|
||||
: []),
|
||||
]);
|
||||
|
||||
await touchScenario(scenario.id, userId);
|
||||
return NextResponse.json({ ok: true, lastPhaseDuration: change.newDuration });
|
||||
return NextResponse.json({ ok: true, lastPhaseDuration: change?.newDuration ?? null });
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user