Skip to content

Commit

Permalink
feat(Dépôts de besoins): Synchroniser les DDB avec brevo à la validat…
Browse files Browse the repository at this point in the history
…ion (#1179)
  • Loading branch information
madjid-asa authored May 3, 2024
1 parent c4733e7 commit b1495bf
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 0 deletions.
2 changes: 2 additions & 0 deletions config/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,8 @@

BREVO_TENDERS_AUTHOR_SUPER_SIAES_TEMPLATE_ID = env.int("BREVO_TENDERS_AUTHOR_SUPER_SIAES_TEMPLATE_ID", 61)

BREVO_TENDERS_MIN_AMOUNT_TO_SEND = env.int("BREVO_TENDERS_MIN_AMOUNT_TO_SEND", 34998)

# -- hubspot
HUBSPOT_API_KEY = env.str("HUBSPOT_API_KEY", "set-it")
HUBSPOT_IS_ACTIVATED = env.bool("HUBSPOT_IS_ACTIVATED", False)
Expand Down
7 changes: 7 additions & 0 deletions lemarche/tenders/admin.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from ckeditor.widgets import CKEditorWidget
from django import forms
from django.conf import settings
from django.contrib import admin
from django.contrib.contenttypes.admin import GenericTabularInline
from django.core.exceptions import ValidationError
Expand All @@ -21,6 +22,7 @@
from lemarche.users import constants as user_constants
from lemarche.users.models import User
from lemarche.utils.admin.admin_site import admin_site
from lemarche.utils.apis import api_brevo
from lemarche.utils.fields import ChoiceArrayField, pretty_print_readonly_jsonfield
from lemarche.www.tenders.tasks import restart_send_tender_task

Expand Down Expand Up @@ -765,6 +767,11 @@ def response_change(self, request, obj: Tender):
return HttpResponseRedirect("./#structures") # redirect to structures sections
if request.POST.get("_validate_tender"):
obj.set_validated()
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)
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(".")
elif request.POST.get("_restart_tender"):
Expand Down
10 changes: 10 additions & 0 deletions lemarche/tenders/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -981,6 +981,16 @@ def is_validated_or_sent(self) -> bool:
def is_partner_approch(self) -> bool:
return self.author_id == settings.PARTNER_APPROCH_USER_ID

@property
def amount_int(self) -> int:
if self.amount_exact:
return self.amount_exact
elif self.amount:
return tender_constants.AMOUNT_RANGE_CHOICE_EXACT.get(self.amount)
else:
# else return -1 for unknown
return -1

def set_validated(self):
self.validated_at = timezone.now()
self.status = tender_constants.STATUS_VALIDATED
Expand Down

0 comments on commit b1495bf

Please sign in to comment.