Skip to content

Commit

Permalink
feat: Add Hubspot lead tracking for Hubspot data (#3647)
Browse files Browse the repository at this point in the history
  • Loading branch information
zachaysan authored Mar 28, 2024
1 parent ce38ab7 commit ee1c396
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 5 deletions.
6 changes: 4 additions & 2 deletions api/integrations/lead_tracking/hubspot/lead_tracker.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
Organisation,
Subscription,
)
from users.models import FFAdminUser
from users.models import FFAdminUser, HubspotLead

from .client import HubspotClient

Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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,
Expand All @@ -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)
Expand Down
25 changes: 25 additions & 0 deletions api/users/migrations/0036_create_hubspot_lead.py
Original file line number Diff line number Diff line change
@@ -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)),
],
),
]
11 changes: 11 additions & 0 deletions api/users/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

0 comments on commit ee1c396

Please sign in to comment.