schema improvements

This commit is contained in:
makearmy 2025-10-05 17:09:39 -04:00
parent eb962b8283
commit 4ef3160515
2 changed files with 158 additions and 112 deletions

View file

@ -1,6 +1,6 @@
// app/api/submit/settings/route.ts
import { NextResponse } from "next/server";
import { uploadFile, bytesFromMB, dxGET, dxPATCH, dxPOST } from "@/lib/directus";
import { uploadFile, createSettingsItem, bytesFromMB, dxGET, dxPATCH } from "@/lib/directus";
import { requireBearer } from "@/app/api/_lib/auth";
/**
@ -18,7 +18,7 @@ import { requireBearer } from "@/app/api/_lib/auth";
* - settings_co2gal
* - settings_uv
*
* Edit:
* Also supports editing:
* Body must include { mode: "edit", submission_id: string|number }
* We PATCH via filter[submission_id][_eq] and owner = current user.
* */
@ -53,7 +53,7 @@ function num(v: any, fallback: number | null = null) {
}
type ReadResult = {
mode: "json" | "multipart";
mode: "json" | "multipart"; // transport mode, not create/edit
body: any;
photoFile: File | null;
screenFile: File | null;
@ -140,8 +140,7 @@ export async function POST(req: Request) {
}
// Create vs Edit
const op: "create" | "edit" =
body?.mode === "edit" ? "edit" : "create";
const op: "create" | "edit" = body?.mode === "edit" ? "edit" : "create";
// Required basics
const setting_title = String(body?.setting_title || "").trim();
@ -174,18 +173,17 @@ export async function POST(req: Request) {
const mat_thickness = num(body?.mat_thickness, null);
const source = body?.source ?? null;
const lens = body?.lens ?? null;
// CO₂ galvo extras (relations)
const lens_conf = body?.lens_conf ?? null;
const lens_apt = body?.lens_apt ?? null;
const lens_exp = body?.lens_exp ?? null;
const focus = num(body?.focus, null);
const setting_notes = String(body?.setting_notes || "").trim();
// Shared string fields
const laser_soft = body?.laser_soft ?? null;
const repeat_all = num(body?.repeat_all, null);
const laser_soft = body?.laser_soft ?? null; // exact key: 'laser_soft'
const repeat_all = num(body?.repeat_all, null); // universally applicable
// CO2 lens extras (may be null on non-co2)
const lens_conf = body?.lens_conf ?? null;
const lens_apt = body?.lens_apt ?? null;
const lens_exp = body?.lens_exp ?? null;
// Upload / accept existing file ids
let photo_id: string | null = body?.photo ?? null;
@ -241,14 +239,13 @@ export async function POST(req: Request) {
setting_notes,
// Ownership & attribution
owner: meId || null, // M2O to directus_users
uploader, // string mirror of username
owner: meId || null, // M2O to directus_users
uploader, // string mirror of username
// exact keys
laser_soft,
repeat_all,
// material / optics
mat,
mat_coat,
mat_color,
@ -256,15 +253,13 @@ export async function POST(req: Request) {
mat_thickness,
source,
lens,
focus,
// CO₂ galvo extras
// CO2-specific lens extras
lens_conf,
lens_apt,
lens_exp,
focus,
// repeaters
fill_settings: fills,
line_settings: lines,
raster_settings: rasters,
@ -273,37 +268,6 @@ export async function POST(req: Request) {
last_modified_date: nowIso,
};
// ── Per-target requireds (server-side enforcement) ─────────
if (target === "settings_co2gal") {
const missing: string[] = [];
const req = {
setting_title,
uploader,
photo: op === "create" ? photo_id : true, // on edit, can be omitted
source,
lens,
lens_conf,
lens_apt,
lens_exp,
focus: Number.isFinite(focus as number),
mat,
mat_coat,
mat_color,
mat_opacity,
laser_soft,
repeat_all: Number.isFinite(repeat_all as number),
};
for (const [k, v] of Object.entries(req)) {
if (!v) missing.push(k);
}
if (missing.length) {
return NextResponse.json(
{ error: `Missing required: ${missing.join(", ")}` },
{ status: 400 }
);
}
}
if (op === "create") {
// Create-only fields
basePayload.photo = photo_id;
@ -312,14 +276,9 @@ export async function POST(req: Request) {
basePayload.submitted_via = "makearmy-app";
basePayload.submitted_at = nowIso;
// 🔑 Directus requires { data: {...} }
const res = await dxPOST<{ data: { id: string | number } }>(
`/items/${target}`,
bearer,
{ data: basePayload }
);
return NextResponse.json({ ok: true, id: res?.data?.id });
// Helper is expected to wrap as { data: … } internally
const { data } = await createSettingsItem(target, basePayload, bearer);
return NextResponse.json({ ok: true, id: data.id });
}
// EDIT mode
@ -342,10 +301,11 @@ export async function POST(req: Request) {
// enforce owner matches current user (works whether owner is id or M2O)
qs.set("filter[_and][1][owner][_eq]", String(meId));
// ⬇⬇⬇ Directus expects { data: {...} } here (this fixes the 400 "data is required")
const res = await dxPATCH<{ data: any[] }>(
`/items/${target}?${qs.toString()}`,
bearer,
editPayload
{ data: editPayload }
);
const updatedCount = Array.isArray(res?.data) ? res.data.length : 0;