routing fixes

This commit is contained in:
makearmy 2025-09-28 11:44:34 -04:00
parent 0cc1069526
commit a490471e4f
5 changed files with 37 additions and 57 deletions

View file

@ -8,8 +8,8 @@ function readCookie(name: string, cookieHeader: string) {
export async function GET(req: Request) {
const base = process.env.NEXT_PUBLIC_API_BASE_URL!;
// NOTE: include username explicitly
const url = `${base}/users/me?fields=id,username,display_name,first_name,last_name,email`;
// <- add username to the requested fields
const url = `${base}/users/me?fields=id,username,display_name,first_name,last_name,email`;
const cookieHeader = req.headers.get("cookie") ?? "";
const ma_at = readCookie("ma_at", cookieHeader);
@ -18,14 +18,12 @@ export async function GET(req: Request) {
if (cookieHeader) headers.cookie = cookieHeader;
if (ma_at) headers.authorization = `Bearer ${ma_at}`;
const res = await fetch(url, { headers, cache: "no-store" });
const body = await res.json().catch(() => ({}));
const res = await fetch(url, { headers, cache: "no-store" });
const text = await res.text().catch(() => "");
let body: any; try { body = text ? JSON.parse(text) : {}; } catch { body = {}; }
return new NextResponse(JSON.stringify(body), {
status: res.status,
headers: {
"content-type": "application/json",
"cache-control": "no-store",
},
headers: { "content-type": "application/json", "cache-control": "no-store" },
});
}

View file

@ -4,51 +4,36 @@ export const dynamic = "force-dynamic";
import { NextRequest, NextResponse } from "next/server";
const BASE = (process.env.DIRECTUS_URL || "").replace(/\/$/, "");
function nmForTarget(target?: string | null) {
// Optional: narrow list per tab; adjust if your data differs.
switch (target) {
case "fiber": return 1064;
case "uv": return 355;
case "co2-galvo":
case "co2-gantry": return 10600;
default: return null;
}
}
export async function GET(req: NextRequest) {
try {
const ma_at = req.cookies.get("ma_at")?.value;
if (!ma_at) return NextResponse.json({ error: "Not authenticated" }, { status: 401 });
const userAt = req.cookies.get("ma_at")?.value;
if (!userAt) return NextResponse.json({ error: "Not authenticated" }, { status: 401 });
const target = req.nextUrl.searchParams.get("target"); // "fiber" | "uv" | "co2-galvo" | "co2-gantry"
const url = new URL(`${BASE}/items/laser_source`);
// Your collection has make/model (no single "name")
url.searchParams.set("fields", "id,make,model,nm");
// IMPORTANT: schema uses submission_id as the FK target
url.searchParams.set("fields", "submission_id,make,model,nm");
url.searchParams.set("sort", "make,model");
const nm = nmForTarget(target);
if (nm != null) {
url.searchParams.set("filter[nm][_eq]", String(nm));
}
const res = await fetch(String(url), {
headers: { Accept: "application/json", Authorization: `Bearer ${ma_at}` },
headers: { Accept: "application/json", Authorization: `Bearer ${userAt}` },
cache: "no-store",
});
const text = await res.text().catch(() => "");
const json = text ? JSON.parse(text) : {};
if (!res.ok) {
return NextResponse.json(
{ error: `Directus ${res.status}: ${text || res.statusText}` },
{ status: res.status }
);
return NextResponse.json({ error: `Directus ${res.status}: ${text || res.statusText}` }, { status: res.status });
}
const json = text ? JSON.parse(text) : { data: [] };
const data = (json?.data ?? []).map((r: any) => ({
id: r.id,
label: [r.make, r.model].filter(Boolean).join(" ").trim() || String(r.id),
}));
const rows: Array<{ submission_id: string | number; make?: string; model?: string; nm?: string | number }> =
json?.data ?? [];
const data = rows
.map((r) => {
const parts = [r.make, r.model, r.nm ? `${r.nm}nm` : null].filter(Boolean);
return { id: r.submission_id, label: parts.join(" ") };
})
.filter((x) => x.label);
return NextResponse.json({ data });
} catch (e: any) {

View file

@ -6,31 +6,28 @@ const BASE = (process.env.DIRECTUS_URL || "").replace(/\/$/, "");
export async function GET(req: NextRequest) {
try {
const ma_at = req.cookies.get("ma_at")?.value;
if (!ma_at) return NextResponse.json({ error: "Not authenticated" }, { status: 401 });
const userAt = req.cookies.get("ma_at")?.value;
if (!userAt) return NextResponse.json({ error: "Not authenticated" }, { status: 401 });
const url = new URL(`${BASE}/items/material_opacity`);
url.searchParams.set("fields", "id,opacity");
url.searchParams.set("sort", "sort,opacity");
url.searchParams.set("sort", "opacity");
const res = await fetch(String(url), {
headers: { Accept: "application/json", Authorization: `Bearer ${ma_at}` },
headers: { Accept: "application/json", Authorization: `Bearer ${userAt}` },
cache: "no-store",
});
const text = await res.text().catch(() => "");
const json = text ? JSON.parse(text) : {};
if (!res.ok) {
return NextResponse.json(
{ error: `Directus ${res.status}: ${text || res.statusText}` },
{ status: res.status }
);
return NextResponse.json({ error: `Directus ${res.status}: ${text || res.statusText}` }, { status: res.status });
}
const json = text ? JSON.parse(text) : { data: [] };
const data = (json?.data ?? []).map((r: any) => ({
id: r.id,
label: r.opacity ?? String(r.id),
}));
const rows: Array<{ id: number | string; opacity?: string | number }> = json?.data ?? [];
const data = rows
.map(({ id, opacity }) => ({ id, label: String(opacity ?? "") }))
.filter((x) => x.label);
return NextResponse.json({ data });
} catch (e: any) {

View file

@ -168,14 +168,14 @@ export default function SettingsSubmit({ initialTarget }: { initialTarget?: Targ
};
}, []);
// Prefer username; then email; then names/display; lastly short id
// Prefer username; then display_name; then full name; then email.
const meLabel =
(me?.username && me.username.trim()) ||
(me?.email && me.email.trim()) ||
([me?.first_name, me?.last_name].filter(Boolean).join(" ").trim()) ||
(me?.display_name && me.display_name.trim()) ||
(me?.id && `User ${shortId(me.id)}`) ||
"Unknown user";
([me?.first_name, me?.last_name].filter(Boolean).join(" ").trim()) ||
(me?.email && me.email.trim()) ||
"";
// Options
const mats = useOptions("material");