Files
gruperly/apps/backend/src/modules/attendees/features/update/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

65 lines
2.3 KiB
TypeScript

import type { PrismaClient } from '@generated/prisma/client';
import type { AttendeeDto, ProblemDetails, Result, UpdateAttendee } from '@gruperly/shared';
import { err, ok } from '@gruperly/shared';
import { notFoundResourceProblem } from '@/http/problem-builders';
import prisma from '@/lib/prisma';
import { findGroupForUser } from '@/modules/group-waitlist/lib/helpers';
import { isUniqueViolation, personPhoneTakenProblem } from '@/modules/people/lib/people';
import { attendeeWithPerson, normalizePhone, toAttendeeDto } from '../../lib/helpers';
type UpdateAttendeeDeps = {
db?: Pick<PrismaClient, 'group' | 'attendee' | 'person'>;
};
type UpdateAttendeeCommand = {
groupId: string;
attendeeId: string;
userId: string;
input: UpdateAttendee;
};
export class UpdateAttendeeDetails {
constructor(private readonly deps: UpdateAttendeeDeps = {}) {}
async execute(command: UpdateAttendeeCommand): Promise<Result<AttendeeDto, ProblemDetails>> {
const { groupId, attendeeId, userId, input } = command;
const db = this.deps.db ?? prisma;
const access = await findGroupForUser(db, groupId, userId);
if (!access.ok) return access;
const attendee = await db.attendee.findFirst({
where: { id: attendeeId, groupId },
select: { personId: true },
});
if (!attendee) return err(notFoundResourceProblem('Attendee', attendeeId));
// Contact data is the person's: the change shows in every group they are in.
try {
await db.person.update({
where: { id: attendee.personId },
data: normalizeEditableFields(input),
});
} catch (error) {
if (isUniqueViolation(error)) return err(personPhoneTakenProblem());
throw error;
}
const updated = await db.attendee.findUniqueOrThrow({
where: { id: attendeeId },
include: attendeeWithPerson,
});
return ok(toAttendeeDto(updated));
}
}
function normalizeEditableFields(input: UpdateAttendee): UpdateAttendee {
return Object.fromEntries(
Object.entries(input).map(([key, value]) => {
if (value === '') return [key, null];
// Same normalization as the add form, so the phone keeps matching.
if (key === 'phone' && typeof value === 'string') return [key, normalizePhone(value)];
return [key, value];
}),
);
}