- 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>
131 lines
5.7 KiB
Plaintext
131 lines
5.7 KiB
Plaintext
// WhatsApp messaging (automated payment reminders and re-engagement).
|
|
//
|
|
// Nothing here is tied to one provider: the teacher links their own number
|
|
// through whichever `MessagingProvider` the server runs (OpenWA for the MVP,
|
|
// Kapso afterwards) and the row records which one it was linked with. Changing
|
|
// `WHATSAPP_PROVIDER` leaves the old rows behind as "requires reconnect".
|
|
|
|
enum MessagingProviderId {
|
|
OPENWA
|
|
KAPSO
|
|
}
|
|
|
|
enum WhatsappAccountStatus {
|
|
PENDING
|
|
ACTIVE
|
|
DISCONNECTED
|
|
}
|
|
|
|
enum WhatsappMessageKind {
|
|
PAYMENT_REMINDER
|
|
REENGAGEMENT
|
|
}
|
|
|
|
enum OutboundMessageStatus {
|
|
QUEUED
|
|
SENT
|
|
DELIVERED
|
|
READ
|
|
FAILED
|
|
SKIPPED
|
|
}
|
|
|
|
enum WhatsappTemplateStatus {
|
|
PENDING
|
|
APPROVED
|
|
REJECTED
|
|
DISABLED
|
|
}
|
|
|
|
// The teacher's linked WhatsApp. One per teacher.
|
|
model WhatsappAccount {
|
|
id String @id @default(cuid())
|
|
ownerId String @unique(map: "whatsapp_accounts_ownerId_key") @map("owner_id")
|
|
provider MessagingProviderId
|
|
status WhatsappAccountStatus @default(PENDING)
|
|
// OpenWA: session id. Kapso: phone_number_id (null until the setup finishes).
|
|
externalId String? @map("external_id")
|
|
displayPhoneNumber String? @map("display_phone_number")
|
|
// Provider-specific identifiers (Kapso: customerId, setupLinkId, businessAccountId).
|
|
// Never secrets: API keys and webhook secrets live in the environment.
|
|
providerData Json? @map("provider_data")
|
|
connectedAt DateTime? @map("connected_at")
|
|
createdAt DateTime @default(now()) @map("created_at")
|
|
updatedAt DateTime @updatedAt @map("updated_at")
|
|
|
|
owner User @relation(fields: [ownerId], references: [id], onDelete: Cascade, map: "whatsapp_accounts_ownerId_fkey")
|
|
templates WhatsappTemplate[]
|
|
|
|
// A number (or session) cannot be linked to two teachers.
|
|
@@unique([provider, externalId], map: "whatsapp_accounts_provider_externalId_key")
|
|
@@map("whatsapp_accounts")
|
|
}
|
|
|
|
// Message templates submitted to Meta for one account (Kapso only: OpenWA sends
|
|
// free text). Meta approves them per WhatsApp Business Account.
|
|
model WhatsappTemplate {
|
|
id String @id @default(cuid())
|
|
accountId String @map("account_id")
|
|
kind WhatsappMessageKind
|
|
name String
|
|
language String
|
|
metaTemplateId String? @map("meta_template_id")
|
|
status WhatsappTemplateStatus @default(PENDING)
|
|
rejectedReason String? @map("rejected_reason")
|
|
createdAt DateTime @default(now()) @map("created_at")
|
|
updatedAt DateTime @updatedAt @map("updated_at")
|
|
|
|
account WhatsappAccount @relation(fields: [accountId], references: [id], onDelete: Cascade, map: "whatsapp_templates_accountId_fkey")
|
|
|
|
@@unique([accountId, kind], map: "whatsapp_templates_accountId_kind_key")
|
|
@@index([status], map: "whatsapp_templates_status_idx")
|
|
@@map("whatsapp_templates")
|
|
}
|
|
|
|
// One automated message to one member. It is the send log and the idempotency
|
|
// key at once: the row is created before calling the provider, and the unique
|
|
// (kind, attendee, week) is what stops a second run of the weekly job from
|
|
// writing to the same person twice.
|
|
model OutboundMessage {
|
|
id String @id @default(cuid())
|
|
ownerId String @map("owner_id")
|
|
groupId String @map("group_id")
|
|
attendeeId String @map("attendee_id")
|
|
paymentId String? @map("payment_id")
|
|
kind WhatsappMessageKind
|
|
provider MessagingProviderId
|
|
// ISO week the job ran for, e.g. "2026-W43".
|
|
periodKey String @map("period_key")
|
|
toPhone String @map("to_phone")
|
|
providerMessageId String? @map("provider_message_id")
|
|
status OutboundMessageStatus @default(QUEUED)
|
|
errorCode String? @map("error_code")
|
|
errorMessage String? @map("error_message")
|
|
sentAt DateTime? @map("sent_at")
|
|
createdAt DateTime @default(now()) @map("created_at")
|
|
updatedAt DateTime @updatedAt @map("updated_at")
|
|
|
|
group Group @relation(fields: [groupId], references: [id], onDelete: Cascade, map: "outbound_messages_groupId_fkey")
|
|
attendee Attendee @relation(fields: [attendeeId], references: [id], onDelete: Cascade, map: "outbound_messages_attendeeId_fkey")
|
|
payment Payment? @relation(fields: [paymentId], references: [id], onDelete: SetNull, map: "outbound_messages_paymentId_fkey")
|
|
|
|
@@unique([kind, attendeeId, periodKey], map: "outbound_messages_kind_attendeeId_periodKey_key")
|
|
@@unique([provider, providerMessageId], map: "outbound_messages_provider_providerMessageId_key")
|
|
// Re-engagement cooldown: "was this member written to in the last 14 days?"
|
|
@@index([attendeeId, kind, createdAt], map: "outbound_messages_attendeeId_kind_createdAt_idx")
|
|
@@index([groupId, createdAt], map: "outbound_messages_groupId_createdAt_idx")
|
|
@@map("outbound_messages")
|
|
}
|
|
|
|
// Webhook deliveries already processed. Both providers deliver at least once and
|
|
// retry, so the delivery's idempotency key is burned here before applying it.
|
|
model WhatsappWebhookEvent {
|
|
provider MessagingProviderId
|
|
dedupeKey String @map("dedupe_key")
|
|
receivedAt DateTime @default(now()) @map("received_at")
|
|
|
|
@@id([provider, dedupeKey], map: "whatsapp_webhook_events_pkey")
|
|
@@index([receivedAt], map: "whatsapp_webhook_events_receivedAt_idx")
|
|
@@map("whatsapp_webhook_events")
|
|
}
|