import { NextRequest, NextResponse } from 'next/server'; import { getSession } from '@/lib/session'; import * as phpdb from '@/lib/phpdb'; export async function POST(request: NextRequest, { params }: { params: Promise<{ id: string }> }) { const session = await getSession(); if (!session) return NextResponse.json({ error: 'Nicht angemeldet' }, { status: 401 }); const { id } = await params; const sofueId = parseInt(id); if (!sofueId) return NextResponse.json({ error: 'Ungültige ID' }, { status: 400 }); try { const { aktion, termin, grund } = await request.json(); // Das Kürzel für die Notiz stammt aus der Session, nicht aus dem Body. const kuerzel = session.kuerzel; if (aktion === 'absage') { await phpdb.sofueAbsagen(sofueId, grund ?? '', kuerzel); } else if (aktion === 'verschiebung') { if (typeof termin !== 'string' || !/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:00$/.test(termin)) { return NextResponse.json({ error: 'Neuer Termin fehlt oder ist ungültig' }, { status: 400 }); } await phpdb.sofueVerschieben(sofueId, termin, grund ?? '', kuerzel); } else { return NextResponse.json({ error: 'Unbekannte Aktion' }, { status: 400 }); } return NextResponse.json({ ok: true }); } catch (error: unknown) { if (error instanceof Error && error.message.includes('404')) { return NextResponse.json({ error: 'Führung nicht gefunden' }, { status: 404 }); } console.error('POST /api/sofue/[id]:', error); return NextResponse.json({ error: 'Datenbankfehler' }, { status: 500 }); } }