diff --git a/src/app/users/page.tsx b/src/app/users/page.tsx index 9c2303a..5db2f11 100644 --- a/src/app/users/page.tsx +++ b/src/app/users/page.tsx @@ -4,6 +4,31 @@ import { getAllUsersWithStats } from '@/services/auth'; import { getGravatarUrl } from '@/lib/gravatar'; import { PageHeader } from '@/components/ui'; +const OWNED_WORKSHOP_COUNT_KEYS = [ + 'sessions', + 'motivatorSessions', + 'yearReviewSessions', + 'weeklyCheckInSessions', + 'weatherSessions', + 'gifMoodSessions', +] as const; + +const SHARED_WORKSHOP_COUNT_KEYS = [ + 'sharedSessions', + 'sharedMotivatorSessions', + 'sharedYearReviewSessions', + 'sharedWeeklyCheckInSessions', + 'sharedWeatherSessions', + 'sharedGifMoodSessions', +] as const; + +function sumCountKeys( + counts: Record, + keys: readonly string[] +): number { + return keys.reduce((acc, key) => acc + (counts[key] ?? 0), 0); +} + function formatRelativeTime(date: Date): string { const now = new Date(); const diffMs = now.getTime() - date.getTime(); @@ -28,7 +53,11 @@ export default async function UsersPage() { // Calculate some global stats const totalSessions = users.reduce( - (acc, u) => acc + u._count.sessions + u._count.motivatorSessions, + (acc, u) => acc + sumCountKeys(u._count, OWNED_WORKSHOP_COUNT_KEYS), + 0 + ); + const totalSharedSessions = users.reduce( + (acc, u) => acc + sumCountKeys(u._count, SHARED_WORKSHOP_COUNT_KEYS), 0 ); const avgSessionsPerUser = users.length > 0 ? totalSessions / users.length : 0; @@ -56,12 +85,7 @@ export default async function UsersPage() {
Moy. par user
-
- {users.reduce( - (acc, u) => acc + u._count.sharedSessions + u._count.sharedMotivatorSessions, - 0 - )} -
+
{totalSharedSessions}
Partages actifs
@@ -69,8 +93,8 @@ export default async function UsersPage() { {/* Users List */}
{users.map((user) => { - const totalUserSessions = user._count.sessions + user._count.motivatorSessions; - const totalShares = user._count.sharedSessions + user._count.sharedMotivatorSessions; + const totalUserSessions = sumCountKeys(user._count, OWNED_WORKSHOP_COUNT_KEYS); + const totalShares = sumCountKeys(user._count, SHARED_WORKSHOP_COUNT_KEYS); const isCurrentUser = user.id === session.user?.id; return ( @@ -115,7 +139,7 @@ export default async function UsersPage() { backgroundColor: 'color-mix(in srgb, var(--strength) 15%, transparent)', color: 'var(--strength)', }} - title="Sessions SWOT" + title="Ateliers créés (tous types)" > - {user._count.sessions} -
-
- - - - {user._count.motivatorSessions} + {totalUserSessions}
{totalShares > 0 && (
{ - const users = await prisma.user.findMany({ + return prisma.user.findMany({ select: { id: true, email: true, @@ -218,32 +226,20 @@ export async function getAllUsersWithStats(): Promise { _count: { select: { sessions: true, + motivatorSessions: true, + yearReviewSessions: true, + weeklyCheckInSessions: true, + weatherSessions: true, + gifMoodSessions: true, sharedSessions: true, + sharedMotivatorSessions: true, + sharedYearReviewSessions: true, + sharedWeeklyCheckInSessions: true, + sharedWeatherSessions: true, + sharedGifMoodSessions: true, }, }, }, orderBy: { createdAt: 'desc' }, }); - - // Get motivator counts in bulk (2 queries instead of 2*N) - const motivatorCounts = await prisma.movingMotivatorsSession.groupBy({ - by: ['userId'], - _count: { id: true }, - }); - const sharedMotivatorCounts = await prisma.mMSessionShare.groupBy({ - by: ['userId'], - _count: { id: true }, - }); - - const motivatorMap = new Map(motivatorCounts.map((m) => [m.userId, m._count.id])); - const sharedMotivatorMap = new Map(sharedMotivatorCounts.map((m) => [m.userId, m._count.id])); - - return users.map((user) => ({ - ...user, - _count: { - ...user._count, - motivatorSessions: motivatorMap.get(user.id) ?? 0, - sharedMotivatorSessions: sharedMotivatorMap.get(user.id) ?? 0, - }, - })); }