{
  "name": "killBottleneck — 8D status back to the sheet",
  "nodes": [
    {
      "parameters": {
        "rule": {
          "interval": [
            {
              "field": "minutes",
              "minutesInterval": 10
            }
          ]
        }
      },
      "id": "44444444-4444-4444-8444-444444444444",
      "name": "Every 10 minutes",
      "type": "n8n-nodes-base.scheduleTrigger",
      "typeVersion": 1.2,
      "position": [
        0,
        0
      ]
    },
    {
      "parameters": {
        "url": "https://yourcompany.killbottleneck.com/api/kb/v1/maps/paste_your_map_id_here",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "Authorization",
              "value": "Bearer kb_user_paste_your_key_here"
            }
          ]
        },
        "options": {}
      },
      "id": "55555555-5555-4555-8555-555555555555",
      "name": "Read the map",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        220,
        0
      ]
    },
    {
      "parameters": {
        "jsCode": "// Turns the map into a list of \"card → how far the 8D got\", which then flows into the sheet.\n// ⚙️ FILL IN TWO LINES — the same as in the first workflow:\nconst KB_BRANCH = 'New complaints'; // kanban board: 'D1 – Team formation'\nconst KB_KANBAN = false;            // a kanban board? set true\n\n/** An index of goals: the parent and the row the parent stands in (the columns, on a kanban). */\nfunction index(tree) {\n  const all = {};\n  const walk = (goals, parent, parentRow) => {\n    (goals || []).forEach((u) => {\n      all[u.id] = { goal: u, parent, parentRow };\n      walk(u.children, u, goals);\n    });\n  };\n  walk(tree, null, []);\n  return all;\n}\n\n/** One sentence for the sheet — the column (kanban), or the done subgoals (classic map). */\nfunction progressText(entry) {\n  if (KB_KANBAN) {\n    const columns = entry.parentRow || [];\n    const position = columns.indexOf(entry.parent) + 1;\n    if (!position) return 'not under any column';\n    if (position === columns.length && entry.goal.status === 'done') return '✅ closed (' + position + '/' + columns.length + ')';\n    return entry.parent.title + ' (' + position + '/' + columns.length + ')';\n  }\n  const steps = entry.goal.children || [];\n  if (!steps.length) return entry.goal.status === 'done' ? 'closed' : 'waiting for the 8D to expand';\n  const done = steps.filter((k) => k.status === 'done').length;\n  if (done === steps.length) return '✅ closed (' + done + '/' + steps.length + ')';\n  return steps.filter((k) => k.status !== 'done')[0].title + ' (' + done + '/' + steps.length + ')';\n}\n\nconst map = $input.first().json;\nconst all = index(map.tree);\n\nconst entry = Object.values(all).filter((z) => z.goal.title === KB_BRANCH)[0];\nif (!entry) throw new Error('The map has no \"' + KB_BRANCH + '\" goal.');\n\n// Classic map: cards hang under one goal. Kanban: they are spread across all columns of\n// the row the first one stands in — hence the siblings of the entry node.\nconst columns = KB_KANBAN ? (entry.parent ? entry.parent.children || [] : [entry.goal]) : [entry.goal];\nconst cards = columns.reduce((acc, s) => acc.concat(s.children || []), []);\n\nreturn cards.map((card) => ({\n  json: {\n    killBottleneck: card.id,\n    '8D status': progressText(all[card.id]),\n  },\n}));"
      },
      "id": "66666666-6666-4666-8666-666666666666",
      "name": "Compute the progress",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        440,
        0
      ]
    },
    {
      "parameters": {
        "operation": "update",
        "documentId": {
          "__rl": true,
          "value": "SEM_ID_TABULKY",
          "mode": "id"
        },
        "sheetName": {
          "__rl": true,
          "value": "gid=0",
          "mode": "list",
          "cachedResultName": "Reklamace"
        },
        "columns": {
          "mappingMode": "autoMapInputData",
          "matchingColumns": [
            "killBottleneck"
          ],
          "value": {}
        },
        "options": {}
      },
      "id": "77777777-7777-4777-8777-777777777777",
      "name": "Update rows in the sheet",
      "type": "n8n-nodes-base.googleSheets",
      "typeVersion": 4.5,
      "position": [
        660,
        0
      ],
      "credentials": {
        "googleSheetsOAuth2Api": {
          "id": "SEM_ID_PRIHLASENI",
          "name": "Google Sheets"
        }
      }
    }
  ],
  "connections": {
    "Every 10 minutes": {
      "main": [
        [
          {
            "node": "Read the map",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Read the map": {
      "main": [
        [
          {
            "node": "Compute the progress",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Compute the progress": {
      "main": [
        [
          {
            "node": "Update rows in the sheet",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "settings": {
    "executionOrder": "v1"
  }
}
