{
  "name": "Company Enrichment - WORKING",
  "nodes": [
    {
      "parameters": {
        "path": "company-enrichment-working",
        "httpMethod": "POST",
        "responseMode": "responseNode",
        "options": {}
      },
      "id": "webhook-working",
      "name": "Webhook",
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 2,
      "position": [
        250,
        300
      ]
    },
    {
      "parameters": {
        "functionCode": "const startupData = items[0].json;\nreturn [{\n  json: {\n    startup_id: startupData.startup_id,\n    company_name: startupData.company_name,\n    website: startupData.website || '',\n    domain: startupData.domain || (startupData.website ? new URL(startupData.website).hostname : ''),\n    company_number: startupData.company_number || '',\n    jurisdiction: startupData.jurisdiction || 'us'\n  }\n}];"
      },
      "id": "extract-working",
      "name": "Extract Startup Data",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        450,
        300
      ]
    },
    {
      "parameters": {
        "url": "={{$json.website}}",
        "options": {
          "timeout": 10000
        }
      },
      "id": "website-working",
      "name": "1. Fetch Website",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4,
      "position": [
        650,
        200
      ],
      "continueOnFail": true
    },
    {
      "parameters": {
        "url": "=https://api.opencorporates.com/v0.4/companies/search?q={{$json.company_name}}&jurisdiction_code={{$json.jurisdiction}}",
        "options": {
          "timeout": 10000
        }
      },
      "id": "opencorp-working",
      "name": "2. OpenCorporates",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4,
      "position": [
        650,
        400
      ],
      "continueOnFail": true
    },
    {
      "parameters": {
        "functionCode": "const allInputs = $input.all();\n\nlet websiteData = {};\nlet opencorporatesData = {};\n\nfor (const input of allInputs) {\n  const json = input.json;\n  if (json.body && json.statusCode) {\n    websiteData = json;\n  } else if (json.results && json.results.companies) {\n    opencorporatesData = json;\n  }\n}\n\nconst startupData = $node['Extract Startup Data'].json;\n\nconst enrichmentData = {\n  enriched_at: new Date().toISOString(),\n  sources: [\n    websiteData.body ? 'website_scraping' : null,\n    opencorporatesData.results ? 'opencorporates' : null\n  ].filter(Boolean),\n\n  founded_year: null,\n  employee_count: null,\n  headquarters: {\n    city: null,\n    country: null,\n    address: null\n  },\n\n  social: {\n    linkedin: null,\n    twitter: null,\n    github: null,\n    crunchbase: null\n  },\n\n  legal: {\n    company_number: opencorporatesData.results?.companies?.[0]?.company?.company_number || null,\n    jurisdiction: opencorporatesData.results?.companies?.[0]?.company?.jurisdiction_code || null,\n    incorporation_date: opencorporatesData.results?.companies?.[0]?.company?.incorporation_date || null,\n    legal_status: opencorporatesData.results?.companies?.[0]?.company?.current_status || null,\n    company_type: opencorporatesData.results?.companies?.[0]?.company?.company_type || null,\n    registered_address: opencorporatesData.results?.companies?.[0]?.company?.registered_address_in_full || null\n  },\n\n  market: {\n    market_trend_score: null,\n    competitive_landscape: null\n  },\n\n  _raw: {\n    website_html_length: websiteData.body?.length || 0,\n    opencorporates_results_count: opencorporatesData.results?.companies?.length || 0\n  },\n\n  errors: [\n    !websiteData.body ? 'website_fetch_failed' : null,\n    !opencorporatesData.results ? 'opencorporates_failed' : null\n  ].filter(Boolean)\n};\n\nreturn [{\n  json: {\n    startup_id: startupData.startup_id,\n    company_name: startupData.company_name,\n    enrichment_data: enrichmentData,\n    enrichment_status: enrichmentData.errors.length > 0 ? 'partial' : 'completed',\n    enrichment_quality_score: (\n      (enrichmentData.legal.company_number ? 50 : 0) +\n      (websiteData.body?.length > 1000 ? 30 : 0) +\n      (enrichmentData.legal.incorporation_date ? 20 : 0)\n    )\n  }\n}];"
      },
      "id": "aggregate-working",
      "name": "Aggregate Enrichment Data",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        850,
        300
      ]
    },
    {
      "parameters": {
        "operation": "update",
        "tableId": "deals",
        "options": {}
      },
      "id": "supabase-working",
      "name": "Store in Supabase",
      "type": "n8n-nodes-base.supabase",
      "typeVersion": 1,
      "position": [
        1050,
        300
      ],
      "continueOnFail": true,
      "credentials": {
        "supabaseApi": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "respondWith": "json",
        "responseBody": "={{$json}}",
        "options": {}
      },
      "id": "respond-working",
      "name": "Respond to Webhook",
      "type": "n8n-nodes-base.respondToWebhook",
      "typeVersion": 1,
      "position": [
        1250,
        300
      ]
    }
  ],
  "connections": {
    "Webhook": {
      "main": [
        [
          {
            "node": "Extract Startup Data",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Extract Startup Data": {
      "main": [
        [
          {
            "node": "1. Fetch Website",
            "type": "main",
            "index": 0
          },
          {
            "node": "2. OpenCorporates",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "1. Fetch Website": {
      "main": [
        [
          {
            "node": "Aggregate Enrichment Data",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "2. OpenCorporates": {
      "main": [
        [
          {
            "node": "Aggregate Enrichment Data",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Aggregate Enrichment Data": {
      "main": [
        [
          {
            "node": "Store in Supabase",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Store in Supabase": {
      "main": [
        [
          {
            "node": "Respond to Webhook",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "settings": {},
  "tags": []
}