import { NextRequest, NextResponse } from "next/server"; import { prisma } from "@/lib/db"; import { getOwnedElement } from "@/lib/queries"; import { getCurrentUserId } from "@/lib/session"; import { phaseDataSchema } from "@/lib/elements"; // Speichert die Werte eines Elements innerhalb einer Lebensphase (Upsert). export async function PUT( request: NextRequest, { params }: { params: Promise<{ elementId: string; phaseId: string }> } ) { const userId = await getCurrentUserId(); if (!userId) return NextResponse.json({ error: "Nicht authentifiziert." }, { status: 401 }); const { elementId, phaseId } = await params; const element = await getOwnedElement(elementId, userId); if (!element) return NextResponse.json({ error: "Element nicht gefunden." }, { status: 404 }); const phase = await prisma.phase.findFirst({ where: { id: phaseId, scenarioId: element.scenarioId } }); if (!phase) return NextResponse.json({ error: "Phase nicht gefunden." }, { status: 404 }); const body = await request.json(); const parsed = phaseDataSchema.safeParse(body); if (!parsed.success) return NextResponse.json({ error: "Ungültige Eingabe." }, { status: 400 }); await prisma.elementPhaseValue.upsert({ where: { elementId_phaseId: { elementId, phaseId } }, create: { elementId, phaseId, data: parsed.data }, update: { data: parsed.data }, }); return NextResponse.json({ ok: true }); }