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:
175
apps/backend/test/attendee-add-to-group.test.ts
Normal file
175
apps/backend/test/attendee-add-to-group.test.ts
Normal file
@@ -0,0 +1,175 @@
|
||||
import { beforeEach, describe, expect, it, mock, vi } from 'bun:test';
|
||||
import { Hono } from 'hono';
|
||||
|
||||
const db = {
|
||||
group: {
|
||||
findUnique: mock(),
|
||||
},
|
||||
attendee: {
|
||||
findFirst: mock(),
|
||||
count: mock(),
|
||||
create: mock(),
|
||||
update: mock(),
|
||||
},
|
||||
};
|
||||
|
||||
mock.module('@/lib/prisma', () => ({
|
||||
default: db,
|
||||
getPrismaClient: mock(),
|
||||
UnitOfWork: class {
|
||||
executeResult = mock(async (cb: (tx: unknown) => Promise<unknown>) => cb(db));
|
||||
execute = mock(async (cb: (tx: unknown) => Promise<unknown>) => cb(db));
|
||||
},
|
||||
}));
|
||||
|
||||
import prisma from '@/lib/prisma';
|
||||
import { groupsRoutes } from '@/modules/groups';
|
||||
|
||||
const teacherId = 'teacher-1';
|
||||
const group = (overrides: Record<string, unknown> = {}) => ({
|
||||
id: 'group-1',
|
||||
name: 'Taller de Pintura',
|
||||
createdById: teacherId,
|
||||
capacity: 20,
|
||||
...overrides,
|
||||
});
|
||||
const targetGroup = (overrides: Record<string, unknown> = {}) =>
|
||||
group({ id: 'group-2', name: 'Taller de Cerámica', ...overrides });
|
||||
|
||||
const person = {
|
||||
id: 'person-1',
|
||||
fullName: 'Martín Gómez',
|
||||
phone: '+5491133445566',
|
||||
email: null,
|
||||
guardianName: null,
|
||||
guardianPhone: null,
|
||||
notes: null,
|
||||
};
|
||||
|
||||
const membership = (overrides: Record<string, unknown> = {}) => ({
|
||||
id: 'att-2',
|
||||
groupId: 'group-2',
|
||||
personId: 'person-1',
|
||||
person,
|
||||
notifyToken: 'notify-2',
|
||||
status: 'ACTIVE',
|
||||
createdAt: new Date('2026-11-02T10:00:00.000Z'),
|
||||
updatedAt: new Date('2026-11-02T10:00:00.000Z'),
|
||||
...overrides,
|
||||
});
|
||||
|
||||
function addRequest(targetGroupId: string, userId = teacherId) {
|
||||
const app = new Hono();
|
||||
app.use('*', async (c, next) => {
|
||||
c.set('user', { id: userId } as never);
|
||||
await next();
|
||||
});
|
||||
app.route('/groups', groupsRoutes);
|
||||
return app.request('/groups/group-1/attendees/att-1/add-to-group', {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ targetGroupId }),
|
||||
});
|
||||
}
|
||||
|
||||
describe('POST /groups/:groupId/attendees/:attendeeId/add-to-group', () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
prisma.group.findUnique.mockReset();
|
||||
prisma.attendee.findFirst.mockReset();
|
||||
prisma.group.findUnique
|
||||
.mockResolvedValueOnce(group() as never)
|
||||
.mockResolvedValueOnce(targetGroup() as never);
|
||||
prisma.attendee.count.mockResolvedValue(3);
|
||||
});
|
||||
|
||||
it('creates a membership of the same person in the target group', async () => {
|
||||
prisma.attendee.findFirst
|
||||
.mockResolvedValueOnce({ personId: 'person-1' } as never)
|
||||
.mockResolvedValueOnce(null);
|
||||
prisma.attendee.create.mockResolvedValue(membership() as never);
|
||||
|
||||
const res = await addRequest('group-2');
|
||||
|
||||
expect(res.status).toBe(200);
|
||||
expect(await res.json()).toMatchObject({
|
||||
id: 'att-2',
|
||||
groupId: 'group-2',
|
||||
personId: 'person-1',
|
||||
fullName: 'Martín Gómez',
|
||||
});
|
||||
expect(prisma.attendee.create).toHaveBeenCalledWith({
|
||||
data: { groupId: 'group-2', personId: 'person-1' },
|
||||
include: { person: { select: expect.any(Object) } },
|
||||
});
|
||||
// The source membership is not touched.
|
||||
expect(prisma.attendee.update).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('reactivates a former (DROPPED) membership in the target group', async () => {
|
||||
prisma.attendee.findFirst
|
||||
.mockResolvedValueOnce({ personId: 'person-1' } as never)
|
||||
.mockResolvedValueOnce({ id: 'att-old', status: 'DROPPED' } as never);
|
||||
prisma.attendee.update.mockResolvedValue(membership({ id: 'att-old' }) as never);
|
||||
|
||||
const res = await addRequest('group-2');
|
||||
|
||||
expect(res.status).toBe(200);
|
||||
expect(prisma.attendee.update).toHaveBeenCalledWith({
|
||||
where: { id: 'att-old' },
|
||||
data: { status: 'ACTIVE' },
|
||||
include: { person: { select: expect.any(Object) } },
|
||||
});
|
||||
expect(prisma.attendee.create).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('rejects with 409 when the person is already in the target group', async () => {
|
||||
prisma.attendee.findFirst
|
||||
.mockResolvedValueOnce({ personId: 'person-1' } as never)
|
||||
.mockResolvedValueOnce({ id: 'att-2', status: 'PAUSED' } as never);
|
||||
|
||||
const res = await addRequest('group-2');
|
||||
|
||||
expect(res.status).toBe(409);
|
||||
expect((await res.json()).code).toBe('attendee_already_registered');
|
||||
expect(prisma.attendee.create).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('rejects with 409 when the target group reached its capacity', async () => {
|
||||
prisma.group.findUnique.mockReset();
|
||||
prisma.group.findUnique
|
||||
.mockResolvedValueOnce(group() as never)
|
||||
.mockResolvedValueOnce(targetGroup({ capacity: 3 }) as never);
|
||||
prisma.attendee.findFirst
|
||||
.mockResolvedValueOnce({ personId: 'person-1' } as never)
|
||||
.mockResolvedValueOnce(null);
|
||||
|
||||
const res = await addRequest('group-2');
|
||||
|
||||
expect(res.status).toBe(409);
|
||||
expect((await res.json()).code).toBe('group_capacity_reached');
|
||||
expect(prisma.attendee.create).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('rejects adding to the same group', async () => {
|
||||
prisma.attendee.findFirst.mockResolvedValueOnce({ personId: 'person-1' } as never);
|
||||
|
||||
const res = await addRequest('group-1');
|
||||
|
||||
expect(res.status).toBe(409);
|
||||
expect((await res.json()).code).toBe('attendee_same_group');
|
||||
});
|
||||
|
||||
it('rejects a target group owned by another teacher', async () => {
|
||||
prisma.group.findUnique.mockReset();
|
||||
prisma.group.findUnique
|
||||
.mockResolvedValueOnce(group() as never)
|
||||
.mockResolvedValueOnce(targetGroup({ createdById: 'other-user' }) as never);
|
||||
prisma.attendee.findFirst.mockResolvedValueOnce({ personId: 'person-1' } as never);
|
||||
|
||||
const res = await addRequest('group-2');
|
||||
|
||||
expect(res.status).toBe(403);
|
||||
expect(prisma.attendee.create).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user