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:
89
apps/web/src/features/groups/detail/GroupAutomationsCard.tsx
Normal file
89
apps/web/src/features/groups/detail/GroupAutomationsCard.tsx
Normal 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>
|
||||
)
|
||||
}
|
||||
@@ -3,6 +3,7 @@ import type { ReactNode } from 'react'
|
||||
import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query'
|
||||
import type {
|
||||
AttendeeDto,
|
||||
UpdateGroupAutomations,
|
||||
AttendeeListItem,
|
||||
GroupDto,
|
||||
GroupPreRegistrationDto,
|
||||
@@ -10,6 +11,7 @@ import type {
|
||||
GroupRiskLevel,
|
||||
} from '@gruperly/shared'
|
||||
import { useToast } from '../../../components/ui'
|
||||
import { useAuth } from '../../../context/AuthProvider'
|
||||
import {
|
||||
ApiError,
|
||||
approveGroupPreRegistration,
|
||||
@@ -22,6 +24,7 @@ import {
|
||||
promoteGroupWaitlistEntry,
|
||||
rejectGroupPreRegistration,
|
||||
removeGroupWaitlistEntry,
|
||||
updateGroupAutomations,
|
||||
} from '../../../lib/api'
|
||||
import { useRemoveAttendee } from './useRemoveAttendee'
|
||||
|
||||
@@ -80,6 +83,10 @@ type GroupDetailContextValue = {
|
||||
copyInviteLink: () => Promise<void>
|
||||
copyWhatsappMessage: () => Promise<void>
|
||||
openWhatsapp: () => Promise<void>
|
||||
/** Solo el dueño ve y cambia las automatizaciones: salen de su WhatsApp. */
|
||||
isGroupOwner: boolean
|
||||
updateAutomations: (patch: UpdateGroupAutomations) => void
|
||||
isUpdatingAutomations: boolean
|
||||
}
|
||||
|
||||
const GroupDetailContext = createContext<GroupDetailContextValue | null>(null)
|
||||
@@ -87,6 +94,7 @@ const GroupDetailContext = createContext<GroupDetailContextValue | null>(null)
|
||||
export function GroupDetailProvider({ groupId, children }: { groupId: string; children: ReactNode }) {
|
||||
const queryClient = useQueryClient()
|
||||
const toast = useToast()
|
||||
const { user } = useAuth()
|
||||
|
||||
const [isInviteModalOpen, setIsInviteModalOpen] = useState(false)
|
||||
const [listSection, setListSectionState] = useState<ListSection>('members')
|
||||
@@ -172,6 +180,19 @@ export function GroupDetailProvider({ groupId, children }: { groupId: string; ch
|
||||
},
|
||||
})
|
||||
|
||||
const automationsMutation = useMutation({
|
||||
mutationFn: (patch: UpdateGroupAutomations) => updateGroupAutomations(groupId, patch),
|
||||
onSuccess: (automations) => {
|
||||
queryClient.setQueryData<GroupDto>(['group', groupId], (current) =>
|
||||
current ? { ...current, ...automations } : current,
|
||||
)
|
||||
toast.success('Guardamos los mensajes automáticos del grupo.')
|
||||
},
|
||||
onError: (err: Error) => {
|
||||
toast.error(err.message || 'No se pudieron guardar los mensajes automáticos.')
|
||||
},
|
||||
})
|
||||
|
||||
const removeAttendeeMutation = useRemoveAttendee({
|
||||
groupId,
|
||||
onRemoved: () => setAttendeeToRemove(null),
|
||||
@@ -303,6 +324,9 @@ export function GroupDetailProvider({ groupId, children }: { groupId: string; ch
|
||||
group,
|
||||
groupTitle,
|
||||
groupQueryPending: groupQuery.isPending,
|
||||
isGroupOwner: Boolean(group && user && group.createdById === user.id),
|
||||
updateAutomations: (patch) => automationsMutation.mutate(patch),
|
||||
isUpdatingAutomations: automationsMutation.isPending,
|
||||
riskLevel: riskOverviewQuery.data?.items.find((item) => item.groupId === groupId)?.riskLevel ?? 'NONE',
|
||||
attendees,
|
||||
attendeesTotal,
|
||||
|
||||
@@ -3,6 +3,7 @@ import { Loader2 } from 'lucide-react'
|
||||
import { useState } from 'react'
|
||||
import { Button } from '../../../components/ui'
|
||||
import { ChargeMembersModal } from './ChargeMembersModal'
|
||||
import { GroupAutomationsCard } from './GroupAutomationsCard'
|
||||
import { GroupHeader } from './GroupHeader'
|
||||
import { GroupInfoCard } from './GroupInfoCard'
|
||||
import { InviteLinkModal } from './InviteLinkModal'
|
||||
@@ -52,6 +53,7 @@ export function GroupDetailView() {
|
||||
) : (
|
||||
<>
|
||||
<GroupInfoCard />
|
||||
<GroupAutomationsCard />
|
||||
<MembersWaitlistSection />
|
||||
</>
|
||||
)}
|
||||
|
||||
Reference in New Issue
Block a user