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,58 @@
import type { PrismaClient } from '@generated/prisma/client';
import type {
GroupAutomationsDto,
ProblemDetails,
Result,
UpdateGroupAutomations,
} from '@gruperly/shared';
import { err, ok } from '@gruperly/shared';
import { forbiddenProblem, notFoundResourceProblem } from '@/http/problem-builders';
import prisma from '@/lib/prisma';
type UpdateGroupAutomationsDeps = { db?: Pick<PrismaClient, 'group'> };
/**
* Turns the group's weekly WhatsApp automations on or off. Only the owner can:
* the messages go out from their own WhatsApp.
*
* Turning one on does not require a linked WhatsApp. The weekly job skips
* groups whose owner has nothing usable, and the UI explains why.
*/
export class UpdateGroupAutomationsUseCase {
constructor(private readonly deps: UpdateGroupAutomationsDeps = {}) {}
async execute(
groupId: string,
userId: string,
input: UpdateGroupAutomations,
): Promise<Result<GroupAutomationsDto, ProblemDetails>> {
const db = this.deps.db ?? prisma;
const group = await db.group.findUnique({
where: { id: groupId },
select: { id: true, createdById: true },
});
if (!group) return err(notFoundResourceProblem('Group', groupId));
if (group.createdById !== userId) {
return err(
forbiddenProblem({
detail: 'Solo quien creó el grupo puede cambiar sus automatizaciones.',
code: 'group_owner_required',
}),
);
}
const updated = await db.group.update({
where: { id: groupId },
data: {
...(input.autoPaymentReminders !== undefined
? { autoPaymentReminders: input.autoPaymentReminders }
: {}),
...(input.autoReengagement !== undefined ? { autoReengagement: input.autoReengagement } : {}),
},
select: { autoPaymentReminders: true, autoReengagement: true },
});
return ok(updated);
}
}