refactor: P0-1/2/4 解耦修复 - 拆分过耦合文件 + dashboard 解耦
This commit is contained in:
@@ -1,10 +1,10 @@
|
||||
import "server-only"
|
||||
|
||||
import { cache } from "react"
|
||||
import { eq } from "drizzle-orm"
|
||||
import { count, desc, eq, gt, inArray } from "drizzle-orm"
|
||||
|
||||
import { db } from "@/shared/db"
|
||||
import { roles, users, usersToRoles } from "@/shared/db/schema"
|
||||
import { roles, sessions, users, usersToRoles } from "@/shared/db/schema"
|
||||
|
||||
export type UserProfile = {
|
||||
id: string
|
||||
@@ -69,3 +69,84 @@ export const getUserProfile = cache(async (userId: string): Promise<UserProfile
|
||||
updatedAt: user.updatedAt,
|
||||
}
|
||||
})
|
||||
|
||||
export type UsersDashboardStats = {
|
||||
userCount: number
|
||||
activeSessionsCount: number
|
||||
userRoleCounts: Array<{ role: string; count: number }>
|
||||
recentUsers: Array<{
|
||||
id: string
|
||||
name: string | null
|
||||
email: string
|
||||
role: string | null
|
||||
createdAt: string
|
||||
}>
|
||||
}
|
||||
|
||||
export const getUsersDashboardStats = cache(async (): Promise<UsersDashboardStats> => {
|
||||
const now = new Date()
|
||||
|
||||
const [userCountRow, activeSessionsRow, userRoleCountRows, recentUserRows] = await Promise.all([
|
||||
db.select({ value: count() }).from(users),
|
||||
db.select({ value: count() }).from(sessions).where(gt(sessions.expires, now)),
|
||||
db
|
||||
.select({ role: roles.name, value: count() })
|
||||
.from(usersToRoles)
|
||||
.innerJoin(roles, eq(usersToRoles.roleId, roles.id))
|
||||
.groupBy(roles.name),
|
||||
db
|
||||
.select({
|
||||
id: users.id,
|
||||
name: users.name,
|
||||
email: users.email,
|
||||
createdAt: users.createdAt,
|
||||
})
|
||||
.from(users)
|
||||
.orderBy(desc(users.createdAt))
|
||||
.limit(8),
|
||||
])
|
||||
|
||||
const userCount = Number(userCountRow[0]?.value ?? 0)
|
||||
const activeSessionsCount = Number(activeSessionsRow[0]?.value ?? 0)
|
||||
|
||||
const userRoleCounts = userRoleCountRows
|
||||
.map((r) => ({ role: r.role ?? "unknown", count: Number(r.value ?? 0) }))
|
||||
.sort((a, b) => b.count - a.count)
|
||||
|
||||
const recentUserIds = recentUserRows.map((u) => u.id)
|
||||
const recentRoleRows = recentUserIds.length
|
||||
? await db
|
||||
.select({
|
||||
userId: usersToRoles.userId,
|
||||
roleName: roles.name,
|
||||
})
|
||||
.from(usersToRoles)
|
||||
.innerJoin(roles, eq(usersToRoles.roleId, roles.id))
|
||||
.where(inArray(usersToRoles.userId, recentUserIds))
|
||||
: []
|
||||
|
||||
const rolesByUserId = new Map<string, string[]>()
|
||||
for (const row of recentRoleRows) {
|
||||
const list = rolesByUserId.get(row.userId) ?? []
|
||||
list.push(row.roleName)
|
||||
rolesByUserId.set(row.userId, list)
|
||||
}
|
||||
|
||||
const recentUsers = recentUserRows.map((u) => {
|
||||
const roleNames = rolesByUserId.get(u.id) ?? []
|
||||
return {
|
||||
id: u.id,
|
||||
name: u.name,
|
||||
email: u.email,
|
||||
role: resolvePrimaryRole(roleNames),
|
||||
createdAt: u.createdAt.toISOString(),
|
||||
}
|
||||
})
|
||||
|
||||
return {
|
||||
userCount,
|
||||
activeSessionsCount,
|
||||
userRoleCounts,
|
||||
recentUsers,
|
||||
}
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user