Files
gruperly/apps/backend/test/attention.test.ts
Jose Selesan 58c776e18c feat(groups): kids groups with an adult in charge
- Add the `kidsGroups` plan feature (Pro) and `Group.audience` (ADULTS | KIDS);
  creating or switching a group to KIDS requires the feature, existing KIDS
  groups keep working after a downgrade.
- Model the adult in charge as a Person linked through `person_guardians`;
  migrate the old guardian_name/guardian_phone columns and drop them.
- Kids quick add: kid + guardian form, guardian lookup by phone to pick an
  existing kid or add a sibling; backend rejects KIDS adds without guardian.
- Attendee page: "Adulto a cargo" section with siblings, editable via
  PATCH /groups/:gid/attendees/:aid/guardian.
- Contact phone (WhatsApp jobs, attention, student access invite, students at
  risk, attendee actions) prefers the guardian's phone.
- Edit group screen (PATCH /groups/:gid, owner only); switching back to ADULTS
  is blocked while kids have no phone of their own.
- Hide bulk import in KIDS groups until it carries guardian data.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-11 13:27:13 -03:00

131 lines
5.1 KiB
TypeScript

import { beforeEach, describe, expect, it, mock } from 'bun:test';
import { Prisma } from '@generated/prisma/client';
import { GetAttentionOverview } from '@/modules/attention/features/get-overview/use-case';
import { ResolveStudentAlert } from '@/modules/attention/features/resolve/use-case';
const NOW = new Date('2026-10-27T20:00:00.000Z');
const DAY_MS = 24 * 60 * 60 * 1000;
const at = (days: number) => new Date(NOW.getTime() - days * DAY_MS);
const db = {
group: { findMany: mock() },
attendee: { findMany: mock(), findFirst: mock() },
classSession: { findMany: mock() },
attendance: { findMany: mock() },
payment: { findMany: mock() },
attendeeAlertResolution: { findMany: mock(), upsert: mock() },
user: { findUnique: mock() },
};
function overview() {
return new GetAttentionOverview({ db: db as never, now: NOW });
}
beforeEach(() => {
for (const delegate of Object.values(db)) {
for (const fn of Object.values(delegate)) fn.mockReset();
}
});
describe('GetAttentionOverview', () => {
it('sin grupos no consulta nada más', async () => {
db.group.findMany.mockResolvedValue([]);
const result = await overview().execute('user-1');
expect(result).toEqual({ ok: true, value: { groups: [] } });
expect(db.attendee.findMany).not.toHaveBeenCalled();
});
it('devuelve todos los miembros con sus alertas, sin las resueltas', async () => {
db.user.findUnique.mockResolvedValue({ country: 'AR' });
db.group.findMany.mockResolvedValue([{ id: 'g1', name: 'Natación', capacity: 30 }]);
db.attendee.findMany.mockResolvedValue([
{ id: 'a1', groupId: 'g1', fullName: 'Agustina', phone: '1155550000', createdAt: at(90) },
{ id: 'a2', groupId: 'g1', fullName: 'Valentina', phone: null, guardians: [{ guardian: { phone: '1166660000' } }], createdAt: at(90) },
{ id: 'a3', groupId: 'g1', fullName: 'Felipe', phone: null, createdAt: at(90) },
]);
db.classSession.findMany.mockResolvedValue([
{ id: 's1', groupId: 'g1', startsAt: at(1) },
{ id: 's2', groupId: 'g1', startsAt: at(3) },
{ id: 's3', groupId: 'g1', startsAt: at(5) },
]);
const row = (attendeeId: string, sessionId: string, status: string, days: number) => ({
attendeeId,
classSessionId: sessionId,
status,
classSession: { startsAt: at(days) },
});
db.attendance.findMany.mockResolvedValue([
row('a1', 's1', 'ABSENT', 1),
row('a1', 's2', 'ABSENT', 3),
row('a1', 's3', 'ABSENT', 5),
// Valentina missed two classes, but the professor already resolved it.
row('a2', 's1', 'ABSENT', 1),
row('a2', 's2', 'ABSENT', 3),
row('a3', 's1', 'PRESENT', 1),
]);
db.payment.findMany.mockResolvedValue([
{
id: 'p1',
attendeeId: 'a2',
amount: new Prisma.Decimal('15000.00'),
currency: 'ARS',
dueDate: new Date('2026-10-10T12:00:00.000Z'),
checkoutUrl: 'https://mp.test/p1',
},
]);
db.attendeeAlertResolution.findMany.mockResolvedValue([
{ attendeeId: 'a2', kind: 'ATTENDANCE', signature: 's1' },
]);
const result = await overview().execute('user-1');
if (!result.ok) throw new Error('expected ok');
const [group] = result.value.groups;
expect(group).toMatchObject({ groupId: 'g1', groupName: 'Natación', capacity: 30 });
expect(group.members.map((member) => member.attendeeId)).toEqual(['a1', 'a2', 'a3']);
const [agustina, valentina, felipe] = group.members;
expect(agustina.phone).toBe('1155550000');
expect(agustina.whatsappPhone).toBe('+5491155550000');
expect(agustina.alerts).toMatchObject([{ tipo: 'asistencia', gravedad: 'urgente', etiqueta: '3 seguidas' }]);
expect(valentina.phone).toBe('1166660000');
expect(valentina.alerts).toMatchObject([{ tipo: 'pago', gravedad: 'vigilar', etiqueta: 'Cuota vencida' }]);
expect(valentina.overduePayments).toEqual([
{ id: 'p1', amount: 15000, currency: 'ARS', dueDate: '2026-10-10T12:00:00.000Z', checkoutUrl: 'https://mp.test/p1' },
]);
expect(felipe.alerts).toEqual([]);
});
});
describe('ResolveStudentAlert', () => {
it('guarda la firma con upsert por alumno y tipo', async () => {
db.attendee.findFirst.mockResolvedValue({ id: 'a1' });
const result = await new ResolveStudentAlert({ db: db as never }).execute('a1', 'user-1', {
tipo: 'pago',
firma: 'p1,p2',
});
expect(result).toEqual({ ok: true, value: { attendeeId: 'a1', tipo: 'pago', firma: 'p1,p2' } });
expect(db.attendeeAlertResolution.upsert).toHaveBeenCalledWith(
expect.objectContaining({
where: { attendeeId_kind: { attendeeId: 'a1', kind: 'PAYMENT' } },
create: expect.objectContaining({ signature: 'p1,p2', resolvedById: 'user-1' }),
}),
);
});
it('un alumno de un grupo ajeno es 404 y no escribe nada', async () => {
db.attendee.findFirst.mockResolvedValue(null);
const result = await new ResolveStudentAlert({ db: db as never }).execute('a9', 'user-1', {
tipo: 'asistencia',
firma: 's1',
});
expect(result.ok).toBe(false);
if (!result.ok) expect(result.error.status).toBe(404);
expect(db.attendeeAlertResolution.upsert).not.toHaveBeenCalled();
});
});