{
  "name": "Property Matching",
  "nodes": [
    {
      "parameters": {
        "httpMethod": "POST",
        "path": "property-match",
        "responseMode": "responseNode",
        "options": {}
      },
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 2.1,
      "position": [
        -256,
        112
      ],
      "id": "aed2feff-b169-43b1-86c7-52bb47a3ef2e",
      "name": "Capture Lead",
      "webhookId": "8d9e79f3-35f4-4837-b140-aba640ac43a1"
    },
    {
      "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": "30d32442-0db5-4f19-ac11-b2cc8c446045",
      "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. \"3BHK\"\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": "fe284a6c-1269-48c9-9332-471edf07a034",
      "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": "2df20f0a-726a-433d-81f0-fac43af47f17",
      "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": "41805845-0f45-4e16-b589-44bc11a09ee6",
      "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": "478c68c1-8a20-4089-914f-ee918cd0b42d",
      "name": "Send Property Details",
      "webhookId": "a8d68971-b0c4-4783-b12a-f4f57091ffa0",
      "credentials": {
        "gmailOAuth2": {
          "id": "YpJCpL7lQqKeGFdc",
          "name": "Suresh_Gmail account"
        }
      }
    },
    {
      "parameters": {
        "operation": "create",
        "base": {
          "__rl": true,
          "value": "appe77qnWMLUWfKEe",
          "mode": "list",
          "cachedResultName": "Property Matching(Real Estate)",
          "cachedResultUrl": "https://airtable.com/appe77qnWMLUWfKEe"
        },
        "table": {
          "__rl": true,
          "value": "tbljB7wf7ccn3YjkE",
          "mode": "list",
          "cachedResultName": "Buyers",
          "cachedResultUrl": "https://airtable.com/appe77qnWMLUWfKEe/tbljB7wf7ccn3YjkE"
        },
        "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": "113d5993-c617-44fd-92f2-72a93706ab9c",
      "name": "Append Lead Data",
      "credentials": {
        "airtableTokenApi": {
          "id": "X9oQ7gQXD99VMvtY",
          "name": "Suresh_Airtable Access Token"
        }
      }
    },
    {
      "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": "693875bd-a268-4251-9ee3-173b5a8e30ed",
      "name": "Notify Sales Agent",
      "webhookId": "63d9f8dc-8b33-46c5-b660-bba59a60d3e2",
      "credentials": {
        "slackOAuth2Api": {
          "id": "VgwGmlOcfqlGImrU",
          "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": "d5d13e69-44f6-481d-b6dc-8c0f33be9e3e",
      "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": "23b9276f-cb51-47f6-879b-76e0f633433b",
      "name": "No Properties Found Respond"
    },
    {
      "parameters": {
        "content": "## AirTable\n\nhttps://airtable.com/appe77qnWMLUWfKEe/shrQ9t6tboCuNkswq\n\n## Payload\n{\n    \"name\":\"Suresh\",\n    \"email\":\"johndeo@gmail.com\",\n    \"city\": \"Delhi\",\n    \"phone\":\"9836462538\",\n    \"type\": \"3BHK Apartment\",\n    \"budget\": \"4300000\" \n}",
        "height": 384
      },
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -576,
        -144
      ],
      "typeVersion": 1,
      "id": "fdb4f633-f9e1-4639-b823-b66b20c0b0c9",
      "name": "Sticky Note"
    },
    {
      "parameters": {
        "operation": "search",
        "base": {
          "__rl": true,
          "value": "appe77qnWMLUWfKEe",
          "mode": "list",
          "cachedResultName": "Property Matching(Real Estate)",
          "cachedResultUrl": "https://airtable.com/appe77qnWMLUWfKEe"
        },
        "table": {
          "__rl": true,
          "value": "tblWdmd25jGWaqPd3",
          "mode": "list",
          "cachedResultName": "Properties",
          "cachedResultUrl": "https://airtable.com/appe77qnWMLUWfKEe/tblWdmd25jGWaqPd3"
        },
        "filterByFormula": "={{ $json.formula }}",
        "options": {}
      },
      "type": "n8n-nodes-base.airtable",
      "typeVersion": 2.1,
      "position": [
        256,
        112
      ],
      "id": "a4e31e31-6fb0-48c2-b074-1ab4878699cf",
      "name": "Fetch Properties Required & ± 5% budget range",
      "alwaysOutputData": true,
      "credentials": {
        "airtableTokenApi": {
          "id": "X9oQ7gQXD99VMvtY",
          "name": "Suresh_Airtable Access Token"
        }
      }
    }
  ],
  "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": "1c992684-1170-4df2-a08d-f76ef2469008",
  "meta": {
    "templateCredsSetupCompleted": true,
    "instanceId": "9272721148ea09184b6bbb7ce6219dab088562dd450e2df8280d57c2e34c7d84"
  },
  "id": "g6ggRoPlhbilnk4Z",
  "tags": []
}