build fixes
This commit is contained in:
parent
e3d59b5512
commit
26fa64790d
2 changed files with 367 additions and 580 deletions
|
|
@ -1,6 +1,6 @@
|
|||
// app/api/submit/settings/route.ts
|
||||
import { NextResponse } from "next/server";
|
||||
import { uploadFile, createSettingsItem, bytesFromMB, dxGET, dxPATCH } from "@/lib/directus";
|
||||
import { uploadFile, bytesFromMB, dxGET, dxPATCH, dxPOST } 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
|
||||
*
|
||||
* Also supports editing:
|
||||
* Edit:
|
||||
* 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"; // transport mode, not create/edit
|
||||
mode: "json" | "multipart";
|
||||
body: any;
|
||||
photoFile: File | null;
|
||||
screenFile: File | null;
|
||||
|
|
@ -174,12 +174,18 @@ 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; // exact key: 'laser_soft'
|
||||
const repeat_all = num(body?.repeat_all, null); // universally applicable
|
||||
const laser_soft = body?.laser_soft ?? null;
|
||||
const repeat_all = num(body?.repeat_all, null);
|
||||
|
||||
// Upload / accept existing file ids
|
||||
let photo_id: string | null = body?.photo ?? null;
|
||||
|
|
@ -233,6 +239,7 @@ export async function POST(req: Request) {
|
|||
const basePayload: Record<string, any> = {
|
||||
setting_title,
|
||||
setting_notes,
|
||||
|
||||
// Ownership & attribution
|
||||
owner: meId || null, // M2O to directus_users
|
||||
uploader, // string mirror of username
|
||||
|
|
@ -241,6 +248,7 @@ export async function POST(req: Request) {
|
|||
laser_soft,
|
||||
repeat_all,
|
||||
|
||||
// material / optics
|
||||
mat,
|
||||
mat_coat,
|
||||
mat_color,
|
||||
|
|
@ -248,8 +256,15 @@ export async function POST(req: Request) {
|
|||
mat_thickness,
|
||||
source,
|
||||
lens,
|
||||
|
||||
// CO₂ galvo extras
|
||||
lens_conf,
|
||||
lens_apt,
|
||||
lens_exp,
|
||||
|
||||
focus,
|
||||
|
||||
// repeaters
|
||||
fill_settings: fills,
|
||||
line_settings: lines,
|
||||
raster_settings: rasters,
|
||||
|
|
@ -258,6 +273,37 @@ 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;
|
||||
|
|
@ -266,8 +312,14 @@ export async function POST(req: Request) {
|
|||
basePayload.submitted_via = "makearmy-app";
|
||||
basePayload.submitted_at = nowIso;
|
||||
|
||||
const { data } = await createSettingsItem(target, basePayload, bearer);
|
||||
return NextResponse.json({ ok: true, id: data.id });
|
||||
// 🔑 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 });
|
||||
}
|
||||
|
||||
// EDIT mode
|
||||
|
|
|
|||
File diff suppressed because it is too large
Load diff
Loading…
Add table
Add a link
Reference in a new issue