- 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>
158 lines
4.7 KiB
TypeScript
158 lines
4.7 KiB
TypeScript
import { beforeEach, describe, expect, it, mock, vi } from 'bun:test';
|
|
import { Hono } from 'hono';
|
|
|
|
const db = {
|
|
payment: {
|
|
findUnique: mock(),
|
|
update: mock(),
|
|
},
|
|
user: {
|
|
findUnique: mock(),
|
|
},
|
|
};
|
|
|
|
mock.module('@/lib/prisma', () => ({
|
|
default: db,
|
|
getPrismaClient: mock(),
|
|
UnitOfWork: class {},
|
|
}));
|
|
|
|
import prisma from '@/lib/prisma';
|
|
import { paymentsRoutes } from '@/modules/payments';
|
|
import { RenewPaymentLink } from '@/modules/payments/features/renew/use-case';
|
|
|
|
const userId = 'user-1';
|
|
const webOrigin = 'http://localhost:6173';
|
|
|
|
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;
|
|
}
|
|
|
|
function paymentWithoutLink(overrides: Record<string, unknown> = {}) {
|
|
return {
|
|
id: 'pay-1',
|
|
groupId: 'group-1',
|
|
attendeeId: 'att-1',
|
|
amount: { toString: () => '120.00' } as { toString(): string },
|
|
currency: 'ARS',
|
|
status: 'PENDING',
|
|
description: null,
|
|
checkoutUrl: null,
|
|
mpPreferenceId: null,
|
|
dueDate: new Date('2026-10-05T12:00:00.000Z'),
|
|
paidAt: null,
|
|
createdAt: new Date('2026-10-01T10:00:00.000Z'),
|
|
updatedAt: new Date('2026-10-01T10:00:00.000Z'),
|
|
attendee: { person: { fullName: 'Juana Pérez' } },
|
|
group: {
|
|
name: 'Vinyasa',
|
|
price: { toString: () => '120.00' },
|
|
createdById: userId,
|
|
members: [],
|
|
},
|
|
...overrides,
|
|
};
|
|
}
|
|
|
|
describe('renew payment link', () => {
|
|
beforeEach(() => {
|
|
vi.clearAllMocks();
|
|
});
|
|
|
|
it('creates a new link for a payment that lost it and returns the payment', async () => {
|
|
prisma.payment.findUnique.mockResolvedValue(paymentWithoutLink());
|
|
prisma.user.findUnique.mockResolvedValue({ id: userId, country: 'AR' });
|
|
prisma.payment.update.mockResolvedValue({
|
|
...paymentWithoutLink(),
|
|
checkoutUrl: 'https://mp.link/pay-1',
|
|
group: { name: 'Vinyasa', price: { toString: () => '120.00' } },
|
|
});
|
|
const getToken = mock(async () => ({
|
|
ok: true,
|
|
value: { accountId: 'acc-1', accessToken: 'token-1', expiresAt: new Date() },
|
|
}));
|
|
const createPreference = mock(async () => ({
|
|
id: 'pref-new',
|
|
initPoint: 'https://mp.link/pay-1',
|
|
sandboxInitPoint: null,
|
|
}));
|
|
|
|
const result = await new RenewPaymentLink({ getToken, createPreference, webOrigin }).execute(
|
|
'pay-1',
|
|
userId,
|
|
);
|
|
|
|
expect(result.ok).toBe(true);
|
|
if (!result.ok) throw new Error('expected ok');
|
|
expect(result.value).toMatchObject({
|
|
id: 'pay-1',
|
|
groupName: 'Vinyasa',
|
|
attendeeName: 'Juana Pérez',
|
|
checkoutUrl: 'https://mp.link/pay-1',
|
|
});
|
|
expect(createPreference).toHaveBeenCalledWith('token-1', {
|
|
title: 'Vinyasa',
|
|
unitPrice: 120,
|
|
currencyId: 'ARS',
|
|
externalReference: 'pay-1',
|
|
backUrl: `${webOrigin}/student/payments`,
|
|
});
|
|
expect(prisma.payment.update).toHaveBeenCalledWith({
|
|
where: { id: 'pay-1' },
|
|
data: { checkoutUrl: 'https://mp.link/pay-1', mpPreferenceId: 'pref-new' },
|
|
include: {
|
|
attendee: { select: { person: { select: { fullName: true } } } },
|
|
group: { select: { name: true, price: true } },
|
|
},
|
|
});
|
|
});
|
|
|
|
it('keeps an existing link: idempotent, no Mercado Pago call', async () => {
|
|
prisma.payment.findUnique.mockResolvedValue(
|
|
paymentWithoutLink({ checkoutUrl: 'https://mp.link/pay-1' }),
|
|
);
|
|
|
|
const res = await makeApp({ id: userId }).request('/payments/pay-1/renew', { method: 'POST' });
|
|
|
|
expect(res.status).toBe(200);
|
|
expect(prisma.payment.update).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it('answers 409 when the owner has no linked account', async () => {
|
|
prisma.payment.findUnique.mockResolvedValue(paymentWithoutLink());
|
|
prisma.user.findUnique.mockResolvedValue({ id: userId, country: 'AR' });
|
|
const getToken = mock(async () => ({
|
|
ok: false,
|
|
error: { type: 'x', title: 'Conflict', status: 409, detail: 'x', code: 'mp_not_connected' },
|
|
}));
|
|
|
|
const result = await new RenewPaymentLink({ getToken }).execute('pay-1', userId);
|
|
|
|
expect(result.ok).toBe(false);
|
|
expect(prisma.payment.update).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it('rejects a payment of a group the user has no access to', async () => {
|
|
prisma.payment.findUnique.mockResolvedValue(
|
|
paymentWithoutLink({
|
|
group: {
|
|
name: 'Vinyasa',
|
|
price: { toString: () => '120.00' },
|
|
createdById: 'someone-else',
|
|
members: [],
|
|
},
|
|
}),
|
|
);
|
|
|
|
const res = await makeApp({ id: userId }).request('/payments/pay-1/renew', { method: 'POST' });
|
|
|
|
expect(res.status).toBe(403);
|
|
expect(prisma.payment.update).not.toHaveBeenCalled();
|
|
});
|
|
}); |