Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(Brevo): Améliorer le script de synchro des structures vers Brevo #1229

Merged
merged 3 commits into from
May 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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
4 changes: 2 additions & 2 deletions clevercloud/cron.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
raphodn marked this conversation as resolved.
Show resolved Hide resolved
"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",
Expand All @@ -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"
]
]
38 changes: 0 additions & 38 deletions lemarche/crm/management/commands/crm_brevo_sync.py

This file was deleted.

46 changes: 46 additions & 0 deletions lemarche/crm/management/commands/crm_brevo_sync_companies.py
Original file line number Diff line number Diff line change
@@ -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}...")
7 changes: 5 additions & 2 deletions lemarche/utils/apis/api_brevo.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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),
Expand Down
Loading