{
  "name": "Weekly Expense Summary to Slack (template)",
  "nodes": [
    {
      "parameters": {
        "rule": {
          "interval": [
            {
              "field": "cronExpression",
              "expression": "0 9 * * 1"
            }
          ]
        }
      },
      "id": "a1b2c3d4-0000-4000-8000-000000000028",
      "name": "Every Monday 09:00",
      "type": "n8n-nodes-base.scheduleTrigger",
      "typeVersion": 1.2,
      "position": [
        0,
        300
      ]
    },
    {
      "parameters": {
        "jsCode": "// Paste your Slack channel ID, then activate. Runs every Monday at 09:00 in the workflow's timezone.\nreturn [{ json: {\n  slackChannelId: 'PASTE_CHANNEL_ID', // in Slack: open the channel > channel name > About > Channel ID\n  sheetName: 'Expense Log',           // the sheet the invoice reader created\n  currency: 'Rp',\n} }];"
      },
      "id": "a1b2c3d4-0000-4000-8000-000000000029",
      "name": "Config",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        220,
        300
      ]
    },
    {
      "parameters": {
        "authentication": "predefinedCredentialType",
        "nodeCredentialType": "googleDriveOAuth2Api",
        "method": "GET",
        "url": "https://www.googleapis.com/drive/v3/files",
        "sendQuery": true,
        "queryParameters": {
          "parameters": [
            {
              "name": "q",
              "value": "={{ \"name = '\" + $json.sheetName + \"' and mimeType = 'application/vnd.google-apps.spreadsheet' and trashed = false\" }}"
            },
            {
              "name": "fields",
              "value": "files(id,name)"
            },
            {
              "name": "supportsAllDrives",
              "value": "true"
            },
            {
              "name": "includeItemsFromAllDrives",
              "value": "true"
            }
          ]
        },
        "options": {}
      },
      "id": "a1b2c3d4-0000-4000-8000-000000000030",
      "name": "Find Expense Log",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        440,
        300
      ],
      "retryOnFail": true,
      "maxTries": 3,
      "waitBetweenTries": 3000
    },
    {
      "parameters": {
        "jsCode": "const cfg = $('Config').first().json;\nconst f = ($input.first().json.files || [])[0];\nif (!f) throw new Error('No sheet named \"' + cfg.sheetName + '\" found. Run the invoice reader once first, or fix sheetName in Config.');\nreturn [{ json: { sheetId: f.id } }];"
      },
      "id": "a1b2c3d4-0000-4000-8000-000000000031",
      "name": "Pick Sheet",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        660,
        300
      ]
    },
    {
      "parameters": {
        "authentication": "predefinedCredentialType",
        "nodeCredentialType": "googleSheetsOAuth2Api",
        "url": "=https://sheets.googleapis.com/v4/spreadsheets/{{ $json.sheetId }}/values/Invoices!A:N",
        "options": {}
      },
      "id": "a1b2c3d4-0000-4000-8000-000000000032",
      "name": "Read Invoices",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        880,
        300
      ],
      "retryOnFail": true,
      "maxTries": 3,
      "waitBetweenTries": 3000
    },
    {
      "parameters": {
        "jsCode": "// Last week (Monday to Sunday) from the Invoices tab: total, by category, top vendors, and what needs review.\nconst cfg = $('Config').first().json;\nconst [head, ...rows] = $input.first().json.values || [[]];\nconst col = n => head.indexOf(n);\nconst today = new Date(); const dow = (today.getDay() + 6) % 7;\nconst start = new Date(today); start.setDate(today.getDate() - dow - 7); start.setHours(0, 0, 0, 0);\nconst end = new Date(start); end.setDate(start.getDate() + 7);\nconst week = rows.filter(r => { const d = new Date(r[col('Expense Date')]); return d >= start && d < end; });\nconst ok = week.filter(r => r[col('Status')] === 'OK');\nconst review = week.filter(r => r[col('Status')] !== 'OK');\nconst num = v => Number(String(v).replace(/[^0-9.-]/g, '')) || 0;\nconst money = v => cfg.currency + ' ' + Math.round(v).toLocaleString('id-ID');\nconst sumBy = key => Object.entries(ok.reduce((m, r) => (m[r[col(key)] || 'Other'] = (m[r[col(key)] || 'Other'] || 0) + num(r[col('Total')]), m), {})).sort((a, b) => b[1] - a[1]);\nconst total = ok.reduce((s, r) => s + num(r[col('Total')]), 0);\nconst fmt = d => d.toLocaleDateString('en-GB', { day: 'numeric', month: 'short' });\nconst last = new Date(end); last.setDate(end.getDate() - 1);\nconst lines = [\n  '*Expenses, ' + fmt(start) + ' to ' + fmt(last) + '*',\n  money(total) + ' across ' + ok.length + ' receipts' + (review.length ? ', plus ' + review.length + ' waiting for review' : ''),\n  '', '*By category*', ...sumBy('Category').slice(0, 6).map(([k, v]) => '- ' + k + ': ' + money(v)),\n  '', '*Top vendors*', ...sumBy('Vendor').slice(0, 3).map(([k, v]) => '- ' + k + ': ' + money(v)),\n  ...(review.length ? ['', '*Needs review*', ...review.map(r => '- ' + (r[col('Vendor')] || r[col('File Name')]) + ': ' + r[col('Review Reason')])] : []),\n];\nreturn [{ json: { text: lines.join('\\n') } }];"
      },
      "id": "a1b2c3d4-0000-4000-8000-000000000033",
      "name": "Build Summary",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        1100,
        300
      ]
    },
    {
      "parameters": {
        "select": "channel",
        "channelId": {
          "__rl": true,
          "value": "={{ $('Config').first().json.slackChannelId }}",
          "mode": "id"
        },
        "text": "={{ $json.text }}",
        "otherOptions": {
          "includeLinkToWorkflow": false,
          "mrkdwn": true,
          "unfurl_links": false
        }
      },
      "id": "a1b2c3d4-0000-4000-8000-000000000034",
      "name": "Post to Slack",
      "type": "n8n-nodes-base.slack",
      "typeVersion": 2.4,
      "position": [
        1320,
        300
      ]
    },
    {
      "parameters": {
        "content": "## Weekly expense summary\nEvery Monday 09:00: reads last week from the **Invoices** tab and posts totals by category, top vendors and anything waiting for review to Slack.\n\n**Setup**: connect Google Drive OAuth2, Google Sheets OAuth2 and Slack. Paste the Slack channel ID into **Config** and invite your Slack app to that channel. Execute once to test, then turn on **Active**.",
        "height": 260,
        "width": 460
      },
      "id": "a1b2c3d4-0000-4000-8000-000000000035",
      "name": "How it works",
      "type": "n8n-nodes-base.stickyNote",
      "typeVersion": 1,
      "position": [
        0,
        -40
      ]
    }
  ],
  "connections": {
    "Every Monday 09:00": {
      "main": [
        [
          {
            "node": "Config",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Config": {
      "main": [
        [
          {
            "node": "Find Expense Log",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Find Expense Log": {
      "main": [
        [
          {
            "node": "Pick Sheet",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Pick Sheet": {
      "main": [
        [
          {
            "node": "Read Invoices",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Read Invoices": {
      "main": [
        [
          {
            "node": "Build Summary",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Build Summary": {
      "main": [
        [
          {
            "node": "Post to Slack",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "settings": {
    "executionOrder": "v1"
  },
  "pinData": {}
}