added user rig submission, logout | initial

This commit is contained in:
makearmy 2025-09-26 14:18:24 -04:00
parent 9261fbc165
commit b341a3675e
8 changed files with 635 additions and 420 deletions

View file

@ -0,0 +1,76 @@
// app/api/my/rigs/[id]/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/:id] Missing DIRECTUS_URL");
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, ctx: any) {
try {
const id = ctx?.params?.id;
const fields = [
"id","name","rig_type","notes","meta",
"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/${id}?fields=${encodeURIComponent(fields)}`);
return NextResponse.json({ ok: true, data });
} catch (e: any) {
const msg = e?.message || "Load failed";
const code = msg.includes("Not authenticated") ? 401 : (msg.includes("404") ? 404 : 500);
return NextResponse.json({ error: msg }, { status: code });
}
}
export async function PATCH(req: NextRequest, ctx: any) {
try {
const id = ctx?.params?.id;
const body = await req.json();
const { data } = await df(`/items/rigs/${id}`, { method: "PATCH", body: JSON.stringify(body) });
return NextResponse.json({ ok: true, id: data?.id ?? id });
} catch (e: any) {
const msg = e?.message || "Update failed";
return NextResponse.json({ error: msg }, { status: msg.includes("Not authenticated") ? 401 : 500 });
}
}
export async function DELETE(_req: NextRequest, ctx: any) {
try {
const id = ctx?.params?.id;
await df(`/items/rigs/${id}`, { method: "DELETE" });
return NextResponse.json({ ok: true });
} catch (e: any) {
const msg = e?.message || "Delete failed";
return NextResponse.json({ error: msg }, { status: msg.includes("Not authenticated") ? 401 : 500 });
}
}

83
app/api/my/rigs/route.ts Normal file
View file

@ -0,0 +1,83 @@
// 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 });
}
}