From 031ada530b175d855416aec869c9ad983d535232 Mon Sep 17 00:00:00 2001 From: Tim Howitz Date: Mon, 2 Jun 2025 08:44:55 +0100 Subject: [PATCH] Added logging of bulk pallet --- src/app/api/warehouse/log-bulk/route.ts | 30 +++++++++++++++++++++++++ src/app/warehouse/page.tsx | 21 ++++++++++------- 2 files changed, 43 insertions(+), 8 deletions(-) create mode 100644 src/app/api/warehouse/log-bulk/route.ts diff --git a/src/app/api/warehouse/log-bulk/route.ts b/src/app/api/warehouse/log-bulk/route.ts new file mode 100644 index 0000000..97aaf8c --- /dev/null +++ b/src/app/api/warehouse/log-bulk/route.ts @@ -0,0 +1,30 @@ +import { NextRequest, NextResponse } from "next/server"; +import { apiAuthMiddleware } from "@utils/apiAuthMiddleware"; +import { prisma } from "@utils/prisma"; + +export async function POST(request: NextRequest) { + try { + const body = await request.json(); + const { palletNote, warehouseLocation } = body; + + const authResult = await apiAuthMiddleware(); + if ("user" in authResult === false) return authResult; // Handle error response + + const { user } = authResult; + + if (!palletNote || !warehouseLocation) { + return NextResponse.json({ error: "Missing fields" }, { status: 400 }); + } + + await prisma.pallet.create({ + data: { + palletNote, + warehouseArea: warehouseLocation, + }, + }); + + return NextResponse.json({ message: "Pallet logged successfully" }, { status: 200 }); + } catch (e: any) { + return NextResponse.json({ error: e.message }, { status: 500 }); + } +} diff --git a/src/app/warehouse/page.tsx b/src/app/warehouse/page.tsx index aaf28ac..4523f16 100644 --- a/src/app/warehouse/page.tsx +++ b/src/app/warehouse/page.tsx @@ -237,7 +237,7 @@ function LogModal({ onClose }: { onClose: () => void }) { // Modal Component for Bulk Logging function BulkLogModal({ onClose }: { onClose: () => void }) { const [palletNote, setPalletNote] = useState(""); - const [storageLocation, setStorageLocation] = useState(""); + const [warehouseLocation, setWarehouseLocation] = useState(""); const [error, setError] = useState(""); const [isSubmitting, setIsSubmitting] = useState(false); @@ -247,22 +247,27 @@ function BulkLogModal({ onClose }: { onClose: () => void }) { } }; - const handleLog = async () => { - if (!palletNote || !storageLocation) { + async function handleLog() { + if (!palletNote || !warehouseLocation) { setError("All fields are required."); return; } setIsSubmitting(true); try { - await new Promise((resolve) => setTimeout(resolve, 500)); // Simulated API call - alert(`Logged bulk pallet to storage: ${storageLocation}`); + await axios.post("/api/warehouse/log-bulk", { + palletNote, + warehouseLocation, + }); + + // todo replace with better alert + alert(`Logged bulk pallet to storage: ${warehouseLocation}`); onClose(); } catch { setError("Failed to log pallet. Please try again."); } finally { setIsSubmitting(false); } - }; + } return (
void }) { setStorageLocation(e.target.value)} + value={warehouseLocation} + onChange={(e) => setWarehouseLocation(e.target.value)} className="w-full p-2 border border-neutral-300 rounded-md placeholder-neutral-400 focus:ring-2 focus:ring-blue-500" aria-label="Storage Location" disabled={isSubmitting}