{
  "name": "Auto-Archive Stale Deals — HubSpot → Slack",
  "nodes": [
    {
      "parameters": { "rule": { "interval": [{ "triggerAtHour": 7 }] } },
      "id": "schedule", "name": "Daily 7am",
      "type": "n8n-nodes-base.scheduleTrigger", "typeVersion": 1.2, "position": [0, 0]
    },
    {
      "parameters": {
        "method": "POST", "url": "https://api.hubapi.com/crm/v3/objects/deals/search",
        "authentication": "predefinedCredentialType", "nodeCredentialType": "hubspotApi",
        "sendBody": true, "specifyBody": "json",
        "jsonBody": "={\n  \"filterGroups\": [\n    {\n      \"filters\": [\n        { \"propertyName\": \"hs_lastmodifieddate\", \"operator\": \"LT\", \"value\": \"\" + $now.minus({days: 60}).toMillis() },\n        { \"propertyName\": \"dealstage\", \"operator\": \"NOT_IN\", \"values\": [\"closedwon\", \"closedlost\"] }\n      ]\n    }\n  ],\n  \"properties\": [\"dealname\", \"amount\", \"dealstage\", \"hubspot_owner_id\", \"hs_lastmodifieddate\"],\n  \"sorts\": [{ \"propertyName\": \"hs_lastmodifieddate\", \"direction\": \"ASCENDING\" }],\n  \"limit\": 100\n}",
        "options": {}
      },
      "id": "search-stale", "name": "Search Stale Deals",
      "type": "n8n-nodes-base.httpRequest", "typeVersion": 4.2, "position": [220, 0],
      "credentials": { "hubspotApi": { "id": "credential-id", "name": "HubSpot API" } }
    },
    {
      "parameters": { "conditions": { "conditions": [{ "leftValue": "={{ $json.total }}", "rightValue": 0, "operator": { "type": "number", "operation": "gt" } }], "combinator": "and" } },
      "id": "has-results", "name": "Any Stale Deals?",
      "type": "n8n-nodes-base.if", "typeVersion": 2, "position": [440, 0]
    },
    {
      "parameters": { "fieldToSplitOut": "results" },
      "id": "split", "name": "Split Out",
      "type": "n8n-nodes-base.splitOut", "typeVersion": 1, "position": [660, 0]
    },
    {
      "parameters": {
        "method": "GET",
        "url": "=https://api.hubapi.com/crm/v3/owners/{{ $json.properties.hubspot_owner_id }}",
        "authentication": "predefinedCredentialType", "nodeCredentialType": "hubspotApi",
        "options": {}
      },
      "id": "fetch-owner", "name": "Fetch Owner",
      "type": "n8n-nodes-base.httpRequest", "typeVersion": 4.2, "position": [880, 0],
      "credentials": { "hubspotApi": { "id": "credential-id", "name": "HubSpot API" } }
    },
    {
      "parameters": {
        "select": "channel", "channelId": { "__rl": true, "value": "sales-pipeline", "mode": "name" },
        "messageType": "block",
        "blocksUi": "={\n  \"blocks\": [\n    {\n      \"type\": \"section\",\n      \"text\": {\n        \"type\": \"mrkdwn\",\n        \"text\": \"⚠️ *Stale Deal Warning*\\n\\n*\" + $('Split Out').item.json.properties.dealname + \"* has had no activity for \" + Math.round((Date.now() - new Date($('Split Out').item.json.properties.hs_lastmodifieddate)) / 86400000) + \" days.\\n\\nThis deal will be moved to *Closed Lost* in 48 hours unless you update it.\\n\\n<https://app.hubspot.com/contacts/YOUR_PORTAL_ID/deal/\" + $('Split Out').item.json.id + \"|View deal in HubSpot>\"\n      }\n    }\n  ]\n}",
        "otherOptions": { "unfurl_links": false }
      },
      "id": "warn-slack", "name": "Warn in Slack",
      "type": "n8n-nodes-base.slack", "typeVersion": 2.3, "position": [1100, 0],
      "credentials": { "slackApi": { "id": "credential-id", "name": "Slack API" } }
    },
    {
      "parameters": { "amount": 48, "unit": "hours" },
      "id": "wait", "name": "Wait 48 Hours",
      "type": "n8n-nodes-base.wait", "typeVersion": 1.1, "position": [1320, 0]
    },
    {
      "parameters": {
        "method": "GET",
        "url": "=https://api.hubapi.com/crm/v3/objects/deals/{{ $('Split Out').item.json.id }}",
        "authentication": "predefinedCredentialType", "nodeCredentialType": "hubspotApi",
        "sendQuery": true,
        "queryParameters": { "parameters": [{ "name": "properties", "value": "hs_lastmodifieddate,dealstage" }] },
        "options": {}
      },
      "id": "recheck", "name": "Re-Check Deal",
      "type": "n8n-nodes-base.httpRequest", "typeVersion": 4.2, "position": [1540, 0],
      "credentials": { "hubspotApi": { "id": "credential-id", "name": "HubSpot API" } }
    },
    {
      "parameters": { "conditions": { "conditions": [{ "leftValue": "={{ new Date($json.properties.hs_lastmodifieddate) > $now.minus({hours: 48}).toJSDate() }}", "rightValue": true, "operator": { "type": "boolean", "operation": "false" } }], "combinator": "and" } },
      "id": "still-stale", "name": "Still Stale?",
      "type": "n8n-nodes-base.if", "typeVersion": 2, "position": [1760, 0]
    },
    {
      "parameters": {
        "method": "PATCH",
        "url": "=https://api.hubapi.com/crm/v3/objects/deals/{{ $('Split Out').item.json.id }}",
        "authentication": "predefinedCredentialType", "nodeCredentialType": "hubspotApi",
        "sendBody": true, "specifyBody": "json",
        "jsonBody": "{ \"properties\": { \"dealstage\": \"closedlost\", \"closed_lost_reason\": \"Stale — auto-archived after 60 days\" } }",
        "options": {}
      },
      "id": "close-deal", "name": "Close Deal",
      "type": "n8n-nodes-base.httpRequest", "typeVersion": 4.2, "position": [1980, 0],
      "credentials": { "hubspotApi": { "id": "credential-id", "name": "HubSpot API" } }
    },
    {
      "parameters": {
        "select": "channel", "channelId": { "__rl": true, "value": "sales-pipeline", "mode": "name" },
        "messageType": "block",
        "blocksUi": "={\n  \"blocks\": [\n    {\n      \"type\": \"section\",\n      \"text\": {\n        \"type\": \"mrkdwn\",\n        \"text\": \"🗄️ *Deal Auto-Archived*\\n*\" + $('Split Out').item.json.properties.dealname + \"* was moved to Closed Lost after 60+ days of inactivity.\"\n      }\n    }\n  ]\n}",
        "otherOptions": { "unfurl_links": false }
      },
      "id": "confirm-slack", "name": "Confirm in Slack",
      "type": "n8n-nodes-base.slack", "typeVersion": 2.3, "position": [2200, 0],
      "credentials": { "slackApi": { "id": "credential-id", "name": "Slack API" } }
    }
  ],
  "connections": {
    "Daily 7am": { "main": [[{ "node": "Search Stale Deals", "type": "main", "index": 0 }]] },
    "Search Stale Deals": { "main": [[{ "node": "Any Stale Deals?", "type": "main", "index": 0 }]] },
    "Any Stale Deals?": { "main": [[{ "node": "Split Out", "type": "main", "index": 0 }]] },
    "Split Out": { "main": [[{ "node": "Fetch Owner", "type": "main", "index": 0 }]] },
    "Fetch Owner": { "main": [[{ "node": "Warn in Slack", "type": "main", "index": 0 }]] },
    "Warn in Slack": { "main": [[{ "node": "Wait 48 Hours", "type": "main", "index": 0 }]] },
    "Wait 48 Hours": { "main": [[{ "node": "Re-Check Deal", "type": "main", "index": 0 }]] },
    "Re-Check Deal": { "main": [[{ "node": "Still Stale?", "type": "main", "index": 0 }]] },
    "Still Stale?": { "main": [[{ "node": "Close Deal", "type": "main", "index": 0 }]] },
    "Close Deal": { "main": [[{ "node": "Confirm in Slack", "type": "main", "index": 0 }]] }
  },
  "pinData": {}, "settings": { "executionOrder": "v1" },
  "staticData": null, "tags": [], "triggerCount": 0, "active": false,
  "meta": { "instanceId": "", "templateId": "revi-archive-stale" }
}
