{
 "name": "TWWP: overdue invoice chaser with tiered reminders (UC-0887)",
 "nodes": [
  {
   "parameters": {
    "httpMethod": "POST",
    "path": "twwp-overdue-invoice-chaser-0887",
    "responseMode": "responseNode",
    "options": {}
   },
   "id": "webhook",
   "name": "Webhook (synthetic input)",
   "type": "n8n-nodes-base.webhook",
   "typeVersion": 2,
   "position": [
    0,
    0
   ],
   "webhookId": "46f63dfb-b044-5c90-8830-dc8b86ed1916"
  },
  {
   "parameters": {
    "jsCode": "const b = $input.first().json.body || {};\nconst hint = (b.approved !== undefined && typeof b.approved !== 'boolean') ? 'approved must be true or false, written without quotes (not yes, and not \"true\")' : undefined;\nconst validDate = v => { if (typeof v !== 'string' || !/^\\d{4}-\\d{2}-\\d{2}$/.test(v)) return null; const d = new Date(v + 'T00:00:00Z'); return (!isNaN(d) && d.toISOString().slice(0, 10) === v) ? d : null; };\nconst today = validDate(b.today);\nconst drafts = [], disputed = [], review_queue = [];\nfor (const inv of (b.invoices || [])) {\n  if (inv.status === 'paid') continue;\n  if (inv.status === 'disputed') { disputed.push(inv.id); continue; }\n  const due = validDate(inv.due);\n  if (!today || !due) { review_queue.push(inv.id); continue; }   // unreadable or impossible date: a person looks, never a tier\n  const age = Math.floor((today - due) / 86400000);\n  if (age < 1) continue;\n  const tier = age <= 14 ? 'friendly' : (age <= 30 ? 'firm' : 'final');\n  drafts.push({ invoice: inv.id, customer: inv.customer, amount: inv.amount, days_overdue: age, tier, subject: tier === 'final' ? 'Final notice: invoice ' + inv.id : (tier === 'firm' ? 'Reminder: invoice ' + inv.id + ' is overdue' : 'Friendly reminder: invoice ' + inv.id) });\n}\nreturn [{ json: { hint, drafts, disputed, review_queue, hold_reasons: [] } }];"
   },
   "id": "extract-and-decide",
   "name": "Extract and decide",
   "type": "n8n-nodes-base.code",
   "typeVersion": 2,
   "position": [
    240,
    0
   ]
  },
  {
   "parameters": {
    "jsCode": "// DEMO ONLY. A person's approval is stood in for by the request field approved === true, so the tests run without a screen.\n// In real use replace this node with a SEPARATE approval step (a person's click or a signed event: authenticated, bound to this exact draft, asked again if the draft changes).\n// FIXED LOGIC, not a model field: anything that would send, pay or change data ALWAYS waits for approval. The extract / AI step can only ADD reasons to hold (hold_reasons);\n// it can never remove the hold, skip the approval, or open the gate. A missing or malformed hold_reasons is treated as a hold.\nconst j = $input.first().json;\nconst body = ($('Webhook (synthetic input)').first().json.body) || {};\nconst approved = body.approved === true;\nconst modelHolds = !Array.isArray(j.hold_reasons) || j.hold_reasons.length > 0;\nreturn [{ json: { ...j, gate_open: approved && !modelHolds } }];"
   },
   "id": "approval-check-(demo-toggle)",
   "name": "Approval check (demo toggle)",
   "type": "n8n-nodes-base.code",
   "typeVersion": 2,
   "position": [
    360,
    0
   ]
  },
  {
   "parameters": {
    "conditions": {
     "options": {
      "caseSensitive": true,
      "leftValue": "",
      "typeValidation": "strict"
     },
     "conditions": [
      {
       "id": "approved",
       "leftValue": "={{ $json.gate_open }}",
       "rightValue": true,
       "operator": {
        "type": "boolean",
        "operation": "true",
        "singleValue": true
       }
      }
     ],
     "combinator": "and"
    },
    "options": {}
   },
   "id": "gate",
   "name": "Approval gate",
   "type": "n8n-nodes-base.if",
   "typeVersion": 2.2,
   "position": [
    600,
    0
   ]
  },
  {
   "parameters": {
    "jsCode": "const j = $input.first().json;\nreturn [{ json: { status: 'drafts_ready', queued_local: j.drafts.map(d => d.invoice), drafts: j.drafts, call_the_customer: j.disputed, review_queue: j.review_queue } }];"
   },
   "id": "local-stub-(drafts-only)",
   "name": "Local stub (drafts only)",
   "type": "n8n-nodes-base.code",
   "typeVersion": 2,
   "position": [
    840,
    -100
   ]
  },
  {
   "parameters": {
    "jsCode": "const j = $input.first().json;\nreturn [{ json: { ...(j.hint ? { approval_hint: j.hint } : {}), status: 'held_for_approval', held_for_owner: j.drafts.filter(d => d.tier === 'final').map(d => d.invoice), drafts: j.drafts, call_the_customer: j.disputed, review_queue: j.review_queue } }];"
   },
   "id": "hold-for-approval",
   "name": "Hold for approval",
   "type": "n8n-nodes-base.code",
   "typeVersion": 2,
   "position": [
    840,
    100
   ]
  },
  {
   "parameters": {
    "respondWith": "json",
    "responseBody": "={{ $json }}",
    "options": {}
   },
   "id": "respond:-released",
   "name": "Respond: released",
   "type": "n8n-nodes-base.respondToWebhook",
   "typeVersion": 1.1,
   "position": [
    1080,
    -100
   ]
  },
  {
   "parameters": {
    "respondWith": "json",
    "responseBody": "={{ $json }}",
    "options": {}
   },
   "id": "respond:-held",
   "name": "Respond: held",
   "type": "n8n-nodes-base.respondToWebhook",
   "typeVersion": 1.1,
   "position": [
    1080,
    100
   ]
  },
  {
   "parameters": {
    "content": "APPROVAL GATE (demo): the owner approves every reminder before it is queued (approved=true); nothing is released without it, final notices included. Invoices with an unreadable or impossible date go to a review queue and are never given a tier. The request field approved stands in for a person: in real use replace the Approval check node with a separate, authenticated approval bound to the draft, never model output. Synthetic data only. No node here sends anything outside this machine; the 'AI step' is a deterministic stand-in to swap for an AI node.",
    "height": 230,
    "width": 440
   },
   "id": "note",
   "name": "Note",
   "type": "n8n-nodes-base.stickyNote",
   "typeVersion": 1,
   "position": [
    560,
    -220
   ]
  }
 ],
 "connections": {
  "Webhook (synthetic input)": {
   "main": [
    [
     {
      "node": "Extract and decide",
      "type": "main",
      "index": 0
     }
    ]
   ]
  },
  "Extract and decide": {
   "main": [
    [
     {
      "node": "Approval check (demo toggle)",
      "type": "main",
      "index": 0
     }
    ]
   ]
  },
  "Approval check (demo toggle)": {
   "main": [
    [
     {
      "node": "Approval gate",
      "type": "main",
      "index": 0
     }
    ]
   ]
  },
  "Approval gate": {
   "main": [
    [
     {
      "node": "Local stub (drafts only)",
      "type": "main",
      "index": 0
     }
    ],
    [
     {
      "node": "Hold for approval",
      "type": "main",
      "index": 0
     }
    ]
   ]
  },
  "Local stub (drafts only)": {
   "main": [
    [
     {
      "node": "Respond: released",
      "type": "main",
      "index": 0
     }
    ]
   ]
  },
  "Hold for approval": {
   "main": [
    [
     {
      "node": "Respond: held",
      "type": "main",
      "index": 0
     }
    ]
   ]
  }
 },
 "settings": {
  "executionOrder": "v1",
  "saveDataSuccessExecution": "none",
  "saveDataErrorExecution": "none"
 },
 "id": "twwpuc0887",
 "active": false,
 "pinData": {},
 "tags": []
}