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 (