{
  "name": "Company brief on request (webhook)",
  "nodes": [
    {
      "parameters": {
        "content": "## Company brief on request\nAsk `GET http://127.0.0.1:5678/webhook/company-brief?name=<company name>&country=EE` and get back a short brief as JSON: register facts, the latest quarter's staff, turnover and labour taxes, the management board and the contacts filed with the register. **Run now** answers the sample question in **Sample question**.\n\n**Cost per question: about $0.006** (register search $0.004, tax figures $0.002; the register trace is free). A name the register does not know is free and answers 404.",
        "height": 340,
        "width": 620,
        "color": 4
      },
      "id": "c8a2ec80-8786-5cd2-98fa-71927d8c4852",
      "name": "Note: what it does",
      "type": "n8n-nodes-base.stickyNote",
      "typeVersion": 1,
      "position": [
        -80,
        -420
      ]
    },
    {
      "parameters": {
        "content": "### What to change\n- Estonian companies get the full brief. Elsewhere (`country=FI`, `GB`, ...) the register facts still come back; the tax figures are Estonian only and stay empty.\n- Call it from a form, a CRM button, Slack or an AI agent: anything that can send a GET.\n- **Write the brief** is plain JavaScript: change the wording or the fields there.",
        "height": 340,
        "width": 460
      },
      "id": "535891d9-d4d3-515d-aed7-639bc9fe673c",
      "name": "Note: what to change",
      "type": "n8n-nodes-base.stickyNote",
      "typeVersion": 1,
      "position": [
        580,
        -420
      ]
    },
    {
      "parameters": {
        "path": "company-brief",
        "responseMode": "responseNode",
        "options": {}
      },
      "id": "c3944eb4-6206-5e92-ae89-a527cb9b6d61",
      "name": "Webhook: company-brief",
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 2.1,
      "position": [
        0,
        200
      ],
      "webhookId": "461de484-5006-5129-ae35-6b856642a578"
    },
    {
      "parameters": {},
      "id": "e63a4a47-ff2a-51ef-8d70-d3ee6a6df8a7",
      "name": "Run now",
      "type": "n8n-nodes-base.manualTrigger",
      "typeVersion": 1,
      "position": [
        0,
        0
      ]
    },
    {
      "parameters": {
        "assignments": {
          "assignments": [
            {
              "id": "dccd4d5c-f7f3-538e-9b4e-5495aa6132b6",
              "name": "query.name",
              "value": "Type a company name here",
              "type": "string"
            },
            {
              "id": "9136da9a-620f-5485-ad90-2efc2f2e5204",
              "name": "query.country",
              "value": "EE",
              "type": "string"
            }
          ]
        },
        "options": {}
      },
      "id": "cbb97b94-13e9-5c71-880a-e20e57a6ec7c",
      "name": "Sample question",
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "position": [
        200,
        0
      ]
    },
    {
      "parameters": {
        "assignments": {
          "assignments": [
            {
              "id": "9cb5495d-73bf-58d5-8371-b211ebf5bc1e",
              "name": "name",
              "value": "={{ $json.query.name }}",
              "type": "string"
            },
            {
              "id": "8263500d-b674-54a4-b1f7-faadc4d76270",
              "name": "country",
              "value": "={{ ($json.query.country || 'EE').toUpperCase() }}",
              "type": "string"
            }
          ]
        },
        "options": {}
      },
      "id": "e8bcbd8f-4b0e-5797-9989-103037f1bb2a",
      "name": "Question",
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "position": [
        300,
        200
      ]
    },
    {
      "parameters": {
        "resource": "action",
        "operation": "checkCompany",
        "companyName": "={{ $json.name }}",
        "country": "={{ $json.country }}",
        "options": {
          "maxCostUsd": 0.02,
          "tag": "n8n-company-brief"
        }
      },
      "id": "89336b7d-0fd9-5af7-9222-805aa5d22473",
      "name": "Ahel: find in register",
      "type": "n8n-nodes-ahel.ahel",
      "typeVersion": 1,
      "position": [
        480,
        100
      ]
    },
    {
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "leftValue": "",
            "typeValidation": "loose",
            "version": 2
          },
          "conditions": [
            {
              "id": "ad1219de-8b92-50dc-b393-65b66267a419",
              "leftValue": "={{ $json.found }}",
              "rightValue": "",
              "operator": {
                "type": "boolean",
                "operation": "true",
                "singleValue": true
              }
            }
          ],
          "combinator": "and"
        },
        "options": {}
      },
      "id": "3ab548f7-0f7e-5a83-a464-3ac73f40f952",
      "name": "Found?",
      "type": "n8n-nodes-base.if",
      "typeVersion": 2.2,
      "position": [
        720,
        100
      ]
    },
    {
      "parameters": {
        "resource": "action",
        "operation": "runTask",
        "task": {
          "__rl": true,
          "mode": "id",
          "value": "companies.trace"
        },
        "argumentsMode": "json",
        "argumentsJson": "={\n  \"company_key\": \"{{ $json.country }}:{{ $json.registry_number }}\",\n  \"people_limit\": 1\n}",
        "options": {
          "maxResults": 1,
          "maxCostUsd": 0.01,
          "tag": "n8n-company-brief",
          "timeoutSeconds": 90,
          "answerField": "trace"
        }
      },
      "id": "fd74ed55-75d6-5c4f-a83a-636ceb65d113",
      "name": "Ahel: register trace",
      "type": "n8n-nodes-ahel.ahel",
      "typeVersion": 1,
      "position": [
        960,
        0
      ]
    },
    {
      "parameters": {
        "resource": "action",
        "operation": "runTask",
        "task": {
          "__rl": true,
          "mode": "id",
          "value": "companies.tax.quarterly"
        },
        "argumentsMode": "json",
        "argumentsJson": "={\n  \"country\": \"{{ $json.country }}\",\n  \"registry_number\": \"{{ $json.registry_number }}\"\n}",
        "options": {
          "maxResults": 1,
          "maxCostUsd": 0.01,
          "tag": "n8n-company-brief",
          "answerField": "tax"
        }
      },
      "id": "da551f00-fec5-5a13-8eaf-c40f49990ae9",
      "name": "Ahel: tax figures",
      "type": "n8n-nodes-ahel.ahel",
      "typeVersion": 1,
      "position": [
        1200,
        0
      ],
      "onError": "continueRegularOutput"
    },
    {
      "parameters": {
        "jsCode": "// Turns the register answer, the register trace and the tax figures into one short brief.\nconst j = $json;\nconst trace = j.trace || {};\nconst reg = trace.registry || {};\nconst tax = j.tax || {};\nconst rows = (part) => ((reg[part] || {}).results || []).map((r) => r.data || {});\n\n// The register names roles and contact kinds in Estonian.\nconst ROLES = { 'Juhatuse liige': 'board member', 'Juhatuse esimees': 'chair of the board', 'Nõukogu esimees': 'chair of the supervisory board',\n  'Nõukogu liige': 'supervisory board member', 'Prokurist': 'procurator', 'Asutaja': 'founder', 'Osanik': 'shareholder', 'Aktsionär': 'shareholder' };\nconst KINDS = { 'Elektronposti aadress': 'email', 'Telefon': 'phone', 'Mobiiltelefon': 'phone', 'Interneti WWW aadress': 'website' };\n\nconst people = rows('people')\n  .filter((p) => ROLES[p.role] && !['Aktsionär', 'Osanik'].includes(p.role))\n  .slice(0, 8)\n  .map((p) => ({ name: p.name, role: ROLES[p.role], since: p.start_date || null }));\nconst contacts = {};\nfor (const c of rows('contacts')) {\n  const kind = KINDS[c.kind];\n  if (!kind || c.current === false || !c.value) continue;\n  (contacts[kind] = contacts[kind] || []).push(c.value);\n}\nconst latest = (field) => Object.entries(tax[field] || {}).filter(([, v]) => v !== null && v !== undefined).pop() || [null, null];\nconst [quarter, employees] = latest('employees');\nconst [, turnover] = latest('turnover');\nconst [, labourTaxes] = latest('labour_taxes');\nconst eur = (n) => (n === null || n === undefined ? 'not reported' : new Intl.NumberFormat('en-GB', { style: 'currency', currency: 'EUR', maximumFractionDigits: 0 }).format(n));\n\nconst lines = [\n  `${j.name} (${j.legal_form_label || j.legal_form || 'company'}, registry code ${j.registry_number}), ${j.status_label || j.status || 'status unknown'}, founded ${j.founded_date || 'unknown'}, ${j.city || ''}.`.replace(', .', '.'),\n  tax.activity ? `Activity: ${tax.activity}.` : null,\n  quarter ? `${quarter.toUpperCase()} ${tax.year}: ${employees ?? 'no'} employees, turnover ${eur(turnover)}, labour taxes ${eur(labourTaxes)}.` : 'No quarterly tax figures published.',\n  people.length ? `Management: ${people.map((p) => `${p.name} (${p.role})`).join('; ')}.` : null,\n  Object.keys(contacts).length ? `On file: ${Object.entries(contacts).map(([k, v]) => `${k} ${v.join(', ')}`).join('; ')}.` : null,\n].filter(Boolean);\n\nconst cost = (j.cost_usd || 0) + (trace.cost_usd || 0) + (tax.cost_usd || 0);\nreturn [{ json: {\n  asked_for: $('Question').first().json.name,\n  brief: lines.join('\\n'),\n  company: { name: j.name, registry_code: j.registry_number, country: j.country, legal_form: j.legal_form_label, status: j.status_label, founded: j.founded_date, city: j.city },\n  latest_quarter: quarter ? { quarter: `${tax.year} ${quarter}`, employees, turnover_eur: turnover, labour_taxes_eur: labourTaxes, activity: tax.activity || null } : null,\n  management: people,\n  contacts,\n  sources: ['Business register (RIK e-Business Register open data)', 'Estonian Tax and Customs Board quarterly figures'],\n  cost_usd: Math.round(cost * 1e6) / 1e6,\n} }];"
      },
      "id": "115f846a-fe27-5189-b1f1-5fd0cbcc290e",
      "name": "Write the brief",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        1440,
        0
      ]
    },
    {
      "parameters": {
        "respondWith": "firstIncomingItem",
        "options": {}
      },
      "id": "fc936052-1772-56a8-a92b-39ec543d279c",
      "name": "Answer with the brief",
      "type": "n8n-nodes-base.respondToWebhook",
      "typeVersion": 1.4,
      "position": [
        1680,
        0
      ]
    },
    {
      "parameters": {
        "respondWith": "json",
        "responseBody": "={{ { error: 'not_found', message: 'The register has no company by that name.', asked_for: $('Question').item.json.name, country: $('Question').item.json.country } }}",
        "options": {
          "responseCode": 404
        }
      },
      "id": "ee180479-feae-5a92-ac2d-08fd95dcb2e5",
      "name": "Answer: not found",
      "type": "n8n-nodes-base.respondToWebhook",
      "typeVersion": 1.4,
      "position": [
        960,
        220
      ]
    }
  ],
  "connections": {
    "Run now": {
      "main": [
        [
          {
            "node": "Sample question",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Sample question": {
      "main": [
        [
          {
            "node": "Question",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Webhook: company-brief": {
      "main": [
        [
          {
            "node": "Question",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Question": {
      "main": [
        [
          {
            "node": "Ahel: find in register",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Ahel: find in register": {
      "main": [
        [
          {
            "node": "Found?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Found?": {
      "main": [
        [
          {
            "node": "Ahel: register trace",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Answer: not found",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Ahel: register trace": {
      "main": [
        [
          {
            "node": "Ahel: tax figures",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Ahel: tax figures": {
      "main": [
        [
          {
            "node": "Write the brief",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Write the brief": {
      "main": [
        [
          {
            "node": "Answer with the brief",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "pinData": {},
  "settings": {
    "executionOrder": "v1"
  },
  "meta": {
    "templateCredsSetupCompleted": false
  },
  "tags": []
}
