{
  "name": "Fleetix: platba a faktúra → účtovníctvo",
  "nodes": [
    {
      "parameters": {
        "httpMethod": "POST",
        "path": "fleetix-uctovnictvo",
        "responseMode": "onReceived",
        "options": {
          "rawBody": true
        }
      },
      "id": "00000000-0000-0000-0000-00000000000b",
      "name": "Webhook z Fleetixu",
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 2,
      "position": [
        0,
        0
      ],
      "webhookId": "00000000-0000-0000-0000-0000000003f3"
    },
    {
      "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-00000000000c",
      "name": "Overiť podpis",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        220,
        0
      ]
    },
    {
      "parameters": {
        "rules": {
          "values": [
            {
              "conditions": {
                "options": {
                  "caseSensitive": true,
                  "leftValue": "",
                  "typeValidation": "strict"
                },
                "conditions": [
                  {
                    "id": "00000000-0000-0000-0000-000000001771",
                    "leftValue": "={{ $json.type }}",
                    "rightValue": "payment.succeeded",
                    "operator": {
                      "type": "string",
                      "operation": "equals"
                    }
                  }
                ],
                "combinator": "and"
              },
              "renameOutput": true,
              "outputKey": "Platba"
            },
            {
              "conditions": {
                "options": {
                  "caseSensitive": true,
                  "leftValue": "",
                  "typeValidation": "strict"
                },
                "conditions": [
                  {
                    "id": "00000000-0000-0000-0000-000000001772",
                    "leftValue": "={{ $json.type }}",
                    "rightValue": "invoice.issued",
                    "operator": {
                      "type": "string",
                      "operation": "equals"
                    }
                  }
                ],
                "combinator": "and"
              },
              "renameOutput": true,
              "outputKey": "Faktúra"
            }
          ]
        },
        "options": {}
      },
      "id": "00000000-0000-0000-0000-00000000000d",
      "name": "Podľa udalosti",
      "type": "n8n-nodes-base.switch",
      "typeVersion": 3.2,
      "position": [
        440,
        0
      ]
    },
    {
      "parameters": {
        "operation": "append",
        "documentId": {
          "__rl": true,
          "mode": "url",
          "value": "https://docs.google.com/spreadsheets/d/VASA_TABULKA/edit"
        },
        "sheetName": {
          "__rl": true,
          "mode": "name",
          "value": "Platby"
        },
        "columns": {
          "mappingMode": "defineBelow",
          "value": {
            "Dátum": "={{ $('Overiť podpis').item.json.createdAt }}",
            "Rezervácia": "={{ $json.data.reservation.number }}",
            "Suma": "={{ $json.data.payment.amount }}",
            "Mena": "={{ $json.data.payment.currency }}",
            "Druh": "={{ $json.data.payment.kind === 'deposit' ? 'záloha' : 'doplatok' }}",
            "ID platby": "={{ $json.data.payment.id }}",
            "Režim": "={{ $json.mode }}"
          },
          "matchingColumns": [
            "ID platby"
          ],
          "schema": []
        },
        "options": {}
      },
      "id": "00000000-0000-0000-0000-00000000000e",
      "name": "Platba do knihy platieb",
      "type": "n8n-nodes-base.googleSheets",
      "typeVersion": 4.5,
      "position": [
        660,
        -100
      ],
      "notes": "Hárok Platby: Dátum, Rezervácia, Suma, Mena, Druh, ID platby, Režim. Účtovníčka si z neho páruje úhrady."
    },
    {
      "parameters": {
        "fromEmail": "fakturacia@vasa-firma.sk",
        "toEmail": "uctovnictvo@vasa-firma.sk",
        "subject": "=Faktúra {{ $json.data.invoice.number }} z Fleetixu",
        "emailFormat": "text",
        "text": "=Vo Fleetixe vznikla faktúra {{ $json.data.invoice.number }} na {{ $json.data.invoice.total }} {{ $json.data.invoice.currency }}.\nFaktúra vo Fleetixe: {{ $json.data.invoice.appUrl }}\nExport do účtovného programu (POHODA, Money S3, ISDOC) nájdete vo Fleetixe vo Financiách.",
        "options": {}
      },
      "id": "00000000-0000-0000-0000-00000000000f",
      "name": "E-mail účtovníčke",
      "type": "n8n-nodes-base.emailSend",
      "typeVersion": 2.1,
      "position": [
        660,
        100
      ],
      "notes": "Polia faktúry podľa udalosti invoice.issued. Ak ich váš účtovný systém chce priamo, nahraďte tento uzol uzlom HTTP Request na jeho API."
    }
  ],
  "connections": {
    "Webhook z Fleetixu": {
      "main": [
        [
          {
            "node": "Overiť podpis",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Overiť podpis": {
      "main": [
        [
          {
            "node": "Podľa udalosti",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Podľa udalosti": {
      "main": [
        [
          {
            "node": "Platba do knihy platieb",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "E-mail účtovníčke",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "settings": {
    "executionOrder": "v1"
  },
  "meta": {
    "templateCredsSetupCompleted": false,
    "source": "https://js.fleetix.eu/sablony/"
  },
  "tags": []
}
