diff --git a/lemarche/api/tenders/tests.py b/lemarche/api/tenders/tests.py index 04c4d4810..c345cdfb5 100644 --- a/lemarche/api/tenders/tests.py +++ b/lemarche/api/tenders/tests.py @@ -1,9 +1,9 @@ +from unittest.mock import patch + from django.conf import settings from django.test import TestCase from django.urls import reverse -from unittest.mock import patch - from lemarche.perimeters.factories import PerimeterFactory from lemarche.sectors.factories import SectorFactory from lemarche.tenders import constants as tender_constants @@ -190,7 +190,7 @@ def test_create_contact_call_has_user_buyer_attributes(self, mock_create_contact self.assertEqual(kwargs["email"], user.email) self.assertIn(settings.BREVO_CL_SIGNUP_BUYER_ID, kwargs["list_ids"]) - self.assertEqual(attributes["MONTANT_BESOIN_ACHETEUR"], tender.amount) + self.assertEqual(attributes["MONTANT_BESOIN_ACHETEUR"], tender.amount_int) self.assertEqual(attributes["TYPE_BESOIN_ACHETEUR"], tender.kind) if sectors.exists(): diff --git a/lemarche/utils/apis/api_brevo.py b/lemarche/utils/apis/api_brevo.py index ff628a5cf..822f5e10e 100644 --- a/lemarche/utils/apis/api_brevo.py +++ b/lemarche/utils/apis/api_brevo.py @@ -4,6 +4,7 @@ import sib_api_v3_sdk from django.conf import settings +from django.core.exceptions import ObjectDoesNotExist from huey.contrib.djhuey import task from sib_api_v3_sdk.rest import ApiException @@ -51,9 +52,10 @@ def create_contact(user, list_id: int, tender=None): # WHATSAPP, TYPE_ORGANISATION, LIEN_FICHE_COMMERCIALE, TAUX_DE_COMPLETION } - if tender: + try: + tender = user.tenders.get(id=tender.id) sectors = tender.sectors.all() - attributes["MONTANT_BESOIN_ACHETEUR"] = tender.amount + attributes["MONTANT_BESOIN_ACHETEUR"] = tender.amount_int attributes["TYPE_BESOIN_ACHETEUR"] = tender.kind # Check if there is at least one sector whose tender source is TALLY @@ -62,6 +64,11 @@ def create_contact(user, list_id: int, tender=None): else: attributes["TYPE_VERTICALE_ACHETEUR"] = None + except ObjectDoesNotExist: + print("L'objet Tender demandé n'existe pas pour cet utilisateur.") + except AttributeError as e: + print(f"Erreur d'attribut : {e}") + new_contact = sib_api_v3_sdk.CreateContact( email=user.email, list_ids=[list_id], diff --git a/lemarche/www/tenders/tests.py b/lemarche/www/tenders/tests.py index 92407975f..cd9c5b52d 100644 --- a/lemarche/www/tenders/tests.py +++ b/lemarche/www/tenders/tests.py @@ -293,7 +293,7 @@ def test_create_contact_call_has_user_buyer_attributes(self, mock_create_contact self.assertEqual(kwargs["email"], user.email) self.assertIn(settings.BREVO_CL_SIGNUP_BUYER_ID, kwargs["list_ids"]) - self.assertEqual(attributes["MONTANT_BESOIN_ACHETEUR"], tender.amount) + self.assertEqual(attributes["MONTANT_BESOIN_ACHETEUR"], tender.amount_int) self.assertEqual(attributes["TYPE_BESOIN_ACHETEUR"], tender.kind) self.assertIsNone( attributes["TYPE_VERTICALE_ACHETEUR"], "Expected TYPE_VERTICALE_ACHETEUR to be None for non-TALLY sources"