Skip to content

Commit 4b676b5

Browse files
Zaimwa9emyller
andauthored
fix: rely-solely-on-hubspot-to-create-organisations (#5739)
Co-authored-by: Evandro Myller <22429+emyller@users.noreply.github.com>
1 parent 567ffee commit 4b676b5

2 files changed

Lines changed: 11 additions & 27 deletions

File tree

api/integrations/lead_tracking/hubspot/lead_tracker.py

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ def create_lead(self, user: FFAdminUser, organisation: Organisation) -> None:
9595
hubspot_contact_id = self._get_or_create_user_hubspot_id(user)
9696
if not hubspot_contact_id:
9797
return
98-
hubspot_org_id = self._get_or_create_organisation_hubspot_id(user, organisation)
98+
hubspot_org_id = self._get_organisation_hubspot_id(user, organisation)
9999
if not hubspot_org_id:
100100
return
101101

@@ -134,7 +134,7 @@ def _get_or_create_user_hubspot_id(self, user: FFAdminUser) -> str | None:
134134

135135
return hubspot_contact_id
136136

137-
def _get_or_create_organisation_hubspot_id(
137+
def _get_organisation_hubspot_id(
138138
self,
139139
user: FFAdminUser,
140140
organisation: Organisation,
@@ -154,10 +154,12 @@ def _get_or_create_organisation_hubspot_id(
154154
company_kwargs["organisation_id"] = organisation.id
155155
company_kwargs["active_subscription"] = organisation.subscription.plan
156156

157-
# As Hubspot creates/associates companies based on contact domain
157+
# As Hubspot creates/associates companies automatically based on contact domain
158158
# we need to get the hubspot id when this user creates the company for the first time
159159
# and update the company name
160-
company = self._get_or_create_hubspot_company(**company_kwargs)
160+
company = self._get_hubspot_company_by_domain(domain)
161+
if not company:
162+
return None
161163
org_hubspot_id: str = company["id"]
162164

163165
properties = company.get("properties", {})
@@ -177,21 +179,11 @@ def _get_or_create_organisation_hubspot_id(
177179

178180
return org_hubspot_id
179181

180-
def _get_or_create_hubspot_company(
182+
def _get_hubspot_company_by_domain(
181183
self,
182184
domain: str,
183-
organisation_id: int,
184-
name: str,
185-
active_subscription: str | None = None,
186185
) -> dict[str, Any]:
187186
company = self.client.get_company_by_domain(domain)
188-
if not company:
189-
company = self.client.create_company(
190-
name=name,
191-
domain=domain,
192-
organisation_id=organisation_id,
193-
active_subscription=active_subscription,
194-
)
195187

196188
return company # type: ignore[no-any-return]
197189

api/tests/unit/integrations/lead_tracking/hubspot/test_unit_hubspot_lead_tracking.py

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ def test_hubspot_user_org_hook_creates_hubspot_user_and_organisation_association
176176
mock_client_existing_contact.get_contact.assert_called_once_with(user)
177177

178178

179-
def test_create_organisation_lead_creates_contact_when_not_found(
179+
def test_create_organisation_lead_creates_contact_when_not_found_but_not_company(
180180
organisation: Organisation,
181181
mocker: MockerFixture,
182182
) -> None:
@@ -209,16 +209,8 @@ def test_create_organisation_lead_creates_contact_when_not_found(
209209

210210
assert mock_client.get_contact.call_count == 2
211211
mock_client.create_lead_form.assert_called_once_with(user=user)
212-
mock_client.create_company.assert_called_once_with(
213-
name=organisation.name,
214-
active_subscription="free",
215-
organisation_id=organisation.id,
216-
domain="example.com",
217-
)
218-
mock_client.associate_contact_to_company.assert_called_once_with(
219-
contact_id=HUBSPOT_USER_ID,
220-
company_id=HUBSPOT_COMPANY_ID,
221-
)
212+
mock_client.create_company.assert_not_called() # We rely on Hubspot creating contacts
213+
mock_client.associate_contact_to_company.assert_not_called()
222214

223215

224216
def test_create_organisation_lead_creates_contact_for_existing_org(
@@ -413,7 +405,7 @@ def test_create_leads_skips_association_on_missing_ids(
413405
tracker, "_get_or_create_user_hubspot_id", return_value=hubspot_contact_id
414406
)
415407
mocker.patch.object(
416-
tracker, "_get_or_create_organisation_hubspot_id", return_value=hubspot_org_id
408+
tracker, "_get_organisation_hubspot_id", return_value=hubspot_org_id
417409
)
418410

419411
# When

0 commit comments

Comments
 (0)