{
  "name": "Weekly Pipeline Snapshot — HubSpot → Google Sheets",
  "nodes": [
    {
      "parameters": { "rule": { "interval": [{ "triggerAtHour": 8, "triggerAtDay": 1 }] } },
      "id": "schedule", "name": "Weekly Monday 8am",
      "type": "n8n-nodes-base.scheduleTrigger", "typeVersion": 1.2, "position": [0, 0]
    },
    {
      "parameters": {
        "method": "GET", "url": "https://api.hubapi.com/crm/v3/pipelines/deals",
        "authentication": "predefinedCredentialType", "nodeCredentialType": "hubspotApi", "options": {}
      },
      "id": "fetch-stages", "name": "Fetch Stages",
      "type": "n8n-nodes-base.httpRequest", "typeVersion": 4.2, "position": [220, 0],
      "credentials": { "hubspotApi": { "id": "credential-id", "name": "HubSpot API" } }
    },
    {
      "parameters": {
        "method": "POST", "url": "https://api.hubapi.com/crm/v3/objects/deals/search",
        "authentication": "predefinedCredentialType", "nodeCredentialType": "hubspotApi",
        "sendBody": true, "specifyBody": "json",
        "jsonBody": "{\n  \"filterGroups\": [{ \"filters\": [{ \"propertyName\": \"pipeline\", \"operator\": \"EQ\", \"value\": \"default\" }] }],\n  \"properties\": [\"dealname\", \"amount\", \"dealstage\", \"pipeline\", \"closedate\", \"createdate\"],\n  \"sorts\": [{ \"propertyName\": \"amount\", \"direction\": \"DESCENDING\" }],\n  \"limit\": 100\n}",
        "options": {}
      },
      "id": "fetch-deals", "name": "Fetch Deals",
      "type": "n8n-nodes-base.httpRequest", "typeVersion": 4.2, "position": [440, 0],
      "credentials": { "hubspotApi": { "id": "credential-id", "name": "HubSpot API" } }
    },
    {
      "parameters": {
        "mode": "runOnceForAllItems",
        "jsCode": "const deals = $input.all().map(item => item.json);\nconst results = deals[0]?.results || deals;\nconst pipelineData = $('Fetch Stages').first().json.results;\n\nconst stageMap = {};\nconst stageOrder = [];\nfor (const pipeline of pipelineData) {\n  for (const stage of pipeline.stages) {\n    stageMap[stage.id] = stage.label;\n    stageOrder.push(stage.label);\n  }\n}\n\nlet totalValue = 0;\nconst byStage = {};\n\nfor (const deal of results) {\n  const amount = parseFloat(deal.properties.amount || '0');\n  totalValue += amount;\n  const stageId = deal.properties.dealstage;\n  const stageName = stageMap[stageId] || stageId;\n  if (!byStage[stageName]) byStage[stageName] = { count: 0, value: 0 };\n  byStage[stageName].count += 1;\n  byStage[stageName].value += amount;\n}\n\nconst today = new Date().toISOString().split('T')[0];\nconst row = [today, totalValue, results.length];\nfor (const stageName of stageOrder) {\n  const data = byStage[stageName] || { count: 0, value: 0 };\n  row.push(data.count, data.value);\n}\n\nreturn [{ json: { row, totalValue, dealCount: results.length, byStage } }];"
      },
      "id": "aggregate", "name": "Aggregate Metrics",
      "type": "n8n-nodes-base.code", "typeVersion": 2, "position": [660, 0]
    },
    {
      "parameters": {
        "method": "POST",
        "url": "=https://sheets.googleapis.com/v4/spreadsheets/YOUR_SPREADSHEET_ID/values/Sheet1!A1:append?valueInputOption=USER_ENTERED",
        "authentication": "predefinedCredentialType", "nodeCredentialType": "googleSheetsOAuth2Api",
        "sendBody": true, "specifyBody": "json",
        "jsonBody": "={ \"values\": [{{ $json.row }}] }",
        "options": {}
      },
      "id": "append-sheet", "name": "Append to Sheets",
      "type": "n8n-nodes-base.httpRequest", "typeVersion": 4.2, "position": [880, 0],
      "credentials": { "googleSheetsOAuth2Api": { "id": "credential-id", "name": "Google Sheets OAuth2" } }
    }
  ],
  "connections": {
    "Weekly Monday 8am": { "main": [[{ "node": "Fetch Stages", "type": "main", "index": 0 }]] },
    "Fetch Stages": { "main": [[{ "node": "Fetch Deals", "type": "main", "index": 0 }]] },
    "Fetch Deals": { "main": [[{ "node": "Aggregate Metrics", "type": "main", "index": 0 }]] },
    "Aggregate Metrics": { "main": [[{ "node": "Append to Sheets", "type": "main", "index": 0 }]] }
  },
  "pinData": {}, "settings": { "executionOrder": "v1" },
  "staticData": null, "tags": [], "triggerCount": 0, "active": false,
  "meta": { "instanceId": "", "templateId": "revi-pipeline-snapshot" }
}
