{
  "meta": {
    "templateCredsSetupCompleted": true
  },
  "nodes": [
    {
      "id": "655b90b2-0e86-4bef-adfd-0a5f1525c3ee",
      "name": "Webhook",
      "type": "n8n-nodes-base.webhook",
      "position": [
        0,
        0
      ],
      "parameters": {
        "path": "signsnaphome-sign-in-trigger",
        "options": {},
        "httpMethod": "POST"
      },
      "typeVersion": 2.1
    },
    {
      "id": "5e350a04-18b9-4dc0-b201-c0ac2022f0e3",
      "name": "Parse & Enrich Data",
      "type": "n8n-nodes-base.code",
      "position": [
        176,
        0
      ],
      "parameters": {
        "jsCode": "// Extract webhook data from body\nconst items = $input.all();\nconst webhookData = items[0].json.body;\n\n// Standard fields that might exist\nconst standardFields = [\n  'openHouseTitle',\n  'openHouseType', \n  'submissionTimestamp',\n  'guestPhotoUrl',\n  'first_name',\n  'last_name',\n  'phone_number',\n  'email',\n  'are_you_currently_working_with_an_agent',\n  'what_did_you_rate_the_house',\n  'do_you_have_a_signed_buyer_agreement',\n  'webhookUrl',\n  'executionMode'\n];\n\n// Separate standard and custom fields\nconst standardData = {};\nconst customFields = {};\n\nfor (const [key, value] of Object.entries(webhookData)) {\n  if (standardFields.includes(key)) {\n    standardData[key] = value;\n  } else {\n    customFields[key] = value;\n  }\n}\n\n// Format timestamp\nconst timestamp = standardData.submissionTimestamp ? \n  new Date(standardData.submissionTimestamp).toLocaleString('en-US', {\n    dateStyle: 'full',\n    timeStyle: 'short'\n  }) : 'Not provided';\n\n// Check if rating field exists\nconst hasRating = standardData.what_did_you_rate_the_house !== undefined && \n                  standardData.what_did_you_rate_the_house !== null &&\n                  standardData.what_did_you_rate_the_house !== '';\n\nconst rating = hasRating ? parseInt(standardData.what_did_you_rate_the_house) : null;\n\n// Determine lead priority based on agent status and rating (if available)\nlet leadPriority = 'MEDIUM';\nlet embedColor = 0xFFA500; // Orange for medium\n\nconst hasAgent = standardData.are_you_currently_working_with_an_agent?.toLowerCase();\nconst hasBuyerAgreement = standardData.do_you_have_a_signed_buyer_agreement?.toLowerCase();\n\n// HOT lead: No agent and high rating (or no rating field but no agent)\nif (hasAgent === 'no' && (!hasRating || rating >= 4)) {\n  leadPriority = 'HOT';\n  embedColor = 0xFF0000; // Red for hot\n} \n// COLD lead: Has agent with agreement OR low rating\nelse if ((hasAgent === 'yes' && hasBuyerAgreement === 'yes') || (hasRating && rating <= 2)) {\n  leadPriority = 'COLD';\n  embedColor = 0x0099FF; // Blue for cold\n}\n// WARM lead: Has agent but no agreement\nelse if (hasAgent === 'yes' && hasBuyerAgreement === 'no') {\n  leadPriority = 'WARM';\n  embedColor = 0xFFA500; // Orange for warm\n}\n\n// Process base64 image\nlet imageBase64 = null;\nlet hasPhoto = false;\nlet imageMimeType = 'image/jpeg';\n\nif (standardData.guestPhotoUrl && standardData.guestPhotoUrl.startsWith('data:image')) {\n  try {\n    // Extract mime type\n    const mimeMatch = standardData.guestPhotoUrl.match(/data:(image\\/[a-zA-Z]+);base64,/);\n    if (mimeMatch) {\n      imageMimeType = mimeMatch[1];\n    }\n    \n    // Extract base64 data\n    const base64Data = standardData.guestPhotoUrl.split(',')[1];\n    imageBase64 = base64Data;\n    hasPhoto = true;\n  } catch (error) {\n    console.log('Error processing image:', error.message);\n  }\n}\n\n// Build custom fields display\nconst customFieldsArray = Object.entries(customFields)\n  .filter(([key, value]) => value !== null && value !== undefined && value !== '')\n  .map(([key, value]) => {\n    const formattedKey = key.replace(/_/g, ' ')\n      .split(' ')\n      .map(word => word.charAt(0).toUpperCase() + word.slice(1))\n      .join(' ');\n    return { name: formattedKey, value: String(value), inline: true };\n  });\n\nreturn [{\n  json: {\n    // Core guest data\n    firstName: standardData.first_name || 'Not provided',\n    lastName: standardData.last_name || 'Not provided',\n    fullName: `${standardData.first_name || ''} ${standardData.last_name || ''}`.trim() || 'Anonymous Guest',\n    email: standardData.email || null,\n    phone: standardData.phone_number || null,\n    \n    // Property info\n    propertyAddress: standardData.openHouseTitle || 'Unknown Property',\n    openHouseType: standardData.openHouseType || 'open-house',\n    \n    // Engagement data\n    hasRating: hasRating,\n    rating: rating,\n    ratingDisplay: hasRating ? `${rating}/5` : 'Not rated',\n    hasAgent: standardData.are_you_currently_working_with_an_agent || 'Not specified',\n    hasBuyerAgreement: standardData.do_you_have_a_signed_buyer_agreement || null,\n    timestamp: timestamp,\n    rawTimestamp: standardData.submissionTimestamp,\n    \n    // Lead scoring\n    leadPriority: leadPriority,\n    embedColor: embedColor,\n    \n    // Image data\n    hasPhoto: hasPhoto,\n    imageBase64: imageBase64,\n    imageMimeType: imageMimeType,\n    \n    // Custom fields\n    customFields: customFieldsArray,\n    hasCustomFields: customFieldsArray.length > 0,\n    \n    // Contact preferences\n    hasEmail: !!standardData.email,\n    hasPhone: !!standardData.phone_number,\n    \n    // Raw data for reference\n    rawData: webhookData\n  }\n}];"
      },
      "typeVersion": 2
    },
    {
      "id": "51e8b880-f380-4539-b1c9-980e2260454d",
      "name": "Convert Image to Binary",
      "type": "n8n-nodes-base.moveBinaryData",
      "position": [
        400,
        0
      ],
      "parameters": {
        "mode": "jsonToBinary",
        "options": {
          "encoding": "base64",
          "fileName": "={{ $json.fullName.replace(/ /g, '_') }}_photo.jpg",
          "mimeType": "={{ $json.imageMimeType }}"
        },
        "sourceKey": "imageBase64",
        "convertAllData": false,
        "destinationKey": "photo"
      },
      "typeVersion": 1.1
    },
    {
      "id": "78954bf0-f3d9-4af5-bddc-dbcb1a77e8c9",
      "name": "Discord Notification",
      "type": "n8n-nodes-base.httpRequest",
      "notes": "Sends to Discord with photo thumbnail",
      "position": [
        624,
        0
      ],
      "parameters": {
        "url": "YOUR_WEBHOOK_URL_HERE",
        "method": "POST",
        "options": {},
        "sendBody": true,
        "contentType": "multipart-form-data",
        "bodyParameters": {
          "parameters": [
            {
              "name": "payload_json",
              "value": "={{ \n  (function() {\n    // Build fields array\n    const fields = [\n      {\n        \"name\": \"\ud83d\udc64 Name\",\n        \"value\": $json.fullName,\n        \"inline\": true\n      },\n      {\n        \"name\": \"\ud83c\udfe2 Has Agent?\",\n        \"value\": $json.hasAgent,\n        \"inline\": true\n      }\n    ];\n    \n    // Add rating field only if it exists\n    if ($json.hasRating) {\n      fields.push({\n        \"name\": \"\u2b50 Rating\",\n        \"value\": $json.ratingDisplay,\n        \"inline\": true\n      });\n    }\n    \n    // Add buyer agreement if available\n    if ($json.hasBuyerAgreement) {\n      fields.push({\n        \"name\": \"\ud83d\udcdd Buyer Agreement\",\n        \"value\": $json.hasBuyerAgreement,\n        \"inline\": true\n      });\n    }\n    \n    // Add contact info\n    fields.push(\n      {\n        \"name\": \"\ud83d\udce7 Email\",\n        \"value\": $json.email || 'Not provided',\n        \"inline\": true\n      },\n      {\n        \"name\": \"\ud83d\udcf1 Phone\",\n        \"value\": $json.phone || 'Not provided',\n        \"inline\": true\n      },\n      {\n        \"name\": \"\ud83d\udd52 Visit Time\",\n        \"value\": $json.timestamp,\n        \"inline\": false\n      }\n    );\n    \n    // Add custom fields if any\n    if ($json.customFields && $json.customFields.length > 0) {\n      fields.push(...$json.customFields);\n    }\n    \n    const payload = {\n      \"embeds\": [\n        {\n          \"title\": \"\ud83c\udfe0 New Open House Visitor - \" + $json.leadPriority + \" LEAD\",\n          \"description\": \"**\" + $json.fullName + \"** visited **\" + $json.propertyAddress + \"**\",\n          \"color\": $json.embedColor,\n          \"fields\": fields,\n          \"footer\": {\n            \"text\": \"SignSnap Home | Lead Priority: \" + $json.leadPriority\n          },\n          \"timestamp\": $json.rawTimestamp\n        }\n      ]\n    };\n    \n    // Add thumbnail if photo exists (will use attachment)\n    if ($json.hasPhoto) {\n      payload.embeds[0].thumbnail = {\n        \"url\": \"attachment://\" + $json.fullName.replace(/ /g, '_') + \"_photo.jpg\"\n      };\n    }\n    \n    return JSON.stringify(payload);\n  })()\n}}"
            },
            {
              "name": "files[0]",
              "parameterType": "formBinaryData",
              "inputDataFieldName": "photo"
            }
          ]
        }
      },
      "typeVersion": 4.2,
      "alwaysOutputData": true
    },
    {
      "id": "36952b83-ea4c-4001-a53b-a179c5dea94e",
      "name": "Has Phone Number?",
      "type": "n8n-nodes-base.if",
      "position": [
        848,
        0
      ],
      "parameters": {
        "options": {},
        "conditions": {
          "options": {
            "version": 2,
            "leftValue": "",
            "caseSensitive": true,
            "typeValidation": "strict"
          },
          "combinator": "and",
          "conditions": [
            {
              "id": "phone-check",
              "operator": {
                "type": "boolean",
                "operation": "equals"
              },
              "leftValue": "={{ $('Parse & Enrich Data').item.json.hasPhone }}",
              "rightValue": true
            }
          ]
        }
      },
      "typeVersion": 2.2
    },
    {
      "id": "c2e72a81-eb01-4ebc-bade-f1aa0735c1d9",
      "name": "Send SMS (Twilio)",
      "type": "n8n-nodes-base.twilio",
      "notes": "Configure Twilio credentials in n8n",
      "position": [
        1056,
        -112
      ],
      "parameters": {
        "to": "={{ $('Parse & Enrich Data').item.json.phone }}",
        "from": "={{ $credentials.twilioPhoneNumber }}",
        "message": "=Hi {{ $('Parse & Enrich Data').item.json.firstName }}! Thank you for visiting {{ $('Parse & Enrich Data').item.json.propertyAddress }} today. {{ $json.embeds[0].fields[1].value === 'No' ? \"We'd love to help you find your perfect home! Our experienced team can provide exclusive listings and personalized service. Reply to schedule a private consultation!\" : \"Hope you enjoy the property! Feel free to reach out if you have any additional questions.\" }}",
        "options": {}
      },
      "typeVersion": 1
    },
    {
      "id": "4c73efc1-4cc0-4eaa-8f5c-09b59b9a4958",
      "name": "Has Email?",
      "type": "n8n-nodes-base.if",
      "position": [
        1056,
        96
      ],
      "parameters": {
        "options": {},
        "conditions": {
          "options": {
            "version": 2,
            "leftValue": "",
            "caseSensitive": true,
            "typeValidation": "strict"
          },
          "combinator": "and",
          "conditions": [
            {
              "id": "email-check",
              "operator": {
                "type": "boolean",
                "operation": "equals"
              },
              "leftValue": "={{ $('Parse & Enrich Data').item.json.hasEmail }}",
              "rightValue": true
            }
          ]
        }
      },
      "typeVersion": 2.2
    },
    {
      "id": "c02d15c4-0254-4dff-82a8-603eeda2982d",
      "name": "Send Welcome Email",
      "type": "n8n-nodes-base.emailSend",
      "notes": "Configure SMTP credentials in n8n",
      "position": [
        1280,
        96
      ],
      "parameters": {
        "html": "=Hi {{ $('Parse & Enrich Data').item.json.firstName }}!\n\nThank you for visiting {{ $('Parse & Enrich Data').item.json.propertyAddress }} today. \n{{ $json.embeds[0].fields[1].value === 'No' ? \"We'd love to help you find your perfect home! Our experienced team can provide exclusive listings and personalized service. Reply to schedule a private consultation!\" : \"Hope you enjoy the property! Feel free to reach out if you have any additional questions.\" }}\n\n{SET YOUR TEAM OR PERSONAL FOOTER HERE}",
        "options": {},
        "subject": "=Thank You for Visiting {{ $('Parse & Enrich Data').item.json.propertyAddress }}!",
        "toEmail": "={{ $('Parse & Enrich Data').item.json.email }}",
        "fromEmail": "YOUR_EMAIL_HERE"
      },
      "typeVersion": 2.1
    },
    {
      "id": "2f817a77-00c0-48ca-b640-800559834b2c",
      "name": "Sticky Note",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -240,
        -304
      ],
      "parameters": {
        "height": 256,
        "content": "## Recieve Sign In Data from Sign Snap Home\n**Start Free Here** signsnaphome.com\n\nSet up a custom webhook and set it to auto fire! Use the link below to paste into Sign Snap Home."
      },
      "typeVersion": 1
    },
    {
      "id": "a3a41519-d806-4582-ad58-2d6834c1a663",
      "name": "Sticky Note1",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        144,
        -192
      ],
      "parameters": {
        "width": 160,
        "height": 128,
        "content": "## Check and Enrich Sign In Data\n"
      },
      "typeVersion": 1
    },
    {
      "id": "d5fc183c-7fdd-4f6e-8856-43bb21b834c8",
      "name": "Sticky Note2",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        368,
        -160
      ],
      "parameters": {
        "width": 150,
        "height": 96,
        "content": "Convert Guest Photo from Base64 to Binary for Discord Send"
      },
      "typeVersion": 1
    },
    {
      "id": "7aa663fa-1756-4f0e-bb98-d4defecd8481",
      "name": "Sticky Note3",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        608,
        -336
      ],
      "parameters": {
        "width": 150,
        "height": 272,
        "content": "## Send to Discord\n\nAdd your Webhook URL Below. Great to get updates for team member sign ins or if you're chatting with another guest."
      },
      "typeVersion": 1
    },
    {
      "id": "39a19190-f7cb-43bb-8c78-475e86d1c7d0",
      "name": "Sticky Note4",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        880,
        -384
      ],
      "parameters": {
        "width": 624,
        "height": 192,
        "content": "## Check if Phone is provided\n**Otherwise Send Email** \n\nCheck the Send SMS and Send Email to customize your messaging. If guest does not have agent they recieve longer custom messaging saying we'd love to help them. \n\nEdit the if no part of the josn to change the if no agent response. Make sure you preview and test your use cases."
      },
      "typeVersion": 1
    }
  ],
  "connections": {
    "Webhook": {
      "main": [
        [
          {
            "node": "Parse & Enrich Data",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Has Email?": {
      "main": [
        [
          {
            "node": "Send Welcome Email",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Has Phone Number?": {
      "main": [
        [
          {
            "node": "Send SMS (Twilio)",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Has Email?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Parse & Enrich Data": {
      "main": [
        [
          {
            "node": "Convert Image to Binary",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Discord Notification": {
      "main": [
        [
          {
            "node": "Has Phone Number?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Convert Image to Binary": {
      "main": [
        [
          {
            "node": "Discord Notification",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}