feat: add country selection during onboarding and implement payment management features
- Introduced CountryStep component for selecting the country during onboarding, affecting payment currency. - Updated FirstGroupStep to include country information. - Enhanced PaymentsProvider to manage payment states and renewals. - Created PaymentRow component for displaying individual payment details. - Implemented PaymentsView for listing and filtering payments by group and status. - Added API endpoints for fetching and creating payments, including batch processing. - Defined payment schemas for validation and data handling. - Updated GroupHeader and GroupDetailView to include charging functionality for group members.
This commit is contained in:
129
apps/backend/test/payments-reconcile.test.ts
Normal file
129
apps/backend/test/payments-reconcile.test.ts
Normal file
@@ -0,0 +1,129 @@
|
||||
import { beforeEach, describe, expect, it, mock, vi } from 'bun:test';
|
||||
|
||||
const db = {
|
||||
payment: {
|
||||
findMany: mock(),
|
||||
update: mock(),
|
||||
},
|
||||
};
|
||||
|
||||
mock.module('@/lib/prisma', () => ({
|
||||
default: db,
|
||||
getPrismaClient: mock(),
|
||||
UnitOfWork: class {},
|
||||
}));
|
||||
|
||||
import prisma from '@/lib/prisma';
|
||||
import { MpApiError } from '@/modules/mp/lib/mp-oauth';
|
||||
import { ReconcilePayments } from '@/modules/payments/jobs';
|
||||
|
||||
const ownerId = 'user-1';
|
||||
const pendingPayment = (id: string, overrides: Record<string, unknown> = {}) => ({
|
||||
id,
|
||||
groupId: 'group-1',
|
||||
status: 'PENDING',
|
||||
checkoutUrl: 'https://mp.link/pay',
|
||||
createdAt: new Date(Date.now() - 10 * 60 * 1000),
|
||||
group: { createdById: ownerId },
|
||||
...overrides,
|
||||
});
|
||||
|
||||
function okAccount() {
|
||||
return {
|
||||
ok: true,
|
||||
value: { accountId: 'acc-1', accessToken: 'token-1', expiresAt: new Date() },
|
||||
};
|
||||
}
|
||||
|
||||
describe('reconcile payments', () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
});
|
||||
|
||||
it('marks an approved Mercado Pago payment as PAID', async () => {
|
||||
prisma.payment.findMany.mockResolvedValue([pendingPayment('pay-1')]);
|
||||
const getToken = mock(async () => okAccount());
|
||||
const search = mock(async () => ({
|
||||
id: '999999999',
|
||||
status: 'approved',
|
||||
externalReference: 'pay-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(prisma.payment.update).toHaveBeenCalledWith({
|
||||
where: { id: 'pay-1' },
|
||||
data: {
|
||||
status: 'PAID',
|
||||
mpPaymentId: '999999999',
|
||||
paidAt: new Date('2026-10-01T12:30:00.000Z'),
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
it('skips a payment that is still pending in Mercado Pago', async () => {
|
||||
prisma.payment.findMany.mockResolvedValue([pendingPayment('pay-1')]);
|
||||
const getToken = mock(async () => okAccount());
|
||||
const search = mock(async () => ({
|
||||
id: '999999999',
|
||||
status: 'pending',
|
||||
externalReference: 'pay-1',
|
||||
liveMode: true,
|
||||
dateApproved: null,
|
||||
}));
|
||||
|
||||
const result = await new ReconcilePayments({ getToken, search }).execute();
|
||||
|
||||
expect(result).toEqual({ paid: 0, skipped: 0, failed: 0 });
|
||||
expect(prisma.payment.update).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('skips rows when the owner has no usable token', async () => {
|
||||
prisma.payment.findMany.mockResolvedValue([pendingPayment('pay-1')]);
|
||||
const getToken = mock(async () => ({
|
||||
ok: false,
|
||||
error: { type: 'x', title: 'Conflict', status: 409, detail: 'x', code: 'mp_not_connected' },
|
||||
}));
|
||||
const search = mock(async () => {
|
||||
throw new Error('should not be called');
|
||||
});
|
||||
|
||||
const result = await new ReconcilePayments({ getToken, search }).execute();
|
||||
|
||||
expect(result).toEqual({ paid: 0, skipped: 1, failed: 0 });
|
||||
expect(search).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('counts an unreachable Mercado Pago as failed and leaves the row pending', async () => {
|
||||
prisma.payment.findMany.mockResolvedValue([pendingPayment('pay-1')]);
|
||||
const getToken = mock(async () => okAccount());
|
||||
const search = mock(async () => {
|
||||
throw new MpApiError(0, null);
|
||||
});
|
||||
|
||||
const result = await new ReconcilePayments({ getToken, search }).execute();
|
||||
|
||||
expect(result).toEqual({ paid: 0, skipped: 0, failed: 1 });
|
||||
expect(prisma.payment.update).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('only reconciles payments linked for at least five minutes', async () => {
|
||||
prisma.payment.findMany.mockResolvedValue([]);
|
||||
const now = new Date('2026-10-01T12:00:00.000Z');
|
||||
|
||||
await new ReconcilePayments({ now }).execute();
|
||||
|
||||
expect(prisma.payment.findMany).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
where: {
|
||||
status: 'PENDING',
|
||||
checkoutUrl: { not: null },
|
||||
createdAt: { lt: new Date('2026-10-01T11:55:00.000Z') },
|
||||
},
|
||||
}),
|
||||
);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user