{
  "name": "ezGPT - Save Report to Local Folder",
  "active": false,
  "nodes": [
    {
      "id": "manual",
      "name": "Manual Trigger",
      "type": "n8n-nodes-base.manualTrigger",
      "typeVersion": 1,
      "position": [
        0,
        260
      ],
      "parameters": {}
    },
    {
      "id": "sample-report",
      "name": "Sample Report",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        220,
        260
      ],
      "parameters": {
        "mode": "runOnceForAllItems",
        "jsCode": "return [{ json: { text: \"Учебный отчёт n8n.\\nЗаявок: 3\\nСтатус: проверено\\n\" } }];"
      }
    },
    {
      "id": "check-input",
      "name": "Check Input",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        440,
        260
      ],
      "parameters": {
        "mode": "runOnceForAllItems",
        "jsCode": "const items = $input.all();\nif (items.length !== 1) throw new Error('one_report_required');\nconst row = items[0].json;\nif (Object.keys(row).join('|') !== 'text' || typeof row.text !== 'string' ||\n    !row.text.trim() || !row.text.isWellFormed() ||\n    /[\\u0000-\\u0008\\u000b\\u000c\\u000e-\\u001f\\u007f]/u.test(row.text)) {\n  throw new Error('report_text_invalid');\n}\nconst size = Buffer.byteLength(row.text, 'utf8');\nif (size < 1 || size > 4096) throw new Error('report_size_invalid');\nconst id = String($execution.id);\nif (!/^\\d+$/.test(id)) throw new Error('execution_id_required');\nconst fileName = 'report-' + id + '.txt';\nreturn [{ json: { text: row.text, file_name: fileName,\n  output_path: '/files/outbox/' + fileName, source_bytes: size } }];"
      }
    },
    {
      "id": "make",
      "name": "Make File",
      "type": "n8n-nodes-base.convertToFile",
      "typeVersion": 1.1,
      "position": [
        660,
        260
      ],
      "parameters": {
        "operation": "toText",
        "sourceProperty": "text",
        "binaryPropertyName": "document",
        "options": {
          "fileName": "={{$json.file_name}}",
          "encoding": "utf8",
          "addBOM": false
        }
      }
    },
    {
      "id": "check-binary",
      "name": "Check Binary",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        880,
        260
      ],
      "parameters": {
        "mode": "runOnceForAllItems",
        "jsCode": "const items = $input.all();\nif (items.length !== 1 || Object.keys(items[0].binary ?? {}).join('|') !== 'document') {\n  throw new Error('one_document_required');\n}\nconst item = items[0], source = $('Check Input').first().json;\nconst expectedName = 'report-' + String($execution.id) + '.txt';\nif (source.file_name !== expectedName || source.output_path !== '/files/outbox/' + expectedName ||\n    item.binary.document.fileName !== expectedName) throw new Error('report_path_invalid');\nconst bytes = await this.helpers.getBinaryDataBuffer(0, 'document');\nif (!bytes.equals(Buffer.from(source.text, 'utf8')) || bytes.length !== source.source_bytes) {\n  throw new Error('report_bytes_mismatch');\n}\nreturn [{ json: source, binary: item.binary }];"
      }
    },
    {
      "id": "write",
      "name": "Write Report",
      "type": "n8n-nodes-base.readWriteFile",
      "typeVersion": 1,
      "position": [
        1100,
        260
      ],
      "parameters": {
        "operation": "write",
        "fileName": "={{$json.output_path}}",
        "dataPropertyName": "document",
        "options": {
          "append": false
        }
      }
    },
    {
      "id": "read",
      "name": "Read Saved",
      "type": "n8n-nodes-base.readWriteFile",
      "typeVersion": 1,
      "position": [
        1320,
        260
      ],
      "alwaysOutputData": true,
      "parameters": {
        "operation": "read",
        "fileSelector": "={{ $('Check Binary').first().json.output_path }}",
        "options": {
          "dataPropertyName": "saved"
        }
      }
    },
    {
      "id": "check-saved",
      "name": "Check Saved",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        1540,
        260
      ],
      "parameters": {
        "mode": "runOnceForAllItems",
        "jsCode": "const items = $input.all();\nif (items.length !== 1 || Object.keys(items[0].binary ?? {}).join('|') !== 'saved') {\n  throw new Error('one_saved_file_required');\n}\nconst source = $('Check Binary').first().json;\nif ($('Write Report').first().json.fileName !== source.output_path) throw new Error('written_path_mismatch');\nconst item = items[0];\nif (item.binary.saved.fileName !== source.file_name) throw new Error('saved_name_mismatch');\nconst bytes = await this.helpers.getBinaryDataBuffer(0, 'saved');\nif (!bytes.equals(Buffer.from(source.text, 'utf8')) || bytes.length !== source.source_bytes) {\n  throw new Error('saved_bytes_mismatch');\n}\nreturn [{ json: { file_name: source.file_name, container_path: source.output_path,\n  source_bytes: bytes.length, saved: true, verified: true, external_send: false } }];"
      }
    }
  ],
  "connections": {
    "Manual Trigger": {
      "main": [
        [
          {
            "node": "Sample Report",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Sample Report": {
      "main": [
        [
          {
            "node": "Check Input",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Check Input": {
      "main": [
        [
          {
            "node": "Make File",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Make File": {
      "main": [
        [
          {
            "node": "Check Binary",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Check Binary": {
      "main": [
        [
          {
            "node": "Write Report",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Write Report": {
      "main": [
        [
          {
            "node": "Read Saved",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Read Saved": {
      "main": [
        [
          {
            "node": "Check Saved",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "settings": {
    "executionOrder": "v1"
  },
  "pinData": {}
}
