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:
@@ -4,11 +4,12 @@ import { type CountryCode, err, INVITATION_TTL_HOURS, ok, toWhatsappE164 } from
|
||||
import { notFoundResourceProblem } from '@/http/problem-builders';
|
||||
import prisma, { UnitOfWork } from '@/lib/prisma';
|
||||
import { findGroupForUser } from '@/modules/group-waitlist/lib/helpers';
|
||||
import { contactPhone } from '@/modules/people/lib/people';
|
||||
import { generateAccessToken, hashToken, studentLinkUrl } from '@/modules/student-portal/lib';
|
||||
import { computeStudentAccess } from '../../lib/status';
|
||||
|
||||
type InviteStudentDeps = {
|
||||
db?: Pick<PrismaClient, 'group' | 'attendee' | 'studentAccountAttendee'>;
|
||||
db?: Pick<PrismaClient, 'group' | 'attendee' | 'studentAccountPerson'>;
|
||||
unitOfWork?: Pick<UnitOfWork, 'executeResult'>;
|
||||
now?: Date;
|
||||
};
|
||||
@@ -36,9 +37,8 @@ export class InviteStudent {
|
||||
where: { id: attendeeId, groupId },
|
||||
select: {
|
||||
id: true,
|
||||
fullName: true,
|
||||
phone: true,
|
||||
guardianPhone: true,
|
||||
personId: true,
|
||||
person: { select: { fullName: true, phone: true, guardianPhone: true } },
|
||||
group: { select: { owner: { select: { country: true } } } },
|
||||
},
|
||||
});
|
||||
@@ -49,8 +49,9 @@ export class InviteStudent {
|
||||
const unitOfWork = this.deps.unitOfWork ?? new UnitOfWork(prisma as PrismaClient);
|
||||
|
||||
const issued = await unitOfWork.executeResult(async (tx) => {
|
||||
const link = await tx.studentAccountAttendee.findFirst({
|
||||
where: { attendeeId },
|
||||
// One account per person: inviting from any of their groups reuses it.
|
||||
const link = await tx.studentAccountPerson.findFirst({
|
||||
where: { personId: attendee.personId },
|
||||
orderBy: { createdAt: 'asc' },
|
||||
select: { accountId: true },
|
||||
});
|
||||
@@ -58,7 +59,11 @@ export class InviteStudent {
|
||||
link?.accountId
|
||||
?? (
|
||||
await tx.studentAccount.create({
|
||||
data: { name: attendee.fullName, createdById: userId, attendees: { create: { attendeeId } } },
|
||||
data: {
|
||||
name: attendee.person.fullName,
|
||||
createdById: userId,
|
||||
people: { create: { personId: attendee.personId } },
|
||||
},
|
||||
select: { id: true },
|
||||
})
|
||||
).id;
|
||||
@@ -90,8 +95,8 @@ export class InviteStudent {
|
||||
return ok({
|
||||
url: studentLinkUrl(token),
|
||||
expiresAt: expiresAt.toISOString(),
|
||||
whatsappPhone: toWhatsappPhone(attendee.phone ?? attendee.guardianPhone, attendee.group.owner.country),
|
||||
access: await computeStudentAccess(db, attendeeId, now),
|
||||
whatsappPhone: toWhatsappPhone(contactPhone(attendee.person), attendee.group.owner.country),
|
||||
access: await computeStudentAccess(db, attendee.personId, now),
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user