Skip to content

Commit

Permalink
feat(Emails): Besoins : configurer les envois d'e-mails à l'auteur av…
Browse files Browse the repository at this point in the history
…ec TemplateTransactional (2/X) (#1341)
  • Loading branch information
raphodn authored Jul 17, 2024
1 parent 9aaa614 commit c02db9f
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 34 deletions.
14 changes: 0 additions & 14 deletions config/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -333,18 +333,6 @@
# -- tender: siae & partners
MAILJET_TENDERS_PARTNER_PRESENTATION_TEMPLATE_ID = env.int("MAILJET_TENDERS_PARTNER_PRESENTATION_TEMPLATE_ID", 3868179)
# -- tender: author
MAILJET_TENDERS_AUTHOR_SIAE_INTERESTED_1_TEMPLATE_ID = env.int(
"MAILJET_TENDERS_AUTHOR_SIAE_INTERESTED_1_TEMPLATE_ID", 3867188
)
MAILJET_TENDERS_AUTHOR_SIAE_INTERESTED_2_TEMPLATE_ID = env.int(
"MAILJET_TENDERS_AUTHOR_SIAE_INTERESTED_2_TEMPLATE_ID", 4306699
)
MAILJET_TENDERS_AUTHOR_SIAE_INTERESTED_5_TEMPLATE_ID = env.int(
"MAILJET_TENDERS_AUTHOR_SIAE_INTERESTED_5_TEMPLATE_ID", 4306770
)
MAILJET_TENDERS_AUTHOR_SIAE_INTERESTED_5_MORE_TEMPLATE_ID = env.int(
"MAILJET_TENDERS_AUTHOR_SIAE_INTERESTED_5_MORE_TEMPLATE_ID", 3867200
)
MAILJET_TENDERS_AUTHOR_FEEDBACK_30D_TEMPLATE_ID = env.int("MAILJET_TENDERS_AUTHOR_FEEDBACK_30D_TEMPLATE_ID", 4017446)
MAILJET_TENDERS_AUTHOR_TRANSACTIONED_QUESTION_7D_TEMPLATE_ID = env.int(
"MAILJET_TENDERS_AUTHOR_TRANSACTIONED_QUESTION_7D_TEMPLATE_ID", 5207181
Expand All @@ -364,8 +352,6 @@

INBOUND_EMAIL_IS_ACTIVATED = env.bool("INBOUND_EMAIL_IS_ACTIVATED", True)

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)

# Caching
Expand Down
10 changes: 9 additions & 1 deletion lemarche/conversations/factories.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import factory
from factory.django import DjangoModelFactory

from lemarche.conversations.models import Conversation
from lemarche.conversations.models import Conversation, TemplateTransactional
from lemarche.siaes.factories import SiaeFactory


Expand All @@ -15,3 +15,11 @@ class Meta:
sender_email = factory.Sequence("email{0}@beta.gouv.fr".format)
siae = factory.SubFactory(SiaeFactory)
initial_body_message = factory.Faker("name", locale="fr_FR")


class TemplateTransactionalFactory(DjangoModelFactory):
class Meta:
model = TemplateTransactional

name = factory.Faker("name", locale="fr_FR")
code = factory.Faker("name", locale="fr_FR")
33 changes: 14 additions & 19 deletions lemarche/www/tenders/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from lemarche.tenders.models import PartnerShareTender, Tender, TenderSiae
from lemarche.users.models import User
from lemarche.utils import constants
from lemarche.utils.apis import api_brevo, api_mailjet, api_slack
from lemarche.utils.apis import api_mailjet, api_slack
from lemarche.utils.data import date_to_string
from lemarche.utils.emails import send_mail_async, whitelist_recipient_list
from lemarche.utils.urls import get_domain_url, get_object_admin_url, get_object_share_url
Expand Down Expand Up @@ -445,27 +445,23 @@ def send_siae_interested_email_to_author(tender: Tender):

if tender_siae_detail_contact_click_count == 1:
should_send_email = True
email_subject = "Un premier prestataire intéressé !"
template_id = settings.MAILJET_TENDERS_AUTHOR_SIAE_INTERESTED_1_TEMPLATE_ID
email_template = TemplateTransactional.objects.get(code="TENDERS_AUTHOR_SIAE_INTERESTED_1")
elif tender_siae_detail_contact_click_count == 2:
should_send_email = True
email_subject = "Un deuxième prestataire intéressé !"
template_id = settings.MAILJET_TENDERS_AUTHOR_SIAE_INTERESTED_2_TEMPLATE_ID
email_template = TemplateTransactional.objects.get(code="TENDERS_AUTHOR_SIAE_INTERESTED_2")
elif tender_siae_detail_contact_click_count == 5:
should_send_email = True
email_subject = "Un cinquième prestataire intéressé !"
template_id = settings.MAILJET_TENDERS_AUTHOR_SIAE_INTERESTED_5_TEMPLATE_ID
email_template = TemplateTransactional.objects.get(code="TENDERS_AUTHOR_SIAE_INTERESTED_5")
elif tender_siae_detail_contact_click_count % 5 == 0:
should_send_email = True
email_subject = "5 nouveaux prestataires intéressés !"
template_id = settings.MAILJET_TENDERS_AUTHOR_SIAE_INTERESTED_5_MORE_TEMPLATE_ID
email_template = TemplateTransactional.objects.get(code="TENDERS_AUTHOR_SIAE_INTERESTED_5_MORE")
else:
pass

if should_send_email:
recipient_list = whitelist_recipient_list([tender.author.email]) # tender.contact_email ?
if recipient_list and not tender.contact_notifications_disabled:
recipient_email = recipient_list[0] if recipient_list else ""
if len(recipient_list) and not tender.contact_notifications_disabled:
recipient_email = recipient_list[0]
recipient_name = tender.author.full_name

variables = {
Expand All @@ -474,19 +470,17 @@ def send_siae_interested_email_to_author(tender: Tender):
"TENDER_SIAE_INTERESTED_LIST_URL": f"{get_object_share_url(tender)}/prestataires", # noqa
}

api_mailjet.send_transactional_email_with_template(
template_id=template_id,
email_template.send_transactional_email(
recipient_email=recipient_email,
recipient_name=recipient_name,
variables=variables,
subject=email_subject,
)

# log email
log_item = {
"action": "email_siae_interested",
"email_template": email_template.code,
"email_to": recipient_email,
"email_subject": email_subject,
# "email_body": email_body,
"email_timestamp": timezone.now().isoformat(),
}
Expand Down Expand Up @@ -668,9 +662,10 @@ def notify_admin_siae_wants_cocontracting(tender: Tender, siae: Siae):


def send_super_siaes_email_to_author(tender: Tender, top_siaes: list[Siae]):
email_template = TemplateTransactional.objects.get(code="TENDERS_AUTHOR_SUPER_SIAES")
recipient_list = whitelist_recipient_list([tender.author.email])
if recipient_list:
recipient_email = recipient_list[0] if recipient_list else ""
if len(recipient_list) and not tender.contact_notifications_disabled:
recipient_email = recipient_list[0]
recipient_name = tender.author.full_name

# Use transaction parameters of Brevo with loop for siaes, documentation :
Expand All @@ -694,8 +689,7 @@ def send_super_siaes_email_to_author(tender: Tender, top_siaes: list[Siae]):
}
)

api_brevo.send_transactional_email_with_template(
template_id=settings.BREVO_TENDERS_AUTHOR_SUPER_SIAES_TEMPLATE_ID,
email_template.send_transactional_email(
recipient_email=recipient_email,
recipient_name=recipient_name,
variables=variables,
Expand All @@ -704,6 +698,7 @@ def send_super_siaes_email_to_author(tender: Tender, top_siaes: list[Siae]):
# log email
log_item = {
"action": "email_super_siaes",
"email_template": email_template.code,
"email_to": recipient_email,
"email_timestamp": timezone.now().isoformat(),
"email_variables": variables,
Expand Down
2 changes: 2 additions & 0 deletions lemarche/www/tenders/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from django.utils import timezone
from sesame.utils import get_query_string as sesame_get_query_string

from lemarche.conversations.factories import TemplateTransactionalFactory
from lemarche.perimeters.factories import PerimeterFactory
from lemarche.perimeters.models import Perimeter
from lemarche.sectors.factories import SectorFactory
Expand Down Expand Up @@ -1329,6 +1330,7 @@ def setUpTestData(cls):
cls.tender_contact_click_stat_url = reverse(
"tenders:detail-contact-click-stat", kwargs={"slug": cls.tender.slug}
)
TemplateTransactionalFactory(code="TENDERS_AUTHOR_SIAE_INTERESTED_1")

def test_anonymous_user_cannot_notify_interested(self):
response = self.client.get(self.tender_detail_url)
Expand Down

0 comments on commit c02db9f

Please sign in to comment.