From 8428ad7386db5ae933e557ba4a954d32a8a60129 Mon Sep 17 00:00:00 2001
From: Nicolas ROCHE <nroche@entrouvert.com>
Date: Wed, 23 Nov 2022 19:08:17 +0100
Subject: [PATCH 3/3] toulouse-maelis: manage emergency person list on
 createFamily (#71646)

---
 passerelle/contrib/toulouse_maelis/models.py  |  5 ++++
 passerelle/contrib/toulouse_maelis/schemas.py | 21 +---------------
 tests/test_toulouse_maelis.py                 | 24 +++++++++----------
 3 files changed, 18 insertions(+), 32 deletions(-)

diff --git a/passerelle/contrib/toulouse_maelis/models.py b/passerelle/contrib/toulouse_maelis/models.py
index b42f6d68..31eb3ed1 100644
--- a/passerelle/contrib/toulouse_maelis/models.py
+++ b/passerelle/contrib/toulouse_maelis/models.py
@@ -623,16 +623,21 @@ class ToulouseMaelis(BaseResource, HTTPResource):
         parameters={'NameID': {'description': 'Publik NameID'}},
         post={'request_body': {'schema': {'application/json': schemas.UPDATE_FAMILY_SCHEMA}}},
     )
     def update_family(self, request, NameID, post_data):
         family_id = self.get_link(NameID).family_id
         self.assert_family_payload_in_referential(post_data)
         self.replace_null_values(post_data)
 
+        # adapt payload to use same input as create_family
+        if len(post_data.get('emergencyPersonList', [])):
+            persons = post_data.pop('emergencyPersonList')
+            post_data['emergencyPersonList'] = [{'personList': persons}]
+
         response = self.call('Family', 'updateFamily', dossierNumber=family_id, **post_data)
         data = serialize_object(response)
         family_id = data.get('number')
         errors = data.get('childErrorList')
         if errors:
             err_codes = [x.split(':')[0][:4] for x in errors]
             raise APIError(' ; '.join(errors), err_code=', '.join(err_codes))
         return {'data': data}
diff --git a/passerelle/contrib/toulouse_maelis/schemas.py b/passerelle/contrib/toulouse_maelis/schemas.py
index 5b88784f..a15411c6 100644
--- a/passerelle/contrib/toulouse_maelis/schemas.py
+++ b/passerelle/contrib/toulouse_maelis/schemas.py
@@ -117,35 +117,16 @@ EMERGENCY_PERSON_SCHEMA = {
         },
     },
     'unflatten': True,
     'additionalProperties': False,
 }
 EMERGENCY_PERSON_SCHEMA['properties'].update(PERSON_PROPERTIES)
 
 
-EMERGENCY_PERSON_LIST_SCHEMA = {
-    '$schema': 'http://json-schema.org/draft-04/schema#',
-    'title': 'Family persons',
-    'description': "Liste des personnes à prévenir en cas d'urgence",
-    'type': 'object',
-    'properties': {
-        'personList': {
-            'oneOf': [
-                {
-                    'type': 'array',
-                    'items': EMERGENCY_PERSON_SCHEMA,
-                },
-                {'type': 'null'},
-            ],
-        },
-    },
-}
-
-
 AUTHORIZED_PERSON_SCHEMA = {
     '$schema': 'http://json-schema.org/draft-04/schema#',
     'title': 'Family persons',
     'description': "Personnes autorisées à venir chercher l'enfant",
     'type': 'object',
     'required': ['personInfo', 'personQuality'],
     'properties': {
         'personInfo': {
@@ -600,17 +581,17 @@ UPDATE_FAMILY_SCHEMA = {
             'oneOf': [{'type': 'string'}, {'type': 'null'}],
         },
         'rl1': RLINFO_SCHEMA,
         'rl2': RLINFO_SCHEMA,
         'emergencyPersonList': {
             'oneOf': [
                 {
                     'type': 'array',
-                    'items': EMERGENCY_PERSON_LIST_SCHEMA,
+                    'items': EMERGENCY_PERSON_SCHEMA,
                 },
                 {'type': 'null'},
             ],
         },
         'childList': {
             'oneOf': [
                 {
                     'type': 'array',
diff --git a/tests/test_toulouse_maelis.py b/tests/test_toulouse_maelis.py
index 12ee5619..256e755a 100644
--- a/tests/test_toulouse_maelis.py
+++ b/tests/test_toulouse_maelis.py
@@ -1247,22 +1247,22 @@ def test_update_family(mocked_post, mocked_get, con, app):
         'rl1/birth/dateBirth': '1938-07-26',
         'rl1/birth/place': 'Rabbat',
         'rl1/adresse/street1': 'Chateau',
         'rl1/adresse/town': 'Paris',
         'rl1/adresse/zipcode': '75014',
         'rl1/adresse/numComp': 'B',
         'rl1/profession/codeCSP': 'ART',
         'rl1/CAFInfo/organ': 'A10007752822',
-        'emergencyPersonList/0/personList/0/civility': 'MME',
-        'emergencyPersonList/0/personList/0/firstname': 'Keny',
-        'emergencyPersonList/0/personList/0/lastname': 'Arkana',
-        'emergencyPersonList/0/personList/0/sexe': 'F',
-        'emergencyPersonList/0/personList/0/dateBirth': '1982-12-20',
-        'emergencyPersonList/0/personList/0/quality': 'T',
+        'emergencyPersonList/0/civility': 'MME',
+        'emergencyPersonList/0/firstname': 'Keny',
+        'emergencyPersonList/0/lastname': 'Arkana',
+        'emergencyPersonList/0/sexe': 'F',
+        'emergencyPersonList/0/dateBirth': '1982-12-20',
+        'emergencyPersonList/0/quality': 'T',
         'childList/0/lastname': 'Zimmerman',  # add child
         'childList/0/firstname': 'Robert',
         'childList/0/sexe': 'M',
         'childList/0/birth/dateBirth': '1941-05-24',
         'childList/0/birth/place': 'Duluth',
         'childList/0/dietcode': 'RSV',
         'childList/0/paiInfoBean/code': 'PAIALI',
         'childList/0/medicalRecord/vaccinList/0/code': 'DTC',
@@ -1382,22 +1382,22 @@ def test_update_family_wrong_referential_key_error(mocked_post, mocked_get, con,
         'rl1/birth/dateBirth': '1938-07-26',
         'rl1/birth/place': 'Rabbat',
         'rl1/adresse/street1': 'Chateau',
         'rl1/adresse/town': 'Paris',
         'rl1/adresse/zipcode': '75014',
         'rl1/adresse/numComp': 'B',
         'rl1/profession/codeCSP': 'ART',
         'rl1/CAFInfo/organ': 'A10007752822',
-        'emergencyPersonList/0/personList/0/civility': 'MME',
-        'emergencyPersonList/0/personList/0/firstname': 'Keny',
-        'emergencyPersonList/0/personList/0/lastname': 'Arkana',
-        'emergencyPersonList/0/personList/0/sexe': 'F',
-        'emergencyPersonList/0/personList/0/dateBirth': '1982-12-20',
-        'emergencyPersonList/0/personList/0/quality': 'T',
+        'emergencyPersonList/0/civility': 'MME',
+        'emergencyPersonList/0/firstname': 'Keny',
+        'emergencyPersonList/0/lastname': 'Arkana',
+        'emergencyPersonList/0/sexe': 'F',
+        'emergencyPersonList/0/dateBirth': '1982-12-20',
+        'emergencyPersonList/0/quality': 'T',
         'childList/0/lastname': 'Zimmerman',
         'childList/0/firstname': 'Robert',
         'childList/0/sexe': 'M',
         'childList/0/birth/dateBirth': '1941-05-24',
         'childList/0/birth/place': 'Duluth',
         'childList/0/dietcode': 'RSV',
         'childList/0/paiInfoBean/code': 'PAIALI',
         'childList/0/medicalRecord/vaccinList/0/code': 'plop',
-- 
2.30.2

