{
  "id": "fix-3109-inbound-owner-writeback",
  "title": "Inbound owner assignment serializes the final bound owner for complete and placeholder-shell HubSpot contacts",
  "kind": "hubspot_contract",
  "linkedIssues": [3109, 4236, 4916],
  "externalEffects": "none",
  "provenance": {
    "businessScenario": "After inbound routing assigns a CRM owner, the Lead owner-shadow flow must stamp the bound HubSpot owner id and the existing-contact PATCH contract must include it as hubspot_owner_id. A nameless inbound contact is represented temporarily by LastName=HubSpot Lead and Company=HubSpot Lead; that placeholder shell must allow an owner-only PATCH without echoing synthetic fields. This regression is provider-free because Xiaopeng currently retains a database credential that targets production HubSpot.",
    "acceptanceBoundary": "PASS proves the local flow and outbound payload contract only. The placeholder-shell owner exception must precede the generic required-field shell blocker and select only HubSpot_Owner_Id__c. PASS does not claim a HubSpot API write or provider-side owner readback."
  },
  "contractChecks": [
    {
      "id": "owner-shadow-runs-on-inbound-create-and-update",
      "type": "flow_start",
      "flow": "HubSpot_Lead_Owner_Shadow_BeforeSave.flow.json",
      "expect": {
        "triggerType": "RecordBeforeSave",
        "recordTriggerType": "CreateAndUpdate"
      }
    },
    {
      "id": "bound-owner-stamps-lead-shadow",
      "type": "record_update",
      "flow": "HubSpot_Lead_Owner_Shadow_BeforeSave.flow.json",
      "update": "StampLeadOwnerShadowOutbound",
      "expect": {
        "inputReference": "$Record",
        "inputAssignments": [
          { "field": "HubSpot_Owner_Id__c", "value": { "elementReference": "$leadOwnerBinding.HubSpotOwnerId__c" } }
        ]
      }
    },
    {
      "id": "owner-shadow-change-routes-to-existing-contact-patch",
      "type": "flow_rule",
      "flow": "HubSpot_Lead_Contact_Outbound_Update.flow.json",
      "decision": "SkipConnectorInboundEcho",
      "rule": "OwnerShadowChangedDuringInbound",
      "expect": {
        "connector": { "targetReference": "PushLeadToHubSpotContact" }
      }
    },
    {
      "id": "new-company-shell-owner-shadow-routes-to-owner-only-patch",
      "type": "flow_rule",
      "flow": "HubSpot_Lead_Contact_Outbound_Update.flow.json",
      "decision": "SkipConnectorInboundEcho",
      "rule": "OwnerShadowChangedDuringInboundWithNewCompanyShell",
      "expect": {
        "connector": { "targetReference": "SelectOwnerOnlyHubSpotPayload" }
      }
    },
    {
      "id": "required-field-shell-owner-shadow-routes-to-owner-only-patch",
      "type": "flow_rule",
      "flow": "HubSpot_Lead_Contact_Outbound_Update.flow.json",
      "decision": "SkipConnectorInboundEcho",
      "rule": "OwnerShadowChangedDuringInboundWithRequiredFieldShell",
      "expect": {
        "conditionLogic": "and",
        "conditions": [
          {
            "leftValueReference": "$SystemContext.ConnectorSyncOrigin",
            "operator": "EqualTo",
            "rightValue": { "stringValue": "hubspot-crm-connector" }
          },
          {
            "leftValueReference": "$Record.HubSpot_Id__c",
            "operator": "IsNotBlank"
          },
          {
            "leftValueReference": "$Record.HubSpot_Owner_Id__c",
            "operator": "IsNotBlank"
          },
          {
            "leftValueReference": "$Record.HubSpot_Owner_Id__c",
            "operator": "NotEqualTo",
            "rightValue": { "elementReference": "$Record__Old.HubSpot_Owner_Id__c" }
          },
          {
            "leftValueReference": "$Record.LastName",
            "operator": "EqualTo",
            "rightValue": { "stringValue": "HubSpot Lead" }
          },
          {
            "leftValueReference": "$Record.Company",
            "operator": "EqualTo",
            "rightValue": { "stringValue": "HubSpot Lead" }
          }
        ],
        "connector": { "targetReference": "SelectOwnerOnlyHubSpotPayload" }
      }
    },
    {
      "id": "required-field-shell-owner-exception-precedes-placeholder-blocker",
      "type": "flow_rule_order",
      "flow": "HubSpot_Lead_Contact_Outbound_Update.flow.json",
      "decision": "SkipConnectorInboundEcho",
      "beforeRule": "OwnerShadowChangedDuringInboundWithRequiredFieldShell",
      "afterRule": "RequiredFieldShell",
      "expect": { "ordered": true }
    },
    {
      "id": "new-company-shell-patch-selects-only-owner-shadow",
      "type": "flow_assignment",
      "flow": "HubSpot_Lead_Contact_Outbound_Update.flow.json",
      "assignment": "SelectOwnerOnlyHubSpotPayload",
      "expect": {
        "connector": { "targetReference": "PushLeadToHubSpotContact" },
        "assignmentItems": [
          {
            "assignToReference": "outboundPayloadSourceFields",
            "operator": "Assign",
            "value": { "stringValue": "HubSpot_Owner_Id__c" }
          }
        ]
      }
    },
    {
      "id": "lead-owner-writeback-uses-contact-patch-endpoint",
      "type": "flow_action",
      "flow": "HubSpot_Lead_Contact_Outbound_Update.flow.json",
      "action": "PushLeadToHubSpotContact",
      "expect": {
        "actionName": "PushToConnector",
        "actionType": "PushToConnector",
        "inputParameters": {
          "profileOwnerPack": { "stringValue": "hubspot-crm-connector" },
          "profileName": { "stringValue": "hubspot-crm-contact-write" },
          "sourceObject": { "stringValue": "Lead" },
          "targetObject": { "stringValue": "contacts" },
          "remoteId": { "elementReference": "$Record.HubSpot_Id__c" },
          "updatePathTemplate": { "stringValue": "/crm/v3/objects/contacts/{remoteId}" },
          "payloadSourceFields": { "elementReference": "outboundPayloadSourceFields" }
        }
      }
    },
    {
      "id": "lead-owner-shadow-serializes-as-hubspot-owner-id",
      "type": "mapping_row",
      "mapping": "contact_lead_outbound.json",
      "rule": { "sourceObject": "Lead", "targetObject": "contacts", "actionType": "PushToConnector", "direction": "outbound" },
      "row": { "sourceField": "HubSpot_Owner_Id__c", "targetField": "hubspot_owner_id" },
      "expect": { "sourceField": "HubSpot_Owner_Id__c", "targetField": "hubspot_owner_id" }
    }
  ]
}
