import Link from "next/link"; import { endOfMonth, startOfMonth, startOfDay, endOfDay, subWeeks, startOfWeek, addDays, format as fnsFormat } from "date-fns"; import { format } from "date-fns"; import { de } from "date-fns/locale"; import { BarChart3, Calendar, CheckCircle2, Clock, Users, AlertTriangle, ArrowRight } from "lucide-react"; import { prisma } from "@/lib/prisma"; import { LatestBookingsPanel } from "@/components/admin/latest-bookings-panel"; export const dynamic = "force-dynamic"; export default async function AdminOverviewPage() { const now = new Date(); const eightWeeksAgo = subWeeks(now, 7); const [ activeResources, totalResources, upcomingAppointments, monthTotal, monthCancelled, monthNoShow, openDeliveryIssues, todayAppointments, weeklyAppointments ] = await Promise.all([ prisma.calendarConn.count({ where: { user: { role: "STAFF", isActive: true } } }), prisma.calendarConn.count({ where: { user: { role: "STAFF" } } }), prisma.appointment.count({ where: { status: "CONFIRMED", startAt: { gte: now } } }), prisma.appointment.count({ where: { status: "CONFIRMED", startAt: { gte: startOfMonth(now), lte: endOfMonth(now) } } }), prisma.appointment.count({ where: { status: "CANCELLED", startAt: { gte: startOfMonth(now), lte: endOfMonth(now) } } }), prisma.appointment.count({ where: { status: "CONFIRMED", noShowAt: { not: null }, startAt: { gte: startOfMonth(now), lte: endOfMonth(now) } } }), prisma.deliveryIssue.count({ where: { resolvedAt: null } }), prisma.appointment.findMany({ where: { status: "CONFIRMED", startAt: { gte: startOfDay(now), lte: endOfDay(now) } }, include: { staff: { select: { name: true } } }, orderBy: { startAt: "asc" } }), prisma.appointment.findMany({ where: { status: "CONFIRMED", startAt: { gte: eightWeeksAgo } }, select: { startAt: true } }) ]); // Build weekly chart data const weeks: { label: string; count: number }[] = []; for (let i = 7; i >= 0; i--) { const weekStart = startOfWeek(subWeeks(now, i), { weekStartsOn: 1 }); const weekEnd = addDays(weekStart, 6); const count = weeklyAppointments.filter((a) => a.startAt >= weekStart && a.startAt <= weekEnd).length; weeks.push({ label: format(weekStart, "dd.MM.", { locale: de }), count }); } const maxCount = Math.max(1, ...weeks.map((w) => w.count)); return (
{format(now, "EEEE, d. MMMM yyyy", { locale: de })}
{a.customerFirstName} {a.customerLastName}
{a.customerEmail}