import { auth } from "@/auth"; import { db } from "@/lib/db"; import { redirect } from "next/navigation"; import Link from "next/link"; import { formatCurrency, formatDate, PO_STATUS_LABELS, PO_STATUS_VARIANTS } from "@/lib/utils"; import { PoStatusBadge } from "@/components/po/po-status-badge"; import type { Metadata } from "next"; export const metadata: Metadata = { title: "My 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 pos = await db.purchaseOrder.findMany({ where: { submitterId: userId }, orderBy: { updatedAt: "desc" }, include: { vessel: { select: { name: true } }, account: { select: { name: true, code: true } }, }, }); const open = pos.filter((p) => ["DRAFT", "SUBMITTED", "MGR_REVIEW", "VENDOR_ID_PENDING", "EDITS_REQUESTED"].includes(p.status) ); const closed = pos.filter((p) => ["MGR_APPROVED", "SENT_FOR_PAYMENT", "PAID_DELIVERED", "CLOSED", "REJECTED"].includes(p.status) ); return (

My Purchase Orders

+ New PO
{closed.length > 0 && } {pos.length === 0 && (

You haven't raised any purchase orders yet.

Create your first PO →
)}
); } type PoRow = { id: string; poNumber: string; title: string; status: import("@prisma/client").POStatus; totalAmount: import("@prisma/client").Prisma.Decimal; vessel: { name: string }; account: { name: string; code: string }; updatedAt: Date; managerNote: string | null; }; 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 && (

Note: {po.managerNote}

)}
{po.vessel.name} {formatCurrency(Number(po.totalAmount))} {formatDate(po.updatedAt)}
); }