Skip to content

Commit

Permalink
add test for date range query
Browse files Browse the repository at this point in the history
  • Loading branch information
choden-dev committed Aug 2, 2024
1 parent e97a107 commit dcd4a29
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 0 deletions.
56 changes: 56 additions & 0 deletions server/src/data-layer/services/BookingHistoryService.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,60 @@ describe("BookingHistoryService integration tests", () => {

expect(result).toEqual(newEvent)
})

it("Should be able to fetch history in between a range of dates", async () => {
/**
* In these tests we don't care about these
*/
const startDate = dateToFirestoreTimeStamp(new Date(2002, 10, 8))
const endDate = dateToFirestoreTimeStamp(new Date(2002, 10, 10))

const searchStartDate = dateToFirestoreTimeStamp(new Date(2001, 10, 6))
const searchEndDate = dateToFirestoreTimeStamp(new Date(2001, 10, 9))

const availabilityEvent =
await bookingHistoryService.addAvailibilityChangeEvent({
timestamp: dateToFirestoreTimeStamp(new Date(2001, 10, 9)),
change: 69,
start_date: startDate,
end_date: endDate,
event_type: "changed_date_availability"
})

const deletedEvent = await bookingHistoryService.addBookingDeletedEvent({
timestamp: dateToFirestoreTimeStamp(new Date(2001, 10, 9)),
start_date: startDate,
end_date: endDate,
event_type: "removed_user_from_booking",
uid: "deleted-user"
})

const addedEvent = await bookingHistoryService.addBookingAddedEvent({
timestamp: dateToFirestoreTimeStamp(new Date(2001, 10, 9)),
start_date: startDate,
end_date: endDate,
event_type: "added_user_to_booking",
uid: "added-user"
})

const notIncludedEvent = await bookingHistoryService.addBookingAddedEvent({
timestamp: dateToFirestoreTimeStamp(new Date(2001, 10, 10)),
start_date: startDate,
end_date: endDate,
event_type: "added_user_to_booking",
uid: "unincluded-user"
})

const foundEvents =
await bookingHistoryService.getAllHistoryBetweenDateRange(
searchStartDate,
searchEndDate
)

expect(foundEvents).toContainEqual(availabilityEvent)
expect(foundEvents).toContainEqual(addedEvent)
expect(foundEvents).toContainEqual(deletedEvent)
expect(foundEvents).not.toContainEqual(notIncludedEvent)
})
it("Should be able to fetch the latest X events")
})
23 changes: 23 additions & 0 deletions server/src/data-layer/services/BookingHistoryService.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { firestoreTimestampToDate } from "data-layer/adapters/DateUtils"
import db from "data-layer/adapters/FirestoreCollections"
import {
DocumentDataWithUid,
Expand All @@ -9,6 +10,7 @@ import {
BookingDeletedEvent,
BookingHistoryEvent
} from "data-layer/models/firebase"
import { Timestamp } from "firebase-admin/firestore"

class BookingHistoryService {
/**
Expand Down Expand Up @@ -43,6 +45,27 @@ class BookingHistoryService {
return await db.bookingHistory.add(event)
}

/**
* Returns all history events whose timestamps fall between the given timestamps
*
* @param startDate the first date to return history events for
* @param endDate the last date to return history events for
* @returns a list of all events that fall between the specified date range
*/
public async getAllHistoryBetweenDateRange(
startDate: Timestamp,
endDate: Timestamp
): Promise<DocumentDataWithUid<BookingHistoryEvent>[]> {
const res = await db.bookingHistory
.where("timestamp", ">=", firestoreTimestampToDate(startDate))
.where("timestamp", "<=", firestoreTimestampToDate(endDate))
.get()

return res.docs.map((doc) => {
return { ...doc.data(), id: doc.id }
})
}

/**
* Fetches the **latest** page of booking history events.
*
Expand Down

0 comments on commit dcd4a29

Please sign in to comment.