Skip to content

Commit

Permalink
create deal when tender is created
Browse files Browse the repository at this point in the history
  • Loading branch information
madjid-asa committed Jan 24, 2024
1 parent b341e9e commit d7ef156
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 0 deletions.
3 changes: 3 additions & 0 deletions lemarche/tenders/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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"):
Expand Down
88 changes: 88 additions & 0 deletions lemarche/utils/apis/api_brevo_crm.py
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit d7ef156

Please sign in to comment.