{
  "name": "Nightly Data Cleanup Cron",
  "nodes": [
    {
      "parameters": {
        "rule": {
          "interval": [{ "field": "days", "daysInterval": 1, "triggerAtHour": 0, "triggerAtMinute": 30 }]
        }
      },
      "id": "nccr0001-0001-4000-8000-000000000001",
      "name": "Midnight Cleanup Trigger",
      "type": "n8n-nodes-base.scheduleTrigger",
      "typeVersion": 1,
      "position": [240, 300]
    },
    {
      "parameters": {
        "assignments": {
          "assignments": [
            { "id": "c1", "name": "cutoff_date", "value": "={{ $now.minus({ days: 30 }).toISO() }}", "type": "string" },
            { "id": "c2", "name": "run_id", "value": "={{ 'cleanup-' + $now.toFormat('yyyyMMdd') }}", "type": "string" }
          ]
        },
        "options": {}
      },
      "id": "nccr0001-0002-4000-8000-000000000002",
      "name": "Set Cutoff Date",
      "type": "n8n-nodes-base.set",
      "typeVersion": 3,
      "position": [460, 300]
    },
    {
      "parameters": {
        "method": "DELETE",
        "url": "={{ $env.CLEANUP_API_URL }}/records/stale",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            { "name": "Authorization", "value": "Bearer {{ $env.CLEANUP_API_KEY }}" },
            { "name": "Content-Type", "value": "application/json" }
          ]
        },
        "sendBody": true,
        "bodyParameters": {
          "parameters": [
            { "name": "before", "value": "={{ $json.cutoff_date }}" },
            { "name": "dry_run", "value": "false" }
          ]
        },
        "options": {}
      },
      "id": "nccr0001-0003-4000-8000-000000000003",
      "name": "Delete Stale Records",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4,
      "position": [680, 300]
    },
    {
      "parameters": {
        "conditions": {
          "conditions": [
            {
              "leftValue": "={{ $json.deleted_count }}",
              "rightValue": 0,
              "operator": { "type": "number", "operation": "gt" }
            }
          ]
        }
      },
      "id": "nccr0001-0004-4000-8000-000000000004",
      "name": "Any Records Deleted?",
      "type": "n8n-nodes-base.if",
      "typeVersion": 2,
      "position": [900, 300]
    },
    {
      "parameters": {
        "select": "channel",
        "channelId": { "__rl": true, "mode": "id", "value": "={{ $env.SLACK_OPS_CHANNEL }}" },
        "text": ":broom: *Nightly Cleanup Complete*\n*Run ID:* {{ $('Set Cutoff Date').item.json.run_id }}\n*Records Deleted:* {{ $('Delete Stale Records').item.json.deleted_count }}\n*Cutoff:* {{ $('Set Cutoff Date').item.json.cutoff_date }}",
        "otherOptions": {}
      },
      "id": "nccr0001-0005-4000-8000-000000000005",
      "name": "Notify Slack of Cleanup",
      "type": "n8n-nodes-base.slack",
      "typeVersion": 2,
      "position": [1120, 220]
    },
    {
      "parameters": {},
      "id": "nccr0001-0006-4000-8000-000000000006",
      "name": "Nothing to Clean (No-op)",
      "type": "n8n-nodes-base.noOp",
      "typeVersion": 1,
      "position": [1120, 400]
    }
  ],
  "connections": {
    "Midnight Cleanup Trigger": {
      "main": [[{ "node": "Set Cutoff Date", "type": "main", "index": 0 }]]
    },
    "Set Cutoff Date": {
      "main": [[{ "node": "Delete Stale Records", "type": "main", "index": 0 }]]
    },
    "Delete Stale Records": {
      "main": [[{ "node": "Any Records Deleted?", "type": "main", "index": 0 }]]
    },
    "Any Records Deleted?": {
      "main": [
        [{ "node": "Notify Slack of Cleanup", "type": "main", "index": 0 }],
        [{ "node": "Nothing to Clean (No-op)", "type": "main", "index": 0 }]
      ]
    }
  },
  "active": false,
  "settings": { "executionOrder": "v1" },
  "meta": {},
  "pinData": {}
}
