diff --git a/connector-superoffice/worker.py b/connector-superoffice/worker.py index bd1b21c1..eaf9af9b 100644 --- a/connector-superoffice/worker.py +++ b/connector-superoffice/worker.py @@ -30,18 +30,31 @@ def process_job(job, so_client: SuperOfficeClient): contact_id = None job_title = payload.get("JobTitle") - if "PersonId" in payload: - person_id = int(payload["PersonId"]) - elif "PrimaryKey" in payload and "person" in event_low: - person_id = int(payload["PrimaryKey"]) + # Try getting IDs from FieldValues (more reliable for Webhooks) + field_values = payload.get("FieldValues", {}) + if "person_id" in field_values: + 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: - contact_id = int(payload["ContactId"]) - elif "PrimaryKey" in payload and "contact" in event_low: - contact_id = int(payload["PrimaryKey"]) + if not contact_id: + if "ContactId" in payload: + contact_id = int(payload["ContactId"]) + elif "PrimaryKey" in payload and "contact" in event_low: + contact_id = int(payload["PrimaryKey"]) # 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: person_details = so_client.get_person( person_id, @@ -50,8 +63,15 @@ def process_job(job, so_client: SuperOfficeClient): if person_details: if not job_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: logger.warning(f"Failed to fetch person details for {person_id}: {e}")