This commit is contained in:
@@ -0,0 +1,118 @@
|
||||
import { NextRequest, NextResponse } from "next/server";
|
||||
import { z } from "zod";
|
||||
import { prisma } from "@/lib/db";
|
||||
|
||||
const transitionItemSchema = z.object({
|
||||
positionType: z.enum(["SECURITY", "REAL_ESTATE"]),
|
||||
securityId: z.string().nullable().optional(),
|
||||
realEstateId: z.string().nullable().optional(),
|
||||
decision: z.enum(["CARRY_OVER", "SELL"]),
|
||||
salePrice: z.number().nullable().optional(),
|
||||
});
|
||||
|
||||
const putTransitionSchema = z.object({
|
||||
items: z.array(transitionItemSchema),
|
||||
});
|
||||
|
||||
// Liefert die aktuellen Positionen der Phase (Wertschriften + Immobilien) sowie eine
|
||||
// evtl. bereits vorhandene Entscheidung, damit die UI den Uebergangs-Screen (TDD 4.4)
|
||||
// rendern kann.
|
||||
export async function GET(
|
||||
_request: NextRequest,
|
||||
{ params }: { params: Promise<{ phaseId: string }> }
|
||||
) {
|
||||
const { phaseId } = await params;
|
||||
const phase = await prisma.phase.findUnique({
|
||||
where: { id: phaseId },
|
||||
include: { securities: true, realEstates: true },
|
||||
});
|
||||
if (!phase) {
|
||||
return NextResponse.json({ error: "Phase nicht gefunden." }, { status: 404 });
|
||||
}
|
||||
|
||||
const nextPhase = await prisma.phase.findFirst({
|
||||
where: { planId: phase.planId, sequenceNumber: phase.sequenceNumber + 1 },
|
||||
});
|
||||
|
||||
const transition = await prisma.phaseTransition.findUnique({
|
||||
where: { fromPhaseId: phaseId },
|
||||
include: { items: true },
|
||||
});
|
||||
|
||||
return NextResponse.json({
|
||||
positions: {
|
||||
securities: phase.securities,
|
||||
realEstates: phase.realEstates,
|
||||
},
|
||||
nextPhase,
|
||||
transition,
|
||||
});
|
||||
}
|
||||
|
||||
// Speichert die Entscheidungen (Uebernehmen/Verkaufen) fuer jede Position der Vorphase.
|
||||
export async function PUT(
|
||||
request: NextRequest,
|
||||
{ params }: { params: Promise<{ phaseId: string }> }
|
||||
) {
|
||||
const { phaseId } = await params;
|
||||
const body = await request.json();
|
||||
const parsed = putTransitionSchema.safeParse(body);
|
||||
if (!parsed.success) {
|
||||
return NextResponse.json({ error: parsed.error.flatten() }, { status: 400 });
|
||||
}
|
||||
|
||||
const phase = await prisma.phase.findUnique({
|
||||
where: { id: phaseId },
|
||||
include: { securities: true, realEstates: true },
|
||||
});
|
||||
if (!phase) {
|
||||
return NextResponse.json({ error: "Phase nicht gefunden." }, { status: 404 });
|
||||
}
|
||||
|
||||
const nextPhase = await prisma.phase.findFirst({
|
||||
where: { planId: phase.planId, sequenceNumber: phase.sequenceNumber + 1 },
|
||||
});
|
||||
if (!nextPhase) {
|
||||
return NextResponse.json(
|
||||
{ error: "Es existiert noch keine Folgephase fuer diesen Uebergang." },
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
|
||||
const requiredIds = new Set([
|
||||
...phase.securities.map((s) => `SECURITY:${s.id}`),
|
||||
...phase.realEstates.map((re) => `REAL_ESTATE:${re.id}`),
|
||||
]);
|
||||
const providedIds = new Set(
|
||||
parsed.data.items.map((i) => `${i.positionType}:${i.securityId ?? i.realEstateId}`)
|
||||
);
|
||||
const missing = [...requiredIds].filter((id) => !providedIds.has(id));
|
||||
if (missing.length > 0) {
|
||||
return NextResponse.json(
|
||||
{ error: "Fuer jede bestehende Position muss Uebernehmen oder Verkaufen gewaehlt werden." },
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
|
||||
const transition = await prisma.$transaction(async (tx) => {
|
||||
await tx.phaseTransition.deleteMany({ where: { fromPhaseId: phaseId } });
|
||||
return tx.phaseTransition.create({
|
||||
data: {
|
||||
fromPhaseId: phaseId,
|
||||
toPhaseId: nextPhase.id,
|
||||
items: {
|
||||
create: parsed.data.items.map((i) => ({
|
||||
positionType: i.positionType,
|
||||
securityId: i.securityId ?? null,
|
||||
realEstateId: i.realEstateId ?? null,
|
||||
decision: i.decision,
|
||||
salePrice: i.salePrice ?? null,
|
||||
})),
|
||||
},
|
||||
},
|
||||
include: { items: true },
|
||||
});
|
||||
});
|
||||
|
||||
return NextResponse.json({ transition });
|
||||
}
|
||||
Reference in New Issue
Block a user