{
  "name": "Route webhook by type",
  "nodes": [
    {
      "id": "b94a36cc-a233-4532-b965-8c357c2614e6",
      "name": "On events",
      "webhookId": "f678dd52-f800-445c-b694-02ea32aa75cd",
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 2.2,
      "position": [
        -880,
        0
      ],
      "parameters": {
        "httpMethod": "POST",
        "path": "events",
        "responseMode": "lastNode",
        "responseData": "firstEntryJson",
        "options": {}
      }
    },
    {
      "id": "f9d2ac13-5f3d-4382-83f8-b836e3c09224",
      "name": "Route by type",
      "type": "n8n-nodes-base.if",
      "typeVersion": 2.2,
      "position": [
        -640,
        0
      ],
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "leftValue": "",
            "typeValidation": "loose",
            "version": 2
          },
          "conditions": [
            {
              "id": "e9309fd5-e11c-4c92-ac9f-7f9a00c6aea4",
              "leftValue": "={{ $json.body.type }}",
              "rightValue": "purchase",
              "operator": {
                "type": "string",
                "operation": "equals",
                "name": "filter.operator.string.equals"
              }
            }
          ],
          "combinator": "and"
        },
        "options": {
          "ignoreCase": false
        }
      }
    },
    {
      "id": "f040cf8b-7221-41ba-9f04-fcbce18420dd",
      "name": "Pick purchase fields",
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.5,
      "position": [
        -400,
        -160
      ],
      "parameters": {
        "mode": "manual",
        "assignments": {
          "assignments": [
            {
              "id": "6e38f9b5-3c28-435c-adf1-91f3bd6cf306",
              "name": "type",
              "type": "string",
              "value": "={{ $json.body.type }}"
            },
            {
              "id": "28175efa-18ac-4836-b72e-f04cfff1599c",
              "name": "item",
              "type": "string",
              "value": "={{ $json.body.item }}"
            },
            {
              "id": "90da5678-3b14-4b0c-a931-9cd598bb281b",
              "name": "amount",
              "type": "number",
              "value": "={{ $json.body.amount }}"
            },
            {
              "id": "e47388cc-b402-4e9e-8df1-3b04a64f0f59",
              "name": "email",
              "type": "string",
              "value": "={{ $json.body.email }}"
            }
          ]
        },
        "includeOtherFields": false,
        "options": {}
      }
    },
    {
      "id": "2ac03537-dc46-417a-b755-d6f866dca692",
      "name": "Build purchase line",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        -160,
        -160
      ],
      "parameters": {
        "mode": "runOnceForAllItems",
        "jsCode": "// One CSV line per item, quoted where needed, ending with a newline.\n// Convert to File's CSV mode adds a byte-order mark per chunk and no trailing\n// newline, so a hand-built line is the reliable way to append rows.\nconst esc = (v) => {\n  const s = String(v ?? '');\n  return /[\",\\n]/.test(s) ? '\"' + s.replace(/\"/g, '\"\"') + '\"' : s;\n};\nconst lines = $input.all().map(({ json }) =>\n  [json.type, json.item, json.amount, json.email].map(esc).join(','),\n);\nreturn [{ json: { line: lines.join('\\n') + '\\n' } }];"
      }
    },
    {
      "id": "2f276bdc-16cb-457f-9943-ef334c425045",
      "name": "To text file (purchases)",
      "type": "n8n-nodes-base.convertToFile",
      "typeVersion": 1.2,
      "position": [
        80,
        -160
      ],
      "parameters": {
        "operation": "toText",
        "sourceProperty": "line",
        "binaryPropertyName": "data",
        "options": {
          "addBOM": false,
          "fileName": "events-purchases.csv"
        }
      }
    },
    {
      "id": "a9726c39-9471-4e6a-a73f-717cf8eba578",
      "name": "Append purchases",
      "type": "n8n-nodes-base.readWriteFile",
      "typeVersion": 1.1,
      "position": [
        320,
        -160
      ],
      "parameters": {
        "operation": "write",
        "fileName": "/home/hatch/.n8n-files/events-purchases.csv",
        "dataPropertyName": "data",
        "options": {
          "append": true
        }
      }
    },
    {
      "id": "e4e4e879-b755-4300-9612-79b35edb26d5",
      "name": "Pick signup fields",
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.5,
      "position": [
        -400,
        160
      ],
      "parameters": {
        "mode": "manual",
        "assignments": {
          "assignments": [
            {
              "id": "8d12dfc6-e093-4964-9666-2d88ef69e869",
              "name": "type",
              "type": "string",
              "value": "={{ $json.body.type }}"
            },
            {
              "id": "06414691-53fd-4045-982c-e0a77fe5ecfa",
              "name": "name",
              "type": "string",
              "value": "={{ $json.body.name }}"
            },
            {
              "id": "b6cc976c-cb49-4a6c-9500-c79b5ef26cfd",
              "name": "email",
              "type": "string",
              "value": "={{ $json.body.email }}"
            }
          ]
        },
        "includeOtherFields": false,
        "options": {}
      }
    },
    {
      "id": "301ba84a-be37-489d-b86f-9ec23ad497f0",
      "name": "Build signup line",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        -160,
        160
      ],
      "parameters": {
        "mode": "runOnceForAllItems",
        "jsCode": "// One CSV line per item, quoted where needed, ending with a newline.\n// Convert to File's CSV mode adds a byte-order mark per chunk and no trailing\n// newline, so a hand-built line is the reliable way to append rows.\nconst esc = (v) => {\n  const s = String(v ?? '');\n  return /[\",\\n]/.test(s) ? '\"' + s.replace(/\"/g, '\"\"') + '\"' : s;\n};\nconst lines = $input.all().map(({ json }) =>\n  [json.type, json.name, json.email].map(esc).join(','),\n);\nreturn [{ json: { line: lines.join('\\n') + '\\n' } }];"
      }
    },
    {
      "id": "b166c2cc-d51a-434d-ac11-6f253682bd43",
      "name": "To text file (signups)",
      "type": "n8n-nodes-base.convertToFile",
      "typeVersion": 1.2,
      "position": [
        80,
        160
      ],
      "parameters": {
        "operation": "toText",
        "sourceProperty": "line",
        "binaryPropertyName": "data",
        "options": {
          "addBOM": false,
          "fileName": "events-signups.csv"
        }
      }
    },
    {
      "id": "a362bca3-d791-439b-b908-4c89b17f1b55",
      "name": "Append signups",
      "type": "n8n-nodes-base.readWriteFile",
      "typeVersion": 1.1,
      "position": [
        320,
        160
      ],
      "parameters": {
        "operation": "write",
        "fileName": "/home/hatch/.n8n-files/events-signups.csv",
        "dataPropertyName": "data",
        "options": {
          "append": true
        }
      }
    },
    {
      "id": "6b5ce254-1af6-4c75-95e5-064a963c493d",
      "name": "Reply",
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.5,
      "position": [
        560,
        0
      ],
      "parameters": {
        "mode": "manual",
        "assignments": {
          "assignments": [
            {
              "id": "9cd9bf15-9673-4cd9-9451-b1f4d8607349",
              "name": "status",
              "type": "string",
              "value": "saved"
            },
            {
              "id": "73f6fc98-d39a-4fd9-8c3b-b77a17a25691",
              "name": "type",
              "type": "string",
              "value": "={{ $('On events').first().json.body.type }}"
            },
            {
              "id": "c466cb53-02a2-44c8-8b80-73a21d7366aa",
              "name": "file",
              "type": "string",
              "value": "={{ $('On events').first().json.body.type === 'purchase' ? 'events-purchases.csv' : 'events-signups.csv' }}"
            },
            {
              "id": "f4a6970c-d320-454b-9ffd-5671ee5773f9",
              "name": "email",
              "type": "string",
              "value": "={{ $('On events').first().json.body.email }}"
            }
          ]
        },
        "includeOtherFields": false,
        "options": {}
      }
    }
  ],
  "connections": {
    "On events": {
      "main": [
        [
          {
            "node": "Route by type",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Route by type": {
      "main": [
        [
          {
            "node": "Pick purchase fields",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Pick signup fields",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Pick purchase fields": {
      "main": [
        [
          {
            "node": "Build purchase line",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Build purchase line": {
      "main": [
        [
          {
            "node": "To text file (purchases)",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "To text file (purchases)": {
      "main": [
        [
          {
            "node": "Append purchases",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Append purchases": {
      "main": [
        [
          {
            "node": "Reply",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Pick signup fields": {
      "main": [
        [
          {
            "node": "Build signup line",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Build signup line": {
      "main": [
        [
          {
            "node": "To text file (signups)",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "To text file (signups)": {
      "main": [
        [
          {
            "node": "Append signups",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Append signups": {
      "main": [
        [
          {
            "node": "Reply",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "active": false,
  "settings": {
    "executionOrder": "v1"
  },
  "pinData": null
}
