{
  "name": "MADIAD Hub - Track plan usage",
  "nodes": [
    {
      "parameters": {
        "rule": {
          "interval": [
            {
              "field": "days",
              "triggerAtHour": 8
            }
          ]
        }
      },
      "type": "n8n-nodes-base.scheduleTrigger",
      "typeVersion": 1.2,
      "position": [
        -160,
        0
      ],
      "id": "Every morning at 8",
      "name": "Every morning at 8"
    },
    {
      "parameters": {
        "method": "GET",
        "url": "https://api.madiad.com/v1/usage",
        "authentication": "genericCredentialType",
        "genericAuthType": "httpHeaderAuth",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "X-Madiad-Source",
              "value": "tpl-usage-v1"
            }
          ]
        },
        "options": {
          "response": {
            "response": {
              "neverError": true,
              "fullResponse": true
            }
          }
        }
      },
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.4,
      "position": [
        60,
        0
      ],
      "id": "Read usage",
      "name": "Read usage",
      "onError": "continueRegularOutput"
    },
    {
      "parameters": {
        "jsCode": "\n// limit = null means UNLIMITED, not zero. Reading it the other way tells a customer they are out of allowance when they have no ceiling at all.\nconst u = $json.body ?? {};\nconst cap = (m) => (m?.limit === null || m?.limit === undefined ? '-' : m.limit);\nconst warn = [];\nfor (const [name, m] of [['profiles', u.profiles], ['uploads', u.uploads], ['DMs', u.direct_messages]]) {\n  if (m && typeof m.limit === 'number' && m.limit > 0 && (m.used ?? 0) / m.limit >= 0.8)\n    warn.push(name + ' ' + m.used + '/' + m.limit);\n}\nif (u.period?.expires_on) {\n  const days = Math.ceil((new Date(u.period.expires_on) - new Date()) / 86400000);\n  if (days <= 7) warn.push('plan expires in ' + days + 'd');\n}\nreturn [{ json: {\n  'Date': $now.setZone('UTC').toFormat('yyyy-MM-dd HH:mm'),\n  'Plan': u.plan?.name ?? u.plan?.id ?? '',\n  'Profiles': String(u.profiles?.used ?? '') + '/' + cap(u.profiles),\n  'Posts': String(u.uploads?.used ?? '') + '/' + cap(u.uploads),\n  'DMs': String(u.direct_messages?.used ?? '') + '/' + cap(u.direct_messages),\n  'Expires': u.period?.expires_on ?? '',\n  'Warning': warn.join(' | '),\n} }];\n"
      },
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        280,
        0
      ],
      "id": "Build the row",
      "name": "Build the row"
    },
    {
      "parameters": {
        "operation": "append",
        "documentId": {
          "__rl": true,
          "value": "",
          "mode": "list",
          "cachedResultName": "[MADIAD Template] Publishing Schedule"
        },
        "sheetName": {
          "__rl": true,
          "value": "Usage",
          "mode": "name"
        },
        "columns": {
          "mappingMode": "autoMapInputData",
          "matchingColumns": [],
          "schema": []
        },
        "options": {}
      },
      "type": "n8n-nodes-base.googleSheets",
      "typeVersion": 4.7,
      "position": [
        500,
        0
      ],
      "id": "Append to Usage",
      "name": "Append to Usage"
    },
    {
      "parameters": {
        "content": "## MADIAD Hub - Track plan usage\n\n1. Pick your own copy of the sheet in EVERY Google Sheets node.\n2. Create a **Header Auth** credential named `MADIAD Hub`:",
        "height": 300,
        "width": 430,
        "color": 3
      },
      "type": "n8n-nodes-base.stickyNote",
      "typeVersion": 1,
      "position": [
        -620,
        -160
      ],
      "id": "Setup",
      "name": "Setup"
    }
  ],
  "connections": {
    "Every morning at 8": {
      "main": [
        [
          {
            "node": "Read usage",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Read usage": {
      "main": [
        [
          {
            "node": "Build the row",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Build the row": {
      "main": [
        [
          {
            "node": "Append to Usage",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "settings": {
    "executionOrder": "v1"
  },
  "pinData": {},
  "meta": {
    "templateCredsSetupCompleted": false
  }
}