← back to Norma
app/api/intelligence/paths/route.ts
47 lines
import { NextRequest, NextResponse } from 'next/server';
import { requireRole } from '@/lib/require-role';
import { findPaths } from '@/lib/path-finder';
/**
* GET /api/intelligence/paths
* Query params (all required except max_depth):
* ?from_type= — entity_type of the origin node
* ?from_id= — entity_id of the origin node
* ?to_type= — entity_type of the destination node
* ?to_id= — entity_id of the destination node
* ?max_depth= — maximum traversal depth (default 3, max 6)
* Returns: { paths: [...], count: N }
*/
export async function GET(request: NextRequest) {
const auth = requireRole(request, 'admin', 'staff');
if (auth instanceof NextResponse) return auth;
try {
const { searchParams } = new URL(request.url);
const fromType = searchParams.get('from_type');
const fromId = searchParams.get('from_id');
const toType = searchParams.get('to_type');
const toId = searchParams.get('to_id');
// All four identity params are required
if (!fromType || !fromId || !toType || !toId) {
return NextResponse.json(
{ error: 'Missing required params: from_type, from_id, to_type, to_id' },
{ status: 400 },
);
}
let maxDepth = parseInt(searchParams.get('max_depth') || '3', 10);
if (isNaN(maxDepth) || maxDepth < 1) maxDepth = 3;
if (maxDepth > 6) maxDepth = 6;
const paths = await findPaths(fromType, fromId, toType, toId, maxDepth);
return NextResponse.json({ paths, count: paths.length });
} catch (err) {
console.error('[api/intelligence/paths] GET error:', (err as Error).message);
return NextResponse.json({ error: 'Failed to find paths' }, { status: 500 });
}
}