feat(whatsapp): mensajes automáticos semanales con proveedor intercambiable

- Puerto MessagingProvider con adaptadores OpenWA (MVP, QR) y Kapso
  (setup link de Meta + templates), elegido con WHATSAPP_PROVIDER.
- Vinculación del WhatsApp del profesor en el onboarding y en Ajustes.
- Job semanal (lunes 10:00 AR): recordatorio de pago y reenganche a
  alumnos en riesgo (cooldown 14 días), con switches por grupo.
- Idempotencia por (kind, attendee, semana ISO), webhooks firmados con
  HMAC y deduplicados, normalización de teléfonos a E.164.
- Servicio openwa en docker-compose y guía en docs/whatsapp.md.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Jose Selesan
2026-10-22 08:38:09 -03:00
parent 8c5caecdda
commit ec65c2198d
90 changed files with 6784 additions and 117 deletions

View File

@@ -0,0 +1,89 @@
import type { GroupAutomationsDto } from '@gruperly/shared'
import { Link } from '@tanstack/react-router'
import { MessageCircle } from 'lucide-react'
import { Switch } from '../../../components/ui'
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 })
if (!group || !isGroupOwner) return null
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">
<h2 className="text-sm font-semibold text-primary">Mensajes automáticos por WhatsApp</h2>
<p className="text-xs text-foreground/50">{WEEKLY_SEND_LABEL}</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)}
aria-label={automation.title}
aria-describedby={hintId}
onCheckedChange={(next) => updateAutomations({ [automation.key]: next })}
/>
</li>
)
})}
</ul>
</section>
)
}