From 5fc9d85ef71ce4fdaaece2d03866520a017337aa Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Laur=C3=A9line=20Gu=C3=A9rin?= <zebuline@entrouvert.com>
Date: Thu, 27 Feb 2020 14:30:30 +0100
Subject: [PATCH] toulouse_axel: new endpoint clae_children_activities_info
 (#40229)

---
 passerelle/contrib/toulouse_axel/models.py | 23 +++++-
 tests/test_toulouse_axel.py                | 95 ++++++++++++++++++++++
 2 files changed, 117 insertions(+), 1 deletion(-)

diff --git a/passerelle/contrib/toulouse_axel/models.py b/passerelle/contrib/toulouse_axel/models.py
index 78c20417..9f162adb 100644
--- a/passerelle/contrib/toulouse_axel/models.py
+++ b/passerelle/contrib/toulouse_axel/models.py
@@ -1014,7 +1014,28 @@ class ToulouseAxel(BaseResource):
         return {child_id: bool(child.get('ACTIVITE', [])) for child_id, child in children_activities.items()}
 
     @endpoint(
-        description=_("Get information about CLAE booking"),
+        description=_("Get information about CLAE activities for all children"),
+        perm='can_access',
+        parameters={
+            'NameID': {'description': _('Publik ID')},
+            'booking_date': {'description': _('Booking date')},
+        })
+    def clae_children_activities_info(self, request, NameID, booking_date):
+        link = self.get_link(NameID)
+        try:
+            booking_date = datetime.datetime.strptime(booking_date, utils.json_date_format)
+        except ValueError:
+            raise APIError('bad date format', err_code='bad-request', http_status=400)
+
+        reference_year = utils.get_reference_year_from_date(booking_date)
+        children_activities = self.get_children_activities(dui=link.dui, reference_year=reference_year)
+        for child in children_activities.values():
+            child['id'] = child['IDPERSONNE']
+            child['text'] = '{} {}'.format(child['PRENOM'], child['NOM']).strip()
+        return {'data': list(children_activities.values())}
+
+    @endpoint(
+        description=_("Get information about CLAE booking for a child"),
         perm='can_access',
         parameters={
             'NameID': {'description': _('Publik ID')},
diff --git a/tests/test_toulouse_axel.py b/tests/test_toulouse_axel.py
index 2bc2dbb2..e10894dd 100644
--- a/tests/test_toulouse_axel.py
+++ b/tests/test_toulouse_axel.py
@@ -1931,6 +1931,101 @@ def test_pay_invoice_endpoint(app, resource):
     }
 
 
+def test_clae_children_activities_info_endpoint_axel_error(app, resource):
+    Link.objects.create(resource=resource, name_id='yyy', dui='XXX', person_id='42')
+    with mock.patch('passerelle.contrib.toulouse_axel.models.enfants_activites') as operation:
+        operation.side_effect = AxelError('FooBar')
+        resp = app.get('/toulouse-axel/test/clae_children_activities_info?NameID=yyy&booking_date=2020-01-20')
+    assert resp.json['err_desc'] == "Axel error: FooBar"
+    assert resp.json['err'] == 'error'
+
+
+@pytest.mark.parametrize('value', ['foo', '20/01/2020', '2020'])
+def test_clae_children_activities_info_endpoint_bad_date_format(app, resource, value):
+    Link.objects.create(resource=resource, name_id='yyy', dui='XXX', person_id='42')
+    resp = app.get('/toulouse-axel/test/clae_children_activities_info?NameID=yyy&booking_date=%s' % value, status=400)
+    assert resp.json['err_desc'] == "bad date format"
+    assert resp.json['err'] == 'bad-request'
+
+
+def test_clae_children_activities_info_endpoint_no_result(app, resource, child_activities_data):
+    resp = app.get('/toulouse-axel/test/clae_children_activities_info?NameID=yyy&booking_date=2020-01-20')
+    assert resp.json['err_desc'] == "Person not found"
+    assert resp.json['err'] == 'not-found'
+
+
+def test_clae_children_activities_info_endpoint(app, resource, child_activities_data):
+    Link.objects.create(resource=resource, name_id='yyy', dui='XXX', person_id='42')
+    filepath = os.path.join(os.path.dirname(__file__), 'data/toulouse_axel/child_activities.xml')
+    with open(filepath) as xml:
+        content = xml.read()
+    with mock_getdata(content, 'EnfantsActivites'):
+        resp = app.get('/toulouse-axel/test/clae_children_activities_info?NameID=yyy&booking_date=2020-01-20')
+    assert resp.json['err'] == 0
+    assert resp.json['data'] == [
+        {
+            'ACTIVITE': [
+                {
+                    'COUTREVIENT': '99999',
+                    'DATEDEBUT': '2019-08-01',
+                    'DATEENTREE': '2019-08-01',
+                    'DATEFIN': '2020-07-31',
+                    'DATESORTIE': '2020-07-31',
+                    'IDACTIVITE': 'A19P1M1',
+                    'ISPAI': False,
+                    'LIBELLEACTIVITE': 'Temps du matin',
+                    'TARIF': '0.42',
+                    'TYPEACTIVITE': 'MAT'
+                },
+                {
+                    'COUTREVIENT': '99999',
+                    'DATEDEBUT': '2019-08-01',
+                    'DATEENTREE': '2019-08-01',
+                    'DATEFIN': '2020-07-31',
+                    'DATESORTIE': '2020-07-31',
+                    'IDACTIVITE': 'A19P1M2',
+                    'ISPAI': False,
+                    'LIBELLEACTIVITE': 'Temps du midi',
+                    'TARIF': '0.43',
+                    'TYPEACTIVITE': 'MIDI'
+                },
+                {
+                    'COUTREVIENT': '99999',
+                    'DATEDEBUT': '2019-08-01',
+                    'DATEENTREE': '2019-08-01',
+                    'DATEFIN': '2020-07-31',
+                    'DATESORTIE': '2020-07-31',
+                    'IDACTIVITE': 'A19P1M3',
+                    'ISPAI': False,
+                    'LIBELLEACTIVITE': 'Temps du soir',
+                    'TARIF': '0.44',
+                    'TYPEACTIVITE': 'SOIR'
+                },
+                {
+                    'COUTREVIENT': '99999',
+                    'DATEDEBUT': '2019-08-01',
+                    'DATEENTREE': '2019-08-01',
+                    'DATEFIN': '2020-07-31',
+                    'DATESORTIE': '2020-07-31',
+                    'IDACTIVITE': 'A19P1M4',
+                    'ISPAI': False,
+                    'LIBELLEACTIVITE': 'Temps mercredi apr\xe8s midi',
+                    'TARIF': '0.45',
+                    'TYPEACTIVITE': 'GARD'
+                }
+            ],
+            'DATENAISSANCE': '2019-01-01',
+            'IDPERSONNE': '3535',
+            'LIBELLEECOLE': 'MATERNELLE 1',
+            'NOM': 'foo',
+            'PRENOM': 'foo',
+            'REGIME': 'SV',
+            'id': '3535',
+            'text': 'foo foo'
+        }
+    ]
+
+
 def test_clae_booking_info_endpoint_axel_error(app, resource):
     Link.objects.create(resource=resource, name_id='yyy', dui='XXX', person_id='42')
     with mock.patch('passerelle.contrib.toulouse_axel.models.enfants_activites') as operation:
-- 
2.20.1

