Skip to content

Commit

Permalink
split brevo api (emails and crm)
Browse files Browse the repository at this point in the history
  • Loading branch information
madjid-asa committed Jan 24, 2024
1 parent fee8553 commit b341e9e
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 45 deletions.
43 changes: 0 additions & 43 deletions lemarche/utils/apis/api_brevo.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
import json
import logging

import sib_api_v3_sdk
from django.conf import settings
from huey.contrib.djhuey import task
from sib_api_v3_sdk.rest import ApiException

from lemarche.users.models import User


logger = logging.getLogger(__name__)

Expand All @@ -25,46 +22,6 @@ def get_api_client():
return sib_api_v3_sdk.ApiClient(config)


def create_contact(user: User, list_id: int):
api_client = get_api_client()
api_instance = sib_api_v3_sdk.ContactsApi(api_client)
new_contact = sib_api_v3_sdk.CreateContact(
email=user.email,
list_ids=[list_id],
attributes={
"NOM": user.first_name,
"PRENOM": user.last_name,
"DATE_INSCRIPTION": user.created_at,
"TYPE_ORGANISATION": user.buyer_kind_detail,
"NOM_ENTREPRISE": user.company_name,
},
ext_id=str(user.id),
update_enabled=True,
)

try:
api_response = api_instance.create_contact(new_contact)
logger.info("Succes Brevo->ContactsApi->create_contact: %s\n" % api_response)
except ApiException as e:
logger.error("Exception when calling Brevo->ContactsApi->create_contact: %s\n" % e)


def remove_contact_from_list(user: User, list_id: int):
api_client = get_api_client()
api_instance = sib_api_v3_sdk.ContactsApi(api_client)
contact_emails = sib_api_v3_sdk.RemoveContactFromList(emails=[user.email])

try:
api_response = api_instance.remove_contact_from_list(list_id=list_id, contact_emails=contact_emails)
logger.info("Succes Brevo->ContactsApi->remove_contact_from_list: %s\n" % api_response)
except ApiException as e:
error_body = json.loads(e.body)
if error_body.get("message") == "Contact already removed from list and/or does not exist":
logger.info("calling Brevo->ContactsApi->remove_contact_from_list: contact doesn't exist in this list")
else:
logger.error("Exception when calling Brevo->ContactsApi->remove_contact_from_list: %s\n" % e)


@task()
def send_transactional_email_with_template(
template_id: int,
Expand Down
4 changes: 2 additions & 2 deletions lemarche/www/auth/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

from lemarche.users import constants as user_constants
from lemarche.users.models import User
from lemarche.utils.apis import api_brevo, api_hubspot, api_mailjet
from lemarche.utils.apis import api_brevo_crm, api_hubspot, api_mailjet
from lemarche.utils.emails import send_mail_async, whitelist_recipient_list
from lemarche.utils.urls import get_domain_url

Expand Down Expand Up @@ -97,7 +97,7 @@ def add_to_contact_list(user: User, type: str, source: str = user_constants.SOUR
if user.kind == user.KIND_BUYER:
# TODO: we still use it ?
api_hubspot.add_user_to_crm(user)
api_brevo.create_contact(user=user, list_id=settings.BREVO_CL_SIGNUP_BUYER_ID)
api_brevo_crm.create_contact(user=user, list_id=settings.BREVO_CL_SIGNUP_BUYER_ID)
elif type == "buyer_search":
contact_list_id = settings.MAILJET_NL_CL_BUYER_SEARCH_SIAE_LIST_ID
elif type == "buyer_search_traiteur":
Expand Down

0 comments on commit b341e9e

Please sign in to comment.