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:
70
apps/backend/src/modules/student-access/lib/status.ts
Normal file
70
apps/backend/src/modules/student-access/lib/status.ts
Normal file
@@ -0,0 +1,70 @@
|
||||
import type { PrismaClient } from '@generated/prisma/client';
|
||||
import type { StudentAccessDto } from '@gruperly/shared';
|
||||
|
||||
export type StudentAccessStatusDb = Pick<PrismaClient, 'studentAccountAttendee'>;
|
||||
|
||||
/* Passive state for the attendee's card: active (a live session) > invited
|
||||
(a pending invitation) > expired (invited before, nothing alive) > not invited. */
|
||||
export async function computeStudentAccess(
|
||||
db: StudentAccessStatusDb,
|
||||
attendeeId: string,
|
||||
now: Date,
|
||||
): Promise<StudentAccessDto> {
|
||||
const link = await db.studentAccountAttendee.findFirst({
|
||||
where: { attendeeId },
|
||||
orderBy: { createdAt: 'asc' },
|
||||
select: {
|
||||
account: {
|
||||
select: {
|
||||
_count: { select: { passkeys: true } },
|
||||
sessions: {
|
||||
where: { revokedAt: null, expiresAt: { gt: now } },
|
||||
orderBy: { lastUsedAt: 'desc' },
|
||||
select: { device: true, createdAt: true, lastUsedAt: true },
|
||||
},
|
||||
tokens: {
|
||||
where: { kind: 'INVITATION' },
|
||||
orderBy: { createdAt: 'desc' },
|
||||
take: 1,
|
||||
select: { usedAt: true, revokedAt: true, expiresAt: true },
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
if (!link) {
|
||||
return { status: 'not_invited', invitationExpiresAt: null, devices: [], hasPasskey: false };
|
||||
}
|
||||
|
||||
const { account } = link;
|
||||
const devices = account.sessions.map((session) => ({
|
||||
device: session.device,
|
||||
createdAt: session.createdAt.toISOString(),
|
||||
lastUsedAt: session.lastUsedAt.toISOString(),
|
||||
}));
|
||||
const hasPasskey = account._count.passkeys > 0;
|
||||
const lastInvitation = account.tokens[0] ?? null;
|
||||
const pending =
|
||||
lastInvitation && !lastInvitation.usedAt && !lastInvitation.revokedAt && lastInvitation.expiresAt > now
|
||||
? lastInvitation
|
||||
: null;
|
||||
|
||||
if (devices.length > 0) {
|
||||
return { status: 'active', invitationExpiresAt: null, devices, hasPasskey };
|
||||
}
|
||||
if (pending) {
|
||||
return {
|
||||
status: 'invited',
|
||||
invitationExpiresAt: pending.expiresAt.toISOString(),
|
||||
devices,
|
||||
hasPasskey,
|
||||
};
|
||||
}
|
||||
return {
|
||||
status: lastInvitation ? 'expired' : 'not_invited',
|
||||
invitationExpiresAt: null,
|
||||
devices,
|
||||
hasPasskey,
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user