{
  "name": "Flowgrammer Canada — email approved dispatch",
  "nodes": [
    {
      "id": "fg-85",
      "name": "Dispatch manually",
      "type": "n8n-nodes-base.manualTrigger",
      "typeVersion": 1,
      "position": [
        0,
        300
      ],
      "parameters": {}
    },
    {
      "id": "fg-86",
      "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-87",
      "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-88",
      "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, 'email', $('Settings').first().json.dispatch_enabled === true && $('Settings').first().json.dry_run === false] }}"
        }
      },
      "retryOnFail": true,
      "maxTries": 3,
      "waitBetweenTries": 2000
    },
    {
      "id": "fg-89",
      "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-90",
      "name": "Send approved reminder",
      "type": "n8n-nodes-base.emailSend",
      "typeVersion": 2.1,
      "position": [
        1300,
        300
      ],
      "parameters": {
        "fromEmail": "={{ $(\"Settings\").first().json.from_email }}",
        "toEmail": "={{ $(\"Claim one approved action\").first().json.result.recipient }}",
        "subject": "={{ $(\"Claim one approved action\").first().json.result.subject }}",
        "emailFormat": "text",
        "text": "={{ $(\"Claim one approved action\").first().json.result.text }}",
        "options": {}
      },
      "onError": "continueRegularOutput",
      "retryOnFail": false
    },
    {
      "id": "fg-91",
      "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 accepted = r.accepted || []; const ok = !r.error && !!r.messageId && accepted.some(x => String(x).toLowerCase() === a.result.recipient.toLowerCase()); return [{json:{id:a.id,client_id:a.client_id,ok,destination_id:ok ? r.messageId : 'RECONCILE BEFORE RETRY'}}];"
      }
    },
    {
      "id": "fg-92",
      "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-93",
      "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-94",
      "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": "Send approved reminder",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Send approved reminder": {
      "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": []
}
