import { auth } from "@/auth"; import { db } from "@/lib/db"; import { hasPermission } from "@/lib/permissions"; import { CREWING_ENABLED } from "@/lib/feature-flags"; import { canCancel } from "@/lib/requisition-state-machine"; import { redirect, notFound } from "next/navigation"; import Link from "next/link"; import { ArrowLeft } from "lucide-react"; import { Badge } from "@/components/ui/badge"; import { WithdrawRequisitionButton } from "./withdraw-button"; import { STATUS_VARIANT, STATUS_LABEL, REASON_LABEL, ageLabel } from "../requisition-ui"; import type { Metadata } from "next"; export const metadata: Metadata = { title: "Requisition" }; export default async function RequisitionDetailPage({ params, }: { params: Promise<{ id: string }>; }) { if (!CREWING_ENABLED) notFound(); const session = await auth(); if (!session?.user) redirect("/login"); if (!hasPermission(session.user.role, "view_requisitions")) redirect("/dashboard"); const { id } = await params; const req = await db.requisition.findUnique({ where: { id }, include: { rank: { select: { name: true, code: true } }, vessel: { select: { name: true } }, site: { select: { name: true } }, raisedBy: { select: { name: true } }, sourceReliefRequest: { select: { id: true, requestedBy: { select: { name: true } } } }, _count: { select: { applications: true } }, }, }); if (!req) notFound(); const location = req.vessel?.name ?? req.site?.name ?? "—"; const canWithdraw = hasPermission(session.user.role, "cancel_requisition") && canCancel(req.status, session.user.role); const details: [string, string][] = [ ["Requisition", req.code], ["Rank", `${req.rank.name} (${req.rank.code})`], ["Vessel / site", location], ["Reason", REASON_LABEL[req.reason]], ["Raised by", req.autoRaised ? "System (auto-raised)" : req.raisedBy?.name ?? "—"], ["Raised", `${ageLabel(req.createdAt.toISOString())} ago`], ["Needed by", req.neededBy ? req.neededBy.toLocaleDateString() : "—"], ]; if (req.status === "CANCELLED" && req.cancellationReason) { details.push(["Withdrawn", req.cancellationReason]); } return (
{req.code} · {REASON_LABEL[req.reason]} · {ageLabel(req.createdAt.toISOString())} ago
Notes
{req.notes}
{req._count.applications}
candidate{req._count.applications === 1 ? "" : "s"} in the pipeline
Open recruitment pipeline →