Skip to content

Commit

Permalink
Add Tender task send_tenders_siaes_survey
Browse files Browse the repository at this point in the history
  • Loading branch information
raphodn committed Feb 19, 2024
1 parent 0503860 commit 242c476
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 13 deletions.
3 changes: 3 additions & 0 deletions config/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,9 @@
MAILJET_TENDERS_AUTHOR_TRANSACTIONED_QUESTION_7D_TEMPLATE_ID = env.int(
"MAILJET_TENDERS_AUTHOR_TRANSACTIONED_QUESTION_7D_TEMPLATE_ID", 5207181
) # 4951625
MAILJET_TENDERS_SIAE_TRANSACTIONED_QUESTION_7D_TEMPLATE_ID = env.int(
"MAILJET_TENDERS_SIAE_TRANSACTIONED_QUESTION_7D_TEMPLATE_ID", 5207266
)


# -- Sendinblue (BREVO)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

from django.core.management.base import BaseCommand

from lemarche.tenders.models import TenderSiae
from lemarche.www.tenders.tasks import send_tenders_siae_survey
from lemarche.tenders.models import Tender
from lemarche.www.tenders.tasks import send_tenders_siaes_survey


seven_days_ago = datetime.today().date() - timedelta(days=7)
Expand Down Expand Up @@ -32,18 +32,11 @@ def handle(self, kind=None, reminder=False, dry_run=False, **options):
self.stdout.write("Script to send email tender transactioned_question to interested siaes...")

# tender must be sent & start_working_date J+7
tendersiae_qs = TenderSiae.objects.filter(
tender__first_sent_at__isnull=False, tender__start_working_date=seven_days_ago
)
# siae must be interested
tendersiae_qs = tendersiae_qs.filter(detail_contact_click_date__isnull=False)
# siae must not have received the survey yet
tendersiae_qs = tendersiae_qs.filter(survey_transactioned_answer=None, survey_transactioned_send_date=None)
tender_qs = Tender.objects.sent().filter(start_working_date=seven_days_ago)

self.stdout.write(f"Found {tendersiae_qs.count()} tendersiaes")
self.stdout.write(f"Found {tender_qs.count()} tenders")

if not dry_run:
email_kind = "transactioned_question_7d"
for tendersiae in tendersiae_qs:
send_tenders_siae_survey(tendersiae, kind=email_kind)
self.stdout.write(f"Sent {tendersiae.count()} {email_kind}")
for tender in tender_qs:
send_tenders_siaes_survey(tender, kind=email_kind)
73 changes: 73 additions & 0 deletions lemarche/www/tenders/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -590,6 +590,79 @@ def send_tenders_author_feedback_or_survey(tender: Tender, kind="feedback_30d"):
tender.save()


def send_tenders_siaes_survey(tender: Tender, kind="transactioned_question_7d"):
tendersiae_qs = TenderSiae.objects.filter(tender=tender)

if kind == "transactioned_question_7d":
# siae must be interested
tendersiae_qs = tendersiae_qs.filter(detail_contact_click_date__isnull=False)
# siae must not have received the survey yet
tendersiae_qs = tendersiae_qs.filter(survey_transactioned_answer=None, survey_transactioned_send_date=None)

for tendersiae in tendersiae_qs:
send_tenders_siae_survey(tendersiae, kind=kind)

# log email batch
log_item = {
"action": f"email_siaes_{kind}_sent",
"siae_count": tendersiae_qs.count(),
"email_timestamp": timezone.now().isoformat(),
}
tender.logs.append(log_item)
tender.save()


def send_tenders_siae_survey(tendersiae: TenderSiae, kind="transactioned_question_7d"):
email_subject = f"Suite à notre demande de {tendersiae.tender.title}"
template_id = settings.MAILJET_TENDERS_SIAE_TRANSACTIONED_QUESTION_7D_TEMPLATE_ID

for user in tendersiae.siae.users.all():
recipient_list = whitelist_recipient_list([user.email])
if recipient_list:
recipient_email = recipient_list[0] if recipient_list else ""
recipient_name = user.full_name

variables = {
"TENDER_AUTHOR_FULL_NAME": tendersiae.tender.contact_full_name,
"tender_author_company": tendersiae.tender.author.company_name,
"TENDER_TITLE": tendersiae.tender.title,
"TENDER_VALIDATE_AT": tendersiae.tender.first_sent_at.strftime("%d %B %Y"), # TODO: TENDER_SENT_AT?
"TENDER_KIND": tendersiae.tender.get_kind_display(),
}

user_sesame_query_string = sesame_get_query_string(user) # TODO: sesame scope parameter
answer_url_with_sesame_token = (
f"https://{get_domain_url()}"
+ reverse(
"tenders:detail-siae-survey-transactioned", args=[tendersiae.tender.slug, tendersiae.siae.slug]
)
+ user_sesame_query_string
)
variables["ANSWER_YES_URL"] = answer_url_with_sesame_token + "&answer=True"
variables["ANSWER_NO_URL"] = answer_url_with_sesame_token + "&answer=False"
# add timestamp
tendersiae.survey_transactioned_send_date = timezone.now()

api_mailjet.send_transactional_email_many_recipient_with_template(
template_id=template_id,
subject=email_subject,
recipient_email=recipient_email,
recipient_name=recipient_name,
variables=variables,
)

# log email
log_item = {
"action": f"email_{kind}_sent",
"email_to": recipient_list,
"email_subject": email_subject,
# "email_body": email_body,
"email_timestamp": timezone.now().isoformat(),
}
tendersiae.logs.append(log_item)
tendersiae.save()


def notify_admin_siae_wants_cocontracting(tender: Tender, siae: Siae):
email_subject = f"Marché de l'inclusion : la structure {siae.name} souhaite répondre en co-traitance"
tender_admin_url = get_admin_url_object(tender)
Expand Down

0 comments on commit 242c476

Please sign in to comment.