{
  "nodes": [
    {
      "id": "64d1ae53-8868-4b7f-8e9b-68626d15aef3",
      "name": "Capture Lead",
      "type": "n8n-nodes-base.webhook",
      "position": [
        -1392,
        288
      ],
      "parameters": {
        "path": "property-match",
        "options": {},
        "httpMethod": "POST",
        "responseMode": "responseNode"
      },
      "typeVersion": 2.1
    },
    {
      "id": "3bdb4234-d07e-43ae-9aac-d1c1eb5d7d8f",
      "name": "Structure & Clean Data",
      "type": "n8n-nodes-base.set",
      "position": [
        -1200,
        288
      ],
      "parameters": {
        "options": {},
        "assignments": {
          "assignments": [
            {
              "id": "2f5c08e6-480b-419e-b077-6eb8f741ded3",
              "name": "body.budget",
              "type": "string",
              "value": "={{ $json.body.budget }}"
            },
            {
              "id": "90e7fc8d-2188-45b9-847c-8ce3caa77fd0",
              "name": "body.city",
              "type": "string",
              "value": "={{ $json.body.city }}"
            },
            {
              "id": "08fbe007-e278-4a6d-9abb-e61c01657e56",
              "name": "body.property_type",
              "type": "string",
              "value": "={{ $json.body.type }}"
            }
          ]
        }
      },
      "typeVersion": 3.4
    },
    {
      "id": "fc5fd614-c9c4-4dcb-9e77-7f7a647c85cf",
      "name": "Formula Creation",
      "type": "n8n-nodes-base.code",
      "position": [
        -992,
        288
      ],
      "parameters": {
        "jsCode": "const input = items[0].json.body || items[0].json;\n\n// --- User Inputs ---\nconst searchTerm = (input.property_type || \"\").trim(); // e.g. \"3BHK\"\nconst city = (input.city || \"\").replace(/\"/g, '\\\\\"');\nconst budgetStr = (input.budget || \"\").toString().replace(/[\u20b9,]/g, \"\");\nconst budget = parseInt(budgetStr, 10);\n\n// --- Budget range (\u00b15%) ---\nconst lowerLimit = Math.floor(budget * 0.95);\nconst upperLimit = Math.ceil(budget * 1.05);\n\nconst formula = `AND(\n  FIND(LOWER(\"${searchTerm}\"), LOWER({Title})) > 0,\n  {Location} = \"${city}\",\n  OR(\n    VALUE(SUBSTITUTE(SUBSTITUTE({Price}, \"\u20b9\", \"\"), \",\", \"\")) <= ${budget},\n    AND(\n      VALUE(SUBSTITUTE(SUBSTITUTE({Price}, \"\u20b9\", \"\"), \",\", \"\")) >= ${lowerLimit},\n      VALUE(SUBSTITUTE(SUBSTITUTE({Price}, \"\u20b9\", \"\"), \",\", \"\")) <= ${upperLimit}\n    )\n  )\n)`;\n\n// --- Return ---\nreturn [{ json: { formula } }];\n"
      },
      "typeVersion": 2
    },
    {
      "id": "172dc29f-ee03-4c5f-b58d-f103fbb23d0a",
      "name": "Check Match Availability",
      "type": "n8n-nodes-base.if",
      "position": [
        -512,
        288
      ],
      "parameters": {
        "options": {},
        "conditions": {
          "options": {
            "version": 2,
            "leftValue": "",
            "caseSensitive": true,
            "typeValidation": "strict"
          },
          "combinator": "and",
          "conditions": [
            {
              "id": "f2371a14-812c-4ff5-b602-2ef349211b4f",
              "operator": {
                "type": "string",
                "operation": "notEmpty",
                "singleValue": true
              },
              "leftValue": "={{ $json.id }}",
              "rightValue": ""
            }
          ]
        }
      },
      "typeVersion": 2.2
    },
    {
      "id": "36fc778c-c07c-478a-8843-e72a3db96374",
      "name": "Generate Email Template",
      "type": "n8n-nodes-base.code",
      "position": [
        -288,
        208
      ],
      "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;\">\u2714\ufe0f ${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          \ud83d\udccd ${property.Location || \"-\"} | \ud83c\udfe0 ${property.Type || \"-\"}<br/>\n          \ud83d\udcb0 <strong>${property.Price || \"-\"}</strong><br/>\n          \ud83d\udecf\ufe0f ${property.Bedrooms || \"-\"} | \ud83d\udec1 ${property.Bathrooms || \"-\"} | \ud83d\udcd0 ${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          \ud83d\udc64 Suggested for: <em>${property[\"Suggested Buyer Type\"] || \"N/A\"}</em> <br/>\n          \ud83c\udff7\ufe0f 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;\">\ud83c\udfe1 Your Matched Properties</h1>\n        <p style=\"color:#666; font-size:14px;\">We\u2019ve 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        \u00a9 ${new Date().getFullYear()} Property Finder | All Rights Reserved\n      </td>\n    </tr>\n  </table>\n</body>\n</html>\n`;\n\nreturn [{ json: { html: emailHtml } }];"
      },
      "typeVersion": 2
    },
    {
      "id": "dcb25710-b773-4081-96f9-c6183cf08a4f",
      "name": "Send Property Details",
      "type": "n8n-nodes-base.gmail",
      "position": [
        0,
        304
      ],
      "parameters": {
        "sendTo": "={{ $('Capture Lead').first().json.body.email }}",
        "message": "={{ $json.html }}",
        "options": {
          "appendAttribution": false
        },
        "subject": "=\ud83c\udfe1 Properties Matching Your Requirements"
      },
      "credentials": {
        "gmailOAuth2": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 2.1
    },
    {
      "id": "72c5e30d-ebf1-4632-a555-2ee4c07d394f",
      "name": "Append Lead Data",
      "type": "n8n-nodes-base.airtable",
      "position": [
        320,
        208
      ],
      "parameters": {
        "base": {
          "__rl": true,
          "mode": "list",
          "value": "appe77qnWMLUWfKEe",
          "cachedResultUrl": "https://airtable.com/appe77qnWMLUWfKEe",
          "cachedResultName": "Property Matching(Real Estate)"
        },
        "table": {
          "__rl": true,
          "mode": "list",
          "value": "tbljB7wf7ccn3YjkE",
          "cachedResultUrl": "https://airtable.com/appe77qnWMLUWfKEe/tbljB7wf7ccn3YjkE",
          "cachedResultName": "Buyers"
        },
        "columns": {
          "value": {
            "City": "={{ $('Capture Lead').first().json.body.city }}",
            "Name": "={{ $('Capture Lead').first().json.body.name }}",
            "Email": "={{ $('Capture Lead').first().json.body.email }}",
            "Budget": "={{ $('Capture Lead').first().json.body.budget }}",
            "Status": "New",
            "Lead Source": "Other",
            "Matched Properties": "={{ $('Fetch Properties Required & \u00b1 5% budget range').first().json.Title }}",
            "Preferred Property Type": "={{ $('Capture Lead').first().json.body.type }}"
          },
          "schema": [
            {
              "id": "Name",
              "type": "string",
              "display": true,
              "removed": false,
              "readOnly": false,
              "required": false,
              "displayName": "Name",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "Email",
              "type": "string",
              "display": true,
              "removed": false,
              "readOnly": false,
              "required": false,
              "displayName": "Email",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "Budget",
              "type": "string",
              "display": true,
              "removed": false,
              "readOnly": false,
              "required": false,
              "displayName": "Budget",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "City",
              "type": "string",
              "display": true,
              "removed": false,
              "readOnly": false,
              "required": false,
              "displayName": "City",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "Preferred Property Type",
              "type": "string",
              "display": true,
              "removed": false,
              "readOnly": false,
              "required": false,
              "displayName": "Preferred Property Type",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "Matched Properties",
              "type": "string",
              "display": true,
              "removed": false,
              "readOnly": false,
              "required": false,
              "displayName": "Matched Properties",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "Lead Source",
              "type": "options",
              "display": true,
              "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"
                }
              ],
              "removed": false,
              "readOnly": false,
              "required": false,
              "displayName": "Lead Source",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "Status",
              "type": "options",
              "display": true,
              "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"
                }
              ],
              "removed": false,
              "readOnly": false,
              "required": false,
              "displayName": "Status",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            }
          ],
          "mappingMode": "defineBelow",
          "matchingColumns": [],
          "attemptToConvertTypes": false,
          "convertFieldsToString": false
        },
        "options": {},
        "operation": "create"
      },
      "typeVersion": 2.1
    },
    {
      "id": "36ef382d-1993-4ea7-9357-156fdde59faf",
      "name": "Notify Sales Agent",
      "type": "n8n-nodes-base.slack",
      "position": [
        320,
        368
      ],
      "parameters": {
        "text": "=:tada: New Lead Alert! :tada:\n\nA new lead has been captured. Here are the details:\n\n\ud83d\udc64 Name: {{ $('Capture Lead').first().json.body.name }}\n\n\ud83d\udcde Phone: {{ $('Capture Lead').first().json.body.phone }}\n\n\ud83d\udce7 Email: {{ $('Capture Lead').first().json.body.email }}\n\n\ud83d\udccd Location: {{ $('Capture Lead').first().json.body.city }}\n\n\ud83c\udfe0 Interest: {{ $('Capture Lead').first().json.body.type }}\n\nPlease reach out to this lead as soon as possible!",
        "select": "channel",
        "channelId": {
          "__rl": true,
          "mode": "list",
          "value": "C090F70N52M",
          "cachedResultName": "website-uptime"
        },
        "otherOptions": {}
      },
      "credentials": {
        "slackApi": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 2.3
    },
    {
      "id": "9d189a53-c713-4823-9f09-b6c722bf371e",
      "name": "Send Lead Confirmation Message",
      "type": "n8n-nodes-base.respondToWebhook",
      "position": [
        320,
        528
      ],
      "parameters": {
        "options": {},
        "respondWith": "json",
        "responseBody": "{\n  \"Response\": \"Great news! We\u2019ve found matching properties so Please check your email \u2014 we\u2019ve sent the property details \ud83d\udce9\"\n}"
      },
      "typeVersion": 1.4
    },
    {
      "id": "9875183f-c879-4036-b048-531dcab34af9",
      "name": "No Properties Found Respond",
      "type": "n8n-nodes-base.respondToWebhook",
      "position": [
        -288,
        352
      ],
      "parameters": {
        "options": {},
        "respondWith": "json",
        "responseBody": "{\n  \"Response\": \"Sorry, we couldn\u2019t find any properties matching your exact requirements. Please try adjusting your budget or property preferences\"\n}"
      },
      "typeVersion": 1.4
    },
    {
      "id": "bf56b3b0-9c66-47ff-86d7-76be428ec26f",
      "name": "Sticky Note",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -1888,
        480
      ],
      "parameters": {
        "color": 5,
        "width": 432,
        "height": 352,
        "content": "## Sample Data & Resources\n\nhttps://airtable.com/appe77qnWMLUWfKEe/shrQ9t6tboCuNkswq\n\n### Webhook Payload:\n\n{\n    \"name\":\"Suresh\",\n    \"email\":\"johndeo@gmail.com\",\n    \"city\": \"Delhi\",\n    \"phone\":\"9836462538\",\n    \"type\": \"3BHK Apartment\",\n    \"budget\": \"4300000\" \n}"
      },
      "typeVersion": 1
    },
    {
      "id": "5f7bfac5-2d21-4c20-9f01-828291f39b86",
      "name": "Fetch Properties Required & \u00b1 5% budget range",
      "type": "n8n-nodes-base.airtable",
      "position": [
        -736,
        288
      ],
      "parameters": {
        "base": {
          "__rl": true,
          "mode": "list",
          "value": "appe77qnWMLUWfKEe",
          "cachedResultUrl": "https://airtable.com/appe77qnWMLUWfKEe",
          "cachedResultName": "Property Matching(Real Estate)"
        },
        "table": {
          "__rl": true,
          "mode": "list",
          "value": "tblWdmd25jGWaqPd3",
          "cachedResultUrl": "https://airtable.com/appe77qnWMLUWfKEe/tblWdmd25jGWaqPd3",
          "cachedResultName": "Properties"
        },
        "options": {},
        "operation": "search",
        "filterByFormula": "={{ $json.formula }}"
      },
      "typeVersion": 2.1,
      "alwaysOutputData": true
    },
    {
      "id": "c8209aa6-488f-4606-9b9f-89b3472cacf3",
      "name": "Workflow Overview",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -1888,
        96
      ],
      "parameters": {
        "width": 436,
        "height": 368,
        "content": "## How it works\n1. Captures and structures the lead data\n2. Builds a dynamic Airtable formula to search properties matching their criteria (exact budget or \u00b15% range)\n3. Fetches matching properties from Airtable\n4. If matches found: generates a beautiful HTML email with property cards and sends it to the lead\n5. Logs the lead in Airtable and notifies the sales team via Slack\n6. Returns appropriate confirmation messages\n\n## Setup steps\n1. **Airtable**: Connect your Airtable account \n2. **Gmail**: Authenticate Gmail OAuth2 \n3. **Slack**: Connect Slack OAuth2 "
      },
      "typeVersion": 1
    },
    {
      "id": "07fbbd79-70f4-4d3a-8f17-f25cd876be78",
      "name": "Lead Intake Section",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -1440,
        96
      ],
      "parameters": {
        "color": 7,
        "width": 600,
        "height": 412,
        "content": "## Lead Intake\nCaptures incoming lead data via webhook and structures it for processing (budget, city, property type)."
      },
      "typeVersion": 1
    },
    {
      "id": "d270e4b8-61f5-43da-872f-060000b53db3",
      "name": "Property Search Section",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -832,
        96
      ],
      "parameters": {
        "color": 7,
        "width": 712,
        "height": 412,
        "content": "## Property Search Engine\nBuilds Airtable formula with \u00b15% budget flexibility, searches properties database, and validates if matches exist."
      },
      "typeVersion": 1
    },
    {
      "id": "444b2b72-c453-4dc8-8a5f-dac5996acd97",
      "name": "Success Response Section",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -112,
        96
      ],
      "parameters": {
        "color": 7,
        "width": 816,
        "height": 604,
        "content": "## Success Flow\nGenerates HTML email with property cards, sends to lead, logs in Airtable, notifies sales team, and respond to webhook."
      },
      "typeVersion": 1
    }
  ],
  "connections": {
    "Capture Lead": {
      "main": [
        [
          {
            "node": "Structure & Clean Data",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Formula Creation": {
      "main": [
        [
          {
            "node": "Fetch Properties Required & \u00b1 5% budget range",
            "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
          }
        ]
      ]
    },
    "Structure & Clean Data": {
      "main": [
        [
          {
            "node": "Formula Creation",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Generate Email Template": {
      "main": [
        [
          {
            "node": "Send Property Details",
            "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
          }
        ]
      ]
    },
    "Fetch Properties Required & \u00b1 5% budget range": {
      "main": [
        [
          {
            "node": "Check Match Availability",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}