import { NextRequest, NextResponse } from 'next/server' import { createClient } from '@/lib/supabase/server' import { getLicServerConfig } from '@/lib/actions/licserver-config' import type { PartnerDto, LicServerProblemDetails } from '@/lib/types/licserver' /** * GET /api/licserver/partners/[id] * * Proxy to LicServer GET /api-v1/partners/{id} * [id] must be a valid UUID. * * Secured: requires a valid Supabase session. * base_url + api_key are read from DB settings (admin-configurable), * with fallback to LICSERVER_BASE_URL / LICSERVER_API_KEY env vars. */ export async function GET( _req: NextRequest, { params }: { params: Promise<{ id: string }> } ) { // ── Auth guard ───────────────────────────────────────────────────────── const supabase = await createClient() const { data: { user } } = await supabase.auth.getUser() if (!user) { return NextResponse.json({ error: 'Unauthorized' }, { status: 401 }) } // ── Load config from DB (falls back to env vars) ────────────────────── const cfg = await getLicServerConfig() if (!cfg.base_url || !cfg.api_key) { console.error('[licserver] base_url or api_key not configured (DB or env)') return NextResponse.json( { error: 'LicServer nicht konfiguriert. Bitte in den Admin-Einstellungen hinterlegen.' }, { status: 503 } ) } const { id } = await params // Basic UUID format guard const UUID_RE = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i if (!UUID_RE.test(id)) { return NextResponse.json({ error: 'Invalid partner ID' }, { status: 400 }) } // ── Proxy request ────────────────────────────────────────────────────── try { const res = await fetch( `${cfg.base_url}/api-v1/partners/${id}`, { method: 'GET', headers: { 'X-Api-Key': cfg.api_key, 'Accept': 'application/json', }, signal: AbortSignal.timeout(5_000), } ) if (!res.ok) { const problem: LicServerProblemDetails = await res.json().catch(() => ({})) return NextResponse.json(problem, { status: res.status }) } const data: PartnerDto = await res.json() return NextResponse.json(data) } catch (err) { console.error('[licserver] partner/:id fetch error:', err) return NextResponse.json( { error: 'LicServer nicht erreichbar' }, { status: 502 } ) } }