import { describe, expect, it, mock } from 'bun:test'; import { ok } from '@gruperly/shared'; import { CreateAttendeeUseCase } from '@/modules/attendees/features/create/use-case'; import { UpdateAttendeeGuardian } from '@/modules/attendees/features/update-guardian/use-case'; import { contactPhone, findPersonSummaryByPhone } from '@/modules/people/lib/people'; const ownerId = 'owner-1'; const at = new Date('2026-11-01T00:00:00.000Z'); function kidsGroup(overrides: Record = {}) { return { id: 'g1', createdById: ownerId, capacity: null, audience: 'KIDS', members: [], ...overrides }; } function createdAttendee(guardian = { id: 'adult-1', fullName: 'Ana Pérez', phone: '1155550000', email: null }) { return { id: 'att-1', groupId: 'g1', personId: 'kid-1', notifyToken: null, createdAt: at, updatedAt: at, person: { id: 'kid-1', fullName: 'Juan Pérez', email: null, phone: null, notes: null, guardians: [{ guardian }] }, }; } function makeDb(overrides: { adult?: unknown; sameNameKid?: unknown } = {}) { return { group: { findUnique: mock(async () => kidsGroup()) }, person: { // 1st call: the adult by phone. 2nd: a kid of that adult with the same name. findFirst: mock() .mockResolvedValueOnce(overrides.adult ?? null) .mockResolvedValueOnce(overrides.sameNameKid ?? null), }, attendee: { findFirst: mock(async () => null), count: mock(async () => 0), create: mock(async () => createdAttendee()), update: mock(), }, groupWaitlistEntry: { findFirst: mock(), create: mock() }, }; } const planGuard = { canAddAttendees: mock(async () => ok(undefined)) }; const guardian = { fullName: 'Ana Pérez', phone: '11 5555-0000' }; describe('kids groups: adding a kid', () => { it('asks for the adult in charge', async () => { const db = makeDb(); const result = await new CreateAttendeeUseCase({ db: db as never, planGuard }).execute('g1', ownerId, { firstName: 'Juan', lastName: 'Pérez', phone: '1144440000', }); expect(result.ok).toBe(false); if (!result.ok) expect(result.error).toMatchObject({ status: 400, code: 'guardian_required' }); expect(db.attendee.create).not.toHaveBeenCalled(); }); it('creates the kid with no phone and a new adult in charge', async () => { const db = makeDb(); const result = await new CreateAttendeeUseCase({ db: db as never, planGuard }).execute('g1', ownerId, { firstName: 'Juan', lastName: 'Pérez', guardian, }); expect(result.ok).toBe(true); if (result.ok && result.value.outcome === 'created') { expect(result.value.attendee).toMatchObject({ guardianName: 'Ana Pérez', guardianPhone: '1155550000', phone: null }); } expect(db.attendee.create).toHaveBeenCalledWith( expect.objectContaining({ data: { group: { connect: { id: 'g1' } }, person: { create: { createdById: ownerId, fullName: 'Juan Pérez', phone: null, email: null, notes: null, guardians: { create: { guardian: { create: { createdById: ownerId, fullName: 'Ana Pérez', phone: '1155550000', email: null } }, }, }, }, }, }, }), ); }); it('links a sibling to the adult the teacher already has (same phone)', async () => { const db = makeDb({ adult: { id: 'adult-1', fullName: 'Ana Pérez', email: null, phone: '1155550000', attendees: [] } }); const result = await new CreateAttendeeUseCase({ db: db as never, planGuard }).execute('g1', ownerId, { firstName: 'Sofía', lastName: 'Pérez', guardian, }); expect(result.ok).toBe(true); // The sibling lookup: same adult, same name, case-insensitive. expect(db.person.findFirst.mock.calls[1]?.[0]).toMatchObject({ where: { createdById: ownerId, fullName: { equals: 'Sofía Pérez', mode: 'insensitive' }, guardians: { some: { guardianId: 'adult-1' } }, }, }); expect(db.attendee.create.mock.calls[0]?.[0]).toMatchObject({ data: { person: { create: { fullName: 'Sofía Pérez', guardians: { create: { guardian: { connect: { id: 'adult-1' } } } } } } }, }); }); it('recognizes the same kid of the same adult instead of creating a twin', async () => { const db = makeDb({ adult: { id: 'adult-1', fullName: 'Ana Pérez', email: null, phone: '1155550000', attendees: [] }, sameNameKid: { id: 'kid-1', fullName: 'Juan Pérez', phone: null, guardians: [] }, }); db.attendee.findFirst.mockResolvedValue({ id: 'att-1', status: 'ACTIVE' } as never); const result = await new CreateAttendeeUseCase({ db: db as never, planGuard }).execute('g1', ownerId, { firstName: 'juan', lastName: 'pérez', guardian, }); expect(result.ok).toBe(false); if (!result.ok) expect(result.error).toMatchObject({ code: 'attendee_already_registered' }); expect(db.attendee.create).not.toHaveBeenCalled(); }); it('keeps asking for a phone in an adults group', async () => { const db = makeDb(); db.group.findUnique.mockResolvedValue(kidsGroup({ audience: 'ADULTS' })); const result = await new CreateAttendeeUseCase({ db: db as never, planGuard }).execute('g1', ownerId, { firstName: 'Ana', lastName: '', }); expect(result.ok).toBe(true); expect(db.attendee.create.mock.calls[0]?.[0]).toMatchObject({ data: { person: { create: { phone: null } } } }); }); }); describe('kids groups: contact', () => { it('reaches a kid through the adult in charge', () => { expect(contactPhone({ phone: '1144440000', guardians: [{ guardian: { phone: '1155550000' } }] })).toBe('1155550000'); expect(contactPhone({ phone: '1144440000', guardians: [] })).toBe('1144440000'); expect(contactPhone({ phone: null })).toBeNull(); }); it('the lookup by phone offers the kids of that adult', async () => { const db = { person: { findFirst: mock(async () => ({ id: 'adult-1', fullName: 'Ana Pérez', email: null, phone: '1155550000', attendees: [] })), findUnique: mock(async () => ({ children: [ { child: { id: 'kid-1', fullName: 'Juan Pérez', attendees: [{ id: 'att-1', groupId: 'g1', status: 'ACTIVE', group: { name: 'Natación' } }], }, }, ], })), }, }; const summary = await findPersonSummaryByPhone(db as never, ownerId, '1155550000'); expect(summary?.children).toEqual([ { id: 'kid-1', fullName: 'Juan Pérez', groups: [{ attendeeId: 'att-1', groupId: 'g1', groupName: 'Natación', status: 'ACTIVE' }] }, ]); }); }); describe('kids groups: editing the adult in charge', () => { function guardianDb(person: Record) { return { group: { findUnique: mock(async () => ({ id: 'g1', createdById: ownerId, members: [] })) }, attendee: { findFirst: mock(async () => ({ personId: 'kid-1', person: { id: 'kid-1', ...person } })), findUniqueOrThrow: mock(async () => createdAttendee()), }, person: { update: mock(), findFirst: mock(async () => null), create: mock(async () => ({ id: 'adult-2' })) }, personGuardian: { create: mock() }, }; } it('edits the adult the kid already has (shared with the siblings)', async () => { const db = guardianDb({ phone: null, guardians: [{ guardian: { id: 'adult-1', fullName: 'Ana', phone: '1155550000', email: null } }] }); const result = await new UpdateAttendeeGuardian({ db: db as never }).execute({ groupId: 'g1', attendeeId: 'att-1', userId: ownerId, input: { fullName: 'Ana Pérez', phone: '11 5555-0001', email: 'ana@example.com' }, }); expect(result.ok).toBe(true); expect(db.person.update).toHaveBeenCalledWith({ where: { id: 'adult-1' }, data: { fullName: 'Ana Pérez', phone: '1155550001', email: 'ana@example.com' }, }); expect(db.personGuardian.create).not.toHaveBeenCalled(); }); it('links a new adult when the kid had none', async () => { const db = guardianDb({ phone: null, guardians: [] }); const result = await new UpdateAttendeeGuardian({ db: db as never }).execute({ groupId: 'g1', attendeeId: 'att-1', userId: ownerId, input: { fullName: 'Ana Pérez', phone: '1155550000' }, }); expect(result.ok).toBe(true); expect(db.personGuardian.create).toHaveBeenCalledWith({ data: { childId: 'kid-1', guardianId: 'adult-2' } }); }); });