{
  "nodes": [
    {
      "parameters": {
        "method": "POST",
        "url": "=https://services.leadconnectorhq.com/contacts/",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "Authorization",
              "value": "Bearer HIDDEN_API_KEY"
            },
            {
              "name": "Version",
              "value": "2021-07-28"
            },
            {
              "name": "Content-Type",
              "value": "application/json"
            }
          ]
        },
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={\n  \"firstName\": \"{{ $('Fuzzy Match Opportunity').first().json.projectName }}\",\n  \"locationId\": \"{{ $('Fuzzy Match Opportunity').first().json.locationId }}\",\n  \"email\": \"{{ $('Fuzzy Match Opportunity').first().json.projectName.toLowerCase().replace(/[^a-z0-9]/g, '.') }}@projekt.intern\"\n}",
        "options": {}
      },
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        5056,
        1504
      ],
      "id": "HIDDEN_UUID",
      "name": "Create Contact"
    },
    {
      "parameters": {
        "method": "POST",
        "url": "=https://services.leadconnectorhq.com/opportunities/",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "Authorization",
              "value": "Bearer HIDDEN_API_KEY"
            },
            {
              "name": "Version",
              "value": "2021-07-28"
            },
            {
              "name": "Content-Type",
              "value": "application/json"
            }
          ]
        },
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={\n  \"pipelineId\": \"HIDDEN_ID\",\n  \"locationId\": \"{{ $('Fuzzy Match Opportunity').first().json.locationId }}\",\n  \"name\": \"{{ $('Fuzzy Match Opportunity').first().json.projectName }}\",\n  \"pipelineStageId\": \"HIDDEN_UUID\",\n  \"status\": \"open\",\n  \"contactId\": \"{{ $json.contact.id }}\"\n}",
        "options": {}
      },
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        5280,
        1504
      ],
      "id": "HIDDEN_UUID",
      "name": "Create Opportunity"
    },
    {
      "parameters": {
        "jsCode": "const data = $input.first().json;\n// From YES branch: data.opportunityId already set\n// From NO branch (Create Opportunity response): data.opportunity.id or data.id\nconst oppId = data.opportunityId || (data.opportunity && data.opportunity.id) || data.id;\n\nconst input = $('Subworkflow Input').first().json;\nreturn [{ json: {\n  opportunityId: oppId,\n  locationId:    input.locationId,\n  apiKey:        input.apiKey,\n  filename:      input.filename\n} }];"
      },
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        5504,
        1328
      ],
      "id": "HIDDEN_UUID",
      "name": "Resolve Opportunity ID"
    },
    {
      "parameters": {
        "jsCode": "const mediaResult = $input.first().json;\nconst resolvedData = $('Resolve Opportunity ID').first().json;\n\nconst fileUrl = mediaResult.fileUrl || mediaResult.url || (mediaResult.data && mediaResult.data.url) || '';\n\nif (!fileUrl) {\n  throw new Error('GHL media upload did not return a file URL. Response: ' + JSON.stringify(mediaResult));\n}\n\nreturn [{ json: {\n  opportunityId: resolvedData.opportunityId,\n  apiKey:        resolvedData.apiKey,\n  fileUrl\n} }];"
      },
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        5936,
        1328
      ],
      "id": "HIDDEN_UUID",
      "name": "Extract File URL"
    },
    {
      "parameters": {
        "method": "PUT",
        "url": "=https://services.leadconnectorhq.com/opportunities/{{ $json.opportunityId }}",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "Authorization",
              "value": "Bearer HIDDEN_API_KEY"
            },
            {
              "name": "Version",
              "value": "2021-07-28"
            },
            {
              "name": "Content-Type",
              "value": "application/json"
            }
          ]
        },
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={\n  \"customFields\": [\n    {\n      \"key\": \"siteReport\",\n      \"field_value\": \"{{ $json.fileUrl }}\"\n    }\n  ]\n}",
        "options": {}
      },
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        6160,
        1328
      ],
      "id": "HIDDEN_UUID",
      "name": "Set siteReport Custom Field"
    }
  ],
  "connections": {
    "Create Contact": {
      "main": [
        [
          {
            "node": "Create Opportunity",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Create Opportunity": {
      "main": [
        [
          {
            "node": "Resolve Opportunity ID",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Resolve Opportunity ID": {
      "main": [
        [
          {
            "node": "Upload PDF to GHL Media",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Extract File URL": {
      "main": [
        [
          {
            "node": "Set siteReport Custom Field",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Set siteReport Custom Field": {
      "main": [
        [
          {
            "node": "Return Success",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}
