Skip to content

Commit

Permalink
feat: Add domain to Hubspot company (#3648)
Browse files Browse the repository at this point in the history
  • Loading branch information
zachaysan committed Mar 27, 2024
1 parent 44ed1bf commit 87d2d52
Show file tree
Hide file tree
Showing 4 changed files with 138 additions and 5 deletions.
26 changes: 25 additions & 1 deletion api/app/settings/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -1036,7 +1036,31 @@
ENABLE_HUBSPOT_LEAD_TRACKING = env.bool("ENABLE_HUBSPOT_LEAD_TRACKING", False)
HUBSPOT_IGNORE_DOMAINS = env.list("HUBSPOT_IGNORE_DOMAINS", [])
HUBSPOT_IGNORE_DOMAINS_REGEX = env("HUBSPOT_IGNORE_DOMAINS_REGEX", "")

HUBSPOT_IGNORE_ORGANISATION_DOMAINS = env.list(
"HUBSPOT_IGNORE_ORGANISATION_DOMAINS",
[
"126.com",
"163.com",
"aol.com",
"att.net",
"comcast.net",
"gmail.com",
"gmx.com",
"hotmail.com",
"icloud.com",
"live.com",
"mail.com",
"mail.ru",
"outlook.co.uk",
"outlook.com",
"protonmail.com",
"qq.com",
"sina.com",
"yahoo.com",
"yandex.com",
"zoho.com",
],
)

# List of plan ids that support seat upgrades
AUTO_SEAT_UPGRADE_PLANS = env.list("AUTO_SEAT_UPGRADE_PLANS", default=[])
Expand Down
8 changes: 7 additions & 1 deletion api/integrations/lead_tracking/hubspot/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,19 @@ def create_contact(self, user: FFAdminUser, hubspot_company_id: str) -> dict:
return response.to_dict()

def create_company(
self, name: str, active_subscription: str, organisation_id: int
self,
name: str,
active_subscription: str,
organisation_id: int,
domain: str | None,
) -> dict:
properties = {
"name": name,
"active_subscription": active_subscription,
"orgid": str(organisation_id),
}
if domain:
properties["domain"] = domain

simple_public_object_input_for_create = SimplePublicObjectInputForCreate(
properties=properties,
Expand Down
11 changes: 9 additions & 2 deletions api/integrations/lead_tracking/hubspot/lead_tracker.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,21 +58,28 @@ def create_lead(self, user: FFAdminUser, organisation: Organisation) -> None:
# for an existing organisation, so return early.
return

hubspot_id = self.get_or_create_organisation_hubspot_id(organisation)
hubspot_id = self.get_or_create_organisation_hubspot_id(organisation, user)

self.client.create_contact(user, hubspot_id)

def get_or_create_organisation_hubspot_id(self, organisation: Organisation) -> str:
def get_or_create_organisation_hubspot_id(
self, organisation: Organisation, user: FFAdminUser
) -> str:
"""
Return the Hubspot API's id for an organisation.
"""
if getattr(organisation, "hubspot_organisation", None):
return organisation.hubspot_organisation.hubspot_id

if user.email_domain in settings.HUBSPOT_IGNORE_ORGANISATION_DOMAINS:
domain = None
else:
domain = user.email_domain
response = self.client.create_company(
name=organisation.name,
active_subscription=organisation.subscription.plan,
organisation_id=organisation.id,
domain=domain,
)

# Store the organisation data in the database since we are
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,102 @@ def test_hubspot_with_new_contact_and_new_organisation(
) -> None:
# Given
settings.ENABLE_HUBSPOT_LEAD_TRACKING = True
domain = "example.com"
user = FFAdminUser.objects.create(
email="new.user@example.com",
email=f"new.user@{domain}",
first_name="Frank",
last_name="Louis",
marketing_consent_given=True,
)

future_hubspot_id = "10280696017"
mock_create_company = mocker.patch(
"integrations.lead_tracking.hubspot.client.HubspotClient.create_company",
return_value={
"id": future_hubspot_id,
"properties": {
"createdate": "2024-02-26T19:41:57.959Z",
"hs_lastmodifieddate": "2024-02-26T19:41:57.959Z",
"hs_object_id": future_hubspot_id,
"hs_object_source": "INTEGRATION",
"hs_object_source_id": "2902325",
"hs_object_source_label": "INTEGRATION",
"name": organisation.name,
},
"properties_with_history": None,
"created_at": datetime.datetime(2024, 2, 26, 19, 41, 57, 959000),
"updated_at": datetime.datetime(2024, 2, 26, 19, 41, 57, 959000),
"archived": False,
"archived_at": None,
},
)

mock_get_contact = mocker.patch(
"integrations.lead_tracking.hubspot.client.HubspotClient.get_contact",
return_value=None,
)

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",
"properties": {
"createdate": "2024-02-26T20:02:50.069Z",
"email": user.email,
"firstname": user.first_name,
"hs_all_contact_vids": "1000551",
"hs_email_domain": "example.com",
"hs_is_contact": "true",
"hs_is_unworked": "true",
"hs_marketable_status": user.marketing_consent_given,
"hs_object_id": "1000551",
"hs_object_source": "INTEGRATION",
"hs_object_source_id": "2902325",
"hs_object_source_label": "INTEGRATION",
"hs_pipeline": "contacts-lifecycle-pipeline",
"lastmodifieddate": "2024-02-26T20:02:50.069Z",
"lastname": user.last_name,
},
"properties_with_history": None,
"updated_at": datetime.datetime(2024, 2, 26, 20, 2, 50, 69000),
},
)

assert getattr(organisation, "hubspot_organisation", None) is None
# When
user.add_organisation(organisation, role=OrganisationRole.ADMIN)

# Then
organisation.refresh_from_db()
assert organisation.hubspot_organisation.hubspot_id == future_hubspot_id

mock_create_company.assert_called_once_with(
name=organisation.name,
domain=domain,
active_subscription="free",
organisation_id=organisation.id,
)
mock_create_contact.assert_called_once_with(user, future_hubspot_id)
mock_get_contact.assert_called_once_with(user)


def test_hubspot_with_filtered_email_domain_contact_and_new_organisation(
organisation: Organisation,
settings: SettingsWrapper,
mocker: MockerFixture,
) -> None:
# Given
settings.ENABLE_HUBSPOT_LEAD_TRACKING = True
domain = "example.com"

# Setting that will filter out the domain for the user.
settings.HUBSPOT_IGNORE_ORGANISATION_DOMAINS = [domain]

user = FFAdminUser.objects.create(
email=f"new.user@{domain}",
first_name="Frank",
last_name="Louis",
marketing_consent_given=True,
Expand Down Expand Up @@ -89,8 +183,10 @@ def test_hubspot_with_new_contact_and_new_organisation(
# Then
organisation.refresh_from_db()
assert organisation.hubspot_organisation.hubspot_id == future_hubspot_id
# Domain is `None` since it was filtered out.
mock_create_company.assert_called_once_with(
name=organisation.name,
domain=None,
active_subscription="free",
organisation_id=organisation.id,
)
Expand Down

0 comments on commit 87d2d52

Please sign in to comment.