dxGet fix for rigs

This commit is contained in:
makearmy 2025-09-29 21:12:32 -04:00
parent 74f100acf0
commit c596ad4b7d

View file

@ -16,6 +16,7 @@ export async function GET(req: Request) {
const q = new URL(req.url).searchParams;
const limit = Math.min(parseInt(q.get("limit") || "50", 10), 100);
const debug = q.get("debug") === "1";
const fields = [
"id",
@ -35,16 +36,63 @@ export async function GET(req: Request) {
"laser_software.name",
"date_created",
"date_updated",
// Add owner.id in case we need to inspect it in debug payloads
"owner.id"
].join(",");
// ✅ filter on the relation's id
const path =
`/items/user_rigs?filter[owner][id][_eq]=${encodeURIComponent(me.id)}` +
`&fields=${encodeURIComponent(fields)}` +
const base =
`/items/user_rigs` +
`?fields=${encodeURIComponent(fields)}` +
`&sort=-date_updated` +
`&limit=${limit}`;
const rows = await dxGET<any[]>(path, bearer); // dxGET returns unwrapped `data`
const attempts = [
{ label: "owner.id", path: `${base}&filter[owner][id][_eq]=${encodeURIComponent(me.id)}` },
{ label: "owner", path: `${base}&filter[owner][_eq]=${encodeURIComponent(me.id)}` },
];
let picked: { label: string; path: string } | null = null;
let rows: any[] = [];
for (const a of attempts) {
try {
const r = await dxGET<any[]>(a.path, bearer); // dxGET returns unwrapped `data`
if (Array.isArray(r) && r.length > 0) {
picked = a;
rows = r;
break;
}
// Keep the emptiest result in case both return [], so we can still respond
if (!picked) {
picked = a;
rows = Array.isArray(r) ? r : [];
}
} catch {
// ignore and try next attempt
}
}
if (debug) {
// In debug mode, include meta so you can see what's happening directly in the browser
return NextResponse.json({
meta: {
me: me.id,
picked: picked?.label,
attempts: await Promise.all(
attempts.map(async (a) => {
try {
const r = await dxGET<any[]>(a.path, bearer);
return { label: a.label, count: Array.isArray(r) ? r.length : 0 };
} catch (e: any) {
return { label: a.label, error: String(e?.message || e) };
}
})
),
},
data: rows,
});
}
return NextResponse.json(rows ?? []);
} catch (e: any) {
return bad(e?.message || "Failed to load rigs", e?.status || 500);
@ -101,7 +149,7 @@ export async function DELETE(req: Request) {
const id = url.searchParams.get("id");
if (!id) return bad("Missing: id");
// ✅ fetch owner.id for a precise comparison
// Ensure the rig belongs to the current user (owner.id for precision)
const me = await dxGET<{ id: string }>("/users/me?fields=id", bearer);
const rig = await dxGET<{ id: string; owner?: { id?: string } }>(
`/items/user_rigs/${encodeURIComponent(id)}?fields=id,owner.id`,