83 lines
3.4 KiB
TypeScript
83 lines
3.4 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!;
|
|
if (!BASE) console.warn("[my/rigs] Missing DIRECTUS_URL");
|
|
|
|
// Pull the user's Directus access token from cookies
|
|
function bearerFromCookies() {
|
|
const at = cookies().get("ma_at")?.value;
|
|
if (!at) throw new Error("Not authenticated");
|
|
return `Bearer ${at}`;
|
|
}
|
|
|
|
async function df(path: string, init?: RequestInit) {
|
|
const res = await fetch(`${BASE}${path}`, {
|
|
...init,
|
|
headers: {
|
|
Accept: "application/json",
|
|
Authorization: bearerFromCookies(),
|
|
"Content-Type": "application/json",
|
|
...(init?.headers || {}),
|
|
},
|
|
cache: "no-store",
|
|
});
|
|
const text = await res.text();
|
|
let json: any = null;
|
|
try { json = text ? JSON.parse(text) : null; } catch {}
|
|
if (!res.ok) throw new Error(`Directus error ${res.status}: ${text || res.statusText}`);
|
|
return json ?? {};
|
|
}
|
|
|
|
export async function GET(_req: NextRequest) {
|
|
try {
|
|
// Ownership is enforced by Directus policy (owner = $CURRENT_USER)
|
|
const fields = [
|
|
"id","name","rig_type",
|
|
"laser_source.id","laser_source.make","laser_source.model",
|
|
"laser_scan_lens.id","laser_scan_lens.field_size","laser_scan_lens.f_number",
|
|
"laser_focus_lens.id","laser_focus_lens.name",
|
|
"laser_scan_lens_apt.id","laser_scan_lens_apt.name",
|
|
"laser_scan_lens_exp.id","laser_scan_lens_exp.multiplier",
|
|
"laser_software.id","laser_software.name",
|
|
"date_created","date_updated"
|
|
].join(",");
|
|
|
|
const { data } = await df(`/items/rigs?fields=${encodeURIComponent(fields)}&limit=200&sort=-date_updated`);
|
|
return NextResponse.json({ ok: true, data });
|
|
} catch (e: any) {
|
|
const msg = e?.message || "Failed to load rigs";
|
|
return NextResponse.json({ error: msg }, { status: msg.includes("Not authenticated") ? 401 : 500 });
|
|
}
|
|
}
|
|
|
|
export async function POST(req: NextRequest) {
|
|
try {
|
|
const body = await req.json();
|
|
|
|
// Minimal validation
|
|
if (!body?.name) return NextResponse.json({ error: "Missing name" }, { status: 400 });
|
|
if (!body?.rig_type) return NextResponse.json({ error: "Missing rig_type" }, { status: 400 });
|
|
|
|
// owner is set by Directus preset (owner: $CURRENT_USER)
|
|
const payload = {
|
|
name: body.name,
|
|
rig_type: body.rig_type, // "fiber" | "co2_galvo" | "co2_gantry" | "uv"
|
|
laser_source: body.laser_source ?? null,
|
|
laser_scan_lens: body.laser_scan_lens ?? null,
|
|
laser_focus_lens: body.laser_focus_lens ?? null,
|
|
laser_scan_lens_apt: body.laser_scan_lens_apt ?? null,
|
|
laser_scan_lens_exp: body.laser_scan_lens_exp ?? null,
|
|
laser_software: body.laser_software ?? null,
|
|
notes: body.notes ?? null,
|
|
meta: body.meta ?? null, // future: measured focal distance, spot size, etc.
|
|
};
|
|
|
|
const { data } = await df(`/items/rigs`, { method: "POST", body: JSON.stringify(payload) });
|
|
return NextResponse.json({ ok: true, id: data?.id });
|
|
} catch (e: any) {
|
|
const msg = e?.message || "Create failed";
|
|
return NextResponse.json({ error: msg }, { status: msg.includes("Not authenticated") ? 401 : 500 });
|
|
}
|
|
}
|