routing fixes
This commit is contained in:
parent
0ee9686fb1
commit
44a690f4d2
20 changed files with 769 additions and 962 deletions
|
|
@ -1,42 +1,43 @@
|
|||
// app/api/options/laser_source/route.ts
|
||||
export const dynamic = "force-dynamic";
|
||||
import { NextRequest } from "next/server";
|
||||
import { dFetchJSON, applyQFilter, json, Option } from "../_lib";
|
||||
|
||||
import { NextRequest, NextResponse } from "next/server";
|
||||
const BASE = (process.env.DIRECTUS_URL || "").replace(/\/$/, "");
|
||||
type Row = { submission_id: string | number; make?: string | null; model?: string | null; nm?: string | null };
|
||||
|
||||
function rangeForTarget(target?: string | null): [number, number] | null {
|
||||
if (!target) return null;
|
||||
const t = target.toLowerCase();
|
||||
if (t === "fiber") return [1000, 9000];
|
||||
if (t === "uv") return [300, 400];
|
||||
if (t === "co2-gantry" || t === "co2-galvo") return [10000, 11000];
|
||||
return null;
|
||||
}
|
||||
function parseNm(s?: string | null): number | null {
|
||||
if (!s) return null;
|
||||
const m = String(s).match(/(\d+(\.\d+)?)/);
|
||||
return m ? Number(m[1]) : null;
|
||||
}
|
||||
|
||||
export async function GET(req: NextRequest) {
|
||||
try {
|
||||
const userAt = req.cookies.get("ma_at")?.value;
|
||||
if (!userAt) return NextResponse.json({ error: "Not authenticated" }, { status: 401 });
|
||||
const url = new URL(req.url);
|
||||
const q = url.searchParams.get("q");
|
||||
const target = url.searchParams.get("target"); // fiber | uv | co2-gantry | co2-galvo
|
||||
const { data } = await dFetchJSON<{ data: Row[] }>(
|
||||
req,
|
||||
"/items/laser_source?fields=submission_id,make,model,nm&limit=2000&sort=make,model"
|
||||
);
|
||||
|
||||
const url = new URL(`${BASE}/items/laser_source`);
|
||||
// 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 range = rangeForTarget(target);
|
||||
const filteredByNm = range
|
||||
? data.filter((r) => {
|
||||
const v = parseNm(r.nm);
|
||||
return v != null && v >= range[0] && v <= range[1];
|
||||
})
|
||||
: data;
|
||||
|
||||
const res = await fetch(String(url), {
|
||||
headers: { Accept: "application/json", Authorization: `Bearer ${userAt}` },
|
||||
cache: "no-store",
|
||||
});
|
||||
const options: Option[] = filteredByNm.map((r) => ({
|
||||
id: r.submission_id,
|
||||
label: [r.make, r.model].filter(Boolean).join(" ") || String(r.submission_id),
|
||||
}));
|
||||
|
||||
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 });
|
||||
}
|
||||
|
||||
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) {
|
||||
return NextResponse.json({ error: e?.message || "Failed to load laser sources" }, { status: 500 });
|
||||
}
|
||||
return json(applyQFilter(options, q, (o) => o.label));
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue