100 lines
3.1 KiB
TypeScript
100 lines
3.1 KiB
TypeScript
import { NextResponse } from "next/server";
|
|
import { prisma } from "@/lib/db";
|
|
import { scenarioInclude } from "@/lib/scenario-service";
|
|
|
|
type Params = { params: Promise<{ planId: string }> };
|
|
|
|
export async function POST(request: Request, { params }: Params) {
|
|
const { planId } = await params;
|
|
const body = await request.json().catch(() => ({}));
|
|
|
|
const plan = await prisma.plan.findUnique({
|
|
where: { id: planId },
|
|
include: {
|
|
scenarios: {
|
|
orderBy: { sortOrder: "desc" },
|
|
take: 1,
|
|
include: scenarioInclude,
|
|
},
|
|
},
|
|
});
|
|
|
|
if (!plan) {
|
|
return NextResponse.json({ error: "Plan nicht gefunden" }, { status: 404 });
|
|
}
|
|
|
|
const source = body.copyFromScenarioId
|
|
? await prisma.scenario.findUnique({
|
|
where: { id: body.copyFromScenarioId },
|
|
include: scenarioInclude,
|
|
})
|
|
: plan.scenarios[0];
|
|
|
|
const nextOrder = (plan.scenarios[0]?.sortOrder ?? 0) + 1;
|
|
|
|
const scenario = await prisma.scenario.create({
|
|
data: {
|
|
planId,
|
|
name: body.name ?? `Szenario ${nextOrder + 1}`,
|
|
isBaseline: false,
|
|
sortOrder: nextOrder,
|
|
},
|
|
});
|
|
|
|
if (source && source.phases.length > 0) {
|
|
for (const phase of source.phases) {
|
|
const newPhase = await prisma.lifePhase.create({
|
|
data: {
|
|
scenarioId: scenario.id,
|
|
name: phase.name,
|
|
sortOrder: phase.sortOrder,
|
|
durationYears: phase.durationYears,
|
|
isRetirementPhase: phase.isRetirementPhase,
|
|
incomeMode: phase.incomeMode,
|
|
householdIncome: phase.householdIncome,
|
|
person1Income: phase.person1Income,
|
|
person2Income: phase.person2Income,
|
|
householdExpenses: phase.householdExpenses,
|
|
inflationRate: phase.inflationRate,
|
|
expectedPensionIncome: phase.expectedPensionIncome,
|
|
expectedPensionCapital: phase.expectedPensionCapital,
|
|
pensionCapitalTaxRate: phase.pensionCapitalTaxRate,
|
|
securities: {
|
|
create: phase.securities.map((s) => ({
|
|
name: s.name,
|
|
ownerTag: s.ownerTag,
|
|
startBalance: s.startBalance,
|
|
expectedReturnRate: s.expectedReturnRate,
|
|
annualSavingsAllocation: s.annualSavingsAllocation,
|
|
sortOrder: s.sortOrder,
|
|
})),
|
|
},
|
|
properties: {
|
|
create: phase.properties.map((p) => ({
|
|
name: p.name,
|
|
currentValue: p.currentValue,
|
|
mortgageBalance: p.mortgageBalance,
|
|
valueGrowthRate: p.valueGrowthRate,
|
|
annualAmortization: p.annualAmortization,
|
|
saleTaxRatePercent: p.saleTaxRatePercent,
|
|
sortOrder: p.sortOrder,
|
|
})),
|
|
},
|
|
oneTimeEvents: {
|
|
create: phase.oneTimeEvents.map((e) => ({
|
|
name: e.name,
|
|
yearOffset: e.yearOffset,
|
|
amount: e.amount,
|
|
type: e.type,
|
|
taxRatePercent: e.taxRatePercent,
|
|
})),
|
|
},
|
|
},
|
|
});
|
|
void newPhase;
|
|
}
|
|
}
|
|
|
|
return NextResponse.json(scenario, { status: 201 });
|
|
}
|