-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b23ac44
commit 30a9225
Showing
1 changed file
with
78 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
import db from "data-layer/adapters/FirestoreCollections" | ||
import { | ||
DocumentDataWithUid, | ||
PaginatedFirebaseResponse | ||
} from "data-layer/models/common" | ||
import { | ||
BookingAddedEvent, | ||
BookingAvailabilityChangeEvent, | ||
BookingDeletedEvent, | ||
BookingHistoryEvent | ||
} from "data-layer/models/firebase" | ||
|
||
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} | ||
*/ | ||
public async addBookingDeletedEvent(event: BookingDeletedEvent) { | ||
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} | ||
*/ | ||
public async addBookingAddedEvent(event: BookingAddedEvent) { | ||
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} | ||
*/ | ||
public async addAvailibilityChangeEvent( | ||
event: BookingAvailabilityChangeEvent | ||
) { | ||
await db.bookingHistory.add(event) | ||
} | ||
|
||
/** | ||
* Fetches the **latest** page of booking history events. | ||
* | ||
* @param limit how many history events to fetch, defaults to `100` | ||
* @param startAfter the firebase document snapshot to paginate from | ||
* @returns the page of booking history items and a cursor pointing to the | ||
* last `id` to use for pagination | ||
*/ | ||
public async getAllHistory( | ||
limit: number = 100, | ||
startAfter?: FirebaseFirestore.DocumentSnapshot< | ||
BookingHistoryEvent, | ||
FirebaseFirestore.DocumentData | ||
> | ||
): Promise< | ||
PaginatedFirebaseResponse<DocumentDataWithUid<BookingHistoryEvent>> | ||
> { | ||
const res = await db.bookingHistory | ||
.orderBy("timestamp") | ||
.startAfter(startAfter || 0) | ||
.limit(limit) | ||
.get() | ||
|
||
const historyPage: DocumentDataWithUid<BookingHistoryEvent>[] = | ||
res.docs.map((event) => { | ||
return { ...event.data(), id: event.id } | ||
}) | ||
|
||
return { | ||
data: historyPage, | ||
nextCursor: res.docs[res.docs.length - 1]?.id || undefined | ||
} | ||
} | ||
} | ||
|
||
export default BookingHistoryService |