{
  "name": "[Email Campaigns] Bulk Seed",
  "nodes": [
    {
      "parameters": {},
      "id": "manual-trigger",
      "name": "Manual Trigger",
      "type": "n8n-nodes-base.manualTrigger",
      "typeVersion": 1,
      "position": [
        220,
        300
      ]
    },
    {
      "parameters": {
        "method": "GET",
        "url": "https://dabbahwala-latest.onrender.com/api/campaigns/active-contacts",
        "options": {
          "timeout": 30000
        }
      },
      "id": "fetch-active-contacts",
      "name": "Fetch Active Contacts",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        440,
        300
      ]
    },
    {
      "parameters": {
        "jsCode": "const items = $input.all();\nif (!items || items.length === 0) return [];\nif (items.length === 1 && Array.isArray(items[0].json)) {\n  return items[0].json.map(c => ({ json: c }));\n}\nreturn items;"
      },
      "id": "split-contacts",
      "name": "Split Contacts",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        660,
        300
      ]
    },
    {
      "parameters": {
        "jsCode": "const INSTANTLY_IDS = {\n  'NURTURE_SLOW':            '76a88797-961a-47b6-af11-77e2211c4e73',\n  'PROMO_STANDARD':          'f3e2d621-9bf2-4130-bc1c-f8168fc44e1e',\n  'ACTIVE_CUSTOMER':         'c763e229-f633-468b-bfe4-7f9a4fd21036',\n  'PROMO_AGGRESSIVE':        '87d44ff1-8720-4c1d-92ff-b827970f323f',\n  'NEW_CUSTOMER_ONBOARDING': '8a5ccbfb-500d-4060-ad99-76aa0159bbf2',\n  'REACTIVATION':            '69c84455-d9b8-437f-b249-8325d23798e6'\n};\n\nconst item = $input.item.json;\nconst campaignId = INSTANTLY_IDS[item.current_campaign];\n\nif (!campaignId) {\n  return { json: { skip: true, email: item.email, reason: 'no_instantly_campaign_for_' + item.current_campaign } };\n}\n\nconst firstName = (item.first_name || '').trim();\nif (!firstName) {\n  return { json: { skip: true, email: item.email, reason: 'missing_first_name' } };\n}\n\nreturn {\n  json: {\n    skip:        false,\n    campaign:    campaignId,\n    email:       item.email,\n    first_name:  firstName,\n    last_name:   (item.last_name || '').trim(),\n    phone:       item.phone || null\n  }\n};",
        "mode": "runOnceForEachItem"
      },
      "id": "prepare-seed-payload",
      "name": "Prepare Seed Payload",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        880,
        300
      ]
    },
    {
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "leftValue": "",
            "typeValidation": "strict"
          },
          "conditions": [
            {
              "id": "not-skip",
              "leftValue": "={{ $json.skip }}",
              "rightValue": true,
              "operator": {
                "type": "boolean",
                "operation": "false"
              }
            }
          ],
          "combinator": "and"
        },
        "options": {}
      },
      "id": "has-first-name",
      "name": "Has First Name?",
      "type": "n8n-nodes-base.if",
      "typeVersion": 2,
      "position": [
        1100,
        300
      ]
    },
    {
      "parameters": {
        "method": "POST",
        "url": "https://api.instantly.ai/api/v2/leads",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "Authorization",
              "value": "=Bearer {{ $('Get Credentials').first().json.INSTANTLY_BEARER }}"
            }
          ]
        },
        "sendBody": true,
        "bodyParameters": {
          "parameters": [
            {
              "name": "campaign_id",
              "value": "={{ $json.campaign }}"
            },
            {
              "name": "email",
              "value": "={{ $json.email }}"
            },
            {
              "name": "first_name",
              "value": "={{ $json.first_name }}"
            },
            {
              "name": "last_name",
              "value": "={{ $json.last_name }}"
            },
            {
              "name": "phone",
              "value": "={{ $json.phone }}"
            },
            {
              "name": "skip_if_in_workspace",
              "value": "=true"
            }
          ]
        },
        "options": {
          "batching": {
            "batch": {
              "batchSize": 5,
              "batchInterval": 2000
            }
          }
        },
        "authentication": "none"
      },
      "id": "add-lead-to-instantly",
      "name": "Add Lead to Instantly",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        1320,
        220
      ],
      "continueOnFail": true
    },
    {
      "parameters": {},
      "id": "skipped-no-op",
      "name": "Skipped \u2014 No First Name",
      "type": "n8n-nodes-base.noOp",
      "typeVersion": 1,
      "position": [
        1320,
        400
      ]
    },
    {
      "id": "get-credentials",
      "name": "Get Credentials",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        470,
        300
      ],
      "parameters": {
        "method": "GET",
        "url": "https://dabbahwala-latest.onrender.com/api/credentials",
        "authentication": "genericCredentialType",
        "genericAuthType": "httpHeaderAuth",
        "options": {}
      },
      "credentials": {
        "httpHeaderAuth": {
          "name": "<your credential>"
        }
      }
    }
  ],
  "connections": {
    "Manual Trigger": {
      "main": [
        [
          {
            "node": "Get Credentials",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Fetch Active Contacts": {
      "main": [
        [
          {
            "node": "Split Contacts",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Split Contacts": {
      "main": [
        [
          {
            "node": "Prepare Seed Payload",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Prepare Seed Payload": {
      "main": [
        [
          {
            "node": "Has First Name?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Has First Name?": {
      "main": [
        [
          {
            "node": "Add Lead to Instantly",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Skipped \u2014 No First Name",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Get Credentials": {
      "main": [
        [
          {
            "node": "Prepare Seed Payload",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "settings": {
    "executionOrder": "v1"
  },
  "tags": [
    {
      "name": "DabbahWala",
      "id": "dabbahwala"
    },
    {
      "name": "Instantly",
      "id": "tag-instantly"
    }
  ]
}