From 52481fc1ade7257a9cae9170cd35e72486914af4 Mon Sep 17 00:00:00 2001 From: bcho892 Date: Wed, 31 Jul 2024 11:36:51 +1200 Subject: [PATCH 1/4] define and document required interfaces --- server/src/data-layer/models/firebase.ts | 76 ++++++++++++++++++++++++ 1 file changed, 76 insertions(+) diff --git a/server/src/data-layer/models/firebase.ts b/server/src/data-layer/models/firebase.ts index 98b05a206..0683ea693 100644 --- a/server/src/data-layer/models/firebase.ts +++ b/server/src/data-layer/models/firebase.ts @@ -187,3 +187,79 @@ export interface Event { */ max_occupancy?: number } + +/** + * Base type to be able to log events that admins perform on booking related resources in the admin view + */ +interface BookingHistory { + /** + * The time which the booking operation was performed. MUST be in UTC format + */ + timestamp: Timestamp + + /** + * The start of the operated on date range + */ + start_date: Timestamp + + /** + * The end of the operated on date range + */ + end_date: Timestamp + + /** + * The type of event that the admin performed, used for parsing on the front-end + * + * Each of these are associated with the following: + * + * - `"added_user_to_booking"`: {@link BookingAddedEvent} + * - `"removed_user_from_booking"`: {@link BookingDeletedEvent} + * - `"changed_date_availability"`: {@link BookingAvailabilityChangeEvent} + */ + event_type: + | "added_user_to_booking" + | "removed_user_from_booking" + | "changed_date_availability" +} + +/** + * Event used to track a user being **manually** added to a booking (only possible via admin view) + * + * @extends BookingHistory {@link BookingHistory} + */ +export interface BookingAddedEvent extends BookingHistory { + /** + * The id corresponding to the user who had a **manually** added booking + */ + uid: string +} + +/** + * Event used to track the removal of a user from a date range (only possible via admin view) + * + * @extends BookingHistory {@link BookingHistory} + */ +export interface BookingDeletedEvent extends BookingHistory { + /** + * The id corresponding to the user who had a **manually** deleted booking + */ + uid: string +} + +/** + * Event used to track the history of the availability of dates changing + * + * @extends BookingHistory {@link BookingHistory} + */ +export interface BookingAvailabilityChangeEvent extends BookingHistory { + /** + * The **signed** difference between the newly available slots and the previously available slots. + * + * For example, if the original available slots was 32, and the availability was set to 0, + * the `change` in the slots needs to be **0 - 32 = -32** + * + * And vice versa, if the original available slots was 16, and the availability was set to 32, + * the `change` would be **32 - 16 = 16** + */ + change: number +} From 0cc76326f658309a7e230b4850960e7290d3020c Mon Sep 17 00:00:00 2001 From: bcho892 Date: Wed, 31 Jul 2024 21:37:31 +1200 Subject: [PATCH 2/4] create bookingHistory collection --- server/src/data-layer/adapters/FirestoreCollections.ts | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/server/src/data-layer/adapters/FirestoreCollections.ts b/server/src/data-layer/adapters/FirestoreCollections.ts index 65c6a2bdb..bc2561848 100644 --- a/server/src/data-layer/adapters/FirestoreCollections.ts +++ b/server/src/data-layer/adapters/FirestoreCollections.ts @@ -2,6 +2,9 @@ import "dotenv/config" import { Booking, + BookingAddedEvent, + BookingAvailabilityChangeEvent, + BookingDeletedEvent, BookingSlot, UserAdditionalInfo } from "data-layer/models/firebase" @@ -29,7 +32,10 @@ const firestore = Object.assign( const db = { users: firestore.collection("users"), bookings: firestore.collection("bookings"), - bookingSlots: firestore.collection("booking_slots") + bookingSlots: firestore.collection("booking_slots"), + bookingHistory: firestore.collection< + BookingAddedEvent | BookingDeletedEvent | BookingAvailabilityChangeEvent + >("booking_history") } as const export default db From dbbf33bcaefc92944011ab22e335061593a88ef3 Mon Sep 17 00:00:00 2001 From: bcho892 Date: Fri, 2 Aug 2024 13:20:39 +1200 Subject: [PATCH 3/4] add required types to children --- server/src/data-layer/models/firebase.ts | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/server/src/data-layer/models/firebase.ts b/server/src/data-layer/models/firebase.ts index 0683ea693..f6145bd3a 100644 --- a/server/src/data-layer/models/firebase.ts +++ b/server/src/data-layer/models/firebase.ts @@ -232,6 +232,7 @@ export interface BookingAddedEvent extends BookingHistory { * The id corresponding to the user who had a **manually** added booking */ uid: string + event_type: "added_user_to_booking" } /** @@ -244,6 +245,7 @@ export interface BookingDeletedEvent extends BookingHistory { * The id corresponding to the user who had a **manually** deleted booking */ uid: string + event_type: "removed_user_from_booking" } /** @@ -262,4 +264,13 @@ export interface BookingAvailabilityChangeEvent extends BookingHistory { * the `change` would be **32 - 16 = 16** */ change: number + event_type: "changed_date_availability" } + +/** + * Helper type to specify the possible datastruces for the booking history + */ +export type BookingHistoryEvent = + | BookingAddedEvent + | BookingDeletedEvent + | BookingAvailabilityChangeEvent From cfbe8634cc9d3892ce9c0db1e06c31246da925f3 Mon Sep 17 00:00:00 2001 From: bcho892 Date: Fri, 2 Aug 2024 19:27:12 +1200 Subject: [PATCH 4/4] update collection to use util tye --- server/src/data-layer/adapters/FirestoreCollections.ts | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/server/src/data-layer/adapters/FirestoreCollections.ts b/server/src/data-layer/adapters/FirestoreCollections.ts index bc2561848..646e7deca 100644 --- a/server/src/data-layer/adapters/FirestoreCollections.ts +++ b/server/src/data-layer/adapters/FirestoreCollections.ts @@ -2,9 +2,7 @@ import "dotenv/config" import { Booking, - BookingAddedEvent, - BookingAvailabilityChangeEvent, - BookingDeletedEvent, + BookingHistoryEvent, BookingSlot, UserAdditionalInfo } from "data-layer/models/firebase" @@ -33,9 +31,7 @@ const db = { users: firestore.collection("users"), bookings: firestore.collection("bookings"), bookingSlots: firestore.collection("booking_slots"), - bookingHistory: firestore.collection< - BookingAddedEvent | BookingDeletedEvent | BookingAvailabilityChangeEvent - >("booking_history") + bookingHistory: firestore.collection("booking_history") } as const export default db