pelagia-portal/App/app/(portal)/admin/vessels/actions.ts
Hardik cc7251e6b7 feat: Cost Centre covers vessels and sites, vessel codes, Accounting Code rename, vessel-site assignment
- Undo Vessel→Cost Centre rename in admin (admin shows "Vessel Management" again)
- Sidebar: "Cost Centres"→"Vessels", "Accounts"→"Accounting Codes"
- PO forms (new/edit/import/manager-edit) now show both Vessels (with code) and Sites in the
  Cost Centre dropdown, encoded as v:<id> / s:<id> via a costCentreRef field
- vesselId on PurchaseOrder is now nullable; siteId is set when a site is the cost centre
- History, approvals, dashboard, my-orders, payments display vessel.name ?? site.name as Cost Centre
- History and approvals cost centre filters use costCentreRef URL param supporting both types
- Admin vessel form: adds Site assignment dropdown
- Admin accounts: renamed to "Accounting Code" throughout (pages, forms, sidebar)
- PO detail and exports: "Account" label renamed to "Accounting Code"
- Site detail: "Assigned Vessels (Cost Centres)" heading; vessel detail breadcrumb fixed
- Create PO links from vessel/site detail use ?costCentreRef= param
- Export routes handle costCentreRef filter param (with legacy vesselId fallback)
- DB migration: ALTER TABLE PurchaseOrder ALTER COLUMN vesselId DROP NOT NULL
- CLAUDE.md updated with Cost Centre Model documentation

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-05-30 03:04:29 +05:30

81 lines
3 KiB
TypeScript

"use server";
import { auth } from "@/auth";
import { db } from "@/lib/db";
import { hasPermission } from "@/lib/permissions";
import { z } from "zod";
import { revalidatePath } from "next/cache";
import { nextId } from "@/lib/id-generators";
type ActionResult = { ok: true } | { error: string };
const vesselSchema = z.object({
name: z.string().min(1, "Vessel name is required"),
siteId: z.string().optional(),
});
export async function createVessel(formData: FormData): Promise<ActionResult> {
const session = await auth();
if (!session?.user || !hasPermission(session.user.role, "manage_vessels_accounts")) {
return { error: "Unauthorized" };
}
const parsed = vesselSchema.safeParse({
name: formData.get("name"),
siteId: (formData.get("siteId") as string) || undefined,
});
if (!parsed.success) return { error: parsed.error.errors[0]?.message ?? "Validation failed" };
const existingCodes = await db.vessel.findMany({ select: { code: true } });
const code = nextId("SITE", existingCodes.map((v) => v.code));
await db.vessel.create({ data: { name: parsed.data.name, code, siteId: parsed.data.siteId ?? null } });
revalidatePath("/admin/vessels");
return { ok: true };
}
export async function updateVessel(formData: FormData): Promise<ActionResult> {
const session = await auth();
if (!session?.user || !hasPermission(session.user.role, "manage_vessels_accounts")) {
return { error: "Unauthorized" };
}
const id = formData.get("id") as string;
if (!id) return { error: "Vessel ID is required" };
const parsed = vesselSchema.safeParse({
name: formData.get("name"),
siteId: (formData.get("siteId") as string) || undefined,
});
if (!parsed.success) return { error: parsed.error.errors[0]?.message ?? "Validation failed" };
await db.vessel.update({ where: { id }, data: { name: parsed.data.name, siteId: parsed.data.siteId ?? null } });
revalidatePath("/admin/vessels");
return { ok: true };
}
export async function deleteVessel(id: string): Promise<ActionResult> {
const session = await auth();
if (!session?.user || !hasPermission(session.user.role, "manage_vessels_accounts")) return { error: "Unauthorized" };
const inUse = await db.purchaseOrder.findFirst({ where: { vesselId: id } });
if (inUse) return { error: "Cannot delete: vessel is referenced in purchase orders. Remove those POs first." };
await db.vessel.delete({ where: { id } });
revalidatePath("/admin/vessels");
return { ok: true };
}
export async function toggleVesselActive(vesselId: string): Promise<ActionResult> {
const session = await auth();
if (!session?.user || !hasPermission(session.user.role, "manage_vessels_accounts")) {
return { error: "Unauthorized" };
}
const vessel = await db.vessel.findUnique({ where: { id: vesselId }, select: { isActive: true } });
if (!vessel) return { error: "Vessel not found" };
await db.vessel.update({ where: { id: vesselId }, data: { isActive: !vessel.isActive } });
revalidatePath("/admin/vessels");
return { ok: true };
}