From ee1c396c7b22a1d751e5408a9e78450c22480fa8 Mon Sep 17 00:00:00 2001 From: Zach Aysan Date: Thu, 28 Mar 2024 13:21:53 -0400 Subject: [PATCH] feat: Add Hubspot lead tracking for Hubspot data (#3647) --- .../lead_tracking/hubspot/lead_tracker.py | 6 +++-- .../test_unit_hubspot_lead_tracking.py | 10 +++++--- .../migrations/0036_create_hubspot_lead.py | 25 +++++++++++++++++++ api/users/models.py | 11 ++++++++ 4 files changed, 47 insertions(+), 5 deletions(-) create mode 100644 api/users/migrations/0036_create_hubspot_lead.py diff --git a/api/integrations/lead_tracking/hubspot/lead_tracker.py b/api/integrations/lead_tracking/hubspot/lead_tracker.py index 59a225240e98..489f74ec25e2 100644 --- a/api/integrations/lead_tracking/hubspot/lead_tracker.py +++ b/api/integrations/lead_tracking/hubspot/lead_tracker.py @@ -8,7 +8,7 @@ Organisation, Subscription, ) -from users.models import FFAdminUser +from users.models import FFAdminUser, HubspotLead from .client import HubspotClient @@ -60,7 +60,9 @@ def create_lead(self, user: FFAdminUser, organisation: Organisation) -> None: hubspot_id = self.get_or_create_organisation_hubspot_id(organisation, user) - self.client.create_contact(user, hubspot_id) + response = self.client.create_contact(user, hubspot_id) + + HubspotLead.objects.create(user=user, hubspot_id=response["id"]) def get_or_create_organisation_hubspot_id( self, organisation: Organisation, user: FFAdminUser diff --git a/api/tests/unit/integrations/lead_tracking/hubspot/test_unit_hubspot_lead_tracking.py b/api/tests/unit/integrations/lead_tracking/hubspot/test_unit_hubspot_lead_tracking.py index b525d967832c..680bb51ff659 100644 --- a/api/tests/unit/integrations/lead_tracking/hubspot/test_unit_hubspot_lead_tracking.py +++ b/api/tests/unit/integrations/lead_tracking/hubspot/test_unit_hubspot_lead_tracking.py @@ -9,7 +9,7 @@ OrganisationRole, ) from task_processor.task_run_method import TaskRunMethod -from users.models import FFAdminUser +from users.models import FFAdminUser, HubspotLead def test_hubspot_with_new_contact_and_new_organisation( @@ -54,13 +54,14 @@ def test_hubspot_with_new_contact_and_new_organisation( return_value=None, ) + hubspot_lead_id = "1000551" mock_create_contact = mocker.patch( "integrations.lead_tracking.hubspot.client.HubspotClient.create_contact", return_value={ "archived": False, "archived_at": None, "created_at": datetime.datetime(2024, 2, 26, 20, 2, 50, 69000), - "id": "1000551", + "id": hubspot_lead_id, "properties": { "createdate": "2024-02-26T20:02:50.069Z", "email": user.email, @@ -88,6 +89,7 @@ def test_hubspot_with_new_contact_and_new_organisation( user.add_organisation(organisation, role=OrganisationRole.ADMIN) # Then + assert HubspotLead.objects.filter(hubspot_id=hubspot_lead_id).exists() is True organisation.refresh_from_db() assert organisation.hubspot_organisation.hubspot_id == future_hubspot_id @@ -221,13 +223,14 @@ def test_hubspot_with_new_contact_and_existing_organisation( return_value=None, ) + hubspot_lead_id = "1000551" mock_create_contact = mocker.patch( "integrations.lead_tracking.hubspot.client.HubspotClient.create_contact", return_value={ "archived": False, "archived_at": None, "created_at": datetime.datetime(2024, 2, 26, 20, 2, 50, 69000), - "id": "1000551", + "id": hubspot_lead_id, "properties": { "createdate": "2024-02-26T20:02:50.069Z", "email": user.email, @@ -254,6 +257,7 @@ def test_hubspot_with_new_contact_and_existing_organisation( user.add_organisation(organisation, role=OrganisationRole.ADMIN) # Then + assert HubspotLead.objects.filter(hubspot_id=hubspot_lead_id).exists() is True mock_create_company.assert_not_called() mock_create_contact.assert_called_once_with(user, hubspot_id) mock_get_contact.assert_called_once_with(user) diff --git a/api/users/migrations/0036_create_hubspot_lead.py b/api/users/migrations/0036_create_hubspot_lead.py new file mode 100644 index 000000000000..ad06ca94eac2 --- /dev/null +++ b/api/users/migrations/0036_create_hubspot_lead.py @@ -0,0 +1,25 @@ +# Generated by Django 3.2.24 on 2024-03-21 17:16 + +from django.conf import settings +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + dependencies = [ + ('users', '0035_add_ldap_dn'), + ] + + operations = [ + migrations.CreateModel( + name='HubspotLead', + fields=[ + ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('hubspot_id', models.CharField(max_length=100, unique=True)), + ('created_at', models.DateTimeField(auto_now_add=True)), + ('updated_at', models.DateTimeField(auto_now=True)), + ('user', models.OneToOneField(on_delete=django.db.models.deletion.CASCADE, related_name='hubspot_lead', to=settings.AUTH_USER_MODEL)), + ], + ), + ] diff --git a/api/users/models.py b/api/users/models.py index 7c8f48915dfc..31411e0208b8 100644 --- a/api/users/models.py +++ b/api/users/models.py @@ -411,3 +411,14 @@ def add_users_by_id(self, user_ids: list): def remove_users_by_id(self, user_ids: list): self.users.remove(*user_ids) + + +class HubspotLead(models.Model): + user = models.OneToOneField( + FFAdminUser, + related_name="hubspot_lead", + on_delete=models.CASCADE, + ) + hubspot_id = models.CharField(unique=True, max_length=100, null=False) + created_at = models.DateTimeField(auto_now_add=True) + updated_at = models.DateTimeField(auto_now=True)