diff --git a/clevercloud/crm_brevo_sync.sh b/clevercloud/crm_brevo_sync_companies.sh similarity index 59% rename from clevercloud/crm_brevo_sync.sh rename to clevercloud/crm_brevo_sync_companies.sh index bd0e21e3f..9a9204523 100644 --- a/clevercloud/crm_brevo_sync.sh +++ b/clevercloud/crm_brevo_sync_companies.sh @@ -1,10 +1,10 @@ #!/bin/bash -l -# Sync with Brevo CRM (Siae companies, ...) +# Sync Siae with Brevo CRM (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..." +if [[ -z "$CRON_CRM_BREVO_SYNC_COMPANIES_ENABLED" ]]; then + echo "CRON_CRM_BREVO_SYNC_COMPANIES_ENABLED not set. Exiting..." exit 0 fi @@ -19,4 +19,4 @@ fi # $APP_HOME is set by default by clever cloud. cd $APP_HOME -django-admin crm_brevo_sync +django-admin crm_brevo_sync_companies --recently-updated diff --git a/clevercloud/cron.json b/clevercloud/cron.json index 07504baf1..7fd235067 100644 --- a/clevercloud/cron.json +++ b/clevercloud/cron.json @@ -12,7 +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_and_count_fields.sh", - "55 7 * * 1 $ROOT/clevercloud/crm_brevo_sync.sh", + "55 7 * * 1 $ROOT/clevercloud/crm_brevo_sync_companies.sh", "0 7 * * 2 $ROOT/clevercloud/siaes_send_completion_reminder_emails.sh", "30 7 * * * $ROOT/clevercloud/tenders_send_author_list_of_super_siaes_emails.sh", "0 8 * * * $ROOT/clevercloud/siaes_send_user_request_reminder_emails.sh", @@ -21,4 +21,4 @@ "0 9 * * * $ROOT/clevercloud/tenders_send_siae_contacted_reminder_emails.sh", "10 9 * * * $ROOT/clevercloud/tenders_send_siae_interested_reminder_emails.sh", "*/5 8-15 * * 1-5 $ROOT/clevercloud/tenders_send_validated.sh" -] \ No newline at end of file +] diff --git a/lemarche/crm/management/commands/crm_brevo_sync.py b/lemarche/crm/management/commands/crm_brevo_sync.py deleted file mode 100644 index 8a015bf61..000000000 --- a/lemarche/crm/management/commands/crm_brevo_sync.py +++ /dev/null @@ -1,38 +0,0 @@ -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=10) - - -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__gte=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/crm/management/commands/crm_brevo_sync_companies.py b/lemarche/crm/management/commands/crm_brevo_sync_companies.py new file mode 100644 index 000000000..c8f71c99a --- /dev/null +++ b/lemarche/crm/management/commands/crm_brevo_sync_companies.py @@ -0,0 +1,46 @@ +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=10) + + +class Command(BaseCommand): + """ + Script to send Siae to Brevo CRM (companies) + + Usage: + python manage.py crm_brevo_sync_companies --recently-updated + python manage.py crm_brevo_sync_companies + """ + + def add_arguments(self, parser): + parser.add_argument( + "--recently-updated", dest="recently_updated", action="store_true", help="Only sync recently updated Siaes" + ) + + def handle(self, recently_updated: bool, **options): + self.stdout.write("-" * 80) + self.stdout.write("Script to sync Siaes with Brevo CRM (companies)...") + + # Step 1: build the queryset + siaes_qs = Siae.objects.all() + self.stdout.write(f"Sync Siae > Brevo: we have {Siae.objects.count()} siaes") + # Update only the recently updated siaes + if recently_updated: + siaes_qs = siaes_qs.filter(updated_at__gte=ten_days_ago) + self.stdout.write(f"Sync Siae > Brevo: {siaes_qs.count()} recently updated") + + # Step 2: loop on the siaes + for index, siae in enumerate(siaes_qs): + api_brevo.create_or_update_company(siae) + if (index % 10) == 0: # avoid API rate-limiting + time.sleep(1) + if (index % 500) == 0: + print(f"{index}...") diff --git a/lemarche/utils/apis/api_brevo.py b/lemarche/utils/apis/api_brevo.py index 7ecddff01..31a3c73ce 100644 --- a/lemarche/utils/apis/api_brevo.py +++ b/lemarche/utils/apis/api_brevo.py @@ -83,6 +83,11 @@ def create_or_update_company(siae): siae_brevo_company_body = sib_api_v3_sdk.Body( name=siae.name, attributes={ + # default attributes + # name, owner, linked_contacts, revenue, number_of_employees, created_at, last_updated_at, next_activity_date, owner_assign_date, number_of_contacts, number_of_activities, industry # noqa + "domain": siae.website, + "phone_number": siae.contact_phone_display, + # custom attributes "app_id": siae.id, "siae": True, "active": siae.is_active, @@ -92,8 +97,6 @@ def create_or_update_company(siae): "address_post_code": siae.post_code, "address_city": siae.city, "contact_email": siae.contact_email, - "contact_phone": siae.contact_phone_display, - "domain": siae.website, "logo_url": siae.logo_url, "geo_range": siae.geo_range, "app_url": get_object_share_url(siae),