39 lines
1.0 KiB
TypeScript
39 lines
1.0 KiB
TypeScript
'use server'
|
|
|
|
import { prisma } from "@/lib/prisma";
|
|
import { redirect } from "next/navigation";
|
|
import { revalidatePath } from "next/cache";
|
|
import { z } from "zod";
|
|
|
|
const updateSchema = z.object({
|
|
plan: z.enum(["trial", "starter", "standard", "premium"]),
|
|
status: z.enum(["active", "suspended", "expired"]),
|
|
});
|
|
|
|
export async function updateSubscription(establishmentId: string, formData: FormData) {
|
|
const plan = formData.get("plan") as string;
|
|
const status = formData.get("status") as string;
|
|
|
|
const parsed = updateSchema.safeParse({ plan, status });
|
|
if (!parsed.success) {
|
|
throw new Error("Données invalides");
|
|
}
|
|
|
|
await prisma.subscription.update({
|
|
where: { establishmentId },
|
|
data: {
|
|
plan: parsed.data.plan,
|
|
status: parsed.data.status,
|
|
},
|
|
});
|
|
|
|
revalidatePath(`/superadmin/establishments/${establishmentId}`);
|
|
}
|
|
|
|
export async function deleteEstablishment(establishmentId: string) {
|
|
await prisma.establishment.delete({
|
|
where: { id: establishmentId },
|
|
});
|
|
redirect("/superadmin/establishments");
|
|
}
|