Skip to content

Commit

Permalink
CRM: new script to sync siae data with Brevo companies
Browse files Browse the repository at this point in the history
  • Loading branch information
raphodn committed Feb 27, 2024
1 parent 780222a commit 352ffb7
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 2 deletions.
22 changes: 22 additions & 0 deletions clevercloud/crm_brevo_sync.sh
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions clevercloud/cron.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
2 changes: 2 additions & 0 deletions config/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,8 @@
"lemarche.stats",
# CMS (Wagtail)
"lemarche.cms",
# Brevo CRM
"lemarche.crm",
]

WAGTAIL_APPS = [
Expand Down
Empty file added lemarche/crm/__init__.py
Empty file.
38 changes: 38 additions & 0 deletions lemarche/crm/management/commands/crm_brevo_sync.py
Original file line number Diff line number Diff line change
@@ -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)
4 changes: 2 additions & 2 deletions lemarche/utils/apis/api_brevo.py
Original file line number Diff line number Diff line change
Expand Up @@ -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': '<brevo_company_id>'}
siae.set_brevo_id(api_response.id)
Expand Down

0 comments on commit 352ffb7

Please sign in to comment.