Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(Brevo): méthode pour créer (et maj) des entreprises (SIAE) #1099

Merged
merged 6 commits into from
Feb 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions config/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -864,6 +864,7 @@
"from lemarche.utils import constants",
"from lemarche.siaes import constants as siae_constants",
"from lemarche.tenders import constants as tender_constants",
"from lemarche.utils.apis import api_brevo, api_mailjet",
]
FORM_RENDERER = "django.forms.renderers.TemplatesSetting"

Expand Down
58 changes: 53 additions & 5 deletions lemarche/utils/apis/api_brevo.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
from huey.contrib.djhuey import task
from sib_api_v3_sdk.rest import ApiException

from lemarche.siaes.models import Siae
from lemarche.users.models import User
from lemarche.utils.urls import get_object_admin_url, get_object_share_url


logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -44,9 +46,9 @@ def create_contact(user: User, list_id: int):

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


def remove_contact_from_list(user: User, list_id: int):
Expand All @@ -56,13 +58,59 @@ def remove_contact_from_list(user: User, list_id: int):

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)
logger.info(f"Success Brevo->ContactsApi->remove_contact_from_list: {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)
logger.error(f"Exception when calling Brevo->ContactsApi->remove_contact_from_list: {e}")


def create_or_update_company(siae: Siae):
"""
Brevo docs:
- Python library: https://github.com/sendinblue/APIv3-python-library/blob/master/docs/CompaniesApi.md
- API: https://developers.brevo.com/reference/get_companies
"""
api_client = get_api_client()
api_instance = sib_api_v3_sdk.CompaniesApi(api_client)

siae_brevo_company_body = sib_api_v3_sdk.Body(
name=siae.name,
attributes={
"app_id": siae.id,
"siae": True,
"description": siae.description,
"kind": siae.kind,
"address_street": siae.address,
"address_post_code": siae.post_code,
"address_city": siae.city,
"contact_email": siae.contact_email,
"contact_phone": siae.contact_phone,
"domain": siae.website,
"logo_url": siae.logo_url,
"geo_range": siae.geo_range,
"app_url": get_object_share_url(siae),
"app_admin_url": get_object_admin_url(siae),
},
)

if siae.brevo_company_id: # update
try:
api_response = api_instance.companies_id_patch(siae.brevo_company_id, siae_brevo_company_body)
# logger.info(f"Success Brevo->CompaniesApi->create_or_update_company (update): {api_response}")
# api_response: {'attributes': None, 'id': None, 'linked_contacts_ids': None, 'linked_deals_ids': None}
except ApiException as e:
logger.error(f"Exception when calling Brevo->CompaniesApi->create_or_update_company (update): {e}")
else: # create
try:
api_response = api_instance.companies_post(siae_brevo_company_body)
logger.info(f"Success Brevo->CompaniesApi->create_or_update_company (create): {api_response}")
# api_response: {'id': '<brevo_company_id>'}
siae.set_brevo_id(api_response.id)
except ApiException as e:
logger.error(f"Exception when calling Brevo->CompaniesApi->create_or_update_company (create): {e}")


@task()
Expand Down Expand Up @@ -91,6 +139,6 @@ def send_transactional_email_with_template(
logger.info("Brevo: send transactional email with template")
return response
except ApiException as e:
print("Exception when calling SMTPApi->send_transac_email: %s\n" % e)
print(f"Exception when calling SMTPApi->send_transac_email: {e}")
else:
logger.info("Brevo: email not sent (DEV or TEST environment detected)")
Loading