{
  "name": "40_480_BATCH_TRIGGER",
  "nodes": [
    {
      "parameters": {
        "httpMethod": "POST",
        "path": "batch-trigger",
        "responseMode": "lastNode",
        "options": {}
      },
      "id": "webhook-trigger",
      "name": "Webhook Trigger",
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 2,
      "position": [
        240,
        300
      ]
    },
    {
      "parameters": {
        "jsCode": "// Input: { batch_id }\nconst body = $input.first().json.body ?? $input.first().json;\nif (!body.batch_id) throw new Error('batch_id fehlt im Request');\nreturn [{ json: { batch_id: body.batch_id } }];"
      },
      "id": "validate-input",
      "name": "Input validieren",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        460,
        300
      ]
    },
    {
      "parameters": {
        "method": "GET",
        "url": "https://directus.automation-plus-ki.de/items/400_content_pipeline?filter[batch_id][_eq]={{ $json.batch_id }}&limit=1",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "Authorization",
              "value": "Bearer ={{ $env.DIRECTUS_TOKEN }}"
            }
          ]
        }
      },
      "id": "fetch-batch",
      "name": "Batch Job laden",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        680,
        300
      ]
    },
    {
      "parameters": {
        "jsCode": "// Batch Job aus Response extrahieren + Topics parsen\nconst res   = $input.first().json;\nconst batch = res.data?.[0];\nif (!batch) throw new Error(`Batch Job nicht gefunden: ${$('Input validieren').item.json.batch_id}`);\n\nconst topics = JSON.parse(batch.topics_json ?? '[]');\nconst steps  = JSON.parse(batch.steps_json  ?? '[\"blog\",\"image\",\"voice\"]');\n\nreturn topics.map((topic, idx) => ({ json: {\n  batch_id:   batch.batch_id,\n  batch_db_id: batch.Id,\n  topic,\n  category: batch.category,\n  steps,\n  topic_index: idx,\n  total_count: topics.length\n} }));"
      },
      "id": "parse-topics",
      "name": "Topics aufteilen",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        900,
        300
      ]
    },
    {
      "parameters": {
        "method": "PATCH",
        "url": "https://directus.automation-plus-ki.de/items/400_content_pipeline",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "Authorization",
              "value": "Bearer ={{ $env.DIRECTUS_TOKEN }}"
            }
          ]
        },
        "sendBody": true,
        "contentType": "json",
        "bodyParameters": {
          "parameters": [
            {
              "name": "Id",
              "value": "={{ $json.batch_db_id }}"
            },
            {
              "name": "status",
              "value": "running"
            },
            {
              "name": "started_at",
              "value": "={{ $now.toISO() }}"
            }
          ]
        }
      },
      "id": "mark-running",
      "name": "Batch: running markieren",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        1120,
        300
      ]
    },
    {
      "parameters": {
        "batchSize": 2,
        "options": {}
      },
      "id": "split-batches",
      "name": "2er-Batches",
      "type": "n8n-nodes-base.splitInBatches",
      "typeVersion": 3,
      "position": [
        1340,
        300
      ]
    },
    {
      "parameters": {
        "method": "POST",
        "url": "https://directus.automation-plus-ki.de/items/400_content_pipeline",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "Authorization",
              "value": "Bearer ={{ $env.DIRECTUS_TOKEN }}"
            }
          ]
        },
        "sendBody": true,
        "contentType": "json",
        "bodyParameters": {
          "parameters": [
            {
              "name": "job_id",
              "value": "={{ $now.toMillis() }}-{{ $json.topic_index }}"
            },
            {
              "name": "topic",
              "value": "={{ $json.topic }}"
            },
            {
              "name": "category",
              "value": "={{ $json.category }}"
            },
            {
              "name": "stage",
              "value": "text"
            },
            {
              "name": "status",
              "value": "idle"
            },
            {
              "name": "target_platforms",
              "value": "blog,instagram,linkedin"
            },
            {
              "name": "created_at",
              "value": "={{ $now.toISO() }}"
            }
          ]
        }
      },
      "id": "create-pipeline-job",
      "name": "Pipeline Job anlegen",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        1560,
        300
      ]
    },
    {
      "parameters": {
        "method": "POST",
        "url": "=https://n8n.automation-plus-ki.de/webhook/content-master",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "Content-Type",
              "value": "application/json"
            }
          ]
        },
        "sendBody": true,
        "contentType": "json",
        "bodyParameters": {
          "parameters": [
            {
              "name": "pipeline_job_id",
              "value": "={{ $('Pipeline Job anlegen').item.json.job_id }}"
            },
            {
              "name": "topic",
              "value": "={{ $('2er-Batches').item.json.topic }}"
            },
            {
              "name": "category",
              "value": "={{ $('2er-Batches').item.json.category }}"
            }
          ]
        },
        "options": {
          "timeout": 10000,
          "redirect": {
            "redirect": {
              "followRedirects": true
            }
          }
        }
      },
      "id": "trigger-master-flow",
      "name": "Master Flow triggern",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        1780,
        300
      ]
    },
    {
      "parameters": {
        "method": "PATCH",
        "url": "https://directus.automation-plus-ki.de/items/400_content_pipeline",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "Authorization",
              "value": "Bearer ={{ $env.DIRECTUS_TOKEN }}"
            }
          ]
        },
        "sendBody": true,
        "contentType": "json",
        "bodyParameters": {
          "parameters": [
            {
              "name": "Id",
              "value": "={{ $('Topics aufteilen').item.json.batch_db_id }}"
            },
            {
              "name": "done_count",
              "value": "={{ $('Topics aufteilen').item.json.topic_index + 1 }}"
            }
          ]
        }
      },
      "id": "update-progress",
      "name": "Fortschritt aktualisieren",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        2000,
        300
      ]
    },
    {
      "parameters": {
        "amount": 30,
        "unit": "seconds"
      },
      "id": "rate-limit-pause",
      "name": "30s Pause (Rate Limit)",
      "type": "n8n-nodes-base.wait",
      "typeVersion": 1.1,
      "position": [
        2220,
        300
      ]
    },
    {
      "parameters": {
        "respondWith": "json",
        "responseBody": "={ \"ok\": true, \"batch_id\": \"{{ $('Input validieren').item.json.batch_id }}\", \"message\": \"Batch gestartet\" }"
      },
      "id": "respond-ok",
      "name": "Response OK",
      "type": "n8n-nodes-base.respondToWebhook",
      "typeVersion": 1.1,
      "position": [
        2440,
        300
      ]
    }
  ],
  "connections": {
    "Webhook Trigger": {
      "main": [
        [
          {
            "node": "Input validieren",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Input validieren": {
      "main": [
        [
          {
            "node": "Batch Job laden",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Batch Job laden": {
      "main": [
        [
          {
            "node": "Topics aufteilen",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Topics aufteilen": {
      "main": [
        [
          {
            "node": "Batch: running markieren",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Batch: running markieren": {
      "main": [
        [
          {
            "node": "2er-Batches",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "2er-Batches": {
      "main": [
        [
          {
            "node": "Pipeline Job anlegen",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Pipeline Job anlegen": {
      "main": [
        [
          {
            "node": "Master Flow triggern",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Master Flow triggern": {
      "main": [
        [
          {
            "node": "Fortschritt aktualisieren",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Fortschritt aktualisieren": {
      "main": [
        [
          {
            "node": "30s Pause (Rate Limit)",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "30s Pause (Rate Limit)": {
      "main": [
        [
          {
            "node": "Response OK",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "settings": {
    "executionOrder": "v1",
    "saveManualExecutions": true,
    "callerPolicy": "workflowsFromSameOwner",
    "errorWorkflow": ""
  },
  "staticData": null,
  "meta": {
    "templateCredsSetupCompleted": true
  },
  "versionId": "40_480_BATCH_TRIGGER_v1",
  "tags": [
    {
      "name": "content-pipeline"
    },
    {
      "name": "batch"
    }
  ]
}