Skip to content

Commit

Permalink
Dépôt de besoin : envoyer un e-mail à J+30 aux auteurs pour savoir si…
Browse files Browse the repository at this point in the history
… il y a eu une transaction (#865)

* Rename unused existing management command

* Update task. option to filter on tender kind

* Add to cron
  • Loading branch information
raphodn authored Aug 22, 2023
1 parent 4f3eb0d commit d6071a9
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 10 deletions.
1 change: 1 addition & 0 deletions clevercloud/cron.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"40 7 * * 1 $ROOT/clevercloud/siaes_update_api_zrr_fields.sh",
"0 7 * * 2 $ROOT/clevercloud/siaes_send_completion_reminder_emails.sh",
"0 8 * * * $ROOT/clevercloud/siaes_send_user_request_reminder_emails.sh",
"30 8 * * * $ROOT/clevercloud/tenders_send_author_transactioned_question_emails.sh",
"0 9 * * * $ROOT/clevercloud/tenders_send_siae_contacted_reminder_emails.sh",
"10 9 * * * $ROOT/clevercloud/tenders_send_siae_interested_reminder_emails.sh",
"20 9 * * * $ROOT/clevercloud/tenders_send_author_incremental.sh"
Expand Down
22 changes: 22 additions & 0 deletions clevercloud/tenders_send_author_transactioned_question_emails.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#!/bin/bash -l

# Send email for tender transactioned question to author

# Do not run if this env var is not set:
if [[ -z "$CRON_TENDER_SEND_AUTHOR_TRANSACTIONED_QUESTION_ENABLED" ]]; then
echo "CRON_TENDER_SEND_AUTHOR_TRANSACTIONED_QUESTION_ENABLED not set. Exiting..."
exit 0
fi

# About clever cloud cronjobs:
# https://www.clever-cloud.com/doc/tools/crons/

if [[ "$INSTANCE_NUMBER" != "0" ]]; then
echo "Instance number is ${INSTANCE_NUMBER}. Stop here."
exit 0
fi

# $APP_HOME is set by default by clever cloud.
cd $APP_HOME

django-admin send_author_transactioned_question_emails --kind QUOTE
3 changes: 3 additions & 0 deletions config/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,9 @@
"MAILJET_TENDERS_AUTHOR_INCREMENTAL_2D_TEMPLATE_ID", 4585824
)
MAILJET_TENDERS_AUTHOR_FEEDBACK_30D_TEMPLATE_ID = env.int("MAILJET_TENDERS_AUTHOR_FEEDBACK_30D_TEMPLATE_ID", 4017446)
MAILJET_TENDERS_AUTHOR_TRANSACTIONED_QUESTION_30D_TEMPLATE_ID = env.int(
"MAILJET_TENDERS_AUTHOR_TRANSACTIONED_QUESTION_30D_TEMPLATE_ID", 4951625
)


# -- Sendinblue (BREVO)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,23 @@
from django.db.models import Q

from lemarche.tenders.models import Tender
from lemarche.www.tenders.tasks import send_tenders_author_feedback_30_days
from lemarche.www.tenders.tasks import send_tenders_author_30_days


class Command(BaseCommand):
"""
Daily script to send email feedback for tenders
Daily script to send an email to tender authors
When? J+30 after validation of tenders
Usage:
python manage.py send_user_tenders_feedback --dry-run
python manage.py send_user_tenders_feedback
python manage.py send_author_transactioned_question_emails --dry-run
python manage.py send_author_transactioned_question_emails --all
python manage.py send_author_transactioned_question_emails --kind QUOTE
python manage.py send_author_transactioned_question_emails
"""

def add_arguments(self, parser):
parser.add_argument("--kind", type=str, dest="kind")
parser.add_argument("--dry-run", dest="dry_run", action="store_true", help="Dry run, no sends")
parser.add_argument(
"--all",
Expand All @@ -26,13 +29,15 @@ def add_arguments(self, parser):
help="Send to all tenders validated 30 days ago or more",
)

def handle(self, dry_run=False, is_all_tenders=False, **options):
def handle(self, kind=None, dry_run=False, is_all_tenders=False, **options):
self.stdout.write("-" * 80)
self.stdout.write("Script to send email feedback for tenders...")

self.stdout.write("-" * 80)
thirty_days_ago = datetime.today().date() - timedelta(days=30)
tenders_validated = Tender.objects.validated()
if kind:
tenders_validated = tenders_validated.filter(kind=kind)
if is_all_tenders:
# all tenders validated 30 days ago or more
tenders_for_feedbacks = tenders_validated.filter(validated_at__date__lte=thirty_days_ago)
Expand All @@ -47,7 +52,7 @@ def handle(self, dry_run=False, is_all_tenders=False, **options):

if not dry_run:
for tender in tenders_for_feedbacks:
send_tenders_author_feedback_30_days(tender)
send_tenders_author_30_days(tender, kind="transactioned_question")
self.stdout.write(f"Sent {tenders_for_feedbacks.count()} J+30 feedbacks")

self.stdout.write("-" * 80)
Expand Down
11 changes: 8 additions & 3 deletions lemarche/www/tenders/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -484,7 +484,7 @@ def send_author_incremental_2_days_email(tender: Tender):
tender.save()


def send_tenders_author_feedback_30_days(tender: Tender):
def send_tenders_author_30_days(tender: Tender, kind="feedback"):
email_subject = f"Concernant votre {tender.get_kind_display()} sur le Marché de l'inclusion"
recipient_list = whitelist_recipient_list([tender.author.email])
if recipient_list:
Expand All @@ -498,8 +498,13 @@ def send_tenders_author_feedback_30_days(tender: Tender):
"TENDER_KIND": tender.get_kind_display(),
}

if kind == "transactioned_question":
template_id = settings.MAILJET_TENDERS_AUTHOR_TRANSACTIONED_QUESTION_30D_TEMPLATE_ID
else:
template_id = settings.MAILJET_TENDERS_AUTHOR_FEEDBACK_30D_TEMPLATE_ID

api_mailjet.send_transactional_email_with_template(
template_id=settings.MAILJET_TENDERS_AUTHOR_FEEDBACK_30D_TEMPLATE_ID,
template_id=template_id,
subject=email_subject,
recipient_email=recipient_email,
recipient_name=recipient_name,
Expand All @@ -508,7 +513,7 @@ def send_tenders_author_feedback_30_days(tender: Tender):

# log email
log_item = {
"action": "email_feedback_30d_sent",
"action": f"email_{kind}_30d_sent",
"email_to": recipient_email,
"email_subject": email_subject,
# "email_body": email_body,
Expand Down
2 changes: 1 addition & 1 deletion lemarche/www/tenders/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -873,7 +873,7 @@ def test_update_tendersiae_stats_on_tender_contact_click(self):

# def test_send_email_for_feedbacks_set_log(self):
# self.assertEqual(len(self.tender.logs), 0)
# send_tenders_author_feedback_30_days(self.tender)
# send_tenders_author_30_days(self.tender, kind="feedback")
# # fetch tender to be sure to have the last version of tender
# tender: Tender = Tender.objects.get(pk=self.tender.pk)
# self.assertEqual(len(tender.logs), 1)
Expand Down

0 comments on commit d6071a9

Please sign in to comment.