Files
gruperly/apps/backend/test/attendee-get-by-id.test.ts
Jose Selesan 58c776e18c feat(groups): kids groups with an adult in charge
- Add the `kidsGroups` plan feature (Pro) and `Group.audience` (ADULTS | KIDS);
  creating or switching a group to KIDS requires the feature, existing KIDS
  groups keep working after a downgrade.
- Model the adult in charge as a Person linked through `person_guardians`;
  migrate the old guardian_name/guardian_phone columns and drop them.
- Kids quick add: kid + guardian form, guardian lookup by phone to pick an
  existing kid or add a sibling; backend rejects KIDS adds without guardian.
- Attendee page: "Adulto a cargo" section with siblings, editable via
  PATCH /groups/:gid/attendees/:aid/guardian.
- Contact phone (WhatsApp jobs, attention, student access invite, students at
  risk, attendee actions) prefers the guardian's phone.
- Edit group screen (PATCH /groups/:gid, owner only); switching back to ADULTS
  is blocked while kids have no phone of their own.
- Hide bulk import in KIDS groups until it carries guardian data.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-11 13:27:13 -03:00

146 lines
4.6 KiB
TypeScript

import { beforeEach, describe, expect, it, mock, vi } from 'bun:test';
import { Hono } from 'hono';
const db = {
group: {
findUnique: mock(),
},
attendee: {
findFirst: mock(),
},
personGuardian: {
findMany: 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',
notes: null,
// The adult in charge, a person of their own.
guardians: [{ guardian: { id: 'person-9', fullName: 'Luis Pérez', phone: '5598765432', email: 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);
prisma.personGuardian.findMany.mockResolvedValue([{ child: { id: 'person-2', fullName: 'Tomás Pérez' } }]);
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',
guardianPersonId: 'person-9',
guardianName: 'Luis Pérez',
guardianPhone: '5598765432',
guardianEmail: 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' }],
siblings: [{ personId: 'person-2', fullName: 'Tomás Pérez' }],
});
expect(prisma.personGuardian.findMany).toHaveBeenCalledWith(
expect.objectContaining({ where: { guardianId: 'person-9', childId: { not: 'person-1' } } }),
);
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);
});
});