{
  "apiVersion": 63,
  "status": "Active",
  "label": "HubSpot Deal \u2192 Opportunity Upsert",
  "description": "Creates or updates a NexusCRM Opportunity from a fetched HubSpot deal record.",
  "runAtomically": true,
  "start": {
    "locationX": 40,
    "locationY": 0,
    "connector": {
      "targetReference": "FindOpportunityByHubSpotId"
    }
  },
  "variables": [
    {
      "name": "payload",
      "dataType": "Any",
      "isInput": true
    },
    {
      "name": "record",
      "dataType": "Any",
      "isInput": true
    },
    {
      "name": "opportunityId",
      "dataType": "String",
      "isOutput": true
    },
    {
      "name": "sourceID",
      "dataType": "String",
      "isInput": true
    }
  ],
  "recordLookups": [
    {
      "name": "FindOpportunityByHubSpotId",
      "label": "Find Opportunity by HubSpot ID",
      "locationX": 40,
      "locationY": 120,
      "object": "Opportunity",
      "filterLogic": "and",
      "filters": [
        {
          "field": "HubSpot_Id__c",
          "operator": "EqualTo",
          "value": {
            "elementReference": "sourceID"
          }
        }
      ],
      "outputReference": "$opportunity",
      "getFirstRecordOnly": true,
      "connector": {
        "targetReference": "ChooseCreateOrUpdate"
      }
    },
    {
      "name": "FindOpportunityOwnerBinding",
      "label": "Find Opportunity owner binding",
      "locationX": 220,
      "locationY": 720,
      "object": "HubSpotOwnerBinding__c",
      "filterLogic": "and",
      "filters": [
        {
          "field": "HubSpotOwnerId__c",
          "operator": "EqualTo",
          "value": {
            "elementReference": "record.hubspot_owner_id"
          }
        },
        {
          "field": "BindingStatus__c",
          "operator": "EqualTo",
          "value": {
            "stringValue": "bound"
          }
        },
        {
          "field": "CRMPrincipalType__c",
          "operator": "EqualTo",
          "value": {
            "stringValue": "User"
          }
        }
      ],
      "outputReference": "$opportunityOwnerBinding",
      "getFirstRecordOnly": true,
      "connector": {
        "targetReference": "CheckOpportunityOwnerBinding"
      }
    },
    {
      "name": "FindOpportunityOwnerBindingDup",
      "label": "Find duplicate Opportunity owner binding",
      "locationX": 400,
      "locationY": 840,
      "object": "HubSpotOwnerBinding__c",
      "filterLogic": "and",
      "filters": [
        {
          "field": "HubSpotOwnerId__c",
          "operator": "EqualTo",
          "value": {
            "elementReference": "record.hubspot_owner_id"
          }
        },
        {
          "field": "BindingStatus__c",
          "operator": "EqualTo",
          "value": {
            "stringValue": "bound"
          }
        },
        {
          "field": "CRMPrincipalType__c",
          "operator": "EqualTo",
          "value": {
            "stringValue": "User"
          }
        },
        {
          "field": "Id",
          "operator": "NotEqualTo",
          "value": {
            "elementReference": "$opportunityOwnerBinding.Id"
          }
        }
      ],
      "outputReference": "$opportunityOwnerBindingDup",
      "getFirstRecordOnly": true,
      "connector": {
        "targetReference": "CheckOpportunityOwnerAmbiguous"
      }
    },
    {
      "name": "FindDefaultOwnerBinding",
      "label": "Find default owner User (hardcoded fallback owner)",
      "locationX": 240, "locationY": 300,
      "object": "User",
      "filterLogic": "and",
      "filters": [
        { "field": "Email", "operator": "EqualTo", "value": { "stringValue": "admin@example.invalid" } },
        { "field": "IsActive", "operator": "EqualTo", "value": true }
      ],
      "outputReference": "$defaultOwnerUser",
      "getFirstRecordOnly": true,
      "connector": { "targetReference": "CheckDefaultOwnerForShell" }
    }
  ],
  "decisions": [
    {
      "name": "ChooseCreateOrUpdate",
      "label": "Create when no matching Opportunity exists",
      "locationX": 220,
      "locationY": 240,
      "rules": [
        {
          "name": "OpportunityMissing",
          "conditionLogic": "and",
          "conditions": [
            {
              "leftValueReference": "$opportunity.Id",
              "operator": "IsBlank"
            }
          ],
          "connector": {
            "targetReference": "FindDefaultOwnerBinding"
          }
        }
      ],
      "defaultConnector": {
        "targetReference": "ApplyMappingsToExistingOpportunity"
      }
    },
    {
      "name": "CheckOpportunityOwnerBinding",
      "label": "Apply bound Opportunity owner?",
      "locationX": 220,
      "locationY": 840,
      "rules": [
        {
          "name": "BoundOpportunityOwnerFound",
          "conditionLogic": "and",
          "conditions": [
            {
              "leftValueReference": "$opportunityOwnerBinding.Id",
              "operator": "IsNotBlank"
            }
          ],
          "connector": {
            "targetReference": "FindOpportunityOwnerBindingDup"
          }
        }
      ]
    },
    {
      "name": "CheckOpportunityOwnerAmbiguous",
      "label": "Duplicate Opportunity owner binding found?",
      "locationX": 400,
      "locationY": 960,
      "rules": [
        {
          "name": "OpportunityOwnerDupFound",
          "conditionLogic": "and",
          "conditions": [
            {
              "leftValueReference": "$opportunityOwnerBindingDup.Id",
              "operator": "IsNotBlank"
            }
          ],
          "connector": {
            "targetReference": "FailAmbiguousOpportunityOwner"
          }
        }
      ],
      "defaultConnector": {
        "targetReference": "ApplyBoundOwnerToOpportunity"
      }
    },
    {
      "name": "CheckDefaultOwnerForShell",
      "label": "Own the new Opportunity shell with the Integration User when a default owner binding exists?",
      "locationX": 240, "locationY": 330,
      "rules": [
        { "name": "DefaultOwnerConfigured", "conditionLogic": "and",
          "conditions": [ { "leftValueReference": "$defaultOwnerUser.Id", "operator": "IsNotBlank" } ],
          "connector": { "targetReference": "CreateOpportunityShellWithIntegrationOwner" } }
      ],
      "defaultConnector": { "targetReference": "CreateOpportunityShell" }
    }
  ],
  "recordCreates": [
    {
      "name": "CreateOpportunityShell",
      "label": "Create Opportunity shell",
      "locationX": 40,
      "locationY": 360,
      "object": "Opportunity",
      "inputAssignments": [
        {
          "field": "Name",
          "value": {
            "elementReference": "record.dealname"
          }
        },
        {
          "field": "StageName",
          "value": {
            "stringValue": "Prospecting"
          }
        },
        {
          "field": "CloseDate",
          "value": {
            "dateValue": "2099-12-31"
          }
        },
        {
          "field": "HubSpot_Id__c",
          "value": {
            "elementReference": "sourceID"
          }
        },
        {
          "field": "HubSpot_Sync_Enabled__c",
          "value": true
        }
      ],
      "assignRecordIdToReference": "opportunityId",
      "connector": {
        "targetReference": "ApplyMappingsToCreatedOpportunity"
      }
    },
    {
      "name": "CreateOpportunityShellWithIntegrationOwner",
      "label": "Create Opportunity shell owned by the Integration User",
      "locationX": 240,
      "locationY": 360,
      "object": "Opportunity",
      "inputAssignments": [
        {
          "field": "Name",
          "value": {
            "elementReference": "record.dealname"
          }
        },
        {
          "field": "StageName",
          "value": {
            "stringValue": "Prospecting"
          }
        },
        {
          "field": "CloseDate",
          "value": {
            "dateValue": "2099-12-31"
          }
        },
        {
          "field": "HubSpot_Id__c",
          "value": {
            "elementReference": "sourceID"
          }
        },
        {
          "field": "HubSpot_Sync_Enabled__c",
          "value": true
        },
        {
          "field": "OwnerId",
          "value": {
            "elementReference": "$defaultOwnerUser.Id"
          }
        }
      ],
      "assignRecordIdToReference": "opportunityId",
      "connector": {
        "targetReference": "ApplyMappingsToCreatedOpportunity"
      }
    }
  ],
  "recordUpdates": [
    {
      "name": "StampRemoteIdOnExistingOpportunity",
      "label": "Stamp HubSpot remote id on existing Opportunity",
      "locationX": 400,
      "locationY": 480,
      "object": "Opportunity",
      "filters": [
        {
          "field": "Id",
          "operator": "EqualTo",
          "value": {
            "elementReference": "$opportunity.Id"
          }
        }
      ],
      "inputAssignments": [
        {
          "field": "HubSpot_Id__c",
          "value": {
            "elementReference": "sourceID"
          }
        }
      ],
      "connector": {
        "targetReference": "ExposeMatchedOpportunityId"
      }
    },
    {
      "name": "ApplyBoundOwnerToOpportunity",
      "label": "Apply bound owner to Opportunity",
      "locationX": 220,
      "locationY": 960,
      "object": "Opportunity",
      "filters": [
        {
          "field": "Id",
          "operator": "EqualTo",
          "value": {
            "elementReference": "opportunityId"
          }
        }
      ],
      "inputAssignments": [
        {
          "field": "OwnerId",
          "value": {
            "elementReference": "$opportunityOwnerBinding.CRMPrincipalId__c"
          }
        }
      ]
    }
  ],
  "actionCalls": [
    {
      "name": "ApplyMappingsToExistingOpportunity",
      "label": "Apply HubSpot mappings to existing Opportunity",
      "locationX": 400,
      "locationY": 360,
      "actionName": "applyConnectorFieldMappings",
      "actionType": "applyConnectorFieldMappings",
      "inputParameters": [
        {
          "name": "source",
          "value": {
            "stringValue": "pingcap-hubspot-companion"
          }
        },
        {
          "name": "sourceObject",
          "value": {
            "stringValue": "deals"
          }
        },
        {
          "name": "targetObject",
          "value": {
            "stringValue": "Opportunity"
          }
        },
        {
          "name": "actionType",
          "value": {
            "stringValue": "hubspotDealOpportunityInbound"
          }
        },
        {
          "name": "direction",
          "value": {
            "stringValue": "inbound"
          }
        },
        {
          "name": "targetId",
          "value": {
            "elementReference": "$opportunity.Id"
          }
        },
        {
          "name": "sourceRecord",
          "value": {
            "elementReference": "record"
          }
        }
      ],
      "connector": {
        "targetReference": "StampRemoteIdOnExistingOpportunity"
      }
    },
    {
      "name": "ApplyMappingsToCreatedOpportunity",
      "label": "Apply HubSpot mappings to created Opportunity",
      "locationX": 40,
      "locationY": 480,
      "actionName": "applyConnectorFieldMappings",
      "actionType": "applyConnectorFieldMappings",
      "inputParameters": [
        {
          "name": "source",
          "value": {
            "stringValue": "pingcap-hubspot-companion"
          }
        },
        {
          "name": "sourceObject",
          "value": {
            "stringValue": "deals"
          }
        },
        {
          "name": "targetObject",
          "value": {
            "stringValue": "Opportunity"
          }
        },
        {
          "name": "actionType",
          "value": {
            "stringValue": "hubspotDealOpportunityInbound"
          }
        },
        {
          "name": "direction",
          "value": {
            "stringValue": "inbound"
          }
        },
        {
          "name": "targetId",
          "value": {
            "elementReference": "opportunityId"
          }
        },
        {
          "name": "sourceRecord",
          "value": {
            "elementReference": "record"
          }
        }
      ],
      "connector": {
        "targetReference": "RegisterCreatedOpportunityAuthorship"
      }
    },
    {
      "name": "RegisterCreatedOpportunityAuthorship",
      "label": "Finalize inbound remote identity (created)",
      "locationX": 40,
      "locationY": 600,
      "actionName": "finalizeConnectorInboundRemoteIdentity",
      "actionType": "finalizeConnectorInboundRemoteIdentity",
      "inputParameters": [
        {
          "name": "targetFlowDeveloperName",
          "value": {
            "stringValue": "HubSpot_Opportunity_Deal_Outbound_Update"
          }
        },
        {
          "name": "targetFlowActionName",
          "value": {
            "stringValue": "PushOpportunityToHubSpotDeal"
          }
        }
      ],
      "connector": {
        "targetReference": "FindOpportunityOwnerBinding"
      }
    },
    {
      "name": "RegisterExistingOpportunityAuthorship",
      "label": "Finalize inbound remote identity (existing)",
      "locationX": 400,
      "locationY": 600,
      "actionName": "finalizeConnectorInboundRemoteIdentity",
      "actionType": "finalizeConnectorInboundRemoteIdentity",
      "inputParameters": [
        {
          "name": "targetFlowDeveloperName",
          "value": {
            "stringValue": "HubSpot_Opportunity_Deal_Outbound_Update"
          }
        },
        {
          "name": "targetFlowActionName",
          "value": {
            "stringValue": "PushOpportunityToHubSpotDeal"
          }
        }
      ],
      "connector": {
        "targetReference": "FindOpportunityOwnerBinding"
      }
    }
  ],
  "assignments": [
    {
      "name": "ExposeMatchedOpportunityId",
      "label": "Expose matched Opportunity Id",
      "locationX": 400,
      "locationY": 600,
      "assignmentItems": [
        {
          "assignToReference": "opportunityId",
          "operator": "Assign",
          "value": {
            "elementReference": "$opportunity.Id"
          }
        }
      ],
      "connector": {
        "targetReference": "RegisterExistingOpportunityAuthorship"
      }
    }
  ],
  "customErrors": [
    {
      "name": "FailAmbiguousOpportunityOwner",
      "label": "Ambiguous Opportunity owner binding",
      "locationX": 580,
      "locationY": 960,
      "customErrorMessages": [
        {
          "errorMessage": "HubSpot owner id resolves to multiple active Opportunity owner bindings; refusing to assign an arbitrary owner",
          "statusCode": "HUBSPOT_OWNER_BINDING_AMBIGUOUS_OPPORTUNITY"
        }
      ]
    }
  ]
}
