Skip to content

Commit

Permalink
Amélioration de la mise à jour de siae.extra_data
Browse files Browse the repository at this point in the history
  • Loading branch information
chloend committed Dec 5, 2024
1 parent 8e2e5da commit c3d5e1d
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 16 deletions.
12 changes: 8 additions & 4 deletions lemarche/crm/management/commands/crm_brevo_sync_companies.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
41 changes: 30 additions & 11 deletions lemarche/crm/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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.")
Expand Down Expand Up @@ -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(
Expand Down
4 changes: 3 additions & 1 deletion lemarche/utils/apis/api_brevo.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
},
)

Expand Down

0 comments on commit c3d5e1d

Please sign in to comment.