Skip to content

Commit

Permalink
Optimisation de requête sur Tender et modification et ajout de tender…
Browse files Browse the repository at this point in the history
….amount_int
  • Loading branch information
chloend committed Nov 7, 2024
1 parent ab337a1 commit 285b8c2
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 10 deletions.
6 changes: 3 additions & 3 deletions lemarche/api/tenders/tests.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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():
Expand Down
21 changes: 15 additions & 6 deletions lemarche/utils/apis/api_brevo.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -51,17 +52,25 @@ def create_contact(user, list_id: int, tender=None):
# WHATSAPP, TYPE_ORGANISATION, LIEN_FICHE_COMMERCIALE, TAUX_DE_COMPLETION
}

if tender:
sectors = tender.sectors.all()
attributes["MONTANT_BESOIN_ACHETEUR"] = tender.amount
try:
tender = user.tenders.get(id=tender.id)
first_sector = tender.sectors.first()
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
if tender.source == tender_constants.SOURCE_TALLY and sectors.exists():
attributes["TYPE_VERTICALE_ACHETEUR"] = sectors.first().name
# Check if there is one sector whose tender source is TALLY
if tender.source == tender_constants.SOURCE_TALLY and first_sector:
attributes["TYPE_VERTICALE_ACHETEUR"] = first_sector.name
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}")
except Exception as e:
print(f"Une erreur inattendue est survenue : {e}")

new_contact = sib_api_v3_sdk.CreateContact(
email=user.email,
list_ids=[list_id],
Expand Down
2 changes: 1 addition & 1 deletion lemarche/www/tenders/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit 285b8c2

Please sign in to comment.