{
  "name": "Enroutia · Daily cost per client to Google Sheets",
  "nodes": [
    {
      "parameters": {
        "rule": {
          "interval": [
            {
              "triggerAtHour": 7
            }
          ]
        }
      },
      "name": "Every morning",
      "type": "n8n-nodes-base.scheduleTrigger",
      "typeVersion": 1.2,
      "position": [
        0,
        0
      ]
    },
    {
      "parameters": {
        "assignments": {
          "assignments": [
            {
              "id": "enroutia_api",
              "name": "ENROUTIA_API",
              "value": "https://platform.enroutia.com",
              "type": "string"
            }
          ]
        },
        "includeOtherFields": true,
        "options": {}
      },
      "name": "Settings",
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "position": [
        220,
        0
      ]
    },
    {
      "parameters": {
        "method": "GET",
        "url": "={{ $('Settings').item.json.ENROUTIA_API }}/api/usage/export.csv",
        "sendQuery": true,
        "queryParameters": {
          "parameters": [
            {
              "name": "days",
              "value": "2"
            },
            {
              "name": "by",
              "value": "key"
            }
          ]
        },
        "authentication": "genericCredentialType",
        "genericAuthType": "httpHeaderAuth",
        "options": {
          "timeout": 30000,
          "response": {
            "response": {
              "responseFormat": "text"
            }
          }
        }
      },
      "name": "Usage by key",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        440,
        0
      ],
      "credentials": {
        "httpHeaderAuth": {
          "name": "Enroutia account token"
        }
      }
    },
    {
      "parameters": {
        "jsCode": "// The export is one line per day and bucket: date,bucket,calls,tokens_in,tokens_out,cents.\n// Keep yesterday's rows (today's are still filling) and turn them into sheet rows.\nconst csv = String($json.data || '');\nconst [header, ...lines] = csv.trim().split('\\n');\nconst columns = header.split(',');\nconst yesterday = new Date(Date.now() - 86400000).toISOString().slice(0, 10);\n\n// Cells are plain except a bucket with a comma, which the CSV quotes.\nconst parse = (line) => {\n  const cells = [];\n  let cell = '', quoted = false;\n  for (let i = 0; i < line.length; i++) {\n    const ch = line[i];\n    if (quoted) {\n      if (ch === '\"' && line[i + 1] === '\"') { cell += '\"'; i++; }\n      else if (ch === '\"') quoted = false;\n      else cell += ch;\n    } else if (ch === '\"') quoted = true;\n    else if (ch === ',') { cells.push(cell); cell = ''; }\n    else cell += ch;\n  }\n  cells.push(cell);\n  return cells;\n};\n\nreturn lines\n  .map((line) => Object.fromEntries(parse(line).map((value, i) => [columns[i], value])))\n  .filter((row) => row.date === yesterday)\n  .map((row) => ({ json: {\n    date: row.date,\n    client: row.bucket,\n    calls: Number(row.calls),\n    eur: Number(row.cents) / 100,\n  } }));\n"
      },
      "name": "Yesterday per client",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        660,
        0
      ]
    },
    {
      "parameters": {
        "operation": "append",
        "documentId": {
          "__rl": true,
          "value": "",
          "mode": "url"
        },
        "sheetName": {
          "__rl": true,
          "value": "Enroutia cost",
          "mode": "name"
        },
        "columns": {
          "mappingMode": "autoMapInputData",
          "value": {},
          "matchingColumns": [],
          "schema": []
        },
        "options": {}
      },
      "name": "Append to sheet",
      "type": "n8n-nodes-base.googleSheets",
      "typeVersion": 4.5,
      "position": [
        880,
        0
      ],
      "credentials": {
        "googleSheetsOAuth2Api": {
          "name": "Google Sheets account"
        }
      }
    }
  ],
  "connections": {
    "Every morning": {
      "main": [
        [
          {
            "node": "Settings",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Usage by key": {
      "main": [
        [
          {
            "node": "Yesterday per client",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Yesterday per client": {
      "main": [
        [
          {
            "node": "Append to sheet",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Settings": {
      "main": [
        [
          {
            "node": "Usage by key",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "active": false,
  "settings": {
    "executionOrder": "v1"
  },
  "pinData": {},
  "meta": {
    "templateCredsSetupCompleted": false
  },
  "tags": []
}
