Skip to content

Commit

Permalink
fix: erroneous booking query
Browse files Browse the repository at this point in the history
  • Loading branch information
dyzhuu committed Sep 30, 2024
1 parent c1254c4 commit ae702b8
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 17 deletions.
36 changes: 21 additions & 15 deletions src/app/api/bookings/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,30 +68,36 @@ export const POST = userRouteWrapper(
? MEMBER_MAX_SESSIONS
: NON_MEMBER_MAX_SESSIONS;

// const [bookingsThisWeek] = await db
// .select({ count: count() })
// .from(bookings)
// .innerJoin(bookingDetails, eq(bookings.id, bookingDetails.bookingId))
// .innerJoin(
// bookingPeriods,
// and(
// gte(bookings.createdAt, bookingPeriods.bookingOpenTime),
// lte(bookings.createdAt, bookingPeriods.bookingCloseTime)
// )
// )
// .where(eq(bookings.userId, currentUser.id));

const [bookingsThisWeek] = await db
.select({ count: count() })
.from(bookings)
.innerJoin(bookingDetails, eq(bookings.id, bookingDetails.bookingId))
.innerJoin(
bookingPeriods,
and(
gte(bookings.createdAt, bookingPeriods.bookingOpenTime),
lte(bookings.createdAt, bookingPeriods.bookingCloseTime)
)
)
.where(
and(
eq(bookings.userId, currentUser!.id),
sql`date_trunc('week', ${bookings.createdAt}) = date_trunc('week', CURRENT_DATE)`
eq(bookings.userId, currentUser.id),
lte(bookingPeriods.bookingOpenTime, new Date()),
gte(bookingPeriods.bookingCloseTime, new Date())
)
);

// const [bookingsThisWeek] = await db
// .select({ count: count() })
// .from(bookings)
// .innerJoin(bookingDetails, eq(bookings.id, bookingDetails.bookingId))
// .where(
// and(
// eq(bookings.userId, currentUser!.id),
// sql`date_trunc('week', ${bookings.createdAt}) = date_trunc('week', CURRENT_DATE)`
// )
// );

// if user has already booked the maximum allowed sessions for this week
if (bookingsThisWeek.count + numOfSessions > allowedBookingCount) {
return responses.badRequest({
Expand Down
4 changes: 2 additions & 2 deletions src/app/sessions/select-play-level/client-page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,9 @@ export default function ClientSelectPlayLevelPage() {
});
} else if (code === "ALREADY_BOOKED") {
toast({
title: "Something went wrong.",
title: "Session Already Booked",
description:
"An error occurred while confirming your booking. Please try again.",
"You have already booked this session. Please select a different session.",
variant: "destructive",
});
} else if (code === "LIMIT_REACHED") {
Expand Down

0 comments on commit ae702b8

Please sign in to comment.