hopefully last lens route fix

This commit is contained in:
Alexander Sellite 2025-09-22 14:57:41 -04:00
parent 2ef5d72a40
commit ce369e66e3

View file

@ -2,13 +2,7 @@
import { NextResponse } from "next/server";
import { directusFetch } from "@/lib/directus";
/**
* Lens options:
* - Fiber / UV / CO2 Galvo -> laser_scan_lens (field_size, focal_length)
* - CO2 Gantry -> laser_focus_lens (focal_length)
* Returns [{ id, label }] using submission_id if present, otherwise id.
*/
/** Map target to the correct lens collection */
function collectionForTarget(
target?: string
): "laser_scan_lens" | "laser_focus_lens" | null {
@ -34,11 +28,11 @@ export async function GET(req: Request) {
const coll = collectionForTarget(target);
if (!coll) return NextResponse.json({ data: [] });
// Important: don't request fields that don't exist.
// IMPORTANT: only request fields that exist on each collection.
const fields =
coll === "laser_scan_lens"
? "submission_id,id,field_size,focal_length"
: "submission_id,id,focal_length";
? "id,field_size,focal_length"
: "id,focal_length";
const url = `/items/${coll}?fields=${encodeURIComponent(
fields
@ -48,7 +42,7 @@ export async function GET(req: Request) {
const list = Array.isArray(data) ? data : [];
const items = list.map((x) => {
const id = String(x?.submission_id ?? x?.id);
const id = String(x?.id);
const fieldSize =
x?.field_size !== null && x?.field_size !== undefined
@ -58,11 +52,14 @@ export async function GET(req: Request) {
const fnum = Number(x?.focal_length);
const focalTxt = Number.isFinite(fnum) ? `F${fnum} mm` : "";
// Label: field_size first, then focal length (requested order).
let label = [fieldSize, focalTxt].filter(Boolean).join(" — ");
// Requested order: field_size first, then focal length
let label =
coll === "laser_scan_lens"
? [fieldSize, focalTxt].filter(Boolean).join(" — ")
: focalTxt || id;
if (!label) label = id;
// Sort numerically by focal length when available, else alpha by label.
const sortKey: number | string = Number.isFinite(fnum)
? fnum
: label.toLowerCase();