Files
gruperly/apps/backend/test/attendee-get-by-id.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

147 lines
4.6 KiB
TypeScript

import { beforeEach, describe, expect, it, mock, vi } from 'bun:test';
import { Hono } from 'hono';
const db = {
group: {
findUnique: mock(),
},
attendee: {
findFirst: mock(),
},
personGuardian: {
findMany: mock(),
},
};
mock.module('@/lib/prisma', () => ({
default: db,
getPrismaClient: mock(),
UnitOfWork: class {},
}));
import prisma from '@/lib/prisma';
import getAttendeeRoute from '@/modules/attendees/features/get-by-id/route';
const userId = 'user-1';
function makeApp(userValue: unknown) {
const app = new Hono();
app.use('*', async (c, next) => {
c.set('user', userValue as never);
await next();
});
app.route('/groups', getAttendeeRoute);
return app;
}
function makeGroup(overrides: Record<string, unknown> = {}) {
return { id: 'group-1', name: 'Funcional', createdById: userId, members: [], ...overrides };
}
const attendee = {
id: 'a-1',
groupId: 'group-1',
personId: 'person-1',
person: {
id: 'person-1',
fullName: 'Ana Pérez',
email: 'ana@example.com',
phone: '5512345678',
notes: null,
// The adult in charge, a person of their own.
guardians: [{ guardian: { id: 'person-9', fullName: 'Luis Pérez', phone: '5598765432', email: null } }],
// Every membership of the person: this one and another group.
attendees: [
{ id: 'a-1', groupId: 'group-1', status: 'ACTIVE', group: { name: 'Funcional' } },
{ id: 'a-2', groupId: 'group-2', status: 'PAUSED', group: { name: 'Yoga' } },
],
},
notifyToken: 'token-1',
createdAt: new Date('2026-08-01T10:00:00.000Z'),
updatedAt: new Date('2026-08-01T10:00:00.000Z'),
payments: [
{ status: 'PENDING', dueDate: new Date('2026-10-10T00:00:00.000Z') },
],
};
describe('GET /groups/:groupId/attendees/:attendeeId', () => {
beforeEach(() => {
vi.clearAllMocks();
});
it('devuelve el participante del grupo', async () => {
prisma.group.findUnique.mockResolvedValue(makeGroup());
prisma.attendee.findFirst.mockResolvedValue(attendee);
prisma.personGuardian.findMany.mockResolvedValue([{ child: { id: 'person-2', fullName: 'Tomás Pérez' } }]);
const res = await makeApp({ id: userId }).request('/groups/group-1/attendees/a-1');
expect(res.status).toBe(200);
expect(await res.json()).toEqual({
id: 'a-1',
groupId: 'group-1',
personId: 'person-1',
fullName: 'Ana Pérez',
email: 'ana@example.com',
phone: '5512345678',
guardianPersonId: 'person-9',
guardianName: 'Luis Pérez',
guardianPhone: '5598765432',
guardianEmail: null,
notes: null,
payWithMercadoPago: true,
notifyToken: 'token-1',
createdAt: '2026-08-01T10:00:00.000Z',
updatedAt: '2026-08-01T10:00:00.000Z',
payment: { status: 'PENDING', dueDate: '2026-10-10T00:00:00.000Z' },
otherGroups: [{ attendeeId: 'a-2', groupId: 'group-2', groupName: 'Yoga', status: 'PAUSED' }],
siblings: [{ personId: 'person-2', fullName: 'Tomás Pérez' }],
});
expect(prisma.personGuardian.findMany).toHaveBeenCalledWith(
expect.objectContaining({ where: { guardianId: 'person-9', childId: { not: 'person-1' } } }),
);
expect(prisma.attendee.findFirst).toHaveBeenCalledWith({
where: { id: 'a-1', groupId: 'group-1' },
include: {
person: { select: expect.objectContaining({ fullName: true, attendees: expect.any(Object) }) },
payments: {
where: {
status: { not: 'CANCELLED' },
dueDate: { gte: expect.any(Date), lt: expect.any(Date) },
},
orderBy: { dueDate: 'desc' },
take: 1,
select: { status: true, dueDate: true },
},
},
});
});
it('devuelve 404 si el participante no pertenece al grupo', async () => {
prisma.group.findUnique.mockResolvedValue(makeGroup());
prisma.attendee.findFirst.mockResolvedValue(null);
const res = await makeApp({ id: userId }).request('/groups/group-1/attendees/nope');
expect(res.status).toBe(404);
expect(await res.json()).toMatchObject({ code: 'not_found' });
});
it('devuelve 403 si el usuario no tiene acceso al grupo', async () => {
prisma.group.findUnique.mockResolvedValue(
makeGroup({ createdById: 'otro-user', members: [] }),
);
const res = await makeApp({ id: userId }).request('/groups/group-1/attendees/a-1');
expect(res.status).toBe(403);
expect(await res.json()).toMatchObject({ code: 'group_access_denied' });
});
it('devuelve 401 si no hay sesión', async () => {
const res = await makeApp(undefined).request('/groups/group-1/attendees/a-1');
expect(res.status).toBe(401);
});
});