From 91549e3080699d6098d40f7951136d1b0ce21651 Mon Sep 17 00:00:00 2001
From: Nicolas ROCHE <nroche@entrouvert.com>
Date: Fri, 25 Nov 2022 08:00:30 +0100
Subject: [PATCH] toulouse-maelis: manage child insurance (#71696)

---
 functests/toulouse_maelis/conftest.py         | 12 ++++++-
 .../data/test_create_family.json              |  8 ++++-
 functests/toulouse_maelis/test_family.py      | 21 ++++++++++++
 passerelle/contrib/toulouse_maelis/schemas.py | 32 +++++++++++++++++++
 tests/data/toulouse_maelis/R_read_family.xml  |  7 ++++
 .../toulouse_maelis/R_read_family_relax.xml   |  7 ++++
 .../R_read_family_reordered.xml               |  7 ++++
 tests/test_toulouse_maelis.py                 | 28 +++++++++++++++-
 8 files changed, 119 insertions(+), 3 deletions(-)

diff --git a/functests/toulouse_maelis/conftest.py b/functests/toulouse_maelis/conftest.py
index 9564ed71..81f1be28 100644
--- a/functests/toulouse_maelis/conftest.py
+++ b/functests/toulouse_maelis/conftest.py
@@ -365,30 +365,40 @@ def create_data(request, conn):
     # create family
     create_family_payload = copy.deepcopy(FAMILY_PAYLOAD)
     del create_family_payload['childList'][1]  # without Lisa
     del create_family_payload['rl2']  # without Homer
     create_family_payload['rl1']['lastname'] = lastname
     for child in create_family_payload['childList']:
         child['lastname'] = lastname
 
+    # test insurance here because it cannot be reset
+    create_family_payload['childList'][0]['insurance'] = {
+        'company': 'Total Disaster Insurance',
+        'contractNumber': '123',
+        'memberNumber': '456',
+        'contractStart': '2022-01-01',
+        'contractEnd': '2022-12-31',
+    }
+
     url = conn + '/create-family?NameID=%s' % name_id
     resp = requests.post(url, json=create_family_payload)
     resp.raise_for_status()
     create_result = resp.json()
     assert create_result['err'] == 0
     print('\ncreate DUI: %s' % str(create_result['data']['number']))
     data = diff_family(conn, name_id, 'test_create_family.json')
 
     return {
         'name_id': name_id,  # linked
         'family_id': str(create_result['data']['number']),
+        'family_payload': create_family_payload,
         'lastname': lastname,
         'rl1_num': data['RL1']['num'],
-        'family_payload': create_family_payload,
+        'bart_num': data['childList'][0]['num'],
         'data': data,
     }
 
 
 @pytest.fixture(scope='session')
 def update_data(request, conn):
     name_id = request.config.getoption('--nameid')
     lastname = request.config.getoption('--lastname')
diff --git a/functests/toulouse_maelis/data/test_create_family.json b/functests/toulouse_maelis/data/test_create_family.json
index 12ee696c..c3f5dafc 100644
--- a/functests/toulouse_maelis/data/test_create_family.json
+++ b/functests/toulouse_maelis/data/test_create_family.json
@@ -149,17 +149,23 @@
           },
           {
             "code": "DTC",
             "label": "DIPHTERIE TETANOS COQUELUCHE",
             "vaccinationDate": "2011-01-11T00:00:00+01:00"
           }
         ]
       },
-      "insurance": null,
+      "insurance": {
+        "company": "Total Disaster Insurance",
+        "contractNumber": "123",
+        "memberNumber": "456",
+        "contractStart": "2022-01-01T00:00:00+01:00",
+        "contractEnd": "2022-12-31T00:00:00+01:00"
+      },
       "paiInfoBean": {
         "code": "PAIMED",
         "dateDeb": "2022-09-01T00:00:00+02:00",
         "dateFin": "2023-07-01T00:00:00+02:00",
         "description": "mischievous, rebellious, misunderstood, disruptive",
         "code_text": "MEDICAL"
       },
       "mother": "N/A",
diff --git a/functests/toulouse_maelis/test_family.py b/functests/toulouse_maelis/test_family.py
index c71e5cc3..db5006d4 100644
--- a/functests/toulouse_maelis/test_family.py
+++ b/functests/toulouse_maelis/test_family.py
@@ -547,16 +547,37 @@ def test_update_child(conn, update_data, create_data):
     for key in 'dietcode', 'paiInfoBean', 'medicalRecord', 'authorizedPersonList', 'indicatorList':
         if key in payload:
             del payload[key]
     resp = requests.post(url, json=payload)
     resp.raise_for_status()
     assert resp.json()['err'] == 0
     assert diff_family(conn, update_data['name_id'], 'test_update_family.json')
 
+    # update Bart insurance (on create_data as it cannot be reset)
+    unlink(conn, create_data['name_id'])
+    link(conn, create_data)
+    payload = copy.deepcopy(create_data['family_payload']['childList'][0])
+    for key in 'dietcode', 'paiInfoBean', 'medicalRecord', 'authorizedPersonList', 'indicatorList':
+        if key in payload:
+            del payload[key]
+    payload['insurance'] = {
+        'company': 'Armagedon Colapse',
+        'contractNumber': '444',
+        'memberNumber': '555',
+        'contractStart': '2022-01-02',
+        'contractEnd': '2022-12-31',
+    }
+    url = conn + '/update-child?NameID=%s&child_id=%s' % (create_data['name_id'], create_data['bart_num'])
+    resp = requests.post(url, json=payload)
+    resp.raise_for_status()
+    res = resp.json()
+    assert res['err'] == 0
+    assert diff_child(conn, create_data['name_id'], 0, 'test_update_child_insurance.json', 'insurance')
+
 
 def test_update_child_dietcode(conn, update_data):
     unlink(conn, update_data['name_id'])
     link(conn, update_data)
     url = conn + '/update-child-dietcode?NameID=%s&child_id=%s&dietcode=' % (
         update_data['name_id'],
         update_data['bart_num'],
     )
diff --git a/passerelle/contrib/toulouse_maelis/schemas.py b/passerelle/contrib/toulouse_maelis/schemas.py
index 03854375..87268730 100644
--- a/passerelle/contrib/toulouse_maelis/schemas.py
+++ b/passerelle/contrib/toulouse_maelis/schemas.py
@@ -533,16 +533,47 @@ PAIINFO_SCHEMA = {
         'description': {
             'description': 'Texte libre de description (max 500 caractères)',
             'oneOf': [{'type': 'string'}, {'type': 'null'}],
         },
     },
     'additionalProperties': False,
 }
 
+INSURANCE_SCHEMA = {
+    '$schema': 'http://json-schema.org/draft-04/schema#',
+    'title': 'Insurace',
+    'description': "Informations sur l'assurance",
+    'type': 'object',
+    'properties': {
+        'company': {
+            'description': "Compagnie d'assurrance",
+            'oneOf': [{'type': 'string'}, {'type': 'null'}],
+        },
+        'contractNumber': {
+            'description': 'Numéro du contrat',
+            'oneOf': [{'type': 'string'}, {'type': 'null'}],
+        },
+        'memberNumber': {
+            'description': 'Numéro de membre',
+            'oneOf': [{'type': 'string'}, {'type': 'null'}],
+        },
+        'contractStart': {
+            'description': 'Date de début du contrat',
+            'type': 'string',
+            'pattern': '^([0-9]{4}-[0-9]{2}-[0-9]{2}){0,1}$',
+        },
+        'contractEnd': {
+            'description': 'Date de fin du contrat',
+            'type': 'string',
+            'pattern': '^([0-9]{4}-[0-9]{2}-[0-9]{2}){0,1}$',
+        },
+    },
+}
+
 CHILD_SCHEMA = {
     '$schema': 'http://json-schema.org/draft-04/schema#',
     'title': 'Child',
     'description': "Informations sur la création d'un enfant",
     'type': 'object',
     'required': ['sexe', 'firstname', 'lastname', 'birth'],
     'properties': {
         'num': {
@@ -564,16 +595,17 @@ CHILD_SCHEMA = {
             'oneOf': BOOLEAN_TYPES,
         },
         'bLeaveAlone': {
             'description': 'Autorisation à partir seul',
             'oneOf': BOOLEAN_TYPES,
         },
         'medicalRecord': {'oneOf': [MEDICALRECORD_SCHEMA, {'type': 'null'}]},
         'paiInfoBean': {'oneOf': [PAIINFO_SCHEMA, {'type': 'null'}]},
+        'insurance': {'oneOf': [INSURANCE_SCHEMA, {'type': 'null'}]},
         'authorizedPersonList': {
             'oneOf': [
                 {
                     'type': 'array',
                     'items': AUTHORIZED_PERSON_SCHEMA,
                 },
                 {'type': 'null'},
             ],
diff --git a/tests/data/toulouse_maelis/R_read_family.xml b/tests/data/toulouse_maelis/R_read_family.xml
index b23d33cd..d28bc67c 100644
--- a/tests/data/toulouse_maelis/R_read_family.xml
+++ b/tests/data/toulouse_maelis/R_read_family.xml
@@ -180,16 +180,23 @@
               <vaccinationDate>2011-01-11T00:00:00+01:00</vaccinationDate>
             </vaccinList>
             <vaccinList>
               <code>ROR</code>
               <label>ROUGEOLE-OREILLONS-RUBEOLE</label>
               <vaccinationDate>1970-01-11T00:00:00+01:00</vaccinationDate>
             </vaccinList>
           </medicalRecord>
+          <insurance>
+            <company>Total Disaster Insurance</company>
+            <contractNumber>123</contractNumber>
+            <memberNumber>456</memberNumber>
+            <contractStart>2022-01-01T00:00:00+01:00</contractStart>
+            <contractEnd>2022-12-31T00:00:00+01:00</contractEnd>
+          </insurance>
           <mother>
             <num>613963</num>
             <civility>MME</civility>
             <firstname>JANE</firstname>
             <lastname>DOE</lastname>
           </mother>
           <father>
             <num>613878</num>
diff --git a/tests/data/toulouse_maelis/R_read_family_relax.xml b/tests/data/toulouse_maelis/R_read_family_relax.xml
index 37f9e649..363d9b12 100644
--- a/tests/data/toulouse_maelis/R_read_family_relax.xml
+++ b/tests/data/toulouse_maelis/R_read_family_relax.xml
@@ -180,16 +180,23 @@
               <vaccinationDate>2011-01-11T00:00:00+01:00</vaccinationDate>
             </vaccinList>
             <vaccinList>
               <code>ROR</code>
               <label>ROUGEOLE-OREILLONS-RUBEOLE</label>
               <vaccinationDate>1970-01-11T00:00:00+01:00</vaccinationDate>
             </vaccinList>
           </medicalRecord>
+          <insurance>
+            <company>Total Disaster Insurance</company>
+            <contractNumber>123</contractNumber>
+            <memberNumber>456</memberNumber>
+            <contractStart>2022-01-01T00:00:00+01:00</contractStart>
+            <contractEnd>2022-12-31T00:00:00+01:00</contractEnd>
+          </insurance>
           <mother>
             <num>613963</num>
             <civility>MME</civility>
             <firstname>JANE</firstname>
             <lastname>DOE</lastname>
           </mother>
           <father>
             <num>613878</num>
diff --git a/tests/data/toulouse_maelis/R_read_family_reordered.xml b/tests/data/toulouse_maelis/R_read_family_reordered.xml
index 8186cc29..966fb694 100644
--- a/tests/data/toulouse_maelis/R_read_family_reordered.xml
+++ b/tests/data/toulouse_maelis/R_read_family_reordered.xml
@@ -180,16 +180,23 @@
               <vaccinationDate>2011-01-11T00:00:00+01:00</vaccinationDate>
             </vaccinList>
             <vaccinList>
               <code>ROR</code>
               <label>ROUGEOLE-OREILLONS-RUBEOLE</label>
               <vaccinationDate>1970-01-11T00:00:00+01:00</vaccinationDate>
             </vaccinList>
           </medicalRecord>
+          <insurance>
+            <company>Total Disaster Insurance</company>
+            <contractNumber>123</contractNumber>
+            <memberNumber>456</memberNumber>
+            <contractStart>2022-01-01T00:00:00+01:00</contractStart>
+            <contractEnd>2022-12-31T00:00:00+01:00</contractEnd>
+          </insurance>
           <mother>
             <num>613963</num>
             <civility>MME</civility>
             <firstname>JANE</firstname>
             <lastname>DOE</lastname>
           </mother>
           <father>
             <num>613878</num>
diff --git a/tests/test_toulouse_maelis.py b/tests/test_toulouse_maelis.py
index ba585396..2f4431b6 100644
--- a/tests/test_toulouse_maelis.py
+++ b/tests/test_toulouse_maelis.py
@@ -899,17 +899,23 @@ def test_read_family(family_service, xml, con, app):
             'place': 'Saint-louis',
             'communeCode': '91122',
             'countryCode': '99100',
         },
         'dietcode': 'RSV',
         'dietcode_text': '3- RÉGIME SANS VIANDE',
         'bPhoto': True,
         'bLeaveAlone': False,
-        'insurance': None,
+        'insurance': {
+            'company': 'Total Disaster Insurance',
+            'contractEnd': '2022-12-31T00:00:00+01:00',
+            'contractNumber': '123',
+            'contractStart': '2022-01-01T00:00:00+01:00',
+            'memberNumber': '456',
+        },
         'subscribeSchoolList': [],
         'mother': {'num': 613963, 'civility': 'MME', 'firstname': 'JANE', 'lastname': 'DOE'},
         'father': {'num': 613878, 'civility': 'M.', 'firstname': 'JHON', 'lastname': 'DOE'},
         'rl': None,
         'subscribeActivityList': [],
     }
     if xml != 'R_read_family_relax.xml':
         assert resp.json['data']['childList'][0]['paiInfoBean'] == {
@@ -1288,16 +1294,21 @@ def test_create_family(family_service, con, app):
         'childList/0/sexe': 'M',
         'childList/0/birth/dateBirth': '1941-05-24',
         'childList/0/birth/place': 'Saint-louis',
         'childList/0/birth/communeCode': '91122',
         'childList/0/birth/countryCode': '99100',
         'childList/0/dietcode': 'RSV',
         'childList/0/medicalRecord/vaccinList/0/code': 'DTC',
         'childList/0/medicalRecord/vaccinList/0/vaccinationDate': '1940-07-26',
+        'childList/0/insurance/company': 'Total Disaster Insurance',
+        'childList/0/insurance/contractNumber': '123',
+        'childList/0/insurance/memberNumber': '456',
+        'childList/0/insurance/contractStart': '2022-01-01',
+        'childList/0/insurance/contractEnd': '2022-12-31',
     }
 
     resp = app.post_json(url + '?NameID=local', params=params)
     assert resp.json['err'] == 0
     assert resp.json['data'] == {
         'number': 196545,
         'password': '394634V2',
         'rl1ErrorList': [],
@@ -1442,16 +1453,21 @@ def test_update_family(family_service, con, app):
         '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',
         'childList/0/medicalRecord/vaccinList/0/vaccinationDate': '1940-07-26',
+        'childList/0/insurance/company': 'Armagedon Colapse',
+        'childList/0/insurance/contractNumber': '444',
+        'childList/0/insurance/memberNumber': '555',
+        'childList/0/insurance/contractStart': '2022-01-02',
+        'childList/0/insurance/contractEnd': '2022-12-31',
         'childList/1/num': '613880',  # update child
         'childList/1/firstname': 'Brunelle',
         'childList/1/lastname': 'Doe',
         'childList/1/birth/dateBirth': '1943-01-19',
         'childList/1/sexe': 'F',
     }
 
     Link.objects.create(resource=con, family_id='1312', name_id='local')
@@ -1962,16 +1978,21 @@ def test_create_child(family_service, con, app):
     family_service.add_soap_response('createChild', get_xml_file('R_create_child.xml'))
     url = get_endpoint('create-child')
     params = {
         'lastname': 'DOE',
         'firstname': 'JANNIS',
         'sexe': 'F',
         'birth/dateBirth': '1943-01-19',
         'birth/place': 'Port Arthur',
+        'insurance/company': 'Total Disaster Insurance',
+        'insurance/contractNumber': '123',
+        'insurance/memberNumber': '456',
+        'insurance/contractStart': '2022-01-01',
+        'insurance/contractEnd': '2022-12-31',
     }
 
     Link.objects.create(resource=con, family_id='1312', name_id='local')
     resp = app.post_json(url + '?NameID=local', params=params)
     assert resp.json['err'] == 0
     assert resp.json['data'] == {'child_id': 613880}
 
 
@@ -2068,16 +2089,21 @@ def test_update_child(family_service, con, app):
     params = {
         'lastname': 'DOE',
         'firstname': 'JANNIS',
         'sexe': 'F',
         'birth/dateBirth': '1943-01-19',
         'birth/place': 'Port Arthur',
         'bPhoto': True,
         'bLeaveAlone': False,
+        'insurance/company': 'Armagedon Colapse',
+        'insurance/contractNumber': '444',
+        'insurance/memberNumber': '555',
+        'insurance/contractStart': '2022-01-02',
+        'insurance/contractEnd': '2022-12-31',
     }
 
     Link.objects.create(resource=con, family_id='1312', name_id='local')
     resp = app.post_json(url + '?NameID=local&child_id=613880', params=params)
     assert resp.json['err'] == 0
 
 
 def test_update_child_not_linked_error(con, app):
-- 
2.30.2

