Skip to content

Commit

Permalink
Merge pull request #76 from noQ-sweden/569-BUG-Booking-that-starts-in…
Browse files Browse the repository at this point in the history
…-the-past-can't-be-approved
  • Loading branch information
natischmidt authored Nov 6, 2024
2 parents 60260f6 + e34204e commit 4870d9e
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 10 deletions.
19 changes: 17 additions & 2 deletions noq_django/rest_api/api/host_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,16 @@ def get_host_data(request):
def count_bookings(request):
host = Host.objects.get(users=request.user)

# Get current date
current_date = timezone.now().date()

# Count only bookings that have a start date today or in the future
pending_count = Booking.objects.filter(
product__host=host,
status__description__in=['pending', 'advised_against', 'accepted']).count()
status__description__in=['pending', 'advised_against', 'accepted'],
start_date__gte=current_date # Only count bookings with start dates today or in the future
).count()

arrivals_count = Booking.objects.filter(
product__host=host,
start_date=date.today()
Expand Down Expand Up @@ -155,7 +162,15 @@ def get_pending_bookings(request, limiter: Optional[
int] = None): # Limiter example /pending?limiter=10 for 10 results, empty returns all
host = Host.objects.get(users=request.user)
status_list = ['pending', 'accepted', 'advised_against']
bookings = Booking.objects.filter(product__host=host, status__description__in=status_list)

# Get current date
current_date = timezone.now().date()

bookings = Booking.objects.filter(
product__host=host,
status__description__in=status_list,
start_date__gte=current_date
)

if limiter is not None and limiter > 0:
return bookings[:limiter]
Expand Down
51 changes: 43 additions & 8 deletions noq_django/rest_api/api/test/test_host_handle_booking_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,11 @@ def host_login(self):
self.client.login(username=self.host_name, password=self.password)
print(self.client)



def delete_bookings(self):
Booking.objects.all().delete()


def delete_users(self):
# Delete the host_user created for the tests
users = User.objects.all().delete()
Expand Down Expand Up @@ -101,9 +105,13 @@ def setUp(self):
)
booking_status.save()

# Create pending bookings
# 1 product x 4 guests = 4 bookings
# Create host and user association (only need to do this once)
host = Host.objects.get(name="Host 1")
host_user = User.objects.get(username="user.host@test.nu")
host.users.add(host_user)
host.save()

# Create pending bookings
host_products = Product.objects.filter(host=host)
clients = Client.objects.all()
start_date = datetime.now()
Expand All @@ -124,11 +132,6 @@ def setUp(self):


def test_batch_accept_bookings(self):
# Connect host_user and host
host = Host.objects.get(name="Host 1")
host_user = User.objects.get(username="user.host@test.nu")
host.users.add(host_user)
host.save()

# There should be 4 pending bookings to start with
bookings = Booking.objects.filter(status=State.PENDING).all()
Expand Down Expand Up @@ -156,8 +159,40 @@ def test_batch_accept_bookings(self):
parsed_response = json.loads(response.content)
self.assertEqual(len(parsed_response), 0)

def test_pending_bookings_exclude_past_start_date(self):

# Create one booking with a past start date
Booking.objects.bulk_create([
Booking(
start_date=datetime.now().date() - timedelta(days=1), # Past start date
end_date=datetime.now().date() + timedelta(days=1),
product=Product.objects.get(name="room"),
user=Client.objects.first(),
status=BookingStatus.objects.get(id=State.PENDING)
),
Booking(
start_date=datetime.now().date() + timedelta(days=1), # Future start date
end_date=datetime.now().date() + timedelta(days=2),
product=Product.objects.get(name="room"),
user=Client.objects.last(),
status=BookingStatus.objects.get(id=State.PENDING)
)
])
# Check the number of pending bookings in the database
pending_bookings_count = Booking.objects.filter(status=State.PENDING).count()
self.assertEqual(pending_bookings_count, 6)

# Call the API to retrieve pending bookings
response = self.client.get("/api/host/pending")
self.assertEqual(response.status_code, 200)

# Parse response and verify that only the future booking is included
bookings = json.loads(response.content)
self.assertEqual(len(bookings), 5) # Only 5 bookings should be returned (The 4 bookings created in the set up function plus the one booking that has a future start date)


def tearDown(self):
# After the tests delete all data generated for the tests
self.delete_bookings()
self.delete_products()
self.delete_users()

0 comments on commit 4870d9e

Please sign in to comment.