I am trying to build a Booking Mechanism which does not allow that a user has two overlapping bookings. A User has to input a Date Range for a booking, which gets stored in firebase (Cloud Firestore). Before the booking goes through I want to check if it is overlapping with another booking of this user.
I store these two variables for each booking request, which I want to use for this query:
selectedStartDate (Timestamp) selectedEndDate (Timestamp)
What I try what doesn't work:
bool overlappingBookingDateCheck(currentUserId, selectedDate) {
bool unavailable = false;
StreamBuilder(
stream: FirebaseFirestore.instance
.collection('users')
.doc(currentUserId)
.collection('bookings')
.where("status", isEqualTo: "active")
.where("selectedStartDate",
isGreaterThanOrEqualTo: selectedDate.start)
.where("selectedEndDate", isLessThanOrEqualTo: selectedDate.end)
.snapshots(),
builder: (ctx, streamSnapshot) {
if (streamSnapshot.connectionState == ConnectionState.waiting) {
return Center(
child: CircularProgressIndicator(),
);
}
// If there was a match in firebase, then there is an
// overlapping booking, so return unavaiable is true
if (streamSnapshot.hasData) {
unavailable = true;
}
});
return unavailable;
}
There are several Problems I have with this code. One is that the multiple where statements are AND and not OR. This prevents me from checking if the start date OR the end date is overlapping with another booking. In both cases the booking should be denied. The second is that I get an error that is is not possible to filter multiple fields with inequality operators (<, <=, >, or >=).
Would be very happy if anyone can help me out how I get the query that I need (checking if the requested booking daterange overlapps in anyway with an existings booking).
Thank You!!