Skip to content

[BACKEND] Capacity check hook for registration #12

@aleckshen

Description

@aleckshen

Is your feature request related to a problem? Please describe

When someone submits a registration via POST /api/event-registrations, there is no logic to check whether the event has space, whether the user is already registered, or whether the event is still accepting sign-ups.

Describe the solution you'd like

Create a beforeChange hook at src/payload/hooks/eventRegistrations/checkCapacity.ts that runs automatically whenever a new registration is created. The hook will be wired into the EventRegistrations collection (#10).

Logic (in order):

  1. Skip if the operation is not create
  2. Fetch the event by ID — throw "Event not found" if it doesn't exist
  3. If event status is completed or cancelled, throw "This event is no longer accepting registrations"
  4. Check for a duplicate — query for an existing non-cancelled registration matching the same event + user (or event + guestEmail). Throw "You are already registered for this event" if found
  5. Count current registered registrations for the event. Throw "This event is full" if count >= maxCapacity
  6. If the request has a logged-in user, auto-set data.user from the session
  7. Return data so Payload can save it

Once this ticket and Ticket 4 are both merged, add checkCapacity to the beforeChange hooks array in EventRegistrations.ts.

Describe alternatives you've considered

A custom Next.js route handler was considered but a Payload hook keeps all business logic inside the Payload layer, meaning it runs regardless of how the collection is accessed (REST, Local API, admin panel).

Additional context

Depends on the Events (#9) and EventRegistrations (#10) collections. After this is wired up, the confirmation email call will also be added to the end of this hook.

Ref: https://payloadcms.com/docs/hooks/collections

BEFORE MERGING

  • Code generation run (hint: pnpm typegen)
  • PR Reviewed (For non-trivial changes)
  • All required PR checks passing

Metadata

Metadata

Assignees

No one assigned

    Labels

    featureNew feature being worked on

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions