Skip to content

Commit

Permalink
add files 🤡
Browse files Browse the repository at this point in the history
  • Loading branch information
choden-dev committed Aug 2, 2024
1 parent b23ac44 commit 30a9225
Showing 1 changed file with 78 additions and 0 deletions.
78 changes: 78 additions & 0 deletions server/src/data-layer/services/BookingHistoryService.ts
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

0 comments on commit 30a9225

Please sign in to comment.