import { auth } from "@/auth"; import { db } from "@/lib/db"; import { redirect } from "next/navigation"; import Link from "next/link"; import { formatCurrency, formatDate } from "@/lib/utils"; import { PoStatusBadge } from "@/components/po/po-status-badge"; import type { Metadata } from "next"; export const metadata: Metadata = { title: "Closed Purchase Orders" }; export default async function MyOrdersPage() { const session = await auth(); if (!session?.user) redirect("/login"); const { role, id: userId } = session.user; if (!["TECHNICAL", "MANNING", "MANAGER", "SUPERUSER"].includes(role)) redirect("/dashboard"); const closed = await db.purchaseOrder.findMany({ where: { submitterId: userId, status: { in: ["MGR_APPROVED", "SENT_FOR_PAYMENT", "PAID_DELIVERED", "CLOSED", "REJECTED"] }, }, orderBy: { updatedAt: "desc" }, include: { vessel: { select: { name: true } }, site: { select: { name: true } }, account: { select: { name: true, code: true } }, actions: { where: { actionType: { in: ["EDITS_REQUESTED", "REJECTED", "APPROVED", "APPROVED_WITH_NOTE"] }, note: { not: null }, }, orderBy: { createdAt: "desc" }, take: 1, select: { actor: { select: { name: true } } }, }, }, }); return (

Closed Purchase Orders

+ New PO
{closed.length === 0 && (

No closed purchase orders yet.

View active orders on the dashboard →
)}
); } type PoRow = { id: string; poNumber: string; title: string; status: import("@prisma/client").POStatus; totalAmount: import("@prisma/client").Prisma.Decimal; vessel: { name: string } | null; site: { name: string } | null; account: { name: string; code: string }; updatedAt: Date; managerNote: string | null; actions: { actor: { name: string } }[]; }; function PoTable({ title, rows, className = "" }: { title: string; rows: PoRow[]; className?: string }) { if (rows.length === 0) return null; return (

{title}

{rows.map((po) => ( ))}
PO Number Title Cost Centre Status Amount Updated
{po.poNumber} {po.title} {po.managerNote && (

{po.actions[0]?.actor.name ? `${po.actions[0].actor.name}: ` : "Note: "}{po.managerNote}

)}
{po.vessel?.name ?? po.site?.name ?? "—"} {formatCurrency(Number(po.totalAmount))} {formatDate(po.updatedAt)}
); }