41 lines
1.3 KiB
TypeScript
41 lines
1.3 KiB
TypeScript
import { redirect } from 'next/navigation'
|
|
import { Prisma } from '@/generated/prisma/client'
|
|
import { getAdminContext } 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(_props: { params: Promise<{ locale: string }> }) {
|
|
const { permissions } = await getAdminContext()
|
|
if (!permissions.isSuperAdmin) redirect('/admin')
|
|
|
|
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)
|
|
}
|
|
|
|
// Auto-create CMS roles for ranks >= 4
|
|
for (const rank of ranks) {
|
|
if (rank.id >= 4) {
|
|
await prisma.aclRole.upsert({
|
|
where: { slug: `rank_${rank.id}` },
|
|
create: { slug: `rank_${rank.id}`, title: rank.rank_name || `Rank ${rank.id}` },
|
|
update: {},
|
|
})
|
|
}
|
|
}
|
|
|
|
const ranksWithCounts = ranks.map((rank) => ({
|
|
...rank,
|
|
userCount: countMap[rank.id] ?? 0,
|
|
}))
|
|
|
|
return <PermissionsList ranks={ranksWithCounts} />
|
|
}
|