Users: employeeId auto-generated from role prefix (TCH/MAN/ACC/MGR/SUP/AUD/ADM) followed by next sequential number; shown read-only in edit form, removed from create form. Cost Centres: new code field (SITE-001 ...) added to Vessel model with migration + backfill; auto-generated on create, read-only in edit. Vendors and Accounts: code/vendorId inputs pre-filled with the next suggested ID (VND-001, ACC-001) from the server page; user can override with any PREFIX-NUMBER format, validated by regex. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
78 lines
2.8 KiB
TypeScript
78 lines
2.8 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"),
|
|
});
|
|
|
|
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"),
|
|
});
|
|
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 } });
|
|
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"),
|
|
});
|
|
if (!parsed.success) return { error: parsed.error.errors[0]?.message ?? "Validation failed" };
|
|
|
|
await db.vessel.update({ where: { id }, data: { name: parsed.data.name } });
|
|
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 };
|
|
}
|