{
  "name": "Prospec\u00e7\u00e3o Ativa - Financial Services & M\u00eddia LATAM",
  "nodes": [
    {
      "parameters": {
        "rule": {
          "interval": [
            {}
          ]
        }
      },
      "type": "n8n-nodes-base.scheduleTrigger",
      "typeVersion": 1.2,
      "position": [
        -1600,
        0
      ],
      "id": "trigger-001",
      "name": "Manual Trigger"
    },
    {
      "parameters": {
        "method": "POST",
        "url": "https://api.apollo.io/v1/mixed_companies/search",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "Content-Type",
              "value": "application/json"
            },
            {
              "name": "X-Api-Key",
              "value": "SUA_APOLLO_API_KEY"
            }
          ]
        },
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={\n  \"page\": 1,\n  \"per_page\": 100,\n  \"organization_industry_tag_ids\": [],\n  \"organization_locations\": [\n    \"Brazil\",\n    \"Mexico\",\n    \"Colombia\",\n    \"Argentina\",\n    \"Chile\",\n    \"Peru\",\n    \"Ecuador\",\n    \"Venezuela\",\n    \"Bolivia\",\n    \"Paraguay\",\n    \"Uruguay\"\n  ],\n  \"q_organization_keyword_tags\": [\n    \"financial services\",\n    \"fintech\",\n    \"banking\",\n    \"media\",\n    \"journalism\",\n    \"broadcasting\"\n  ],\n  \"organization_num_employees_ranges\": [\n    \"50,500\"\n  ]\n}",
        "options": {}
      },
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.3,
      "position": [
        -1360,
        0
      ],
      "id": "apollo-001",
      "name": "Apollo - Buscar Empresas"
    },
    {
      "parameters": {
        "fieldToSplitOut": "organizations",
        "options": {}
      },
      "type": "n8n-nodes-base.splitOut",
      "typeVersion": 1,
      "position": [
        -1120,
        0
      ],
      "id": "split-001",
      "name": "Split Empresas"
    },
    {
      "parameters": {
        "options": {}
      },
      "type": "n8n-nodes-base.splitInBatches",
      "typeVersion": 3,
      "position": [
        -880,
        0
      ],
      "id": "loop-001",
      "name": "Loop Over Empresas"
    },
    {
      "parameters": {
        "method": "POST",
        "url": "https://api.tavily.com/search",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "Content-Type",
              "value": "application/json"
            }
          ]
        },
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={\n  \"api_key\": \"SUA_TAVILY_API_KEY\",\n  \"query\": \"{{ $json.name }} merger acquisition acquired M&A Latin America\",\n  \"search_depth\": \"basic\",\n  \"max_results\": 3\n}",
        "options": {}
      },
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.3,
      "position": [
        -640,
        -160
      ],
      "id": "tavily-ma",
      "name": "Tavily - Check M&A"
    },
    {
      "parameters": {
        "method": "POST",
        "url": "https://api.tavily.com/search",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "Content-Type",
              "value": "application/json"
            }
          ]
        },
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={\n  \"api_key\": \"SUA_TAVILY_API_KEY\",\n  \"query\": \"{{ $('Loop Over Empresas').item.json.name }} scandal fraud corruption lawsuit investigation\",\n  \"search_depth\": \"basic\",\n  \"max_results\": 3\n}",
        "options": {}
      },
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.3,
      "position": [
        -640,
        0
      ],
      "id": "tavily-scandal",
      "name": "Tavily - Check Esc\u00e2ndalos"
    },
    {
      "parameters": {
        "method": "POST",
        "url": "https://api.tavily.com/search",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "Content-Type",
              "value": "application/json"
            }
          ]
        },
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={\n  \"api_key\": \"SUA_TAVILY_API_KEY\",\n  \"query\": \"{{ $('Loop Over Empresas').item.json.name }} number of employees total staff headcount\",\n  \"search_depth\": \"basic\",\n  \"max_results\": 3\n}",
        "options": {}
      },
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.3,
      "position": [
        -640,
        160
      ],
      "id": "tavily-employees",
      "name": "Tavily - N\u00famero Exato Funcion\u00e1rios"
    },
    {
      "parameters": {
        "jsCode": "const company = $('Loop Over Empresas').item.json;\nconst maResults = $('Tavily - Check M&A').item.json;\nconst scandalResults = $('Tavily - Check Esc\u00e2ndalos').item.json;\nconst employeeResults = $('Tavily - N\u00famero Exato Funcion\u00e1rios').item.json;\n\n// --- CHECK M&A ---\nconst maKeywords = ['acquired', 'merger', 'acquisition', 'adquirida', 'fus\u00e3o', 'aquisi\u00e7\u00e3o', 'comprada por'];\nconst maText = JSON.stringify(maResults).toLowerCase();\nconst hasMa = maKeywords.some(k => maText.includes(k));\nconst maStatus = hasMa ? 'Sim - Verificar' : 'N\u00e3o Identificado';\n\n// --- CHECK ESC\u00c2NDALOS ---\nconst scandalKeywords = ['fraud', 'corruption', 'scandal', 'lawsuit', 'investigation', 'fraude', 'corrup\u00e7\u00e3o', 'esc\u00e2ndalo', 'processo', 'investiga\u00e7\u00e3o', 'san\u00e7\u00e3o'];\nconst scandalText = JSON.stringify(scandalResults).toLowerCase();\nconst hasScandal = scandalKeywords.some(k => scandalText.includes(k));\nconst scandalStatus = hasScandal ? 'Sim - Verificar' : 'N\u00e3o Identificado';\n\n// --- N\u00daMERO EXATO DE FUNCION\u00c1RIOS ---\nconst empText = JSON.stringify(employeeResults);\nconst empMatch = empText.match(/(\\d{2,6})\\s*(employees|funcion\u00e1rios|staff|colaboradores)/i);\nconst exactEmployees = empMatch ? empMatch[1] : (company.estimated_num_employees || company.num_employees || 'N\u00e3o encontrado');\n\n// --- FILTROS ---\nconst numEmp = parseInt(exactEmployees) || parseInt(company.estimated_num_employees) || 0;\nconst passesEmployeeFilter = numEmp >= 50;\nconst passesMaFilter = !hasMa;\nconst passesScandalFilter = !hasScandal;\n\nif (!passesEmployeeFilter || !passesMaFilter || !passesScandalFilter) {\n  return [];\n}\n\n// --- MONTAR LINHA FINAL ---\nreturn [{\n  json: {\n    'Nome da Empresa': company.name || '',\n    'Setor': company.industry || '',\n    'Pa\u00eds': company.hq_location_country || company.country || '',\n    'Cidade': company.hq_location_city || company.city || '',\n    'Website': company.website_url || company.primary_domain || '',\n    'N\u00famero de Funcion\u00e1rios': exactEmployees,\n    'S\u00f3cios / C-Levels': (company.organization_job_postings || []).length > 0 ? 'Ver LinkedIn' : 'N\u00e3o encontrado',\n    'Receita Estimada': company.annual_revenue_printed || 'N\u00e3o encontrado',\n    'Status M&A': maStatus,\n    'Esc\u00e2ndalos': scandalStatus,\n    'Data': new Date().toISOString().split('T')[0]\n  }\n}];"
      },
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        -320,
        0
      ],
      "id": "filter-001",
      "name": "Filtros + Montar Linha"
    },
    {
      "parameters": {
        "jsCode": "const items = $input.all();\n\nif (items.length === 0) {\n  return [{ json: { message: 'Nenhuma empresa passou pelos filtros.' } }];\n}\n\n// Cabe\u00e7alhos\nconst headers = [\n  'Nome da Empresa',\n  'Setor',\n  'Pa\u00eds',\n  'Cidade',\n  'Website',\n  'N\u00famero de Funcion\u00e1rios',\n  'S\u00f3cios / C-Levels',\n  'Receita Estimada',\n  'Status M&A',\n  'Esc\u00e2ndalos',\n  'Data'\n];\n\n// Montar CSV\nconst rows = items.map(item => {\n  return headers.map(h => {\n    const val = item.json[h] || '';\n    return `\"${String(val).replace(/\"/g, '\"\"')}\"`;\n  }).join(',');\n});\n\nconst csv = [headers.join(','), ...rows].join('\\n');\n\nreturn [{\n  json: {},\n  binary: {\n    data: {\n      data: Buffer.from(csv).toString('base64'),\n      mimeType: 'text/csv',\n      fileName: `prospeccao_latam_${new Date().toISOString().split('T')[0]}.csv`,\n      fileExtension: 'csv'\n    }\n  }\n}];"
      },
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        160,
        0
      ],
      "id": "excel-001",
      "name": "Gerar CSV/Excel"
    },
    {
      "parameters": {
        "respondWith": "binary",
        "options": {
          "responseHeaders": {
            "entries": [
              {
                "name": "Content-Disposition",
                "value": "=attachment; filename=\"prospeccao_latam_{{ $now.format('yyyy-MM-dd') }}.csv\""
              },
              {
                "name": "Content-Type",
                "value": "text/csv"
              }
            ]
          }
        }
      },
      "type": "n8n-nodes-base.respondToWebhook",
      "typeVersion": 1.4,
      "position": [
        400,
        0
      ],
      "id": "respond-001",
      "name": "Download Excel"
    },
    {
      "parameters": {
        "content": "## 1. TRIGGER\nClique em Execute para rodar o workflow manualmente.",
        "height": 200,
        "width": 240,
        "color": 3
      },
      "type": "n8n-nodes-base.stickyNote",
      "typeVersion": 1,
      "position": [
        -1640,
        -160
      ],
      "id": "note-001",
      "name": "Nota 1"
    },
    {
      "parameters": {
        "content": "## 2. APOLLO\nBusca 100 empresas de Financial Services e M\u00eddia na Am\u00e9rica Latina com 50-500 funcion\u00e1rios.\n\n\u26a0\ufe0f Troque SUA_APOLLO_API_KEY pela sua chave.",
        "height": 240,
        "width": 280,
        "color": 4
      },
      "type": "n8n-nodes-base.stickyNote",
      "typeVersion": 1,
      "position": [
        -1400,
        -200
      ],
      "id": "note-002",
      "name": "Nota 2"
    },
    {
      "parameters": {
        "content": "## 3. CHECKS via TAVILY\n- M&A: verifica se foi adquirida ou fez aquisi\u00e7\u00f5es\n- Esc\u00e2ndalos: fraude, corrup\u00e7\u00e3o, processos\n- Funcion\u00e1rios: tenta buscar n\u00famero exato\n\n\u26a0\ufe0f Troque SUA_TAVILY_API_KEY pela sua chave.",
        "height": 300,
        "width": 280,
        "color": 7
      },
      "type": "n8n-nodes-base.stickyNote",
      "typeVersion": 1,
      "position": [
        -880,
        -320
      ],
      "id": "note-003",
      "name": "Nota 3"
    },
    {
      "parameters": {
        "content": "## 4. FILTROS\nEmpresas que N\u00c3O passam s\u00e3o descartadas:\n- Passou por M&A \u2192 fora\n- Tem esc\u00e2ndalo \u2192 fora\n- Menos de 50 funcion\u00e1rios \u2192 fora",
        "height": 240,
        "width": 260,
        "color": 6
      },
      "type": "n8n-nodes-base.stickyNote",
      "typeVersion": 1,
      "position": [
        -360,
        -240
      ],
      "id": "note-004",
      "name": "Nota 4"
    },
    {
      "parameters": {
        "content": "## 5. OUTPUT\nGera arquivo CSV compat\u00edvel com Excel e disponibiliza para download.",
        "height": 200,
        "width": 240,
        "color": 3
      },
      "type": "n8n-nodes-base.stickyNote",
      "typeVersion": 1,
      "position": [
        120,
        -200
      ],
      "id": "note-005",
      "name": "Nota 5"
    }
  ],
  "connections": {
    "Manual Trigger": {
      "main": [
        [
          {
            "node": "Apollo - Buscar Empresas",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Apollo - Buscar Empresas": {
      "main": [
        [
          {
            "node": "Split Empresas",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Split Empresas": {
      "main": [
        [
          {
            "node": "Loop Over Empresas",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Loop Over Empresas": {
      "main": [
        [],
        [
          {
            "node": "Tavily - Check M&A",
            "type": "main",
            "index": 0
          },
          {
            "node": "Tavily - Check Esc\u00e2ndalos",
            "type": "main",
            "index": 0
          },
          {
            "node": "Tavily - N\u00famero Exato Funcion\u00e1rios",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Tavily - Check M&A": {
      "main": [
        [
          {
            "node": "Filtros + Montar Linha",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Tavily - Check Esc\u00e2ndalos": {
      "main": [
        [
          {
            "node": "Filtros + Montar Linha",
            "type": "main",
            "index": 1
          }
        ]
      ]
    },
    "Tavily - N\u00famero Exato Funcion\u00e1rios": {
      "main": [
        [
          {
            "node": "Filtros + Montar Linha",
            "type": "main",
            "index": 2
          }
        ]
      ]
    },
    "Filtros + Montar Linha": {
      "main": [
        [
          {
            "node": "Gerar CSV/Excel",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Gerar CSV/Excel": {
      "main": [
        [
          {
            "node": "Download Excel",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "active": false,
  "settings": {
    "executionOrder": "v1"
  },
  "meta": {
    "templateCredsSetupCompleted": false
  },
  "tags": []
}