From 6d11b1658e5444176ceac6d325f75b3b4bdb6d23 Mon Sep 17 00:00:00 2001 From: Kartik-M24 <161120979+Kartik-M24@users.noreply.github.com> Date: Sat, 29 Jun 2024 00:53:15 +1200 Subject: [PATCH] 498 backend include booking id in fetch users endpoint (#523) * 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 --- .../services/BookingDataService.test.ts | 5 +++-- .../data-layer/services/BookingDataService.ts | 12 ++++++++---- server/src/middleware/routes.test.ts | 18 +++++++++++++++--- .../controllers/BookingController.ts | 19 +++++++++++++++---- .../response-models/UserResponse.ts | 4 ++++ 5 files changed, 45 insertions(+), 13 deletions(-) diff --git a/server/src/data-layer/services/BookingDataService.test.ts b/server/src/data-layer/services/BookingDataService.test.ts index 808c50752..c392e7549 100644 --- a/server/src/data-layer/services/BookingDataService.test.ts +++ b/server/src/data-layer/services/BookingDataService.test.ts @@ -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" @@ -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 = diff --git a/server/src/data-layer/services/BookingDataService.ts b/server/src/data-layer/services/BookingDataService.ts index eabd16fec..511e0dde0 100644 --- a/server/src/data-layer/services/BookingDataService.ts +++ b/server/src/data-layer/services/BookingDataService.ts @@ -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 { + public async getBookingsBySlotId( + bookingSlotID: string + ): Promise>> { 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 } /** diff --git a/server/src/middleware/routes.test.ts b/server/src/middleware/routes.test.ts index bacb2c7fc..f538cee65 100644 --- a/server/src/middleware/routes.test.ts +++ b/server/src/middleware/routes.test.ts @@ -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() @@ -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") @@ -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 }) + ]) }) ]) }) diff --git a/server/src/service-layer/controllers/BookingController.ts b/server/src/service-layer/controllers/BookingController.ts index 861cf1a11..896ce81ff 100644 --- a/server/src/service-layer/controllers/BookingController.ts +++ b/server/src/service-layer/controllers/BookingController.ts @@ -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" @@ -281,7 +284,7 @@ export class BookingController extends Controller { /** The response data array */ const responseData: Array<{ date: Timestamp - users: CombinedUserData[] + users: BookingIdandUserData[] }> = [] /** Iterating through each booking slot */ @@ -289,7 +292,7 @@ export class BookingController extends Controller { /** 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) { @@ -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 + })) }) }) diff --git a/server/src/service-layer/response-models/UserResponse.ts b/server/src/service-layer/response-models/UserResponse.ts index 7f8848f5c..edca3f226 100644 --- a/server/src/service-layer/response-models/UserResponse.ts +++ b/server/src/service-layer/response-models/UserResponse.ts @@ -26,3 +26,7 @@ export interface AllUsersResponse CursorPaginatedResponse { data?: CombinedUserData[] } + +export interface BookingIdandUserData extends CombinedUserData { + bookingId: string +}