-
Notifications
You must be signed in to change notification settings - Fork 45
feat: add real-time seat locking and booking confirmation flow #139
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
Open
Johan621
wants to merge
5
commits into
niharika-mente:main
Choose a base branch
from
Johan621:feature/realtime-seat-locking
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
188386d
feat(api): add seat lock confirm release and availability endpoints
Johan621 846f467
feat(ui): add lock-based booking flow with realtime availability
Johan621 bef71b8
fix(bookings): count and list only confirmed bookings
Johan621 fd23a16
feat(booking): add lock and status fields to booking schema
Johan621 64ee8bc
feat(api): add seat lock confirm release and availability endpoints
Johan621 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,106 @@ | ||
| import { NextRequest, NextResponse } from "next/server"; | ||
| import { Types } from "mongoose"; | ||
| import mongoose from "mongoose"; | ||
| import connectToDatabase from "@/lib/mongodb"; | ||
| import Booking from "@/database/booking.model"; | ||
| import Event from "@/database/event.model"; | ||
|
|
||
| interface RouteParams { | ||
| params: Promise<{ slug: string }>; | ||
| } | ||
|
|
||
| async function expireOldLocks(eventId: string) { | ||
| await Booking.updateMany( | ||
| { | ||
| eventId, | ||
| status: "locked", | ||
| lockExpiresAt: { $lte: new Date() }, | ||
| }, | ||
| { $set: { status: "expired" } } | ||
| ); | ||
| } | ||
|
|
||
| async function getSeatStats(eventId: string, session?: mongoose.ClientSession) { | ||
| const now = new Date(); | ||
|
|
||
| const [confirmedAgg, lockedAgg] = await Promise.all([ | ||
| Booking.aggregate([ | ||
| { | ||
| $match: { | ||
| eventId: new Types.ObjectId(eventId), | ||
| status: "confirmed", | ||
| }, | ||
| }, | ||
| { $group: { _id: null, total: { $sum: "$seats" } } }, | ||
| ]).session(session || null), | ||
|
|
||
| Booking.aggregate([ | ||
| { | ||
| $match: { | ||
| eventId: new Types.ObjectId(eventId), | ||
| status: "locked", | ||
| lockExpiresAt: { $gt: now }, | ||
| }, | ||
| }, | ||
| { $group: { _id: null, total: { $sum: "$seats" } } }, | ||
| ]).session(session || null), | ||
| ]); | ||
|
|
||
| return { | ||
| confirmedSeats: confirmedAgg[0]?.total || 0, | ||
| lockedSeats: lockedAgg[0]?.total || 0, | ||
| }; | ||
| } | ||
|
|
||
| export async function GET(_req: NextRequest, { params }: RouteParams) { | ||
| try { | ||
| const { slug: eventId } = await params; | ||
|
|
||
| if (!eventId || !Types.ObjectId.isValid(eventId)) { | ||
| return NextResponse.json({ message: "Invalid event ID format." }, { status: 400 }); | ||
| } | ||
|
|
||
| await connectToDatabase(); | ||
|
|
||
| const event = await Event.findById(eventId); | ||
| if (!event) { | ||
| return NextResponse.json({ message: "Event not found." }, { status: 404 }); | ||
| } | ||
|
|
||
| await expireOldLocks(eventId); | ||
|
|
||
| // IMPORTANT: if your event model uses another field, replace here: | ||
| const capacity = Number((event as any).capacity ?? (event as any).totalSeats ?? 0); | ||
|
|
||
| if (!Number.isFinite(capacity) || capacity <= 0) { | ||
| return NextResponse.json( | ||
| { message: "Event capacity is not configured." }, | ||
| { status: 400 } | ||
| ); | ||
| } | ||
|
|
||
| const { confirmedSeats, lockedSeats } = await getSeatStats(eventId); | ||
| const availableSeats = Math.max(0, capacity - confirmedSeats - lockedSeats); | ||
|
|
||
| return NextResponse.json( | ||
| { | ||
| eventId, | ||
| capacity, | ||
| confirmedSeats, | ||
| lockedSeats, | ||
| availableSeats, | ||
| serverTime: new Date().toISOString(), | ||
| }, | ||
| { status: 200 } | ||
| ); | ||
| } catch (error: any) { | ||
| console.error("Error in availability API:", error); | ||
| return NextResponse.json( | ||
| { | ||
| message: "Internal server error.", | ||
| error: error instanceof Error ? error.message : "Unknown", | ||
| }, | ||
| { status: 500 } | ||
|
Johan621 marked this conversation as resolved.
|
||
| ); | ||
| } | ||
| } | ||
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.