Files
edubox/server/app/dashboard/students/[id]/actions.ts
T
root 479a8de858 fix: activation via connexion WS principale, cascade delete student→nodes, lien fiche étudiant
- agent/websocket.go: expose sendMessage() + notifyUI() pour broadcaster
  les résultats d'activation à tous les clients UI connectés
- agent/ui.go: supprime forwardActivation(), utilise sendMessage() sur
  la connexion WS principale au lieu d'une connexion temporaire
- agent/activation.go: ajoute os.MkdirAll avant l'écriture d'activation.json
- server/prisma/schema.prisma: onDelete Cascade sur Node→Student
- server/app/dashboard/students/page.tsx: nom cliquable vers fiche détail
- server/app/dashboard/students/[id]/actions.ts: deleteMany → delete
2026-06-06 22:41:15 +00:00

64 lines
1.7 KiB
TypeScript

"use server";
import { prisma } from "@/lib/prisma";
import { getServerSession } from "next-auth/next";
import { authOptions } from "@/lib/auth-config";
import { redirect } from "next/navigation";
function generateCode(): string {
const chars = "ABCDEFGHJKLMNPQRSTUVWXYZ23456789";
let code = "";
for (let i = 0; i < 6; i++) {
code += chars.charAt(Math.floor(Math.random() * chars.length));
}
return code;
}
export async function deleteStudent(formData: FormData) {
const session = await getServerSession(authOptions);
if (!session?.user) redirect("/login");
if (!session.user.establishmentId && session.user.role !== "superadmin") redirect("/dashboard");
const id = formData.get("id") as string;
if (!id) return;
const establishmentId = session.user.establishmentId;
const student = await prisma.student.findFirst({
where: {
id,
class: establishmentId ? { establishmentId } : undefined,
},
});
if (!student) return;
await prisma.student.delete({ where: { id } });
redirect("/dashboard/students");
}
export async function generateActivationCodeAction(formData: FormData) {
const session = await getServerSession(authOptions);
if (!session?.user) redirect("/login");
if (!session.user.establishmentId && session.user.role !== "superadmin") redirect("/dashboard");
const id = formData.get("id") as string;
if (!id) return;
const establishmentId = session.user.establishmentId;
const student = await prisma.student.findFirst({
where: {
id,
class: establishmentId ? { establishmentId } : undefined,
},
});
if (!student) return;
await prisma.student.update({
where: { id },
data: { activationCode: generateCode() },
});
redirect(`/dashboard/students/${id}`);
}