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:
@@ -100,7 +100,7 @@ describe('GET /groups-risk/overview', () => {
|
||||
});
|
||||
});
|
||||
|
||||
it('cuenta el motivo: faltas seguidas y asistencia baja, sin repetir alumnos', async () => {
|
||||
it('cuenta el motivo: faltas seguidas o inactividad, sin repetir alumnos', async () => {
|
||||
prismaFetchGroupFindMany([group('group-1', 'Funcional'), group('group-2', 'Yoga')]);
|
||||
prismaFetchAttendeeFindMany([
|
||||
attendee('a-1', 'group-1'),
|
||||
@@ -113,6 +113,7 @@ describe('GET /groups-risk/overview', () => {
|
||||
{ id: 's2', groupId: 'group-1', startsAt: at(2) },
|
||||
{ id: 's3', groupId: 'group-1', startsAt: at(3) },
|
||||
{ id: 's4', groupId: 'group-1', startsAt: at(4) },
|
||||
{ id: 's7', groupId: 'group-1', startsAt: at(15) },
|
||||
{ id: 's5', groupId: 'group-2', startsAt: at(1) },
|
||||
{ id: 's6', groupId: 'group-2', startsAt: at(2) },
|
||||
]);
|
||||
@@ -121,11 +122,10 @@ describe('GET /groups-risk/overview', () => {
|
||||
attendance('a-1', 's1', 'ABSENT', at(1)),
|
||||
attendance('a-1', 's2', 'ABSENT', at(2)),
|
||||
attendance('a-1', 's3', 'PRESENT', at(3)),
|
||||
// No streak, but attends less than half of the month's classes.
|
||||
attendance('a-2', 's1', 'PRESENT', at(1)),
|
||||
attendance('a-2', 's2', 'ABSENT', at(2)),
|
||||
attendance('a-2', 's3', 'ABSENT', at(3)),
|
||||
attendance('a-2', 's4', 'ABSENT', at(4)),
|
||||
// A single absence, but 14 days between their last presence and the
|
||||
// class they missed (the ones in between have no record): inactivity.
|
||||
attendance('a-2', 's1', 'ABSENT', at(1)),
|
||||
attendance('a-2', 's7', 'PRESENT', at(15)),
|
||||
// Ok.
|
||||
attendance('a-3', 's1', 'PRESENT', at(1)),
|
||||
attendance('a-3', 's2', 'PRESENT', at(2)),
|
||||
@@ -144,7 +144,7 @@ describe('GET /groups-risk/overview', () => {
|
||||
{
|
||||
groupId: 'group-1',
|
||||
groupName: 'Funcional',
|
||||
riskLevel: 'MEDIUM',
|
||||
riskLevel: 'HIGH',
|
||||
absenceStreakCount: 1,
|
||||
lowAttendanceCount: 1,
|
||||
},
|
||||
|
||||
@@ -1,10 +1,5 @@
|
||||
import { describe, expect, it } from 'bun:test';
|
||||
import {
|
||||
classifyRisk,
|
||||
lastAttendedAt,
|
||||
monthlyAttendanceRate,
|
||||
trailingAbsentStreak,
|
||||
} from '@/modules/analytics/lib/risk';
|
||||
import { lastAttendedAt, monthlyAttendanceRate } from '@/modules/analytics/lib/risk';
|
||||
|
||||
const day = 24 * 60 * 60 * 1000;
|
||||
const at = (offsetDays: number) => new Date(Date.now() - offsetDays * day);
|
||||
@@ -12,25 +7,6 @@ const at = (offsetDays: number) => new Date(Date.now() - offsetDays * day);
|
||||
function entry(status: 'PRESENT' | 'ABSENT' | 'EXCUSED', offsetDays: number) {
|
||||
return { status, occurredAt: at(offsetDays) };
|
||||
}
|
||||
describe('trailingAbsentStreak', () => {
|
||||
it('devuelve 0 sin registros', () => {
|
||||
expect(trailingAbsentStreak([])).toBe(0);
|
||||
});
|
||||
|
||||
it('cuenta ausencias consecutivas sin justificar desde la más reciente', () => {
|
||||
expect(trailingAbsentStreak([entry('ABSENT', 0)])).toBe(1);
|
||||
expect(trailingAbsentStreak([entry('ABSENT', 0), entry('ABSENT', 7), entry('ABSENT', 14)])).toBe(3);
|
||||
});
|
||||
|
||||
it('rompe la cadena con PRESENT o EXCUSED', () => {
|
||||
expect(
|
||||
trailingAbsentStreak([entry('ABSENT', 0), entry('ABSENT', 7), entry('PRESENT', 14)]),
|
||||
).toBe(2);
|
||||
expect(trailingAbsentStreak([entry('EXCUSED', 0), entry('ABSENT', 7)])).toBe(0);
|
||||
expect(trailingAbsentStreak([entry('PRESENT', 0)])).toBe(0);
|
||||
});
|
||||
});
|
||||
|
||||
describe('lastAttendedAt', () => {
|
||||
it('devuelve la clase PRESENT más reciente', () => {
|
||||
const now = Date.now();
|
||||
@@ -59,21 +35,3 @@ describe('monthlyAttendanceRate', () => {
|
||||
expect(monthlyAttendanceRate([entry('ABSENT', 0)], 3)).toBe(0);
|
||||
});
|
||||
});
|
||||
|
||||
describe('classifyRisk', () => {
|
||||
it('HIGH con 3 o más ausencias consecutivas', () => {
|
||||
expect(classifyRisk(3, 80)).toBe('HIGH');
|
||||
expect(classifyRisk(5, 20)).toBe('HIGH');
|
||||
});
|
||||
|
||||
it('MEDIUM con 2 ausencias consecutivas o asistencia menor al 50%', () => {
|
||||
expect(classifyRisk(2, 90)).toBe('MEDIUM');
|
||||
expect(classifyRisk(1, 40)).toBe('MEDIUM');
|
||||
expect(classifyRisk(2, 40)).toBe('MEDIUM');
|
||||
});
|
||||
|
||||
it('sin riesgo si no cumple ningún umbral', () => {
|
||||
expect(classifyRisk(1, 60)).toBeNull();
|
||||
expect(classifyRisk(0, 60)).toBeNull();
|
||||
});
|
||||
});
|
||||
@@ -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');
|
||||
|
||||
|
||||
@@ -36,7 +36,7 @@ const groupA = {
|
||||
billingType: 'MONTHLY',
|
||||
dueDay: 5,
|
||||
capacity: 8,
|
||||
_count: { attendees: 3 },
|
||||
_count: { attendees: 3, preRegistrations: 2 },
|
||||
};
|
||||
|
||||
const groupB = {
|
||||
@@ -48,7 +48,7 @@ const groupB = {
|
||||
billingType: null,
|
||||
dueDay: null,
|
||||
capacity: null,
|
||||
_count: { attendees: 2 },
|
||||
_count: { attendees: 2, preRegistrations: 0 },
|
||||
};
|
||||
|
||||
const madeUpAmount = (value: string) => ({ toString: () => value });
|
||||
@@ -113,6 +113,8 @@ describe('GetHomeSummary', () => {
|
||||
attendeeName: 'Ana García',
|
||||
},
|
||||
],
|
||||
// Only the groups with something waiting: Cuadrilla B has none.
|
||||
pendingPreRegistrations: [{ groupId: 'g1', groupName: 'Cuadrilla A', count: 2 }],
|
||||
});
|
||||
|
||||
const where = {
|
||||
@@ -160,7 +162,7 @@ describe('GetHomeSummary', () => {
|
||||
billingType: null,
|
||||
dueDay: null,
|
||||
capacity: null,
|
||||
_count: { attendees: 0 },
|
||||
_count: { attendees: 0, preRegistrations: 0 },
|
||||
},
|
||||
]);
|
||||
|
||||
@@ -238,6 +240,7 @@ describe('GetHomeSummary', () => {
|
||||
stats: { groups: 0, attendees: 0, pendingPayments: 0, pendingAmount: 0 },
|
||||
nextClass: null,
|
||||
upcomingPayments: [],
|
||||
pendingPreRegistrations: [],
|
||||
});
|
||||
expect(prisma.payment.aggregate).not.toHaveBeenCalled();
|
||||
expect(prisma.payment.findMany).not.toHaveBeenCalled();
|
||||
@@ -259,6 +262,7 @@ describe('home routes', () => {
|
||||
stats: { groups: 0, attendees: 0, pendingPayments: 0, pendingAmount: 0 },
|
||||
nextClass: null,
|
||||
upcomingPayments: [],
|
||||
pendingPreRegistrations: [],
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
@@ -273,20 +273,26 @@ describe('payment reminders', () => {
|
||||
describe('re-engagement messages', () => {
|
||||
function atRiskGroup() {
|
||||
// Two sessions in the window, both missed by att-1: a streak of 2 (MEDIUM).
|
||||
db.classSession.findMany.mockResolvedValue([{ id: 's1' }, { id: 's2' }]);
|
||||
const s1 = new Date('2026-10-16T10:00:00Z');
|
||||
const s2 = new Date('2026-10-14T10:00:00Z');
|
||||
const enrolled = new Date('2026-09-01T10:00:00Z');
|
||||
db.classSession.findMany.mockResolvedValue([
|
||||
{ id: 's1', startsAt: s1 },
|
||||
{ id: 's2', startsAt: s2 },
|
||||
]);
|
||||
db.attendee.findMany
|
||||
.mockResolvedValueOnce([
|
||||
{ id: 'att-1', fullName: 'Ana López', phone: '1123456789' },
|
||||
{ id: 'att-2', fullName: 'Beto', phone: '1198765432' },
|
||||
{ id: 'att-1', fullName: 'Ana López', phone: '1123456789', createdAt: enrolled },
|
||||
{ id: 'att-2', fullName: 'Beto', phone: '1198765432', createdAt: enrolled },
|
||||
])
|
||||
.mockResolvedValueOnce([
|
||||
{ id: 'att-1', phone: '1123456789', guardianPhone: null },
|
||||
]);
|
||||
db.attendance.findMany.mockResolvedValue([
|
||||
{ attendeeId: 'att-1', status: 'ABSENT', classSession: { startsAt: new Date('2026-10-16T10:00:00Z') } },
|
||||
{ attendeeId: 'att-1', status: 'ABSENT', classSession: { startsAt: new Date('2026-10-14T10:00:00Z') } },
|
||||
{ attendeeId: 'att-2', status: 'PRESENT', classSession: { startsAt: new Date('2026-10-16T10:00:00Z') } },
|
||||
{ attendeeId: 'att-2', status: 'PRESENT', classSession: { startsAt: new Date('2026-10-14T10:00:00Z') } },
|
||||
{ attendeeId: 'att-1', classSessionId: 's1', status: 'ABSENT', classSession: { startsAt: s1 } },
|
||||
{ attendeeId: 'att-1', classSessionId: 's2', status: 'ABSENT', classSession: { startsAt: s2 } },
|
||||
{ attendeeId: 'att-2', classSessionId: 's1', status: 'PRESENT', classSession: { startsAt: s1 } },
|
||||
{ attendeeId: 'att-2', classSessionId: 's2', status: 'PRESENT', classSession: { startsAt: s2 } },
|
||||
]);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user