From 8f8262da29f4db891be82e9261d230d8f2e1f842 Mon Sep 17 00:00:00 2001 From: Benjamin Dauvergne Date: Tue, 28 May 2019 14:38:54 +0200 Subject: [PATCH] atos_genesys: filter call_cherche_beneficiaire results (#33492) Create a datasource from contact informations. --- passerelle/apps/atos_genesys/models.py | 62 +++++++++++++++++++++----- tests/test_atos_genesys.py | 23 ++++++++-- 2 files changed, 71 insertions(+), 14 deletions(-) diff --git a/passerelle/apps/atos_genesys/models.py b/passerelle/apps/atos_genesys/models.py index 5b4f0ae8..7d9e4294 100644 --- a/passerelle/apps/atos_genesys/models.py +++ b/passerelle/apps/atos_genesys/models.py @@ -363,24 +363,66 @@ class Resource(BaseResource, HTTPResource): }) def search(self, request, first_name, last_name, date_of_birth): try: - date_of_birth = datetime.datetime.strptime(date_of_birth, '%Y-%m-%d') + date_of_birth = datetime.datetime.strptime(date_of_birth, '%Y-%m-%d').date() except (ValueError, TypeError): raise APIError('invalid date_of_birth: %r' % date_of_birth) beneficiaires = self.call_cherche_beneficiaire( prenom=first_name, nom=last_name, dob=date_of_birth) + response = {'data': []} for beneficiaire in beneficiaires: - ref_per = beneficiaire.get('REF_PER') - if not ref_per: + id_per = beneficiaire.get('ID_PER') + if not id_per: + self.logger.warning('no ID_PER') continue - dossier = self.call_select_usager_by_ref(ref_per) - identification = dossier['IDENTIFICATION'][0] - beneficiaire['ID_PER'] = identification['ID_PER'] - beneficiaire['TEL_FIXE'] = identification.get('TEL_FIXE', '') - beneficiaire['TEL_MOBILE'] = identification.get('TEL_MOBILE', '') - beneficiaire['MAIL'] = identification.get('MAIL', '') - return {'data': beneficiaires} + try: + dob = beneficiaire['DATE_NAISSANCE'] + except KeyError: + self.logger.warning('id_per %s: no DATE_NAISSANCE', id_per) + continue + try: + dob = datetime.datetime.strptime(dob, '%d/%m/%Y').date() + except (ValueError, TypeError): + self.logger.warning('id_per %s: invalid DATE_NAISSANCE', id_per) + continue + if dob != date_of_birth: + self.logger.debug('ignoring id_per %s different dob %s != %s', id_per, dob, date_of_birth) + continue + dossier = self.call_select_usager(id_per) + try: + identification = dossier['IDENTIFICATION'][0] + except KeyError: + self.logger.warning('id_per %s: dossier is empty', id_per) + continue + assert identification['ID_PER'] == id_per + tel1 = ''.join(c for c in identification.get('TEL_MOBILE', '') if c.isdigit()) + tel2 = ''.join(c for c in identification.get('TEL_FIXE', '') if c.isdigit()) + email = identification.get('MAIL', '').strip() + channels = beneficiaire['CANAUX'] = [] + if tel1: + channels.append({ + 'id': 'tel1', + 'text': tel1[:2] + '*****' + tel1[-3:], + 'phone': tel1, + }) + if tel2: + channels.append({ + 'id': 'tel2', + 'text': tel2[:2] + '*****' + tel2[-3:], + 'phone': tel2, + }) + if email: + channels.append({ + 'id': 'email1', + 'text': email[:2] + '***@***' + email[-3:], + 'email': email, + }) + if not channels: + self.logger.debug('id_per %s: no contact information, ignored', id_per) + continue + response['data'].append(beneficiaire) + return response @endpoint(name='link-by-id-per', methods=['post'], diff --git a/tests/test_atos_genesys.py b/tests/test_atos_genesys.py index d9abda5c..74f37f06 100644 --- a/tests/test_atos_genesys.py +++ b/tests/test_atos_genesys.py @@ -243,12 +243,13 @@ def test_ws_search(app, genesys): 951858 ANTIBES (006) FRANCE + 1234 ''' with utils.mock_url(FAKE_URL + 'WSUsagerPublik/services/PublikService/chercheBeneficiaire', RESPONSE_SEARCH): - with utils.mock_url(FAKE_URL + 'WSUsagerPublik/services/PublikService/selectUsagerByRef', + with utils.mock_url(FAKE_URL + 'WSUsagerPublik/services/PublikService/selectUsager', RESPONSE_SELECT_USAGER): response = app.get(url + '?' + urlencode({ 'first_name': 'John', @@ -263,9 +264,23 @@ def test_ws_search(app, genesys): assert data['NOMPER'] == 'John' assert data['PRENOMPER'] == 'Doe' assert data['DATE_NAISSANCE'] == '01/01/1925' - assert data['TEL_FIXE'] == '06.44.44.44.44' - assert data['TEL_MOBILE'] == '06.55.55.55.55' - assert data['MAIL'] == 'test@sirus.fr' + assert data['CANAUX'] == [ + { + 'id': 'tel1', + 'text': '06*****555', + 'phone': '0655555555', + }, + { + 'id': 'tel2', + 'text': '06*****444', + 'phone': '0644444444', + }, + { + 'id': 'email1', + 'text': 'te***@***.fr', + 'email': 'test@sirus.fr' + } + ] def test_ws_link_by_id_per(app, genesys): -- 2.20.1