feat: rediseño de Grupos y pantalla "Requiere atención"
- Grupos: subtítulo con resumen (grupos · alumnos · lugares libres), sección "Requiere atención" con los 4 alumnos más urgentes y tarjetas con avatares (punteados por lugar libre en grupos chicos, superpuestos con "+N" en grandes; con alerta primero y anillo de gravedad). - Pantalla nueva /groups/attention: filtros Todos/Asistencia/Pagos, secciones Urgente y A vigilar, Contactar / Enviar recordatorio por wa.me y "Resuelto" persistente con update optimista. - FAB "Nuevo grupo" que se pliega al scrollear (respeta reduced motion); los toasts en mobile suben para no quedar debajo. - Alertas centralizadas en computeStudentAlerts (shared). La regla de asistencia pasa a: urgente = 3 seguidas o 4 de las últimas 6; a vigilar = 2 de las últimas 4 (sin inactividad por días ni tendencia). Cuotas: 1 vencida vigilar, 2+ urgente. - Backend: GET /attention y POST /attention/:attendeeId/resolve; tabla attendee_alert_resolutions (la alerta vuelve si cambia la firma). - Tokens de gravedad para claro y oscuro; se quita el indicador de pills. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
193
apps/backend/test/student-alerts.test.ts
Normal file
193
apps/backend/test/student-alerts.test.ts
Normal file
@@ -0,0 +1,193 @@
|
||||
import { describe, expect, it } from 'bun:test';
|
||||
import {
|
||||
applyResolutions,
|
||||
compareByWorstAlert,
|
||||
computeStudentAlerts,
|
||||
type MemberAttendance,
|
||||
type MemberClassSession,
|
||||
STUDENT_ALERT_RULES,
|
||||
type StudentAlert,
|
||||
worstAlert,
|
||||
} from '@gruperly/shared';
|
||||
|
||||
/* The single source of the "Requiere atención" alerts. These tests pin the
|
||||
thresholds (attendance through the shared risk rule, fees through
|
||||
`STUDENT_ALERT_RULES`), the copy the screens show and the order. */
|
||||
|
||||
const DAY_MS = 24 * 60 * 60 * 1000;
|
||||
const NOW = new Date('2026-10-27T20:00:00.000Z');
|
||||
const daysAgo = (days: number) => new Date(NOW.getTime() - days * DAY_MS).toISOString();
|
||||
|
||||
type Status = MemberClassSession['status'];
|
||||
|
||||
/** Classes on consecutive days, oldest first; the last one is `ses_1`. */
|
||||
function attendance(statuses: Status[]): MemberAttendance {
|
||||
return {
|
||||
attendeeId: 'at_1',
|
||||
fullName: 'Agustina Pérez',
|
||||
enrolledAt: daysAgo(365),
|
||||
sessions: statuses.map((status, index) => ({
|
||||
sessionId: `ses_${statuses.length - index}`,
|
||||
startsAt: daysAgo(statuses.length - index),
|
||||
status,
|
||||
})),
|
||||
};
|
||||
}
|
||||
|
||||
const fee = (id: string, dueDate: string) => ({ id, dueDate });
|
||||
|
||||
function alertsFor(statuses: Status[], fees: { id: string; dueDate: string }[] = []) {
|
||||
return computeStudentAlerts({ attendance: attendance(statuses), overduePayments: fees }, NOW);
|
||||
}
|
||||
|
||||
describe('computeStudentAlerts: asistencia', () => {
|
||||
it('sin faltas ni cuotas no hay alertas', () => {
|
||||
expect(alertsFor(['PRESENT', 'PRESENT', 'PRESENT'])).toEqual([]);
|
||||
});
|
||||
|
||||
it('3 faltas seguidas son urgentes y lo dicen así', () => {
|
||||
const [alert] = alertsFor(['PRESENT', 'ABSENT', 'ABSENT', 'ABSENT']);
|
||||
expect(alert).toMatchObject({
|
||||
tipo: 'asistencia',
|
||||
gravedad: 'urgente',
|
||||
etiqueta: '3 seguidas',
|
||||
detalle: 'No viene hace 3 clases seguidas.',
|
||||
firma: 'ses_1',
|
||||
});
|
||||
});
|
||||
|
||||
it('4 de las últimas 6 sin racha son urgentes', () => {
|
||||
const [alert] = alertsFor(['PRESENT', 'ABSENT', 'ABSENT', 'PRESENT', 'ABSENT', 'ABSENT', 'PRESENT']);
|
||||
expect(alert).toMatchObject({
|
||||
gravedad: 'urgente',
|
||||
etiqueta: '4 faltas',
|
||||
detalle: 'Faltó a 4 de las últimas 6 clases.',
|
||||
firma: 'ses_2',
|
||||
});
|
||||
});
|
||||
|
||||
it('2 faltas recientes son "a vigilar"', () => {
|
||||
expect(alertsFor(['PRESENT', 'PRESENT', 'ABSENT', 'ABSENT'])[0]).toMatchObject({
|
||||
gravedad: 'vigilar',
|
||||
etiqueta: '2 faltas',
|
||||
detalle: 'Faltó a las últimas 2 clases.',
|
||||
});
|
||||
expect(alertsFor(['PRESENT', 'ABSENT', 'PRESENT', 'ABSENT', 'PRESENT'])[0]).toMatchObject({
|
||||
gravedad: 'vigilar',
|
||||
detalle: 'Faltó a 2 de las últimas 4 clases.',
|
||||
});
|
||||
});
|
||||
|
||||
it('una sola falta no alcanza', () => {
|
||||
expect(alertsFor(['PRESENT', 'PRESENT', 'PRESENT', 'ABSENT'])).toEqual([]);
|
||||
});
|
||||
|
||||
it('las justificadas y las clases sin registro no cuentan como faltas', () => {
|
||||
expect(alertsFor(['PRESENT', 'EXCUSED', 'ABSENT', null, 'EXCUSED'])).toEqual([]);
|
||||
});
|
||||
});
|
||||
|
||||
describe('computeStudentAlerts: pagos', () => {
|
||||
it('1 cuota vencida es "a vigilar" con su fecha', () => {
|
||||
expect(alertsFor([], [fee('pay_1', '2026-10-10T12:00:00.000Z')])).toEqual([
|
||||
{
|
||||
tipo: 'pago',
|
||||
gravedad: 'vigilar',
|
||||
etiqueta: 'Cuota vencida',
|
||||
detalle: 'La cuota de octubre venció el 10/10.',
|
||||
firma: 'pay_1',
|
||||
peso: 1,
|
||||
},
|
||||
]);
|
||||
});
|
||||
|
||||
it(`${STUDENT_ALERT_RULES.urgentOverdue} o más cuotas vencidas son urgentes y nombran los meses`, () => {
|
||||
const [alert] = alertsFor(
|
||||
[],
|
||||
[fee('pay_b', '2026-10-10T12:00:00.000Z'), fee('pay_a', '2026-09-10T12:00:00.000Z')],
|
||||
);
|
||||
expect(alert).toMatchObject({
|
||||
gravedad: 'urgente',
|
||||
etiqueta: '2 cuotas',
|
||||
detalle: 'Debe las cuotas de septiembre y octubre.',
|
||||
firma: 'pay_a,pay_b',
|
||||
});
|
||||
});
|
||||
|
||||
it('con tres meses los separa con coma y "y"', () => {
|
||||
const [alert] = alertsFor(
|
||||
[],
|
||||
[
|
||||
fee('p1', '2026-08-10T12:00:00.000Z'),
|
||||
fee('p2', '2026-09-10T12:00:00.000Z'),
|
||||
fee('p3', '2026-10-10T12:00:00.000Z'),
|
||||
],
|
||||
);
|
||||
expect(alert.detalle).toBe('Debe las cuotas de agosto, septiembre y octubre.');
|
||||
});
|
||||
|
||||
it('dos cuotas del mismo mes caen al conteo', () => {
|
||||
const [alert] = alertsFor([], [fee('p1', '2026-10-01T12:00:00.000Z'), fee('p2', '2026-10-20T12:00:00.000Z')]);
|
||||
expect(alert.detalle).toBe('Debe 2 cuotas vencidas.');
|
||||
});
|
||||
});
|
||||
|
||||
describe('orden por gravedad', () => {
|
||||
it('con varias alertas, la urgente va primero', () => {
|
||||
const alerts = alertsFor(
|
||||
['PRESENT', 'PRESENT', 'ABSENT', 'ABSENT'],
|
||||
[fee('p1', '2026-09-10T12:00:00.000Z'), fee('p2', '2026-10-10T12:00:00.000Z')],
|
||||
);
|
||||
expect(alerts.map((alert) => [alert.tipo, alert.gravedad])).toEqual([
|
||||
['pago', 'urgente'],
|
||||
['asistencia', 'vigilar'],
|
||||
]);
|
||||
expect(worstAlert(alerts)?.tipo).toBe('pago');
|
||||
});
|
||||
|
||||
it('ordena alumnos por su peor alerta, después por la fuerza y por nombre', () => {
|
||||
const alert = (gravedad: StudentAlert['gravedad'], peso: number): StudentAlert => ({
|
||||
tipo: 'asistencia',
|
||||
gravedad,
|
||||
etiqueta: '',
|
||||
detalle: '',
|
||||
firma: 'x',
|
||||
peso,
|
||||
});
|
||||
const students = [
|
||||
{ fullName: 'Zoe', alerts: [] },
|
||||
{ fullName: 'Martina', alerts: [alert('vigilar', 2)] },
|
||||
{ fullName: 'Tomás', alerts: [alert('urgente', 3)] },
|
||||
{ fullName: 'Agustina', alerts: [alert('urgente', 4)] },
|
||||
{ fullName: 'Bruno', alerts: [alert('vigilar', 2)] },
|
||||
];
|
||||
expect([...students].sort(compareByWorstAlert).map((student) => student.fullName)).toEqual([
|
||||
'Agustina',
|
||||
'Tomás',
|
||||
'Bruno',
|
||||
'Martina',
|
||||
'Zoe',
|
||||
]);
|
||||
});
|
||||
});
|
||||
|
||||
describe('applyResolutions', () => {
|
||||
const statuses: Status[] = ['PRESENT', 'ABSENT', 'ABSENT', 'ABSENT'];
|
||||
|
||||
it('oculta la alerta resuelta mientras la firma sea la misma', () => {
|
||||
const alerts = alertsFor(statuses);
|
||||
expect(applyResolutions(alerts, [{ tipo: 'asistencia', firma: 'ses_1' }])).toEqual([]);
|
||||
});
|
||||
|
||||
it('una falta nueva cambia la firma y la alerta vuelve', () => {
|
||||
const alerts = alertsFor([...statuses, 'ABSENT']);
|
||||
expect(alerts[0].firma).toBe('ses_1');
|
||||
// The resolution was stored when the last absence was `ses_2`.
|
||||
expect(applyResolutions(alerts, [{ tipo: 'asistencia', firma: 'ses_2' }])).toHaveLength(1);
|
||||
});
|
||||
|
||||
it('resolver la asistencia no oculta los pagos', () => {
|
||||
const alerts = alertsFor(statuses, [fee('p1', '2026-10-10T12:00:00.000Z')]);
|
||||
expect(applyResolutions(alerts, [{ tipo: 'asistencia', firma: 'ses_1' }]).map((a) => a.tipo)).toEqual(['pago']);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user