58 lines
1.8 KiB
TypeScript
58 lines
1.8 KiB
TypeScript
"use server";
|
|
|
|
import { revalidatePath } from "next/cache";
|
|
import { UserRole } from "@prisma/client";
|
|
import { z } from "zod";
|
|
|
|
import { requireRole } from "@/lib/auth-utils";
|
|
import { prisma } from "@/lib/prisma";
|
|
import { staffAction } from "../personal/actions";
|
|
|
|
// The staff service creates the person, profile, account and mail job in one transaction.
|
|
export async function createEducator(rawPayload: unknown) {
|
|
const parsed = z.record(z.string(), z.unknown()).safeParse(rawPayload);
|
|
if (!parsed.success) return { error: "Ungültige Eingabedaten." };
|
|
return staffAction("educatorCreate", parsed.data);
|
|
}
|
|
|
|
export async function updateEducator(id: string, rawPayload: unknown) {
|
|
const parsed = z.record(z.string(), z.unknown()).safeParse(rawPayload);
|
|
if (!parsed.success) return { error: "Ungültige Eingabedaten." };
|
|
return staffAction("educatorUpdate", { ...parsed.data, educatorId: id });
|
|
}
|
|
|
|
export async function resendEducatorInvitation(educatorId: string) {
|
|
return staffAction("educatorInvite", { educatorId });
|
|
}
|
|
|
|
export async function deleteEducator(id: string) {
|
|
const session = await requireRole([UserRole.ADMIN]);
|
|
const kitaId = session.user.kitaId;
|
|
if (!kitaId) {
|
|
return { error: "Kein Mandant zugeordnet." };
|
|
}
|
|
|
|
try {
|
|
const profile = await prisma.staffProfile.findFirst({
|
|
where: { educatorId: id, kitaId },
|
|
});
|
|
if (profile)
|
|
return {
|
|
error:
|
|
"Diese Fachkraft hat ein Personalprofil. Bitte auf inaktiv setzen, damit Zeiten und Konten erhalten bleiben.",
|
|
};
|
|
await prisma.educator.delete({
|
|
where: {
|
|
id,
|
|
kitaId,
|
|
},
|
|
});
|
|
|
|
revalidatePath("/dashboard/erzieher");
|
|
return { success: true };
|
|
} catch (error) {
|
|
console.error("Fehler beim Löschen:", error);
|
|
return { error: "Ein Fehler ist aufgetreten." };
|
|
}
|
|
}
|