From 58b8f60f38685eb3f5f6112c60866574bcd3d119 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Laur=C3=A9line=20Gu=C3=A9rin?= <zebuline@entrouvert.com>
Date: Mon, 27 Jan 2020 15:39:31 +0100
Subject: [PATCH] toulouse_axel: add labels to explain some values (#39291)

---
 functests/toulouse_axel/test_toulouse_axel.py |  8 ++-
 passerelle/contrib/toulouse_axel/models.py    | 30 +++++++--
 passerelle/contrib/toulouse_axel/utils.py     | 61 +++++++++++++++++++
 tests/data/toulouse_axel/family_info.xml      |  4 +-
 tests/test_toulouse_axel.py                   | 18 ++++++
 5 files changed, 111 insertions(+), 10 deletions(-)
 create mode 100644 passerelle/contrib/toulouse_axel/utils.py

diff --git a/functests/toulouse_axel/test_toulouse_axel.py b/functests/toulouse_axel/test_toulouse_axel.py
index b6214603..47c465ff 100644
--- a/functests/toulouse_axel/test_toulouse_axel.py
+++ b/functests/toulouse_axel/test_toulouse_axel.py
@@ -45,19 +45,21 @@ def test_link(conn, user):
     payload['DROITALIMAGE'] = 'NON'
     payload['REVENUS']['CHOIXREVENU'] = ''
     # remove non editable fields
-    for key in ['SITUATIONFAMILIALE', 'NBENFANTACTIF', 'NBRLACTIF', 'IDDUI', 'CODEMISEAJOUR']:
+    for key in ['SITUATIONFAMILIALE', 'SITUATIONFAMILIALE_label', 'NBENFANTACTIF', 'NBRLACTIF', 'IDDUI', 'CODEMISEAJOUR']:
         payload.pop(key)
-    for key in ['IDPERSONNE', 'NOM', 'PRENOM', 'NOMJEUNEFILLE', 'DATENAISSANCE', 'CIVILITE', 'INDICATEURRL']:
+    for key in ['IDPERSONNE', 'NOM', 'PRENOM', 'NOMJEUNEFILLE', 'DATENAISSANCE', 'CIVILITE', 'INDICATEURRL', 'CSP_label']:
         if 'RL1' in payload:
             payload['RL1'].pop(key)
         if 'RL2' in payload:
             payload['RL2'].pop(key)
-    for key in ['MONTANTTOTAL', 'DATEVALIDITE', 'SFI', 'IREVENUS', 'RNF', 'NBENFANTSACHARGE']:
+    for key in ['MONTANTTOTAL', 'DATEVALIDITE', 'SFI', 'IREVENUS', 'RNF', 'NBENFANTSACHARGE', 'TYPEREGIME_label']:
         payload['REVENUS'].pop(key, None)
     for enfant in payload['ENFANT']:
         for key in ['NOM', 'DATENAISSANCE', 'SEXE', 'PRENOMPERE', 'PRENOMMERE', 'NOMPERE', 'NOMMERE', 'RATTACHEAUTREDUI', 'PRENOM']:
             enfant.pop(key)
         enfant['AUTORISATIONURGENCEMEDICALE'] = 'OUI'
+        for contact in enfant.get('CONTACT', []):
+            contact.pop('LIENPARENTE_label')
         if 'SANITAIRE' not in enfant:
             continue
         # manage handicap data (not the same schema)
diff --git a/passerelle/contrib/toulouse_axel/models.py b/passerelle/contrib/toulouse_axel/models.py
index 13c8b92a..62e5403a 100644
--- a/passerelle/contrib/toulouse_axel/models.py
+++ b/passerelle/contrib/toulouse_axel/models.py
@@ -36,6 +36,7 @@ from passerelle.base.models import BaseResource
 from passerelle.utils.api import endpoint
 from passerelle.utils.jsonresponse import APIError
 from passerelle.utils.xml import JSONSchemaFromXMLSchema
+from . import utils
 
 logger = logging.getLogger('passerelle.contrib.toulouse_axel')
 
@@ -540,8 +541,28 @@ class ToulouseAxel(BaseResource):
         })
     def family_info(self, request, NameID):
         family_data = self.get_family_data(NameID)
+        family_data['SITUATIONFAMILIALE_label'] = utils.get_label(utils.situation_familiale_mapping, family_data['SITUATIONFAMILIALE'])
+        for key in ['RL1', 'RL2']:
+            if key not in family_data:
+                continue
+            rl = family_data[key]
+            rl['CSP_label'] = utils.get_label(utils.csp_mapping, rl['CSP'])
+        for child in family_data.get('ENFANT', []):
+            for contact in child.get('CONTACT', []):
+                contact['LIENPARENTE_label'] = utils.get_label(utils.lien_parente_mapping, contact['LIENPARENTE'])
+        if 'REVENUS' in family_data:
+            family_data['REVENUS']['TYPEREGIME_label'] = utils.get_label(utils.type_regime_mapping, family_data['REVENUS']['TYPEREGIME'])
         return {'data': family_data}
 
+    def get_child_data(self, name_id, child_id):
+        family_data = self.get_family_data(name_id)
+
+        for child in family_data['ENFANT']:
+            if child['IDPERSONNE'] == child_id:
+                return child
+
+        raise APIError('Child not found', err_code='not-found')
+
     @endpoint(
         description=_("Get information about a child"),
         perm='can_access',
@@ -550,13 +571,12 @@ class ToulouseAxel(BaseResource):
             'idpersonne': {'description': _('Child ID')},
         })
     def child_info(self, request, idpersonne, NameID):
-        family_data = self.get_family_data(NameID)
+        child_data = self.get_child_data(NameID, idpersonne)
 
-        for child in family_data['ENFANT']:
-            if child['IDPERSONNE'] == idpersonne:
-                return {'data': child}
+        for contact in child_data.get('CONTACT', []):
+            contact['LIENPARENTE_label'] = utils.get_label(utils.lien_parente_mapping, contact['LIENPARENTE'])
 
-        raise APIError('Child not found', err_code='not-found')
+        return {'data': child_data}
 
     def pre_sanitize_update_family_data(self, post_data):
         # before json payload validation, check maj fields and remove empty blocks
diff --git a/passerelle/contrib/toulouse_axel/utils.py b/passerelle/contrib/toulouse_axel/utils.py
new file mode 100644
index 00000000..fbee631e
--- /dev/null
+++ b/passerelle/contrib/toulouse_axel/utils.py
@@ -0,0 +1,61 @@
+# -*- coding: utf-8 -*-
+# passerelle - uniform access to multiple data sources and services
+# Copyright (C) 2020  Entr'ouvert
+#
+# This program is free software: you can redistribute it and/or modify it
+# under the terms of the GNU Affero General Public License as published
+# by the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU Affero General Public License for more details.
+#
+# You should have received a copy of the GNU Affero General Public License
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+situation_familiale_mapping = {
+    'C': u'Célibataire',
+    'D': u'Divorcé (e)',
+    'M': u'Marié (e)',
+    'S': u'Séparé (e)',
+    'V': 'Veuf (ve)',
+    'VM': 'Vie Maritale',
+    'P': 'Pacs',
+}
+
+csp_mapping = {
+    'OUV': 'OUVRIER',
+    'EMP': 'EMPLOYES',
+    'ETU': 'ETUDIANT',
+    'RET': 'RETRAITES',
+    'STA': 'PERSONNE EN STAGE',
+    'AGEX': 'AGRICULTEURS EXPLOITANTS',
+    'ARCO': 'ARTISANS COMMERCANTS CHEFS ENTREPRISE',
+    'CADP': 'CADRES PROFESSIONS INTELLECTUELLES SUPERIEURES',
+    'PRIN': 'PROFESSIONS INTERMEDIAIRES',
+    'SACT': 'AUTRES PERSONNES SANS ACTIVITE PROFESSIONNELLE',
+    'REC': 'RECHERCHE EMPLOI',
+}
+
+lien_parente_mapping = {
+    'GRP1': 'Grands-parents paternels',
+    'GRP2': 'Grands-parents maternels',
+    'VOI': 'Voisin',
+    'FRE': "Frère de l'enfant",
+    'SOE': "Soeur de l'enfant",
+    'AMI': 'Ami (e)',
+    'FAMI': 'Membre de la famille',
+    'BABY': 'Baby sitter',
+}
+
+type_regime_mapping = {
+    'GENE': 'REGIME GENERAL',
+    'ZAU': 'AUTRE',
+    'MSA': 'MSA',
+}
+
+
+def get_label(mapping, code):
+    return mapping.get(code, code)
diff --git a/tests/data/toulouse_axel/family_info.xml b/tests/data/toulouse_axel/family_info.xml
index 2e42f007..efbe4c07 100644
--- a/tests/data/toulouse_axel/family_info.xml
+++ b/tests/data/toulouse_axel/family_info.xml
@@ -4,7 +4,7 @@
     <CODEMISEAJOUR>19</CODEMISEAJOUR>
     <NBRLACTIF>2</NBRLACTIF>
     <NBENFANTACTIF>2</NBENFANTACTIF>
-    <SITUATIONFAMILIALE>P</SITUATIONFAMILIALE>
+    <SITUATIONFAMILIALE>S</SITUATIONFAMILIALE>
     <REACTUALISATIONENLIGNE>NON</REACTUALISATIONENLIGNE>
     <DEMATFACTURES>NON</DEMATFACTURES>
     <ADRESSE>
@@ -131,7 +131,7 @@
       <CONTACT>
         <NOM>foo</NOM>
         <PRENOM>foo</PRENOM>
-        <LIENPARENTE/>
+        <LIENPARENTE>GRP1</LIENPARENTE>
         <ENCASURGENCE>OUI</ENCASURGENCE>
         <CHERCHERLENFANT>OUI</CHERCHERLENFANT>
         <TELFIXE>0505050505</TELFIXE>
diff --git a/tests/test_toulouse_axel.py b/tests/test_toulouse_axel.py
index 933fb70f..eca24ebe 100644
--- a/tests/test_toulouse_axel.py
+++ b/tests/test_toulouse_axel.py
@@ -1,3 +1,4 @@
+# -*- coding: utf-8 -*-
 # passerelle - uniform access to multiple data sources and services
 # Copyright (C) 2019 Entr'ouvert
 #
@@ -604,8 +605,21 @@ def test_family_info_endpoint(app, resource):
         'RL1',
         'RL2',
         'SITUATIONFAMILIALE',
+        'SITUATIONFAMILIALE_label',
         'TELFIXE',
     ])
+    assert resp.json['data']['SITUATIONFAMILIALE'] == 'S'
+    assert resp.json['data']['SITUATIONFAMILIALE_label'] == u'Séparé (e)'
+    assert resp.json['data']['RL1']['CSP'] == 'ETU'
+    assert resp.json['data']['RL1']['CSP_label'] == 'ETUDIANT'
+    assert resp.json['data']['RL2']['CSP'] == 'EMP'
+    assert resp.json['data']['RL2']['CSP_label'] == 'EMPLOYES'
+    assert resp.json['data']['ENFANT'][0]['CONTACT'][0]['LIENPARENTE'] == 'GRP1'
+    assert resp.json['data']['ENFANT'][0]['CONTACT'][0]['LIENPARENTE_label'] == 'Grands-parents paternels'
+    assert resp.json['data']['ENFANT'][0]['CONTACT'][1]['LIENPARENTE'] is None
+    assert resp.json['data']['ENFANT'][0]['CONTACT'][1]['LIENPARENTE_label'] is None
+    assert resp.json['data']['REVENUS']['TYPEREGIME'] == 'GENE'
+    assert resp.json['data']['REVENUS']['TYPEREGIME_label'] == 'REGIME GENERAL'
 
 
 def test_child_info_endpoint_axel_error(app, resource):
@@ -655,6 +669,10 @@ def test_child_info_endpoint(app, resource):
         'SANITAIRE',
         'SEXE',
     ])
+    assert resp.json['data']['CONTACT'][0]['LIENPARENTE'] == 'GRP1'
+    assert resp.json['data']['CONTACT'][0]['LIENPARENTE_label'] == 'Grands-parents paternels'
+    assert resp.json['data']['CONTACT'][1]['LIENPARENTE'] is None
+    assert resp.json['data']['CONTACT'][1]['LIENPARENTE_label'] is None
 
 
 def test_update_family_info_endpoint_axel_error(app, resource, update_params, family_data):
-- 
2.20.1

