{
    "name": "Beeznez starter: weekly KPI digest",
    "nodes": [
        {
            "parameters": {
                "content": "## Weekly KPI digest\n**Untested starter** from Beeznez Tech. Not run in production.\n\n1. In Google Sheets choose File, Share, Publish to web, pick your sheet, choose CSV, and paste the link into the Fetch node.\n2. First column is the date, one row per week, newest row last, numeric columns after it. Plain CSV only, with no commas inside values.\n3. Set your AI API key and model name, and the audience in the prompt.\n4. Add SMTP credentials and your addresses.\nCode does the arithmetic. The model only writes the wording.\n\nKeep a person in the loop before anything reaches a customer.",
                "height": 320,
                "width": 380
            },
            "id": "f019193d-1ba3-4b5c-a0bb-5c8ab2f7dd73",
            "name": "READ ME",
            "type": "n8n-nodes-base.stickyNote",
            "typeVersion": 1,
            "position": [
                -420,
                -40
            ]
        },
        {
            "parameters": {
                "rule": {
                    "interval": [
                        {
                            "field": "weeks",
                            "weeksInterval": 1,
                            "triggerAtDay": [
                                1
                            ],
                            "triggerAtHour": 8,
                            "triggerAtMinute": 0
                        }
                    ]
                }
            },
            "id": "9026111e-3fbd-4f64-b75e-b6c4a421bed3",
            "name": "Every Monday at 8",
            "type": "n8n-nodes-base.scheduleTrigger",
            "typeVersion": 1.2,
            "position": [
                -20,
                200
            ]
        },
        {
            "parameters": {
                "url": "https://docs.google.com/spreadsheets/d/e/YOUR_SHEET_ID/pub?output=csv",
                "options": {
                    "response": {
                        "response": {
                            "responseFormat": "text",
                            "outputPropertyName": "data"
                        }
                    }
                }
            },
            "id": "940daf79-5429-4795-8245-7718787d5a38",
            "name": "Fetch CSV",
            "type": "n8n-nodes-base.httpRequest",
            "typeVersion": 4.2,
            "position": [
                200,
                200
            ]
        },
        {
            "parameters": {
                "jsCode": "const lines = $input.first().json.data.trim().split(/\\r?\\n/);\nconst header = lines[0].split(',').map(s => s.trim());\nconst rows = lines.slice(1).map(l => l.split(',').map(s => s.trim()));\nif (rows.length < 2) { throw new Error('Need at least two rows: this week and last week.'); }\nconst last = rows[rows.length - 1];\nconst prev = rows[rows.length - 2];\nconst table = [];\nfor (let i = 1; i < header.length; i++) {\n  const a = parseFloat(last[i]);\n  const b = parseFloat(prev[i]);\n  if (Number.isNaN(a) || Number.isNaN(b)) { continue; }\n  const change = Math.round((a - b) * 100) / 100;\n  const pct = b === 0 ? 'percent n/a' : (Math.round(((a - b) / b) * 1000) / 10) + '%';\n  table.push(header[i] + ': ' + a + ' (was ' + b + ', change ' + (change >= 0 ? '+' : '') + change + ', ' + pct + ')');\n}\nreturn [{ json: { period: last[0], previous: prev[0], table: table.join('\\n') } }];"
            },
            "id": "58b9c091-a284-4dfc-9593-eb328d65f36a",
            "name": "Calculate changes (exact arithmetic)",
            "type": "n8n-nodes-base.code",
            "typeVersion": 2,
            "position": [
                420,
                200
            ]
        },
        {
            "parameters": {
                "method": "POST",
                "url": "https://api.openai.com/v1/chat/completions",
                "sendHeaders": true,
                "headerParameters": {
                    "parameters": [
                        {
                            "name": "Authorization",
                            "value": "Bearer YOUR_API_KEY"
                        },
                        {
                            "name": "Content-Type",
                            "value": "application/json"
                        }
                    ]
                },
                "sendBody": true,
                "specifyBody": "json",
                "jsonBody": "={{ JSON.stringify({ model: \"YOUR_MODEL\", temperature: 0, messages: [ { role: \"system\", content: \"You are writing a weekly update for YOUR AUDIENCE. Using only the numbers provided, write five bullets: what changed, by how much (absolute and percent), the likely reason only if the data supports one, and one thing to check next week. If a number looks like a data error, say so instead of explaining it.\" }, { role: \"user\", content: \"Data for \" + $json.period + \" compared with \" + $json.previous + \":\\n\" + $json.table } ] }) }}",
                "options": {
                    "timeout": 60000
                }
            },
            "id": "441ca790-22da-41d7-9f5e-1c12327ee7df",
            "name": "AI: write the weekly summary",
            "type": "n8n-nodes-base.httpRequest",
            "typeVersion": 4.2,
            "position": [
                660,
                200
            ]
        },
        {
            "parameters": {
                "fromEmail": "you@example.com",
                "toEmail": "team@example.com",
                "subject": "=Weekly KPI digest: {{ $(\"Calculate changes (exact arithmetic)\").item.json.period }}",
                "emailFormat": "text",
                "text": "={{ $json.choices[0].message.content }}\n\n---\nNumbers (calculated by code):\n{{ $(\"Calculate changes (exact arithmetic)\").item.json.table }}",
                "options": []
            },
            "id": "3c6bddbf-c022-4bb5-89c9-3bd1b1c98a6e",
            "name": "Email the digest",
            "type": "n8n-nodes-base.emailSend",
            "typeVersion": 2.1,
            "position": [
                900,
                200
            ]
        }
    ],
    "connections": {
        "Every Monday at 8": {
            "main": [
                [
                    {
                        "node": "Fetch CSV",
                        "type": "main",
                        "index": 0
                    }
                ]
            ]
        },
        "Fetch CSV": {
            "main": [
                [
                    {
                        "node": "Calculate changes (exact arithmetic)",
                        "type": "main",
                        "index": 0
                    }
                ]
            ]
        },
        "Calculate changes (exact arithmetic)": {
            "main": [
                [
                    {
                        "node": "AI: write the weekly summary",
                        "type": "main",
                        "index": 0
                    }
                ]
            ]
        },
        "AI: write the weekly summary": {
            "main": [
                [
                    {
                        "node": "Email the digest",
                        "type": "main",
                        "index": 0
                    }
                ]
            ]
        }
    },
    "active": false,
    "settings": {
        "executionOrder": "v1"
    },
    "tags": []
}