-
Notifications
You must be signed in to change notification settings - Fork 43
Prevent Duplicate Event Registrations #133
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
TarunyaProgrammer
merged 3 commits into
niharika-mente:main
from
AYUSH-P-SINGH:prevent-duplicat-event
Jul 1, 2026
+261
−93
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,109 @@ | ||
| import { NextRequest, NextResponse } from "next/server"; | ||
| import { Types } from "mongoose"; | ||
| import connectToDatabase from "@/lib/mongodb"; | ||
| import { Booking, Event } from "@/database"; | ||
| import { revalidatePath } from "next/cache"; | ||
|
|
||
| interface RouteParams { | ||
| params: Promise<{ id: string }>; | ||
| } | ||
|
|
||
| const EMAIL_REGEX = /^[^\s@]+@[^\s@]+\.[^\s@]+$/; | ||
|
|
||
| export async function POST(req: NextRequest, { params }: RouteParams) { | ||
| try { | ||
| const { id: eventId } = await params; | ||
|
|
||
| // 1. Validate event ID | ||
| if (!eventId || !Types.ObjectId.isValid(eventId)) { | ||
| return NextResponse.json( | ||
| { message: "Invalid event ID format." }, | ||
| { status: 400 } | ||
| ); | ||
| } | ||
|
|
||
| // 2. Parse request body | ||
| let body; | ||
| try { | ||
| body = await req.json(); | ||
| } catch { | ||
| return NextResponse.json( | ||
| { message: "Invalid request body." }, | ||
| { status: 400 } | ||
| ); | ||
| } | ||
|
|
||
| const { email } = body; | ||
|
|
||
| // 3. Validate email presence and format | ||
| if (!email || typeof email !== "string" || !email.trim()) { | ||
| return NextResponse.json( | ||
| { message: "Email is required." }, | ||
| { status: 400 } | ||
| ); | ||
| } | ||
|
|
||
| const cleanEmail = email.trim().toLowerCase(); | ||
|
|
||
| if (!EMAIL_REGEX.test(cleanEmail)) { | ||
| return NextResponse.json( | ||
| { message: "Please provide a valid email address." }, | ||
| { status: 400 } | ||
| ); | ||
| } | ||
|
|
||
| // 4. Connect to database | ||
| await connectToDatabase(); | ||
|
|
||
| // 5. Verify if the event exists | ||
| const event = await Event.findById(eventId); | ||
| if (!event) { | ||
| return NextResponse.json( | ||
| { message: "Event not found." }, | ||
| { status: 404 } | ||
| ); | ||
| } | ||
|
|
||
| // 6. Check for existing registration | ||
| const existingRegistration = await Booking.findOne({ | ||
| eventId, | ||
| email: cleanEmail, | ||
| }); | ||
|
|
||
| if (existingRegistration) { | ||
| return NextResponse.json( | ||
| { message: "You have already registered for this event." }, | ||
| { status: 409 } | ||
| ); | ||
| } | ||
|
|
||
| // 7. Save registration (create booking) | ||
| const booking = await Booking.create({ | ||
| eventId, | ||
| email: cleanEmail, | ||
| }); | ||
|
|
||
| // 8. Revalidate paths to refresh cache | ||
| revalidatePath(`/events/${event.slug}`); | ||
| revalidatePath("/"); | ||
|
|
||
| return NextResponse.json( | ||
| { message: "Registration successful", booking }, | ||
| { status: 201 } | ||
| ); | ||
| } catch (error: any) { | ||
| // 9. Handle duplicate key errors from unique compound index | ||
| if (error && error.code === 11000) { | ||
| return NextResponse.json( | ||
| { message: "You have already registered for this event." }, | ||
| { status: 409 } | ||
| ); | ||
| } | ||
|
|
||
| console.error("Error in registration API:", error); | ||
| return NextResponse.json( | ||
| { message: "Internal server error.", error: error instanceof Error ? error.message : "Unknown" }, | ||
| { status: 500 } | ||
| ); | ||
| } | ||
| } | ||
|
TarunyaProgrammer marked this conversation as resolved.
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.