diff --git a/CHANGELOG.md b/CHANGELOG.md index 44b61e6..81aeed4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,10 @@ and this project adheres to ## [Unreleased] +### Fixed + +- 🚑️(plugins) fix name from SIRET specific case #674 + ## [1.10.1] - 2025-01-27 ### Added diff --git a/src/backend/plugins/organizations.py b/src/backend/plugins/organizations.py index 1dcbc47..334372b 100644 --- a/src/backend/plugins/organizations.py +++ b/src/backend/plugins/organizations.py @@ -24,27 +24,22 @@ class NameFromSiretOrganizationPlugin(BaseOrganizationPlugin): _api_url = "https://recherche-entreprises.api.gouv.fr/search?q={siret}" @staticmethod - def _extract_name_from_organization_data(organization_data): - """Extract the name from the organization data.""" - try: - return organization_data["liste_enseignes"][0].title() - except KeyError: - logger.warning("Missing key 'liste_enseignes' in %s", organization_data) - except IndexError: - logger.warning("Empty list 'liste_enseignes' in %s", organization_data) - return None - - def get_organization_name_from_results(self, data, siret): + def get_organization_name_from_results(data, siret): """Return the organization name from the results of a SIRET search.""" for result in data["results"]: - nature = "nature_juridique" - commune = nature in result and result[nature] == "7210" + is_commune = ( + result.get("nature_juridique") == "7210" + ) # INSEE code for commune for organization in result["matching_etablissements"]: if organization.get("siret") == siret: - if commune: + if is_commune: return organization["libelle_commune"].title() - return self._extract_name_from_organization_data(organization) + store_signs = organization.get("liste_enseignes") or [] + if store_signs: + return store_signs[0].title() + if name := result.get("nom_raison_sociale"): + return name.title() logger.warning("No organization name found for SIRET %s", siret) return None diff --git a/src/backend/plugins/tests/organizations/test_name_from_siret_organization_plugin.py b/src/backend/plugins/tests/organizations/test_name_from_siret_organization_plugin.py index 56c8ac2..37dc3a6 100644 --- a/src/backend/plugins/tests/organizations/test_name_from_siret_organization_plugin.py +++ b/src/backend/plugins/tests/organizations/test_name_from_siret_organization_plugin.py @@ -168,3 +168,43 @@ def test_extract_name_from_org_data_when_commune( plugin = NameFromSiretOrganizationPlugin() name = plugin.get_organization_name_from_results(data, "21580304000017") assert name == "Varzy" + + +@responses.activate +def test_organization_plugins_run_after_create_no_list_enseignes( + organization_plugins_settings, +): + """Test the run_after_create method of the organization plugins for nominal case.""" + responses.add( + responses.GET, + "https://recherche-entreprises.api.gouv.fr/search?q=12345678901234", + json={ + "results": [ + { + "nom_raison_sociale": "AMAZING ORGANIZATION", + # skipping some fields + "matching_etablissements": [ + # skipping some fields + { + "liste_enseignes": None, + "siret": "12345678901234", + } + ], + } + ], + "total_results": 1, + "page": 1, + "per_page": 10, + "total_pages": 1, + }, + status=200, + ) + + organization = Organization.objects.create( + name="12345678901234", registration_id_list=["12345678901234"] + ) + assert organization.name == "Amazing Organization" + + # Check that the organization has been updated in the database also + organization.refresh_from_db() + assert organization.name == "Amazing Organization"