From 4ba4de07fdec03a94fc14e7d824c967c717904f4 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Laur=C3=A9line=20Gu=C3=A9rin?= <zebuline@entrouvert.com>
Date: Tue, 29 Oct 2019 14:30:12 +0100
Subject: [PATCH 2/2] api: return places info on fillslot api for an event
 agenda (#35297)

---
 chrono/api/views.py | 27 ++++++++++++++++++---------
 tests/test_api.py   | 43 +++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 61 insertions(+), 9 deletions(-)

diff --git a/chrono/api/views.py b/chrono/api/views.py
index 15cb364..9991c19 100644
--- a/chrono/api/views.py
+++ b/chrono/api/views.py
@@ -124,6 +124,19 @@ def get_agenda_detail(request, agenda):
     return agenda_detail
 
 
+def get_event_places(event):
+    places = {
+        'total': event.places,
+        'reserved': event.booked_places,
+        'available': event.places - event.booked_places,
+    }
+    if event.waiting_list_places:
+        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)
+    return places
+
+
 class Agendas(APIView):
     permission_classes = ()
 
@@ -338,6 +351,7 @@ class Fillslots(APIView):
                              format=format)
 
     def fillslot(self, request, agenda_identifier=None, slots=[], format=None):
+        multiple_booking = not slots
         try:
             agenda = Agenda.objects.get(slug=agenda_identifier)
         except Agenda.DoesNotExist:
@@ -529,6 +543,9 @@ class Fillslots(APIView):
                 'slug': available_desk.slug}
         if to_cancel_booking:
             response['cancelled_booking_id'] = cancelled_booking_id
+        if agenda.kind == 'events' and not multiple_booking:
+            event = events[0]
+            response['places'] = get_event_places(event)
 
         return Response(response)
 
@@ -614,16 +631,8 @@ class SlotStatus(APIView):
         event = get_object_or_404(Event, id=event_pk)
         response = {
             'err': 0,
-            'places': {
-                'total': event.places,
-                'reserved': event.booked_places,
-                'available': event.places - event.booked_places,
-            }
+            'places': get_event_places(event),
         }
-        if event.waiting_list_places:
-            response['places']['waiting_list_total'] = event.waiting_list_places
-            response['places']['waiting_list_reserved'] = event.waiting_list
-            response['places']['waiting_list_available'] = (event.waiting_list_places - event.waiting_list)
         return Response(response)
 
 slot_status = SlotStatus.as_view()
diff --git a/tests/test_api.py b/tests/test_api.py
index 78e6d40..ddc95ba 100644
--- a/tests/test_api.py
+++ b/tests/test_api.py
@@ -736,6 +736,49 @@ def test_booking_api_with_data(app, some_data, user):
     assert Booking.objects.all()[0].extra_data == {'hello': 'world'}
 
 
+def test_booking_api_available(app, some_data, meetings_agenda, user):
+    agenda = Agenda.objects.filter(label=u'Foo bar')[0]
+    event = Event.objects.filter(agenda=agenda)[0]
+
+    app.authorization = ('Basic', ('john.doe', 'password'))
+    resp = app.post_json('/api/agenda/%s/fillslot/%s/' % (agenda.pk, event.pk))
+    assert resp.json['err'] == 0
+    assert resp.json['places']['total'] == 10
+    assert resp.json['places']['available'] == 9
+    assert resp.json['places']['reserved'] == 1
+    assert 'waiting_list_total' not in resp.json['places']
+
+    Booking.objects.create(event=event, in_waiting_list=True)
+    event.waiting_list_places = 5
+    event.save()
+
+    resp = app.post_json('/api/agenda/%s/fillslot/%s/' % (agenda.pk, event.pk))
+    assert resp.json['err'] == 0
+    assert resp.json['places']['total'] == 10
+    assert resp.json['places']['available'] == 9
+    assert resp.json['places']['reserved'] == 1
+    assert resp.json['places']['waiting_list_total'] == 5
+    assert resp.json['places']['waiting_list_available'] == 3
+    assert resp.json['places']['waiting_list_reserved'] == 2
+
+    # not for mettings agenda
+    meeting_type = MeetingType.objects.get(agenda=meetings_agenda)
+    resp = app.get('/api/agenda/meetings/%s/datetimes/' % meeting_type.pk)
+    event_id = resp.json['data'][2]['id']
+    resp = app.post_json('/api/agenda/%s/fillslot/%s/' % (meetings_agenda.pk, event_id))
+    assert resp.json['err'] == 0
+    assert 'places' not in resp.json
+
+    # not for multiple booking
+    events = [x for x in Event.objects.filter(agenda=agenda).order_by('start_datetime') if x.in_bookable_period()][:2]
+    slots = [x.pk for x in events]
+
+    resp = app.post('/api/agenda/%s/fillslots/' % agenda.slug,
+                    params={'slots': slots, 'count': '3'})
+    assert resp.json['err'] == 0
+    assert 'places' not in resp.json
+
+
 def test_booking_api_with_cancel_booking(app, some_data, user):
     agenda_id = Agenda.objects.filter(label=u'Foo bar')[0].id
     event_0, event_1, event_2, event_3 = Event.objects.filter(agenda_id=agenda_id)[0:4]
-- 
2.20.1

