feat(billing): planes Free/Pro con Suscripciones de Mercado Pago

- Módulo billing: suscripción Pro mensual vía /preapproval con credenciales
  de plataforma por país, webhook firmado y reconciliación por polling.
- PlanGuard en los use-cases que crean grupos/alumnos, cobran con MP o
  activan automatizaciones (402 plan_limit_reached / plan_feature_required /
  plan_read_only), detrás de BILLING_ENFORCEMENT.
- Onboarding: paso Plan obligatorio (POST /onboarding/plan); con Free se
  saltean Cobros y WhatsApp.
- Web: pantalla /billing, aviso de Pro desde el MutationCache.
- Migración billing_subscriptions y docs/billing.md.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Jose Selesan
2026-10-23 15:49:27 -03:00
parent f4d2c26e29
commit f5e77c99f6
93 changed files with 4402 additions and 117 deletions

View File

@@ -12,6 +12,9 @@ const db = {
whatsappAccount: {
findUnique: mock(async () => null),
},
billingSubscription: {
findUnique: mock(async () => null),
},
group: {
create: mock(),
},
@@ -72,6 +75,9 @@ const firstGroupPayload = {
country: 'AR',
};
const noPlan = { planSelected: false, selectedPlan: null, subscriptionStatus: null, country: null };
const planSelectedAt = new Date('2026-08-01T09:00:00.000Z');
describe('onboarding routes', () => {
beforeEach(() => {
vi.clearAllMocks();
@@ -90,6 +96,7 @@ describe('onboarding routes', () => {
paymentConnected: false,
whatsappConnected: false,
completed: false,
...noPlan,
});
});
@@ -105,6 +112,7 @@ describe('onboarding routes', () => {
paymentConnected: true,
whatsappConnected: false,
completed: false,
...noPlan,
});
});
@@ -124,6 +132,7 @@ describe('onboarding routes', () => {
paymentConnected: false,
whatsappConnected: false,
completed: false,
...noPlan,
});
});
@@ -138,6 +147,27 @@ describe('onboarding routes', () => {
paymentConnected: true,
whatsappConnected: false,
completed: true,
...noPlan,
});
});
it('reports a chosen Pro plan while its subscription is still pending', async () => {
prisma.user.findUnique.mockResolvedValue({
id: userId,
onboardingCompleted: false,
planSelectedAt,
country: 'MX',
});
prisma.mpAccount.findUnique.mockResolvedValue(null);
prisma.billingSubscription.findUnique.mockResolvedValueOnce({ status: 'PENDING' });
const res = await makeApp({ id: userId }).request('/onboarding/status');
expect(await res.json()).toMatchObject({
planSelected: true,
selectedPlan: 'PRO',
subscriptionStatus: 'PENDING',
country: 'MX',
});
});
@@ -151,7 +181,7 @@ describe('onboarding routes', () => {
describe('POST /onboarding/first-group', () => {
it('creates the first group and completes onboarding', async () => {
prisma.user.findUnique.mockResolvedValue({ id: userId, onboardingCompleted: false });
prisma.user.findUnique.mockResolvedValue({ id: userId, onboardingCompleted: false, planSelectedAt });
prisma.group.create.mockResolvedValue(group);
const res = await makeApp({ id: userId }).request('/onboarding/first-group', {
@@ -202,7 +232,7 @@ describe('onboarding routes', () => {
});
it('creates the group without Mercado Pago linked, since that step is skippable', async () => {
prisma.user.findUnique.mockResolvedValue({ id: userId, onboardingCompleted: false });
prisma.user.findUnique.mockResolvedValue({ id: userId, onboardingCompleted: false, planSelectedAt });
prisma.mpAccount.findUnique.mockResolvedValue(null);
prisma.group.create.mockResolvedValue(group);
@@ -216,6 +246,20 @@ describe('onboarding routes', () => {
expect(prisma.group.create).toHaveBeenCalled();
});
it('rejects until a plan was chosen, since the Plan step is mandatory', async () => {
prisma.user.findUnique.mockResolvedValue({ id: userId, onboardingCompleted: false, planSelectedAt: null });
const res = await makeApp({ id: userId }).request('/onboarding/first-group', {
method: 'POST',
body: JSON.stringify(firstGroupPayload),
headers: { 'Content-Type': 'application/json' },
});
expect(res.status).toBe(409);
expect(await res.json()).toMatchObject({ code: 'plan_not_selected' });
expect(prisma.group.create).not.toHaveBeenCalled();
});
it('rejects when onboarding is already completed', async () => {
prisma.user.findUnique.mockResolvedValue({ id: userId, onboardingCompleted: true });