feat(api): add timezone support for group fetching and increase page size
feat(attendance): refactor attendance summary logic and update tests feat(analytics): extend GroupRiskSummary schema with absence and attendance counts feat(groups): enhance group schema with attendee and waitlist counts chore(skills): add frontend design skill with guidelines and license feat(attendee-detail): implement AttendeePhoneLine component for phone display feat(groups): create AttendanceBadge and CapacityIndicator components for group cards feat(groups): add card utility functions for attendance alerts and capacity summaries test(groups): implement tests for attendance alert and capacity summary logic
This commit is contained in:
129
apps/web/src/features/groups/card.ts
Normal file
129
apps/web/src/features/groups/card.ts
Normal file
@@ -0,0 +1,129 @@
|
||||
import type { BillingType, GroupRiskSummary } from '@gruperly/shared'
|
||||
|
||||
/* Derivaciones de la tarjeta del listado de grupos. La tarjeta no consulta
|
||||
nada: decide acá qué se ve, con qué tono y con qué peso visual, para que el
|
||||
JSX solo arme el layout y las reglas se puedan testear sueltas.
|
||||
|
||||
La API manda dos contadores disjuntos por grupo (`absenceStreakCount` y
|
||||
`lowAttendanceCount`): miembros que acumulan 2+ faltas seguidas sin
|
||||
justificar, o que cruzan el mínimo mensual de asistencia. Ver
|
||||
`apps/backend/src/modules/analytics/lib/risk.ts`. */
|
||||
|
||||
export type AttendanceAlert = {
|
||||
/** Miembros que disparan la regla. Es el número que va en el badge. */
|
||||
count: number
|
||||
/** Frase entera: "4 miembros con asistencia menor al 50%". Va como nombre accesible y title. */
|
||||
label: string
|
||||
severity: AttendanceSeverity
|
||||
/** Texto corto que acompaña al número cuando el caso es grave: "bajo 50%", "con faltas". */
|
||||
short: string
|
||||
}
|
||||
|
||||
/* Umbral de severidad: desde la mitad de los miembros el problema ya no es de
|
||||
una persona suelta sino del grupo, y por eso el badge cambia de color y la
|
||||
tarjeta se tiñe. Antes de ese umbral alcanza con marcarlo sin robarle el
|
||||
foco a la tarjeta. Se compara contra la matrícula, no contra el cupo: el
|
||||
cupo puede no estar definido o ser mucho mayor que la cantidad real de
|
||||
miembros. */
|
||||
const PROMINENT_SHARE = 0.5
|
||||
|
||||
export type AttendanceSeverity = 'low' | 'high'
|
||||
|
||||
/**
|
||||
* Única fuente de la regla de severidad: la usan la tarjeta (para el borde) y
|
||||
* el badge (para el color y el texto), así que no pueden desincronizarse.
|
||||
*/
|
||||
export function getAttendanceSeverity(
|
||||
affectedCount: number,
|
||||
attendeesCount: number,
|
||||
): AttendanceSeverity {
|
||||
if (attendeesCount <= 0) return 'low'
|
||||
return affectedCount / attendeesCount >= PROMINENT_SHARE ? 'high' : 'low'
|
||||
}
|
||||
|
||||
/**
|
||||
* Qué alerta de asistencia corresponde al grupo, si alguna.
|
||||
*
|
||||
* Cuando hay miembros de los dos tipos solo se reporta el de faltas seguidas:
|
||||
* es la señal más accionable (alguien se está yendo) y en un badge chico una
|
||||
* sola frase alcanza para explicar el ícono. La ficha del grupo y las
|
||||
* estadísticas sí muestran las dos.
|
||||
*/
|
||||
export function attendanceAlert(
|
||||
risk: GroupRiskSummary | undefined,
|
||||
attendeesCount: number,
|
||||
): AttendanceAlert | null {
|
||||
const reason = dominantReason(risk)
|
||||
if (!reason || attendeesCount <= 0) return null
|
||||
|
||||
return {
|
||||
count: reason.count,
|
||||
label: `${membersLabel(reason.count)} ${reason.text}`,
|
||||
severity: getAttendanceSeverity(reason.count, attendeesCount),
|
||||
short: reason.short,
|
||||
}
|
||||
}
|
||||
|
||||
/* `short` es el texto que se suma al número cuando el caso es grave: el número
|
||||
solo no dice por qué, y la versión larga se reserva para el title y para el
|
||||
nombre accesible. El detalle entero vive en la ficha del grupo. */
|
||||
function dominantReason(
|
||||
risk: GroupRiskSummary | undefined,
|
||||
): { count: number; short: string; text: string } | null {
|
||||
if (!risk) return null
|
||||
|
||||
if (risk.absenceStreakCount > 0) {
|
||||
return {
|
||||
count: risk.absenceStreakCount,
|
||||
short: 'con faltas',
|
||||
text: 'con faltas seguidas',
|
||||
}
|
||||
}
|
||||
if (risk.lowAttendanceCount > 0) {
|
||||
return {
|
||||
count: risk.lowAttendanceCount,
|
||||
short: 'bajo 50%',
|
||||
text: 'con asistencia menor al 50%',
|
||||
}
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
export type CapacitySummary = {
|
||||
/** "4/5 miembros", o "1 miembro" cuando el grupo no tiene cupo definido. */
|
||||
label: string
|
||||
/** 0..1, para la barrita de ocupación. 0 cuando no hay cupo. */
|
||||
occupancy: number
|
||||
hasCapacity: boolean
|
||||
isFull: boolean
|
||||
waitlistCount: number
|
||||
}
|
||||
|
||||
export function capacitySummary(
|
||||
attendeesCount: number,
|
||||
capacity: number | null,
|
||||
waitlistCount: number,
|
||||
): CapacitySummary {
|
||||
const hasCapacity = capacity != null
|
||||
|
||||
return {
|
||||
// Con cupo la unidad es el lugar ("1/5 miembros"), así que el plural no
|
||||
// depende de cuánta gente haya: depende del denominador.
|
||||
label: hasCapacity ? `${attendeesCount}/${capacity} miembros` : membersLabel(attendeesCount),
|
||||
occupancy: hasCapacity && capacity > 0 ? Math.min(attendeesCount / capacity, 1) : 0,
|
||||
hasCapacity,
|
||||
isFull: hasCapacity && attendeesCount >= capacity,
|
||||
waitlistCount,
|
||||
}
|
||||
}
|
||||
|
||||
/** Sufijo del precio según cómo se cobra: "/mes" o "/clase". */
|
||||
export function billingSuffix(billingType: BillingType | null): string | null {
|
||||
if (billingType === 'MONTHLY') return '/mes'
|
||||
if (billingType === 'PER_CLASS') return '/clase'
|
||||
return null
|
||||
}
|
||||
|
||||
function membersLabel(count: number): string {
|
||||
return count === 1 ? '1 miembro' : `${count} miembros`
|
||||
}
|
||||
Reference in New Issue
Block a user