build bug fix
This commit is contained in:
parent
79f0af51eb
commit
1903762e3f
1 changed files with 45 additions and 78 deletions
|
|
@ -2,90 +2,57 @@
|
||||||
import { NextResponse } from "next/server";
|
import { NextResponse } from "next/server";
|
||||||
import { directusFetch } from "@/lib/directus";
|
import { directusFetch } from "@/lib/directus";
|
||||||
|
|
||||||
// Parse "nm" that may be stored as a string (e.g., "1064", "1064nm", "1,064")
|
// Generic collections handled by this route.
|
||||||
function parseNm(v: any): number | null {
|
// (laser_source has its own route so it's NOT included here)
|
||||||
const s = String(v ?? "").replace(/[^0-9.]/g, "");
|
const MAP: Record<
|
||||||
if (!s) return null;
|
string,
|
||||||
const n = Number(s);
|
{ path: string; fields: string; label: (x: any) => string }
|
||||||
return Number.isFinite(n) ? n : null;
|
> = {
|
||||||
}
|
material: { path: "/items/material", fields: "id,name", label: (x) => x.name },
|
||||||
|
material_coating: { path: "/items/material_coating", fields: "id,name", label: (x) => x.name },
|
||||||
// target → wavelength range (nm)
|
material_color: { path: "/items/material_color", fields: "id,name", label: (x) => x.name },
|
||||||
function nmRangeForTarget(t?: string): [number, number] | null {
|
material_opacity: { path: "/items/material_opacity", fields: "id,opacity", label: (x) => String(x.opacity) },
|
||||||
switch (t) {
|
laser_software: { path: "/items/laser_software", fields: "id,name", label: (x) => x.name },
|
||||||
case "settings_fiber": return [1000, 1100];
|
|
||||||
case "settings_uv": return [300, 400];
|
|
||||||
case "settings_co2gan":
|
|
||||||
case "settings_co2gal": return [10000, 11000];
|
|
||||||
default: return null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// generic collections we expose here
|
|
||||||
const GENERIC_MAP: Record<string, { coll: string }> = {
|
|
||||||
material: { coll: "material" },
|
|
||||||
material_coating: { coll: "material_coating" },
|
|
||||||
material_color: { coll: "material_color" },
|
|
||||||
material_opacity: { coll: "material_opacity" },
|
|
||||||
laser_software: { coll: "laser_software" },
|
|
||||||
};
|
};
|
||||||
|
|
||||||
function bestLabel(it: any): string {
|
export async function GET(
|
||||||
// prefer common fields; fall back gracefully
|
req: Request,
|
||||||
const cand = [it.name, it.label, it.title, it.value, it.opacity, it.color, it.coating];
|
{ params }: { params: { collection: string } }
|
||||||
const label = cand.find((x) => x != null && String(x).trim().length) ?? it.id ?? it.submission_id;
|
) {
|
||||||
return String(label);
|
const { searchParams } = new URL(req.url);
|
||||||
}
|
const q = searchParams.get("q")?.trim() || "";
|
||||||
|
const limit = Number(searchParams.get("limit") || "400");
|
||||||
|
const key = params.collection;
|
||||||
|
|
||||||
|
const cfg = MAP[key];
|
||||||
|
if (!cfg) {
|
||||||
|
// Unknown collection → empty list (prevents 4xx spam in logs)
|
||||||
|
return NextResponse.json({ data: [] });
|
||||||
|
}
|
||||||
|
|
||||||
|
// Build query (use a dummy base so URL can append params cleanly)
|
||||||
|
const url = new URL("http://x" + cfg.path);
|
||||||
|
url.searchParams.set("fields", cfg.fields);
|
||||||
|
url.searchParams.set("limit", String(limit));
|
||||||
|
if (q) url.searchParams.set("search", q);
|
||||||
|
|
||||||
export async function GET(req: Request, ctx: { params: { collection: string } }) {
|
|
||||||
try {
|
try {
|
||||||
const { searchParams } = new URL(req.url);
|
const { data } = await directusFetch<{ data: any[] }>(
|
||||||
const q = searchParams.get("q")?.trim() || "";
|
url.pathname + "?" + url.searchParams.toString()
|
||||||
const limit = Number(searchParams.get("limit") || "500");
|
);
|
||||||
const target = searchParams.get("target") || undefined;
|
|
||||||
|
|
||||||
const key = ctx.params.collection;
|
const out = (data || [])
|
||||||
|
.map((it) => ({
|
||||||
|
id: String(it.id),
|
||||||
|
label: cfg.label(it) ?? String(it.id),
|
||||||
|
}))
|
||||||
|
.sort((a, b) => a.label.localeCompare(b.label));
|
||||||
|
|
||||||
// laser_source: wavelength filtered by target, and pk may be 'submission_id'
|
return NextResponse.json({ data: out });
|
||||||
if (key === "laser_source") {
|
|
||||||
const range = nmRangeForTarget(target);
|
|
||||||
if (!range) return NextResponse.json({ error: "missing/invalid target for laser_source" }, { status: 400 });
|
|
||||||
|
|
||||||
const url = `/items/laser_source?limit=${limit}${q ? `&search=${encodeURIComponent(q)}` : ""}`;
|
|
||||||
const { data } = await directusFetch<{ data: any[] }>(url);
|
|
||||||
const [lo, hi] = range;
|
|
||||||
|
|
||||||
const filtered = (data || []).filter((x) => {
|
|
||||||
const nm = parseNm(x?.nm);
|
|
||||||
return nm !== null && nm >= lo && nm <= hi;
|
|
||||||
});
|
|
||||||
|
|
||||||
const out = filtered
|
|
||||||
.map((x) => {
|
|
||||||
const id = String(x.submission_id ?? x.id);
|
|
||||||
const label = [x.make, x.model].filter(Boolean).join(" ").trim() || id;
|
|
||||||
const sortKey = [x.make ?? "", x.model ?? ""].join(" ").toLowerCase();
|
|
||||||
return { id, label, sortKey };
|
|
||||||
})
|
|
||||||
.sort((a, b) => a.sortKey.localeCompare(b.sortKey))
|
|
||||||
.map(({ id, label }) => ({ id, label }));
|
|
||||||
|
|
||||||
return NextResponse.json({ data: out });
|
|
||||||
}
|
|
||||||
|
|
||||||
// generic collections
|
|
||||||
const cfg = GENERIC_MAP[key];
|
|
||||||
if (!cfg) return NextResponse.json({ error: "unsupported collection" }, { status: 400 });
|
|
||||||
|
|
||||||
const url = `/items/${cfg.coll}?limit=${limit}${q ? `&search=${encodeURIComponent(q)}` : ""}`;
|
|
||||||
const { data } = await directusFetch<{ data: any[] }>(url);
|
|
||||||
const mapped = (data || []).map((it) => ({
|
|
||||||
id: String(it.submission_id ?? it.id),
|
|
||||||
label: bestLabel(it),
|
|
||||||
}));
|
|
||||||
mapped.sort((a, b) => a.label.localeCompare(b.label));
|
|
||||||
return NextResponse.json({ data: mapped });
|
|
||||||
} catch (e: any) {
|
} catch (e: any) {
|
||||||
return NextResponse.json({ error: e?.message || "options error" }, { status: 500 });
|
return NextResponse.json(
|
||||||
|
{ error: e?.message || "Directus fetch failed" },
|
||||||
|
{ status: 500 }
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue