Files
gruperly/apps/backend/src/modules/student-access/lib/status.ts
Jose Selesan d21e1c3730 feat: un miembro puede pertenecer a más de un grupo
- Nuevo modelo `Person` (`people`): contacto y notas por profe, con
  teléfono único (índice parcial `people_owner_phone_key`). `Attendee`
  pasa a ser la membresía (`group_id` + `person_id`, único por grupo).
- La cuenta de la app de alumnos se vincula a la persona
  (`student_account_people`): una sola cuenta ve todos sus grupos.
- Migración unifica attendees del mismo profe y teléfono, y fusiona sus
  cuentas de alumno (conserva la usada más recientemente).
- `POST /groups/:gid/attendees/:aid/add-to-group` y
  `GET /people/lookup` para detectar personas existentes al cargar;
  el alta responde `409 person_exists` y acepta `personId`. Bulk, lista
  de espera y preinscripciones vinculan solas.
- El límite del plan cuenta personas con al menos un grupo activo.
- Web: "Agregar a otro grupo", sección "También está en" en la ficha y
  aviso "Ya tenés a esta persona" en el alta individual.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-03 09:46:37 -03:00

72 lines
2.2 KiB
TypeScript

import type { PrismaClient } from '@generated/prisma/client';
import type { StudentAccessDto } from '@gruperly/shared';
export type StudentAccessStatusDb = Pick<PrismaClient, 'studentAccountPerson'>;
/* Passive state for the attendee's card: active (a live session) > invited
(a pending invitation) > expired (invited before, nothing alive) > not invited.
It is the person's access, so every group of theirs shows the same state. */
export async function computeStudentAccess(
db: StudentAccessStatusDb,
personId: string,
now: Date,
): Promise<StudentAccessDto> {
const link = await db.studentAccountPerson.findFirst({
where: { personId },
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,
};
}