feat: implement group pre-registration feature with approval and rejection workflows

- Add API endpoints for managing group pre-registrations, including approval and rejection.
- Create database migration for `group_pre_registrations` table and associated enum for status.
- Implement use cases for listing, approving, and rejecting pre-registrations.
- Add frontend components for displaying pre-registration details and managing approvals/rejections.
- Update shared schemas to include pre-registration data structures and validation.
- Remove obsolete `WaitlistNoticeCard` component and replace with `PreRegistrationNoticeCard`.
This commit is contained in:
Jose Selesan
2026-09-29 15:25:24 -03:00
parent 0f02331dfc
commit 45a1f50231
32 changed files with 1335 additions and 165 deletions

View File

@@ -10,9 +10,10 @@ const db = {
create: mock(),
count: mock(),
},
groupWaitlistEntry: {
groupPreRegistration: {
findFirst: mock(),
create: mock(),
update: mock(),
},
};
@@ -83,18 +84,18 @@ describe('public invitations routes', () => {
});
describe('POST /invitations/:token/join', () => {
it('registers a new attendee successfully', async () => {
it('creates a pending pre-registration instead of an attendee', async () => {
prisma.group.findUnique.mockResolvedValue(mockGroup as never);
prisma.attendee.findFirst.mockResolvedValue(null);
prisma.attendee.create.mockResolvedValue({
id: 'att-1',
prisma.groupPreRegistration.findFirst.mockResolvedValue(null);
prisma.groupPreRegistration.create.mockResolvedValue({
id: 'pre-1',
groupId: 'group-1',
fullName: 'Lucía Méndez',
email: 'lucia@test.com',
phone: '+5491122334455',
guardianName: null,
guardianPhone: null,
notes: null,
status: 'PENDING',
reviewedAt: null,
createdAt: new Date('2026-09-18T10:00:00.000Z'),
updatedAt: new Date('2026-09-18T10:00:00.000Z'),
} as never);
@@ -112,9 +113,11 @@ describe('public invitations routes', () => {
expect(res.status).toBe(201);
const body = await res.json();
expect(body.message).toBe('Inscripción realizada con éxito');
expect(body.attendee.fullName).toBe('Lucía Méndez');
expect(prisma.attendee.create).toHaveBeenCalledWith({
expect(body.status).toBe('pre_registered');
expect(body.attendee).toBeNull();
expect(body.message).toContain('solicitud de inscripción');
expect(prisma.attendee.create).not.toHaveBeenCalled();
expect(prisma.groupPreRegistration.create).toHaveBeenCalledWith({
data: {
groupId: 'group-1',
fullName: 'Lucía Méndez',
@@ -141,7 +144,7 @@ describe('public invitations routes', () => {
expect(res.status).toBe(409);
const body = await res.json();
expect(body.code).toBe('attendee_already_registered');
expect(prisma.attendee.create).not.toHaveBeenCalled();
expect(prisma.groupPreRegistration.create).not.toHaveBeenCalled();
});
it('rejects invalid inputs (missing required name/phone)', async () => {
@@ -157,21 +160,67 @@ describe('public invitations routes', () => {
expect(res.status).toBe(400);
});
it('adds to the waitlist when the group is full', async () => {
it('returns 409 when a pre-registration is already pending', async () => {
prisma.group.findUnique.mockResolvedValue(mockGroup as never);
prisma.attendee.findFirst.mockResolvedValue(null);
prisma.attendee.count.mockResolvedValue(15); // capacity full
prisma.groupWaitlistEntry.findFirst.mockResolvedValue(null);
prisma.groupWaitlistEntry.create.mockResolvedValue({
id: 'wl-1',
prisma.groupPreRegistration.findFirst.mockResolvedValue({
id: 'pre-1',
status: 'PENDING',
} as never);
const res = await app.request('/invitations/valid-token-123/join', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
firstName: 'Lucía',
lastName: 'Méndez',
phone: '+54 9 11 2233-4455',
}),
});
expect(res.status).toBe(409);
const body = await res.json();
expect(body.code).toBe('pre_registration_already_pending');
expect(prisma.groupPreRegistration.create).not.toHaveBeenCalled();
expect(prisma.groupPreRegistration.update).not.toHaveBeenCalled();
});
it('returns 409 when the pre-registration was already approved', async () => {
prisma.group.findUnique.mockResolvedValue(mockGroup as never);
prisma.attendee.findFirst.mockResolvedValue(null);
prisma.groupPreRegistration.findFirst.mockResolvedValue({
id: 'pre-1',
status: 'APPROVED',
} as never);
const res = await app.request('/invitations/valid-token-123/join', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
firstName: 'Lucía',
lastName: 'Méndez',
phone: '+54 9 11 2233-4455',
}),
});
expect(res.status).toBe(409);
const body = await res.json();
expect(body.code).toBe('pre_registration_already_approved');
expect(prisma.groupPreRegistration.create).not.toHaveBeenCalled();
});
it('reactivates a rejected pre-registration back to pending', async () => {
prisma.group.findUnique.mockResolvedValue(mockGroup as never);
prisma.attendee.findFirst.mockResolvedValue(null);
prisma.groupPreRegistration.findFirst.mockResolvedValue({
id: 'pre-1',
groupId: 'group-1',
fullName: 'Lucía Méndez',
phone: '+5491122334455',
email: 'lucia@test.com',
notes: null,
status: 'PENDING',
status: 'REJECTED',
reviewedAt: new Date('2026-09-19T10:00:00.000Z'),
createdAt: new Date('2026-09-18T10:00:00.000Z'),
updatedAt: new Date('2026-09-18T10:00:00.000Z'),
updatedAt: new Date('2026-09-19T10:00:00.000Z'),
} as never);
const res = await app.request('/invitations/valid-token-123/join', {
@@ -187,15 +236,15 @@ describe('public invitations routes', () => {
expect(res.status).toBe(201);
const body = await res.json();
expect(body.status).toBe('waitlisted');
expect(body.attendee).toBeNull();
expect(prisma.attendee.create).not.toHaveBeenCalled();
expect(prisma.groupWaitlistEntry.create).toHaveBeenCalledWith({
expect(body.status).toBe('pre_registered');
expect(prisma.groupPreRegistration.create).not.toHaveBeenCalled();
expect(prisma.groupPreRegistration.update).toHaveBeenCalledWith({
where: { id: 'pre-1' },
data: {
groupId: 'group-1',
status: 'PENDING',
fullName: 'Lucía Méndez',
phone: '+5491122334455',
email: 'lucia@test.com',
reviewedAt: null,
},
});
});

View File

@@ -0,0 +1,365 @@
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(),
},
groupWaitlistEntry: {
findFirst: mock(),
create: mock(),
},
groupPreRegistration: {
findMany: mock(),
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 mockGroup = {
id: 'group-1',
name: 'Taller de Pintura',
description: null,
createdById: teacherId,
inviteToken: 'valid-token-123',
createdAt: new Date('2026-09-01T10:00:00.000Z'),
updatedAt: new Date('2026-09-01T10:00:00.000Z'),
days: ['MONDAY'],
time: '18:00',
capacity: 20,
price: 1500,
billingType: 'MONTHLY',
dueDay: 10,
members: [],
};
const preRegistration = (overrides: Record<string, unknown> = {}) => ({
id: 'pre-1',
groupId: 'group-1',
fullName: 'Martín Gómez',
phone: '+5491133445566',
email: null,
status: 'PENDING',
reviewedAt: null,
createdAt: new Date('2026-09-10T10:00:00.000Z'),
updatedAt: new Date('2026-09-10T10:00:00.000Z'),
...overrides,
});
const attendee = (overrides: Record<string, unknown> = {}) => ({
id: 'att-1',
groupId: 'group-1',
fullName: 'Martín Gómez',
phone: '+5491133445566',
email: null,
guardianName: null,
guardianPhone: null,
notes: null,
notifyToken: 'notify-1',
createdAt: new Date('2026-09-18T10:00:00.000Z'),
updatedAt: new Date('2026-09-18T10:00:00.000Z'),
...overrides,
});
const waitlistEntry = (overrides: Record<string, unknown> = {}) => ({
id: 'wl-1',
groupId: 'group-1',
fullName: 'Martín Gómez',
phone: '+5491133445566',
email: null,
notes: null,
status: 'PENDING',
createdAt: new Date('2026-09-10T10:00:00.000Z'),
updatedAt: new Date('2026-09-10T10:00:00.000Z'),
...overrides,
});
function makeApp(userValue: unknown) {
const app = new Hono();
app.use('*', async (c, next) => {
c.set('user', userValue as never);
await next();
});
app.route('/groups', groupsRoutes);
return app;
}
describe('group pre-registrations', () => {
beforeEach(() => {
vi.clearAllMocks();
});
describe('GET /groups/:groupId/preregistrations', () => {
it('lists pending pre-registrations in FIFO order with pagination', async () => {
prisma.group.findUnique.mockResolvedValue(mockGroup as never);
prisma.groupPreRegistration.findMany.mockResolvedValue([
preRegistration({ id: 'pre-1', fullName: 'Ana García', createdAt: new Date('2026-09-08T10:00:00.000Z') }),
preRegistration({ id: 'pre-2', fullName: 'Pedro López', createdAt: new Date('2026-09-09T10:00:00.000Z') }),
] as never);
prisma.groupPreRegistration.count.mockResolvedValue(2);
const res = await makeApp({ id: teacherId }).request('/groups/group-1/preregistrations');
expect(res.status).toBe(200);
const data = await res.json();
expect(data.data).toHaveLength(2);
expect(data.data[0].fullName).toBe('Ana García');
expect(data.data[0].status).toBe('PENDING');
expect(data.pagination).toEqual({ page: 1, pageSize: 10, total: 2, totalPages: 1 });
expect(prisma.groupPreRegistration.findMany).toHaveBeenCalledWith({
where: { groupId: 'group-1', status: 'PENDING' },
skip: 0,
take: 10,
orderBy: { createdAt: 'asc' },
});
});
it('filters by status when provided', async () => {
prisma.group.findUnique.mockResolvedValue(mockGroup as never);
prisma.groupPreRegistration.findMany.mockResolvedValue([] as never);
prisma.groupPreRegistration.count.mockResolvedValue(0);
const res = await makeApp({ id: teacherId }).request(
'/groups/group-1/preregistrations?status=REJECTED',
);
expect(res.status).toBe(200);
expect(prisma.groupPreRegistration.findMany).toHaveBeenCalledWith({
where: { groupId: 'group-1', status: 'REJECTED' },
skip: 0,
take: 10,
orderBy: { createdAt: 'asc' },
});
});
it('rejects user without access to the group', async () => {
prisma.group.findUnique.mockResolvedValue(mockGroup as never);
const res = await makeApp({ id: 'other-user' }).request('/groups/group-1/preregistrations');
expect(res.status).toBe(403);
expect(prisma.groupPreRegistration.findMany).not.toHaveBeenCalled();
});
});
describe('POST /groups/:groupId/preregistrations/:entryId/approve', () => {
it('adds the person to the group when there is free capacity', async () => {
prisma.group.findUnique.mockResolvedValue(mockGroup as never);
prisma.groupPreRegistration.findFirst.mockResolvedValue(preRegistration() as never);
prisma.attendee.count.mockResolvedValue(5);
prisma.attendee.findFirst.mockResolvedValue(null);
prisma.attendee.create.mockResolvedValue(attendee() as never);
prisma.groupPreRegistration.update.mockResolvedValue(
preRegistration({ status: 'APPROVED', reviewedAt: new Date('2026-09-20T10:00:00.000Z') }) as never,
);
const res = await makeApp({ id: teacherId }).request(
'/groups/group-1/preregistrations/pre-1/approve',
{ method: 'POST' },
);
expect(res.status).toBe(200);
const body = await res.json();
expect(body.outcome).toBe('added');
expect(body.attendee.fullName).toBe('Martín Gómez');
expect(body.waitlistEntry).toBeNull();
expect(prisma.attendee.create).toHaveBeenCalledWith({
data: {
groupId: 'group-1',
fullName: 'Martín Gómez',
phone: '+5491133445566',
email: null,
notes: null,
},
});
expect(prisma.groupPreRegistration.update).toHaveBeenCalledWith({
where: { id: 'pre-1' },
data: { status: 'APPROVED', reviewedAt: expect.any(Date) },
});
});
it('moves the person to the waitlist when the group is full', async () => {
prisma.group.findUnique.mockResolvedValue(mockGroup as never);
prisma.groupPreRegistration.findFirst.mockResolvedValue(preRegistration() as never);
prisma.attendee.count.mockResolvedValue(20); // capacity full
prisma.groupWaitlistEntry.findFirst.mockResolvedValue(null);
prisma.groupWaitlistEntry.create.mockResolvedValue(waitlistEntry() as never);
prisma.groupPreRegistration.update.mockResolvedValue(
preRegistration({ status: 'APPROVED', reviewedAt: new Date('2026-09-20T10:00:00.000Z') }) as never,
);
const res = await makeApp({ id: teacherId }).request(
'/groups/group-1/preregistrations/pre-1/approve',
{ method: 'POST' },
);
expect(res.status).toBe(200);
const body = await res.json();
expect(body.outcome).toBe('waitlisted');
expect(body.attendee).toBeNull();
expect(body.waitlistEntry.fullName).toBe('Martín Gómez');
expect(prisma.attendee.create).not.toHaveBeenCalled();
expect(prisma.groupWaitlistEntry.create).toHaveBeenCalledWith({
data: {
groupId: 'group-1',
fullName: 'Martín Gómez',
phone: '+5491133445566',
email: null,
},
});
expect(prisma.groupPreRegistration.update).toHaveBeenCalledWith({
where: { id: 'pre-1' },
data: { status: 'APPROVED', reviewedAt: expect.any(Date) },
});
});
it('reuses an existing waitlist entry instead of duplicating it', async () => {
prisma.group.findUnique.mockResolvedValue(mockGroup as never);
prisma.groupPreRegistration.findFirst.mockResolvedValue(preRegistration() as never);
prisma.attendee.count.mockResolvedValue(20);
prisma.groupWaitlistEntry.findFirst.mockResolvedValue(waitlistEntry() as never);
prisma.groupPreRegistration.update.mockResolvedValue(
preRegistration({ status: 'APPROVED', reviewedAt: new Date('2026-09-20T10:00:00.000Z') }) as never,
);
const res = await makeApp({ id: teacherId }).request(
'/groups/group-1/preregistrations/pre-1/approve',
{ method: 'POST' },
);
expect(res.status).toBe(200);
expect(prisma.groupWaitlistEntry.create).not.toHaveBeenCalled();
});
it('returns 409 when the phone already belongs to a member', async () => {
prisma.group.findUnique.mockResolvedValue(mockGroup as never);
prisma.groupPreRegistration.findFirst.mockResolvedValue(preRegistration() as never);
prisma.attendee.count.mockResolvedValue(5);
prisma.attendee.findFirst.mockResolvedValue(attendee() as never);
const res = await makeApp({ id: teacherId }).request(
'/groups/group-1/preregistrations/pre-1/approve',
{ method: 'POST' },
);
expect(res.status).toBe(409);
const body = await res.json();
expect(body.code).toBe('attendee_already_registered');
expect(prisma.attendee.create).not.toHaveBeenCalled();
expect(prisma.groupPreRegistration.update).not.toHaveBeenCalled();
});
it('returns 409 when the pre-registration was already reviewed', async () => {
prisma.group.findUnique.mockResolvedValue(mockGroup as never);
prisma.groupPreRegistration.findFirst.mockResolvedValue(
preRegistration({ status: 'APPROVED' }) as never,
);
const res = await makeApp({ id: teacherId }).request(
'/groups/group-1/preregistrations/pre-1/approve',
{ method: 'POST' },
);
expect(res.status).toBe(409);
const body = await res.json();
expect(body.code).toBe('pre_registration_already_reviewed');
expect(prisma.attendee.create).not.toHaveBeenCalled();
});
it('returns 404 when the pre-registration does not exist', async () => {
prisma.group.findUnique.mockResolvedValue(mockGroup as never);
prisma.groupPreRegistration.findFirst.mockResolvedValue(null);
const res = await makeApp({ id: teacherId }).request(
'/groups/group-1/preregistrations/missing/approve',
{ method: 'POST' },
);
expect(res.status).toBe(404);
});
it('rejects approval from a user who is not the owner', async () => {
prisma.group.findUnique.mockResolvedValue(mockGroup as never);
const res = await makeApp({ id: 'other-user' }).request(
'/groups/group-1/preregistrations/pre-1/approve',
{ method: 'POST' },
);
expect(res.status).toBe(403);
expect(prisma.groupPreRegistration.findFirst).not.toHaveBeenCalled();
});
});
describe('POST /groups/:groupId/preregistrations/:entryId/reject', () => {
it('marks the pre-registration as rejected', async () => {
prisma.group.findUnique.mockResolvedValue(mockGroup as never);
prisma.groupPreRegistration.findFirst.mockResolvedValue(preRegistration() as never);
prisma.groupPreRegistration.update.mockResolvedValue(
preRegistration({ status: 'REJECTED', reviewedAt: new Date('2026-09-20T10:00:00.000Z') }) as never,
);
const res = await makeApp({ id: teacherId }).request(
'/groups/group-1/preregistrations/pre-1/reject',
{ method: 'POST' },
);
expect(res.status).toBe(200);
const body = await res.json();
expect(body.entry.status).toBe('REJECTED');
expect(prisma.groupPreRegistration.update).toHaveBeenCalledWith({
where: { id: 'pre-1' },
data: { status: 'REJECTED', reviewedAt: expect.any(Date) },
});
});
it('returns 409 when the pre-registration was already reviewed', async () => {
prisma.group.findUnique.mockResolvedValue(mockGroup as never);
prisma.groupPreRegistration.findFirst.mockResolvedValue(
preRegistration({ status: 'REJECTED' }) as never,
);
const res = await makeApp({ id: teacherId }).request(
'/groups/group-1/preregistrations/pre-1/reject',
{ method: 'POST' },
);
expect(res.status).toBe(409);
const body = await res.json();
expect(body.code).toBe('pre_registration_already_reviewed');
expect(prisma.groupPreRegistration.update).not.toHaveBeenCalled();
});
it('rejects rejection from a user who is not the owner', async () => {
prisma.group.findUnique.mockResolvedValue(mockGroup as never);
const res = await makeApp({ id: 'other-user' }).request(
'/groups/group-1/preregistrations/pre-1/reject',
{ method: 'POST' },
);
expect(res.status).toBe(403);
expect(prisma.groupPreRegistration.findFirst).not.toHaveBeenCalled();
});
});
});