{
  "name": "Workflow B: User-Loan Matching",
  "nodes": [
    {
      "parameters": {
        "path": "user-matching",
        "responseMode": "responseNode",
        "options": {}
      },
      "name": "Webhook Trigger",
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 1,
      "position": [
        250,
        300
      ]
    },
    {
      "parameters": {
        "operation": "executeQuery",
        "query": "-- Stage 1: SQL Pre-filter (Fast Path)\nSELECT \n  u.user_id,\n  u.email,\n  u.monthly_income,\n  u.credit_score,\n  u.age,\n  u.employment_status,\n  p.product_id,\n  p.product_name,\n  p.lender_name,\n  p.interest_rate,\n  p.min_income,\n  p.min_credit_score\nFROM users u\nCROSS JOIN loan_products p\nWHERE u.monthly_income >= p.min_income\n  AND u.credit_score >= p.min_credit_score\n  AND u.age BETWEEN p.min_age AND p.max_age\n  AND (p.employment_required = FALSE OR u.employment_status IN ('employed', 'self-employed'))\nORDER BY u.user_id, p.interest_rate ASC"
      },
      "name": "Stage 1: SQL Pre-filter",
      "type": "n8n-nodes-base.postgres",
      "typeVersion": 1,
      "position": [
        450,
        300
      ],
      "credentials": {
        "postgres": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "functionCode": "// Stage 2: Rule-based Scoring\nconst matches = items.map(item => {\n  const data = item.json;\n  \n  // Calculate match score (0-100)\n  const incomeScore = Math.min(30, (data.monthly_income - data.min_income) / data.min_income * 30);\n  const creditScore = Math.min(70, (data.credit_score - data.min_credit_score) / (850 - data.min_credit_score) * 70);\n  const matchScore = Math.round(incomeScore + creditScore);\n  \n  // Generate match reason\n  const reason = `User exceeds minimum income by ${Math.round((data.monthly_income / data.min_income - 1) * 100)}% and has credit score ${data.credit_score} (min: ${data.min_credit_score})`;\n  \n  return {\n    json: {\n      user_id: data.user_id,\n      product_id: data.product_id,\n      match_score: matchScore,\n      match_reason: reason,\n      ai_enhanced: false\n    }\n  };\n});\n\nreturn matches.filter(m => m.json.match_score >= 60); // Filter out low matches"
      },
      "name": "Stage 2: Rule-based Scoring",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        650,
        300
      ]
    },
    {
      "parameters": {
        "operation": "insert",
        "schema": "public",
        "table": "matches",
        "columns": "user_id,product_id,match_score,match_reason,ai_enhanced"
      },
      "name": "Stage 3: Save Matches to DB",
      "type": "n8n-nodes-base.postgres",
      "typeVersion": 1,
      "position": [
        850,
        300
      ],
      "credentials": {
        "postgres": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "respondWith": "json",
        "responseBody": "={{ JSON.stringify({success: true, matches_created: $items().length}) }}"
      },
      "name": "Respond to Webhook",
      "type": "n8n-nodes-base.respondToWebhook",
      "typeVersion": 1,
      "position": [
        1050,
        300
      ]
    }
  ],
  "connections": {
    "Webhook Trigger": {
      "main": [
        [
          {
            "node": "Stage 1: SQL Pre-filter",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Stage 1: SQL Pre-filter": {
      "main": [
        [
          {
            "node": "Stage 2: Rule-based Scoring",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Stage 2: Rule-based Scoring": {
      "main": [
        [
          {
            "node": "Stage 3: Save Matches to DB",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Stage 3: Save Matches to DB": {
      "main": [
        [
          {
            "node": "Respond to Webhook",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "active": false,
  "settings": {},
  "id": "workflow-b"
}