From 1aa7b99fbd0e712d8c302ed10e8c8b0c03c8af2b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rapha=C3=ABl=20Odini?= Date: Mon, 13 May 2024 16:53:58 +0200 Subject: [PATCH] =?UTF-8?q?feat(Brevo):=20m=C3=A9thode=20pour=20lier=20des?= =?UTF-8?q?=20Contacts=20(User)=20=C3=A0=20une=20Company=20(Siae)=20(#1209?= =?UTF-8?q?)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lemarche/tenders/admin.py | 2 +- lemarche/utils/apis/api_brevo.py | 41 ++++++++++++++++++++++++++++---- 2 files changed, 38 insertions(+), 5 deletions(-) diff --git a/lemarche/tenders/admin.py b/lemarche/tenders/admin.py index 4d087d1e5..d24dba334 100644 --- a/lemarche/tenders/admin.py +++ b/lemarche/tenders/admin.py @@ -770,7 +770,7 @@ def response_change(self, request, obj: Tender): if obj.amount_int > settings.BREVO_TENDERS_MIN_AMOUNT_TO_SEND: api_brevo.create_deal(tender=obj, owner_email=request.user.email) # we link deal(tender) with author contact - api_brevo.link_deal_with_list_contact(tender=obj) + api_brevo.link_deal_with_contact_list(tender=obj) self.message_user(request, "Ce dépôt de besoin a été synchronisé avec Brevo") self.message_user(request, "Ce dépôt de besoin a été validé. Il sera envoyé en temps voulu :)") return HttpResponseRedirect(".") diff --git a/lemarche/utils/apis/api_brevo.py b/lemarche/utils/apis/api_brevo.py index 288c555a0..3522b86b2 100644 --- a/lemarche/utils/apis/api_brevo.py +++ b/lemarche/utils/apis/api_brevo.py @@ -193,7 +193,7 @@ def create_deal(tender, owner_email: str): logger.error("Exception when calling Brevo->DealApi->create_deal: %s\n" % e) -def link_deal_with_list_contact(tender, contact_list: list = None): +def link_deal_with_contact_list(tender, contact_list: list = None): """ Links a Brevo deal to a list of contacts. If no contact list is provided, it defaults to linking the deal with the tender's author. @@ -216,10 +216,9 @@ def link_deal_with_list_contact(tender, contact_list: list = None): 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: - brevo_crm_author_deal_id = tender.author.brevo_contact_id - contact_list = [brevo_crm_author_deal_id] + contact_list = [tender.author.brevo_contact_id] - # link deal with author + # 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) @@ -228,6 +227,40 @@ def link_deal_with_list_contact(tender, contact_list: list = None): 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): + """ + Links a Brevo company to a list of contacts. If no contact list is provided, it defaults + to linking the company with the siae's users. + + This function uses the siae's stored company ID and either a provided list of contact IDs or the + siae author's user(s) ID(s) to link contacts to the company in the Brevo CRM. + + Args: + siae (Siae): The siae object containing the Brevo company ID and author's contact ID. + contact_list (list of int, optional): List of contact IDs to be linked with the company. Defaults to None. + + Raises: + ApiException: If an error occurs during the linking process in the Brevo API. + """ + 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) + + # 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) + + def get_all_users_from_list( list_id: int = settings.BREVO_CL_SIGNUP_BUYER_ID, limit=500, offset=0, max_retries=3, verbose=False ):