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:
@@ -0,0 +1,129 @@
|
||||
-- WhatsApp messaging: linked accounts (provider-agnostic), Kapso templates,
|
||||
-- outbound message log and webhook dedupe; plus the per-group automation switches.
|
||||
|
||||
-- CreateEnum
|
||||
CREATE TYPE "MessagingProviderId" AS ENUM ('OPENWA', 'KAPSO');
|
||||
|
||||
-- CreateEnum
|
||||
CREATE TYPE "WhatsappAccountStatus" AS ENUM ('PENDING', 'ACTIVE', 'DISCONNECTED');
|
||||
|
||||
-- CreateEnum
|
||||
CREATE TYPE "WhatsappMessageKind" AS ENUM ('PAYMENT_REMINDER', 'REENGAGEMENT');
|
||||
|
||||
-- CreateEnum
|
||||
CREATE TYPE "OutboundMessageStatus" AS ENUM ('QUEUED', 'SENT', 'DELIVERED', 'READ', 'FAILED', 'SKIPPED');
|
||||
|
||||
-- CreateEnum
|
||||
CREATE TYPE "WhatsappTemplateStatus" AS ENUM ('PENDING', 'APPROVED', 'REJECTED', 'DISABLED');
|
||||
|
||||
-- AlterTable
|
||||
ALTER TABLE "groups" ADD COLUMN "auto_payment_reminders" BOOLEAN NOT NULL DEFAULT false,
|
||||
ADD COLUMN "auto_reengagement" BOOLEAN NOT NULL DEFAULT false;
|
||||
|
||||
-- AlterTable (pre-existing drift: the schema already declares this default)
|
||||
ALTER TABLE "payments" ALTER COLUMN "currency" SET DEFAULT 'ARS';
|
||||
|
||||
-- CreateTable
|
||||
CREATE TABLE "whatsapp_accounts" (
|
||||
"id" TEXT NOT NULL,
|
||||
"owner_id" TEXT NOT NULL,
|
||||
"provider" "MessagingProviderId" NOT NULL,
|
||||
"status" "WhatsappAccountStatus" NOT NULL DEFAULT 'PENDING',
|
||||
"external_id" TEXT,
|
||||
"display_phone_number" TEXT,
|
||||
"provider_data" JSONB,
|
||||
"connected_at" TIMESTAMP(3),
|
||||
"created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
"updated_at" TIMESTAMP(3) NOT NULL,
|
||||
|
||||
CONSTRAINT "whatsapp_accounts_pkey" PRIMARY KEY ("id")
|
||||
);
|
||||
|
||||
-- CreateTable
|
||||
CREATE TABLE "whatsapp_templates" (
|
||||
"id" TEXT NOT NULL,
|
||||
"account_id" TEXT NOT NULL,
|
||||
"kind" "WhatsappMessageKind" NOT NULL,
|
||||
"name" TEXT NOT NULL,
|
||||
"language" TEXT NOT NULL,
|
||||
"meta_template_id" TEXT,
|
||||
"status" "WhatsappTemplateStatus" NOT NULL DEFAULT 'PENDING',
|
||||
"rejected_reason" TEXT,
|
||||
"created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
"updated_at" TIMESTAMP(3) NOT NULL,
|
||||
|
||||
CONSTRAINT "whatsapp_templates_pkey" PRIMARY KEY ("id")
|
||||
);
|
||||
|
||||
-- CreateTable
|
||||
CREATE TABLE "outbound_messages" (
|
||||
"id" TEXT NOT NULL,
|
||||
"owner_id" TEXT NOT NULL,
|
||||
"group_id" TEXT NOT NULL,
|
||||
"attendee_id" TEXT NOT NULL,
|
||||
"payment_id" TEXT,
|
||||
"kind" "WhatsappMessageKind" NOT NULL,
|
||||
"provider" "MessagingProviderId" NOT NULL,
|
||||
"period_key" TEXT NOT NULL,
|
||||
"to_phone" TEXT NOT NULL,
|
||||
"provider_message_id" TEXT,
|
||||
"status" "OutboundMessageStatus" NOT NULL DEFAULT 'QUEUED',
|
||||
"error_code" TEXT,
|
||||
"error_message" TEXT,
|
||||
"sent_at" TIMESTAMP(3),
|
||||
"created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
"updated_at" TIMESTAMP(3) NOT NULL,
|
||||
|
||||
CONSTRAINT "outbound_messages_pkey" PRIMARY KEY ("id")
|
||||
);
|
||||
|
||||
-- CreateTable
|
||||
CREATE TABLE "whatsapp_webhook_events" (
|
||||
"provider" "MessagingProviderId" NOT NULL,
|
||||
"dedupe_key" TEXT NOT NULL,
|
||||
"received_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
|
||||
CONSTRAINT "whatsapp_webhook_events_pkey" PRIMARY KEY ("provider","dedupe_key")
|
||||
);
|
||||
|
||||
-- CreateIndex
|
||||
CREATE UNIQUE INDEX "whatsapp_accounts_ownerId_key" ON "whatsapp_accounts"("owner_id");
|
||||
|
||||
-- CreateIndex
|
||||
CREATE UNIQUE INDEX "whatsapp_accounts_provider_externalId_key" ON "whatsapp_accounts"("provider", "external_id");
|
||||
|
||||
-- CreateIndex
|
||||
CREATE INDEX "whatsapp_templates_status_idx" ON "whatsapp_templates"("status");
|
||||
|
||||
-- CreateIndex
|
||||
CREATE UNIQUE INDEX "whatsapp_templates_accountId_kind_key" ON "whatsapp_templates"("account_id", "kind");
|
||||
|
||||
-- CreateIndex
|
||||
CREATE INDEX "outbound_messages_attendeeId_kind_createdAt_idx" ON "outbound_messages"("attendee_id", "kind", "created_at");
|
||||
|
||||
-- CreateIndex
|
||||
CREATE INDEX "outbound_messages_groupId_createdAt_idx" ON "outbound_messages"("group_id", "created_at");
|
||||
|
||||
-- CreateIndex
|
||||
CREATE UNIQUE INDEX "outbound_messages_kind_attendeeId_periodKey_key" ON "outbound_messages"("kind", "attendee_id", "period_key");
|
||||
|
||||
-- CreateIndex
|
||||
CREATE UNIQUE INDEX "outbound_messages_provider_providerMessageId_key" ON "outbound_messages"("provider", "provider_message_id");
|
||||
|
||||
-- CreateIndex
|
||||
CREATE INDEX "whatsapp_webhook_events_receivedAt_idx" ON "whatsapp_webhook_events"("received_at");
|
||||
|
||||
-- AddForeignKey
|
||||
ALTER TABLE "whatsapp_accounts" ADD CONSTRAINT "whatsapp_accounts_ownerId_fkey" FOREIGN KEY ("owner_id") REFERENCES "users"("id") ON DELETE CASCADE ON UPDATE CASCADE;
|
||||
|
||||
-- AddForeignKey
|
||||
ALTER TABLE "whatsapp_templates" ADD CONSTRAINT "whatsapp_templates_accountId_fkey" FOREIGN KEY ("account_id") REFERENCES "whatsapp_accounts"("id") ON DELETE CASCADE ON UPDATE CASCADE;
|
||||
|
||||
-- AddForeignKey
|
||||
ALTER TABLE "outbound_messages" ADD CONSTRAINT "outbound_messages_groupId_fkey" FOREIGN KEY ("group_id") REFERENCES "groups"("id") ON DELETE CASCADE ON UPDATE CASCADE;
|
||||
|
||||
-- AddForeignKey
|
||||
ALTER TABLE "outbound_messages" ADD CONSTRAINT "outbound_messages_attendeeId_fkey" FOREIGN KEY ("attendee_id") REFERENCES "attendees"("id") ON DELETE CASCADE ON UPDATE CASCADE;
|
||||
|
||||
-- AddForeignKey
|
||||
ALTER TABLE "outbound_messages" ADD CONSTRAINT "outbound_messages_paymentId_fkey" FOREIGN KEY ("payment_id") REFERENCES "payments"("id") ON DELETE SET NULL ON UPDATE CASCADE;
|
||||
Reference in New Issue
Block a user