57 lines
1.6 KiB
TypeScript
57 lines
1.6 KiB
TypeScript
import { notFound } from "next/navigation";
|
|
import { prisma } from "@/lib/prisma";
|
|
import FastenersClient from "./FastenersClient";
|
|
|
|
export const dynamic = "force-dynamic";
|
|
|
|
export default async function FastenersPage({ params }: { params: Promise<{ id: string }> }) {
|
|
const { id } = await params;
|
|
const project = await prisma.project.findUnique({
|
|
where: { id },
|
|
select: { id: true, code: true, name: true },
|
|
});
|
|
if (!project) notFound();
|
|
|
|
const fasteners = await prisma.fastener.findMany({
|
|
where: { projectId: id },
|
|
orderBy: [{ supplier: "asc" }, { partNumber: "asc" }],
|
|
include: {
|
|
poLines: {
|
|
select: {
|
|
qty: true,
|
|
receivedQty: true,
|
|
po: { select: { status: true } },
|
|
},
|
|
},
|
|
},
|
|
});
|
|
|
|
// Same rollup the GET /fasteners API does — keep it inline here so the
|
|
// initial SSR paint already has remaining/on-order numbers without a
|
|
// client-side round-trip.
|
|
const rows = fasteners.map((f) => {
|
|
let onOrder = 0;
|
|
let received = 0;
|
|
for (const line of f.poLines) {
|
|
if (line.po.status === "cancelled") continue;
|
|
onOrder += line.qty;
|
|
received += line.receivedQty;
|
|
}
|
|
return {
|
|
id: f.id,
|
|
partNumber: f.partNumber,
|
|
description: f.description,
|
|
qty: f.qty,
|
|
supplier: f.supplier,
|
|
unitCost: f.unitCost,
|
|
notes: f.notes,
|
|
onOrder,
|
|
received,
|
|
remaining: Math.max(0, f.qty - received),
|
|
unresolved: Math.max(0, f.qty - onOrder),
|
|
};
|
|
});
|
|
|
|
return <FastenersClient project={project} initial={rows} />;
|
|
}
|