142 lines
3.6 KiB
TypeScript
142 lines
3.6 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";
|
|
|
|
const dutySchema = z.object({
|
|
name: z.string().min(2, "Name muss mindestens 2 Zeichen lang sein").max(50),
|
|
description: z.string().trim().max(2000).optional(),
|
|
});
|
|
|
|
export async function createDuty(rawPayload: unknown) {
|
|
const session = await requireRole([UserRole.ADMIN]);
|
|
const kitaId = session.user.kitaId;
|
|
if (!kitaId) {
|
|
return { error: "Kein Mandant zugeordnet." };
|
|
}
|
|
|
|
const parsed = dutySchema.safeParse(rawPayload);
|
|
|
|
if (!parsed.success) {
|
|
return { error: "Ungültige Eingabedaten." };
|
|
}
|
|
|
|
try {
|
|
await prisma.parentDuty.create({
|
|
data: {
|
|
kitaId,
|
|
name: parsed.data.name,
|
|
description: parsed.data.description,
|
|
},
|
|
});
|
|
revalidatePath("/dashboard/families");
|
|
revalidatePath("/dashboard/adressbuch");
|
|
return { success: true };
|
|
} catch (error) {
|
|
console.error("Fehler beim Erstellen des Amtes:", error);
|
|
return { error: "Ein Fehler ist aufgetreten (evtl. existiert der Name bereits)." };
|
|
}
|
|
}
|
|
|
|
export async function deleteDuty(dutyId: string) {
|
|
const session = await requireRole([UserRole.ADMIN]);
|
|
const kitaId = session.user.kitaId;
|
|
if (!kitaId) {
|
|
return { error: "Kein Mandant zugeordnet." };
|
|
}
|
|
|
|
try {
|
|
await prisma.parentDuty.delete({
|
|
where: {
|
|
id: dutyId,
|
|
kitaId,
|
|
},
|
|
});
|
|
revalidatePath("/dashboard/families");
|
|
revalidatePath("/dashboard/adressbuch");
|
|
return { success: true };
|
|
} catch (error) {
|
|
console.error("Fehler beim Löschen des Amtes:", error);
|
|
return { error: "Ein Fehler ist aufgetreten." };
|
|
}
|
|
}
|
|
|
|
export async function assignDuty(userId: string, dutyId: string) {
|
|
const session = await requireRole([UserRole.ADMIN]);
|
|
const kitaId = session.user.kitaId;
|
|
if (!kitaId) {
|
|
return { error: "Kein Mandant zugeordnet." };
|
|
}
|
|
|
|
try {
|
|
const [user, duty] = await Promise.all([
|
|
prisma.user.findFirst({
|
|
where: { id: userId, kitaId },
|
|
select: { id: true },
|
|
}),
|
|
prisma.parentDuty.findFirst({
|
|
where: { id: dutyId, kitaId },
|
|
select: { id: true },
|
|
}),
|
|
]);
|
|
|
|
if (!user || !duty) {
|
|
return { error: "Nutzer oder Amt gehört nicht zu dieser Kita." };
|
|
}
|
|
|
|
// Check if assignment already exists
|
|
const existing = await prisma.parentDutyAssignment.findUnique({
|
|
where: {
|
|
dutyId_userId: { dutyId, userId },
|
|
},
|
|
});
|
|
|
|
if (existing) {
|
|
return { error: "Der Nutzer hat dieses Amt bereits." };
|
|
}
|
|
|
|
await prisma.parentDutyAssignment.create({
|
|
data: {
|
|
kitaId,
|
|
userId: userId,
|
|
dutyId: dutyId,
|
|
},
|
|
});
|
|
|
|
revalidatePath("/dashboard/families");
|
|
revalidatePath("/dashboard/adressbuch");
|
|
return { success: true };
|
|
} catch (error) {
|
|
console.error("Fehler beim Zuweisen des Amtes:", error);
|
|
return { error: "Ein Fehler ist aufgetreten." };
|
|
}
|
|
}
|
|
|
|
export async function removeDutyAssignment(assignmentId: string) {
|
|
const session = await requireRole([UserRole.ADMIN]);
|
|
const kitaId = session.user.kitaId;
|
|
if (!kitaId) {
|
|
return { error: "Kein Mandant zugeordnet." };
|
|
}
|
|
|
|
try {
|
|
await prisma.parentDutyAssignment.delete({
|
|
where: {
|
|
id: assignmentId,
|
|
kitaId,
|
|
},
|
|
});
|
|
|
|
revalidatePath("/dashboard/families");
|
|
revalidatePath("/dashboard/adressbuch");
|
|
return { success: true };
|
|
} catch (error) {
|
|
console.error("Fehler beim Entfernen des Amtes:", error);
|
|
return { error: "Ein Fehler ist aufgetreten." };
|
|
}
|
|
}
|