// 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_`. // 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") }