routing fixes

This commit is contained in:
makearmy 2025-09-28 11:44:34 -04:00
parent 0cc1069526
commit a490471e4f
5 changed files with 37 additions and 57 deletions

View file

@ -4,51 +4,36 @@ export const dynamic = "force-dynamic";
import { NextRequest, NextResponse } from "next/server";
const BASE = (process.env.DIRECTUS_URL || "").replace(/\/$/, "");
function nmForTarget(target?: string | null) {
// Optional: narrow list per tab; adjust if your data differs.
switch (target) {
case "fiber": return 1064;
case "uv": return 355;
case "co2-galvo":
case "co2-gantry": return 10600;
default: return null;
}
}
export async function GET(req: NextRequest) {
try {
const ma_at = req.cookies.get("ma_at")?.value;
if (!ma_at) return NextResponse.json({ error: "Not authenticated" }, { status: 401 });
const userAt = req.cookies.get("ma_at")?.value;
if (!userAt) return NextResponse.json({ error: "Not authenticated" }, { status: 401 });
const target = req.nextUrl.searchParams.get("target"); // "fiber" | "uv" | "co2-galvo" | "co2-gantry"
const url = new URL(`${BASE}/items/laser_source`);
// Your collection has make/model (no single "name")
url.searchParams.set("fields", "id,make,model,nm");
// IMPORTANT: schema uses submission_id as the FK target
url.searchParams.set("fields", "submission_id,make,model,nm");
url.searchParams.set("sort", "make,model");
const nm = nmForTarget(target);
if (nm != null) {
url.searchParams.set("filter[nm][_eq]", String(nm));
}
const res = await fetch(String(url), {
headers: { Accept: "application/json", Authorization: `Bearer ${ma_at}` },
headers: { Accept: "application/json", Authorization: `Bearer ${userAt}` },
cache: "no-store",
});
const text = await res.text().catch(() => "");
const json = text ? JSON.parse(text) : {};
if (!res.ok) {
return NextResponse.json(
{ error: `Directus ${res.status}: ${text || res.statusText}` },
{ status: res.status }
);
return NextResponse.json({ error: `Directus ${res.status}: ${text || res.statusText}` }, { status: res.status });
}
const json = text ? JSON.parse(text) : { data: [] };
const data = (json?.data ?? []).map((r: any) => ({
id: r.id,
label: [r.make, r.model].filter(Boolean).join(" ").trim() || String(r.id),
}));
const rows: Array<{ submission_id: string | number; make?: string; model?: string; nm?: string | number }> =
json?.data ?? [];
const data = rows
.map((r) => {
const parts = [r.make, r.model, r.nm ? `${r.nm}nm` : null].filter(Boolean);
return { id: r.submission_id, label: parts.join(" ") };
})
.filter((x) => x.label);
return NextResponse.json({ data });
} catch (e: any) {