From 988a3752a4de8f47278578395d8d4b1b3f85372b Mon Sep 17 00:00:00 2001 From: Raphael Odini Date: Tue, 27 Feb 2024 12:47:23 +0100 Subject: [PATCH] CRM: new script to sync siae data with Brevo companies --- clevercloud/crm_brevo_sync.sh | 22 +++++++++++ clevercloud/cron.json | 1 + config/settings/base.py | 2 + lemarche/crm/__init__.py | 0 .../crm/management/commands/crm_brevo_sync.py | 38 +++++++++++++++++++ 5 files changed, 63 insertions(+) create mode 100644 clevercloud/crm_brevo_sync.sh create mode 100644 lemarche/crm/__init__.py create mode 100644 lemarche/crm/management/commands/crm_brevo_sync.py diff --git a/clevercloud/crm_brevo_sync.sh b/clevercloud/crm_brevo_sync.sh new file mode 100644 index 000000000..bd0e21e3f --- /dev/null +++ b/clevercloud/crm_brevo_sync.sh @@ -0,0 +1,22 @@ +#!/bin/bash -l + +# Sync with Brevo CRM (Siae companies, ...) + +# Do not run if this env var is not set: +if [[ -z "$CRON_CRM_BREVO_SYNC_ENABLED" ]]; then + echo "CRON_CRM_BREVO_SYNC_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 crm_brevo_sync diff --git a/clevercloud/cron.json b/clevercloud/cron.json index f377b9235..bfa2d3ccb 100644 --- a/clevercloud/cron.json +++ b/clevercloud/cron.json @@ -12,6 +12,7 @@ "25 7 * * 1 $ROOT/clevercloud/siaes_update_count_fields.sh", "30 7 * * 1 $ROOT/clevercloud/siaes_update_super_badge_field.sh", "50 7 * * 1 $ROOT/clevercloud/companies_update_users.sh", + "55 7 * * 1 $ROOT/clevercloud/crm_brevo_sync.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", diff --git a/config/settings/base.py b/config/settings/base.py index 5c1a4c532..00aa710da 100644 --- a/config/settings/base.py +++ b/config/settings/base.py @@ -147,6 +147,8 @@ "lemarche.stats", # CMS (Wagtail) "lemarche.cms", + # Brevo CRM + "lemarche.crm", ] WAGTAIL_APPS = [ diff --git a/lemarche/crm/__init__.py b/lemarche/crm/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/lemarche/crm/management/commands/crm_brevo_sync.py b/lemarche/crm/management/commands/crm_brevo_sync.py new file mode 100644 index 000000000..488f18505 --- /dev/null +++ b/lemarche/crm/management/commands/crm_brevo_sync.py @@ -0,0 +1,38 @@ +import time +from datetime import timedelta + +from django.utils import timezone + +from lemarche.siaes.models import Siae +from lemarche.utils.apis import api_brevo +from lemarche.utils.commands import BaseCommand + + +ten_days_ago = timezone.now() - timedelta(days=4) + + +class Command(BaseCommand): + """ + (Weekly) script to send Siae to Brevo CRM (companies) + + Usage: + python manage.py crm_brevo_sync + """ + + def handle(self, **options): + self.stdout.write("-" * 80) + self.stdout.write("Script to sync with Brevo CRM...") + + # SIAE --> companies + # Update only the recently updated + siaes_qs = Siae.objects.filter(updated_at__lte=ten_days_ago) + progress = 0 + + self.stdout.write( + f"Companies: updating our {Siae.objects.count()} siaes. {siaes_qs.count()} recently updated." + ) + for siae in siaes_qs: + api_brevo.create_or_update_company(siae) + progress += 1 + if (progress % 10) == 0: # avoid API rate-limiting + time.sleep(1)