From 2748439c23ef3a574e0fb14ce401a125dc63be40 Mon Sep 17 00:00:00 2001
From: Emmanuel Cazenave <ecazenave@entrouvert.com>
Date: Wed, 4 Mar 2020 17:25:45 +0100
Subject: [PATCH] api: add more infos on places for an event agenda (#40412)

---
 chrono/api/views.py | 11 ++++++++++-
 tests/test_api.py   | 11 +++++++++++
 2 files changed, 21 insertions(+), 1 deletion(-)

diff --git a/chrono/api/views.py b/chrono/api/views.py
index dd35b96..d843bd7 100644
--- a/chrono/api/views.py
+++ b/chrono/api/views.py
@@ -176,15 +176,22 @@ def get_agenda_detail(request, agenda):
 
 
 def get_event_places(event):
+    available = event.places - event.booked_places
     places = {
         'total': event.places,
         'reserved': event.booked_places,
-        'available': event.places - event.booked_places,
+        'available': available,
+        'full': event.full,
+        'has_waiting_list': False,
     }
     if event.waiting_list_places:
+        places['has_waiting_list'] = True
         places['waiting_list_total'] = event.waiting_list_places
         places['waiting_list_reserved'] = event.waiting_list
         places['waiting_list_available'] = event.waiting_list_places - event.waiting_list
+        places['waiting_list_activated'] = event.waiting_list > 0 or available <= 0
+        # 'waiting_list_activated' means next booking will go into the waiting list
+
     return places
 
 
@@ -766,6 +773,8 @@ class Fillslots(APIView):
             response['cancelled_booking_id'] = cancelled_booking_id
         if agenda.kind == 'events' and not multiple_booking:
             event = events[0]
+            # event.full is not up to date, it might have been changed by previous new_booking.save().
+            event.refresh_from_db()
             response['places'] = get_event_places(event)
         if agenda.kind == 'events' and multiple_booking:
             response['events'] = [
diff --git a/tests/test_api.py b/tests/test_api.py
index 913ddac..d694107 100644
--- a/tests/test_api.py
+++ b/tests/test_api.py
@@ -971,6 +971,7 @@ def test_booking_api_available(app, some_data, meetings_agenda, user):
     assert resp.json['places']['total'] == 10
     assert resp.json['places']['available'] == 9
     assert resp.json['places']['reserved'] == 1
+    assert resp.json['places']['full'] is False
     assert 'waiting_list_total' not in resp.json['places']
 
     Booking.objects.create(event=event, in_waiting_list=True)
@@ -982,9 +983,11 @@ def test_booking_api_available(app, some_data, meetings_agenda, user):
     assert resp.json['places']['total'] == 10
     assert resp.json['places']['available'] == 9
     assert resp.json['places']['reserved'] == 1
+    assert resp.json['places']['full'] is False
     assert resp.json['places']['waiting_list_total'] == 5
     assert resp.json['places']['waiting_list_available'] == 3
     assert resp.json['places']['waiting_list_reserved'] == 2
+    assert resp.json['places']['waiting_list_activated'] is True
 
     # not for mettings agenda
     meeting_type = MeetingType.objects.get(agenda=meetings_agenda)
@@ -1029,9 +1032,11 @@ def test_booking_api_force_waiting_list(app, some_data, user):
     assert resp.json['places']['total'] == 10
     assert resp.json['places']['available'] == 9
     assert resp.json['places']['reserved'] == 1
+    assert resp.json['places']['full'] is False
     assert resp.json['places']['waiting_list_total'] == 2
     assert resp.json['places']['waiting_list_available'] == 2
     assert resp.json['places']['waiting_list_reserved'] == 0
+    assert resp.json['places']['waiting_list_activated'] is False
 
     # add another booking
     resp = app.post_json(
@@ -1041,9 +1046,11 @@ def test_booking_api_force_waiting_list(app, some_data, user):
     assert resp.json['places']['total'] == 10
     assert resp.json['places']['available'] == 8
     assert resp.json['places']['reserved'] == 2
+    assert resp.json['places']['full'] is False
     assert resp.json['places']['waiting_list_total'] == 2
     assert resp.json['places']['waiting_list_available'] == 2
     assert resp.json['places']['waiting_list_reserved'] == 0
+    assert resp.json['places']['waiting_list_activated'] is False
 
     # add a booking, but in waiting list
     resp = app.post_json(
@@ -1053,9 +1060,11 @@ def test_booking_api_force_waiting_list(app, some_data, user):
     assert resp.json['places']['total'] == 10
     assert resp.json['places']['available'] == 8
     assert resp.json['places']['reserved'] == 2
+    assert resp.json['places']['full'] is False
     assert resp.json['places']['waiting_list_total'] == 2
     assert resp.json['places']['waiting_list_available'] == 1
     assert resp.json['places']['waiting_list_reserved'] == 1
+    assert resp.json['places']['waiting_list_activated'] is True
 
     # add a booking => booked in waiting list
     resp = app.post_json('/api/agenda/%s/fillslot/%s/' % (agenda.pk, event.pk))
@@ -1063,9 +1072,11 @@ def test_booking_api_force_waiting_list(app, some_data, user):
     assert resp.json['places']['total'] == 10
     assert resp.json['places']['available'] == 8
     assert resp.json['places']['reserved'] == 2
+    assert resp.json['places']['full'] is True
     assert resp.json['places']['waiting_list_total'] == 2
     assert resp.json['places']['waiting_list_available'] == 0
     assert resp.json['places']['waiting_list_reserved'] == 2
+    assert resp.json['places']['waiting_list_activated'] is True
 
     # waiting list is full
     resp = app.post_json(
-- 
2.20.1

