Separa il calcolo delle presenze del mese dagli hook e lo copre con i test.
La percentuale di presenze degli ultimi 30 giorni decide le convocazioni, ma stava dentro due useMemo — scritta due volte, una per giocatore e una per la rosa — e nessun test la toccava. Le tre regole che la determinano non sono ovvie: la finestra è di 30 giorni, il ritardo conta come presenza, e convocati vuoto significa tutta la rosa attiva. presenze-mese-core.ts contiene ora il calcolo puro, presenze-mese.ts solo il collegamento agli hook. Comportamento invariato, inclusa la differenza fra i due percorsi: per giocatore si filtrano i convocati, per la rosa l'elenco vuoto si espande agli attivi. Il test fissa le tre regole e i casi che le rompono: nessun evento nella finestra dà 0 e non NaN, chi non è convocato resta fuori dal denominatore, chi non ha mai risposto ci resta dentro. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
+14
-56
@@ -2,77 +2,35 @@ import { useMemo } from "react";
|
||||
import { useEventi } from "./eventi";
|
||||
import { useRispostePresenze } from "./presenze";
|
||||
import { useGiocatoriSquadra } from "./giocatori-squadra";
|
||||
import {
|
||||
presenzeUltimoMese,
|
||||
presenzeUltimoMeseTutti,
|
||||
type StatistichePresenza,
|
||||
} from "./presenze-mese-core";
|
||||
|
||||
/**
|
||||
* Percentuale di presenze dell'ultimo mese (30 giorni), utile per le convocazioni.
|
||||
* Usa solo cache già in memoria: nessuna query aggiuntiva.
|
||||
* Usa solo cache già in memoria: nessuna query aggiuntiva. Il calcolo sta in
|
||||
* `presenze-mese-core.ts`, qui c'è solo il collegamento agli hook.
|
||||
*/
|
||||
export function usePresenzeUltimoMese(giocatoreId: string | undefined) {
|
||||
export function usePresenzeUltimoMese(giocatoreId: string | undefined): StatistichePresenza {
|
||||
const { eventi } = useEventi();
|
||||
const { presenze } = useRispostePresenze();
|
||||
|
||||
return useMemo(() => {
|
||||
if (!giocatoreId) return { presenti: 0, totali: 0, percentuale: 0 };
|
||||
const oggi = new Date();
|
||||
const inizio = new Date(oggi);
|
||||
inizio.setDate(inizio.getDate() - 30);
|
||||
const da = inizio.toISOString().slice(0, 10);
|
||||
const a = oggi.toISOString().slice(0, 10);
|
||||
|
||||
const rilevanti = eventi.filter(
|
||||
(e) =>
|
||||
(e.tipo === "partita" || e.tipo === "allenamento") &&
|
||||
e.data >= da &&
|
||||
e.data <= a &&
|
||||
(e.convocati.length === 0 || e.convocati.includes(giocatoreId)),
|
||||
);
|
||||
const presenti = rilevanti.filter((e) => {
|
||||
const stato = presenze[e.id]?.[giocatoreId];
|
||||
return stato === "presente" || stato === "ritardo";
|
||||
}).length;
|
||||
const totali = rilevanti.length;
|
||||
return {
|
||||
presenti,
|
||||
totali,
|
||||
percentuale: totali ? Math.round((presenti / totali) * 100) : 0,
|
||||
};
|
||||
}, [eventi, presenze, giocatoreId]);
|
||||
return useMemo(
|
||||
() => presenzeUltimoMese(giocatoreId, eventi, presenze),
|
||||
[eventi, presenze, giocatoreId],
|
||||
);
|
||||
}
|
||||
|
||||
/** Percentuale presenze ultimi 30 giorni per tutti i giocatori (mappa per id). */
|
||||
export function usePresenzeUltimoMeseTutti(): Record<
|
||||
string,
|
||||
{ presenti: number; totali: number; percentuale: number }
|
||||
> {
|
||||
export function usePresenzeUltimoMeseTutti(): Record<string, StatistichePresenza> {
|
||||
const { eventi } = useEventi();
|
||||
const { presenze } = useRispostePresenze();
|
||||
const { righe: squadra } = useGiocatoriSquadra();
|
||||
|
||||
return useMemo(() => {
|
||||
const oggi = new Date();
|
||||
const inizio = new Date(oggi);
|
||||
inizio.setDate(inizio.getDate() - 30);
|
||||
const da = inizio.toISOString().slice(0, 10);
|
||||
const a = oggi.toISOString().slice(0, 10);
|
||||
const idRosa = squadra.filter((g) => g.attivo).map((g) => g.id);
|
||||
|
||||
const rilevanti = eventi.filter(
|
||||
(e) => (e.tipo === "partita" || e.tipo === "allenamento") && e.data >= da && e.data <= a,
|
||||
);
|
||||
|
||||
const out: Record<string, { presenti: number; totali: number; percentuale: number }> = {};
|
||||
for (const e of rilevanti) {
|
||||
const ids = e.convocati.length > 0 ? e.convocati : idRosa;
|
||||
for (const id of ids) {
|
||||
const rec = (out[id] ??= { presenti: 0, totali: 0, percentuale: 0 });
|
||||
rec.totali += 1;
|
||||
const stato = presenze[e.id]?.[id];
|
||||
if (stato === "presente" || stato === "ritardo") rec.presenti += 1;
|
||||
}
|
||||
}
|
||||
for (const rec of Object.values(out)) {
|
||||
rec.percentuale = rec.totali ? Math.round((rec.presenti / rec.totali) * 100) : 0;
|
||||
}
|
||||
return out;
|
||||
return presenzeUltimoMeseTutti(eventi, presenze, idRosa);
|
||||
}, [eventi, presenze, squadra]);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user