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

@@ -0,0 +1,91 @@
// Automatic monthly charges and combined Mercado Pago links.
//
// On the teacher's day of the month a run creates the fee of every active
// member of their monthly groups and one Checkout Pro link per payer: the adult
// in charge for kids, the person themselves otherwise. The link covers all the
// payer's fees (`PaymentCheckout` + `Payment.checkoutId`); its
// `external_reference` is `chk_<checkout id>`.
// One per teacher. Off until the teacher turns it on.
model AutoChargeSettings {
id String @id @default(cuid())
ownerId String @unique @map("owner_id")
enabled Boolean @default(false)
// Day of the month (1-28) in the teacher's time zone.
chargeDay Int @default(1) @map("charge_day")
dueInDays Int @default(10) @map("due_in_days")
sendWhatsapp Boolean @default(false) @map("send_whatsapp")
// Last time `enabled` went on: a month whose day already passed is skipped.
enabledAt DateTime? @map("enabled_at")
createdAt DateTime @default(now()) @map("created_at")
updatedAt DateTime @updatedAt @map("updated_at")
owner User @relation(fields: [ownerId], references: [id], onDelete: Cascade)
@@map("auto_charge_settings")
}
enum PaymentRunStatus {
RUNNING
DONE
SKIPPED
}
// One automatic run per teacher and month: the unique key is what makes a
// second tick of the job (or a second server) a no-op.
model PaymentRun {
id String @id @default(cuid())
ownerId String @map("owner_id")
periodKey String @map("period_key")
status PaymentRunStatus @default(RUNNING)
skipReason String? @map("skip_reason")
fees Int @default(0)
checkouts Int @default(0)
linked Int @default(0)
failed Int @default(0)
sent Int @default(0)
createdAt DateTime @default(now()) @map("created_at")
finishedAt DateTime? @map("finished_at")
owner User @relation(fields: [ownerId], references: [id], onDelete: Cascade)
checkoutsList PaymentCheckout[]
@@unique([ownerId, periodKey])
@@map("payment_runs")
}
enum PaymentCheckoutStatus {
OPEN
PAID
// A fee was paid some other way (e.g. a confirmed transfer): the link is
// expired and the remaining fees get links of their own.
SUPERSEDED
}
// One Mercado Pago link covering several fees of the same payer.
model PaymentCheckout {
id String @id @default(cuid())
ownerId String @map("owner_id")
payerPersonId String @map("payer_person_id")
runId String? @map("run_id")
amount Decimal @db.Decimal(10, 2)
currency String
dueDate DateTime @map("due_date")
checkoutUrl String? @map("checkout_url")
mpPreferenceId String? @unique @map("mp_preference_id")
mpPaymentId String? @map("mp_payment_id")
status PaymentCheckoutStatus @default(OPEN)
paidAt DateTime? @map("paid_at")
createdAt DateTime @default(now()) @map("created_at")
updatedAt DateTime @updatedAt @map("updated_at")
owner User @relation(fields: [ownerId], references: [id], onDelete: Cascade)
payer Person @relation(fields: [payerPersonId], references: [id], onDelete: Cascade)
run PaymentRun? @relation(fields: [runId], references: [id], onDelete: SetNull)
payments Payment[]
outboundMessages OutboundMessage[]
@@index([status, createdAt])
@@index([ownerId])
@@map("payment_checkouts")
}