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>
This commit is contained in:
Jose Selesan
2026-11-11 15:08:48 -03:00
parent 5b3a035dcf
commit ac98495a6e
87 changed files with 2648 additions and 106 deletions

View File

@@ -1,10 +1,19 @@
import { beforeEach, describe, expect, it, mock, vi } from 'bun:test';
const tx = {
paymentCheckout: { update: mock() },
payment: { updateMany: mock() },
};
const db = {
payment: {
findMany: mock(),
update: mock(),
},
paymentCheckout: {
findMany: mock(async () => [] as unknown[]),
},
$transaction: mock(async (callback: (client: typeof tx) => Promise<unknown>) => callback(tx)),
};
mock.module('@/lib/prisma', () => ({
@@ -121,9 +130,54 @@ describe('reconcile payments', () => {
where: {
status: 'PENDING',
checkoutUrl: { not: null },
checkoutId: null,
createdAt: { lt: new Date('2026-10-01T11:55:00.000Z') },
},
}),
);
});
it('marks every unpaid fee of an approved combined link as PAID', async () => {
prisma.payment.findMany.mockResolvedValue([]);
db.paymentCheckout.findMany.mockResolvedValueOnce([{ id: 'chk-1', ownerId }]);
const getToken = mock(async () => okAccount());
const search = mock(async () => ({
id: '777',
status: 'approved',
externalReference: 'chk_chk-1',
liveMode: true,
dateApproved: '2026-10-01T12:30:00.000Z',
}));
const result = await new ReconcilePayments({ getToken, search }).execute();
expect(result).toEqual({ paid: 1, skipped: 0, failed: 0 });
expect(search).toHaveBeenCalledWith('token-1', 'chk_chk-1');
const paidAt = new Date('2026-10-01T12:30:00.000Z');
expect(tx.paymentCheckout.update).toHaveBeenCalledWith({
where: { id: 'chk-1' },
data: { status: 'PAID', mpPaymentId: '777', paidAt },
});
expect(tx.payment.updateMany).toHaveBeenCalledWith({
where: { checkoutId: 'chk-1', status: { in: ['PENDING', 'OVERDUE'] } },
data: { status: 'PAID', mpPaymentId: '777', paidAt },
});
});
it('ignores a payment of a combined link whose reference is not the checkout', async () => {
prisma.payment.findMany.mockResolvedValue([]);
db.paymentCheckout.findMany.mockResolvedValueOnce([{ id: 'chk-1', ownerId }]);
const search = mock(async () => ({
id: '777',
status: 'approved',
externalReference: 'chk-1',
liveMode: true,
dateApproved: null,
}));
const result = await new ReconcilePayments({ getToken: mock(async () => okAccount()), search }).execute();
expect(result).toEqual({ paid: 0, skipped: 0, failed: 0 });
expect(tx.payment.updateMany).not.toHaveBeenCalled();
});
});