This commit is contained in:
@@ -0,0 +1,31 @@
|
||||
import { NextRequest, NextResponse } from "next/server";
|
||||
import { prisma } from "@/lib/db";
|
||||
import { planInclude, toHouseholdInput, toPlanInput, getHouseholdOrNull } from "@/lib/queries";
|
||||
import { computePlan, planToCsv } from "@/lib/calculations";
|
||||
|
||||
export async function GET(
|
||||
_request: NextRequest,
|
||||
{ params }: { params: Promise<{ planId: string }> }
|
||||
) {
|
||||
const { planId } = await params;
|
||||
const household = await getHouseholdOrNull();
|
||||
if (!household) {
|
||||
return NextResponse.json({ error: "Kein Haushalt vorhanden." }, { status: 400 });
|
||||
}
|
||||
|
||||
const plan = await prisma.plan.findUnique({ where: { id: planId }, include: planInclude });
|
||||
if (!plan) {
|
||||
return NextResponse.json({ error: "Plan nicht gefunden." }, { status: 404 });
|
||||
}
|
||||
|
||||
const planInput = toPlanInput(plan);
|
||||
const computed = computePlan(planInput, toHouseholdInput(household));
|
||||
const csv = planToCsv(planInput, computed);
|
||||
|
||||
return new NextResponse(csv, {
|
||||
headers: {
|
||||
"Content-Type": "text/csv; charset=utf-8",
|
||||
"Content-Disposition": `attachment; filename="${plan.name.replace(/[^a-z0-9]+/gi, "_")}.csv"`,
|
||||
},
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
import { NextRequest, NextResponse } from "next/server";
|
||||
import { z } from "zod";
|
||||
import { prisma } from "@/lib/db";
|
||||
import { phaseInclude } from "@/lib/queries";
|
||||
|
||||
const createPhaseSchema = z.object({
|
||||
name: z.string().min(1).max(120),
|
||||
durationYears: z.number().int().min(1).max(80),
|
||||
inflationRate: z.number().min(-20).max(50).nullable().optional(),
|
||||
incomeMode: z.enum(["PER_PERSON", "HOUSEHOLD"]).default("HOUSEHOLD"),
|
||||
});
|
||||
|
||||
// Fuegt eine neue Lebensabschnittsphase am Ende der Phasenkette eines Plans an
|
||||
// (TDD Kapitel 3: Phasen werden chronologisch aneinandergereiht).
|
||||
export async function POST(
|
||||
request: NextRequest,
|
||||
{ params }: { params: Promise<{ planId: string }> }
|
||||
) {
|
||||
const { planId } = await params;
|
||||
const body = await request.json();
|
||||
const parsed = createPhaseSchema.safeParse(body);
|
||||
if (!parsed.success) {
|
||||
return NextResponse.json({ error: parsed.error.flatten() }, { status: 400 });
|
||||
}
|
||||
|
||||
const plan = await prisma.plan.findUnique({ where: { id: planId } });
|
||||
if (!plan) {
|
||||
return NextResponse.json({ error: "Plan nicht gefunden." }, { status: 404 });
|
||||
}
|
||||
|
||||
const lastPhase = await prisma.phase.findFirst({
|
||||
where: { planId },
|
||||
orderBy: { sequenceNumber: "desc" },
|
||||
});
|
||||
const nextSequence = (lastPhase?.sequenceNumber ?? 0) + 1;
|
||||
|
||||
const phase = await prisma.phase.create({
|
||||
data: {
|
||||
planId,
|
||||
sequenceNumber: nextSequence,
|
||||
name: parsed.data.name,
|
||||
durationYears: parsed.data.durationYears,
|
||||
inflationRate: parsed.data.inflationRate ?? null,
|
||||
incomeMode: parsed.data.incomeMode,
|
||||
},
|
||||
include: phaseInclude,
|
||||
});
|
||||
|
||||
return NextResponse.json({ phase }, { status: 201 });
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
import { NextRequest, NextResponse } from "next/server";
|
||||
import { prisma } from "@/lib/db";
|
||||
import { planInclude, toHouseholdInput, toPlanInput, getHouseholdOrNull } from "@/lib/queries";
|
||||
import { computePlan } from "@/lib/calculations";
|
||||
|
||||
export async function GET(
|
||||
_request: NextRequest,
|
||||
{ params }: { params: Promise<{ planId: string }> }
|
||||
) {
|
||||
const { planId } = await params;
|
||||
const household = await getHouseholdOrNull();
|
||||
if (!household) {
|
||||
return NextResponse.json({ error: "Kein Haushalt vorhanden." }, { status: 400 });
|
||||
}
|
||||
|
||||
const plan = await prisma.plan.findUnique({ where: { id: planId }, include: planInclude });
|
||||
if (!plan) {
|
||||
return NextResponse.json({ error: "Plan nicht gefunden." }, { status: 404 });
|
||||
}
|
||||
|
||||
const householdInput = toHouseholdInput(household);
|
||||
const planInput = toPlanInput(plan);
|
||||
const computed = computePlan(planInput, householdInput);
|
||||
|
||||
return NextResponse.json({ plan: planInput, computed });
|
||||
}
|
||||
|
||||
export async function DELETE(
|
||||
_request: NextRequest,
|
||||
{ params }: { params: Promise<{ planId: string }> }
|
||||
) {
|
||||
const { planId } = await params;
|
||||
await prisma.plan.delete({ where: { id: planId } });
|
||||
return NextResponse.json({ ok: true });
|
||||
}
|
||||
@@ -0,0 +1,120 @@
|
||||
import { NextRequest, NextResponse } from "next/server";
|
||||
import { z } from "zod";
|
||||
import { prisma } from "@/lib/db";
|
||||
import { phaseInclude, planInclude } from "@/lib/queries";
|
||||
|
||||
const scenarioSchema = z.object({
|
||||
name: z.string().min(1).max(120),
|
||||
branchFromPhaseId: z.string().min(1),
|
||||
});
|
||||
|
||||
// Erstellt ein neues Szenario als Kopie eines bestehenden Plans ab einer gewaehlten
|
||||
// Phase (inklusive). Die Phasenkette bis zu diesem Punkt wird per Deep-Copy dupliziert;
|
||||
// ab dort kann der Benutzer die Kette unabhaengig weiterentwickeln (TDD Kapitel 13).
|
||||
export async function POST(
|
||||
request: NextRequest,
|
||||
{ params }: { params: Promise<{ planId: string }> }
|
||||
) {
|
||||
const { planId } = await params;
|
||||
const body = await request.json();
|
||||
const parsed = scenarioSchema.safeParse(body);
|
||||
if (!parsed.success) {
|
||||
return NextResponse.json({ error: parsed.error.flatten() }, { status: 400 });
|
||||
}
|
||||
|
||||
const sourcePlan = await prisma.plan.findUnique({ where: { id: planId }, include: planInclude });
|
||||
if (!sourcePlan) {
|
||||
return NextResponse.json({ error: "Ursprungsplan nicht gefunden." }, { status: 404 });
|
||||
}
|
||||
|
||||
const branchPhase = sourcePlan.phases.find((p) => p.id === parsed.data.branchFromPhaseId);
|
||||
if (!branchPhase) {
|
||||
return NextResponse.json({ error: "Verzweigungsphase nicht gefunden." }, { status: 404 });
|
||||
}
|
||||
|
||||
const phasesToCopy = sourcePlan.phases
|
||||
.filter((p) => p.sequenceNumber <= branchPhase.sequenceNumber)
|
||||
.sort((a, b) => a.sequenceNumber - b.sequenceNumber);
|
||||
|
||||
const newPlanId = await prisma.$transaction(async (tx) => {
|
||||
const newPlan = await tx.plan.create({
|
||||
data: {
|
||||
householdId: sourcePlan.householdId,
|
||||
name: parsed.data.name,
|
||||
parentPlanId: sourcePlan.id,
|
||||
},
|
||||
});
|
||||
|
||||
let lastNewPhaseId = "";
|
||||
for (const phase of phasesToCopy) {
|
||||
const newPhase = await tx.phase.create({
|
||||
data: {
|
||||
planId: newPlan.id,
|
||||
sequenceNumber: phase.sequenceNumber,
|
||||
name: phase.name,
|
||||
durationYears: phase.durationYears,
|
||||
inflationRate: phase.inflationRate,
|
||||
incomeMode: phase.incomeMode,
|
||||
incomeEntries: {
|
||||
create: phase.incomeEntries.map((e) => ({
|
||||
personId: e.personId,
|
||||
label: e.label,
|
||||
amount: e.amount,
|
||||
})),
|
||||
},
|
||||
expenseEntries: {
|
||||
create: phase.expenseEntries.map((e) => ({ label: e.label, amount: e.amount })),
|
||||
},
|
||||
securities: {
|
||||
create: phase.securities.map((s) => ({
|
||||
name: s.name,
|
||||
startValue: s.startValue,
|
||||
expectedReturn: s.expectedReturn,
|
||||
annualContribution: s.annualContribution,
|
||||
ownerTag: s.ownerTag,
|
||||
saleTaxRate: s.saleTaxRate,
|
||||
})),
|
||||
},
|
||||
realEstates: {
|
||||
create: phase.realEstates.map((re) => ({
|
||||
name: re.name,
|
||||
marketValue: re.marketValue,
|
||||
mortgage: re.mortgage,
|
||||
valueGrowth: re.valueGrowth,
|
||||
amortization: re.amortization,
|
||||
salePrice: re.salePrice,
|
||||
saleTaxRate: re.saleTaxRate,
|
||||
})),
|
||||
},
|
||||
oneTimeEvents: {
|
||||
create: phase.oneTimeEvents.map((e) => ({
|
||||
type: e.type,
|
||||
amount: e.amount,
|
||||
description: e.description,
|
||||
})),
|
||||
},
|
||||
retirementInfos: {
|
||||
create: phase.retirementInfos.map((r) => ({
|
||||
personId: r.personId,
|
||||
ahvAmount: r.ahvAmount,
|
||||
pkPensionAmount: r.pkPensionAmount,
|
||||
lumpSumAmount: r.lumpSumAmount,
|
||||
lumpSumTaxRate: r.lumpSumTaxRate,
|
||||
})),
|
||||
},
|
||||
},
|
||||
include: phaseInclude,
|
||||
});
|
||||
lastNewPhaseId = newPhase.id;
|
||||
}
|
||||
|
||||
await tx.plan.update({
|
||||
where: { id: newPlan.id },
|
||||
data: { branchFromPhaseId: lastNewPhaseId },
|
||||
});
|
||||
|
||||
return newPlan.id;
|
||||
});
|
||||
|
||||
return NextResponse.json({ planId: newPlanId }, { status: 201 });
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
import { NextRequest, NextResponse } from "next/server";
|
||||
import { z } from "zod";
|
||||
import { prisma } from "@/lib/db";
|
||||
import { getHouseholdOrNull } from "@/lib/queries";
|
||||
|
||||
const createPlanSchema = z.object({
|
||||
name: z.string().min(1).max(120),
|
||||
});
|
||||
|
||||
export async function GET() {
|
||||
const household = await getHouseholdOrNull();
|
||||
if (!household) {
|
||||
return NextResponse.json({ plans: [] });
|
||||
}
|
||||
const plans = await prisma.plan.findMany({
|
||||
where: { householdId: household.id },
|
||||
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" },
|
||||
},
|
||||
},
|
||||
});
|
||||
return NextResponse.json({ plans });
|
||||
}
|
||||
|
||||
export async function POST(request: NextRequest) {
|
||||
const household = await getHouseholdOrNull();
|
||||
if (!household) {
|
||||
return NextResponse.json(
|
||||
{ error: "Bitte zuerst das Grundprofil (Onboarding) anlegen." },
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
|
||||
const body = await request.json();
|
||||
const parsed = createPlanSchema.safeParse(body);
|
||||
if (!parsed.success) {
|
||||
return NextResponse.json({ error: parsed.error.flatten() }, { status: 400 });
|
||||
}
|
||||
|
||||
const plan = await prisma.plan.create({
|
||||
data: { householdId: household.id, name: parsed.data.name },
|
||||
});
|
||||
|
||||
return NextResponse.json({ plan }, { status: 201 });
|
||||
}
|
||||
Reference in New Issue
Block a user