Skip to content

Commit

Permalink
Fix tender id None in api_brevo.create_contact
Browse files Browse the repository at this point in the history
  • Loading branch information
chloend committed Dec 16, 2024
1 parent 2dff083 commit a62e493
Showing 1 changed file with 21 additions and 22 deletions.
43 changes: 21 additions & 22 deletions lemarche/utils/apis/api_brevo.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

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 @@ -52,41 +51,41 @@ def create_contact(user, list_id: int, tender=None):
# WHATSAPP, TYPE_ORGANISATION, LIEN_FICHE_COMMERCIALE, TAUX_DE_COMPLETION
}

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 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
if tender:
try:
first_sector = tender.sectors.first()
attributes["MONTANT_BESOIN_ACHETEUR"] = tender.amount_int
attributes["TYPE_BESOIN_ACHETEUR"] = tender.kind

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}")
# 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 AttributeError as e:
logger.error(f"Erreur d'attribut : {e}")
except Exception as e:
logger.error(f"Une erreur inattendue est survenue : {e}")

new_contact = sib_api_v3_sdk.CreateContact(
email=user.email,
list_ids=[list_id],
attributes=attributes,
ext_id=str(user.id),
update_enabled=True,
update_enabled=False,
)

try:
api_response = api_instance.create_contact(new_contact).to_dict()
if not user.brevo_contact_id:
api_response = api_instance.create_contact(new_contact).to_dict()
user.brevo_contact_id = api_response.get("id")
user.save()
logger.info(f"Success Brevo->ContactsApi->create_contact: {api_response}")
logger.info(f"Success Brevo->ContactsApi->create_contact: {api_response.body}")
else:
logger.info("User already exists in Brevo")
except ApiException as e:
logger.error(f"Exception when calling Brevo->ContactsApi->create_contact: {e}")
logger.error(f"Exception when calling Brevo->ContactsApi->create_contact (list_id : {list_id}): {e.body}")


def update_contact(user_identifier: str, attributes_to_update: dict):
Expand Down

0 comments on commit a62e493

Please sign in to comment.