From c3d5e1d24ccb931a0bab9db8c6fd125c1641311f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Chlo=C3=A9?= Date: Thu, 5 Dec 2024 14:58:57 +0100 Subject: [PATCH] =?UTF-8?q?Am=C3=A9lioration=20de=20la=20mise=20=C3=A0=20j?= =?UTF-8?q?our=20de=20siae.extra=5Fdata?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../commands/crm_brevo_sync_companies.py | 12 ++++-- lemarche/crm/tests.py | 41 ++++++++++++++----- lemarche/utils/apis/api_brevo.py | 4 +- 3 files changed, 41 insertions(+), 16 deletions(-) diff --git a/lemarche/crm/management/commands/crm_brevo_sync_companies.py b/lemarche/crm/management/commands/crm_brevo_sync_companies.py index 350402465..1a5014a15 100644 --- a/lemarche/crm/management/commands/crm_brevo_sync_companies.py +++ b/lemarche/crm/management/commands/crm_brevo_sync_companies.py @@ -43,14 +43,18 @@ def handle(self, recently_updated: bool, **options): # Step 3: loop on the siaes for index, siae in enumerate(siaes_qs): new_extra_data = { - "TAUX DE COMPLÉTION": siae.completion_rate, - "BESOINS REÇUS": siae.tender_email_send_count_annotated, - "BESOINS INTERESSÉS": siae.tender_detail_contact_click_count_annotated, + "completion_rate": siae.completion_rate if siae.completion_rate is not None else 0, + "tender_received": siae.tender_email_send_count_annotated, + "tender_interest": siae.tender_detail_contact_click_count_annotated, } # extra_data update if needed if siae.extra_data != new_extra_data: - siae.extra_data = new_extra_data + siae.extra_data.update( + { + "brevo_company_data": new_extra_data, + } + ) siae.save(update_fields=["extra_data"]) api_brevo.create_or_update_company(siae) diff --git a/lemarche/crm/tests.py b/lemarche/crm/tests.py index 4ebacaeea..9dc517fcd 100644 --- a/lemarche/crm/tests.py +++ b/lemarche/crm/tests.py @@ -57,9 +57,13 @@ def setUpTestData(cls): # siae_with_brevo_id.extra_data initialization cls.siae_with_brevo_id.extra_data = { - "TAUX DE COMPLÉTION": cls.siae_with_brevo_id.completion_rate, - "BESOINS REÇUS": cls.siae_with_brevo_id_recent_stats.tender_email_send_count_annotated, - "BESOINS INTERESSÉS": cls.siae_with_brevo_id_recent_stats.tender_detail_contact_click_count_annotated, + "brevo_company_data": { + "completion_rate": cls.siae_with_brevo_id.completion_rate + if cls.siae_with_brevo_id.completion_rate is not None + else 0, + "tender_received": cls.siae_with_brevo_id_recent_stats.tender_email_send_count_annotated, + "tender_interest": cls.siae_with_brevo_id_recent_stats.tender_detail_contact_click_count_annotated, + } } cls.siae_with_brevo_id.save() cls.initial_extra_data = cls.siae_with_brevo_id.extra_data.copy() @@ -115,18 +119,29 @@ def test_siae_has_tender_stats(self): ) def test_siae_extra_data_is_set_on_first_sync(self): - """Test siae is updated if extra_data is changed.""" - # siae_with_user.extra_data should be empty + """ + - Test siae is updated if extra_data is changed. + - Test siae.extra_data update does not erase existing data. + """ initial_extra_data = self.siae_with_user.extra_data.copy() + initial_extra_data["test_data"] = "test value" + + self.siae_with_user.extra_data = initial_extra_data + self.siae_with_user.save(update_fields=["extra_data"]) call_command("crm_brevo_sync_companies", recently_updated=True) self.siae_with_user.refresh_from_db() expected_extra_data = { - "TAUX DE COMPLÉTION": self.siae_with_user.completion_rate, - "BESOINS REÇUS": self.siae_with_user_stats.tender_email_send_count_annotated, - "BESOINS INTERESSÉS": self.siae_with_user_stats.tender_detail_contact_click_count_annotated, + "brevo_company_data": { + "completion_rate": self.siae_with_user.completion_rate + if self.siae_with_user.completion_rate is not None + else 0, + "tender_received": self.siae_with_user_stats.tender_email_send_count_annotated, + "tender_interest": self.siae_with_user_stats.tender_detail_contact_click_count_annotated, + }, + "test_data": "test value", } self.assertNotEqual(initial_extra_data, expected_extra_data, "siae.extra_data aurait dû être mis à jour.") @@ -188,9 +203,13 @@ def test_fields_update_within_90_days_and_ignore_older_changes(self): ) expected_extra_data = { - "TAUX DE COMPLÉTION": self.siae_with_brevo_id.completion_rate, - "BESOINS REÇUS": self.siae_with_brevo_id_recent_stats.tender_email_send_count_annotated, - "BESOINS INTERESSÉS": self.siae_with_brevo_id_recent_stats.tender_detail_contact_click_count_annotated, + "brevo_company_data": { + "completion_rate": self.siae_with_brevo_id.completion_rate + if self.siae_with_brevo_id.completion_rate is not None + else 0, + "tender_received": self.siae_with_brevo_id_recent_stats.tender_email_send_count_annotated, + "tender_interest": self.siae_with_brevo_id_recent_stats.tender_detail_contact_click_count_annotated, + } } self.assertNotEqual( diff --git a/lemarche/utils/apis/api_brevo.py b/lemarche/utils/apis/api_brevo.py index 4a9fc4dc0..fd940eadc 100644 --- a/lemarche/utils/apis/api_brevo.py +++ b/lemarche/utils/apis/api_brevo.py @@ -146,7 +146,9 @@ def create_or_update_company(siae): "geo_range": siae.geo_range, "app_url": get_object_share_url(siae), "app_admin_url": get_object_admin_url(siae), - **siae.extra_data, # includes completion_rate, tender_email_send_count, etc. + "taux_de_completion": siae.extra_data.get("brevo_company_data", {}).get("completion_rate"), + "nombre_de_besoins_recus": siae.extra_data.get("brevo_company_data", {}).get("tender_received"), + "nombre_de_besoins_interesses": siae.extra_data.get("brevo_company_data", {}).get("tender_interest"), }, )