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:
@@ -0,0 +1,19 @@
|
||||
import { Hono } from 'hono';
|
||||
import { problemJson, resultJson, unauthorizedProblem } from '@/http/problem-details';
|
||||
import { ApproveGroupPreRegistration } from './use-case';
|
||||
|
||||
const route = new Hono();
|
||||
|
||||
route.post('/:groupId/preregistrations/:entryId/approve', async (c) => {
|
||||
const user = c.get('user');
|
||||
if (!user) {
|
||||
return problemJson(c, unauthorizedProblem(c.req.path));
|
||||
}
|
||||
const groupId = c.req.param('groupId');
|
||||
const entryId = c.req.param('entryId');
|
||||
const useCase = new ApproveGroupPreRegistration();
|
||||
const result = await useCase.execute(groupId, entryId, user.id);
|
||||
return resultJson(c, result);
|
||||
});
|
||||
|
||||
export default route;
|
||||
@@ -0,0 +1,117 @@
|
||||
import type { PrismaClient } from '@generated/prisma/client';
|
||||
import type {
|
||||
ApproveGroupPreRegistrationResult,
|
||||
ProblemDetails,
|
||||
Result,
|
||||
} from '@gruperly/shared';
|
||||
import { err, ok } from '@gruperly/shared';
|
||||
import { conflictProblem, notFoundResourceProblem } from '@/http/problem-builders';
|
||||
import { default as prisma, UnitOfWork } from '@/lib/prisma';
|
||||
import {
|
||||
findGroupOwnedByUser,
|
||||
type GroupWaitlistEntryRecord,
|
||||
toGroupWaitlistEntryDto,
|
||||
} from '../../../group-waitlist/lib/helpers';
|
||||
import { promoteWaitlistEntryRecord } from '../../../group-waitlist/lib/promote';
|
||||
import { type GroupPreRegistrationRecord } from '../../lib/helpers';
|
||||
|
||||
type ApproveGroupPreRegistrationDeps = {
|
||||
db?: Pick<PrismaClient, 'group' | 'groupPreRegistration'>;
|
||||
unitOfWork?: UnitOfWork;
|
||||
};
|
||||
|
||||
export class ApproveGroupPreRegistration {
|
||||
constructor(private readonly deps: ApproveGroupPreRegistrationDeps = {}) {}
|
||||
|
||||
async execute(
|
||||
groupId: string,
|
||||
entryId: string,
|
||||
userId: string,
|
||||
): Promise<Result<ApproveGroupPreRegistrationResult, ProblemDetails>> {
|
||||
const db = this.deps.db ?? prisma;
|
||||
const unitOfWork = this.deps.unitOfWork ?? new UnitOfWork(prisma);
|
||||
|
||||
const groupResult = await findGroupOwnedByUser(db, groupId, userId);
|
||||
if (!groupResult.ok) {
|
||||
return groupResult;
|
||||
}
|
||||
|
||||
const entry = await db.groupPreRegistration.findFirst({
|
||||
where: { id: entryId, groupId },
|
||||
});
|
||||
if (!entry) {
|
||||
return err(notFoundResourceProblem('GroupPreRegistration', entryId));
|
||||
}
|
||||
if (entry.status !== 'PENDING') {
|
||||
return err(
|
||||
conflictProblem({
|
||||
detail: 'Esta pre-inscripción ya fue revisada.',
|
||||
code: 'pre_registration_already_reviewed',
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
const record = entry as unknown as GroupPreRegistrationRecord;
|
||||
|
||||
return unitOfWork.executeResult(
|
||||
async (tx): Promise<Result<ApproveGroupPreRegistrationResult, ProblemDetails>> => {
|
||||
const promoteResult = await promoteWaitlistEntryRecord(
|
||||
tx,
|
||||
groupResult.value,
|
||||
{
|
||||
id: record.id,
|
||||
groupId: record.groupId,
|
||||
fullName: record.fullName,
|
||||
phone: record.phone,
|
||||
email: record.email,
|
||||
notes: null,
|
||||
status: 'PENDING',
|
||||
createdAt: record.createdAt,
|
||||
updatedAt: record.updatedAt,
|
||||
},
|
||||
);
|
||||
|
||||
if (promoteResult.ok) {
|
||||
await tx.groupPreRegistration.update({
|
||||
where: { id: record.id },
|
||||
data: { status: 'APPROVED', reviewedAt: new Date() },
|
||||
});
|
||||
return ok({
|
||||
outcome: 'added',
|
||||
attendee: promoteResult.value,
|
||||
waitlistEntry: null,
|
||||
});
|
||||
}
|
||||
|
||||
if (promoteResult.error.code !== 'group_capacity_reached') {
|
||||
return promoteResult;
|
||||
}
|
||||
|
||||
const existingWaitlistEntry = await tx.groupWaitlistEntry.findFirst({
|
||||
where: { groupId: record.groupId, phone: record.phone },
|
||||
});
|
||||
const waitlistEntryRecord = existingWaitlistEntry
|
||||
? (existingWaitlistEntry as unknown as GroupWaitlistEntryRecord)
|
||||
: ((await tx.groupWaitlistEntry.create({
|
||||
data: {
|
||||
groupId: record.groupId,
|
||||
fullName: record.fullName,
|
||||
phone: record.phone,
|
||||
email: record.email,
|
||||
},
|
||||
})) as unknown as GroupWaitlistEntryRecord);
|
||||
|
||||
await tx.groupPreRegistration.update({
|
||||
where: { id: record.id },
|
||||
data: { status: 'APPROVED', reviewedAt: new Date() },
|
||||
});
|
||||
|
||||
return ok({
|
||||
outcome: 'waitlisted',
|
||||
attendee: null,
|
||||
waitlistEntry: toGroupWaitlistEntryDto(waitlistEntryRecord),
|
||||
});
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
import {
|
||||
type GroupPreRegistrationQuery,
|
||||
GroupPreRegistrationQuerySchema,
|
||||
} from '@gruperly/shared';
|
||||
import { Hono } from 'hono';
|
||||
import { problemJson, resultJson, unauthorizedProblem } from '@/http/problem-details';
|
||||
import { validate } from '@/http/validate';
|
||||
import { ListGroupPreRegistrations } from './use-case';
|
||||
|
||||
const route = new Hono();
|
||||
|
||||
route.get('/:groupId/preregistrations', validate.query(GroupPreRegistrationQuerySchema), async (c) => {
|
||||
const user = c.get('user');
|
||||
if (!user) {
|
||||
return problemJson(c, unauthorizedProblem(c.req.path));
|
||||
}
|
||||
const groupId = c.req.param('groupId');
|
||||
const query = c.req.valid('query') as GroupPreRegistrationQuery;
|
||||
const useCase = new ListGroupPreRegistrations();
|
||||
const result = await useCase.execute(groupId, user.id, query);
|
||||
return resultJson(c, result);
|
||||
});
|
||||
|
||||
export default route;
|
||||
@@ -0,0 +1,54 @@
|
||||
import type { PrismaClient } from '@generated/prisma/client';
|
||||
import type {
|
||||
GroupPreRegistrationList,
|
||||
GroupPreRegistrationQuery,
|
||||
ProblemDetails,
|
||||
Result,
|
||||
} from '@gruperly/shared';
|
||||
import { ok } from '@gruperly/shared';
|
||||
import { getPaginationMetadata, getPaginationOffset } from '@/lib/pagination';
|
||||
import prisma from '@/lib/prisma';
|
||||
import { findGroupForUser } from '../../../group-waitlist/lib/helpers';
|
||||
import {
|
||||
type GroupPreRegistrationRecord,
|
||||
toGroupPreRegistrationDto,
|
||||
} from '../../lib/helpers';
|
||||
|
||||
type ListGroupPreRegistrationsDeps = {
|
||||
db?: Pick<PrismaClient, 'group' | 'groupPreRegistration'>;
|
||||
};
|
||||
|
||||
export class ListGroupPreRegistrations {
|
||||
constructor(private readonly deps: ListGroupPreRegistrationsDeps = {}) {}
|
||||
|
||||
async execute(
|
||||
groupId: string,
|
||||
userId: string,
|
||||
query: GroupPreRegistrationQuery,
|
||||
): Promise<Result<GroupPreRegistrationList, ProblemDetails>> {
|
||||
const db = this.deps.db ?? prisma;
|
||||
|
||||
const groupResult = await findGroupForUser(db, groupId, userId);
|
||||
if (!groupResult.ok) {
|
||||
return groupResult;
|
||||
}
|
||||
|
||||
const where = { groupId, status: query.status ?? ('PENDING' as const) };
|
||||
const [records, total] = await Promise.all([
|
||||
db.groupPreRegistration.findMany({
|
||||
where,
|
||||
skip: getPaginationOffset(query),
|
||||
take: query.pageSize,
|
||||
orderBy: { createdAt: 'asc' },
|
||||
}),
|
||||
db.groupPreRegistration.count({ where }),
|
||||
]);
|
||||
|
||||
return ok({
|
||||
data: records.map(
|
||||
(record) => toGroupPreRegistrationDto(record as unknown as GroupPreRegistrationRecord),
|
||||
),
|
||||
pagination: getPaginationMetadata(query, total),
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
import { Hono } from 'hono';
|
||||
import { problemJson, resultJson, unauthorizedProblem } from '@/http/problem-details';
|
||||
import { RejectGroupPreRegistration } from './use-case';
|
||||
|
||||
const route = new Hono();
|
||||
|
||||
route.post('/:groupId/preregistrations/:entryId/reject', async (c) => {
|
||||
const user = c.get('user');
|
||||
if (!user) {
|
||||
return problemJson(c, unauthorizedProblem(c.req.path));
|
||||
}
|
||||
const groupId = c.req.param('groupId');
|
||||
const entryId = c.req.param('entryId');
|
||||
const useCase = new RejectGroupPreRegistration();
|
||||
const result = await useCase.execute(groupId, entryId, user.id);
|
||||
return resultJson(c, result);
|
||||
});
|
||||
|
||||
export default route;
|
||||
@@ -0,0 +1,56 @@
|
||||
import type { PrismaClient } from '@generated/prisma/client';
|
||||
import type {
|
||||
ProblemDetails,
|
||||
RejectGroupPreRegistrationResult,
|
||||
Result,
|
||||
} from '@gruperly/shared';
|
||||
import { err, ok } from '@gruperly/shared';
|
||||
import { conflictProblem, notFoundResourceProblem } from '@/http/problem-builders';
|
||||
import prisma from '@/lib/prisma';
|
||||
import { findGroupOwnedByUser } from '../../../group-waitlist/lib/helpers';
|
||||
import { type GroupPreRegistrationRecord, toGroupPreRegistrationDto } from '../../lib/helpers';
|
||||
|
||||
type RejectGroupPreRegistrationDeps = {
|
||||
db?: Pick<PrismaClient, 'group' | 'groupPreRegistration'>;
|
||||
};
|
||||
|
||||
export class RejectGroupPreRegistration {
|
||||
constructor(private readonly deps: RejectGroupPreRegistrationDeps = {}) {}
|
||||
|
||||
async execute(
|
||||
groupId: string,
|
||||
entryId: string,
|
||||
userId: string,
|
||||
): Promise<Result<RejectGroupPreRegistrationResult, ProblemDetails>> {
|
||||
const db = this.deps.db ?? prisma;
|
||||
|
||||
const groupResult = await findGroupOwnedByUser(db, groupId, userId);
|
||||
if (!groupResult.ok) {
|
||||
return groupResult;
|
||||
}
|
||||
|
||||
const entry = await db.groupPreRegistration.findFirst({
|
||||
where: { id: entryId, groupId },
|
||||
});
|
||||
if (!entry) {
|
||||
return err(notFoundResourceProblem('GroupPreRegistration', entryId));
|
||||
}
|
||||
if (entry.status !== 'PENDING') {
|
||||
return err(
|
||||
conflictProblem({
|
||||
detail: 'Esta pre-inscripción ya fue revisada.',
|
||||
code: 'pre_registration_already_reviewed',
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
const updated = await db.groupPreRegistration.update({
|
||||
where: { id: entry.id },
|
||||
data: { status: 'REJECTED', reviewedAt: new Date() },
|
||||
});
|
||||
|
||||
return ok({
|
||||
entry: toGroupPreRegistrationDto(updated as unknown as GroupPreRegistrationRecord),
|
||||
});
|
||||
}
|
||||
}
|
||||
1
apps/backend/src/modules/group-preregistrations/index.ts
Normal file
1
apps/backend/src/modules/group-preregistrations/index.ts
Normal file
@@ -0,0 +1 @@
|
||||
export { default as groupPreRegistrationRoutes } from './routes';
|
||||
@@ -0,0 +1,29 @@
|
||||
import type { GroupPreRegistrationDto } from '@gruperly/shared';
|
||||
|
||||
export type GroupPreRegistrationRecord = {
|
||||
id: string;
|
||||
groupId: string;
|
||||
fullName: string;
|
||||
phone: string;
|
||||
email: string | null;
|
||||
status: GroupPreRegistrationDto['status'];
|
||||
reviewedAt: Date | null;
|
||||
createdAt: Date;
|
||||
updatedAt: Date;
|
||||
};
|
||||
|
||||
export function toGroupPreRegistrationDto(
|
||||
record: GroupPreRegistrationRecord,
|
||||
): GroupPreRegistrationDto {
|
||||
return {
|
||||
id: record.id,
|
||||
groupId: record.groupId,
|
||||
fullName: record.fullName,
|
||||
phone: record.phone,
|
||||
email: record.email,
|
||||
status: record.status,
|
||||
reviewedAt: record.reviewedAt ? record.reviewedAt.toISOString() : null,
|
||||
createdAt: record.createdAt.toISOString(),
|
||||
updatedAt: record.updatedAt.toISOString(),
|
||||
};
|
||||
}
|
||||
12
apps/backend/src/modules/group-preregistrations/routes.ts
Normal file
12
apps/backend/src/modules/group-preregistrations/routes.ts
Normal file
@@ -0,0 +1,12 @@
|
||||
import { Hono } from 'hono';
|
||||
import approveRoute from './features/approve/route';
|
||||
import getAllRoute from './features/get-all/route';
|
||||
import rejectRoute from './features/reject/route';
|
||||
|
||||
const routes = new Hono();
|
||||
|
||||
routes.route('/', getAllRoute);
|
||||
routes.route('/', approveRoute);
|
||||
routes.route('/', rejectRoute);
|
||||
|
||||
export default routes;
|
||||
@@ -6,6 +6,7 @@ import createAttendeeRoute from '../attendees/features/create/route';
|
||||
import getAttendeeRoute from '../attendees/features/get-by-id/route';
|
||||
import getAttendeeStatsRoute from '../attendees/features/get-stats/route';
|
||||
import removeAttendeeRoute from '../attendees/features/remove/route';
|
||||
import groupPreRegistrationRoutes from '../group-preregistrations/routes';
|
||||
import groupWaitlistRoutes from '../group-waitlist/routes';
|
||||
import createGroupRoute from './features/create/route';
|
||||
import getAllRoute from './features/get-all/route';
|
||||
@@ -24,6 +25,7 @@ routes.route('/', bulkCreateAttendeesRoute);
|
||||
routes.route('/', getByIdRoute);
|
||||
routes.route('/', addToGroupWaitlistRoute);
|
||||
routes.route('/', groupWaitlistRoutes);
|
||||
routes.route('/', groupPreRegistrationRoutes);
|
||||
routes.route('/', analyticsRoutes);
|
||||
routes.route('/', getAttendeeRoute);
|
||||
routes.route('/', getAttendeeStatsRoute);
|
||||
|
||||
@@ -1,18 +1,18 @@
|
||||
import type { PrismaClient } from '@generated/prisma/client';
|
||||
import type { AttendeeDto, JoinGroupViaInvite, ProblemDetails, Result } from '@gruperly/shared';
|
||||
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 { type AttendeeRecord, normalizePhone, toAttendeeDto } from '@/modules/attendees/lib/helpers';
|
||||
import { normalizePhone } from '@/modules/attendees/lib/helpers';
|
||||
|
||||
type JoinViaInviteDeps = {
|
||||
db?: Pick<PrismaClient, 'group' | 'attendee' | 'groupWaitlistEntry'>;
|
||||
db?: Pick<PrismaClient, 'group' | 'attendee' | 'groupPreRegistration'>;
|
||||
};
|
||||
|
||||
export type JoinViaInviteResult = {
|
||||
status: 'registered' | 'waitlisted';
|
||||
status: 'pre_registered';
|
||||
message: string;
|
||||
attendee: AttendeeDto | null;
|
||||
attendee: null;
|
||||
};
|
||||
|
||||
export class JoinViaInvite {
|
||||
@@ -53,50 +53,63 @@ export class JoinViaInvite {
|
||||
}
|
||||
|
||||
const fullName = `${payload.firstName.trim()} ${payload.lastName.trim()}`.trim();
|
||||
const email = payload.email?.trim() || null;
|
||||
|
||||
if (group.capacity !== null) {
|
||||
const currentCount = await db.attendee.count({
|
||||
where: { groupId: group.id, status: 'ACTIVE' },
|
||||
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 },
|
||||
});
|
||||
|
||||
if (currentCount >= group.capacity) {
|
||||
const existingWaitlistEntry = await db.groupWaitlistEntry.findFirst({
|
||||
where: { groupId: group.id, phone },
|
||||
});
|
||||
|
||||
if (!existingWaitlistEntry) {
|
||||
await db.groupWaitlistEntry.create({
|
||||
data: {
|
||||
groupId: group.id,
|
||||
fullName,
|
||||
phone,
|
||||
email: payload.email?.trim() || null,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
return ok({
|
||||
status: 'waitlisted',
|
||||
message:
|
||||
'El grupo está completo. Fuiste agregado a la lista de espera. Te contactaremos cuando haya un lugar disponible.',
|
||||
attendee: 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.',
|
||||
});
|
||||
}
|
||||
|
||||
const created = await db.attendee.create({
|
||||
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: payload.email?.trim() || null,
|
||||
email,
|
||||
},
|
||||
});
|
||||
|
||||
return ok({
|
||||
status: 'registered',
|
||||
attendee: toAttendeeDto(created as unknown as AttendeeRecord),
|
||||
message: 'Inscripción realizada con éxito',
|
||||
status: 'pre_registered',
|
||||
attendee: null,
|
||||
message:
|
||||
'Recibimos tu solicitud de inscripción. El profesor revisará tus datos y te confirmará el lugar.',
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user