{
  "id": "fix-4916-new-company-shell-owner-writeback",
  "title": "New HubSpot Contact without Company writes its final Lead owner back without echoing the placeholder shell",
  "kind": "hubspot_sync",
  "executionMode": "serial",
  "executionModeReason": "Creates a unique HubSpot Contact and follows one connector lane through Lead assignment and a provider PATCH; serial execution prevents unrelated owner writes from contaminating latency and readback evidence.",
  "caseTimeoutMs": 600000,
  "linkedIssues": [
    4916
  ],
  "provenance": {
    "businessScenario": "关于 NexusCRM 中的 lead owner 回写到 HubSpot 中的 contact owner，昨晚新版发布后看上去是没有解决。HubSpot 中绝大多数contact 的 owner 目前是 No owner。只有少量是回写成功了（可以忽略 marek 那个 owner，是通过 hubspot中的 flow 设置的）。",
    "reportedBy": "Amber",
    "capturedAt": "2026-08-12",
    "upstreamIssue": "https://github.com/pingcap-inc/nexuscrm-pingcap-pack/issues/4916",
    "calibrated": "Creates a fresh HubSpot Contact with no company and no source HubSpot owner. PASS requires the fresh Lead to retain the inbound Company=HubSpot Lead shell, stamp the final bound HubSpot owner shadow, and PATCH only hubspot_owner_id back to the same fresh Contact. The HubSpot readback also requires Company to remain absent, preventing regression to a broad payload that would echo the synthetic shell. Missing Germany metadata or a bound final owner is BLOCKED, never PASS. [promoted 2026-08-15] Promoted from optional targeted dispatch to a required suite case after a live staging replay of all seven steps passed end to end in ~35s. [recalibrated 2026-09-04] Start with an explicitly empty HubSpot owner and verify the create response is empty/absent. The oracle is the materialized Lead's final OwnerId, independently checked against one active CRM User and one bound User mapping in HubSpotOwnerBinding__c; this case proves final-owner writeback, not which individual the region assignment policy selects. Require the Lead owner to stay stable, its shadow and the same HubSpot Contact to equal that binding, a succeeded Integration command for this exact Lead/action with HTTP 200, no failure code, and exactly one mapped field containing the expected owner, and provider history containing the nonempty target owner. After provider convergence, a final CRM read must still contain the same unique Lead, owner, shadow and shell fields. Provider-side automation alone cannot satisfy the case. Missing or ambiguous user/binding prerequisites remain BLOCKED. No fixed HubSpot or CRM owner id is assumed."
  },
  "steps": [
    {
      "id": "dict",
      "system": "nexuscrm",
      "authRole": "admin",
      "method": "GET",
      "path": "/services/data/v63.0/query?q=SELECT Name FROM Country_Dictionary__c WHERE Full_Name__c='Germany' LIMIT 1",
      "expectStatus": 200,
      "poll": {
        "budgetMs": 30000,
        "intervalMs": 6000
      },
      "onTimeout": "blocked",
      "assertions": [
        {
          "path": "response.body.records[0].Name",
          "exists": true
        }
      ]
    },
    {
      "id": "region",
      "system": "nexuscrm",
      "authRole": "admin",
      "method": "GET",
      "path": "/services/data/v63.0/query?q=SELECT Region_Name__c FROM Countries_Region__c WHERE Name='{{steps.dict.response.body.records[0].Name}}' LIMIT 1",
      "expectStatus": 200,
      "poll": {
        "budgetMs": 30000,
        "intervalMs": 6000
      },
      "onTimeout": "blocked",
      "assertions": [
        {
          "path": "response.body.records[0].Region_Name__c",
          "exists": true
        }
      ]
    },
    {
      "id": "create-contact",
      "system": "hubspot",
      "method": "POST",
      "path": "/crm/v3/objects/contacts",
      "body": {
        "properties": {
          "email": "fix4916-{{__runId}}@example.com",
          "firstname": "Fix4916",
          "lastname": "NewCompanyShell{{__runId}}",
          "country_picklist__sfdc_": "Germany",
          "hubspot_owner_id": ""
        }
      },
      "expectStatusRange": [
        200,
        299
      ],
      "assertions": [
        {
          "path": "response.body.id",
          "exists": true
        },
        {
          "path": "response.body.properties.hubspot_owner_id",
          "exists": false
        }
      ]
    },
    {
      "id": "wait-assigned-lead",
      "system": "nexuscrm",
      "authRole": "admin",
      "method": "GET",
      "path": "/services/data/v63.0/query?q=SELECT Id,Country,Region__c,Company,OwnerId FROM Lead WHERE HubSpot_Id__c='{{steps.create-contact.response.body.id}}'",
      "expectStatus": 200,
      "poll": {
        "budgetMs": 240000,
        "intervalMs": 6000
      },
      "onTimeout": "blocked",
      "diagnostics": {
        "connectorEvent": {
          "sourceObjectId": "{{steps.create-contact.response.body.id}}",
          "blockOnBacklog": true,
          "blockOnForeignClaimedBy": true,
          "foreignClaimedByPattern": "(MacBook|\\.local)"
        }
      },
      "assertions": [
        {
          "path": "response.body.records[0].Country",
          "equals": "Germany"
        },
        {
          "path": "response.body.records[0].Region__c",
          "equals": "{{steps.region.response.body.records[0].Region_Name__c}}"
        },
        {
          "path": "response.body.records[0].Company",
          "equals": "HubSpot Lead"
        },
        {
          "path": "response.body.records[0].OwnerId",
          "exists": true
        },
        {
          "path": "response.body.records[0].OwnerId",
          "notEquals": "system"
        },
        {
          "path": "response.body.records.length",
          "numericEquals": 1
        }
      ]
    },
    {
      "id": "assigned-owner-user",
      "system": "nexuscrm",
      "authRole": "admin",
      "method": "GET",
      "path": "/services/data/v63.0/query?q=SELECT Id,IsActive FROM User WHERE Id='{{steps.wait-assigned-lead.response.body.records[0].OwnerId}}' AND IsActive=true LIMIT 2",
      "expectStatus": 200,
      "onTimeout": "blocked",
      "assertions": [
        {
          "path": "response.body.records.length",
          "numericEquals": 1
        },
        {
          "path": "response.body.records[0].Id",
          "equals": "{{steps.wait-assigned-lead.response.body.records[0].OwnerId}}"
        },
        {
          "path": "response.body.records[0].IsActive",
          "equals": true
        }
      ]
    },
    {
      "id": "final-owner-binding",
      "system": "nexuscrm",
      "authRole": "admin",
      "method": "GET",
      "path": "/services/data/v63.0/query?q=SELECT CRMPrincipalId__c,HubSpotOwnerId__c FROM HubSpotOwnerBinding__c WHERE CRMPrincipalId__c='{{steps.wait-assigned-lead.response.body.records[0].OwnerId}}' AND BindingStatus__c='bound' AND CRMPrincipalType__c='User' LIMIT 2",
      "expectStatus": 200,
      "onTimeout": "blocked",
      "assertions": [
        {
          "path": "response.body.records.length",
          "numericEquals": 1
        },
        {
          "path": "response.body.records[0].CRMPrincipalId__c",
          "equals": "{{steps.assigned-owner-user.response.body.records[0].Id}}"
        },
        {
          "path": "response.body.records[0].HubSpotOwnerId__c",
          "matches": "^[1-9][0-9]*$"
        }
      ]
    },
    {
      "id": "assert-lead-owner-shadow-written",
      "system": "nexuscrm",
      "authRole": "admin",
      "method": "GET",
      "path": "/services/data/v63.0/query?q=SELECT Id,OwnerId,HubSpot_Owner_Id__c FROM Lead WHERE HubSpot_Id__c='{{steps.create-contact.response.body.id}}'",
      "expectStatus": 200,
      "poll": {
        "budgetMs": 120000,
        "intervalMs": 6000
      },
      "onTimeout": "fail",
      "assertions": [
        {
          "path": "response.body.records[0].OwnerId",
          "equals": "{{steps.assigned-owner-user.response.body.records[0].Id}}"
        },
        {
          "path": "response.body.records[0].HubSpot_Owner_Id__c",
          "equals": "{{steps.final-owner-binding.response.body.records[0].HubSpotOwnerId__c}}"
        }
      ]
    },
    {
      "id": "assert-owner-writeback-command-finalized",
      "system": "tidb",
      "method": "GET",
      "sql": "SELECT State, ResponseStatus, RemoteId, JSON_UNQUOTE(JSON_EXTRACT(CONVERT(CanonicalPayload USING utf8mb4), '$.mappedFields.hubspot_owner_id')) AS DesiredHubSpotOwnerId, JSON_LENGTH(JSON_EXTRACT(CONVERT(CanonicalPayload USING utf8mb4), '$.mappedFields')) AS MappedFieldCount, CASE WHEN FailureCode IS NULL OR FailureCode = '' THEN 1 ELSE 0 END AS FailureCodeAbsent FROM `_System_IntegrationOutboundCommand` WHERE SourceObject = 'Lead' AND SourceRecordId = '{{steps.wait-assigned-lead.response.body.records[0].Id}}' AND ConnectorOwnerKey = 'pingcap-hubspot-companion' AND FlowDeveloperName = 'HubSpot_Lead_Contact_Outbound_Update' AND FlowActionName = 'PushLeadToHubSpotContact' ORDER BY CreatedDate DESC LIMIT 1",
      "expectStatus": 200,
      "poll": {
        "budgetMs": 90000,
        "intervalMs": 5000
      },
      "onTimeout": "fail",
      "assertions": [
        {
          "path": "response.body.records[0].State",
          "equals": "succeeded"
        },
        {
          "path": "response.body.records[0].ResponseStatus",
          "numericEquals": 200
        },
        {
          "path": "response.body.records[0].RemoteId",
          "equals": "{{steps.create-contact.response.body.id}}"
        },
        {
          "path": "response.body.records[0].FailureCodeAbsent",
          "numericEquals": 1
        },
        {
          "path": "response.body.records[0].DesiredHubSpotOwnerId",
          "equals": "{{steps.final-owner-binding.response.body.records[0].HubSpotOwnerId__c}}"
        },
        {
          "path": "response.body.records[0].MappedFieldCount",
          "numericEquals": 1
        }
      ]
    },
    {
      "id": "assert-owner-only-hubspot-writeback",
      "system": "hubspot",
      "method": "GET",
      "path": "/crm/v3/objects/contacts/{{steps.create-contact.response.body.id}}?properties=hubspot_owner_id,company&propertiesWithHistory=hubspot_owner_id",
      "expectStatus": 200,
      "poll": {
        "budgetMs": 90000,
        "intervalMs": 5000
      },
      "onTimeout": "fail",
      "assertions": [
        {
          "path": "response.body.properties.hubspot_owner_id",
          "equals": "{{steps.final-owner-binding.response.body.records[0].HubSpotOwnerId__c}}"
        },
        {
          "path": "response.body.properties.company",
          "exists": false
        },
        {
          "path": "response.body.properties.hubspot_owner_id",
          "notEquals": ""
        },
        {
          "path": "response.body.propertiesWithHistory.hubspot_owner_id",
          "arrayContainsObject": {
            "value": "{{steps.final-owner-binding.response.body.records[0].HubSpotOwnerId__c}}"
          }
        }
      ]
    },
    {
      "id": "assert-final-owner-source-stable",
      "system": "nexuscrm",
      "authRole": "admin",
      "method": "GET",
      "path": "/services/data/v63.0/query?q=SELECT Id,OwnerId,HubSpot_Owner_Id__c,Country,Region__c,Company,LastName FROM Lead WHERE HubSpot_Id__c='{{steps.create-contact.response.body.id}}'",
      "expectStatus": 200,
      "onTimeout": "fail",
      "assertions": [
        {
          "path": "response.body.records.length",
          "numericEquals": 1
        },
        {
          "path": "response.body.records[0].Id",
          "equals": "{{steps.wait-assigned-lead.response.body.records[0].Id}}"
        },
        {
          "path": "response.body.records[0].OwnerId",
          "equals": "{{steps.assigned-owner-user.response.body.records[0].Id}}"
        },
        {
          "path": "response.body.records[0].HubSpot_Owner_Id__c",
          "equals": "{{steps.final-owner-binding.response.body.records[0].HubSpotOwnerId__c}}"
        },
        {
          "path": "response.body.records[0].Country",
          "equals": "Germany"
        },
        {
          "path": "response.body.records[0].Region__c",
          "equals": "{{steps.region.response.body.records[0].Region_Name__c}}"
        },
        {
          "path": "response.body.records[0].Company",
          "equals": "HubSpot Lead"
        },
        {
          "path": "response.body.records[0].LastName",
          "equals": "{{steps.create-contact.response.body.properties.lastname}}"
        }
      ]
    }
  ]
}
