import Link from "next/link";
import { TerminStatus, TerminType, UserRole } from "@prisma/client";
import { ArrowRight, ShieldAlert } from "lucide-react";
import { Manrope } from "next/font/google";
import { requireRole } from "@/lib/auth-utils";
import { prisma } from "@/lib/prisma";
import { formatDateKey, getTargetMonthData } from "@/lib/date-utils";
import { Button } from "@/components/ui/button";
import { NotdienstEntry } from "./notdienst-entry";
export const metadata = { title: "Notdienst · Kita-Planer" };
const manrope = Manrope({
subsets: ["latin"],
weight: ["400", "500", "600", "700"],
});
export default async function NotdienstPage() {
// ELTERN können eintragen; Admins/Koordinatoren haben hier eigentlich
// andere Ansichten (Plan-Generierung), aber wir lassen sie zur Demonstration
// ebenfalls durch, falls sie selbst Kinder haben.
const session = await requireRole([
UserRole.ELTERN,
UserRole.ADMIN,
UserRole.KOORDINATOR,
]);
const kitaId = session.user.kitaId;
if (!kitaId) {
return
Kein Mandant zugeordnet.
;
}
// Kita-Einstellungen laden
const kita = await prisma.kita.findUniqueOrThrow({
where: { id: kitaId },
select: { notdienstMinPerChildPerMonth: true },
});
// Nur aktive Kinder des eigenen Haushalts laden.
const children = session.user.familyId
? await prisma.child.findMany({
where: {
kitaId,
familyId: session.user.familyId,
active: true,
},
select: { id: true, firstName: true },
})
: [];
const targetData = getTargetMonthData();
const { targetYear, targetMonth, isLocked } = targetData;
const targetMonthStart = new Date(Date.UTC(targetYear, targetMonth - 1, 1));
const targetMonthEnd = new Date(Date.UTC(targetYear, targetMonth, 1));
const requiredDaysTotal = children.length * kita.notdienstMinPerChildPerMonth;
const childIds = children.map((child) => child.id);
// Bisherige Einträge für den Zielmonat laden, haushaltsweit über die Kinder.
const [availabilities, termine] =
children.length > 0
? await Promise.all([
prisma.notdienstAvailability.findMany({
where: {
kitaId,
childId: { in: childIds },
date: {
gte: targetMonthStart,
lt: targetMonthEnd,
},
},
select: { date: true },
}),
prisma.termin.findMany({
where: {
kitaId,
status: TerminStatus.CONFIRMED,
startDate: { lt: targetMonthEnd },
endDate: { gte: targetMonthStart },
},
select: {
id: true,
title: true,
description: true,
type: true,
startDate: true,
endDate: true,
allDay: true,
},
orderBy: { startDate: "asc" },
}),
])
: [[], []];
const selectedDates = availabilities.map((a) => formatDateKey(a.date));
return (
{children.length === 0 ? (
) : (
({
id: termin.id,
title: termin.title,
category: getTerminCategory(termin.type),
startDate: termin.startDate,
endDate: termin.endDate,
allDay: termin.allDay,
note: termin.description,
}))}
/>
)}
);
}
function getTerminCategory(type: TerminType) {
switch (type) {
case TerminType.SCHLIESSTAG:
return "closure" as const;
case TerminType.TEAMTAG:
return "team" as const;
case TerminType.KITA_FEST:
case TerminType.MITMACH_TAG:
return "social" as const;
case TerminType.ELTERNABEND:
case TerminType.MITGLIEDERVERSAMMLUNG:
case TerminType.ELTERNCAFE:
return "parents" as const;
case TerminType.GEBURTSTAG_INTERN:
case TerminType.GEBURTSTAG_EXTERN:
return "birthday" as const;
default:
return "social" as const;
}
}
function NotdienstLockout() {
return (
Erst Kinder hinterlegen
Bitte lege zuerst deine Kinder im Profil an, bevor du Notdienste
übernimmst.
Zum Profil
);
}