Files
gruperly/apps/web/src/features/groups/detail/GroupAutomationsCard.tsx
Jose Selesan f5e77c99f6 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>
2026-10-23 15:49:27 -03:00

109 lines
4.1 KiB
TypeScript

import type { GroupAutomationsDto } from '@gruperly/shared'
import { Link } from '@tanstack/react-router'
import { MessageCircle } from 'lucide-react'
import { Badge, Switch } from '../../../components/ui'
import { hasPlanFeature, useBillingStatus } from '../../billing'
import { automationBlockedReason, useWhatsappStatus, WEEKLY_SEND_LABEL } from '../../whatsapp'
import { useGroupDetail } from './GroupDetailProvider'
type Automation = {
key: keyof GroupAutomationsDto
kind: 'PAYMENT_REMINDER' | 'REENGAGEMENT'
title: string
description: string
}
const AUTOMATIONS: readonly Automation[] = [
{
key: 'autoPaymentReminders',
kind: 'PAYMENT_REMINDER',
title: 'Recordatorio semanal de pago',
description: 'A quien tenga un cobro pendiente o vencido, con su link de pago.',
},
{
key: 'autoReengagement',
kind: 'REENGAGEMENT',
title: 'Mensaje de reenganche semanal',
description: 'A los miembros en riesgo por faltas. Como mucho uno cada 2 semanas.',
},
]
/**
* Los envíos automáticos por WhatsApp del grupo. Solo el dueño los cambia: los
* mensajes salen de su número. Se pueden apagar siempre; prenderlos exige un
* WhatsApp vinculado y el mensaje aprobado.
*/
export function GroupAutomationsCard() {
const { group, updateAutomations, isUpdatingAutomations, isGroupOwner } = useGroupDetail()
const whatsappQuery = useWhatsappStatus({ enabled: isGroupOwner })
const billingQuery = useBillingStatus({ enabled: isGroupOwner })
if (!group || !isGroupOwner) return null
const includedInPlan = hasPlanFeature(billingQuery.data, 'whatsappAutomations')
return (
<section className="rounded-xl border border-border bg-surface">
<header className="flex items-start gap-3 px-4 pt-3">
<span className="rounded-lg bg-success-soft p-2">
<MessageCircle className="size-4 text-success" />
</span>
<div className="min-w-0 flex-1">
<div className="flex items-center gap-2">
<h2 className="text-sm font-semibold text-primary">Mensajes automáticos por WhatsApp</h2>
{includedInPlan ? null : <Badge variant="info">Pro</Badge>}
</div>
<p className="text-xs text-foreground/50">
{WEEKLY_SEND_LABEL}
{includedInPlan ? null : (
<>
{' '}
Disponible en el plan Pro.{' '}
<Link to="/billing" className="font-medium text-accent-fg underline">
Ver planes
</Link>
</>
)}
</p>
</div>
</header>
<ul className="divide-y divide-border">
{AUTOMATIONS.map((automation) => {
const checked = group[automation.key]
const blocked = automationBlockedReason(whatsappQuery.data, automation.kind)
const hintId = `automation-${automation.key}-hint`
return (
<li key={automation.key} className="flex items-start gap-3 px-4 py-3">
<div className="min-w-0 flex-1">
<p className="text-sm text-primary">{automation.title}</p>
<p id={hintId} className="text-xs text-foreground/50">
{automation.description}
{blocked && !checked ? (
<>
{' '}
<span className="text-warning">{blocked}</span>{' '}
{whatsappQuery.data?.status !== 'active' ? (
<Link to="/settings" className="font-medium text-accent-fg underline">
Ir a Ajustes
</Link>
) : null}
</>
) : null}
</p>
</div>
<Switch
checked={checked}
disabled={
isUpdatingAutomations || (!checked && (blocked !== null || !includedInPlan))
}
aria-label={automation.title}
aria-describedby={hintId}
onCheckedChange={(next) => updateAutomations({ [automation.key]: next })}
/>
</li>
)
})}
</ul>
</section>
)
}