feat: add 'Utilisateurs' link to Header component and implement user statistics retrieval in auth service

This commit is contained in:
Julien Froidefond
2025-11-28 10:55:49 +01:00
parent ff7c846ed1
commit 941151553f
3 changed files with 296 additions and 0 deletions

View File

@@ -144,3 +144,61 @@ export async function updateUserPassword(
return { success: true };
}
export interface UserStats {
sessions: number;
motivatorSessions: number;
sharedSessions: number;
sharedMotivatorSessions: number;
}
export interface UserWithStats {
id: string;
email: string;
name: string | null;
createdAt: Date;
updatedAt: Date;
_count: UserStats;
}
export async function getAllUsersWithStats(): Promise<UserWithStats[]> {
const users = await prisma.user.findMany({
select: {
id: true,
email: true,
name: true,
createdAt: true,
updatedAt: true,
_count: {
select: {
sessions: true,
sharedSessions: true,
},
},
},
orderBy: { createdAt: 'desc' },
});
// Get motivator sessions count separately (Prisma doesn't have these in User model _count directly)
const usersWithMotivators = await Promise.all(
users.map(async (user) => {
const motivatorCount = await prisma.movingMotivatorsSession.count({
where: { userId: user.id },
});
const sharedMotivatorCount = await prisma.mMSessionShare.count({
where: { userId: user.id },
});
return {
...user,
_count: {
...user._count,
motivatorSessions: motivatorCount,
sharedMotivatorSessions: sharedMotivatorCount,
},
};
})
);
return usersWithMotivators;
}