makearmy-app/app/api/options/laser_source/route.ts
2025-09-28 14:54:52 -04:00

43 lines
1.5 KiB
TypeScript

import { NextRequest } from "next/server";
import { dFetchJSON, applyQFilter, json, Option } from "../_lib";
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) {
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 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 options: Option[] = filteredByNm.map((r) => ({
id: r.submission_id,
label: [r.make, r.model].filter(Boolean).join(" ") || String(r.submission_id),
}));
return json(applyQFilter(options, q, (o) => o.label));
}