import { prisma } from "@/lib/prisma"
import Navigation from "@/components/Navigation"
import Link from "next/link"
import { redirect } from "next/navigation"
import { getSession } from "@/lib/auth-simple"
import type { Event as EventModel } from "@prisma/client"
import { RecalculateEloButton } from "../../components/RecalculateEloButton"
export const dynamic = "force-dynamic";
export default async function AdminDashboard() {
console.log('AdminDashboard rendering...')
const session = await getSession()
console.log('AdminDashboard session:', JSON.stringify(session, null, 2))
if (!session) {
console.log('No session found, redirecting to login')
redirect("/auth/login")
}
// Get user role from database since Better Auth doesn't include custom fields in session
const userId = session.user?.id || session.user?.userId
console.log('Looking for user with ID:', userId)
console.log('Session user:', JSON.stringify(session.user, null, 2))
const user = await prisma.user.findUnique({
where: { id: userId }
})
console.log('Found user:', user ? 'yes' : 'no')
console.log('User role:', user?.role)
console.log('User email:', user?.email)
// If user doesn't exist or has no role, redirect to login
if (!user) {
console.log('User not found, redirecting to login')
redirect("/auth/login")
}
// Non-admin users should see a different dashboard
if (user.role !== "club_admin" && user.role !== "site_admin") {
if (user.playerId) {
redirect("/players/" + user.playerId + "/profile")
} else {
// If playerId is null, redirect to rankings page
redirect("/rankings")
}
}
// Get dashboard stats
const [playerCount, tournamentCount, matchCount, recentTournaments] = await Promise.all([
prisma.player.count(),
prisma.event.count(),
prisma.match.count(),
prisma.event.findMany({
take: 5,
orderBy: { createdAt: "desc" },
}),
]) as [number, number, number, EventModel[]]
// Get recent activities (using any type to bypass TypeScript error for now)
const recentActivities = await (prisma as any).activity.findMany({
take: 10,
orderBy: { createdAt: "desc" },
include: {
user: { select: { name: true } },
player: { select: { name: true } },
event: { select: { name: true } },
},
})
return (
{/* Page Header */}
Admin Dashboard
Manage your club's tournaments, players, and matches.
{/* Stats Grid */}
- Total Players
- {playerCount}
- Tournaments
- {tournamentCount}
- Matches Played
- {matchCount}
{/* Quick Actions */}
Quick Actions
New Tournament
Import CSV
View Rankings
All Tournaments
Manage Users
{/* Recent Tournaments */}
Recent Tournaments
{recentTournaments.length > 0 ? (
{recentTournaments.map((tournament) => (
-
{tournament.name}
{tournament.status}
View
))}
) : (
No tournaments yet.
)}
{/* Player Directory Preview */}
Player Directory
View All
{/* This would be populated with actual player data in a real implementation */}
View all players
in the rankings page.
{/* Recent Activity Feed */}
Recent Activity
View All
{recentActivities.length > 0 ? (
{recentActivities.map((activity: any) => (
-
{activity.description}
{new Date(activity.createdAt).toLocaleDateString()} at{' '}
{new Date(activity.createdAt).toLocaleTimeString()}
{activity.type}
))}
) : (
No recent activities.
)}
)
}