- 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`.
116 lines
3.2 KiB
TypeScript
116 lines
3.2 KiB
TypeScript
import type { PrismaClient } from '@generated/prisma/client';
|
|
import type { JoinGroupViaInvite, ProblemDetails, Result } from '@gruperly/shared';
|
|
import { err, ok } from '@gruperly/shared';
|
|
import { conflictProblem, notFoundResourceProblem } from '@/http/problem-builders';
|
|
import prisma from '@/lib/prisma';
|
|
import { normalizePhone } from '@/modules/attendees/lib/helpers';
|
|
|
|
type JoinViaInviteDeps = {
|
|
db?: Pick<PrismaClient, 'group' | 'attendee' | 'groupPreRegistration'>;
|
|
};
|
|
|
|
export type JoinViaInviteResult = {
|
|
status: 'pre_registered';
|
|
message: string;
|
|
attendee: null;
|
|
};
|
|
|
|
export class JoinViaInvite {
|
|
constructor(private readonly deps: JoinViaInviteDeps = {}) {}
|
|
|
|
async execute(
|
|
token: string,
|
|
payload: JoinGroupViaInvite,
|
|
): Promise<Result<JoinViaInviteResult, ProblemDetails>> {
|
|
const db = this.deps.db ?? prisma;
|
|
|
|
const group = await db.group.findUnique({
|
|
where: { inviteToken: token },
|
|
});
|
|
|
|
if (!group) {
|
|
return err(notFoundResourceProblem('Invitación', token));
|
|
}
|
|
|
|
const phone = normalizePhone(payload.phone);
|
|
const existing = await db.attendee.findFirst({
|
|
where: {
|
|
groupId: group.id,
|
|
OR: [
|
|
{ phone },
|
|
{ phone: payload.phone.trim() },
|
|
],
|
|
},
|
|
});
|
|
|
|
if (existing) {
|
|
return err(
|
|
conflictProblem({
|
|
detail: 'Ya existe un alumno registrado con este número de teléfono en este grupo.',
|
|
code: 'attendee_already_registered',
|
|
}),
|
|
);
|
|
}
|
|
|
|
const fullName = `${payload.firstName.trim()} ${payload.lastName.trim()}`.trim();
|
|
const email = payload.email?.trim() || null;
|
|
|
|
const existingPreRegistration = await db.groupPreRegistration.findFirst({
|
|
where: {
|
|
groupId: group.id,
|
|
OR: [
|
|
{ phone },
|
|
{ phone: payload.phone.trim() },
|
|
],
|
|
},
|
|
});
|
|
|
|
if (existingPreRegistration?.status === 'REJECTED') {
|
|
await db.groupPreRegistration.update({
|
|
where: { id: existingPreRegistration.id },
|
|
data: { status: 'PENDING', fullName, email, reviewedAt: null },
|
|
});
|
|
|
|
return ok({
|
|
status: 'pre_registered',
|
|
attendee: null,
|
|
message:
|
|
'Recibimos tu solicitud de inscripción. El profesor revisará tus datos y te confirmará el lugar.',
|
|
});
|
|
}
|
|
|
|
if (existingPreRegistration) {
|
|
const message =
|
|
existingPreRegistration.status === 'APPROVED'
|
|
? 'Tu inscripción para este grupo ya fue aprobada.'
|
|
: 'Ya enviaste una solicitud de inscripción para este grupo. Está pendiente de revisión por el profesor.';
|
|
|
|
return err(
|
|
conflictProblem({
|
|
detail: message,
|
|
code:
|
|
existingPreRegistration.status === 'APPROVED'
|
|
? 'pre_registration_already_approved'
|
|
: 'pre_registration_already_pending',
|
|
}),
|
|
);
|
|
}
|
|
|
|
await db.groupPreRegistration.create({
|
|
data: {
|
|
groupId: group.id,
|
|
fullName,
|
|
phone,
|
|
email,
|
|
},
|
|
});
|
|
|
|
return ok({
|
|
status: 'pre_registered',
|
|
attendee: null,
|
|
message:
|
|
'Recibimos tu solicitud de inscripción. El profesor revisará tus datos y te confirmará el lugar.',
|
|
});
|
|
}
|
|
}
|