{
  "name": "Executive report generator",
  "nodes": [
    {
      "parameters": {
        "rule": {
          "interval": [
            {
              "field": "cronExpression",
              "expression": "30 7 * * 1"
            }
          ]
        }
      },
      "id": "2ef2ce6a-5deb-4410-a008-3362df4ace80",
      "name": "Every Monday 07:30",
      "type": "n8n-nodes-base.scheduleTrigger",
      "typeVersion": 1.2,
      "position": [
        240,
        300
      ]
    },
    {
      "parameters": {
        "operation": "executeQuery",
        "query": "SELECT kpi, week, value FROM kpi_weekly WHERE week >= date_trunc('week', now()) - interval '7 days' ORDER BY kpi, week;",
        "options": {}
      },
      "id": "d2349be8-e912-4855-aa13-e987a25c8942",
      "name": "Pull weekly KPIs",
      "type": "n8n-nodes-base.postgres",
      "typeVersion": 2.5,
      "position": [
        512,
        300
      ],
      "credentials": {
        "postgres": {
          "id": "REPLACE_ME",
          "name": "Postgres (self-hosted)"
        }
      }
    },
    {
      "parameters": {
        "jsCode": "const by = {};\nfor (const r of $input.all()) { (by[r.json.kpi] = by[r.json.kpi] || []).push(r.json); }\nconst rows = Object.entries(by).map(([kpi, v]) => {\n  const [prev, cur] = v.sort((a, b) => new Date(a.week) - new Date(b.week));\n  const delta = cur && prev ? cur.value - prev.value : null;\n  return { kpi, prev: prev?.value ?? null, cur: cur?.value ?? null, delta, pct: delta !== null && prev?.value ? (delta / prev.value) * 100 : null };\n});\nreturn [{ json: { rows } }];"
      },
      "id": "b3449f8d-d999-4945-a080-90f6a713cc0f",
      "name": "Compute deltas",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        784,
        300
      ]
    },
    {
      "parameters": {
        "promptType": "define",
        "text": "=Write the weekly leadership brief from this table only. Use exactly three headings: Headline, What moved, What to watch. Do not introduce any number that is not in the table. If a value is null, say the data is missing.\n\n{{ JSON.stringify($json.rows) }}"
      },
      "id": "3de39712-e6bc-4332-aee8-844f8f7fd498",
      "name": "Write the brief",
      "type": "@n8n/n8n-nodes-langchain.chainLlm",
      "typeVersion": 1.5,
      "position": [
        1056,
        300
      ]
    },
    {
      "parameters": {
        "model": {
          "__rl": true,
          "value": "claude-sonnet-4-5",
          "mode": "list"
        },
        "options": {
          "temperature": 0.2
        }
      },
      "id": "50f23aed-b202-4a07-a4ac-85da4131beda",
      "name": "Claude",
      "type": "@n8n/n8n-nodes-langchain.lmChatAnthropic",
      "typeVersion": 1.3,
      "position": [
        1056,
        534
      ],
      "credentials": {
        "anthropicApi": {
          "id": "REPLACE_ME",
          "name": "Anthropic"
        }
      }
    },
    {
      "parameters": {
        "resource": "databasePage",
        "operation": "create",
        "databaseId": {
          "__rl": true,
          "value": "REPLACE_ME",
          "mode": "id"
        },
        "title": "=Weekly brief {{ $now.toFormat(\"yyyy-LL-dd\") }}",
        "propertiesUi": {
          "propertyValues": []
        }
      },
      "id": "00575094-0af2-498d-a66e-80fd6666c4d7",
      "name": "File in Notion",
      "type": "n8n-nodes-base.notion",
      "typeVersion": 2.2,
      "position": [
        1328,
        300
      ],
      "credentials": {
        "notionApi": {
          "id": "REPLACE_ME",
          "name": "Notion"
        }
      }
    },
    {
      "parameters": {
        "select": "channel",
        "channelId": {
          "__rl": true,
          "value": "leadership",
          "mode": "name"
        },
        "text": "=This week's brief is ready: {{ $json.url }}",
        "otherOptions": {}
      },
      "id": "ab54c577-80ca-4167-a2ce-cb45dd5d0058",
      "name": "Post the link",
      "type": "n8n-nodes-base.slack",
      "typeVersion": 2.3,
      "position": [
        1600,
        300
      ],
      "credentials": {
        "slackApi": {
          "id": "REPLACE_ME",
          "name": "Slack"
        }
      }
    }
  ],
  "connections": {
    "Every Monday 07:30": {
      "main": [
        [
          {
            "node": "Pull weekly KPIs",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Pull weekly KPIs": {
      "main": [
        [
          {
            "node": "Compute deltas",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Compute deltas": {
      "main": [
        [
          {
            "node": "Write the brief",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Write the brief": {
      "main": [
        [
          {
            "node": "File in Notion",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "File in Notion": {
      "main": [
        [
          {
            "node": "Post the link",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Claude": {
      "ai_languageModel": [
        [
          {
            "node": "Write the brief",
            "type": "ai_languageModel",
            "index": 0
          }
        ]
      ]
    }
  },
  "active": false,
  "settings": {
    "executionOrder": "v1"
  },
  "pinData": {},
  "meta": {
    "templateCredsSetupCompleted": false,
    "description": "Monday-morning KPIs, written up and waiting in Notion before anyone asks."
  },
  "tags": []
}