feat: add CRUD forms with Server Actions for establishments, users, classes, students
This commit is contained in:
@@ -0,0 +1,38 @@
|
||||
'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");
|
||||
}
|
||||
Reference in New Issue
Block a user