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:
@@ -4,23 +4,27 @@ import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query'
|
||||
import type {
|
||||
AttendeeDto,
|
||||
GroupDto,
|
||||
GroupPreRegistrationDto,
|
||||
GroupWaitlistEntryDto,
|
||||
GroupRiskLevel,
|
||||
} from '@gruperly/shared'
|
||||
import { useToast } from '../../../components/ui'
|
||||
import {
|
||||
ApiError,
|
||||
approveGroupPreRegistration,
|
||||
getGroup,
|
||||
getGroupAttendees,
|
||||
getGroupPreRegistrations,
|
||||
getGroupsRiskOverview,
|
||||
getGroupWaitlist,
|
||||
getInviteToken,
|
||||
promoteGroupWaitlistEntry,
|
||||
rejectGroupPreRegistration,
|
||||
removeGroupWaitlistEntry,
|
||||
} from '../../../lib/api'
|
||||
import { useRemoveAttendee } from './useRemoveAttendee'
|
||||
|
||||
export type ListSection = 'members' | 'waitlist'
|
||||
export type ListSection = 'members' | 'preregistrations' | 'waitlist'
|
||||
|
||||
type GroupDetailContextValue = {
|
||||
groupId: string
|
||||
@@ -34,6 +38,9 @@ type GroupDetailContextValue = {
|
||||
waitlistTotal: number
|
||||
waitlistPending: boolean
|
||||
firstWaitlistEntry: GroupWaitlistEntryDto | null
|
||||
preRegistrations: GroupPreRegistrationDto[]
|
||||
preRegistrationsTotal: number
|
||||
preRegistrationsPending: boolean
|
||||
hasFreeCapacity: boolean
|
||||
inviteUrl: string | undefined
|
||||
invitePending: boolean
|
||||
@@ -51,12 +58,18 @@ type GroupDetailContextValue = {
|
||||
confirmRemoveAttendee: (promoteFromWaitlist: boolean) => void
|
||||
selectedWaitlistEntry: GroupWaitlistEntryDto | null
|
||||
setSelectedWaitlistEntry: (entry: GroupWaitlistEntryDto | null) => void
|
||||
selectedPreRegistration: GroupPreRegistrationDto | null
|
||||
setSelectedPreRegistration: (entry: GroupPreRegistrationDto | null) => void
|
||||
isSubmittingRemoval: boolean
|
||||
isSubmittingPromotion: boolean
|
||||
isSubmittingWaitlistRemoval: boolean
|
||||
isSubmittingApproval: boolean
|
||||
isSubmittingRejection: boolean
|
||||
isRegeneratingInvite: boolean
|
||||
promoteEntry: (entry: GroupWaitlistEntryDto) => void
|
||||
removeEntry: (entry: GroupWaitlistEntryDto) => void
|
||||
approvePreRegistration: (entry: GroupPreRegistrationDto) => void
|
||||
rejectPreRegistration: (entry: GroupPreRegistrationDto) => void
|
||||
regenerateInvite: () => void
|
||||
copyInviteLink: () => Promise<void>
|
||||
copyWhatsappMessage: () => Promise<void>
|
||||
@@ -73,6 +86,7 @@ export function GroupDetailProvider({ groupId, children }: { groupId: string; ch
|
||||
const [listSection, setListSection] = useState<ListSection>('members')
|
||||
const [attendeeToRemove, setAttendeeToRemove] = useState<AttendeeDto | null>(null)
|
||||
const [selectedWaitlistEntry, setSelectedWaitlistEntry] = useState<GroupWaitlistEntryDto | null>(null)
|
||||
const [selectedPreRegistration, setSelectedPreRegistration] = useState<GroupPreRegistrationDto | null>(null)
|
||||
const [searchFilter, setSearchFilter] = useState('')
|
||||
|
||||
const groupQuery = useQuery({
|
||||
@@ -105,17 +119,30 @@ export function GroupDetailProvider({ groupId, children }: { groupId: string; ch
|
||||
enabled: Boolean(groupId),
|
||||
})
|
||||
|
||||
const preRegistrationsQuery = useQuery({
|
||||
queryKey: ['group-preregistrations', groupId],
|
||||
queryFn: () => getGroupPreRegistrations(groupId, 1, 100),
|
||||
enabled: Boolean(groupId),
|
||||
})
|
||||
|
||||
const group = groupQuery.data
|
||||
const attendees = useMemo(() => attendeesQuery.data?.data ?? [], [attendeesQuery.data])
|
||||
const attendeesTotal = attendeesQuery.data?.pagination?.total ?? attendees.length
|
||||
const waitlistEntries = useMemo(() => waitlistQuery.data?.data ?? [], [waitlistQuery.data])
|
||||
const waitlistTotal = waitlistQuery.data?.pagination?.total ?? waitlistEntries.length
|
||||
const preRegistrations = useMemo(
|
||||
() => preRegistrationsQuery.data?.data ?? [],
|
||||
[preRegistrationsQuery.data],
|
||||
)
|
||||
const preRegistrationsTotal =
|
||||
preRegistrationsQuery.data?.pagination?.total ?? preRegistrations.length
|
||||
const firstWaitlistEntry = waitlistEntries[0] ?? null
|
||||
const hasFreeCapacity = group?.capacity == null || attendeesTotal < group.capacity
|
||||
|
||||
const invalidateGroupQueries = () => {
|
||||
void queryClient.invalidateQueries({ queryKey: ['group-attendees', groupId] })
|
||||
void queryClient.invalidateQueries({ queryKey: ['group-waitlist', groupId] })
|
||||
void queryClient.invalidateQueries({ queryKey: ['group-preregistrations', groupId] })
|
||||
void queryClient.invalidateQueries({ queryKey: ['group', groupId] })
|
||||
}
|
||||
|
||||
@@ -161,10 +188,56 @@ export function GroupDetailProvider({ groupId, children }: { groupId: string; ch
|
||||
},
|
||||
})
|
||||
|
||||
const approvePreRegistrationMutation = useMutation({
|
||||
mutationFn: (entry: GroupPreRegistrationDto) =>
|
||||
approveGroupPreRegistration(groupId, entry.id),
|
||||
onSuccess: (result, entry) => {
|
||||
setSelectedPreRegistration(null)
|
||||
toast.success(
|
||||
result.outcome === 'added'
|
||||
? `${entry.fullName} ahora es miembro del grupo.`
|
||||
: `${entry.fullName} fue agregado a la lista de espera: el grupo está completo.`,
|
||||
)
|
||||
invalidateGroupQueries()
|
||||
},
|
||||
onError: (err: Error) => {
|
||||
if (err instanceof ApiError) {
|
||||
if (err.problem?.code === 'attendee_already_registered') {
|
||||
toast.error('Ya hay un miembro con ese teléfono en el grupo. Puedes rechazar la pre-inscripción.')
|
||||
return
|
||||
}
|
||||
if (err.problem?.code === 'pre_registration_already_reviewed') {
|
||||
toast.error('Esta pre-inscripción ya fue revisada.')
|
||||
invalidateGroupQueries()
|
||||
return
|
||||
}
|
||||
}
|
||||
toast.error(err.message || 'No se pudo aprobar la pre-inscripción.')
|
||||
},
|
||||
})
|
||||
|
||||
const rejectPreRegistrationMutation = useMutation({
|
||||
mutationFn: (entry: GroupPreRegistrationDto) =>
|
||||
rejectGroupPreRegistration(groupId, entry.id),
|
||||
onSuccess: (_result, entry) => {
|
||||
setSelectedPreRegistration(null)
|
||||
toast.success(`La pre-inscripción de ${entry.fullName} fue rechazada.`)
|
||||
invalidateGroupQueries()
|
||||
},
|
||||
onError: (err: Error) => {
|
||||
if (err instanceof ApiError && err.problem?.code === 'pre_registration_already_reviewed') {
|
||||
toast.error('Esta pre-inscripción ya fue revisada.')
|
||||
invalidateGroupQueries()
|
||||
return
|
||||
}
|
||||
toast.error(err.message || 'No se pudo rechazar la pre-inscripción.')
|
||||
},
|
||||
})
|
||||
|
||||
const inviteUrl = inviteTokenQuery.data?.inviteUrl
|
||||
const whatsappMessage =
|
||||
group && inviteUrl
|
||||
? `¡Hola! 👋 Te invito a unirte al grupo *${group.name}*.\n\nCompleta tus datos de inscripción en el siguiente enlace:\n${inviteUrl}`
|
||||
? `¡Hola! 👋 Te invito a unirte al grupo *${group.name}*.\n\nCompleta tu pre-inscripción en el siguiente enlace. El profesor revisará tu solicitud:\n${inviteUrl}`
|
||||
: ''
|
||||
const whatsappShareUrl = whatsappMessage
|
||||
? `https://wa.me/?text=${encodeURIComponent(whatsappMessage)}`
|
||||
@@ -215,6 +288,9 @@ export function GroupDetailProvider({ groupId, children }: { groupId: string; ch
|
||||
waitlistTotal,
|
||||
waitlistPending: waitlistQuery.isPending,
|
||||
firstWaitlistEntry,
|
||||
preRegistrations,
|
||||
preRegistrationsTotal,
|
||||
preRegistrationsPending: preRegistrationsQuery.isPending,
|
||||
hasFreeCapacity,
|
||||
inviteUrl,
|
||||
invitePending: inviteTokenQuery.isPending,
|
||||
@@ -235,9 +311,13 @@ export function GroupDetailProvider({ groupId, children }: { groupId: string; ch
|
||||
},
|
||||
selectedWaitlistEntry,
|
||||
setSelectedWaitlistEntry,
|
||||
selectedPreRegistration,
|
||||
setSelectedPreRegistration,
|
||||
isSubmittingRemoval: removeAttendeeMutation.isRemovingAttendee,
|
||||
isSubmittingPromotion: promoteWaitlistMutation.isPending,
|
||||
isSubmittingWaitlistRemoval: removeWaitlistEntryMutation.isPending,
|
||||
isSubmittingApproval: approvePreRegistrationMutation.isPending,
|
||||
isSubmittingRejection: rejectPreRegistrationMutation.isPending,
|
||||
isRegeneratingInvite: regenerateTokenMutation.isPending,
|
||||
promoteEntry: (entry) => {
|
||||
setSelectedWaitlistEntry(null)
|
||||
@@ -247,6 +327,12 @@ export function GroupDetailProvider({ groupId, children }: { groupId: string; ch
|
||||
setSelectedWaitlistEntry(null)
|
||||
removeWaitlistEntryMutation.mutate(entry)
|
||||
},
|
||||
approvePreRegistration: (entry) => {
|
||||
approvePreRegistrationMutation.mutate(entry)
|
||||
},
|
||||
rejectPreRegistration: (entry) => {
|
||||
rejectPreRegistrationMutation.mutate(entry)
|
||||
},
|
||||
regenerateInvite: () => regenerateTokenMutation.mutate(),
|
||||
copyInviteLink,
|
||||
copyWhatsappMessage,
|
||||
|
||||
Reference in New Issue
Block a user