diff --git a/server/src/data-layer/services/BookingHistoryService.test.ts b/server/src/data-layer/services/BookingHistoryService.test.ts index a4f43c7e2..466f87bc7 100644 --- a/server/src/data-layer/services/BookingHistoryService.test.ts +++ b/server/src/data-layer/services/BookingHistoryService.test.ts @@ -1 +1,66 @@ -describe("BookingHistoryService integration tests", () => {}) +import { dateToFirestoreTimeStamp } from "data-layer/adapters/DateUtils" +import BookingHistoryService from "./BookingHistoryService" +import { Timestamp } from "firebase-admin/firestore" +import db from "data-layer/adapters/FirestoreCollections" +import { cleanFirestore } from "test-config/TestUtils" + +const bookingHistoryService = new BookingHistoryService() +describe("BookingHistoryService integration tests", () => { + afterEach(async () => { + await cleanFirestore() + }) + + it("Should be able to add an event for deleted bookings", async () => { + const startDate = dateToFirestoreTimeStamp(new Date(2002, 10, 8)) + const endDate = dateToFirestoreTimeStamp(new Date(2002, 10, 10)) + const currentTime = dateToFirestoreTimeStamp(new Date()) + + const newEvent = await bookingHistoryService.addBookingDeletedEvent({ + uid: "user-removed-from-booking", + start_date: startDate as Timestamp, + end_date: endDate as Timestamp, + event_type: "removed_user_from_booking", + timestamp: currentTime + }) + + const result = db.bookingHistory.doc("user-removed-from-booking").get() + + expect(result).toEqual(newEvent) + }) + + it("Should be able to add an event for created bookings", async () => { + const startDate = dateToFirestoreTimeStamp(new Date(2002, 10, 8)) + const endDate = dateToFirestoreTimeStamp(new Date(2002, 10, 10)) + const currentTime = dateToFirestoreTimeStamp(new Date()) + + const newEvent = await bookingHistoryService.addBookingAddedEvent({ + uid: "user-added-to-booking", + start_date: startDate as Timestamp, + end_date: endDate as Timestamp, + event_type: "added_user_to_booking", + timestamp: currentTime + }) + + const result = db.bookingHistory.doc("user-added-to-booking").get() + + expect(result).toEqual(newEvent) + }) + + it("Should be able to add an event for availability changes", async () => { + const startDate = dateToFirestoreTimeStamp(new Date(2002, 10, 8)) + const endDate = dateToFirestoreTimeStamp(new Date(2002, 10, 10)) + const currentTime = dateToFirestoreTimeStamp(new Date()) + + const newEvent = await bookingHistoryService.addAvailibilityChangeEvent({ + start_date: startDate as Timestamp, + end_date: endDate as Timestamp, + event_type: "changed_date_availability", + timestamp: currentTime, + change: -69 + }) + + const result = db.bookingHistory.doc(newEvent.id).get() + + expect(result).toEqual(newEvent) + }) +}) diff --git a/server/src/data-layer/services/BookingHistoryService.ts b/server/src/data-layer/services/BookingHistoryService.ts index 12a9a7e01..1516ea545 100644 --- a/server/src/data-layer/services/BookingHistoryService.ts +++ b/server/src/data-layer/services/BookingHistoryService.ts @@ -15,31 +15,32 @@ class BookingHistoryService { * Stores a manual deletion of a booking (by admin) into the booking history collection * * @param event the required parameters defined by {@link BookingDeletedEvent} + * @returns the created document */ - public async addBookingDeletedEvent( - event: BookingDeletedEvent - ): Promise { - await db.bookingHistory.add(event) + public async addBookingDeletedEvent(event: BookingDeletedEvent) { + return await db.bookingHistory.add(event) } /** * Stores a manual creation of a booking (by admin) into the booking history collection * * @param event the required parameters defined by {@link BookingAddedEvent} + * @returns the created document */ - public async addBookingAddedEvent(event: BookingAddedEvent): Promise { - await db.bookingHistory.add(event) + public async addBookingAddedEvent(event: BookingAddedEvent) { + return await db.bookingHistory.add(event) } /** * Stores a modification to the booking availability into the booking history collection * * @param event the required parameters defined by {@link BookingAvailabilityChangeEvent} + * @returns the created document */ public async addAvailibilityChangeEvent( event: BookingAvailabilityChangeEvent - ): Promise { - await db.bookingHistory.add(event) + ) { + return await db.bookingHistory.add(event) } /**