45 lines
1.3 KiB
TypeScript
45 lines
1.3 KiB
TypeScript
import { redirect } from "next/navigation";
|
|
import { Prisma } from "@/generated/prisma/client";
|
|
import { canAccess, getAdminContext, PERMS } from "@/lib/permissions";
|
|
import { prisma } from "@/lib/prisma";
|
|
import { fetchEmulatorRankSummaries } from "@/lib/services/permission-ranks";
|
|
import { PermissionsList } from "./permissions-list";
|
|
|
|
export default async function PermissionsPage({
|
|
searchParams,
|
|
}: {
|
|
params: Promise<{ locale: string }>;
|
|
searchParams?: Promise<{ from?: string }>;
|
|
}) {
|
|
const { session, permissions } = await getAdminContext();
|
|
if (!canAccess(permissions, PERMS.PERMISSIONS_MANAGE, session.user.rank)) {
|
|
redirect("/admin");
|
|
}
|
|
|
|
const sp = (await searchParams) ?? {};
|
|
const fromHousekeeping = sp.from === "housekeeping";
|
|
|
|
const ranks = await fetchEmulatorRankSummaries(prisma);
|
|
|
|
// User counts per rank
|
|
const rawCounts: { rank: number; cnt: number }[] = await prisma.$queryRaw(
|
|
Prisma.sql`SELECT \`rank\`, COUNT(*) as cnt FROM users GROUP BY \`rank\``,
|
|
);
|
|
const countMap: Record<number, number> = {};
|
|
for (const row of rawCounts) {
|
|
countMap[Number(row.rank)] = Number(row.cnt);
|
|
}
|
|
|
|
const ranksWithCounts = ranks.map((rank) => ({
|
|
...rank,
|
|
userCount: countMap[rank.id] ?? 0,
|
|
}));
|
|
|
|
return (
|
|
<PermissionsList
|
|
ranks={ranksWithCounts}
|
|
fromHousekeeping={fromHousekeeping}
|
|
/>
|
|
);
|
|
}
|