{
  "name": "Daily Copilot Usage Collector",
  "nodes": [
    {
      "parameters": {
        "rule": {
          "interval": [
            {
              "field": "days",
              "triggerAtHour": 1,
              "triggerAtMinute": 0
            }
          ]
        }
      },
      "id": "cron-trigger",
      "name": "Daily 01:00",
      "type": "n8n-nodes-base.scheduleTrigger",
      "typeVersion": 1.2,
      "position": [
        0,
        0
      ]
    },
    {
      "parameters": {
        "jsCode": "const now = new Date();\nnow.setDate(now.getDate() - 1);\nconst year = now.getFullYear();\nconst month = now.getMonth() + 1;\nconst day = now.getDate();\nreturn [{ json: { year, month, day } }];"
      },
      "id": "calc-yesterday",
      "name": "Calculate Yesterday",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        220,
        0
      ]
    },
    {
      "parameters": {
        "method": "GET",
        "url": "=https://api.github.com/users/YOUR_GITHUB_USERNAME/settings/billing/premium_request/usage",
        "authentication": "genericCredentialType",
        "genericAuthType": "httpHeaderAuth",
        "sendQuery": true,
        "queryParameters": {
          "parameters": [
            {
              "name": "year",
              "value": "={{ $json.year }}"
            },
            {
              "name": "month",
              "value": "={{ $json.month }}"
            },
            {
              "name": "day",
              "value": "={{ $json.day }}"
            }
          ]
        },
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "Accept",
              "value": "application/vnd.github+json"
            },
            {
              "name": "X-GitHub-Api-Version",
              "value": "2026-03-10"
            }
          ]
        },
        "options": {
          "response": {
            "response": {
              "fullResponse": false
            }
          }
        }
      },
      "id": "fetch-premium",
      "name": "Fetch Premium Requests",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        460,
        -150
      ],
      "credentials": {
        "httpHeaderAuth": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "fieldToSplitOut": "usageItems",
        "options": {}
      },
      "id": "split-premium",
      "name": "Split Premium Items",
      "type": "n8n-nodes-base.splitOut",
      "typeVersion": 1,
      "position": [
        680,
        -150
      ]
    },
    {
      "parameters": {
        "operation": "executeQuery",
        "query": "INSERT INTO copilot_premium_requests (pulled_date, year, month, day, product, sku, model, unit_type, price_per_unit, gross_quantity, net_quantity, gross_amount, discount_quantity, discount_amount, net_amount) VALUES (CURRENT_DATE, {{ $('Calculate Yesterday').item.json.year }}, {{ $('Calculate Yesterday').item.json.month }}, {{ $('Calculate Yesterday').item.json.day }}, '{{ $json.product }}', '{{ $json.sku }}', '{{ $json.model }}', '{{ $json.unitType }}', {{ $json.pricePerUnit || 0 }}, {{ $json.grossQuantity || 0 }}, {{ $json.netQuantity || 0 }}, {{ $json.grossAmount || 0 }}, {{ $json.discountQuantity || 0 }}, {{ $json.discountAmount || 0 }}, {{ $json.netAmount || 0 }}) ON CONFLICT (year, month, day, product, sku, model) DO UPDATE SET gross_quantity = EXCLUDED.gross_quantity, net_quantity = EXCLUDED.net_quantity, gross_amount = EXCLUDED.gross_amount, net_amount = EXCLUDED.net_amount, discount_quantity = EXCLUDED.discount_quantity, discount_amount = EXCLUDED.discount_amount;",
        "options": {}
      },
      "id": "insert-premium",
      "name": "Insert Premium Data",
      "type": "n8n-nodes-base.postgres",
      "typeVersion": 2.5,
      "position": [
        900,
        -150
      ],
      "credentials": {
        "postgres": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "method": "GET",
        "url": "=https://api.github.com/users/YOUR_GITHUB_USERNAME/settings/billing/usage",
        "authentication": "genericCredentialType",
        "genericAuthType": "httpHeaderAuth",
        "sendQuery": true,
        "queryParameters": {
          "parameters": [
            {
              "name": "year",
              "value": "={{ $json.year }}"
            },
            {
              "name": "month",
              "value": "={{ $json.month }}"
            },
            {
              "name": "day",
              "value": "={{ $json.day }}"
            }
          ]
        },
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "Accept",
              "value": "application/vnd.github+json"
            },
            {
              "name": "X-GitHub-Api-Version",
              "value": "2026-03-10"
            }
          ]
        },
        "options": {}
      },
      "id": "fetch-daily",
      "name": "Fetch Daily Usage",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        460,
        0
      ],
      "credentials": {
        "httpHeaderAuth": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "fieldToSplitOut": "usageItems",
        "options": {}
      },
      "id": "split-daily",
      "name": "Split Daily Items",
      "type": "n8n-nodes-base.splitOut",
      "typeVersion": 1,
      "position": [
        680,
        0
      ]
    },
    {
      "parameters": {
        "operation": "executeQuery",
        "query": "INSERT INTO copilot_daily_usage (usage_date, product, sku, quantity, unit_type, price_per_unit, gross_amount, discount_amount, net_amount, repository_name) VALUES ('{{ $json.date }}', '{{ $json.product }}', '{{ $json.sku }}', {{ $json.quantity || 0 }}, '{{ $json.unitType }}', {{ $json.pricePerUnit || 0 }}, {{ $json.grossAmount || 0 }}, {{ $json.discountAmount || 0 }}, {{ $json.netAmount || 0 }}, '{{ $json.repositoryName || '' }}') ON CONFLICT (usage_date, product, sku, repository_name) DO UPDATE SET quantity = EXCLUDED.quantity, gross_amount = EXCLUDED.gross_amount, discount_amount = EXCLUDED.discount_amount, net_amount = EXCLUDED.net_amount;",
        "options": {}
      },
      "id": "insert-daily",
      "name": "Insert Daily Data",
      "type": "n8n-nodes-base.postgres",
      "typeVersion": 2.5,
      "position": [
        900,
        0
      ],
      "credentials": {
        "postgres": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "method": "GET",
        "url": "=https://api.github.com/users/YOUR_GITHUB_USERNAME/settings/billing/usage/summary",
        "authentication": "genericCredentialType",
        "genericAuthType": "httpHeaderAuth",
        "sendQuery": true,
        "queryParameters": {
          "parameters": [
            {
              "name": "year",
              "value": "={{ $json.year }}"
            },
            {
              "name": "month",
              "value": "={{ $json.month }}"
            }
          ]
        },
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "Accept",
              "value": "application/vnd.github+json"
            },
            {
              "name": "X-GitHub-Api-Version",
              "value": "2026-03-10"
            }
          ]
        },
        "options": {}
      },
      "id": "fetch-summary",
      "name": "Fetch Usage Summary",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        460,
        150
      ],
      "credentials": {
        "httpHeaderAuth": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "fieldToSplitOut": "usageItems",
        "options": {}
      },
      "id": "split-summary",
      "name": "Split Summary Items",
      "type": "n8n-nodes-base.splitOut",
      "typeVersion": 1,
      "position": [
        680,
        150
      ]
    },
    {
      "parameters": {
        "operation": "executeQuery",
        "query": "INSERT INTO copilot_usage_summary (year, month, product, sku, unit_type, price_per_unit, gross_quantity, net_quantity, gross_amount, discount_quantity, discount_amount, net_amount, updated_at) VALUES ({{ $('Calculate Yesterday').item.json.year }}, {{ $('Calculate Yesterday').item.json.month }}, '{{ $json.product }}', '{{ $json.sku }}', '{{ $json.unitType }}', {{ $json.pricePerUnit || 0 }}, {{ $json.grossQuantity || 0 }}, {{ $json.netQuantity || 0 }}, {{ $json.grossAmount || 0 }}, {{ $json.discountQuantity || 0 }}, {{ $json.discountAmount || 0 }}, {{ $json.netAmount || 0 }}, NOW()) ON CONFLICT (year, month, product, sku) DO UPDATE SET gross_quantity = EXCLUDED.gross_quantity, net_quantity = EXCLUDED.net_quantity, gross_amount = EXCLUDED.gross_amount, discount_quantity = EXCLUDED.discount_quantity, discount_amount = EXCLUDED.discount_amount, net_amount = EXCLUDED.net_amount, updated_at = NOW();",
        "options": {}
      },
      "id": "insert-summary",
      "name": "Insert Summary Data",
      "type": "n8n-nodes-base.postgres",
      "typeVersion": 2.5,
      "position": [
        900,
        150
      ],
      "credentials": {
        "postgres": {
          "name": "<your credential>"
        }
      }
    }
  ],
  "connections": {
    "Daily 01:00": {
      "main": [
        [
          {
            "node": "Calculate Yesterday",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Calculate Yesterday": {
      "main": [
        [
          {
            "node": "Fetch Premium Requests",
            "type": "main",
            "index": 0
          },
          {
            "node": "Fetch Daily Usage",
            "type": "main",
            "index": 0
          },
          {
            "node": "Fetch Usage Summary",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Fetch Premium Requests": {
      "main": [
        [
          {
            "node": "Split Premium Items",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Split Premium Items": {
      "main": [
        [
          {
            "node": "Insert Premium Data",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Fetch Daily Usage": {
      "main": [
        [
          {
            "node": "Split Daily Items",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Split Daily Items": {
      "main": [
        [
          {
            "node": "Insert Daily Data",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Fetch Usage Summary": {
      "main": [
        [
          {
            "node": "Split Summary Items",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Split Summary Items": {
      "main": [
        [
          {
            "node": "Insert Summary Data",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "settings": {
    "executionOrder": "v1",
    "timezone": "Asia/Hong_Kong"
  }
}