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>
This commit is contained in:
@@ -1,15 +1,17 @@
|
||||
import type { PrismaClient } from '@generated/prisma/client';
|
||||
import type { AttendeeDto, ProblemDetails, Result } from '@gruperly/shared';
|
||||
import { err, ok } from '@gruperly/shared';
|
||||
import { capacityReachedProblem, conflictProblem } from '@/http/problem-builders';
|
||||
import { type AttendeeRecord, toAttendeeDto } from '@/modules/attendees/lib/helpers';
|
||||
import { capacityReachedProblem } from '@/http/problem-builders';
|
||||
import { attendeeWithPerson, toAttendeeDto } from '@/modules/attendees/lib/helpers';
|
||||
import { alreadyInGroupProblem, findOrCreatePerson } from '@/modules/people/lib/people';
|
||||
import { type GroupWaitlistEntryRecord } from './helpers';
|
||||
|
||||
export type PromoteDb = Pick<PrismaClient, 'attendee' | 'groupWaitlistEntry'>;
|
||||
export type PromoteDb = Pick<PrismaClient, 'attendee' | 'groupWaitlistEntry' | 'person'>;
|
||||
|
||||
type PromoteGroup = {
|
||||
id: string;
|
||||
capacity: number | null;
|
||||
createdById: string;
|
||||
};
|
||||
|
||||
export async function findFirstPendingWaitlistEntry(
|
||||
@@ -23,13 +25,13 @@ export async function findFirstPendingWaitlistEntry(
|
||||
return record as GroupWaitlistEntryRecord | null;
|
||||
}
|
||||
|
||||
/* Whether someone can join `group`: free capacity (ACTIVE members) and no
|
||||
member there with the same phone. Shared by the waitlist promotion and the
|
||||
member move between groups. */
|
||||
/* Whether someone can join `group`: free capacity (ACTIVE members) and the
|
||||
person (when there already is one) not being a member there yet. Shared by
|
||||
the waitlist promotion, the member move and "Agregar a otro grupo". */
|
||||
export async function checkCanJoinGroup(
|
||||
db: Pick<PrismaClient, 'attendee'>,
|
||||
group: PromoteGroup,
|
||||
phone: string | null,
|
||||
group: Pick<PromoteGroup, 'id' | 'capacity'>,
|
||||
personId: string | null,
|
||||
): Promise<Result<void, ProblemDetails>> {
|
||||
if (group.capacity !== null) {
|
||||
const currentCount = await db.attendee.count({
|
||||
@@ -40,44 +42,44 @@ export async function checkCanJoinGroup(
|
||||
}
|
||||
}
|
||||
|
||||
if (phone) {
|
||||
if (personId) {
|
||||
const existing = await db.attendee.findFirst({
|
||||
where: { groupId: group.id, phone },
|
||||
where: { groupId: group.id, personId },
|
||||
select: { id: true },
|
||||
});
|
||||
if (existing) {
|
||||
return err(
|
||||
conflictProblem({
|
||||
detail: 'Ya existe un alumno registrado con este número de teléfono en este grupo.',
|
||||
code: 'attendee_already_registered',
|
||||
}),
|
||||
);
|
||||
return err(alreadyInGroupProblem());
|
||||
}
|
||||
}
|
||||
|
||||
return ok(undefined);
|
||||
}
|
||||
|
||||
/* The entry has its own contact data; if its phone is already one of the
|
||||
teacher's people, the new membership goes to that person. */
|
||||
export async function promoteWaitlistEntryRecord(
|
||||
db: PromoteDb,
|
||||
group: PromoteGroup,
|
||||
entry: GroupWaitlistEntryRecord,
|
||||
): Promise<Result<AttendeeDto, ProblemDetails>> {
|
||||
const canJoin = await checkCanJoinGroup(db, group, entry.phone);
|
||||
const person = await findOrCreatePerson(db, group.createdById, {
|
||||
fullName: entry.fullName,
|
||||
phone: entry.phone,
|
||||
email: entry.email,
|
||||
notes: entry.notes,
|
||||
});
|
||||
|
||||
const canJoin = await checkCanJoinGroup(db, group, person.created ? null : person.id);
|
||||
if (!canJoin.ok) {
|
||||
return canJoin;
|
||||
}
|
||||
|
||||
const attendee = await db.attendee.create({
|
||||
data: {
|
||||
groupId: group.id,
|
||||
fullName: entry.fullName,
|
||||
phone: entry.phone,
|
||||
email: entry.email,
|
||||
notes: entry.notes,
|
||||
},
|
||||
data: { groupId: group.id, personId: person.id },
|
||||
include: attendeeWithPerson,
|
||||
});
|
||||
|
||||
return ok(toAttendeeDto(attendee as unknown as AttendeeRecord));
|
||||
return ok(toAttendeeDto(attendee));
|
||||
}
|
||||
|
||||
export type PromoteFirstResult = {
|
||||
|
||||
Reference in New Issue
Block a user