{
  "id": "k-assign-01",
  "title": "Inbound Lead routed to correct owner via Country/Region assignment chain (P95<=15s)",
  "kind": "hubspot_sync",
  "caseTimeoutMs": 540000,
  "linkedIssues": [2582],
  "provenance": {
    "businessScenario": "K-ASSIGN-01 (P-ASSIGN, spec 2.8) — an inbound Contact materialized as a Lead is routed to the owner by NexusCRM's native Lead Assignment flow, which is field-triggered (not binding-gated) and overrides any inbound owner by region: Country self-derives Region__c (Country_Dictionary__c -> Countries_Region__c) and OwnerId lands on the region's active Sales owner, not default/system and not the raw HS owner id. Uses Germany->EMEA (no special-country override) with a unique Company (no same-name Account) to force the region default route. PREREQS modeled as probes: assignment flow active, Country/Region dictionary present (dict step onTimeout=blocked -> BLOCKED-NoDict); integration user Profile must not be Sales/SDR else BLOCKED-AssignmentExcluded (not modelable via API, noted here). Latency gate: owner settles P95<=15s, max<=30s (QA harness criterion). Flow facts (verified on staging): Lead_Assignment_Rule_modification_for_NA_EMEA is RecordBeforeSave/CreateAndUpdate, runs when Region__c is set + Company/Email non-blank (or those change) + creator Profile not Sales/SDR, and UNCONDITIONALLY sets OwnerId to a per-region hardcoded User (no check on existing owner). For EMEA via the SDR route that User is 005RC000003s0anYAA (Marek Schmidt), so the inbound HS owner never affects the final OwnerId; assert OwnerId == 005RC000003s0anYAA, != system, then query User by that OwnerId and assert IsActive=true. The shadow (HubSpot_Lead_Owner_Shadow_BeforeSave, order 750) reflects only the ASSIGNED owner's current HubSpotOwnerBinding: if the assigned owner has a bound binding, HubSpot_Owner_Id__c must equal that binding; if no bound binding exists, it must be null. The case intentionally no longer assumes any shared environment lacks that owner binding. Owner.Profile.Name, nested Owner.IsActive, and Owner_IsActive__c are intentionally avoided because this backend does not return nested Owner relationship fields beyond Id/Name and the formula can read null even when the User is active.",
    "reportedBy": "HubSpot sync spec K-ASSIGN-01 (hubspot-nexuscrm-sync-CASE-EXEC-SELFCONTAINED)",
    "capturedAt": "2026-06-18",
    "calibrated": "[calibrated] 2026-06-20 staging: dictionaries are fully seeded (250 Country_Dictionary__c rows; Germany Full_Name__c=Germany -> Name='DE' -> Countries_Region__c(Name='DE').Region_Name__c='EMEA'). The dict/region prechecks used non-existent fields (CountryCode__c, Countries_Region__c.Region__c); corrected to the real schema (Name, Region_Name__c). Lead.Region__c is a real picklist and is unchanged."
  },
  "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": "c",
      "system": "hubspot",
      "method": "POST",
      "path": "/crm/v3/objects/contacts",
      "body": {
        "properties": {
          "email": "assign-{{__runId}}@example.com",
          "firstname": "AssignFN",
          "lastname": "Case{{__runId}}",
          "company": "AssignCo-{{__runId}}",
          "country_picklist__sfdc_": "Germany",
          "hubspot_owner_id": "6036357"
        }
      },
      "expectStatusRange": [200, 299],
      "assertions": [
        { "path": "response.body.id", "exists": true }
      ]
    },
    {
      "id": "s4",
      "system": "nexuscrm",
      "authRole": "admin",
      "method": "GET",
      "path": "/services/data/v63.0/query?q=SELECT Id,Country,Region__c,OwnerId,HubSpot_Owner_Id__c FROM Lead WHERE HubSpot_Id__c='{{steps.c.response.body.id}}'",
      "expectStatus": 200,
      "poll": { "budgetMs": 240000, "intervalMs": 6000 },
      "onTimeout": "blocked",
      "diagnostics": {
        "connectorEvent": {
          "sourceObjectId": "{{steps.c.response.body.id}}",
          "blockOnBacklog": true,
          "blockOnForeignClaimedBy": true,
          "foreignClaimedByPattern": "(MacBook|\\.local)"
        }
      },
      "assertions": [
        { "path": "response.body.records[0].Country", "equals": "Germany" }
      ]
    },
    {
      "id": "owner-binding",
      "system": "nexuscrm",
      "authRole": "admin",
      "method": "GET",
      "path": "/services/data/v63.0/query?q=SELECT HubSpotOwnerId__c FROM HubSpotOwnerBinding__c WHERE CRMPrincipalId__c='{{steps.s4.response.body.records[0].OwnerId}}' AND BindingStatus__c='bound'",
      "expectStatus": 200,
      "assertions": [
        { "path": "response.body.totalSize", "atLeast": 0 }
      ]
    },
    {
      "id": "owner-user",
      "system": "nexuscrm",
      "authRole": "admin",
      "method": "GET",
      "path": "/services/data/v63.0/query?q=SELECT Id,Name,IsActive FROM User WHERE Id='{{steps.s4.response.body.records[0].OwnerId}}' LIMIT 1",
      "expectStatus": 200,
      "assertions": [
        { "path": "response.body.records[0].Id", "equals": "{{steps.s4.response.body.records[0].OwnerId}}" },
        { "path": "response.body.records[0].IsActive", "equals": true }
      ]
    },
    {
      "id": "s5-base",
      "system": "nexuscrm",
      "authRole": "admin",
      "method": "GET",
      "path": "/services/data/v63.0/query?q=SELECT Id,Country,Region__c,OwnerId,HubSpot_Owner_Id__c FROM Lead WHERE HubSpot_Id__c='{{steps.c.response.body.id}}'",
      "expectStatus": 200,
      "poll": { "budgetMs": 60000, "intervalMs": 6000 },
      "onTimeout": "fail",
      "diagnostics": {
        "connectorEvent": {
          "sourceObjectId": "{{steps.c.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].OwnerId", "exists": true },
        { "path": "response.body.records[0].OwnerId", "equals": "{{steps.owner-user.response.body.records[0].Id}}" },
        { "path": "response.body.records[0].OwnerId", "notEquals": "system" }
      ]
    },
    {
      "id": "s5-bound-shadow",
      "system": "nexuscrm",
      "authRole": "admin",
      "method": "GET",
      "whenPathExists": "steps.owner-binding.response.body.records[0].HubSpotOwnerId__c",
      "path": "/services/data/v63.0/query?q=SELECT Id,HubSpot_Owner_Id__c FROM Lead WHERE HubSpot_Id__c='{{steps.c.response.body.id}}'",
      "expectStatus": 200,
      "poll": { "budgetMs": 120000, "intervalMs": 6000 },
      "onTimeout": "fail",
      "diagnostics": {
        "connectorEvent": {
          "sourceObjectId": "{{steps.c.response.body.id}}",
          "blockOnBacklog": true,
          "blockOnForeignClaimedBy": true,
          "foreignClaimedByPattern": "(MacBook|\\.local)"
        }
      },
      "assertions": [
        { "path": "response.body.records[0].HubSpot_Owner_Id__c", "equals": "{{steps.owner-binding.response.body.records[0].HubSpotOwnerId__c}}" }
      ]
    },
    {
      "id": "s5-unbound-shadow",
      "system": "nexuscrm",
      "authRole": "admin",
      "method": "GET",
      "whenPathMissing": "steps.owner-binding.response.body.records[0].HubSpotOwnerId__c",
      "path": "/services/data/v63.0/query?q=SELECT Id,HubSpot_Owner_Id__c FROM Lead WHERE HubSpot_Id__c='{{steps.c.response.body.id}}'",
      "expectStatus": 200,
      "poll": { "budgetMs": 120000, "intervalMs": 6000 },
      "onTimeout": "fail",
      "diagnostics": {
        "connectorEvent": {
          "sourceObjectId": "{{steps.c.response.body.id}}",
          "blockOnBacklog": true,
          "blockOnForeignClaimedBy": true,
          "foreignClaimedByPattern": "(MacBook|\\.local)"
        }
      },
      "assertions": [
        { "path": "response.body.records[0].HubSpot_Owner_Id__c", "exists": false }
      ]
    }
  ]
}
