feat: una sola regla de riesgo y preinscripciones pendientes en Inicio

- Estadísticas ("Alumnos en riesgo") y el job de reenganche usan la misma
  regla de dominio que la lista de grupos e Inicio
  (summarizeMemberAttendance). El armado del historial por alumno vive en
  analytics/lib/member-attendance.ts y se borra el clasificador viejo
  (racha o asistencia mensual < 50%).
- El badge de alumnos en riesgo muestra el motivo real (faltas seguidas,
  días sin venir o asistencia en baja).
- GET /home devuelve pendingPreRegistrations por grupo (en la misma
  consulta de grupos) y "Para atender" las lista con link a la ficha en
  la pestaña Pendientes (?section=pending).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Jose Selesan
2026-10-27 11:28:21 -03:00
parent b12ffdda37
commit e6d405a71e
17 changed files with 297 additions and 278 deletions

View File

@@ -133,24 +133,35 @@ describe('analytics routes', () => {
});
describe('GET /groups/:groupId/students-at-risk', () => {
const sessions = [at(0), at(7), at(14), at(21)];
// Every 3 days: with weekly classes two absences already add up to 14 days
// without coming, and the inactivity rule would turn the MEDIUM case HIGH.
const sessions = [at(0), at(3), at(6), at(9)];
const sessionRows = sessions.map((startsAt, index) => ({ id: `s${index}`, startsAt: new Date(startsAt) }));
const enrolled = new Date(at(60));
// `offset` is the index in `sessions`: 0 is the most recent class.
const row = (attendeeId: string, status: string, offset: number) => ({
attendeeId,
classSessionId: `s${offset}`,
status,
classSession: { startsAt: new Date(sessions[offset]) },
});
it('clasifica HIGH y MEDIUM según el historial de asistencia', async () => {
prisma.group.findUnique.mockResolvedValue(makeGroup());
prisma.classSession.findMany.mockResolvedValue(sessions.map((startsAt) => ({ id: 's', startsAt })));
prisma.classSession.findMany.mockResolvedValue(sessionRows);
prisma.attendee.findMany.mockResolvedValue([
{ id: 'a-1', fullName: 'Ana Pérez', phone: '+521111111111' },
{ id: 'a-2', fullName: 'Bruno Díaz', phone: null },
{ id: 'a-1', fullName: 'Ana Pérez', phone: '+521111111111', createdAt: enrolled },
{ id: 'a-2', fullName: 'Bruno Díaz', phone: null, createdAt: enrolled },
]);
prisma.attendance.findMany.mockResolvedValue([
{ attendeeId: 'a-1', status: 'ABSENT', classSession: { startsAt: new Date(at(0)) } },
{ attendeeId: 'a-1', status: 'ABSENT', classSession: { startsAt: new Date(at(7)) } },
{ attendeeId: 'a-1', status: 'ABSENT', classSession: { startsAt: new Date(at(14)) } },
{ attendeeId: 'a-1', status: 'PRESENT', classSession: { startsAt: new Date(at(21)) } },
{ attendeeId: 'a-2', status: 'ABSENT', classSession: { startsAt: new Date(at(0)) } },
{ attendeeId: 'a-2', status: 'ABSENT', classSession: { startsAt: new Date(at(7)) } },
{ attendeeId: 'a-2', status: 'PRESENT', classSession: { startsAt: new Date(at(14)) } },
{ attendeeId: 'a-2', status: 'PRESENT', classSession: { startsAt: new Date(at(21)) } },
row('a-1', 'ABSENT', 0),
row('a-1', 'ABSENT', 1),
row('a-1', 'ABSENT', 2),
row('a-1', 'PRESENT', 3),
row('a-2', 'ABSENT', 0),
row('a-2', 'ABSENT', 1),
row('a-2', 'PRESENT', 2),
row('a-2', 'PRESENT', 3),
]);
const res = await makeApp({ id: userId }).request('/groups/group-1/students-at-risk');
@@ -165,7 +176,7 @@ describe('analytics routes', () => {
phone: '+521111111111',
riskLevel: 'HIGH',
consecutiveAbsences: 3,
lastAttendedAt: at(21),
lastAttendedAt: sessions[3],
monthlyAttendanceRate: 25,
},
{
@@ -174,7 +185,7 @@ describe('analytics routes', () => {
phone: null,
riskLevel: 'MEDIUM',
consecutiveAbsences: 2,
lastAttendedAt: at(14),
lastAttendedAt: sessions[2],
monthlyAttendanceRate: 50,
},
],
@@ -183,14 +194,11 @@ describe('analytics routes', () => {
it('devuelve lista vacía cuando nadie está en riesgo', async () => {
prisma.group.findUnique.mockResolvedValue(makeGroup());
prisma.classSession.findMany.mockResolvedValue(sessions.map((startsAt) => ({ id: 's', startsAt })));
prisma.classSession.findMany.mockResolvedValue(sessionRows);
prisma.attendee.findMany.mockResolvedValue([
{ id: 'a-1', fullName: 'Ana Pérez', phone: null },
]);
prisma.attendance.findMany.mockResolvedValue([
{ attendeeId: 'a-1', status: 'PRESENT', classSession: { startsAt: new Date(at(0)) } },
{ attendeeId: 'a-1', status: 'PRESENT', classSession: { startsAt: new Date(at(7)) } },
{ id: 'a-1', fullName: 'Ana Pérez', phone: null, createdAt: enrolled },
]);
prisma.attendance.findMany.mockResolvedValue([row('a-1', 'PRESENT', 0), row('a-1', 'PRESENT', 1)]);
const res = await makeApp({ id: userId }).request('/groups/group-1/students-at-risk');