{
  "name": "Fleetix: nová rezervácia → Google Sheets, Slack a e-mail",
  "nodes": [
    {
      "parameters": {
        "httpMethod": "POST",
        "path": "fleetix-rezervacie",
        "responseMode": "onReceived",
        "options": {
          "rawBody": true
        }
      },
      "id": "00000000-0000-0000-0000-000000000001",
      "name": "Webhook z Fleetixu",
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 2,
      "position": [
        0,
        0
      ],
      "webhookId": "00000000-0000-0000-0000-0000000003e9"
    },
    {
      "parameters": {
        "jsCode": "// Overenie podpisu Fleetixu: X-Fleetix-Signature = sha256=<hex HMAC-SHA256(tajomstvo, surové telo)>.\n// Tajomstvo webhooku (whsec_...) vložte nižšie alebo do premennej prostredia FLEETIX_WEBHOOK_SECRET.\n// n8n na vlastnom serveri: povoľte modul crypto (NODE_FUNCTION_ALLOW_BUILTIN=crypto).\nconst crypto = require('crypto');\nconst SECRET = $env.FLEETIX_WEBHOOK_SECRET || 'whsec_VLOZTE_TAJOMSTVO';\nconst item = $input.first();\nconst raw = Buffer.from(item.binary.data.data, 'base64').toString('utf8');\nconst header = String(item.json.headers['x-fleetix-signature'] || '');\nconst expected = 'sha256=' + crypto.createHmac('sha256', SECRET).update(raw).digest('hex');\nconst a = Buffer.from(header), b = Buffer.from(expected);\nif (a.length !== b.length || !crypto.timingSafeEqual(a, b)) {\n  throw new Error('Neplatný podpis Fleetixu, udalosť sa nespracuje.');\n}\nconst event = JSON.parse(raw);\nreturn [{ json: event }];"
      },
      "id": "00000000-0000-0000-0000-000000000002",
      "name": "Overiť podpis",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        220,
        0
      ]
    },
    {
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "leftValue": "",
            "typeValidation": "strict"
          },
          "conditions": [
            {
              "id": "00000000-0000-0000-0000-00000000138b",
              "leftValue": "={{ $json.type }}",
              "rightValue": "reservation.created",
              "operator": {
                "type": "string",
                "operation": "equals"
              }
            }
          ],
          "combinator": "or"
        },
        "options": {}
      },
      "id": "00000000-0000-0000-0000-000000000003",
      "name": "Len nové rezervácie",
      "type": "n8n-nodes-base.if",
      "typeVersion": 2,
      "position": [
        440,
        0
      ]
    },
    {
      "parameters": {
        "url": "=https://api.fleetix.eu/v1/vehicles/{{ $json.data.reservation.vehicleId }}",
        "authentication": "genericCredentialType",
        "genericAuthType": "httpHeaderAuth",
        "options": {}
      },
      "id": "00000000-0000-0000-0000-000000000004",
      "name": "Načítať auto z API",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        660,
        0
      ],
      "notes": "Credential Header Auth: meno Authorization, hodnota Bearer fx_live_... (kľúč s rozsahom vehicles:read)."
    },
    {
      "parameters": {
        "operation": "append",
        "documentId": {
          "__rl": true,
          "mode": "url",
          "value": "https://docs.google.com/spreadsheets/d/VASA_TABULKA/edit"
        },
        "sheetName": {
          "__rl": true,
          "mode": "name",
          "value": "Rezervácie"
        },
        "columns": {
          "mappingMode": "defineBelow",
          "value": {
            "Číslo": "={{ $('Overiť podpis').item.json.data.reservation.number }}",
            "Stav": "={{ $('Overiť podpis').item.json.data.reservation.status }}",
            "Auto": "={{ $json.name }}",
            "Prevzatie": "={{ $('Overiť podpis').item.json.data.reservation.pickupAt }}",
            "Vrátenie": "={{ $('Overiť podpis').item.json.data.reservation.returnAt }}",
            "Cena": "={{ $('Overiť podpis').item.json.data.reservation.price.total }}",
            "Mena": "={{ $('Overiť podpis').item.json.data.reservation.currency }}",
            "Zdroj": "={{ $('Overiť podpis').item.json.data.reservation.source }}",
            "Portál": "={{ $('Overiť podpis').item.json.data.reservation.portalUrl }}",
            "Vytvorené": "={{ $('Overiť podpis').item.json.data.reservation.createdAt }}"
          },
          "matchingColumns": [],
          "schema": []
        },
        "options": {}
      },
      "id": "00000000-0000-0000-0000-000000000005",
      "name": "Zapísať do Google Sheets",
      "type": "n8n-nodes-base.googleSheets",
      "typeVersion": 4.5,
      "position": [
        880,
        -160
      ],
      "notes": "Vytvorte hárok Rezervácie so stĺpcami Číslo, Stav, Auto, Prevzatie, Vrátenie, Cena, Mena, Zdroj, Portál, Vytvorené."
    },
    {
      "parameters": {
        "select": "channel",
        "channelId": {
          "__rl": true,
          "mode": "name",
          "value": "#rezervacie"
        },
        "text": "=Nová rezervácia {{ $('Overiť podpis').item.json.data.reservation.number }}: {{ $json.name }}, {{ DateTime.fromISO($('Overiť podpis').item.json.data.reservation.pickupAt).setZone('Europe/Bratislava').toFormat('d. M. HH:mm') }} až {{ DateTime.fromISO($('Overiť podpis').item.json.data.reservation.returnAt).setZone('Europe/Bratislava').toFormat('d. M. HH:mm') }}, {{ $('Overiť podpis').item.json.data.reservation.price.total }} {{ $('Overiť podpis').item.json.data.reservation.currency }}",
        "otherOptions": {}
      },
      "id": "00000000-0000-0000-0000-000000000006",
      "name": "Poslať do Slacku",
      "type": "n8n-nodes-base.slack",
      "typeVersion": 2.2,
      "position": [
        880,
        0
      ]
    },
    {
      "parameters": {
        "fromEmail": "rezervacie@vasa-firma.sk",
        "toEmail": "pobocka@vasa-firma.sk",
        "subject": "=Nová rezervácia {{ $('Overiť podpis').item.json.data.reservation.number }}",
        "emailFormat": "text",
        "text": "=Auto: {{ $json.name }}\nPrevzatie: {{ $('Overiť podpis').item.json.data.reservation.pickupAt }}\nVrátenie: {{ $('Overiť podpis').item.json.data.reservation.returnAt }}\nCena: {{ $('Overiť podpis').item.json.data.reservation.price.total }} {{ $('Overiť podpis').item.json.data.reservation.currency }}\nPotvrďte ju vo Fleetixe.",
        "options": {}
      },
      "id": "00000000-0000-0000-0000-000000000007",
      "name": "Poslať e-mail pobočke",
      "type": "n8n-nodes-base.emailSend",
      "typeVersion": 2.1,
      "position": [
        880,
        160
      ]
    }
  ],
  "connections": {
    "Webhook z Fleetixu": {
      "main": [
        [
          {
            "node": "Overiť podpis",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Overiť podpis": {
      "main": [
        [
          {
            "node": "Len nové rezervácie",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Len nové rezervácie": {
      "main": [
        [
          {
            "node": "Načítať auto z API",
            "type": "main",
            "index": 0
          }
        ],
        []
      ]
    },
    "Načítať auto z API": {
      "main": [
        [
          {
            "node": "Zapísať do Google Sheets",
            "type": "main",
            "index": 0
          },
          {
            "node": "Poslať do Slacku",
            "type": "main",
            "index": 0
          },
          {
            "node": "Poslať e-mail pobočke",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "settings": {
    "executionOrder": "v1"
  },
  "meta": {
    "templateCredsSetupCompleted": false,
    "source": "https://js.fleetix.eu/sablony/"
  },
  "tags": []
}
