100 lines
4.5 KiB
TypeScript
100 lines
4.5 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";
|
|
|
|
type ActionResult = { ok: true } | { error: string };
|
|
|
|
const companySchema = z.object({
|
|
name: z.string().min(1, "Company name is required"),
|
|
gstNumber: z.string().optional(),
|
|
address: z.string().optional(),
|
|
telephone: z.string().optional(),
|
|
mobile: z.string().optional(),
|
|
email: z.string().email("Invalid email").optional().or(z.literal("")),
|
|
invoiceEmail: z.string().email("Invalid invoice email").optional().or(z.literal("")),
|
|
invoiceAddress: z.string().optional(),
|
|
});
|
|
|
|
export async function createCompany(formData: FormData): Promise<ActionResult> {
|
|
const session = await auth();
|
|
if (!session?.user || !hasPermission(session.user.role, "manage_vessels_accounts")) {
|
|
return { error: "Unauthorized" };
|
|
}
|
|
|
|
const parsed = companySchema.safeParse({
|
|
name: formData.get("name"),
|
|
gstNumber: (formData.get("gstNumber") as string) || undefined,
|
|
address: (formData.get("address") as string) || undefined,
|
|
telephone: (formData.get("telephone") as string) || undefined,
|
|
mobile: (formData.get("mobile") as string) || undefined,
|
|
email: (formData.get("email") as string) || undefined,
|
|
invoiceEmail: (formData.get("invoiceEmail") as string) || undefined,
|
|
invoiceAddress: (formData.get("invoiceAddress") as string) || undefined,
|
|
});
|
|
if (!parsed.success) return { error: parsed.error.errors[0]?.message ?? "Validation failed" };
|
|
|
|
const { name, gstNumber, address, telephone, mobile, email, invoiceEmail, invoiceAddress } = parsed.data;
|
|
await db.company.create({
|
|
data: { name, gstNumber: gstNumber ?? null, address: address ?? null, telephone: telephone ?? null, mobile: mobile ?? null, email: email || null, invoiceEmail: invoiceEmail || null, invoiceAddress: invoiceAddress ?? null },
|
|
});
|
|
revalidatePath("/admin/companies");
|
|
return { ok: true };
|
|
}
|
|
|
|
export async function updateCompany(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: "Company ID is required" };
|
|
|
|
const parsed = companySchema.safeParse({
|
|
name: formData.get("name"),
|
|
gstNumber: (formData.get("gstNumber") as string) || undefined,
|
|
address: (formData.get("address") as string) || undefined,
|
|
telephone: (formData.get("telephone") as string) || undefined,
|
|
mobile: (formData.get("mobile") as string) || undefined,
|
|
email: (formData.get("email") as string) || undefined,
|
|
invoiceEmail: (formData.get("invoiceEmail") as string) || undefined,
|
|
invoiceAddress: (formData.get("invoiceAddress") as string) || undefined,
|
|
});
|
|
if (!parsed.success) return { error: parsed.error.errors[0]?.message ?? "Validation failed" };
|
|
|
|
const { name, gstNumber, address, telephone, mobile, email, invoiceEmail, invoiceAddress } = parsed.data;
|
|
await db.company.update({
|
|
where: { id },
|
|
data: { name, gstNumber: gstNumber ?? null, address: address ?? null, telephone: telephone ?? null, mobile: mobile ?? null, email: email || null, invoiceEmail: invoiceEmail || null, invoiceAddress: invoiceAddress ?? null },
|
|
});
|
|
revalidatePath("/admin/companies");
|
|
return { ok: true };
|
|
}
|
|
|
|
export async function deleteCompany(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: { companyId: id } });
|
|
if (inUse) return { error: "Cannot delete: company is referenced in purchase orders." };
|
|
|
|
await db.company.delete({ where: { id } });
|
|
revalidatePath("/admin/companies");
|
|
return { ok: true };
|
|
}
|
|
|
|
export async function toggleCompanyActive(id: string): Promise<ActionResult> {
|
|
const session = await auth();
|
|
if (!session?.user || !hasPermission(session.user.role, "manage_vessels_accounts")) {
|
|
return { error: "Unauthorized" };
|
|
}
|
|
const company = await db.company.findUnique({ where: { id }, select: { isActive: true } });
|
|
if (!company) return { error: "Company not found" };
|
|
await db.company.update({ where: { id }, data: { isActive: !company.isActive } });
|
|
revalidatePath("/admin/companies");
|
|
return { ok: true };
|
|
}
|