changement
This commit is contained in:
@@ -1,8 +1,41 @@
|
||||
const bcrypt = require("bcrypt");
|
||||
const prisma = require("../config/prisma");
|
||||
const { getGameWindow, expireOverdueClaims } = require("../utils/gameWindow");
|
||||
|
||||
const AGE_BRACKETS = ["-18", "18-24", "25-34", "35-44", "45-54", "55-64", "65+"];
|
||||
|
||||
function getAgeBracket(birthdate) {
|
||||
if (!birthdate) return "unknown";
|
||||
|
||||
const now = new Date();
|
||||
let age = now.getFullYear() - birthdate.getFullYear();
|
||||
const hasHadBirthdayThisYear =
|
||||
now.getMonth() > birthdate.getMonth() ||
|
||||
(now.getMonth() === birthdate.getMonth() && now.getDate() >= birthdate.getDate());
|
||||
if (!hasHadBirthdayThisYear) age -= 1;
|
||||
|
||||
if (age < 18) return "-18";
|
||||
if (age <= 24) return "18-24";
|
||||
if (age <= 34) return "25-34";
|
||||
if (age <= 44) return "35-44";
|
||||
if (age <= 54) return "45-54";
|
||||
if (age <= 64) return "55-64";
|
||||
return "65+";
|
||||
}
|
||||
|
||||
function computeAgeStats(users) {
|
||||
const counts = Object.fromEntries([...AGE_BRACKETS, "unknown"].map((b) => [b, 0]));
|
||||
for (const user of users) {
|
||||
counts[getAgeBracket(user.birthdate)] += 1;
|
||||
}
|
||||
return counts;
|
||||
}
|
||||
|
||||
const getStats = async (req, res) => {
|
||||
try {
|
||||
await expireOverdueClaims();
|
||||
const gameWindow = await getGameWindow();
|
||||
|
||||
const [
|
||||
ticketsTotal,
|
||||
ticketsUsed,
|
||||
@@ -13,6 +46,7 @@ const getStats = async (req, res) => {
|
||||
users,
|
||||
men,
|
||||
women,
|
||||
clientsWithBirthdate,
|
||||
] = await Promise.all([
|
||||
prisma.ticket.count(),
|
||||
prisma.ticket.count({ where: { isUsed: true } }),
|
||||
@@ -23,6 +57,7 @@ const getStats = async (req, res) => {
|
||||
prisma.user.count({ where: { role: "CLIENT" } }),
|
||||
prisma.user.count({ where: { role: "CLIENT", gender: "Homme" } }),
|
||||
prisma.user.count({ where: { role: "CLIENT", gender: "Femme" } }),
|
||||
prisma.user.findMany({ where: { role: "CLIENT" }, select: { birthdate: true } }),
|
||||
]);
|
||||
|
||||
const lotsStats = await prisma.lot.findMany({
|
||||
@@ -48,6 +83,8 @@ const getStats = async (req, res) => {
|
||||
claimsDelivered,
|
||||
claimsCancelled,
|
||||
users,
|
||||
gameWindow,
|
||||
ageStats: computeAgeStats(clientsWithBirthdate),
|
||||
genderStats: {
|
||||
men,
|
||||
women,
|
||||
|
||||
Reference in New Issue
Block a user