{
  "name": "Daily Digest \u2014 08:00 Europe/Berlin",
  "nodes": [
    {
      "parameters": {
        "rule": {
          "interval": [
            {
              "field": "cronExpression",
              "expression": "0 8 * * *"
            }
          ]
        }
      },
      "id": "cron",
      "name": "Cron 08:00 Europe/Berlin",
      "type": "n8n-nodes-base.scheduleTrigger",
      "typeVersion": 1.2,
      "position": [
        240,
        300
      ],
      "notes": "Runs daily at 08:00 local (container TZ=Europe/Berlin)."
    },
    {
      "parameters": {
        "url": "=https://www.taskade.com/api/v1/workspaces/{{$env.TASKADE_WORKSPACE_ID}}/projects",
        "method": "GET",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "Authorization",
              "value": "=Bearer {{$env.TASKADE_API_KEY}}"
            },
            {
              "name": "Accept",
              "value": "application/json"
            }
          ]
        },
        "options": {
          "response": {
            "response": {
              "responseFormat": "json"
            }
          }
        }
      },
      "id": "list-projects",
      "name": "Taskade: list projects",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        460,
        300
      ]
    },
    {
      "parameters": {
        "jsCode": "// Filter projects that changed within last 24h and summarize.\nconst items = $input.first().json.items || $input.first().json || [];\nconst cutoffMs = Date.now() - 24 * 60 * 60 * 1000;\nconst recent = items.filter(p => {\n  const ts = p.updatedAt || p.updated_at || p.updated || 0;\n  const ms = typeof ts === 'number' ? (ts > 1e12 ? ts : ts * 1000) : Date.parse(ts);\n  return ms >= cutoffMs;\n});\n\nconst lines = [];\nlines.push(`# Daily digest \u2014 ${new Date().toISOString().slice(0,10)}`);\nlines.push('');\nlines.push(`Changed projects in last 24h: ${recent.length}`);\nlines.push('');\nfor (const p of recent.slice(0, 25)) {\n  const name = p.name || p.title || p.id;\n  const id = p.id || '';\n  lines.push(`- ${name} (${id})`);\n}\nif (recent.length === 0) {\n  lines.push('_No project activity in the last 24 hours._');\n}\nreturn [{ json: { markdown: lines.join('\\n'), count: recent.length } }];"
      },
      "id": "format",
      "name": "Format digest markdown",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        680,
        300
      ]
    },
    {
      "parameters": {
        "url": "=https://www.taskade.com/api/v1/projects/{{$env.TASKADE_HQ_PROJECT_ID}}/tasks",
        "method": "POST",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "Authorization",
              "value": "=Bearer {{$env.TASKADE_API_KEY}}"
            },
            {
              "name": "Content-Type",
              "value": "application/json"
            },
            {
              "name": "Accept",
              "value": "application/json"
            }
          ]
        },
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={\n  \"tasks\": [\n    {\n      \"content\": \"{{ ($json.markdown || '').replace(/\\\"/g, '\\\\\\\"').replace(/\\n/g, '\\\\n') }}\",\n      \"placement\": \"afterbegin\"\n    }\n  ]\n}",
        "options": {
          "response": {
            "response": {
              "responseFormat": "json"
            }
          }
        }
      },
      "id": "write-hq",
      "name": "Taskade: write to HQ",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        900,
        300
      ],
      "notes": "Requires TASKADE_HQ_PROJECT_ID in .env \u2014 project where daily digests are posted."
    },
    {
      "parameters": {
        "url": "http://langfuse:3000/api/public/ingestion",
        "method": "POST",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "Authorization",
              "value": "=Basic {{Buffer.from($env.LANGFUSE_PUBLIC_KEY + ':' + $env.LANGFUSE_SECRET_KEY).toString('base64')}}"
            },
            {
              "name": "Content-Type",
              "value": "application/json"
            }
          ]
        },
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={\n  \"batch\": [\n    {\n      \"id\": \"{{$now.toISO()}}-digest\",\n      \"timestamp\": \"{{$now.toISO()}}\",\n      \"type\": \"trace-create\",\n      \"body\": {\n        \"id\": \"daily-digest-{{$now.toMillis()}}\",\n        \"name\": \"daily-digest\",\n        \"metadata\": { \"changedCount\": \"{{$('Format digest markdown').first().json.count}}\" }\n      }\n    }\n  ]\n}",
        "options": {
          "response": {
            "response": {
              "responseFormat": "json",
              "neverError": true
            }
          }
        }
      },
      "id": "langfuse-trace",
      "name": "Langfuse: trace digest",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        1120,
        300
      ],
      "continueOnFail": true
    }
  ],
  "connections": {
    "Cron 08:00 Europe/Berlin": {
      "main": [
        [
          {
            "node": "Taskade: list projects",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Taskade: list projects": {
      "main": [
        [
          {
            "node": "Format digest markdown",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Format digest markdown": {
      "main": [
        [
          {
            "node": "Taskade: write to HQ",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Taskade: write to HQ": {
      "main": [
        [
          {
            "node": "Langfuse: trace digest",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "settings": {
    "executionOrder": "v1",
    "saveExecutionProgress": true,
    "saveDataSuccessExecution": "all",
    "saveDataErrorExecution": "all",
    "timezone": "Europe/Berlin"
  },
  "tags": [
    {
      "name": "taskade"
    },
    {
      "name": "cron"
    },
    {
      "name": "ruflo"
    }
  ]
}