{
  "id": "workflow-action-repeat-identity-idempotency",
  "title": "Distinct Set Campaign executions for one HubSpot contact converge to one Lead and member",
  "kind": "hubspot_sync",
  "optional": true,
  "targetedAcceptance": {
    "schemaVersion": "hubspot-targeted-acceptance/v1",
    "reason": "Set Campaign idempotency under repeated distinct callback ids."
  },
  "executionMode": "serial",
  "executionModeReason": "The case intentionally queues two workflow actions for one source and measures their shared identity result without unrelated queue pressure.",
  "caseTimeoutMs": 420000,
  "linkedIssues": [4041, 4166],
  "provenance": {
    "businessScenario": "在hubspot integration test新增几个case，防止回归，inbound 和set campaign中重复创建lead的问题",
    "reportedBy": "Amber",
    "capturedAt": "2026-07-17",
    "calibrated": "Two distinct Set Campaign callback ids for the same new HubSpot contact and Campaign must both terminalize without creating more than one Lead or more than one CampaignMember."
  },
  "steps": [
    {
      "id": "create-campaign",
      "system": "nexuscrm",
      "authRole": "admin",
      "method": "POST",
      "path": "/services/data/v63.0/sobjects/Campaign",
      "body": {
        "Name": "IT Workflow Repeat Identity {{__runId}}",
        "IsActive": true
      },
      "expectStatusRange": [200, 299],
      "assertions": [
        { "path": "response.body.id", "exists": true }
      ]
    },
    {
      "id": "create-hubspot-contact",
      "system": "hubspot",
      "method": "POST",
      "path": "/crm/v3/objects/contacts/batch/create",
      "generate": {
        "count": 1,
        "into": "inputs",
        "itemTemplate": {
          "properties": {
            "email": "workflow-repeat-{{__runId}}@example.com",
            "firstname": "Workflow",
            "lastname": "Repeat{{__runId}}",
            "company": "Workflow Repeat Identity",
            "lifecyclestage": "lead"
          }
        }
      },
      "expectStatusRange": [200, 299],
      "assertions": [
        { "path": "response.body.results", "lengthAtLeast": 1 }
      ]
    },
    {
      "id": "execute-set-campaign-first",
      "system": "connector_action",
      "path": "/webhooks/hubspot-crm-connector/workflow-action",
      "forEach": "steps.create-hubspot-contact.response.body.results",
      "contactIdField": "id",
      "emailField": "properties.email",
      "campaignId": "{{steps.create-campaign.response.body.id}}",
      "status": "Sent",
      "callbackIdPrefix": "identity-repeat-a",
      "concurrency": 1,
      "assertions": [
        { "path": "response.body.failedCount", "numericEquals": 0 },
        { "path": "response.body.acceptedCount", "numericEquals": 1 }
      ]
    },
    {
      "id": "execute-set-campaign-second",
      "system": "connector_action",
      "path": "/webhooks/hubspot-crm-connector/workflow-action",
      "forEach": "steps.create-hubspot-contact.response.body.results",
      "contactIdField": "id",
      "emailField": "properties.email",
      "campaignId": "{{steps.create-campaign.response.body.id}}",
      "status": "Sent",
      "callbackIdPrefix": "identity-repeat-b",
      "concurrency": 1,
      "assertions": [
        { "path": "response.body.failedCount", "numericEquals": 0 },
        { "path": "response.body.acceptedCount", "numericEquals": 1 }
      ]
    },
    {
      "id": "assert-both-workflow-actions-terminal",
      "system": "tidb",
      "method": "GET",
      "sql": "SELECT COUNT(*) AS EventCount, SUM(CASE WHEN Status IN ('processed', 'skipped') THEN 1 ELSE 0 END) AS TerminalCount, SUM(CASE WHEN Status IN ('failed', 'dead') THEN 1 ELSE 0 END) AS FailedCount FROM `_System_ConnectorEvent` WHERE Namespace = 'hubspot-crm-connector' AND EventKind = 'workflow.action.execute' AND SourceObjectID = '{{steps.create-hubspot-contact.response.body.results[0].id}}'",
      "expectStatus": 200,
      "poll": { "budgetMs": 300000, "intervalMs": 6000 },
      "onTimeout": "fail",
      "assertions": [
        { "path": "response.body.records[0].EventCount", "atLeast": 2 },
        { "path": "response.body.records[0].TerminalCount", "atLeast": 2 },
        { "path": "response.body.records[0].FailedCount", "numericEquals": 0 }
      ]
    },
    {
      "id": "assert-one-remote-id-lead",
      "system": "tidb",
      "method": "GET",
      "sql": "SELECT COUNT(*) AS RemoteIdLeadCount FROM Lead WHERE HubSpot_Id__c = '{{steps.create-hubspot-contact.response.body.results[0].id}}' AND IsConverted = 0",
      "expectStatus": 200,
      "poll": { "budgetMs": 300000, "intervalMs": 6000 },
      "onTimeout": "fail",
      "assertions": [
        { "path": "response.body.records[0].RemoteIdLeadCount", "equals": 1 }
      ]
    },
    {
      "id": "assert-one-same-email-lead",
      "system": "tidb",
      "method": "GET",
      "sql": "SELECT COUNT(*) AS SameEmailLeadCount FROM Lead WHERE Email = 'workflow-repeat-{{__runId}}@example.com' AND IsConverted = 0",
      "expectStatus": 200,
      "assertions": [
        { "path": "response.body.records[0].SameEmailLeadCount", "equals": 1 }
      ]
    },
    {
      "id": "assert-workflow-action-finalized-authorship",
      "system": "tidb",
      "method": "GET",
      "sql": "SELECT (SELECT COUNT(*) FROM `_System_ConnectorRemoteLink` L JOIN Lead P ON P.Id = L.SourceRecordId AND P.IsConverted = 0 AND P.IsDeleted = 0 WHERE L.ConnectorOwnerKey = 'pingcap-hubspot-companion' AND L.RemoteId = '{{steps.create-hubspot-contact.response.body.results[0].id}}' AND L.SourceObject = 'Lead' AND L.DestinationSlotId = 'hubspot-contact' AND L.Status = 'finalized') AS FinalizedLinkCount, (SELECT COUNT(*) FROM `_System_ConnectorInboundRemoteIDEvidence` E JOIN `_System_ConnectorRemoteLink` L ON L.LinkKey = E.ConsumedByLinkKey WHERE L.ConnectorOwnerKey = 'pingcap-hubspot-companion' AND L.RemoteId = '{{steps.create-hubspot-contact.response.body.results[0].id}}' AND L.SourceObject = 'Lead' AND L.Status = 'finalized' AND E.EvidenceStatus = 'consumed' AND E.ActiveKey IS NULL) AS ConsumedEvidenceCount",
      "expectStatus": 200,
      "poll": { "budgetMs": 300000, "intervalMs": 6000 },
      "onTimeout": "fail",
      "assertions": [
        { "path": "response.body.records[0].FinalizedLinkCount", "numericEquals": 1 },
        { "path": "response.body.records[0].ConsumedEvidenceCount", "numericEquals": 1 }
      ]
    },
    {
      "id": "assert-one-campaign-member",
      "system": "tidb",
      "method": "GET",
      "sql": "SELECT COUNT(*) AS CampaignMemberCount FROM CampaignMember WHERE CampaignId = '{{steps.create-campaign.response.body.id}}' AND LeadId IN (SELECT Id FROM Lead WHERE HubSpot_Id__c = '{{steps.create-hubspot-contact.response.body.results[0].id}}' AND IsConverted = 0)",
      "expectStatus": 200,
      "assertions": [
        { "path": "response.body.records[0].CampaignMemberCount", "equals": 1 }
      ]
    }
  ]
}
