{
  "name": "MZPanel: nightly backup check",
  "nodes": [
    {
      "parameters": {
        "rule": {
          "interval": [
            {
              "field": "days",
              "daysInterval": 1,
              "triggerAtHour": 7
            }
          ]
        }
      },
      "id": "53275268-630b-5a19-a6ba-77de5f905ae4",
      "name": "Every day 07:00",
      "type": "n8n-nodes-base.scheduleTrigger",
      "typeVersion": 1.2,
      "position": [
        0,
        300
      ]
    },
    {
      "parameters": {
        "mode": "manual",
        "assignments": {
          "assignments": [
            {
              "id": "657f27a0-c0dd-5a11-b2d2-bf162d6674bd",
              "name": "apiBase",
              "value": "https://api.mzpanel.com",
              "type": "string"
            },
            {
              "id": "507c95e8-6b0c-5c3c-9b66-5f1ae06e3a20",
              "name": "slackWebhookUrl",
              "value": "https://hooks.slack.com/services/REPLACE/ME",
              "type": "string"
            }
          ]
        },
        "options": {}
      },
      "id": "fd71e4d2-ec17-598a-8683-369dc645d199",
      "name": "Settings",
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "position": [
        220,
        300
      ]
    },
    {
      "parameters": {
        "method": "GET",
        "url": "={{ $('Settings').first().json.apiBase }}/v1/events?types=backup.completed,backup.failed&limit=100",
        "options": {},
        "authentication": "genericCredentialType",
        "genericAuthType": "httpHeaderAuth"
      },
      "id": "81c587f4-8c0e-5709-88f5-97656460ce86",
      "name": "Backup events",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        440,
        300
      ],
      "credentials": {
        "httpHeaderAuth": {
          "id": "mzpanel-api-token",
          "name": "MZPanel API token"
        }
      }
    },
    {
      "parameters": {
        "jsCode": "// Count backup runs of the last 24 hours from the event feed (newest first).\nconst body = $input.first().json;\nconst since = Date.now() - 24 * 3600 * 1000;\nconst recent = (body.events || []).filter((e) => Date.parse(e.createdAt) >= since);\nconst failed = recent.filter((e) => e.type === 'backup.failed');\nconst completed = recent.filter((e) => e.type === 'backup.completed');\nif (failed.length === 0 && completed.length > 0) return [];  // all good: stay quiet\nconst lines = failed.map((e) => '- ' + (e.subject || e.data?.job_type || 'backup') + ' on server ' + e.serverId + ' (job ' + (e.data?.job_id || '?') + ')');\nconst text = completed.length === 0 && failed.length === 0\n  ? ':warning: No backup ran in the last 24 hours.'\n  : ':x: ' + failed.length + ' backup run(s) failed in the last 24 hours (' + completed.length + ' succeeded):\\n' + lines.join('\\n');\nreturn [{ json: { text } }];\n"
      },
      "id": "6f6b3137-08b3-548e-960a-0bc17c41c92c",
      "name": "Summarize",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        660,
        300
      ]
    },
    {
      "parameters": {
        "method": "POST",
        "url": "={{ $('Settings').first().json.slackWebhookUrl }}",
        "options": {},
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={{ JSON.stringify({ text: $json.text }) }}"
      },
      "id": "f8766e54-b03a-537c-bc90-8807eac279b9",
      "name": "Post to Slack",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        880,
        300
      ]
    }
  ],
  "connections": {
    "Every day 07:00": {
      "main": [
        [
          {
            "node": "Settings",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Settings": {
      "main": [
        [
          {
            "node": "Backup events",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Backup events": {
      "main": [
        [
          {
            "node": "Summarize",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Summarize": {
      "main": [
        [
          {
            "node": "Post to Slack",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "settings": {
    "executionOrder": "v1"
  },
  "pinData": {},
  "meta": {
    "templateCredsSetupCompleted": false,
    "mzpanel": "Reads backup.completed / backup.failed from GET /v1/events once a day (Read token, every plan) and posts to Slack only when a run failed or none ran."
  }
}
