Files
gruperly/apps/backend/test/attendees.test.ts
Jose Selesan ac98495a6e feat(payments): automatic monthly charges with combined links per payer
- Teacher picks a day of the month in Settings; an hourly job creates the
  fees of all monthly groups on that day in the teacher's time zone
  (one run per teacher and month, partial unique index per attendee/period).
- One Mercado Pago link per payer (PaymentCheckout): the adult in charge
  bundles their kids and their own fees; external_reference chk_<id>,
  dedicated webhook and reconciliation mark every fee PAID at once.
- Person.payWithMercadoPago toggle (default on): off creates the fee
  without a link so the student pays by transfer.
- A fee paid some other way supersedes its combined link and expires the
  preference; renewing rebuilds the combined link or a single one.
- Optional WhatsApp delivery (PAYMENT_LINK kind, new Kapso template);
  weekly reminders now go once per combined link.
- Web: auto charge settings card, per-student toggle, combined link label
  in Cobros and "Pay all" in the student app, in the four locales.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-11 15:08:48 -03:00

138 lines
4.0 KiB
TypeScript

import { beforeEach, describe, expect, it, mock, vi } from 'bun:test';
import { RECENT_ATTENDANCE_LIMIT } from '@gruperly/shared';
import { Hono } from 'hono';
mock.module('@/lib/prisma', () => ({
default: {
attendee: {
findMany: mock(),
count: mock(),
},
},
getPrismaClient: mock(),
UnitOfWork: class {},
}));
import prisma from '@/lib/prisma';
import { attendeesRoutes } from '@/modules/attendees';
const app = new Hono();
app.route('/attendees', attendeesRoutes);
const personSelect = {
id: true,
fullName: true,
email: true,
phone: true,
notes: true,
payWithMercadoPago: true,
guardians: {
take: 1,
orderBy: { createdAt: 'asc' },
select: { guardian: { select: { id: true, fullName: true, phone: true, email: true } } },
},
};
const person = {
id: 'person-1',
fullName: 'Ana Pérez',
email: 'ana@example.com',
phone: null,
notes: null,
guardians: [{ guardian: { id: 'person-9', fullName: 'Luis Pérez', phone: '1199990000', email: null } }],
};
const attendeeRow = {
id: 'attendee-1',
groupId: 'group-1',
personId: 'person-1',
notifyToken: null,
createdAt: new Date('2026-08-01T10:00:00.000Z'),
updatedAt: new Date('2026-08-01T10:00:00.000Z'),
};
// What the Prisma mock returns: the row plus the summary relations.
const attendee = {
...attendeeRow,
person,
attendances: [{ status: 'ABSENT' }, { status: 'PRESENT' }, { status: 'PRESENT' }],
payments: [{ status: 'OVERDUE', dueDate: new Date('2026-09-10T00:00:00.000Z') }],
};
describe('attendees routes', () => {
beforeEach(() => {
vi.clearAllMocks();
});
it('lists attendees with pagination', async () => {
prisma.attendee.findMany.mockResolvedValue([attendee]);
prisma.attendee.count.mockResolvedValue(21);
const res = await app.request('/attendees?page=2&pageSize=10');
expect(res.status).toBe(200);
expect(await res.json()).toEqual({
data: [
{
...attendeeRow,
fullName: person.fullName,
email: person.email,
phone: person.phone,
guardianPersonId: 'person-9',
guardianName: 'Luis Pérez',
guardianPhone: '1199990000',
guardianEmail: null,
notes: person.notes,
payWithMercadoPago: true,
createdAt: attendeeRow.createdAt.toISOString(),
updatedAt: attendeeRow.updatedAt.toISOString(),
// The query comes most recent first; the row shows them
// the other way around, with the most recent class on the right.
recentAttendance: ['PRESENT', 'PRESENT', 'ABSENT'],
payment: { status: 'OVERDUE', dueDate: '2026-09-10T00:00:00.000Z' },
},
],
pagination: { page: 2, pageSize: 10, total: 21, totalPages: 3 },
});
expect(prisma.attendee.findMany).toHaveBeenCalledWith({
skip: 10,
take: 10,
orderBy: { createdAt: 'desc' },
include: {
person: { select: personSelect },
attendances: {
orderBy: { classSession: { startsAt: 'desc' } },
take: RECENT_ATTENDANCE_LIMIT,
select: { status: true },
},
payments: {
where: { status: { not: 'CANCELLED' } },
orderBy: { dueDate: 'desc' },
take: 3,
select: { status: true, dueDate: true },
},
},
});
expect(prisma.attendee.count).toHaveBeenCalledWith();
});
it('uses default pagination', async () => {
prisma.attendee.findMany.mockResolvedValue([]);
prisma.attendee.count.mockResolvedValue(0);
const res = await app.request('/attendees');
expect(res.status).toBe(200);
expect(await res.json()).toEqual({
data: [],
pagination: { page: 1, pageSize: 10, total: 0, totalPages: 0 },
});
});
it('rejects invalid pagination query parameters', async () => {
const res = await app.request('/attendees?page=0&pageSize=25');
expect(res.status).toBe(400);
expect(await res.json()).toMatchObject({ status: 400, title: 'Bad Request' });
});
});