dxGet fix for rigs
This commit is contained in:
parent
74f100acf0
commit
c596ad4b7d
1 changed files with 54 additions and 6 deletions
|
|
@ -16,6 +16,7 @@ export async function GET(req: Request) {
|
||||||
|
|
||||||
const q = new URL(req.url).searchParams;
|
const q = new URL(req.url).searchParams;
|
||||||
const limit = Math.min(parseInt(q.get("limit") || "50", 10), 100);
|
const limit = Math.min(parseInt(q.get("limit") || "50", 10), 100);
|
||||||
|
const debug = q.get("debug") === "1";
|
||||||
|
|
||||||
const fields = [
|
const fields = [
|
||||||
"id",
|
"id",
|
||||||
|
|
@ -35,16 +36,63 @@ export async function GET(req: Request) {
|
||||||
"laser_software.name",
|
"laser_software.name",
|
||||||
"date_created",
|
"date_created",
|
||||||
"date_updated",
|
"date_updated",
|
||||||
|
// Add owner.id in case we need to inspect it in debug payloads
|
||||||
|
"owner.id"
|
||||||
].join(",");
|
].join(",");
|
||||||
|
|
||||||
// ✅ filter on the relation's id
|
const base =
|
||||||
const path =
|
`/items/user_rigs` +
|
||||||
`/items/user_rigs?filter[owner][id][_eq]=${encodeURIComponent(me.id)}` +
|
`?fields=${encodeURIComponent(fields)}` +
|
||||||
`&fields=${encodeURIComponent(fields)}` +
|
|
||||||
`&sort=-date_updated` +
|
`&sort=-date_updated` +
|
||||||
`&limit=${limit}`;
|
`&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 ?? []);
|
return NextResponse.json(rows ?? []);
|
||||||
} catch (e: any) {
|
} catch (e: any) {
|
||||||
return bad(e?.message || "Failed to load rigs", e?.status || 500);
|
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");
|
const id = url.searchParams.get("id");
|
||||||
if (!id) return bad("Missing: 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 me = await dxGET<{ id: string }>("/users/me?fields=id", bearer);
|
||||||
const rig = await dxGET<{ id: string; owner?: { id?: string } }>(
|
const rig = await dxGET<{ id: string; owner?: { id?: string } }>(
|
||||||
`/items/user_rigs/${encodeURIComponent(id)}?fields=id,owner.id`,
|
`/items/user_rigs/${encodeURIComponent(id)}?fields=id,owner.id`,
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue