Files
gruperly/apps/backend/test/payments.test.ts
Jose Selesan d21e1c3730 feat: un miembro puede pertenecer a más de un grupo
- Nuevo modelo `Person` (`people`): contacto y notas por profe, con
  teléfono único (índice parcial `people_owner_phone_key`). `Attendee`
  pasa a ser la membresía (`group_id` + `person_id`, único por grupo).
- La cuenta de la app de alumnos se vincula a la persona
  (`student_account_people`): una sola cuenta ve todos sus grupos.
- Migración unifica attendees del mismo profe y teléfono, y fusiona sus
  cuentas de alumno (conserva la usada más recientemente).
- `POST /groups/:gid/attendees/:aid/add-to-group` y
  `GET /people/lookup` para detectar personas existentes al cargar;
  el alta responde `409 person_exists` y acepta `personId`. Bulk, lista
  de espera y preinscripciones vinculan solas.
- El límite del plan cuenta personas con al menos un grupo activo.
- Web: "Agregar a otro grupo", sección "También está en" en la ficha y
  aviso "Ya tenés a esta persona" en el alta individual.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-03 09:46:37 -03:00

132 lines
3.7 KiB
TypeScript

import { beforeEach, describe, expect, it, mock, vi } from 'bun:test';
import { Hono } from 'hono';
const db = {
group: {
findMany: mock(),
},
payment: {
findMany: mock(),
count: mock(),
},
};
mock.module('@/lib/prisma', () => ({
default: db,
getPrismaClient: mock(),
UnitOfWork: class {},
}));
import prisma from '@/lib/prisma';
import { paymentsRoutes } from '@/modules/payments';
const userId = 'user-1';
const payment = {
id: 'payment-1',
groupId: 'group-1',
attendeeId: 'attendee-1',
amount: { toString: () => '150.50' } as { toString(): string },
currency: 'ARS',
status: 'PENDING',
description: 'Mensualidad de Septiembre',
checkoutUrl: 'https://mercadopago.com/checkout/payment-1',
dueDate: new Date('2026-09-01T10:00:00.000Z'),
paidAt: null,
createdAt: new Date('2026-08-01T10:00:00.000Z'),
updatedAt: new Date('2026-08-01T10:00:00.000Z'),
group: { name: 'Cuadrilla Alfa', price: 150.5 },
attendee: { person: { fullName: 'Juana Pérez' } },
};
function makeApp(userValue: unknown) {
const app = new Hono();
app.use('*', async (c, next) => {
c.set('user', userValue as never);
await next();
});
app.route('/payments', paymentsRoutes);
return app;
}
describe('payments routes', () => {
beforeEach(() => {
vi.clearAllMocks();
});
it('lists payments scoped to the user, with attendee and group names', async () => {
prisma.group.findMany.mockResolvedValue([{ id: 'group-1' }]);
prisma.payment.findMany.mockResolvedValue([payment]);
prisma.payment.count.mockResolvedValue(1);
const res = await makeApp({ id: userId }).request('/payments');
expect(res.status).toBe(200);
expect(await res.json()).toEqual({
data: [
{
id: 'payment-1',
groupId: 'group-1',
groupName: 'Cuadrilla Alfa',
attendeeId: 'attendee-1',
attendeeName: 'Juana Pérez',
amount: 150.5,
currency: 'ARS',
status: 'PENDING',
description: 'Mensualidad de Septiembre',
checkoutUrl: payment.checkoutUrl,
dueDate: payment.dueDate.toISOString(),
paidAt: null,
createdAt: payment.createdAt.toISOString(),
updatedAt: payment.updatedAt.toISOString(),
},
],
pagination: { page: 1, pageSize: 10, total: 1, totalPages: 1 },
});
expect(prisma.group.findMany).toHaveBeenCalledWith({
where: {
OR: [{ createdById: userId }, { members: { some: { userId } } }],
},
select: { id: true },
});
expect(prisma.payment.findMany).toHaveBeenCalledWith({
where: { groupId: { in: ['group-1'] } },
include: {
group: { select: { name: true, price: true } },
attendee: { select: { person: { select: { fullName: true } } } },
},
skip: 0,
take: 10,
orderBy: { createdAt: 'desc' },
});
});
it('filters payments by group and status', async () => {
prisma.group.findMany.mockResolvedValue([{ id: 'group-1' }]);
prisma.payment.findMany.mockResolvedValue([]);
prisma.payment.count.mockResolvedValue(0);
const res = await makeApp({ id: userId }).request(
'/payments?groupId=group-1&status=OVERDUE',
);
expect(res.status).toBe(200);
expect(prisma.payment.findMany).toHaveBeenCalledWith(
expect.objectContaining({
where: { groupId: 'group-1', status: 'OVERDUE' },
}),
);
});
it('rejects requests without a session user', async () => {
const res = await makeApp(null).request('/payments');
expect(res.status).toBe(401);
});
it('rejects invalid pagination query parameters', async () => {
const res = await makeApp({ id: userId }).request('/payments?pageSize=101');
expect(res.status).toBe(400);
});
});