- 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>
136 lines
4.0 KiB
TypeScript
136 lines
4.0 KiB
TypeScript
import { beforeEach, describe, expect, it, mock, vi } from 'bun:test';
|
|
import { Hono } from 'hono';
|
|
|
|
const db = {
|
|
group: {
|
|
findUnique: mock(),
|
|
},
|
|
attendee: {
|
|
findFirst: mock(),
|
|
},
|
|
};
|
|
|
|
mock.module('@/lib/prisma', () => ({
|
|
default: db,
|
|
getPrismaClient: mock(),
|
|
UnitOfWork: class {},
|
|
}));
|
|
|
|
import prisma from '@/lib/prisma';
|
|
import getAttendeeRoute from '@/modules/attendees/features/get-by-id/route';
|
|
|
|
const userId = 'user-1';
|
|
|
|
function makeApp(userValue: unknown) {
|
|
const app = new Hono();
|
|
app.use('*', async (c, next) => {
|
|
c.set('user', userValue as never);
|
|
await next();
|
|
});
|
|
app.route('/groups', getAttendeeRoute);
|
|
return app;
|
|
}
|
|
|
|
function makeGroup(overrides: Record<string, unknown> = {}) {
|
|
return { id: 'group-1', name: 'Funcional', createdById: userId, members: [], ...overrides };
|
|
}
|
|
|
|
const attendee = {
|
|
id: 'a-1',
|
|
groupId: 'group-1',
|
|
personId: 'person-1',
|
|
person: {
|
|
id: 'person-1',
|
|
fullName: 'Ana Pérez',
|
|
email: 'ana@example.com',
|
|
phone: '5512345678',
|
|
guardianName: 'Luis Pérez',
|
|
guardianPhone: null,
|
|
notes: null,
|
|
// Every membership of the person: this one and another group.
|
|
attendees: [
|
|
{ id: 'a-1', groupId: 'group-1', status: 'ACTIVE', group: { name: 'Funcional' } },
|
|
{ id: 'a-2', groupId: 'group-2', status: 'PAUSED', group: { name: 'Yoga' } },
|
|
],
|
|
},
|
|
notifyToken: 'token-1',
|
|
createdAt: new Date('2026-08-01T10:00:00.000Z'),
|
|
updatedAt: new Date('2026-08-01T10:00:00.000Z'),
|
|
payments: [
|
|
{ status: 'PENDING', dueDate: new Date('2026-10-10T00:00:00.000Z') },
|
|
],
|
|
};
|
|
|
|
describe('GET /groups/:groupId/attendees/:attendeeId', () => {
|
|
beforeEach(() => {
|
|
vi.clearAllMocks();
|
|
});
|
|
|
|
it('devuelve el participante del grupo', async () => {
|
|
prisma.group.findUnique.mockResolvedValue(makeGroup());
|
|
prisma.attendee.findFirst.mockResolvedValue(attendee);
|
|
|
|
const res = await makeApp({ id: userId }).request('/groups/group-1/attendees/a-1');
|
|
|
|
expect(res.status).toBe(200);
|
|
expect(await res.json()).toEqual({
|
|
id: 'a-1',
|
|
groupId: 'group-1',
|
|
personId: 'person-1',
|
|
fullName: 'Ana Pérez',
|
|
email: 'ana@example.com',
|
|
phone: '5512345678',
|
|
guardianName: 'Luis Pérez',
|
|
guardianPhone: null,
|
|
notes: null,
|
|
notifyToken: 'token-1',
|
|
createdAt: '2026-08-01T10:00:00.000Z',
|
|
updatedAt: '2026-08-01T10:00:00.000Z',
|
|
payment: { status: 'PENDING', dueDate: '2026-10-10T00:00:00.000Z' },
|
|
otherGroups: [{ attendeeId: 'a-2', groupId: 'group-2', groupName: 'Yoga', status: 'PAUSED' }],
|
|
});
|
|
expect(prisma.attendee.findFirst).toHaveBeenCalledWith({
|
|
where: { id: 'a-1', groupId: 'group-1' },
|
|
include: {
|
|
person: { select: expect.objectContaining({ fullName: true, attendees: expect.any(Object) }) },
|
|
payments: {
|
|
where: {
|
|
status: { not: 'CANCELLED' },
|
|
dueDate: { gte: expect.any(Date), lt: expect.any(Date) },
|
|
},
|
|
orderBy: { dueDate: 'desc' },
|
|
take: 1,
|
|
select: { status: true, dueDate: true },
|
|
},
|
|
},
|
|
});
|
|
});
|
|
|
|
it('devuelve 404 si el participante no pertenece al grupo', async () => {
|
|
prisma.group.findUnique.mockResolvedValue(makeGroup());
|
|
prisma.attendee.findFirst.mockResolvedValue(null);
|
|
|
|
const res = await makeApp({ id: userId }).request('/groups/group-1/attendees/nope');
|
|
|
|
expect(res.status).toBe(404);
|
|
expect(await res.json()).toMatchObject({ code: 'not_found' });
|
|
});
|
|
|
|
it('devuelve 403 si el usuario no tiene acceso al grupo', async () => {
|
|
prisma.group.findUnique.mockResolvedValue(
|
|
makeGroup({ createdById: 'otro-user', members: [] }),
|
|
);
|
|
|
|
const res = await makeApp({ id: userId }).request('/groups/group-1/attendees/a-1');
|
|
|
|
expect(res.status).toBe(403);
|
|
expect(await res.json()).toMatchObject({ code: 'group_access_denied' });
|
|
});
|
|
|
|
it('devuelve 401 si no hay sesión', async () => {
|
|
const res = await makeApp(undefined).request('/groups/group-1/attendees/a-1');
|
|
|
|
expect(res.status).toBe(401);
|
|
});
|
|
});
|