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:
@@ -15,6 +15,10 @@ const db = {
|
||||
create: mock(),
|
||||
count: mock(),
|
||||
},
|
||||
person: {
|
||||
findFirst: mock(),
|
||||
findMany: mock(),
|
||||
},
|
||||
groupWaitlistEntry: {
|
||||
findMany: mock(),
|
||||
findFirst: mock(),
|
||||
@@ -60,9 +64,34 @@ function makeApp(userValue: unknown) {
|
||||
return app;
|
||||
}
|
||||
|
||||
const personRow = (overrides: Record<string, unknown> = {}) => ({
|
||||
id: 'person-1',
|
||||
fullName: 'Martín Gómez',
|
||||
email: 'martin@example.com',
|
||||
phone: '+5491133445566',
|
||||
guardianName: null,
|
||||
guardianPhone: null,
|
||||
notes: 'Viene con su hermano',
|
||||
...overrides,
|
||||
});
|
||||
|
||||
const attendeeRow = (overrides: Record<string, unknown> = {}) => ({
|
||||
id: 'att-1',
|
||||
groupId: 'group-1',
|
||||
personId: 'person-1',
|
||||
person: personRow(),
|
||||
notifyToken: null,
|
||||
createdAt: new Date('2026-09-18T10:00:00.000Z'),
|
||||
updatedAt: new Date('2026-09-18T10:00:00.000Z'),
|
||||
...overrides,
|
||||
});
|
||||
|
||||
describe('attendee incorporation & invite-token in groups', () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
// Nobody with that phone among the teacher's people, unless a test says so.
|
||||
prisma.person.findFirst.mockResolvedValue(null);
|
||||
prisma.person.findMany.mockResolvedValue([]);
|
||||
});
|
||||
|
||||
describe('GET /groups/:groupId/invite-token', () => {
|
||||
@@ -109,18 +138,7 @@ describe('attendee incorporation & invite-token in groups', () => {
|
||||
it('creates an attendee for the group', async () => {
|
||||
prisma.group.findUnique.mockResolvedValue(mockGroup as never);
|
||||
prisma.attendee.findFirst.mockResolvedValue(null);
|
||||
prisma.attendee.create.mockResolvedValue({
|
||||
id: 'att-1',
|
||||
groupId: 'group-1',
|
||||
fullName: 'Martín Gómez',
|
||||
email: 'martin@example.com',
|
||||
phone: '+5491133445566',
|
||||
guardianName: null,
|
||||
guardianPhone: null,
|
||||
notes: 'Viene con su hermano',
|
||||
createdAt: new Date('2026-09-18T10:00:00.000Z'),
|
||||
updatedAt: new Date('2026-09-18T10:00:00.000Z'),
|
||||
} as never);
|
||||
prisma.attendee.create.mockResolvedValue(attendeeRow() as never);
|
||||
|
||||
const app = makeApp({ id: teacherId });
|
||||
const res = await app.request('/groups/group-1/attendees', {
|
||||
@@ -140,20 +158,95 @@ describe('attendee incorporation & invite-token in groups', () => {
|
||||
expect(data.outcome).toBe('created');
|
||||
expect(data.attendee.fullName).toBe('Martín Gómez');
|
||||
expect(data.attendee.phone).toBe('+5491133445566');
|
||||
expect(data.attendee.personId).toBe('person-1');
|
||||
// A new person of the group owner, created with the membership.
|
||||
expect(prisma.attendee.create).toHaveBeenCalledWith({
|
||||
data: {
|
||||
groupId: 'group-1',
|
||||
fullName: 'Martín Gómez',
|
||||
phone: '+5491133445566',
|
||||
email: 'martin@example.com',
|
||||
notes: 'Viene con su hermano',
|
||||
group: { connect: { id: 'group-1' } },
|
||||
person: {
|
||||
create: {
|
||||
createdById: teacherId,
|
||||
fullName: 'Martín Gómez',
|
||||
phone: '+5491133445566',
|
||||
email: 'martin@example.com',
|
||||
notes: 'Viene con su hermano',
|
||||
},
|
||||
},
|
||||
},
|
||||
include: { person: { select: expect.any(Object) } },
|
||||
});
|
||||
});
|
||||
|
||||
it('answers person_exists when the phone is already a person of another group', async () => {
|
||||
prisma.group.findUnique.mockResolvedValue(mockGroup as never);
|
||||
prisma.person.findFirst.mockResolvedValue({
|
||||
...personRow(),
|
||||
attendees: [{ id: 'att-9', groupId: 'group-9', status: 'ACTIVE', group: { name: 'Yoga' } }],
|
||||
} as never);
|
||||
|
||||
const app = makeApp({ id: teacherId });
|
||||
const res = await app.request('/groups/group-1/attendees', {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ firstName: 'Martín', lastName: 'Gómez', phone: '+54 9 11 3344-5566' }),
|
||||
});
|
||||
|
||||
expect(res.status).toBe(409);
|
||||
expect((await res.json()).code).toBe('person_exists');
|
||||
expect(prisma.person.findFirst).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
where: { createdById: teacherId, phone: { in: ['+5491133445566', '+54 9 11 3344-5566'] } },
|
||||
}),
|
||||
);
|
||||
expect(prisma.attendee.create).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('adds an existing person (personId) to the group without a new plan slot', async () => {
|
||||
prisma.group.findUnique.mockResolvedValue(mockGroup as never);
|
||||
prisma.person.findFirst.mockResolvedValue({ id: 'person-1', fullName: 'Martín Gómez' } as never);
|
||||
prisma.attendee.findFirst.mockResolvedValue(null);
|
||||
prisma.attendee.count.mockResolvedValue(1);
|
||||
prisma.attendee.create.mockResolvedValue(attendeeRow() as never);
|
||||
|
||||
const app = makeApp({ id: teacherId });
|
||||
const res = await app.request('/groups/group-1/attendees', {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ firstName: 'x', phone: '1133445566', personId: 'person-1' }),
|
||||
});
|
||||
|
||||
expect(res.status).toBe(201);
|
||||
expect(prisma.person.findFirst).toHaveBeenCalledWith({
|
||||
where: { id: 'person-1', createdById: teacherId },
|
||||
select: { id: true, fullName: true },
|
||||
});
|
||||
expect(prisma.attendee.create).toHaveBeenCalledWith({
|
||||
data: { group: { connect: { id: 'group-1' } }, person: { connect: { id: 'person-1' } } },
|
||||
include: { person: { select: expect.any(Object) } },
|
||||
});
|
||||
});
|
||||
|
||||
it('returns 404 for a personId of another teacher', async () => {
|
||||
prisma.group.findUnique.mockResolvedValue(mockGroup as never);
|
||||
prisma.person.findFirst.mockResolvedValue(null);
|
||||
|
||||
const app = makeApp({ id: teacherId });
|
||||
const res = await app.request('/groups/group-1/attendees', {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ firstName: 'x', phone: '1133445566', personId: 'person-other' }),
|
||||
});
|
||||
|
||||
expect(res.status).toBe(404);
|
||||
expect(prisma.attendee.create).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('rejects duplicate phone number in individual add', async () => {
|
||||
prisma.group.findUnique.mockResolvedValue(mockGroup as never);
|
||||
prisma.attendee.findFirst.mockResolvedValue({ id: 'att-existing' } as never);
|
||||
prisma.person.findFirst.mockResolvedValue({
|
||||
...personRow(),
|
||||
attendees: [{ id: 'att-existing', groupId: 'group-1', status: 'ACTIVE', group: { name: 'Pintura' } }],
|
||||
} as never);
|
||||
|
||||
const app = makeApp({ id: teacherId });
|
||||
const res = await app.request('/groups/group-1/attendees', {
|
||||
@@ -197,18 +290,7 @@ describe('attendee incorporation & invite-token in groups', () => {
|
||||
prisma.group.findUnique.mockResolvedValue(mockGroup as never);
|
||||
prisma.attendee.findFirst.mockResolvedValue(null);
|
||||
prisma.attendee.count.mockResolvedValue(20);
|
||||
prisma.attendee.create.mockResolvedValue({
|
||||
id: 'att-1',
|
||||
groupId: 'group-1',
|
||||
fullName: 'Martín Gómez',
|
||||
email: null,
|
||||
phone: '+5491133445566',
|
||||
guardianName: null,
|
||||
guardianPhone: null,
|
||||
notes: null,
|
||||
createdAt: new Date('2026-09-18T10:00:00.000Z'),
|
||||
updatedAt: new Date('2026-09-18T10:00:00.000Z'),
|
||||
} as never);
|
||||
prisma.attendee.create.mockResolvedValue(attendeeRow() as never);
|
||||
|
||||
const app = makeApp({ id: teacherId });
|
||||
const res = await app.request('/groups/group-1/attendees?allowOverflow=true', {
|
||||
@@ -412,16 +494,19 @@ describe('attendee incorporation & invite-token in groups', () => {
|
||||
describe('POST /groups/:groupId/attendees/bulk (bulk import)', () => {
|
||||
it('imports multiple attendees, skipping duplicates and reporting counts', async () => {
|
||||
prisma.group.findUnique.mockResolvedValue(mockGroup as never);
|
||||
// Simulate that an attendee with phone '11111111' already exists
|
||||
prisma.attendee.findMany.mockResolvedValue([
|
||||
{ phone: '11111111' },
|
||||
// The teacher's people: '11111111' is already in this group, '44444444'
|
||||
// is in another group only (and active there).
|
||||
prisma.person.findMany.mockResolvedValue([
|
||||
{ id: 'person-in-group', phone: '11111111', attendees: [{ groupId: 'group-1', status: 'ACTIVE' }] },
|
||||
{ id: 'person-elsewhere', phone: '44444444', attendees: [{ groupId: 'group-9', status: 'ACTIVE' }] },
|
||||
] as never);
|
||||
|
||||
prisma.attendee.create.mockImplementation(async ({ data }: { data: any }) => ({
|
||||
id: `att-${Math.random()}`,
|
||||
...data,
|
||||
guardianName: null,
|
||||
guardianPhone: null,
|
||||
groupId: 'group-1',
|
||||
personId: data.personId ?? 'person-new',
|
||||
notifyToken: null,
|
||||
person: personRow({ fullName: data.person?.create?.fullName ?? 'Existente' }),
|
||||
createdAt: new Date('2026-09-18T10:00:00.000Z'),
|
||||
updatedAt: new Date('2026-09-18T10:00:00.000Z'),
|
||||
}));
|
||||
@@ -434,19 +519,27 @@ describe('attendee incorporation & invite-token in groups', () => {
|
||||
attendees: [
|
||||
{ firstName: 'Ana', lastName: 'García', phone: '22222222' },
|
||||
{ firstName: 'Pedro', lastName: 'López', phone: '33333333' },
|
||||
{ firstName: 'Duplicado', lastName: 'Existente', phone: '11111111' }, // Already in the DB
|
||||
{ firstName: 'Duplicado', lastName: 'Existente', phone: '11111111' }, // Already in the group
|
||||
{ firstName: 'Duplicado', lastName: 'EnLote', phone: '22222222' }, // Duplicate within the batch
|
||||
{ firstName: 'Otra', lastName: 'Persona', phone: '44444444' }, // In another group: linked
|
||||
],
|
||||
}),
|
||||
});
|
||||
|
||||
expect(res.status).toBe(201);
|
||||
const data = await res.json();
|
||||
expect(data.totalProcessed).toBe(4);
|
||||
expect(data.createdCount).toBe(2);
|
||||
expect(data.totalProcessed).toBe(5);
|
||||
expect(data.createdCount).toBe(3);
|
||||
expect(data.linkedCount).toBe(1);
|
||||
expect(data.duplicatesCount).toBe(2);
|
||||
expect(data.message).toBe('2 alumnos agregados con éxito, 2 duplicados omitidos.');
|
||||
expect(prisma.attendee.create).toHaveBeenCalledTimes(2);
|
||||
expect(data.message).toBe(
|
||||
'3 alumnos agregados con éxito (1 ya estaban en otros de tus grupos), 2 duplicados omitidos.',
|
||||
);
|
||||
expect(prisma.attendee.create).toHaveBeenCalledTimes(3);
|
||||
expect(prisma.attendee.create).toHaveBeenCalledWith({
|
||||
data: { groupId: 'group-1', personId: 'person-elsewhere' },
|
||||
include: { person: { select: expect.any(Object) } },
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -456,12 +549,8 @@ describe('attendee incorporation & invite-token in groups', () => {
|
||||
{
|
||||
id: 'att-1',
|
||||
groupId: 'group-1',
|
||||
fullName: 'Ana García',
|
||||
email: null,
|
||||
phone: '22222222',
|
||||
guardianName: null,
|
||||
guardianPhone: null,
|
||||
notes: null,
|
||||
personId: 'person-1',
|
||||
person: personRow({ fullName: 'Ana García', phone: '22222222', email: null, notes: null }),
|
||||
createdAt: new Date('2026-09-18T10:00:00.000Z'),
|
||||
updatedAt: new Date('2026-09-18T10:00:00.000Z'),
|
||||
attendances: [],
|
||||
@@ -487,6 +576,7 @@ describe('attendee incorporation & invite-token in groups', () => {
|
||||
// The row summary is requested in the same query: one request per
|
||||
// member would be N+1 over a listing of up to 100.
|
||||
include: {
|
||||
person: { select: expect.any(Object) },
|
||||
attendances: {
|
||||
orderBy: { classSession: { startsAt: 'desc' } },
|
||||
take: RECENT_ATTENDANCE_LIMIT,
|
||||
|
||||
Reference in New Issue
Block a user