Files
gruperly/apps/backend/test/payments.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

136 lines
3.9 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,
checkoutId: null,
checkoutItemCount: null,
periodKey: null,
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 } } } },
checkout: { select: { _count: { select: { payments: 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);
});
});