source and lens routing bugs fixed

This commit is contained in:
makearmy 2025-09-22 13:47:00 -04:00
parent 596526e880
commit 627522295f
3 changed files with 139 additions and 82 deletions

View file

@ -1,54 +1,57 @@
// app/api/options/lens/route.ts
// app/app/api/options/lens/route.ts
import { NextResponse } from "next/server";
import { directusFetch } from "@/lib/directus";
/**
* For fiber, co2-galvo, uv f-theta scan lenses
* For co2-gantry focusing lenses
*/
function collectionForTarget(target?: string): { coll: string } | null {
switch (target) {
case "settings_fiber":
case "settings_co2gal":
case "settings_uv":
return { coll: "laser_scan_lens" };
case "settings_co2gan":
return { coll: "laser_focusing_lens" };
default:
return null;
}
}
function bestLensLabel(it: any): string {
// Try common fields, then derive something readable
if (it.name) return String(it.name);
if (it.model) return String(it.model);
if (it.focal_length_mm) return `F${it.focal_length_mm} mm`;
return String(it.submission_id ?? it.id ?? "lens");
// Decide which collection to read based on target
function lensCollectionForTarget(target?: string) {
if (target === "settings_co2gan") return "laser_focus_lens"; // gantry
// fiber, uv, co2gal → scan lenses
return "laser_scan_lens";
}
export async function GET(req: Request) {
try {
const { searchParams } = new URL(req.url);
const q = searchParams.get("q")?.trim() || "";
const limit = Number(searchParams.get("limit") || "500");
const target = searchParams.get("target") || undefined;
const q = (searchParams.get("q") || "").trim().toLowerCase();
const limit = Number(searchParams.get("limit") || "500");
const cfg = collectionForTarget(target);
if (!cfg) return NextResponse.json({ error: "missing/invalid target" }, { status: 400 });
const coll = lensCollectionForTarget(target);
const url = `/items/${cfg.coll}?limit=${limit}${q ? `&search=${encodeURIComponent(q)}` : ""}`;
// Request both possible PK fields, plus name & focal_length for labels
const url = `/items/${coll}?fields=submission_id,id,name,focal_length&limit=${limit}`;
const { data } = await directusFetch<{ data: any[] }>(url);
const list = Array.isArray(data) ? data : [];
const out = (data || [])
.map((it) => ({
id: String(it.submission_id ?? it.id),
label: bestLensLabel(it),
}))
.sort((a, b) => a.label.localeCompare(b.label));
const mapped = list.map((x) => {
const id = String(x.submission_id ?? x.id);
const label =
(x.name && String(x.name)) ||
(x.focal_length != null ? `F${x.focal_length} mm` : id);
const key =
(x.focal_length != null ? String(x.focal_length).padStart(6, "0") : "") +
" " +
label.toLowerCase();
return { id, label, key, focal: Number(x.focal_length ?? NaN) };
});
return NextResponse.json({ data: out });
// Optional text filter
const filtered = q
? mapped.filter((m) => m.label.toLowerCase().includes(q))
: mapped;
// Prefer numeric focal_length ordering, fallback to label
filtered.sort((a, b) => {
const aNum = Number.isFinite(a.focal);
const bNum = Number.isFinite(b.focal);
if (aNum && bNum) return a.focal - b.focal;
if (aNum) return -1;
if (bNum) return 1;
return a.key.localeCompare(b.key);
});
return NextResponse.json({ data: filtered.map(({ id, label }) => ({ id, label })) });
} catch (e: any) {
return NextResponse.json({ error: e?.message || "lens error" }, { status: 500 });
return NextResponse.json({ error: e?.message || "Failed to load lens options" }, { status: 500 });
}
}