{
  "name": "Docspout: weekly report from Airtable",
  "nodes": [
    {
      "parameters": {
        "content": "## Weekly report from Airtable\n1. In Docspout, open a **Weekly report** starter, click **Publish**, and copy its template id (`tpl_...`).\n2. Paste it over `tpl_REPLACE_ME` in the **Render the report** node.\n3. Credentials → New → **Header Auth**: name `Authorization`, value `Bearer sk_test_...` (a test key while you try it; switch to a live key after). Call it `Docspout API key`.\n4. Connect Airtable and Gmail. In **Last week's orders** pick your base and table.\n\nRuns every Monday at 08:00. The email carries a link to the PDF; links last as long as your plan keeps files.",
        "height": 300,
        "width": 560
      },
      "id": "note",
      "name": "Setup",
      "type": "n8n-nodes-base.stickyNote",
      "typeVersion": 1,
      "position": [
        -60,
        -340
      ]
    },
    {
      "parameters": {
        "rule": {
          "interval": [
            {
              "field": "weeks",
              "triggerAtDay": [
                1
              ],
              "triggerAtHour": 8
            }
          ]
        }
      },
      "id": "schedule",
      "name": "Every Monday 08:00",
      "type": "n8n-nodes-base.scheduleTrigger",
      "typeVersion": 1.2,
      "position": [
        0,
        0
      ]
    },
    {
      "parameters": {
        "operation": "search",
        "base": {
          "__rl": true,
          "mode": "url",
          "value": ""
        },
        "table": {
          "__rl": true,
          "mode": "url",
          "value": ""
        },
        "filterByFormula": "IS_AFTER({Date}, DATEADD(TODAY(), -7, 'days'))",
        "options": {}
      },
      "id": "airtable",
      "name": "Last week's orders",
      "type": "n8n-nodes-base.airtable",
      "typeVersion": 2.1,
      "position": [
        220,
        0
      ],
      "credentials": {
        "airtableTokenApi": {
          "id": "",
          "name": "Airtable account"
        }
      }
    },
    {
      "parameters": {
        "jsCode": "// Expects Airtable fields: Date, Product, Amount, Channel. Rename to match your table.\nconst items = $input.all().map((i) => i.json.fields ?? i.json);\nconst sum = (xs) => xs.reduce((a, b) => a + b, 0);\nconst count = (key) => Object.entries(items.reduce((m, r) => ({ ...m, [r[key]]: (m[r[key]] ?? 0) + 1 }), {}))\n  .sort((a, b) => b[1] - a[1]).slice(0, 5).map(([label, n]) => ({ label: String(label), value: String(n) }));\nconst monday = new Date(); monday.setDate(monday.getDate() - ((monday.getDay() + 6) % 7) - 7);\nreturn [{ json: {\n  client: 'Your store',\n  week: monday.toISOString().slice(0, 10),\n  kpis: { revenue: sum(items.map((r) => Number(r.Amount) || 0)), orders: items.length, conversion: 0 },\n  sections: [\n    { title: 'Top products', rows: count('Product') },\n    { title: 'Orders by channel', rows: count('Channel') },\n  ],\n} }];"
      },
      "id": "code",
      "name": "Summarize the week",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        440,
        0
      ]
    },
    {
      "parameters": {
        "method": "POST",
        "url": "https://docspout.com/v1/renders",
        "authentication": "genericCredentialType",
        "genericAuthType": "httpHeaderAuth",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "Idempotency-Key",
              "value": "={{ 'weekly-' + $json.week }}"
            }
          ]
        },
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={{ JSON.stringify({ template: 'tpl_REPLACE_ME', filename: 'weekly-report-' + $json.week, data: $json }) }}",
        "options": {}
      },
      "id": "render-the-report",
      "name": "Render the report",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        660,
        0
      ],
      "credentials": {
        "httpHeaderAuth": {
          "id": "",
          "name": "Docspout API key"
        }
      }
    },
    {
      "parameters": {
        "sendTo": "you@example.com",
        "subject": "=Weekly report {{ $('Summarize the week').item.json.week }}",
        "emailType": "text",
        "message": "=This week's report: {{ $json.url }}",
        "options": {}
      },
      "id": "gmail",
      "name": "Email the link",
      "type": "n8n-nodes-base.gmail",
      "typeVersion": 2.1,
      "position": [
        880,
        0
      ],
      "webhookId": "docspout-gmail-3",
      "credentials": {
        "gmailOAuth2": {
          "id": "",
          "name": "Gmail account"
        }
      }
    }
  ],
  "connections": {
    "Every Monday 08:00": {
      "main": [
        [
          {
            "node": "Last week's orders",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Last week's orders": {
      "main": [
        [
          {
            "node": "Summarize the week",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Summarize the week": {
      "main": [
        [
          {
            "node": "Render the report",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Render the report": {
      "main": [
        [
          {
            "node": "Email the link",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "settings": {
    "executionOrder": "v1"
  },
  "meta": {
    "templateCredsSetupCompleted": false
  },
  "tags": []
}