From d7ef156ebb9f0fe35a843a65ac885f0cbe91ad8b Mon Sep 17 00:00:00 2001 From: "madjid.asa" Date: Wed, 24 Jan 2024 09:49:53 +0100 Subject: [PATCH] create deal when tender is created --- lemarche/tenders/admin.py | 3 + lemarche/utils/apis/api_brevo_crm.py | 88 ++++++++++++++++++++++++++++ 2 files changed, 91 insertions(+) create mode 100644 lemarche/utils/apis/api_brevo_crm.py diff --git a/lemarche/tenders/admin.py b/lemarche/tenders/admin.py index 96ff41f08..dda644a84 100644 --- a/lemarche/tenders/admin.py +++ b/lemarche/tenders/admin.py @@ -19,6 +19,7 @@ from lemarche.tenders.models import PartnerShareTender, Tender, TenderQuestion, TenderStepsData from lemarche.users import constants as user_constants from lemarche.utils.admin.admin_site import admin_site +from lemarche.utils.apis import api_brevo_crm from lemarche.utils.fields import ChoiceArrayField, pretty_print_readonly_jsonfield from lemarche.www.tenders.tasks import restart_send_tender_task @@ -545,6 +546,8 @@ def response_change(self, request, obj: Tender): """ if request.POST.get("_validate_tender"): obj.set_validated() + # brevo create deal + api_brevo_crm.create_deal(tender=obj) self.message_user(request, "Ce dépôt de besoin a été validé. Il sera envoyé en temps voulu :)") return HttpResponseRedirect(".") elif request.POST.get("_restart_tender"): diff --git a/lemarche/utils/apis/api_brevo_crm.py b/lemarche/utils/apis/api_brevo_crm.py new file mode 100644 index 000000000..4364b44c3 --- /dev/null +++ b/lemarche/utils/apis/api_brevo_crm.py @@ -0,0 +1,88 @@ +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.tenders import constants as tender_constants +from lemarche.tenders.models import Tender +from lemarche.users.models import User + + +logger = logging.getLogger(__name__) + +ENV_NOT_ALLOWED = ("dev", "test") + + +def get_config(): + config = sib_api_v3_sdk.Configuration() + config.api_key["api-key"] = settings.BREVO_API_KEY + return config + + +def get_api_client(): + config = get_config() + 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) + + +def create_deal(tender: Tender): + api_client = get_api_client() + api_instance = sib_api_v3_sdk.DealsApi(api_client) + body = sib_api_v3_sdk.Body3( + name=tender.title, + attributes={ + # "deal_name": tender.title, + "deal_description": tender.description, + "deal_stage": "8cd08f5f-1914-4823-a1f6-88b4fe22071d", + "amount": tender_constants.AMOUNT_RANGE_TO_MAX_INT.get(tender.amount, 0), + }, + ) # Body3 | Deal create data. + + try: + new_deal = api_instance.crm_deals_post(body) + logger.info("Succes Brevo->Create a deal : %s\n" % new_deal) + except ApiException as e: + logger.error("Exception when calling Brevo->ContactsApi->create_contact: %s\n" % e)