user_rigs collection update
This commit is contained in:
parent
344e0fcafb
commit
ae1486636e
2 changed files with 231 additions and 396 deletions
|
|
@ -1,76 +1,48 @@
|
|||
// app/api/my/rigs/[id]/route.ts
|
||||
import { NextRequest, NextResponse } from "next/server";
|
||||
import { cookies } from "next/headers";
|
||||
import { directusFetch } from "@/lib/directus";
|
||||
|
||||
const BASE = process.env.DIRECTUS_URL!;
|
||||
if (!BASE) console.warn("[my/rigs/:id] Missing DIRECTUS_URL");
|
||||
const BASE_COLLECTION = "user_rigs";
|
||||
|
||||
function bearerFromCookies() {
|
||||
const at = cookies().get("ma_at")?.value;
|
||||
async function bearerFromCookies() {
|
||||
// In Next 15, types may represent `cookies()` as async—await it to satisfy TS.
|
||||
const store = await cookies();
|
||||
const at = store.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) {
|
||||
export async function PATCH(req: NextRequest, ctx: { params: { id: string } }) {
|
||||
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 auth = await bearerFromCookies();
|
||||
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 });
|
||||
|
||||
const data = await directusFetch<{ data: any }>(`/items/${BASE_COLLECTION}/${ctx.params.id}`, {
|
||||
method: "PATCH",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
Authorization: auth, // overrides submit token in helper
|
||||
},
|
||||
body: JSON.stringify(body),
|
||||
});
|
||||
|
||||
return NextResponse.json({ ok: true, data: data.data });
|
||||
} catch (err: any) {
|
||||
return NextResponse.json({ error: err?.message || "Update failed" }, { status: 400 });
|
||||
}
|
||||
}
|
||||
|
||||
export async function DELETE(_req: NextRequest, ctx: any) {
|
||||
export async function DELETE(_req: NextRequest, ctx: { params: { id: string } }) {
|
||||
try {
|
||||
const id = ctx?.params?.id;
|
||||
await df(`/items/rigs/${id}`, { method: "DELETE" });
|
||||
const auth = await bearerFromCookies();
|
||||
|
||||
await directusFetch(`/items/${BASE_COLLECTION}/${ctx.params.id}`, {
|
||||
method: "DELETE",
|
||||
headers: { Authorization: auth },
|
||||
});
|
||||
|
||||
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 });
|
||||
} catch (err: any) {
|
||||
return NextResponse.json({ error: err?.message || "Delete failed" }, { status: 400 });
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue