{
 "name": "TWWP: inbound lead qualifier with reply or decline draft (UC-0189)",
 "nodes": [
  {
   "parameters": {
    "httpMethod": "POST",
    "path": "twwp-lead-qualification-from-inbound-mail-0189",
    "responseMode": "responseNode",
    "options": {}
   },
   "id": "webhook",
   "name": "Webhook (synthetic input)",
   "type": "n8n-nodes-base.webhook",
   "typeVersion": 2,
   "position": [
    0,
    0
   ],
   "webhookId": "0a36295f-8e2c-5da0-8e4e-33b148042ad0"
  },
  {
   "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 text = String(b.text || '');\nconst num = v => (v === null || v === undefined || (typeof v === 'string' && v.trim() === '')) ? NaN : Number(v);\nconst budget = num(b.budget_eur), days = num(b.days_needed);\nconst referral = /\\b(referral|referred|existing client|worked with you before)\\b/i.test(text);\nlet fit, reason;\nif (referral) { fit = 'route_to_owner'; reason = 'referral or existing client'; }\nelse if (!Number.isFinite(budget) || !Number.isFinite(days)) { fit = 'route_to_owner'; reason = 'budget or timeline missing or not a number: a person decides'; }\nelse if (budget < 1500) { fit = 'low'; reason = 'budget below the 1500 EUR minimum'; }\nelse if (days < 14) { fit = 'low'; reason = 'timeline shorter than 14 days'; }\nelse { fit = 'high'; reason = 'meets budget and timeline rules'; }\nconst draft_type = fit === 'high' ? 'reply' : (fit === 'low' ? 'decline' : 'none');\nreturn [{ json: { hint, fit, reason, draft_type, crm_status: fit === 'low' ? 'declined - ' + (budget < 1500 ? 'budget' : 'timeline') : (fit === 'high' ? 'qualified' : 'owner review'), hold_reasons: fit === 'route_to_owner' ? [reason] : [] } }];"
   },
   "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: 'draft_approved_local', fit: j.fit, draft_type: j.draft_type, crm_status: j.crm_status, sent: false } }];"
   },
   "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: j.fit === 'route_to_owner' ? 'routed_to_owner' : 'draft_pending_approval', fit: j.fit, reason: j.reason, draft_type: j.draft_type, crm_status: j.crm_status } }];"
   },
   "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 freelancer approves the draft before it goes anywhere (approved=true). Referrals and existing clients always go to the freelancer. 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": "twwpuc0189",
 "active": false,
 "pinData": {},
 "tags": []
}