dxGet fix for rigs

This commit is contained in:
makearmy 2025-09-29 20:56:46 -04:00
parent e252a84862
commit 74f100acf0

View file

@ -37,14 +37,14 @@ export async function GET(req: Request) {
"date_updated", "date_updated",
].join(","); ].join(",");
// ✅ filter on the relation's id
const path = const path =
`/items/user_rigs?filter[owner][_eq]=${encodeURIComponent(me.id)}` + `/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}`;
// dxGET already returns the unwrapped `data` array const rows = await dxGET<any[]>(path, bearer); // dxGET returns unwrapped `data`
const rows = await dxGET<any[]>(path, bearer);
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,13 +101,13 @@ 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");
// ensure the rig belongs to the current user // ✅ fetch owner.id for a precise comparison
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<any>( const rig = await dxGET<{ id: string; owner?: { id?: string } }>(
`/items/user_rigs/${encodeURIComponent(id)}?fields=id,owner`, `/items/user_rigs/${encodeURIComponent(id)}?fields=id,owner.id`,
bearer bearer
); );
if (!rig || String(rig.owner) !== String(me.id)) { if (!rig || String(rig.owner?.id) !== String(me.id)) {
return bad("Not your rig", 403); return bad("Not your rig", 403);
} }