Files
Jose Selesan ac98495a6e feat(payments): automatic monthly charges with combined links per payer
- Teacher picks a day of the month in Settings; an hourly job creates the
  fees of all monthly groups on that day in the teacher's time zone
  (one run per teacher and month, partial unique index per attendee/period).
- One Mercado Pago link per payer (PaymentCheckout): the adult in charge
  bundles their kids and their own fees; external_reference chk_<id>,
  dedicated webhook and reconciliation mark every fee PAID at once.
- Person.payWithMercadoPago toggle (default on): off creates the fee
  without a link so the student pays by transfer.
- A fee paid some other way supersedes its combined link and expires the
  preference; renewing rebuilds the combined link or a single one.
- Optional WhatsApp delivery (PAYMENT_LINK kind, new Kapso template);
  weekly reminders now go once per combined link.
- Web: auto charge settings card, per-student toggle, combined link label
  in Cobros and "Pay all" in the student app, in the four locales.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-11 15:08:48 -03:00

115 lines
4.4 KiB
SQL

-- CreateEnum
CREATE TYPE "PaymentRunStatus" AS ENUM ('RUNNING', 'DONE', 'SKIPPED');
-- CreateEnum
CREATE TYPE "PaymentCheckoutStatus" AS ENUM ('OPEN', 'PAID', 'SUPERSEDED');
-- AlterEnum
ALTER TYPE "WhatsappMessageKind" ADD VALUE 'PAYMENT_LINK';
-- AlterTable
ALTER TABLE "outbound_messages" ADD COLUMN "checkout_id" TEXT;
-- AlterTable
ALTER TABLE "payments" ADD COLUMN "checkout_id" TEXT,
ADD COLUMN "period_key" TEXT;
-- AlterTable
ALTER TABLE "people" ADD COLUMN "pay_with_mercado_pago" BOOLEAN NOT NULL DEFAULT true;
-- CreateTable
CREATE TABLE "auto_charge_settings" (
"id" TEXT NOT NULL,
"owner_id" TEXT NOT NULL,
"enabled" BOOLEAN NOT NULL DEFAULT false,
"charge_day" INTEGER NOT NULL DEFAULT 1,
"due_in_days" INTEGER NOT NULL DEFAULT 10,
"send_whatsapp" BOOLEAN NOT NULL DEFAULT false,
"enabled_at" TIMESTAMP(3),
"created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updated_at" TIMESTAMP(3) NOT NULL,
CONSTRAINT "auto_charge_settings_pkey" PRIMARY KEY ("id")
);
-- CreateTable
CREATE TABLE "payment_runs" (
"id" TEXT NOT NULL,
"owner_id" TEXT NOT NULL,
"period_key" TEXT NOT NULL,
"status" "PaymentRunStatus" NOT NULL DEFAULT 'RUNNING',
"skip_reason" TEXT,
"fees" INTEGER NOT NULL DEFAULT 0,
"checkouts" INTEGER NOT NULL DEFAULT 0,
"linked" INTEGER NOT NULL DEFAULT 0,
"failed" INTEGER NOT NULL DEFAULT 0,
"sent" INTEGER NOT NULL DEFAULT 0,
"created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
"finished_at" TIMESTAMP(3),
CONSTRAINT "payment_runs_pkey" PRIMARY KEY ("id")
);
-- CreateTable
CREATE TABLE "payment_checkouts" (
"id" TEXT NOT NULL,
"owner_id" TEXT NOT NULL,
"payer_person_id" TEXT NOT NULL,
"run_id" TEXT,
"amount" DECIMAL(10,2) NOT NULL,
"currency" TEXT NOT NULL,
"due_date" TIMESTAMP(3) NOT NULL,
"checkout_url" TEXT,
"mp_preference_id" TEXT,
"mp_payment_id" TEXT,
"status" "PaymentCheckoutStatus" NOT NULL DEFAULT 'OPEN',
"paid_at" TIMESTAMP(3),
"created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updated_at" TIMESTAMP(3) NOT NULL,
CONSTRAINT "payment_checkouts_pkey" PRIMARY KEY ("id")
);
-- CreateIndex
CREATE UNIQUE INDEX "auto_charge_settings_owner_id_key" ON "auto_charge_settings"("owner_id");
-- CreateIndex
CREATE UNIQUE INDEX "payment_runs_owner_id_period_key_key" ON "payment_runs"("owner_id", "period_key");
-- CreateIndex
CREATE UNIQUE INDEX "payment_checkouts_mp_preference_id_key" ON "payment_checkouts"("mp_preference_id");
-- CreateIndex
CREATE INDEX "payment_checkouts_status_created_at_idx" ON "payment_checkouts"("status", "created_at");
-- CreateIndex
CREATE INDEX "payment_checkouts_owner_id_idx" ON "payment_checkouts"("owner_id");
-- CreateIndex
CREATE INDEX "payments_checkout_id_idx" ON "payments"("checkout_id");
-- AddForeignKey
ALTER TABLE "payments" ADD CONSTRAINT "payments_checkout_id_fkey" FOREIGN KEY ("checkout_id") REFERENCES "payment_checkouts"("id") ON DELETE SET NULL ON UPDATE CASCADE;
-- AddForeignKey
ALTER TABLE "auto_charge_settings" ADD CONSTRAINT "auto_charge_settings_owner_id_fkey" FOREIGN KEY ("owner_id") REFERENCES "users"("id") ON DELETE CASCADE ON UPDATE CASCADE;
-- AddForeignKey
ALTER TABLE "payment_runs" ADD CONSTRAINT "payment_runs_owner_id_fkey" FOREIGN KEY ("owner_id") REFERENCES "users"("id") ON DELETE CASCADE ON UPDATE CASCADE;
-- AddForeignKey
ALTER TABLE "payment_checkouts" ADD CONSTRAINT "payment_checkouts_owner_id_fkey" FOREIGN KEY ("owner_id") REFERENCES "users"("id") ON DELETE CASCADE ON UPDATE CASCADE;
-- AddForeignKey
ALTER TABLE "payment_checkouts" ADD CONSTRAINT "payment_checkouts_payer_person_id_fkey" FOREIGN KEY ("payer_person_id") REFERENCES "people"("id") ON DELETE CASCADE ON UPDATE CASCADE;
-- AddForeignKey
ALTER TABLE "payment_checkouts" ADD CONSTRAINT "payment_checkouts_run_id_fkey" FOREIGN KEY ("run_id") REFERENCES "payment_runs"("id") ON DELETE SET NULL ON UPDATE CASCADE;
-- AddForeignKey
ALTER TABLE "outbound_messages" ADD CONSTRAINT "outbound_messages_checkout_id_fkey" FOREIGN KEY ("checkout_id") REFERENCES "payment_checkouts"("id") ON DELETE SET NULL ON UPDATE CASCADE;
-- One automatic fee per member and month (hand-written: Prisma does not model
-- partial indexes). Manual fees have no period and stay out of it.
CREATE UNIQUE INDEX "payments_attendee_period_key" ON "payments"("attendee_id", "period_key") WHERE "period_key" IS NOT NULL;