feat: add 'Utilisateurs' link to Header component and implement user statistics retrieval in auth service
This commit is contained in:
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user