fix(so-worker): robust id extraction from webhook fieldvalues & safe api access [31188f42]
This commit is contained in:
@@ -30,18 +30,31 @@ def process_job(job, so_client: SuperOfficeClient):
|
|||||||
contact_id = None
|
contact_id = None
|
||||||
job_title = payload.get("JobTitle")
|
job_title = payload.get("JobTitle")
|
||||||
|
|
||||||
if "PersonId" in payload:
|
# Try getting IDs from FieldValues (more reliable for Webhooks)
|
||||||
person_id = int(payload["PersonId"])
|
field_values = payload.get("FieldValues", {})
|
||||||
elif "PrimaryKey" in payload and "person" in event_low:
|
if "person_id" in field_values:
|
||||||
person_id = int(payload["PrimaryKey"])
|
person_id = int(field_values["person_id"])
|
||||||
|
if "contact_id" in field_values:
|
||||||
|
contact_id = int(field_values["contact_id"])
|
||||||
|
if "title" in field_values and not job_title:
|
||||||
|
job_title = field_values["title"]
|
||||||
|
|
||||||
|
# Fallback to older payload structure if not found
|
||||||
|
if not person_id:
|
||||||
|
if "PersonId" in payload:
|
||||||
|
person_id = int(payload["PersonId"])
|
||||||
|
elif "PrimaryKey" in payload and "person" in event_low:
|
||||||
|
person_id = int(payload["PrimaryKey"])
|
||||||
|
|
||||||
if "ContactId" in payload:
|
if not contact_id:
|
||||||
contact_id = int(payload["ContactId"])
|
if "ContactId" in payload:
|
||||||
elif "PrimaryKey" in payload and "contact" in event_low:
|
contact_id = int(payload["ContactId"])
|
||||||
contact_id = int(payload["PrimaryKey"])
|
elif "PrimaryKey" in payload and "contact" in event_low:
|
||||||
|
contact_id = int(payload["PrimaryKey"])
|
||||||
|
|
||||||
# Fallback/Deep Lookup & Fetch JobTitle if missing
|
# Fallback/Deep Lookup & Fetch JobTitle if missing
|
||||||
if person_id:
|
# Only fetch if we are missing critical info AND have a person_id
|
||||||
|
if person_id and (not job_title or not contact_id):
|
||||||
try:
|
try:
|
||||||
person_details = so_client.get_person(
|
person_details = so_client.get_person(
|
||||||
person_id,
|
person_id,
|
||||||
@@ -50,8 +63,15 @@ def process_job(job, so_client: SuperOfficeClient):
|
|||||||
if person_details:
|
if person_details:
|
||||||
if not job_title:
|
if not job_title:
|
||||||
job_title = person_details.get("JobTitle") or person_details.get("Title")
|
job_title = person_details.get("JobTitle") or person_details.get("Title")
|
||||||
if not contact_id and "Contact" in person_details:
|
|
||||||
contact_id = person_details["Contact"].get("ContactId")
|
# Robust extraction of ContactId
|
||||||
|
if not contact_id:
|
||||||
|
contact_obj = person_details.get("Contact")
|
||||||
|
if contact_obj and isinstance(contact_obj, dict):
|
||||||
|
contact_id = contact_obj.get("ContactId")
|
||||||
|
elif "ContactId" in person_details: # Sometimes flat
|
||||||
|
contact_id = person_details.get("ContactId")
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.warning(f"Failed to fetch person details for {person_id}: {e}")
|
logger.warning(f"Failed to fetch person details for {person_id}: {e}")
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user