fix: handle hubspot sync edge cases (409 conflicts, empty line items, duplicate hubspot_id mappings)#3927
Open
Anas12091101 wants to merge 2 commits into
Open
fix: handle hubspot sync edge cases (409 conflicts, empty line items, duplicate hubspot_id mappings)#3927Anas12091101 wants to merge 2 commits into
Anas12091101 wants to merge 2 commits into
Conversation
… duplicate hubspot_id mappings)
for more information, see https://pre-commit.ci
Contributor
|
@Anas12091101 is this ready for review? |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What are the relevant tickets?
https://github.com/mitodl/hq/issues/11149
Description
While running the
sync_db_to_hubspotmanagement command, three edge cases were causing crashes that prevented the full sync from completing.1.
batch_create_hubspot_objects_chunked— ApiException on 400/409 conflictsWhen objects (products/contacts) already existed in HubSpot but the local
HubspotObjectmapping was missing (e.g. after a DB restore), the batch create API returned 400/409. The task re-raised these asApiException, crashing the entire Celery chord. Non-retryable errors (400, 409, 500) are now logged and skipped. 429 (rate limit) errors still raise so Celery autoretry continues to work.2.
sync_contact_hubspot_ids_to_db— IntegrityError on duplicate hubspot_idWhen HubSpot returned a contact whose
hubspot_idwas already mapped to a different local user (e.g. stale mapping from a user email change),update_or_createviolated the unique constraint(hubspot_id, content_type_id). The function now checks for existing conflicting mappings and skips them, leaving existing data unchanged.3.
sync_deal_line_hubspot_ids_to_db— IndexError on empty line itemsWhen a deal in HubSpot had no associated line items (B2B orders enter the
is_b2bbranch unconditionally), accessingline_items[0]caused anIndexError. The function now returnsFalseearly whenline_itemsis empty.How can this change be tested?
Each scenario can be verified end-to-end against a real HubSpot account. Ensure
MITOL_HUBSPOT_API_PRIVATE_TOKENis set in your .env. Run each script via:docker-compose run --rm web python manage.py shell < script_name.pyOn
masterall 3 scripts CRASH. On this branch all 3 PASS.Scenario 1: Product batch create with 400/409 conflict
Save as test_scenario1.py:
Scenario 2: Contact sync with duplicate hubspot_id
Save as test_scenario2.py (note: runs full contact sync, may take a minute):
Scenario 3: Deal with no line items
Save as test_scenario3.py:
Verified results
masterApiException(status=400)[]IntegrityError: duplicate keyIndexError: list index out of rangeFalseAdditional Context