{
  "name": "Loop 4 \u2014 Enrichment",
  "nodes": [
    {
      "parameters": {
        "rule": {
          "interval": [
            {
              "field": "hours",
              "hoursInterval": 1
            }
          ]
        }
      },
      "id": "node-cron-4",
      "name": "Cron Trigger",
      "type": "n8n-nodes-base.scheduleTrigger",
      "typeVersion": 1.1,
      "position": [
        200,
        300
      ]
    },
    {
      "parameters": {
        "operation": "list",
        "base": {
          "__rl": true,
          "value": "YOUR_AIRTABLE_BASE_ID",
          "mode": "id"
        },
        "table": {
          "__rl": true,
          "value": "Leads",
          "mode": "name"
        },
        "filterByFormula": "AND({status}='active',OR({last_enriched_at}='',IS_BEFORE({last_enriched_at},DATEADD(NOW(),-1,'hours'))))"
      },
      "id": "node-at-list-4",
      "name": "List Unenriched Leads",
      "type": "n8n-nodes-base.airtable",
      "typeVersion": 2,
      "position": [
        420,
        300
      ],
      "credentials": {
        "airtableTokenApi": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "method": "GET",
        "url": "=https://api.apollo.io/v1/people/match?api_key=YOUR_APOLLO_API_KEY&email={{encodeURIComponent($json.fields.email)}}",
        "options": {}
      },
      "id": "node-apollo-4",
      "name": "Apollo Enrich",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.1,
      "position": [
        640,
        300
      ]
    },
    {
      "parameters": {
        "jsCode": "const apollo=$json.person||{};\nconst now=new Date().toISOString();\nconst next=new Date(Date.now()+60*60000).toISOString();\nreturn[{json:{lead_id:$('List Unenriched Leads').item.json.id,company:apollo.organization?.name||'',job_title:apollo.title||'',linkedin_url:apollo.linkedin_url||'',company_size:apollo.organization?.estimated_num_employees||'',last_enriched_at:now,next_enrichment_at:next}}];"
      },
      "id": "node-map-4",
      "name": "Map Fields",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        860,
        300
      ]
    },
    {
      "parameters": {
        "operation": "update",
        "base": {
          "__rl": true,
          "value": "YOUR_AIRTABLE_BASE_ID",
          "mode": "id"
        },
        "table": {
          "__rl": true,
          "value": "Leads",
          "mode": "name"
        },
        "id": "={{$json.lead_id}}",
        "fields": {
          "fieldMappingMode": "defineBelow",
          "value": {
            "values": [
              {
                "fieldId": "company",
                "fieldValue": "={{$json.company}}"
              },
              {
                "fieldId": "job_title",
                "fieldValue": "={{$json.job_title}}"
              },
              {
                "fieldId": "linkedin_url",
                "fieldValue": "={{$json.linkedin_url}}"
              },
              {
                "fieldId": "last_enriched_at",
                "fieldValue": "={{$json.last_enriched_at}}"
              },
              {
                "fieldId": "next_enrichment_at",
                "fieldValue": "={{$json.next_enrichment_at}}"
              }
            ]
          }
        }
      },
      "id": "node-at-update-4",
      "name": "Update Lead",
      "type": "n8n-nodes-base.airtable",
      "typeVersion": 2,
      "position": [
        1080,
        300
      ],
      "credentials": {
        "airtableTokenApi": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "operation": "create",
        "base": {
          "__rl": true,
          "value": "YOUR_AIRTABLE_BASE_ID",
          "mode": "id"
        },
        "table": {
          "__rl": true,
          "value": "Interactions",
          "mode": "name"
        },
        "fields": {
          "fieldMappingMode": "defineBelow",
          "value": {
            "values": [
              {
                "fieldId": "lead_id",
                "fieldValue": "={{$json.lead_id}}"
              },
              {
                "fieldId": "type",
                "fieldValue": "enrichment"
              },
              {
                "fieldId": "loop_source",
                "fieldValue": "loop_4"
              },
              {
                "fieldId": "created_at",
                "fieldValue": "={{$json.last_enriched_at}}"
              }
            ]
          }
        }
      },
      "id": "node-at-interact-4",
      "name": "Log Interaction",
      "type": "n8n-nodes-base.airtable",
      "typeVersion": 2,
      "position": [
        1300,
        300
      ],
      "credentials": {
        "airtableTokenApi": {
          "name": "<your credential>"
        }
      }
    }
  ],
  "connections": {
    "Cron Trigger": {
      "main": [
        [
          {
            "node": "List Unenriched Leads",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "List Unenriched Leads": {
      "main": [
        [
          {
            "node": "Apollo Enrich",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Apollo Enrich": {
      "main": [
        [
          {
            "node": "Map Fields",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Map Fields": {
      "main": [
        [
          {
            "node": "Update Lead",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Update Lead": {
      "main": [
        [
          {
            "node": "Log Interaction",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "settings": {
    "executionOrder": "v1"
  },
  "tags": []
}