feat: app de alumnos sin contraseña (invitación de un solo uso, avisos de falta y pagos informados)

- Acceso del alumno o tutor con link de un solo uso (hash SHA-256, consumo
  atómico, el GET no consume), sesión propia de 90 días deslizante, passkeys
  opcionales (SimpleWebAuthn) y mail de recuperación con rate limit.
- Lado alumno (/i/:token, /acceso, /alumno/*): inicio, clases, avisar falta,
  pagos, informar pago con comprobante (S3 privado, magic bytes) y perfil.
- Lado profe: acceso a la app en la ficha del alumno (invitar/reinvitar),
  "Pagos para verificar" en Cobros con badge, faltas avisadas en la ficha de
  grupo y datos para transferir en Ajustes.
- "Requiere atención" y el recordatorio de WhatsApp excluyen cuotas informadas.
- Zona horaria por defecto: Buenos Aires.
- Migración add_student_access, tests contra Postgres real (test:db) y docs.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Jose Selesan
2026-10-28 15:39:09 -03:00
parent b99bc4d519
commit 060a4f09e4
146 changed files with 7994 additions and 32 deletions

View File

@@ -13,9 +13,10 @@ model ClassSession {
createdAt DateTime @default(now()) @map("created_at")
updatedAt DateTime @updatedAt @map("updated_at")
group Group @relation(fields: [groupId], references: [id], onDelete: Cascade, map: "class_sessions_groupId_fkey")
attendances Attendance[]
slotReleases SlotRelease[]
group Group @relation(fields: [groupId], references: [id], onDelete: Cascade, map: "class_sessions_groupId_fkey")
attendances Attendance[]
slotReleases SlotRelease[]
absenceNotices AbsenceNotice[]
@@unique([groupId, startsAt], map: "class_sessions_groupId_startsAt_key")
@@map("class_sessions")

View File

@@ -10,6 +10,9 @@ model User {
country String? @map("country_code")
// When the professor chose Free or Pro in the onboarding (mandatory step).
planSelectedAt DateTime? @map("plan_selected_at")
// Bank transfer details shown to students when they report a payment.
transferAlias String? @map("transfer_alias")
transferHolder String? @map("transfer_holder")
createdAt DateTime @default(now()) @map("created_at")
updatedAt DateTime @updatedAt @map("updated_at")
@@ -24,6 +27,7 @@ model User {
mpAccount MpAccount?
whatsappAccount WhatsappAccount?
subscription BillingSubscription?
studentAccounts StudentAccount[] @relation("StudentAccountsCreated")
@@map("users")
}

View File

@@ -26,6 +26,7 @@ model Group {
preRegistrations GroupPreRegistration[]
classSessions ClassSession[]
outboundMessages OutboundMessage[]
reportedPayments ReportedPayment[]
@@map("groups")
}
@@ -85,12 +86,15 @@ model Attendee {
createdAt DateTime @default(now()) @map("created_at")
updatedAt DateTime @updatedAt @map("updated_at")
group Group @relation(fields: [groupId], references: [id], onDelete: Cascade, map: "attendees_groupId_fkey")
group Group @relation(fields: [groupId], references: [id], onDelete: Cascade, map: "attendees_groupId_fkey")
payments Payment[]
attendances Attendance[]
slotReleases SlotRelease[]
outboundMessages OutboundMessage[]
alertResolutions AttendeeAlertResolution[]
studentAccounts StudentAccountAttendee[]
absenceNotices AbsenceNotice[]
reportedPayments ReportedPayment[]
@@index([groupId], map: "attendees_groupId_idx")
@@map("attendees")
@@ -134,9 +138,10 @@ model Payment {
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")
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")
outboundMessages OutboundMessage[]
reportedItems ReportedPaymentItem[]
@@index([groupId], map: "payments_groupId_idx")
@@index([attendeeId], map: "payments_attendeeId_idx")

View File

@@ -0,0 +1,235 @@
// Student access (attendees or their guardians), without passwords.
//
// The teacher sends a single-use invitation link over WhatsApp; consuming it
// creates a long sliding session on that device. Passkeys and a recovery email
// are optional and let the student get back in without the teacher. None of
// this goes through Better Auth: students have no mandatory email and their
// session must never pass the teacher's endpoints (separate cookie and
// middleware under /api/v1/student).
// The person who signs in: the attendee or a guardian. Linked to one or more
// attendees (a guardian may have several children), always by the teacher.
model StudentAccount {
id String @id @default(cuid())
name String
// Verified recovery email (the student loads it, optional).
recoveryEmail String? @map("recovery_email")
recoveryEmailVerifiedAt DateTime? @map("recovery_email_verified_at")
// Email waiting for its confirmation link; never used to recover.
pendingRecoveryEmail String? @map("pending_recovery_email")
// First sign-in done: the welcome screen is shown only once.
welcomedAt DateTime? @map("welcomed_at")
createdById String @map("created_by_id")
createdAt DateTime @default(now()) @map("created_at")
updatedAt DateTime @updatedAt @map("updated_at")
createdBy User @relation("StudentAccountsCreated", fields: [createdById], references: [id], onDelete: Cascade)
attendees StudentAccountAttendee[]
tokens StudentAccessToken[]
sessions StudentSession[]
passkeys StudentPasskey[]
challenges StudentWebauthnChallenge[]
absenceNotices AbsenceNotice[]
reportedPayments ReportedPayment[]
@@index([recoveryEmail])
@@index([createdById])
@@map("student_accounts")
}
model StudentAccountAttendee {
accountId String @map("account_id")
attendeeId String @map("attendee_id")
createdAt DateTime @default(now()) @map("created_at")
account StudentAccount @relation(fields: [accountId], references: [id], onDelete: Cascade)
attendee Attendee @relation(fields: [attendeeId], references: [id], onDelete: Cascade)
@@id([accountId, attendeeId])
@@index([attendeeId])
@@map("student_account_attendees")
}
enum StudentTokenKind {
INVITATION
RECOVERY
EMAIL_CONFIRMATION
}
// Link token. Only its SHA-256 is stored. Consuming it is a conditional update
// (`used_at IS NULL AND revoked_at IS NULL AND expires_at > now()`), so the
// first device wins and the link dies.
model StudentAccessToken {
id String @id @default(cuid())
tokenHash String @unique @map("token_hash")
accountId String @map("account_id")
kind StudentTokenKind
// EMAIL_CONFIRMATION only: the address being confirmed.
email String?
expiresAt DateTime @map("expires_at")
usedAt DateTime? @map("used_at")
// A new invitation revokes the account's pending tokens.
revokedAt DateTime? @map("revoked_at")
// Teacher who generated the invitation (null for recovery/confirmation).
createdById String? @map("created_by_id")
usedIp String? @map("used_ip")
usedUserAgent String? @map("used_user_agent")
createdAt DateTime @default(now()) @map("created_at")
account StudentAccount @relation(fields: [accountId], references: [id], onDelete: Cascade)
@@index([accountId, kind])
@@map("student_access_tokens")
}
// Long sliding session (90 days, renewed on use). The cookie carries the raw
// token; only its hash is stored.
model StudentSession {
id String @id @default(cuid())
accountId String @map("account_id")
tokenHash String @unique @map("token_hash")
// Human readable, derived from the user agent ("iPhone · Safari").
device String
createdAt DateTime @default(now()) @map("created_at")
lastUsedAt DateTime @default(now()) @map("last_used_at")
expiresAt DateTime @map("expires_at")
revokedAt DateTime? @map("revoked_at")
account StudentAccount @relation(fields: [accountId], references: [id], onDelete: Cascade)
@@index([accountId])
@@map("student_sessions")
}
model StudentPasskey {
id String @id @default(cuid())
accountId String @map("account_id")
credentialId String @unique @map("credential_id")
publicKey Bytes @map("public_key")
counter Int @default(0)
transports String[]
device String
createdAt DateTime @default(now()) @map("created_at")
lastUsedAt DateTime? @map("last_used_at")
account StudentAccount @relation(fields: [accountId], references: [id], onDelete: Cascade)
@@index([accountId])
@@map("student_passkeys")
}
enum WebauthnChallengePurpose {
REGISTER
LOGIN
}
// Pending WebAuthn challenge, single use (deleted when verified), 5 minutes.
model StudentWebauthnChallenge {
id String @id @default(cuid())
challenge String
// Registration only: the signed-in account. Login is discoverable (no account).
accountId String? @map("account_id")
purpose WebauthnChallengePurpose
expiresAt DateTime @map("expires_at")
createdAt DateTime @default(now()) @map("created_at")
account StudentAccount? @relation(fields: [accountId], references: [id], onDelete: Cascade)
@@index([expiresAt])
@@map("student_webauthn_challenges")
}
// Generic sliding-window rate limit: one row per hit, counted by key within
// the window (e.g. "recovery:email:<sha256>", "recovery:ip:<ip>").
model RateLimitEvent {
id String @id @default(cuid())
key String
createdAt DateTime @default(now()) @map("created_at")
@@index([key, createdAt])
@@map("rate_limit_events")
}
enum AbsenceReason {
HEALTH
TRAVEL
WORK_STUDY
OTHER
}
// Absence notice from the student. It only marks the attendance as EXCUSED
// (which already doesn't count for the attendance risk rules); the teacher
// sees it. At most one active (not cancelled) notice per attendee and class:
// enforced by a partial unique index in the migration.
model AbsenceNotice {
id String @id @default(cuid())
attendeeId String @map("attendee_id")
classSessionId String @map("class_session_id")
accountId String? @map("account_id")
reason AbsenceReason?
message String?
createdAt DateTime @default(now()) @map("created_at")
cancelledAt DateTime? @map("cancelled_at")
attendee Attendee @relation(fields: [attendeeId], references: [id], onDelete: Cascade)
classSession ClassSession @relation(fields: [classSessionId], references: [id], onDelete: Cascade)
account StudentAccount? @relation(fields: [accountId], references: [id], onDelete: SetNull)
@@index([attendeeId, classSessionId])
@@index([classSessionId])
@@map("absence_notices")
}
enum ReportedPaymentStatus {
REPORTED
CONFIRMED
REJECTED
}
// Payment reported by the student with a receipt. It stays REPORTED until the
// teacher confirms it (the fees become PAID) or rejects it. `Payment.status` is
// not touched while REPORTED: the MP reconciliation and the overdue job keep
// working as before, and "Requiere atención" skips fees covered by a REPORTED
// report.
model ReportedPayment {
id String @id @default(cuid())
attendeeId String @map("attendee_id")
groupId String @map("group_id")
accountId String? @map("account_id")
totalAmount Decimal @map("total_amount") @db.Decimal(10, 2)
currency String
// Private object key in the storage; null once the retention policy deletes it.
receiptKey String? @map("receipt_key")
receiptMime String @map("receipt_mime")
receiptName String @map("receipt_name")
receiptSize Int @map("receipt_size")
status ReportedPaymentStatus @default(REPORTED)
reviewedById String? @map("reviewed_by_id")
reviewedAt DateTime? @map("reviewed_at")
rejectionReason String? @map("rejection_reason")
createdAt DateTime @default(now()) @map("created_at")
updatedAt DateTime @updatedAt @map("updated_at")
attendee Attendee @relation(fields: [attendeeId], references: [id], onDelete: Cascade)
group Group @relation(fields: [groupId], references: [id], onDelete: Cascade)
account StudentAccount? @relation(fields: [accountId], references: [id], onDelete: SetNull)
items ReportedPaymentItem[]
@@index([groupId, status])
@@index([attendeeId])
@@map("reported_payments")
}
// Fees a report covers. A fee can't be in two REPORTED reports at once
// (checked in the use-case, inside the transaction).
model ReportedPaymentItem {
reportedPaymentId String @map("reported_payment_id")
paymentId String @map("payment_id")
reportedPayment ReportedPayment @relation(fields: [reportedPaymentId], references: [id], onDelete: Cascade)
payment Payment @relation(fields: [paymentId], references: [id], onDelete: Cascade)
@@id([reportedPaymentId, paymentId])
@@index([paymentId])
@@map("reported_payment_items")
}