{
  "name": "MADIAD Hub - Theo dõi hạn mức",
  "nodes": [
    {
      "parameters": {
        "rule": {
          "interval": [
            {
              "field": "days",
              "triggerAtHour": 8
            }
          ]
        }
      },
      "type": "n8n-nodes-base.scheduleTrigger",
      "typeVersion": 1.2,
      "position": [
        -160,
        0
      ],
      "id": "Mỗi sáng 8 giờ",
      "name": "Mỗi sáng 8 giờ"
    },
    {
      "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": "Đọc hạn mức",
      "name": "Đọc hạn mức",
      "onError": "continueRegularOutput"
    },
    {
      "parameters": {
        "jsCode": "\n// limit = null nghĩa là KHÔNG GIỚI HẠN, không phải bằng 0. Đọc nhầm chỗ này là báo cho khách rằng họ hết hạn mức trong khi họ không có trần nào.\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('Asia/Ho_Chi_Minh').toFormat('dd/MM/yyyy 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": "Dựng dòng ghi",
      "name": "Dựng dòng ghi"
    },
    {
      "parameters": {
        "operation": "append",
        "documentId": {
          "__rl": true,
          "value": "",
          "mode": "list",
          "cachedResultName": "[MADIAD Template] Lịch Đăng Hub"
        },
        "sheetName": {
          "__rl": true,
          "value": "Hạn Mức",
          "mode": "name"
        },
        "columns": {
          "mappingMode": "autoMapInputData",
          "matchingColumns": [],
          "schema": []
        },
        "options": {}
      },
      "type": "n8n-nodes-base.googleSheets",
      "typeVersion": 4.7,
      "position": [
        500,
        0
      ],
      "id": "Ghi vào tab Hạn Mức",
      "name": "Ghi vào tab Hạn Mức"
    },
    {
      "parameters": {
        "content": "## MADIAD Hub - Theo dõi hạn mức\n\n1. Chọn Google Sheet của bạn ở TẤT CẢ các node Google Sheets.\n2. Tạo credential **Header Auth** tên `MADIAD Hub`:",
        "height": 300,
        "width": 430,
        "color": 3
      },
      "type": "n8n-nodes-base.stickyNote",
      "typeVersion": 1,
      "position": [
        -620,
        -160
      ],
      "id": "Hướng dẫn",
      "name": "Hướng dẫn"
    }
  ],
  "connections": {
    "Mỗi sáng 8 giờ": {
      "main": [
        [
          {
            "node": "Đọc hạn mức",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Đọc hạn mức": {
      "main": [
        [
          {
            "node": "Dựng dòng ghi",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Dựng dòng ghi": {
      "main": [
        [
          {
            "node": "Ghi vào tab Hạn Mức",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "settings": {
    "executionOrder": "v1"
  },
  "pinData": {},
  "meta": {
    "templateCredsSetupCompleted": false
  }
}