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

@@ -0,0 +1,85 @@
import { describe, expect, test } from 'bun:test'
import type { BillingStatusDto } from '@gruperly/shared'
import { PLAN_LIMITS } from '@gruperly/shared'
import {
formatUsage,
hasPlanFeature,
isPlanProblem,
planBadgeVariant,
planFeatureList,
planNoticeMessage,
subscriptionStatusLabel,
} from '../src/features/billing/copy'
function status(overrides: Partial<BillingStatusDto> = {}): BillingStatusDto {
return {
plan: 'FREE',
status: null,
readOnly: false,
graceUntil: null,
paidThrough: null,
nextPaymentDate: null,
payerEmail: null,
priceAmount: 15000,
currency: 'ARS',
country: 'AR',
availableInCountry: true,
enforced: true,
usage: { groups: 1, attendees: 3 },
limits: PLAN_LIMITS.FREE,
...overrides,
}
}
/* El copy del plan lo comparten Ajustes, /billing, el onboarding y los avisos. */
describe('copy del plan', () => {
test('lista lo que incluye cada plan', () => {
expect(planFeatureList(PLAN_LIMITS.FREE)).toEqual([
{ label: '1 grupo', included: true },
{ label: 'Hasta 15 alumnos activos', included: true },
{ label: 'Cobros con Mercado Pago', included: false },
{ label: 'Mensajes automáticos por WhatsApp', included: false },
])
expect(planFeatureList(PLAN_LIMITS.PRO).every((feature) => feature.included)).toBe(true)
})
test('muestra el uso contra el límite', () => {
expect(formatUsage(3, 15)).toBe('3 de 15')
expect(formatUsage(42, null)).toBe('42')
})
test('nombra cada estado de la suscripción', () => {
expect(subscriptionStatusLabel('AUTHORIZED')).toBe('Activa')
expect(subscriptionStatusLabel('PENDING')).toBe('Pendiente de pago')
expect(subscriptionStatusLabel(null)).toBe('Sin suscripción')
})
test('avisa solo lectura antes que gracia', () => {
expect(planNoticeMessage(status())).toBeNull()
expect(planNoticeMessage(status({ readOnly: true }))?.tone).toBe('danger')
expect(planNoticeMessage(status({ plan: 'PRO', status: 'AUTHORIZED', graceUntil: '2026-11-01T00:00:00.000Z' }))?.tone).toBe('warning')
expect(planBadgeVariant(status({ readOnly: true }))).toBe('danger')
expect(planBadgeVariant(status({ plan: 'PRO', limits: PLAN_LIMITS.PRO }))).toBe('success')
})
test('el aviso de gracia distingue la cancelación de un cobro rechazado', () => {
const graceUntil = '2026-11-01T12:00:00.000Z'
expect(planNoticeMessage(status({ plan: 'PRO', status: 'CANCELLED', graceUntil }))?.text).toContain('Cancelaste Pro')
expect(planNoticeMessage(status({ plan: 'PRO', status: 'AUTHORIZED', graceUntil }))?.text).toContain('No pudimos cobrar')
})
test('deja pasar las funciones sin datos o sin límites aplicados', () => {
expect(hasPlanFeature(undefined, 'mpCharges')).toBe(true)
expect(hasPlanFeature(status({ enforced: false }), 'mpCharges')).toBe(true)
expect(hasPlanFeature(status(), 'mpCharges')).toBe(false)
expect(hasPlanFeature(status({ limits: PLAN_LIMITS.PRO }), 'whatsappAutomations')).toBe(true)
})
test('reconoce un 402 de plan y nada más', () => {
expect(isPlanProblem({ status: 402, problem: { code: 'plan_limit_reached' } })).toBe(true)
expect(isPlanProblem({ status: 402, problem: { code: 'otra_cosa' } })).toBe(false)
expect(isPlanProblem({ status: 409, problem: { code: 'plan_limit_reached' } })).toBe(false)
expect(isPlanProblem(new Error('x'))).toBe(false)
expect(isPlanProblem(null)).toBe(false)
})
})

View File

@@ -0,0 +1,48 @@
import { describe, expect, test } from 'bun:test'
import type { OnboardingStatusDto } from '@gruperly/shared'
import { resumeStep, visibleSteps } from '../src/features/onboarding/steps'
function status(overrides: Partial<OnboardingStatusDto> = {}): OnboardingStatusDto {
return {
step: 'NOT_STARTED',
paymentConnected: false,
whatsappConnected: false,
completed: false,
planSelected: false,
selectedPlan: null,
subscriptionStatus: null,
country: null,
...overrides,
}
}
describe('pasos del onboarding', () => {
test('con Free no se recorren Cobros ni WhatsApp', () => {
expect(visibleSteps('FREE')).toEqual(['welcome', 'country', 'plan', 'group', 'done'])
expect(visibleSteps('PRO')).toEqual(['welcome', 'country', 'plan', 'payment', 'whatsapp', 'group', 'done'])
expect(visibleSteps(null)).toEqual(visibleSteps('PRO'))
})
test('sin plan elegido arranca de cero', () => {
expect(resumeStep(status())).toBeNull()
})
test('con Free retoma en el primer grupo', () => {
expect(resumeStep(status({ planSelected: true, selectedPlan: 'FREE' }))).toBe('group')
})
test('una suscripción a Pro abandonada vuelve al paso Plan', () => {
expect(
resumeStep(status({ planSelected: true, selectedPlan: 'PRO', subscriptionStatus: 'PENDING' })),
).toBe('plan')
})
test('con Pro activo sigue por Cobros, WhatsApp o el grupo', () => {
const pro = { planSelected: true, selectedPlan: 'PRO' as const, subscriptionStatus: 'AUTHORIZED' as const }
expect(resumeStep(status(pro))).toBe('payment')
expect(resumeStep(status({ ...pro, step: 'PAYMENT_CONNECTED', paymentConnected: true }))).toBe('whatsapp')
expect(
resumeStep(status({ ...pro, step: 'PAYMENT_CONNECTED', paymentConnected: true, whatsappConnected: true })),
).toBe('group')
})
})