Skip to content

Commit

Permalink
498 backend include booking id in fetch users endpoint (#523)
Browse files Browse the repository at this point in the history
* Adjusted fetch users to return associated bookingIds for each user and tested to see if it works

* Formatted files properly

* Adjusted fetch users to return bookingId as well by changing what getBookingsBySlotId returns and tested this post request

* Adjusted bookingdataservicetest  to account for  getbookingsbyslotid now also returning the bookingid
  • Loading branch information
Kartik-M24 authored Jun 28, 2024
1 parent 5caabd5 commit 6d11b16
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 13 deletions.
5 changes: 3 additions & 2 deletions server/src/data-layer/services/BookingDataService.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ describe("BookingDataService", () => {
})

it("Should get bookings based on specific id", async () => {
await new BookingDataService().createBooking({
const booking1 = await new BookingDataService().createBooking({
user_id: "Person1",
booking_slot_id: "booking1",
stripe_payment_id: "stripeID1"
Expand All @@ -53,7 +53,8 @@ describe("BookingDataService", () => {
expect(bookingsBy_BSID[0]).toEqual({
user_id: "Person1",
booking_slot_id: "booking1",
stripe_payment_id: "stripeID1"
stripe_payment_id: "stripeID1",
id: booking1.id
})

const bookingsBy_SPID =
Expand Down
12 changes: 8 additions & 4 deletions server/src/data-layer/services/BookingDataService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,18 @@ export default class BookingDataService {
* ```
*
* @param bookingSlotID The booking slot ID to retrieve all bookings for.
* @returns All current bookings associated with this slot.
* @returns All current bookings and bookingId associated with this slot.
*/
public async getBookingsBySlotId(bookingSlotID: string): Promise<Booking[]> {
public async getBookingsBySlotId(
bookingSlotID: string
): Promise<Array<DocumentDataWithUid<Booking>>> {
const result = await FirestoreCollections.bookings
.where("booking_slot_id", "==", bookingSlotID)
.get()
const bookings = result.docs.map((docs) => docs.data())
return bookings
const bookingsAndIdArray = result.docs.map((docs) => {
return { ...docs.data(), id: docs.id }
})
return bookingsAndIdArray
}

/**
Expand Down
18 changes: 15 additions & 3 deletions server/src/middleware/routes.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1222,7 +1222,7 @@ describe("Endpoints", () => {
await cleanFirestore()
})

it("should return users with bookings within the date range", async () => {
it("should return users with bookings (and their corresponding bookingIds) within the date range", async () => {
const bookingSlotService = new BookingSlotService()
const bookingDataService = new BookingDataService()

Expand All @@ -1245,17 +1245,19 @@ describe("Endpoints", () => {
max_bookings: 10
})

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

await bookingDataService.createBooking({
const bookingResult2 = await bookingDataService.createBooking({
user_id: GUEST_USER_UID,
booking_slot_id: slot2.id,
stripe_payment_id: ""
})
const id2 = bookingResult2.id

const res = await request
.post("/bookings/fetch-users")
Expand All @@ -1278,6 +1280,16 @@ describe("Endpoints", () => {
users: expect.arrayContaining([
expect.objectContaining({ uid: GUEST_USER_UID })
])
}),
expect.objectContaining({
bookingIds: expect.arrayContaining([
expect.objectContaining({ bookingId: id1 })
])
}),
expect.objectContaining({
bookingIds: expect.arrayContaining([
expect.objectContaining({ bookingId: id2 })
])
})
])
})
Expand Down
19 changes: 15 additions & 4 deletions server/src/service-layer/controllers/BookingController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@ import {
Request
} from "tsoa"
import { firestoreTimestampToDate } from "data-layer/adapters/DateUtils"
import { CombinedUserData } from "../response-models/UserResponse"
import {
BookingIdandUserData,
CombinedUserData
} from "../response-models/UserResponse"
import { UsersByDateRangeResponse } from "../response-models/BookingResponse"
import UserDataService from "../../data-layer/services/UserDataService"
import * as console from "console"
Expand Down Expand Up @@ -281,15 +284,15 @@ export class BookingController extends Controller {
/** The response data array */
const responseData: Array<{
date: Timestamp
users: CombinedUserData[]
users: BookingIdandUserData[]
}> = []

/** Iterating through each booking slot */
const bookingPromises = bookingSlots.map(async (slot) => {
/** Getting the bookings for the current slot */
const bookings = await bookingDataService.getBookingsBySlotId(slot.id)

/** Extracting the user IDs from the bookings */
/** Extracting the user from the bookings */
const userIds = bookings.map((booking) => booking.user_id)

if (userIds.length === 0) {
Expand Down Expand Up @@ -328,7 +331,15 @@ export class BookingController extends Controller {
/** Adding the date and users to the response data array */
responseData.push({
date: slot.date,
users: combinedUsers
// Mapping the users to include the booking ID
users: combinedUsers.map((user) => ({
...user,
bookingId: bookings.find(
(booking) =>
booking.user_id === user.uid &&
booking.booking_slot_id === slot.id
)?.id
}))
})
})

Expand Down
4 changes: 4 additions & 0 deletions server/src/service-layer/response-models/UserResponse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,7 @@ export interface AllUsersResponse
CursorPaginatedResponse {
data?: CombinedUserData[]
}

export interface BookingIdandUserData extends CombinedUserData {
bookingId: string
}

0 comments on commit 6d11b16

Please sign in to comment.