Files
gruperly/apps/backend/src/modules/student-portal/features/me/use-case.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

81 lines
2.6 KiB
TypeScript

import type { PrismaClient } from '@generated/prisma/client';
import type { ProblemDetails, Result, StudentMe } from '@gruperly/shared';
import { err, ok } from '@gruperly/shared';
import { unauthorizedProblem } from '@/http/problem-details';
import prisma from '@/lib/prisma';
type StudentMeDeps = { db?: Pick<PrismaClient, 'studentAccount'>; now?: Date };
export class GetStudentMe {
constructor(private readonly deps: StudentMeDeps = {}) {}
async execute(accountId: string): Promise<Result<StudentMe, ProblemDetails>> {
const db = this.deps.db ?? prisma;
const account = await db.studentAccount.findUnique({
where: { id: accountId },
select: {
id: true,
name: true,
recoveryEmail: true,
pendingRecoveryEmail: true,
welcomedAt: true,
_count: { select: { passkeys: true } },
// Linked people, and every group each of them is in.
people: {
orderBy: { createdAt: 'asc' },
select: {
person: {
select: {
fullName: true,
attendees: {
where: { status: { not: 'DROPPED' } },
orderBy: { createdAt: 'asc' },
select: {
id: true,
group: { select: { id: true, name: true, owner: { select: { name: true } } } },
},
},
},
},
},
},
},
});
if (!account) return err(unauthorizedProblem('/student-portal/me'));
return ok({
account: {
id: account.id,
name: account.name,
recoveryEmail: account.recoveryEmail,
pendingRecoveryEmail: account.pendingRecoveryEmail,
hasPasskey: account._count.passkeys > 0,
welcomed: account.welcomedAt !== null,
},
students: account.people.flatMap(({ person }) =>
person.attendees.map((attendee) => ({
attendeeId: attendee.id,
fullName: person.fullName,
groupId: attendee.group.id,
groupName: attendee.group.name,
teacherName: attendee.group.owner.name,
})),
),
});
}
}
/* The welcome screen is shown until the student goes past it once. */
export class MarkStudentWelcomed {
constructor(private readonly deps: StudentMeDeps = {}) {}
async execute(accountId: string): Promise<Result<StudentMe, ProblemDetails>> {
const db = this.deps.db ?? prisma;
await db.studentAccount.updateMany({
where: { id: accountId, welcomedAt: null },
data: { welcomedAt: this.deps.now ?? new Date() },
});
return new GetStudentMe({ db }).execute(accountId);
}
}