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) diff --git a/lemarche/utils/apis/api_brevo.py b/lemarche/utils/apis/api_brevo.py index 004129e48..bb4b32e0a 100644 --- a/lemarche/utils/apis/api_brevo.py +++ b/lemarche/utils/apis/api_brevo.py @@ -99,14 +99,14 @@ def create_or_update_company(siae: Siae): if siae.brevo_company_id: # update try: api_response = api_instance.companies_id_patch(siae.brevo_company_id, siae_brevo_company_body) - logger.info("Success Brevo->CompaniesApi->create_or_update_company (update): %s" % api_response) + # logger.info("Success Brevo->CompaniesApi->create_or_update_company (update): %s" % api_response) # api_response: {'attributes': None, 'id': None, 'linked_contacts_ids': None, 'linked_deals_ids': None} except ApiException as e: logger.error("Exception when calling Brevo->CompaniesApi->create_or_update_company (update): %s" % e) else: # create try: api_response = api_instance.companies_post(siae_brevo_company_body) - logger.info("Success Brevo->CompaniesApi->create_or_update_company (create): %s" % api_response) + # logger.info("Success Brevo->CompaniesApi->create_or_update_company (create): %s" % api_response) print(api_response.id) # api_response: {'id': ''} siae.set_brevo_id(api_response.id)