All files / src/app/api/nlq route.ts

83.33% Statements 20/24
46.15% Branches 6/13
100% Functions 2/2
90.9% Lines 20/22

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54        3x   3x     2x 2x   2x 2x     2x 2x       2x   2x               1x 1x   1x 1x         1x 1x   1x 1x 1x 1x              
import { NextRequest, NextResponse } from "next/server";
import { requireAuth, isAuthError } from "@/lib/auth-guard";
import { resolveOdrlScope, userToParticipantId } from "@/lib/odrl-engine";
 
export const dynamic = "force-dynamic";
 
const PROXY_URL = process.env.NEO4J_PROXY_URL ?? "http://localhost:9090";
 
export async function POST(request: NextRequest) {
  const auth = await requireAuth();
  Iif (isAuthError(auth)) return auth;
 
  try {
    const body = await request.json();
 
    // Resolve caller's ODRL scope and forward to neo4j-proxy
    const { session } = auth;
    const participantId = userToParticipantId(
      session.user.email ?? session.user.name ?? session.user.id,
      session.roles,
    );
    const odrlScope = await resolveOdrlScope(participantId);
 
    const resp = await fetch(`${PROXY_URL}/nlq`, {
      method: "POST",
      headers: {
        "Content-Type": "application/json",
        "X-Participant": participantId,
      },
      body: JSON.stringify({ ...body, odrlScope }),
    });
    const data = await resp.json();
    return NextResponse.json(data, { status: resp.status });
  } catch (err: unknown) {
    const message = err instanceof Error ? err.message : "NLQ proxy error";
    return NextResponse.json({ error: message }, { status: 502 });
  }
}
 
export async function GET() {
  const auth = await requireAuth();
  Iif (isAuthError(auth)) return auth;
 
  try {
    const resp = await fetch(`${PROXY_URL}/nlq/templates`);
    const data = await resp.json();
    return NextResponse.json(data);
  } catch (err: unknown) {
    const message =
      err instanceof Error ? err.message : "NLQ templates proxy error";
    return NextResponse.json({ error: message }, { status: 502 });
  }
}