- 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`.
253 lines
8.0 KiB
TypeScript
253 lines
8.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(),
|
|
create: mock(),
|
|
count: mock(),
|
|
},
|
|
groupPreRegistration: {
|
|
findFirst: mock(),
|
|
create: mock(),
|
|
update: mock(),
|
|
},
|
|
};
|
|
|
|
mock.module('@/lib/prisma', () => ({
|
|
default: db,
|
|
getPrismaClient: mock(),
|
|
UnitOfWork: class {},
|
|
}));
|
|
|
|
import prisma from '@/lib/prisma';
|
|
import { invitationsRoutes } from '@/modules/invitations';
|
|
|
|
const app = new Hono();
|
|
app.route('/invitations', invitationsRoutes);
|
|
|
|
const mockGroup = {
|
|
id: 'group-1',
|
|
name: 'Taller de Cerámica',
|
|
description: 'Clases de cerámica los sábados',
|
|
days: ['SATURDAY'],
|
|
time: '10:00',
|
|
capacity: 15,
|
|
price: 500,
|
|
billingType: 'MONTHLY',
|
|
createdById: 'teacher-1',
|
|
owner: { name: 'Prof. Mario Rossi' },
|
|
};
|
|
|
|
describe('public invitations routes', () => {
|
|
beforeEach(() => {
|
|
vi.clearAllMocks();
|
|
});
|
|
|
|
describe('GET /invitations/:token', () => {
|
|
it('returns group details for a valid invite token', async () => {
|
|
prisma.group.findUnique.mockResolvedValue(mockGroup as never);
|
|
|
|
const res = await app.request('/invitations/valid-token-123');
|
|
|
|
expect(res.status).toBe(200);
|
|
const data = await res.json();
|
|
expect(data).toEqual({
|
|
id: 'group-1',
|
|
name: 'Taller de Cerámica',
|
|
description: 'Clases de cerámica los sábados',
|
|
teacherName: 'Prof. Mario Rossi',
|
|
days: ['SATURDAY'],
|
|
time: '10:00',
|
|
capacity: 15,
|
|
price: 500,
|
|
billingType: 'MONTHLY',
|
|
});
|
|
expect(prisma.group.findUnique).toHaveBeenCalledWith({
|
|
where: { inviteToken: 'valid-token-123' },
|
|
include: { owner: { select: { name: true } } },
|
|
});
|
|
});
|
|
|
|
it('returns 404 when the invite token is invalid', async () => {
|
|
prisma.group.findUnique.mockResolvedValue(null);
|
|
|
|
const res = await app.request('/invitations/invalid-token');
|
|
|
|
expect(res.status).toBe(404);
|
|
const body = await res.json();
|
|
expect(body.title).toBe('Not Found');
|
|
});
|
|
});
|
|
|
|
describe('POST /invitations/:token/join', () => {
|
|
it('creates a pending pre-registration instead of an attendee', async () => {
|
|
prisma.group.findUnique.mockResolvedValue(mockGroup as never);
|
|
prisma.attendee.findFirst.mockResolvedValue(null);
|
|
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',
|
|
status: 'PENDING',
|
|
reviewedAt: null,
|
|
createdAt: new Date('2026-09-18T10:00:00.000Z'),
|
|
updatedAt: new Date('2026-09-18T10:00:00.000Z'),
|
|
} 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',
|
|
email: 'lucia@test.com',
|
|
}),
|
|
});
|
|
|
|
expect(res.status).toBe(201);
|
|
const body = await res.json();
|
|
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',
|
|
phone: '+5491122334455',
|
|
email: 'lucia@test.com',
|
|
},
|
|
});
|
|
});
|
|
|
|
it('rejects registration when phone already exists in the group', async () => {
|
|
prisma.group.findUnique.mockResolvedValue(mockGroup as never);
|
|
prisma.attendee.findFirst.mockResolvedValue({ id: 'existing-att' } 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: '1122334455',
|
|
}),
|
|
});
|
|
|
|
expect(res.status).toBe(409);
|
|
const body = await res.json();
|
|
expect(body.code).toBe('attendee_already_registered');
|
|
expect(prisma.groupPreRegistration.create).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it('rejects invalid inputs (missing required name/phone)', async () => {
|
|
const res = await app.request('/invitations/valid-token-123/join', {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({
|
|
firstName: '',
|
|
phone: '12',
|
|
}),
|
|
});
|
|
|
|
expect(res.status).toBe(400);
|
|
});
|
|
|
|
it('returns 409 when a pre-registration is already pending', async () => {
|
|
prisma.group.findUnique.mockResolvedValue(mockGroup as never);
|
|
prisma.attendee.findFirst.mockResolvedValue(null);
|
|
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',
|
|
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-19T10:00:00.000Z'),
|
|
} 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',
|
|
email: 'lucia@test.com',
|
|
}),
|
|
});
|
|
|
|
expect(res.status).toBe(201);
|
|
const body = await res.json();
|
|
expect(body.status).toBe('pre_registered');
|
|
expect(prisma.groupPreRegistration.create).not.toHaveBeenCalled();
|
|
expect(prisma.groupPreRegistration.update).toHaveBeenCalledWith({
|
|
where: { id: 'pre-1' },
|
|
data: {
|
|
status: 'PENDING',
|
|
fullName: 'Lucía Méndez',
|
|
email: 'lucia@test.com',
|
|
reviewedAt: null,
|
|
},
|
|
});
|
|
});
|
|
});
|
|
});
|