{
  "name": "Property Matching for Buyer Leads",
  "nodes": [
    {
      "parameters": {
        "httpMethod": "POST",
        "path": "property-match",
        "responseMode": "responseNode",
        "options": {}
      },
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 2.1,
      "position": [
        -256,
        112
      ],
      "id": "c1682b65-de1f-4141-a8f0-f0683d13a6b0",
      "name": "Capture Lead",
      "webhookId": "8df07b53-1e28-4913-a64c-227b32e1ef23"
    },
    {
      "parameters": {
        "assignments": {
          "assignments": [
            {
              "id": "2f5c08e6-480b-419e-b077-6eb8f741ded3",
              "name": "body.budget",
              "value": "={{ $json.body.budget }}",
              "type": "string"
            },
            {
              "id": "90e7fc8d-2188-45b9-847c-8ce3caa77fd0",
              "name": "body.city",
              "value": "={{ $json.body.city }}",
              "type": "string"
            },
            {
              "id": "08fbe007-e278-4a6d-9abb-e61c01657e56",
              "name": "body.property_type",
              "value": "={{ $json.body.type }}",
              "type": "string"
            }
          ]
        },
        "options": {}
      },
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "position": [
        -80,
        112
      ],
      "id": "7cb32004-d561-42e2-aa48-ae55850309dc",
      "name": "Structure & Clean Data"
    },
    {
      "parameters": {
        "jsCode": "const input = items[0].json.body || items[0].json;\n\n// --- User Inputs ---\nconst searchTerm = (input.property_type || \"\").trim(); // e.g. \"3-Bedroom\"\nconst city = (input.city || \"\").replace(/\"/g, '\\\\\"');\nconst budgetStr = (input.budget || \"\").toString().replace(/[$,]/g, \"\");\nconst budget = parseInt(budgetStr, 10);\n\n// --- Budget range (±5%) ---\nconst lowerLimit = Math.floor(budget * 0.95);\nconst upperLimit = Math.ceil(budget * 1.05);\n\n// --- Formula ---\n// 1. Title contains searchTerm\n// 2. Location = city\n// 3. Price <= budget  (exact)\n// OR\n// 4. Price within ±5% of budget\nconst formula = `AND(\n  FIND(LOWER(\"${searchTerm}\"), LOWER({Title})) > 0,\n  {Location} = \"${city}\",\n  OR(\n    VALUE(SUBSTITUTE(SUBSTITUTE({Price}, \"$\", \"\"), \",\", \"\")) <= ${budget},\n    AND(\n      VALUE(SUBSTITUTE(SUBSTITUTE({Price}, \"$\", \"\"), \",\", \"\")) >= ${lowerLimit},\n      VALUE(SUBSTITUTE(SUBSTITUTE({Price}, \"$\", \"\"), \",\", \"\")) <= ${upperLimit}\n    )\n  )\n)`;\n\n// --- Return ---\nreturn [{ json: { formula } }];\n"
      },
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        96,
        112
      ],
      "id": "3bee7627-9ace-41c9-902c-7f0e3cfa5571",
      "name": "Formula Creation"
    },
    {
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "leftValue": "",
            "typeValidation": "strict",
            "version": 2
          },
          "conditions": [
            {
              "id": "f2371a14-812c-4ff5-b602-2ef349211b4f",
              "leftValue": "={{ $json.id }}",
              "rightValue": "",
              "operator": {
                "type": "string",
                "operation": "notEmpty",
                "singleValue": true
              }
            }
          ],
          "combinator": "and"
        },
        "options": {}
      },
      "type": "n8n-nodes-base.if",
      "typeVersion": 2.2,
      "position": [
        448,
        112
      ],
      "id": "ce54b029-142c-4c0e-b050-6c6c5ac1ccce",
      "name": "Check Match Availability"
    },
    {
      "parameters": {
        "jsCode": "function generatePropertyCard(property) {\n  const images = (property.Images || []).map(img =>\n    `<img src=\"${img.url}\" alt=\"${property.Title}\" style=\"width:100%; max-width:250px; border-radius:8px; margin:5px;\" />`\n  ).join(\"\");\n\n  const features = (property.Features || []).map(f =>\n    `<li style=\"margin:4px 0;\">✔️ ${f}</li>`\n  ).join(\"\");\n\n  return `\n    <tr>\n      <td style=\"padding:20px; border:1px solid #ddd; border-radius:12px;\">\n        <h2 style=\"color:#2c3e50; margin-bottom:8px;\">${property.Title || \"Property\"}</h2>\n        <p style=\"margin:4px 0; font-size:14px; color:#555;\">\n          📍 ${property.Location || \"-\"} | 🏠 ${property.Type || \"-\"}<br/>\n          💰 <strong>${property.Price || \"-\"}</strong><br/>\n          🛏️ ${property.Bedrooms || \"-\"} | 🛁 ${property.Bathrooms || \"-\"} | 📐 ${property[\"Size (sqft)\"] || \"-\"} sqft\n        </p>\n        <div style=\"margin:10px 0; display:flex; flex-wrap:wrap; gap:8px;\">\n          ${images}\n        </div>\n        <ul style=\"padding-left:18px; margin:10px 0; font-size:14px; color:#333;\">\n          ${features}\n        </ul>\n        <p style=\"font-size:13px; color:#888;\">\n          👤 Suggested for: <em>${property[\"Suggested Buyer Type\"] || \"N/A\"}</em> <br/>\n          🏷️ Luxury Property: <strong>${property[\"Is Luxury Property?\"] || \"No\"}</strong>\n        </p>\n        <a href=\"https://your-website.com/property/${property.ID || \"\"}\" \n           style=\"background:#007bff; color:#fff; padding:10px 20px; border-radius:6px; text-decoration:none; font-size:14px;\">\n           View More Details\n        </a>\n      </td>\n    </tr>\n  `;\n}\n\n// Build full email HTML\nconst properties = items.map(i => i.json);\nconst emailHtml = `\n<!DOCTYPE html>\n<html>\n<head>\n  <meta charset=\"UTF-8\" />\n  <title>Matched Properties</title>\n</head>\n<body style=\"font-family:Arial,sans-serif; background:#f9f9f9; padding:20px;\">\n  <table align=\"center\" width=\"600\" style=\"background:#fff; padding:20px; border-radius:12px; border:1px solid #eee;\">\n    <tr>\n      <td>\n        <h1 style=\"color:#333;\">🏡 Your Matched Properties</h1>\n        <p style=\"color:#666; font-size:14px;\">We’ve found some great properties that match your preferences:</p>\n      </td>\n    </tr>\n    ${properties.map(generatePropertyCard).join(\"\")}\n    <tr>\n      <td style=\"padding:20px; text-align:center; font-size:12px; color:#999;\">\n        © ${new Date().getFullYear()} Property Finder | All Rights Reserved\n      </td>\n    </tr>\n  </table>\n</body>\n</html>\n`;\n\nreturn [{ json: { html: emailHtml } }];"
      },
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        656,
        16
      ],
      "id": "1d7b6fdc-7952-4251-b066-935d597fabde",
      "name": "Generate Email Template"
    },
    {
      "parameters": {
        "sendTo": "={{ $('Capture Lead').first().json.body.email }}",
        "subject": "=🏡 Properties Matching Your Requirements",
        "message": "={{ $json.html }}",
        "options": {
          "appendAttribution": false
        }
      },
      "type": "n8n-nodes-base.gmail",
      "typeVersion": 2.1,
      "position": [
        864,
        16
      ],
      "id": "a3ea37d6-eca1-43e0-a44f-9e5804646a0f",
      "name": "Send Property Details",
      "webhookId": "852f8e24-fa15-47af-8ca5-fa7f55ad963c",
      "credentials": {
        "gmailOAuth2": {
          "id": "emmuhpJGs7nAZSDK",
          "name": "Gmail account"
        }
      }
    },
    {
      "parameters": {
        "operation": "create",
        "base": {
          "__rl": true,
          "value": "appe77qnWMLUWfKEe",
          "mode": "list",
          "cachedResultName": "Property Matching(Real Estate)"
        },
        "table": {
          "__rl": true,
          "value": "tbljB7wf7ccn3YjkE",
          "mode": "list",
          "cachedResultName": "Buyers"
        },
        "columns": {
          "mappingMode": "defineBelow",
          "value": {
            "Name": "={{ $('Capture Lead').first().json.body.name }}",
            "Email": "={{ $('Capture Lead').first().json.body.email }}",
            "Budget": "={{ $('Capture Lead').first().json.body.budget }}",
            "City": "={{ $('Capture Lead').first().json.body.city }}",
            "Preferred Property Type": "={{ $('Capture Lead').first().json.body.type }}",
            "Status": "New",
            "Matched Properties": "={{ $('Fetch Properties Required & ± 5% budget range').first().json.Title }}",
            "Lead Source": "Other"
          },
          "matchingColumns": [],
          "schema": [
            {
              "id": "Name",
              "displayName": "Name",
              "required": false,
              "defaultMatch": false,
              "canBeUsedToMatch": true,
              "display": true,
              "type": "string",
              "readOnly": false,
              "removed": false
            },
            {
              "id": "Email",
              "displayName": "Email",
              "required": false,
              "defaultMatch": false,
              "canBeUsedToMatch": true,
              "display": true,
              "type": "string",
              "readOnly": false,
              "removed": false
            },
            {
              "id": "Budget",
              "displayName": "Budget",
              "required": false,
              "defaultMatch": false,
              "canBeUsedToMatch": true,
              "display": true,
              "type": "string",
              "readOnly": false,
              "removed": false
            },
            {
              "id": "City",
              "displayName": "City",
              "required": false,
              "defaultMatch": false,
              "canBeUsedToMatch": true,
              "display": true,
              "type": "string",
              "readOnly": false,
              "removed": false
            },
            {
              "id": "Preferred Property Type",
              "displayName": "Preferred Property Type",
              "required": false,
              "defaultMatch": false,
              "canBeUsedToMatch": true,
              "display": true,
              "type": "string",
              "readOnly": false,
              "removed": false
            },
            {
              "id": "Matched Properties",
              "displayName": "Matched Properties",
              "required": false,
              "defaultMatch": false,
              "canBeUsedToMatch": true,
              "display": true,
              "type": "string",
              "readOnly": false,
              "removed": false
            },
            {
              "id": "Lead Source",
              "displayName": "Lead Source",
              "required": false,
              "defaultMatch": false,
              "canBeUsedToMatch": true,
              "display": true,
              "type": "options",
              "options": [
                {
                  "name": "Website",
                  "value": "Website"
                },
                {
                  "name": "Referral",
                  "value": "Referral"
                },
                {
                  "name": "Agent",
                  "value": "Agent"
                },
                {
                  "name": "Walk-in",
                  "value": "Walk-in"
                },
                {
                  "name": "Social Media",
                  "value": "Social Media"
                },
                {
                  "name": "Other",
                  "value": "Other"
                }
              ],
              "readOnly": false,
              "removed": false
            },
            {
              "id": "Status",
              "displayName": "Status",
              "required": false,
              "defaultMatch": false,
              "canBeUsedToMatch": true,
              "display": true,
              "type": "options",
              "options": [
                {
                  "name": "New",
                  "value": "New"
                },
                {
                  "name": "Contacted",
                  "value": "Contacted"
                },
                {
                  "name": "Viewing Scheduled",
                  "value": "Viewing Scheduled"
                },
                {
                  "name": "Offer Made",
                  "value": "Offer Made"
                },
                {
                  "name": "Closed",
                  "value": "Closed"
                },
                {
                  "name": "Not Interested",
                  "value": "Not Interested"
                }
              ],
              "readOnly": false,
              "removed": false
            }
          ],
          "attemptToConvertTypes": false,
          "convertFieldsToString": false
        },
        "options": {}
      },
      "type": "n8n-nodes-base.airtable",
      "typeVersion": 2.1,
      "position": [
        1120,
        -176
      ],
      "id": "9715dc3e-5809-47ac-b027-18cba4bddd44",
      "name": "Append Lead Data",
      "credentials": {
        "airtableTokenApi": {
          "id": "0lstEdikXtvhmDPJ",
          "name": "Airtable account"
        }
      }
    },
    {
      "parameters": {
        "authentication": "oAuth2",
        "select": "channel",
        "channelId": {
          "__rl": true,
          "value": "C090F70N52M",
          "mode": "list",
          "cachedResultName": "website-uptime"
        },
        "text": "=:tada: New Lead Alert! :tada:\n\nA new lead has been captured. Here are the details:\n\n👤 Name: {{ $('Capture Lead').first().json.body.name }}\n\n📞 Phone: {{ $('Capture Lead').first().json.body.phone }}\n\n📧 Email: {{ $('Capture Lead').first().json.body.email }}\n\n📍 Location: {{ $('Capture Lead').first().json.body.city }}\n\n🏠 Interest: {{ $('Capture Lead').first().json.body.type }}\n\nPlease reach out to this lead as soon as possible!",
        "otherOptions": {}
      },
      "type": "n8n-nodes-base.slack",
      "typeVersion": 2.3,
      "position": [
        1120,
        16
      ],
      "id": "f0abf179-94b0-4a26-8e42-232e4f1af50a",
      "name": "Notify Sales Agent",
      "webhookId": "f747c77f-b87a-4a7c-8d3e-3fa5ff251442",
      "credentials": {
        "slackOAuth2Api": {
          "id": "TF3jowThH6PoCk5Q",
          "name": "Slack account"
        }
      }
    },
    {
      "parameters": {
        "respondWith": "json",
        "responseBody": "{\n  \"Response\": \"Great news! We’ve found matching properties so Please check your email — we’ve sent the property details 📩\"\n}",
        "options": {}
      },
      "type": "n8n-nodes-base.respondToWebhook",
      "typeVersion": 1.4,
      "position": [
        1120,
        208
      ],
      "id": "88bd2bb7-0903-45fe-abaa-21ac137915e6",
      "name": "Send Lead Confirmation Message"
    },
    {
      "parameters": {
        "respondWith": "json",
        "responseBody": "{\n  \"Response\": \"Sorry, we couldn’t find any properties matching your exact requirements. Please try adjusting your budget or property preferences\"\n}",
        "options": {}
      },
      "type": "n8n-nodes-base.respondToWebhook",
      "typeVersion": 1.4,
      "position": [
        656,
        160
      ],
      "id": "384efb6d-0cb4-4b42-870d-966412d7b513",
      "name": "No Properties Found Respond"
    },
    {
      "parameters": {
        "content": "## AirTable\n\nCreate an Airtable base with these tables/fields, then select it in the Airtable nodes:\n- **Property Matching(Real Estate) → Buyers**: `Name`, `Email`, `Budget`, `City`, `Preferred Property Type`, `Matched Properties`, `Lead Source`, `Status`\n- **Property Matching(Real Estate) → Properties**: _(columns as used in the nodes)_\n\n## Payload\n{\n    \"name\":\"John\",\n    \"email\":\"johndeo@gmail.com\",\n    \"city\": \"Austin\",\n    \"phone\":\"5125550123\",\n    \"type\": \"3-Bedroom Apartment\",\n    \"budget\": \"4300000\" \n}",
        "height": 384
      },
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -576,
        -144
      ],
      "typeVersion": 1,
      "id": "8e2186cb-695c-4fb7-a943-24a9b71440c4",
      "name": "Sticky Note"
    },
    {
      "parameters": {
        "operation": "search",
        "base": {
          "__rl": true,
          "value": "appe77qnWMLUWfKEe",
          "mode": "list",
          "cachedResultName": "Property Matching(Real Estate)"
        },
        "table": {
          "__rl": true,
          "value": "tblWdmd25jGWaqPd3",
          "mode": "list",
          "cachedResultName": "Properties"
        },
        "filterByFormula": "={{ $json.formula }}",
        "options": {}
      },
      "type": "n8n-nodes-base.airtable",
      "typeVersion": 2.1,
      "position": [
        256,
        112
      ],
      "id": "7068c960-46e6-476d-9b2b-a0cf80d5b3ed",
      "name": "Fetch Properties Required & ± 5% budget range",
      "alwaysOutputData": true,
      "credentials": {
        "airtableTokenApi": {
          "id": "0lstEdikXtvhmDPJ",
          "name": "Airtable account"
        }
      }
    }
  ],
  "pinData": {},
  "connections": {
    "Capture Lead": {
      "main": [
        [
          {
            "node": "Structure & Clean Data",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Structure & Clean Data": {
      "main": [
        [
          {
            "node": "Formula Creation",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Formula Creation": {
      "main": [
        [
          {
            "node": "Fetch Properties Required & ± 5% budget range",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Check Match Availability": {
      "main": [
        [
          {
            "node": "Generate Email Template",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "No Properties Found Respond",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Generate Email Template": {
      "main": [
        [
          {
            "node": "Send Property Details",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Send Property Details": {
      "main": [
        [
          {
            "node": "Append Lead Data",
            "type": "main",
            "index": 0
          },
          {
            "node": "Notify Sales Agent",
            "type": "main",
            "index": 0
          },
          {
            "node": "Send Lead Confirmation Message",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Append Lead Data": {
      "main": [
        []
      ]
    },
    "Notify Sales Agent": {
      "main": [
        []
      ]
    },
    "Fetch Properties Required & ± 5% budget range": {
      "main": [
        [
          {
            "node": "Check Match Availability",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "active": false,
  "settings": {
    "executionOrder": "v1"
  },
  "versionId": "355a35c0-8b9b-4b56-b51b-e709e9c14ad9",
  "meta": {
    "templateCredsSetupCompleted": true,
    "instanceId": "f1c26778ec36097a9722a22c47c5e9c01f00b8862dadd0121ec118026a370fa6"
  },
  "id": "8ie1PhIhMo0QQTb7",
  "tags": []
}