{
  "name": "Flowgrammer Canada — xero approved dispatch",
  "nodes": [
    {
      "id": "fg-75",
      "name": "Dispatch manually",
      "type": "n8n-nodes-base.manualTrigger",
      "typeVersion": 1,
      "position": [
        0,
        300
      ],
      "parameters": {}
    },
    {
      "id": "fg-76",
      "name": "Settings",
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "position": [
        260,
        300
      ],
      "parameters": {
        "mode": "raw",
        "jsonOutput": "{\"client_id\":\"demo-client\",\"client_name\":\"Demo client\",\"business_display_name\":\"Your business\",\"idempotency_namespace\":\"CHANGE-TO-UNIQUE-CLIENT-UUID\",\"timezone\":\"America/Toronto\",\"locale\":\"en-CA\",\"currency\":\"CAD\",\"province\":\"ON\",\"tax_regime\":\"HST-ON\",\"tax_components\":[[\"HST\",0.13]],\"tax_inclusive\":false,\"rates_valid_from\":\"2026-01-01\",\"rates_valid_through\":\"2026-12-31\",\"dry_run\":true,\"dispatch_enabled\":false,\"batch_size\":10,\"wait_seconds_between_sends\":5,\"amount_tolerance_cents\":1,\"variance_pct_threshold\":20,\"reminder_days\":[3,7,14],\"max_reminders\":3,\"deadline_notice_days\":14,\"max_file_mb\":10,\"max_pages\":20,\"allowed_file_types\":[\"application/pdf\",\"image/png\",\"image/jpeg\"],\"quickbooks_sandbox\":true,\"quickbooks_realm_id\":\"\",\"quickbooks_item_id\":\"\",\"quickbooks_tax_code\":\"\",\"xero_tenant_id\":\"\",\"xero_account_code\":\"\",\"xero_tax_type\":\"\",\"error_notify_to\":\"configure-internal-alert@example.com\",\"from_email\":\"configure-sender@example.com\"}",
        "options": {}
      }
    },
    {
      "id": "fg-77",
      "name": "Validate dispatch configuration",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        520,
        300
      ],
      "parameters": {
        "jsCode": "const s = $input.first().json; if (!/^[a-f0-9-]{36}$/i.test(s.idempotency_namespace)) throw new Error('Set a unique client database UUID namespace before delivery.'); return $input.all();"
      }
    },
    {
      "id": "fg-78",
      "name": "Claim one approved action",
      "type": "n8n-nodes-base.postgres",
      "typeVersion": 2.6,
      "position": [
        780,
        300
      ],
      "parameters": {
        "operation": "executeQuery",
        "query": "select * from fg_finance.claim($1,$2,$3);",
        "options": {
          "queryReplacement": "={{ [$('Settings').first().json.client_id, 'xero', $('Settings').first().json.dispatch_enabled === true && $('Settings').first().json.dry_run === false] }}"
        }
      },
      "retryOnFail": true,
      "maxTries": 3,
      "waitBetweenTries": 2000
    },
    {
      "id": "fg-79",
      "name": "Rate limit",
      "type": "n8n-nodes-base.wait",
      "typeVersion": 1.1,
      "position": [
        1040,
        300
      ],
      "parameters": {
        "amount": "={{ $(\"Settings\").first().json.wait_seconds_between_sends }}",
        "unit": "seconds"
      }
    },
    {
      "id": "fg-80",
      "name": "Create approved ledger draft",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        1300,
        300
      ],
      "parameters": {
        "method": "POST",
        "url": "https://api.xero.com/api.xro/2.0/Invoices",
        "authentication": "predefinedCredentialType",
        "nodeCredentialType": "xeroOAuth2Api",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "Accept",
              "value": "application/json"
            },
            {
              "name": "xero-tenant-id",
              "value": "={{ $json.result.tenant_id }}"
            },
            {
              "name": "Idempotency-Key",
              "value": "={{ $(\"Settings\").first().json.idempotency_namespace + \"-\" + $json.id }}"
            }
          ]
        },
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={{ JSON.stringify($json.result.body) }}",
        "options": {
          "timeout": 30000,
          "response": {
            "response": {
              "fullResponse": true,
              "neverError": true,
              "responseFormat": "json"
            }
          }
        }
      },
      "onError": "continueRegularOutput",
      "retryOnFail": false
    },
    {
      "id": "fg-81",
      "name": "Check destination acknowledgement",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        1560,
        300
      ],
      "parameters": {
        "jsCode": "const a = $('Claim one approved action').first().json; const r = $input.first().json; const body = r.body || {}; const id = body.Invoices?.[0]?.InvoiceID; const invalid = body.Invoices?.some(x => x.HasErrors || x.ValidationErrors?.length); const ok = !r.error && r.statusCode >= 200 && r.statusCode < 300 && !!id && !invalid; return [{json:{id:a.id,client_id:a.client_id,ok,destination_id:ok ? String(id) : 'RECONCILE BEFORE RETRY'}}];"
      }
    },
    {
      "id": "fg-82",
      "name": "Record delivery outcome",
      "type": "n8n-nodes-base.postgres",
      "typeVersion": 2.6,
      "position": [
        1820,
        300
      ],
      "parameters": {
        "operation": "executeQuery",
        "query": "select fg_finance.finish($1,$2,$3,$4);",
        "options": {
          "queryReplacement": "={{ [$json.id,$json.client_id,$json.ok,$json.destination_id] }}"
        }
      },
      "retryOnFail": true,
      "maxTries": 3,
      "waitBetweenTries": 2000
    },
    {
      "id": "fg-83",
      "name": "Stop if outcome uncertain",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        2080,
        300
      ],
      "parameters": {
        "jsCode": "if (!$('Check destination acknowledgement').first().json.ok) throw new Error('Destination result uncertain. Review queue and destination before any retry.'); return $input.all();"
      }
    },
    {
      "id": "fg-84",
      "name": "Read setup guide first",
      "type": "n8n-nodes-base.stickyNote",
      "typeVersion": 1,
      "position": [
        2340,
        -100
      ],
      "parameters": {
        "content": "## Setup required\nOriginal Flowgrammer workflow. Import queue.sql into a separate private PostgreSQL database. Connect least-privilege Postgres credentials to every database node. Configure Settings; keep dry_run=true for testing. Import 12-error-alert.json and select it under Workflow Settings > Error workflow (IDs are instance-specific). No OCR/AI or source-app connectors included: load normalized records using the documented input contract. Human reviewer uses separate database login. See README.md.",
        "height": 380,
        "width": 520
      }
    }
  ],
  "connections": {
    "Dispatch manually": {
      "main": [
        [
          {
            "node": "Settings",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Settings": {
      "main": [
        [
          {
            "node": "Validate dispatch configuration",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Validate dispatch configuration": {
      "main": [
        [
          {
            "node": "Claim one approved action",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Claim one approved action": {
      "main": [
        [
          {
            "node": "Rate limit",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Rate limit": {
      "main": [
        [
          {
            "node": "Create approved ledger draft",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Create approved ledger draft": {
      "main": [
        [
          {
            "node": "Check destination acknowledgement",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Check destination acknowledgement": {
      "main": [
        [
          {
            "node": "Record delivery outcome",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Record delivery outcome": {
      "main": [
        [
          {
            "node": "Stop if outcome uncertain",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "active": false,
  "settings": {
    "executionOrder": "v1",
    "timezone": "America/Toronto",
    "saveDataErrorExecution": "all",
    "saveDataSuccessExecution": "none",
    "executionTimeout": 300
  },
  "pinData": {},
  "tags": []
}
