{
  "name": "Send Connection Requests",
  "nodes": [
    {
      "parameters": {
        "triggerTimes": {
          "item": [
            {
              "hour": "*",
              "minute": "0"
            }
          ]
        }
      },
      "name": "Cron",
      "type": "n8n-nodes-base.cron",
      "typeVersion": 1,
      "position": [
        250,
        300
      ]
    },
    {
      "parameters": {
        "query": "SELECT id, linkedin_url FROM prospects WHERE status = 'pending' ORDER BY created_at ASC LIMIT 10;"
      },
      "name": "Postgres Pending Prospects",
      "type": "n8n-nodes-base.postgres",
      "typeVersion": 1,
      "position": [
        450,
        300
      ]
    },
    {
      "parameters": {
        "batchSize": 1
      },
      "name": "SplitInBatches",
      "type": "n8n-nodes-base.splitInBatches",
      "typeVersion": 1,
      "position": [
        650,
        300
      ]
    },
    {
      "parameters": {
        "authentication": "headerAuth",
        "url": "={{$env.UNIPILE_API_URL}}/linkedin/send-connection",
        "method": "POST",
        "jsonParameters": true,
        "options": {
          "continueOnFail": true
        },
        "bodyParametersJson": "{ \"profile_url\": \"{{$json.linkedin_url}}\" }"
      },
      "name": "Unipile Send Connection",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 1,
      "position": [
        850,
        300
      ]
    },
    {
      "parameters": {
        "query": "UPDATE prospects SET status = 'requested', connection_requested_at = NOW(), updated_at = NOW() WHERE id = {{$json.id}}; INSERT INTO activity_logs (prospect_id, action_type, action_status, details) VALUES ({{$json.id}}, 'send_connection_request', 'success', jsonb_build_object('linkedin_url', {{$json.linkedin_url}}));"
      },
      "name": "Postgres Mark Requested",
      "type": "n8n-nodes-base.postgres",
      "typeVersion": 1,
      "position": [
        1050,
        250
      ]
    },
    {
      "parameters": {
        "query": "UPDATE prospects SET status = 'failed', updated_at = NOW() WHERE id = {{$json.id}}; INSERT INTO activity_logs (prospect_id, action_type, action_status, details) VALUES ({{$json.id}}, 'send_connection_request', 'failure', jsonb_build_object('error', {{$json.error.message}}, 'linkedin_url', {{$json.linkedin_url}}));"
      },
      "name": "Postgres Mark Failed",
      "type": "n8n-nodes-base.postgres",
      "typeVersion": 1,
      "position": [
        1050,
        350
      ]
    }
  ],
  "connections": {
    "Cron": {
      "main": [
        [
          {
            "node": "Postgres Pending Prospects",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Postgres Pending Prospects": {
      "main": [
        [
          {
            "node": "SplitInBatches",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "SplitInBatches": {
      "main": [
        [
          {
            "node": "Unipile Send Connection",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Unipile Send Connection": {
      "main": [
        [
          {
            "node": "Postgres Mark Requested",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Postgres Mark Failed",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}