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>
This commit is contained in:
Jose Selesan
2026-11-11 15:08:48 -03:00
parent 5b3a035dcf
commit ac98495a6e
87 changed files with 2648 additions and 106 deletions

View File

@@ -1,24 +1,24 @@
// Domain models
model Group {
id String @id @default(cuid())
id String @id @default(cuid())
name String
description String?
days WeekDay[]
time String?
capacity Int?
price Decimal? @db.Decimal(10, 2)
billingType BillingType? @map("billing_type")
dueDay Int? @map("due_day")
price Decimal? @db.Decimal(10, 2)
billingType BillingType? @map("billing_type")
dueDay Int? @map("due_day")
// Weekly WhatsApp automations, off until the teacher turns them on.
autoPaymentReminders Boolean @default(false) @map("auto_payment_reminders")
autoReengagement Boolean @default(false) @map("auto_reengagement")
autoPaymentReminders Boolean @default(false) @map("auto_payment_reminders")
autoReengagement Boolean @default(false) @map("auto_reengagement")
// KIDS: each student is a kid and the contact is the adult in charge.
audience GroupAudience @default(ADULTS)
inviteToken String? @unique(map: "groups_inviteToken_key") @map("invite_token")
createdById String @map("created_by_id")
createdAt DateTime @default(now()) @map("created_at")
updatedAt DateTime @updatedAt @map("updated_at")
inviteToken String? @unique(map: "groups_inviteToken_key") @map("invite_token")
createdById String @map("created_by_id")
createdAt DateTime @default(now()) @map("created_at")
updatedAt DateTime @updatedAt @map("updated_at")
owner User @relation("OwnerGroups", fields: [createdById], references: [id], onDelete: Cascade, map: "groups_createdById_fkey")
members GroupMember[]
@@ -85,18 +85,23 @@ enum AttendeeStatus {
// teacher through a hand-written partial index (`people_owner_phone_key`,
// `WHERE phone IS NOT NULL`): Prisma does not model it.
model Person {
id String @id @default(cuid())
createdById String @map("created_by_id")
fullName String @map("full_name")
email String?
phone String?
notes String?
createdAt DateTime @default(now()) @map("created_at")
updatedAt DateTime @updatedAt @map("updated_at")
id String @id @default(cuid())
createdById String @map("created_by_id")
fullName String @map("full_name")
email String?
phone String?
notes String?
// Automatic monthly charges only build a Mercado Pago link for people with
// this on. Off: they prefer to transfer (the fee is still created).
payWithMercadoPago Boolean @default(true) @map("pay_with_mercado_pago")
createdAt DateTime @default(now()) @map("created_at")
updatedAt DateTime @updatedAt @map("updated_at")
owner User @relation("OwnerPeople", fields: [createdById], references: [id], onDelete: Cascade)
attendees Attendee[]
studentAccounts StudentAccountPerson[]
// As the payer of combined Mercado Pago links (themselves or their kids).
checkouts PaymentCheckout[]
// As a kid: the adult in charge (one in v1, enforced by the code).
guardians PersonGuardian[] @relation("GuardianChild")
// As the adult in charge: their kids. An adult has no memberships of their own.
@@ -184,16 +189,25 @@ model Payment {
mpPreferenceId String? @unique(map: "payments_mp_preference_id_key") @map("mp_preference_id")
mpPaymentId String? @map("mp_payment_id")
paidAt DateTime? @map("paid_at")
// Combined link (automatic monthly charges): the fee is paid through it and
// `checkoutUrl` is a copy of the checkout's link.
checkoutId String? @map("checkout_id")
// Month of an automatic charge ("2026-12"). Unique per attendee through a
// hand-written partial index (`payments_attendee_period_key`,
// `WHERE period_key IS NOT NULL`): Prisma does not model it.
periodKey String? @map("period_key")
createdAt DateTime @default(now()) @map("created_at")
updatedAt DateTime @updatedAt @map("updated_at")
group Group @relation(fields: [groupId], references: [id], onDelete: Cascade, map: "payments_groupId_fkey")
attendee Attendee @relation(fields: [attendeeId], references: [id], onDelete: Cascade, map: "payments_attendeeId_fkey")
checkout PaymentCheckout? @relation(fields: [checkoutId], references: [id], onDelete: SetNull)
outboundMessages OutboundMessage[]
reportedItems ReportedPaymentItem[]
@@index([groupId], map: "payments_groupId_idx")
@@index([attendeeId], map: "payments_attendeeId_idx")
@@index([checkoutId])
@@map("payments")
}