source and lens routing bugs fixed
This commit is contained in:
parent
596526e880
commit
627522295f
3 changed files with 139 additions and 82 deletions
65
app/api/options/laser_source/route.ts
Normal file
65
app/api/options/laser_source/route.ts
Normal file
|
|
@ -0,0 +1,65 @@
|
|||
// app/app/api/options/laser_source/route.ts
|
||||
import { NextResponse } from "next/server";
|
||||
import { directusFetch } from "@/lib/directus";
|
||||
|
||||
// Parse "nm" that may be stored as a string (e.g., "1064", "1064nm", "1,064")
|
||||
function parseNm(v: any): number | null {
|
||||
const s = String(v ?? "").replace(/[^0-9.]/g, "");
|
||||
if (!s) return null;
|
||||
const n = Number(s);
|
||||
return Number.isFinite(n) ? n : null;
|
||||
}
|
||||
|
||||
// target → wavelength range (nm)
|
||||
function nmRangeForTarget(t?: string): [number, number] | null {
|
||||
switch (t) {
|
||||
case "settings_fiber": return [1000, 1100];
|
||||
case "settings_uv": return [300, 400];
|
||||
case "settings_co2gan":
|
||||
case "settings_co2gal": return [10000, 11000];
|
||||
default: return null;
|
||||
}
|
||||
}
|
||||
|
||||
export async function GET(req: Request) {
|
||||
try {
|
||||
const { searchParams } = new URL(req.url);
|
||||
const target = searchParams.get("target") || undefined;
|
||||
const q = (searchParams.get("q") || "").trim().toLowerCase();
|
||||
const limit = Number(searchParams.get("limit") || "500");
|
||||
|
||||
const range = nmRangeForTarget(target);
|
||||
if (!range) {
|
||||
return NextResponse.json({ error: "missing/invalid target" }, { status: 400 });
|
||||
}
|
||||
const [lo, hi] = range;
|
||||
|
||||
// Only request fields we can read. laser_source uses submission_id as PK.
|
||||
const url = `/items/laser_source?fields=submission_id,make,model,nm&limit=${limit}`;
|
||||
const { data } = await directusFetch<{ data: any[] }>(url);
|
||||
const list = Array.isArray(data) ? data : [];
|
||||
|
||||
// Filter by nm and optional text query
|
||||
const filtered = list.filter((x) => {
|
||||
const nm = parseNm(x.nm);
|
||||
if (nm === null || nm < lo || nm > hi) return false;
|
||||
if (!q) return true;
|
||||
const label = [x.make, x.model].filter(Boolean).join(" ").toLowerCase();
|
||||
return label.includes(q);
|
||||
});
|
||||
|
||||
// Build labels and sort by make, then model
|
||||
const out = filtered
|
||||
.map((x) => ({
|
||||
id: String(x.submission_id), // critical: use submission_id, not id
|
||||
label: [x.make, x.model].filter(Boolean).join(" ") || String(x.submission_id),
|
||||
sortKey: [x.make ?? "", x.model ?? ""].join(" ").toLowerCase(),
|
||||
}))
|
||||
.sort((a, b) => a.sortKey.localeCompare(b.sortKey))
|
||||
.map(({ id, label }) => ({ id, label }));
|
||||
|
||||
return NextResponse.json({ data: out });
|
||||
} catch (e: any) {
|
||||
return NextResponse.json({ error: e?.message || "Failed to load laser_source" }, { status: 500 });
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue