- 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>
20 lines
883 B
TypeScript
20 lines
883 B
TypeScript
import type { Prisma } from '@generated/prisma/client';
|
|
|
|
export type RateLimitDb = { rateLimitEvent: Prisma.RateLimitEventDelegate };
|
|
|
|
/* Sliding window counted in `rate_limit_events`: returns false (and records
|
|
nothing) once `key` reached `limit` hits inside the window. Old hits of the
|
|
same key are swept on the way. */
|
|
export async function consumeRateLimit(
|
|
db: RateLimitDb,
|
|
key: string,
|
|
options: { limit: number; windowMs: number; now: Date },
|
|
): Promise<boolean> {
|
|
const windowStart = new Date(options.now.getTime() - options.windowMs);
|
|
await db.rateLimitEvent.deleteMany({ where: { key, createdAt: { lt: windowStart } } });
|
|
const hits = await db.rateLimitEvent.count({ where: { key, createdAt: { gte: windowStart } } });
|
|
if (hits >= options.limit) return false;
|
|
await db.rateLimitEvent.create({ data: { key, createdAt: options.now } });
|
|
return true;
|
|
}
|