Skip to content

Commit

Permalink
fix booking creation if user already has a booking (#707)
Browse files Browse the repository at this point in the history
* fix booking creation

* fix test
  • Loading branch information
choden-dev authored Jul 28, 2024
1 parent 32ea0f8 commit 399a883
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 2 deletions.
36 changes: 35 additions & 1 deletion server/src/middleware/tests/BookingController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -402,13 +402,18 @@ describe("BookingController endpoint tests", () => {
max_bookings: 10
})

await bookingSlotService.createBookingSlot({
date: dateToFirestoreTimeStamp(new Date("01/01/2024")),
max_bookings: 10
})

await bookingDataService.createBooking({
user_id: MEMBER_USER_UID,
booking_slot_id: slot1.id,
stripe_payment_id: ""
})

const res = await request
let res = await request
.post("/bookings/create-bookings")
.set("Authorization", `Bearer ${adminToken}`)
.send({
Expand All @@ -426,6 +431,35 @@ describe("BookingController endpoint tests", () => {
])
})
])

expect(
(
await bookingSlotService.getBookingSlotsBetweenDateRange(
startDate,
endDate
)
).length
).toEqual(1)

const newEndDate = dateToFirestoreTimeStamp(new Date("01/01/2024"))

res = await request
.post("/bookings/create-bookings")
.set("Authorization", `Bearer ${adminToken}`)
.send({
startDate,
newEndDate,
userIds: [MEMBER_USER_UID]
})

expect(
(
await bookingSlotService.getBookingSlotsBetweenDateRange(
startDate,
newEndDate
)
).length
).toEqual(2)
})
})
})
6 changes: 5 additions & 1 deletion server/src/service-layer/controllers/BookingController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,12 @@ export class BookingController extends Controller {
let userIds = [...requestBody.userIds]
/** For every slotid add a booking for that id only if user doesn't already have a booking */
const userIdsPromises = userIds.map(async (userId) => {
const existingBookingsForUser =
await bookingDataService.getBookingsByUserId(userId)
if (
(await bookingDataService.getBookingsByUserId(userId)).length !== 0
existingBookingsForUser.some(
(booking) => booking.booking_slot_id === slot.id
)
) {
userIds = userIds.filter((id) => id !== userId) // Remove user from list if they already have a booking
} else {
Expand Down

0 comments on commit 399a883

Please sign in to comment.