Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions hubspot_xpro/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,17 @@ def sync_contact_hubspot_ids_to_db():
alt_email_q |= Q(email__iexact=alt_email)
user = User.objects.filter(alt_email_q).first()
if user:
# Skip if this hubspot_id is already mapped to a different user
existing = (
HubspotObject.objects.filter(
content_type=content_type,
hubspot_id=contact.id,
)
.exclude(object_id=user.id)
.exists()
)
if existing:
continue
HubspotObject.objects.update_or_create(
content_type=content_type,
object_id=user.id,
Expand Down Expand Up @@ -332,6 +343,8 @@ def sync_deal_line_hubspot_ids_to_db(order, hubspot_order_id) -> bool:

matches = 0
expected_matches = 1 if is_b2b else order.lines.count()
if not line_items:
return False
if is_b2b or len(line_items) == 1:
HubspotObject.objects.update_or_create(
content_type=ContentType.objects.get_for_model(order_line),
Expand Down
13 changes: 10 additions & 3 deletions hubspot_xpro/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -378,9 +378,16 @@ def batch_create_hubspot_objects_chunked(
errored_chunks.append(still_failed)
time.sleep(settings.HUBSPOT_TASK_DELAY / 1000)
if errored_chunks:
raise ApiException(
status=last_error_status,
reason=f"Batch hubspot create failed for the following chunks: {errored_chunks}",
if last_error_status == 429: # noqa: PLR2004
raise ApiException(
status=last_error_status,
reason=f"Batch hubspot create failed for the following chunks: {errored_chunks}",
)
log.error(
"Batch hubspot create failed for type %s, chunks: %s (status %s)",
hubspot_type,
errored_chunks,
last_error_status,
)
return created_ids

Expand Down
14 changes: 11 additions & 3 deletions hubspot_xpro/tasks_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ def test_batch_create_hubspot_objects_chunked(mocker, id_count):

@pytest.mark.parametrize(
"status, expected_error", # noqa: PT006
[[429, TooManyRequestsException], [500, ApiException]], # noqa: PT007
[[429, TooManyRequestsException], [500, None]], # noqa: PT007
)
def test_batch_create_hubspot_objects_chunked_error(mocker, status, expected_error):
"""batch_create_hubspot_objects_chunked raise expected exception"""
Expand All @@ -314,12 +314,20 @@ def test_batch_create_hubspot_objects_chunked_error(mocker, status, expected_err
side_effect=(ApiException(status=status)),
)
chunk = sorted([user.id for user in UserFactory.create_batch(3)])
with pytest.raises(expected_error):
tasks.batch_create_hubspot_objects_chunked(
if expected_error:
with pytest.raises(expected_error):
tasks.batch_create_hubspot_objects_chunked(
HubspotObjectType.CONTACTS.value,
"user",
chunk,
)
else:
result = tasks.batch_create_hubspot_objects_chunked(
HubspotObjectType.CONTACTS.value,
"user",
chunk,
)
assert result == []
for item in chunk:
mock_sync_contact.assert_any_call(item)

Expand Down
Loading