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:
86
apps/backend/test/billing-plan-guards.test.ts
Normal file
86
apps/backend/test/billing-plan-guards.test.ts
Normal file
@@ -0,0 +1,86 @@
|
||||
import { describe, expect, it, mock } from 'bun:test';
|
||||
import { err } from '@gruperly/shared';
|
||||
import {
|
||||
planFeatureRequiredProblem,
|
||||
planLimitReachedProblem,
|
||||
} from '@/http/problem-builders';
|
||||
import { BulkCreateAttendeesUseCase } from '@/modules/attendees/features/bulk-create/use-case';
|
||||
import { CreateGroup } from '@/modules/groups/features/create/use-case';
|
||||
import { CreatePaymentBatch } from '@/modules/payments/features/create-batch/use-case';
|
||||
import { UpdateGroupAutomationsUseCase } from '@/modules/whatsapp/features/update-automations/use-case';
|
||||
|
||||
const ownerId = 'owner-1';
|
||||
|
||||
describe('plan guards in the use cases', () => {
|
||||
it('does not create a group past the Free limit', async () => {
|
||||
const unitOfWork = { executeResult: mock() };
|
||||
const planGuard = { canCreateGroup: mock(async () => err(planLimitReachedProblem('groups', 1))) };
|
||||
|
||||
const result = await new CreateGroup({ unitOfWork: unitOfWork as never, planGuard }).execute(
|
||||
{} as never,
|
||||
ownerId,
|
||||
);
|
||||
|
||||
expect(result.ok).toBe(false);
|
||||
if (!result.ok) expect(result.error).toMatchObject({ status: 402, code: 'plan_limit_reached' });
|
||||
expect(unitOfWork.executeResult).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('checks a bulk import against the group owner, with the number of new attendees', async () => {
|
||||
const db = {
|
||||
group: { findUnique: mock(async () => ({ id: 'g1', createdById: ownerId, members: [{ userId: 'admin-1' }] })) },
|
||||
attendee: { findMany: mock(async () => [{ phone: '1111' }]), create: mock() },
|
||||
};
|
||||
const planGuard = { canAddAttendees: mock(async () => err(planLimitReachedProblem('attendees', 15))) };
|
||||
|
||||
const result = await new BulkCreateAttendeesUseCase({ db: db as never, planGuard }).execute('g1', 'admin-1', {
|
||||
attendees: [
|
||||
{ fullName: 'Ana', phone: '2222' },
|
||||
{ fullName: 'Beto', phone: '3333' },
|
||||
{ fullName: 'Repetido', phone: '1111' },
|
||||
],
|
||||
} as never);
|
||||
|
||||
expect(planGuard.canAddAttendees).toHaveBeenCalledWith(ownerId, 2);
|
||||
expect(result.ok).toBe(false);
|
||||
expect(db.attendee.create).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('keeps charging with Mercado Pago for Pro', async () => {
|
||||
const db = {
|
||||
group: { findUnique: mock(async () => ({ id: 'g1', createdById: ownerId, price: 100, members: [] })) },
|
||||
attendee: { findMany: mock() },
|
||||
payment: { create: mock() },
|
||||
user: { findUnique: mock() },
|
||||
};
|
||||
const planGuard = { requireFeature: mock(async () => err(planFeatureRequiredProblem('mpCharges'))) };
|
||||
|
||||
const result = await new CreatePaymentBatch({ db: db as never, planGuard }).execute('g1', ownerId, {
|
||||
dueDate: '2026-11-05T12:00:00.000Z',
|
||||
});
|
||||
|
||||
expect(planGuard.requireFeature).toHaveBeenCalledWith(ownerId, 'mpCharges');
|
||||
expect(result.ok).toBe(false);
|
||||
if (!result.ok) expect(result.error).toMatchObject({ status: 402, code: 'plan_feature_required' });
|
||||
expect(db.attendee.findMany).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('blocks turning automations on for Free, but lets them be turned off', async () => {
|
||||
const db = {
|
||||
group: {
|
||||
findUnique: mock(async () => ({ id: 'g1', createdById: ownerId })),
|
||||
update: mock(async () => ({ autoPaymentReminders: false, autoReengagement: false })),
|
||||
},
|
||||
};
|
||||
const planGuard = { requireFeature: mock(async () => err(planFeatureRequiredProblem('whatsappAutomations'))) };
|
||||
const useCase = new UpdateGroupAutomationsUseCase({ db: db as never, planGuard });
|
||||
|
||||
const on = await useCase.execute('g1', ownerId, { autoPaymentReminders: true });
|
||||
expect(on.ok).toBe(false);
|
||||
expect(db.group.update).not.toHaveBeenCalled();
|
||||
|
||||
const off = await useCase.execute('g1', ownerId, { autoPaymentReminders: false });
|
||||
expect(off.ok).toBe(true);
|
||||
expect(db.group.update).toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user