Skip to content

Commit

Permalink
feat(Brevo): lorsqu'un utilisateur se rattache à une structure, envoy…
Browse files Browse the repository at this point in the history
…er l'info à Brevo (#1211)
  • Loading branch information
raphodn authored May 13, 2024
1 parent a91a735 commit f685d73
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 24 deletions.
50 changes: 26 additions & 24 deletions lemarche/utils/apis/api_brevo.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,20 +211,21 @@ def link_deal_with_contact_list(tender, contact_list: list = None):
api_client = get_api_client()
api_instance = sib_api_v3_sdk.DealsApi(api_client)

try:
# get brevo ids
brevo_crm_deal_id = tender.brevo_deal_id
# Default to the author's contact ID if no contact list is provided
if not contact_list:
contact_list = [tender.author.brevo_contact_id]
if settings.BITOUBI_ENV not in ENV_NOT_ALLOWED:
try:
# get brevo ids
brevo_crm_deal_id = tender.brevo_deal_id
# Default to the author's contact ID if no contact list is provided
if not contact_list:
contact_list = [tender.author.brevo_contact_id]

# link deal with contact_list
# https://github.com/sendinblue/APIv3-python-library/blob/master/docs/Body5.md
body_link_deal_contact = sib_api_v3_sdk.Body5(link_contact_ids=contact_list)
api_instance.crm_deals_link_unlink_id_patch(brevo_crm_deal_id, body_link_deal_contact)
# link deal with contact_list
# https://github.com/sendinblue/APIv3-python-library/blob/master/docs/Body5.md
body_link_deal_contact = sib_api_v3_sdk.Body5(link_contact_ids=contact_list)
api_instance.crm_deals_link_unlink_id_patch(brevo_crm_deal_id, body_link_deal_contact)

except ApiException as e:
logger.error("Exception when calling Brevo->DealApi->crm_deals_link_unlink_id_patch: %s\n" % e)
except ApiException as e:
logger.error("Exception when calling Brevo->DealApi->crm_deals_link_unlink_id_patch: %s\n" % e)


def link_company_with_contact_list(siae, contact_list: list = None):
Expand All @@ -245,20 +246,21 @@ def link_company_with_contact_list(siae, contact_list: list = None):
api_client = get_api_client()
api_instance = sib_api_v3_sdk.CompaniesApi(api_client)

try:
# get brevo ids
brevo_crm_company_id = siae.brevo_company_id
# Default to the siae's user(s) ID(s) if no contact list is provided
if not contact_list:
contact_list = siae.users.values_list("brevo_contact_id", flat=True)
if settings.BITOUBI_ENV not in ENV_NOT_ALLOWED:
try:
# get brevo ids
brevo_crm_company_id = siae.brevo_company_id
# Default to the siae's user(s) ID(s) if no contact list is provided
if not contact_list:
contact_list = list(siae.users.values_list("brevo_contact_id", flat=True))

# link company with contact_list
# https://github.com/sendinblue/APIv3-python-library/blob/master/docs/Body2.md
body_link_company_contact = sib_api_v3_sdk.Body2(link_contact_ids=contact_list)
api_instance.companies_link_unlink_id_patch(brevo_crm_company_id, body_link_company_contact)
# link company with contact_list
# https://github.com/sendinblue/APIv3-python-library/blob/master/docs/Body2.md
body_link_company_contact = sib_api_v3_sdk.Body2(link_contact_ids=contact_list)
api_instance.companies_link_unlink_id_patch(brevo_crm_company_id, body_link_company_contact)

except ApiException as e:
logger.error("Exception when calling Brevo->DealApi->companies_link_unlink_id_patch: %s\n" % e)
except ApiException as e:
logger.error("Exception when calling Brevo->DealApi->companies_link_unlink_id_patch: %s\n" % e)


def get_all_users_from_list(
Expand Down
3 changes: 3 additions & 0 deletions lemarche/www/dashboard_siaes/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from django.views.generic.edit import FormMixin

from lemarche.siaes.models import Siae, SiaeUser, SiaeUserRequest
from lemarche.utils.apis import api_brevo
from lemarche.utils.mixins import SiaeMemberRequiredMixin, SiaeUserAndNotMemberRequiredMixin, SiaeUserRequiredMixin
from lemarche.utils.s3 import S3Upload
from lemarche.www.dashboard_siaes.forms import (
Expand Down Expand Up @@ -76,6 +77,7 @@ def form_valid(self, form):
"""
if not self.object.users.count():
self.object.users.add(self.request.user)
api_brevo.link_company_with_contact_list(self.object)
return super().form_valid(form)
else:
# create SiaeUserRequest + send request email to assignee
Expand Down Expand Up @@ -279,6 +281,7 @@ def form_valid(self, form):
self.object.logs.append({"action": "response_true", "timestamp": self.object.response_date.isoformat()})
self.object.save()
send_siae_user_request_response_email_to_initiator(self.object)
api_brevo.link_company_with_contact_list(self.object.siae)
return super().form_valid(form)

def get_success_url(self):
Expand Down

0 comments on commit f685d73

Please sign in to comment.