{
  "name": "Practice routing a video to its editor",
  "nodes": [
    {
      "name": "Run sample",
      "id": "6dbbc263-f21d-4bab-8c55-a7305bd8db50",
      "type": "n8n-nodes-base.manualTrigger",
      "typeVersion": 1,
      "position": [
        0,
        0
      ],
      "parameters": {}
    },
    {
      "name": "Load two briefs and four events",
      "id": "6dbbc263-f21d-4bab-8c55-a7305bd8db51",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        280,
        0
      ],
      "parameters": {
        "jsCode": "// Sample inputs only. Manual run; no accounts, network calls or external writes.\nreturn [{ json: {\n  briefs: [\n    { id: \"BR-41\", reviewer: \"Maya\", question: \"Does the new role picker match the release?\", job: \"vid_roles\", state: \"Rendering\" },\n    { id: \"BR-42\", reviewer: \"Ari\", question: \"Is the export column named Due date?\", job: \"vid_export\", state: \"Rendering\" }\n  ],\n  events: [\n    { id: \"vid_other\", status: \"completed\", video_url: \"https://example.org/unrelated\" },\n    { id: \"vid_roles\", status: \"completed\", video_url: \"https://example.org/role-picker\" },\n    { id: \"vid_roles\", status: \"completed\", video_url: \"https://example.org/role-picker\" },\n    { id: \"vid_export\", status: \"failed\", error_message: \"Source unavailable\" }\n  ]\n} }];"
      }
    },
    {
      "name": "Match the job to its brief",
      "id": "6dbbc263-f21d-4bab-8c55-a7305bd8db52",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        560,
        0
      ],
      "parameters": {
        "jsCode": "// These sample fields match the flattened ngram node output, not a raw webhook body.\n// One manual execution; use a persistent conditional write for real retries across executions.\nconst { briefs, events } = $input.first().json;\nreturn events.map(event => {\n  const row = briefs.find(row => row.job === event.id);\n  const before = row?.state ?? null;\n  let decision;\n  if (!row) decision = \"Ignore: no matching brief\";\n  else if (row.state !== \"Rendering\") decision = \"No change: already handled\";\n  else if (event.status === \"failed\") {\n    row.state = \"Needs owner\"; row.reason = event.error_message;\n    decision = \"Send failure to the workflow owner\";\n  } else if (event.status === \"completed\" && event.video_url) {\n    row.state = \"Ready for review\"; row.url = event.video_url;\n    decision = \"Give the editor the video and review question\";\n  } else decision = \"Wait: no usable result\";\n  return { json: { job: event.id, brief: row?.id ?? null, before, after: row?.state ?? null,\n    reviewer: row?.state === \"Ready for review\" ? row.reviewer : null,\n    question: row?.state === \"Ready for review\" ? row.question : null,\n    video: row?.url ?? null, reason: row?.reason ?? null, decision } };\n});"
      }
    }
  ],
  "connections": {
    "Run sample": {
      "main": [
        [
          {
            "node": "Load two briefs and four events",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Load two briefs and four events": {
      "main": [
        [
          {
            "node": "Match the job to its brief",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "active": false,
  "settings": {
    "executionOrder": "v1"
  }
}
