{
  "name": "Sync AWS billing invoices with FreeAgent and PostgreSQL tracking",
  "nodes": [
    {
      "id": "generate-signature",
      "name": "Generate AWS Signature",
      "type": "n8n-nodes-base.code",
      "position": [
        352,
        -464
      ]
    },
    {
      "id": "parse-aws-response",
      "name": "Parse AWS Response",
      "type": "n8n-nodes-base.code",
      "position": [
        752,
        -464
      ]
    },
    {
      "id": "filter-by-date-code",
      "name": "Filter By Date",
      "type": "n8n-nodes-base.code",
      "position": [
        944,
        -464
      ]
    },
    {
      "id": "loop-invoices",
      "name": "Loop Over Invoices",
      "type": "n8n-nodes-base.splitInBatches",
      "position": [
        -192,
        -112
      ]
    },
    {
      "id": "check-exists-pg",
      "name": "Check If Already Processed",
      "type": "n8n-nodes-base.postgres",
      "position": [
        144,
        -48
      ]
    },
    {
      "id": "record-pg",
      "name": "Record In PostgreSQL",
      "type": "n8n-nodes-base.postgres",
      "position": [
        1616,
        -64
      ]
    },
    {
      "id": "create-fa-bill",
      "name": "Create FreeAgent Bill",
      "type": "n8n-nodes-base.httpRequest",
      "position": [
        976,
        -64
      ]
    },
    {
      "id": "if-new-invoice",
      "name": "If New Invoice",
      "type": "n8n-nodes-base.if",
      "position": [
        352,
        -48
      ]
    },
    {
      "id": "prepare-fa-bill",
      "name": "Prepare FreeAgent Bill",
      "type": "n8n-nodes-base.code",
      "position": [
        768,
        -64
      ]
    },
    {
      "id": "mark-bill-paid",
      "name": "Mark Bill as Paid",
      "type": "n8n-nodes-base.httpRequest",
      "position": [
        1392,
        -64
      ]
    },
    {
      "id": "prepare-payment",
      "name": "Prepare Payment",
      "type": "n8n-nodes-base.code",
      "position": [
        1184,
        -64
      ]
    },
    {
      "id": "sticky-fetch",
      "name": "Sticky Note",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        80,
        -560
      ],
      "parameters": {
        "width": 1072,
        "height": 328,
        "content": "**1. Fetch AWS Invoices**\nRetrieves last month's invoices via AWS Invoicing API"
      }
    },
    {
      "id": "sticky-dedup",
      "name": "Sticky Note1",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        80,
        -176
      ],
      "parameters": {
        "width": 544,
        "height": 320,
        "content": "**2. Deduplicate**\nSkips invoices already in PostgreSQL"
      }
    },
    {
      "id": "sticky-create",
      "name": "Sticky Note2",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        672,
        -176
      ],
      "parameters": {
        "width": 1192,
        "height": 320,
        "content": "**3. Create & Pay Bill**\nCreates FreeAgent bill, marks paid, logs to database"
      }
    },
    {
      "id": "main-overview-sticky",
      "name": "Workflow Overview",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -880,
        -560
      ],
      "parameters": {
        "width": 580,
        "height": 720,
        "content": "## How it works\nThis workflow automatically imports AWS invoices into FreeAgent accounting software. Running on the 3rd and 4th of each month, it:\n\n1. **Fetches invoices** from AWS using Signature V4 "
      }
    },
    {
      "id": "calculate-last-month",
      "name": "Calculate Last Month Date Range",
      "type": "n8n-nodes-base.code",
      "position": [
        144,
        -464
      ]
    },
    {
      "id": "http-request",
      "name": "Call AWS For Bills",
      "type": "n8n-nodes-base.httpRequest",
      "position": [
        544,
        -464
      ]
    },
    {
      "id": "trigger-monthly",
      "name": "Trigger Monthly",
      "type": "n8n-nodes-base.scheduleTrigger",
      "position": [
        -208,
        -464
      ]
    }
  ],
  "connections": {
    "Filter By Date": {
      "main": [
        [
          {
            "node": "Loop Over Invoices",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "If New Invoice": {
      "main": [
        [
          {
            "node": "Prepare FreeAgent Bill",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Prepare Payment": {
      "main": [
        [
          {
            "node": "Mark Bill as Paid",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Trigger Monthly": {
      "main": [
        [
          {
            "node": "Calculate Last Month Date Range",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Mark Bill as Paid": {
      "main": [
        [
          {
            "node": "Record In PostgreSQL",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Call AWS For Bills": {
      "main": [
        [
          {
            "node": "Parse AWS Response",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Loop Over Invoices": {
      "main": [
        [],
        [
          {
            "node": "Check If Already Processed",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Parse AWS Response": {
      "main": [
        [
          {
            "node": "Filter By Date",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Record In PostgreSQL": {
      "main": [
        [
          {
            "node": "Loop Over Invoices",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Create FreeAgent Bill": {
      "main": [
        [
          {
            "node": "Prepare Payment",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Generate AWS Signature": {
      "main": [
        [
          {
            "node": "Call AWS For Bills",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Prepare FreeAgent Bill": {
      "main": [
        [
          {
            "node": "Create FreeAgent Bill",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Check If Already Processed": {
      "main": [
        [
          {
            "node": "If New Invoice",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Calculate Last Month Date Range": {
      "main": [
        [
          {
            "node": "Generate AWS Signature",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}