100 lines
3.3 KiB
TypeScript
100 lines
3.3 KiB
TypeScript
// app/api/my/rigs/route.ts
|
|
import { NextRequest, NextResponse } from "next/server";
|
|
import { cookies } from "next/headers";
|
|
|
|
const BASE = (process.env.DIRECTUS_URL || "").replace(/\/$/, "");
|
|
|
|
async function bearerFromCookies() {
|
|
const store = await cookies();
|
|
const at = store.get("ma_at")?.value;
|
|
if (!at) throw new Error("Not authenticated");
|
|
return `Bearer ${at}`;
|
|
}
|
|
|
|
async function getMyUserId(bearer: string) {
|
|
const res = await fetch(`${BASE}/users/me`, {
|
|
headers: { Authorization: bearer, Accept: "application/json" },
|
|
cache: "no-store",
|
|
});
|
|
const txt = await res.text();
|
|
if (!res.ok) throw new Error(txt || res.statusText);
|
|
const j = txt ? JSON.parse(txt) : {};
|
|
return j?.data?.id as string;
|
|
}
|
|
|
|
export async function GET(_req: NextRequest) {
|
|
try {
|
|
const bearer = await bearerFromCookies();
|
|
const myId = await getMyUserId(bearer);
|
|
|
|
const fields = [
|
|
"id",
|
|
"name",
|
|
"rig_type",
|
|
"rig_type.name",
|
|
"laser_source",
|
|
"laser_focus_lens",
|
|
"laser_scan_lens",
|
|
"laser_scan_lens_apt",
|
|
"laser_scan_lens_exp",
|
|
"laser_software",
|
|
"notes",
|
|
"user_created",
|
|
"date_created",
|
|
"date_updated",
|
|
].join(",");
|
|
|
|
const url = new URL(`${BASE}/items/user_rigs`);
|
|
url.searchParams.set("fields", fields);
|
|
url.searchParams.set("sort", "-date_created");
|
|
// If you use a custom owner field, switch this to filter[owner][_eq]
|
|
url.searchParams.set("filter[user_created][_eq]", myId);
|
|
|
|
const res = await fetch(String(url), {
|
|
headers: { Authorization: bearer, Accept: "application/json" },
|
|
cache: "no-store",
|
|
});
|
|
|
|
const txt = await res.text();
|
|
if (!res.ok) return NextResponse.json({ error: txt || res.statusText }, { status: res.status });
|
|
const j = txt ? JSON.parse(txt) : { data: [] };
|
|
|
|
const data = (j.data ?? []).map((r: any) => ({
|
|
...r,
|
|
rig_type_name: r?.rig_type?.name ?? r?.rig_type_name ?? null,
|
|
}));
|
|
|
|
return NextResponse.json({ data });
|
|
} catch (e: any) {
|
|
return NextResponse.json({ error: e?.message || "Failed to list rigs" }, { status: 401 });
|
|
}
|
|
}
|
|
|
|
export async function POST(req: NextRequest) {
|
|
try {
|
|
const bearer = await bearerFromCookies();
|
|
const body = await req.json().catch(() => ({}));
|
|
|
|
// If your collection requires a custom 'owner' field, uncomment:
|
|
// const owner = await getMyUserId(bearer);
|
|
// const payload = { ...body, owner };
|
|
const payload = body;
|
|
|
|
const res = await fetch(`${BASE}/items/user_rigs`, {
|
|
method: "POST",
|
|
headers: {
|
|
Authorization: bearer,
|
|
Accept: "application/json",
|
|
"Content-Type": "application/json",
|
|
},
|
|
body: JSON.stringify(payload),
|
|
});
|
|
|
|
const txt = await res.text();
|
|
if (!res.ok) return NextResponse.json({ error: txt || res.statusText }, { status: res.status });
|
|
const j = txt ? JSON.parse(txt) : {};
|
|
return NextResponse.json(j);
|
|
} catch (e: any) {
|
|
return NextResponse.json({ error: e?.message || "Failed to create rig" }, { status: 400 });
|
|
}
|
|
}
|