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
62 lines
1.9 KiB
TypeScript
62 lines
1.9 KiB
TypeScript
export type AttendanceStreamEntry = {
|
|
status: 'PRESENT' | 'ABSENT' | 'EXCUSED';
|
|
occurredAt: Date;
|
|
};
|
|
|
|
export type RiskLevel = 'HIGH' | 'MEDIUM';
|
|
|
|
/** Racha mínima de faltas sin justificar que ya dispara riesgo. */
|
|
export const MIN_RISK_ABSENCE_STREAK = 2;
|
|
|
|
/** Asistencia mensual (ventana de 30 días) por debajo de la cual hay riesgo. */
|
|
export const MIN_RISK_MONTHLY_RATE = 50;
|
|
|
|
// Cuenta las ausencias consecutivas sin justificar desde la clase más reciente.
|
|
// Recibe la lista ordenada de más reciente a más antigua; PRESENT y EXCUSED rompen la cadena.
|
|
export function trailingAbsentStreak(entries: AttendanceStreamEntry[]): number {
|
|
let streak = 0;
|
|
for (const entry of entries) {
|
|
if (entry.status !== 'ABSENT') {
|
|
break;
|
|
}
|
|
streak += 1;
|
|
}
|
|
return streak;
|
|
}
|
|
|
|
// Fecha de la última clase a la que el alumno asistió (PRESENT), o null.
|
|
export function lastAttendedAt(entries: AttendanceStreamEntry[]): Date | null {
|
|
for (const entry of entries) {
|
|
if (entry.status === 'PRESENT') {
|
|
return entry.occurredAt;
|
|
}
|
|
}
|
|
return null;
|
|
}
|
|
|
|
// Porcentaje de asistencia sobre el total de sesiones del período.
|
|
// Las sesiones sin registrar cuentan como no asistidas.
|
|
export function monthlyAttendanceRate(
|
|
entries: AttendanceStreamEntry[],
|
|
totalSessions: number,
|
|
): number {
|
|
if (totalSessions <= 0) {
|
|
return 0;
|
|
}
|
|
const present = entries.filter((entry) => entry.status === 'PRESENT').length;
|
|
return Math.round((present / totalSessions) * 100);
|
|
}
|
|
|
|
// HIGH >= 3 ausencias consecutivas; MEDIUM == 2 consecutivas o asistencia mensual < 50%.
|
|
export function classifyRisk(
|
|
consecutiveAbsences: number,
|
|
monthlyRate: number,
|
|
): RiskLevel | null {
|
|
if (consecutiveAbsences >= MIN_RISK_ABSENCE_STREAK + 1) {
|
|
return 'HIGH';
|
|
}
|
|
if (consecutiveAbsences >= MIN_RISK_ABSENCE_STREAK || monthlyRate < MIN_RISK_MONTHLY_RATE) {
|
|
return 'MEDIUM';
|
|
}
|
|
return null;
|
|
} |