{
  "meta": {
    "templateCredsSetupCompleted": true
  },
  "nodes": [
    {
      "id": "YOUR_NODE_ID_FORM_SUBMISSION_RECEIVED",
      "name": "Form Submission Received",
      "type": "n8n-nodes-base.jotFormTrigger",
      "position": [
        0,
        144
      ],
      "parameters": {
        "form": "YOUR_JOTFORM_FORM_ID",
        "onlyAnswers": false
      },
      "credentials": {
        "jotFormApi": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 1
    },
    {
      "id": "YOUR_NODE_ID_EXTRACT_SUBMISSION_DATA",
      "name": "Extract Submission Data",
      "type": "n8n-nodes-base.set",
      "position": [
        224,
        144
      ],
      "parameters": {
        "options": {},
        "assignments": {
          "assignments": [
            {
              "id": "19a8ba9c-8dbf-43a0-8f27-ae642a62b2e4",
              "name": "formID",
              "type": "string",
              "value": "={{ $json.formID }}"
            },
            {
              "id": "ebf76643-886e-4840-9f9b-48c30d8b1274",
              "name": "submissionID",
              "type": "string",
              "value": "={{ $json.submissionID }}"
            },
            {
              "id": "892a5d16-9375-41ec-90bd-7cb9c580eb93",
              "name": "rawRequest",
              "type": "object",
              "value": "={{ $json.rawRequest }}"
            }
          ]
        }
      },
      "typeVersion": 3.4
    },
    {
      "id": "YOUR_NODE_ID_FETCH_ALL_FORM_SUBMISSIONS",
      "name": "Fetch All Form Submissions",
      "type": "n8n-nodes-base.httpRequest",
      "position": [
        448,
        144
      ],
      "parameters": {
        "url": "=https://api.jotform.com/form/{{ $json.formID }}/submissions",
        "options": {},
        "sendQuery": true,
        "queryParameters": {
          "parameters": [
            {
              "name": "apiKey",
              "value": "YOUR_JOTFORM_API_KEY"
            }
          ]
        }
      },
      "typeVersion": 4.2
    },
    {
      "id": "YOUR_NODE_ID_UNPACK_SUBMISSION_OBJECTS",
      "name": "Unpack Submission Objects",
      "type": "n8n-nodes-base.splitOut",
      "position": [
        672,
        144
      ],
      "parameters": {
        "include": "selectedOtherFields",
        "options": {},
        "fieldToSplitOut": "content",
        "fieldsToInclude": "content.id, content.answers"
      },
      "typeVersion": 1
    },
    {
      "id": "YOUR_NODE_ID_FILTER_ACTIVE_SUBMISSIONS",
      "name": "Filter Active Submissions",
      "type": "n8n-nodes-base.filter",
      "position": [
        896,
        144
      ],
      "parameters": {
        "options": {},
        "conditions": {
          "options": {
            "version": 2,
            "leftValue": "",
            "caseSensitive": true,
            "typeValidation": "strict"
          },
          "combinator": "and",
          "conditions": [
            {
              "id": "YOUR_CONDITION_ID_1",
              "operator": {
                "name": "filter.operator.equals",
                "type": "string",
                "operation": "equals"
              },
              "leftValue": "={{ $json.content.status }}",
              "rightValue": "=ACTIVE"
            },
            {
              "id": "YOUR_CONDITION_ID_2",
              "operator": {
                "name": "filter.operator.equals",
                "type": "string",
                "operation": "equals"
              },
              "leftValue": "={{ $json.content.answers[\"5\"].answer }}",
              "rightValue": "={{ $('Form Submission Received').item.json.rawRequest[\"E-mail\"] }}"
            }
          ]
        }
      },
      "typeVersion": 2.2
    },
    {
      "id": "YOUR_NODE_ID_CHECK_DUPLICATE_COUNT",
      "name": "Check Duplicate Count",
      "type": "n8n-nodes-base.if",
      "position": [
        1120,
        144
      ],
      "parameters": {
        "options": {},
        "conditions": {
          "options": {
            "version": 2,
            "leftValue": "",
            "caseSensitive": true,
            "typeValidation": "loose"
          },
          "combinator": "and",
          "conditions": [
            {
              "id": "YOUR_CONDITION_ID_1",
              "operator": {
                "name": "filter.operator.equals",
                "type": "string",
                "operation": "equals"
              },
              "leftValue": "={{ $json.content.answers[\"5\"].answer }}",
              "rightValue": "={{ $('Form Submission Received').item.json.rawRequest[\"E-mail\"] }}"
            },
            {
              "id": "YOUR_CONDITION_ID_2",
              "operator": {
                "name": "filter.operator.equals",
                "type": "string",
                "operation": "equals"
              },
              "leftValue": "={{ $input.all().length >= 2 }}",
              "rightValue": "true"
            }
          ]
        },
        "looseTypeValidation": true
      },
      "typeVersion": 2.2
    },
    {
      "id": "YOUR_NODE_ID_DELETE_DUPLICATE_SUBMISSION",
      "name": "Delete Duplicate Submission",
      "type": "n8n-nodes-base.httpRequest",
      "position": [
        1328,
        -112
      ],
      "parameters": {
        "url": "=https://api.jotform.com/submission/{{ $('Extract Submission Data').item.json.submissionID }}",
        "method": "DELETE",
        "options": {},
        "sendQuery": true,
        "queryParameters": {
          "parameters": [
            {
              "name": "apiKey",
              "value": "YOUR_JOTFORM_API_KEY"
            }
          ]
        }
      },
      "typeVersion": 4.2
    },
    {
      "id": "YOUR_NODE_ID_GENERATE_WELCOME_EMAIL",
      "name": "Generate Welcome Email",
      "type": "@n8n/n8n-nodes-langchain.agent",
      "position": [
        1344,
        288
      ],
      "parameters": {
        "text": "=Generate a welcome email for this new business registration.\n\nDATA: {{ $('Extract Submission Data').item.json.rawRequest.toJsonString() }}\n\nEmail must include:\n- Owner name (thank them by name)\n- Business name and type\n- Confirmed contact details\n- Next steps (verification 1-2 days, activation 3-5 days)\n- Support contact info\n- Professional footer\n\nFormat: Valid JSON with email_subject and email_body_html only.",
        "options": {
          "systemMessage": "You are a professional email system for business registrations.\n\nGenerate emails in JSON format only:\n- For NEW registrations: Welcome with confirmation\n- For DUPLICATES: Notification with next steps\n\nAlways return:\n{\n  \"email_subject\": \"string\",\n  \"email_body_html\": \"string with inline CSS\"\n}\n\nKeep HTML concise, mobile-responsive, professional tone.\nOutput ONLY JSON, no other text."
        },
        "promptType": "define",
        "hasOutputParser": true
      },
      "executeOnce": true,
      "typeVersion": 2.2
    },
    {
      "id": "YOUR_NODE_ID_GEMINI_LLM_(WELCOME)",
      "name": "Gemini LLM (Welcome)",
      "type": "@n8n/n8n-nodes-langchain.lmChatGoogleGemini",
      "position": [
        1328,
        480
      ],
      "parameters": {
        "options": {}
      },
      "credentials": {
        "googlePalmApi": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 1
    },
    {
      "id": "YOUR_NODE_ID_PARSE_WELCOME_EMAIL_JSON",
      "name": "Parse Welcome Email JSON",
      "type": "@n8n/n8n-nodes-langchain.outputParserStructured",
      "position": [
        1488,
        496
      ],
      "parameters": {
        "jsonSchemaExample": "{\n  \"email_subject\": \"string\",\n  \"email_body_html\": \"string\"\n}"
      },
      "typeVersion": 1.3
    },
    {
      "id": "YOUR_NODE_ID_SEND_WELCOME_EMAIL",
      "name": "Send Welcome Email",
      "type": "n8n-nodes-base.gmail",
      "position": [
        1760,
        288
      ],
      "parameters": {
        "sendTo": "={{ $('Extract Submission Data').item.json.rawRequest['E-mail'] }}",
        "message": "={{ $json.output.email_body_html }}",
        "options": {},
        "subject": "={{ $json.output.email_subject }}"
      },
      "credentials": {
        "gmailOAuth2": {
          "name": "<your credential>"
        }
      },
      "executeOnce": true,
      "typeVersion": 2.1
    },
    {
      "id": "YOUR_NODE_ID_DELIVER_REJECTION_NOTICE",
      "name": "Deliver Rejection Notice",
      "type": "n8n-nodes-base.gmail",
      "position": [
        1840,
        -112
      ],
      "parameters": {
        "sendTo": "={{ $('Extract Submission Data').item.json.rawRequest['E-mail'] }}",
        "message": "={{ $json.output.email_body_html }}",
        "options": {},
        "subject": "={{ $json.output.email_subject }}"
      },
      "credentials": {
        "gmailOAuth2": {
          "name": "<your credential>"
        }
      },
      "executeOnce": true,
      "typeVersion": 2.1
    },
    {
      "id": "YOUR_NODE_ID_COMPOSE_REJECTION_EMAIL",
      "name": "Compose Rejection Email",
      "type": "@n8n/n8n-nodes-langchain.agent",
      "position": [
        1536,
        -112
      ],
      "parameters": {
        "text": "=Generate a duplicate submission notification email.\n\nDUPLICATE SUBMISSION DATA:\n{{ $('Extract Submission Data').item.json.rawRequest.toJsonString() }}\n\nTASK:\n1. Extract business owner name, email, business name, and contact info\n2. Create a professional email explaining:\n   - This submission is a duplicate of a previous one\n   - Which information was already received\n   - Why it's being excluded\n   - What to do if they need to update information\n3. Provide clear next steps\n4. Keep tone friendly and helpful, not punitive\n5. Generate professional HTML email with inline styling\n\nEMAIL RECIPIENT:\n{{ $('Extract Submission Data').item.json.rawRequest['E-mail'] }}\n\nReturn ONLY valid JSON with exactly two fields:\n{\n  \"email_subject\": \"Your Business Registration - Duplicate Submission Notice\",\n  \"email_body_html\": \"<html>...</html>\"\n}\n\nDo not include any text, markdown, or explanations outside the JSON.",
        "options": {
          "systemMessage": "You are a professional email communication system for business form submissions.\n\nYour role:\n1. Detect when a form submission is a duplicate\n2. Generate a professional, friendly notification email\n3. Explain why the submission was not processed\n4. Guide the user on next steps\n5. Return ONLY valid JSON with no additional text\n\nGuidelines:\n- Be clear but not accusatory about the duplicate\n- Maintain professional and courteous tone\n- Extract and reference key information from the submission\n- Include actionable next steps\n- Ensure HTML email is mobile-responsive\n- Use professional color scheme (blues, grays)\n\nOutput ONLY this exact JSON structure with no explanations:\n{\n  \"email_subject\": \"string\",\n  \"email_body_html\": \"string\"\n}"
        },
        "promptType": "define",
        "hasOutputParser": true
      },
      "executeOnce": true,
      "typeVersion": 2.2
    },
    {
      "id": "YOUR_NODE_ID_VALIDATE_EMAIL_FORMAT",
      "name": "Validate Email Format",
      "type": "@n8n/n8n-nodes-langchain.outputParserStructured",
      "position": [
        1680,
        80
      ],
      "parameters": {
        "jsonSchemaExample": "{\n  \"email_subject\": \"string\",\n  \"email_body_html\": \"string\"\n}"
      },
      "typeVersion": 1.3
    },
    {
      "id": "YOUR_NODE_ID_GEMINI_LLM",
      "name": "Gemini LLM",
      "type": "@n8n/n8n-nodes-langchain.lmChatGoogleGemini",
      "position": [
        1504,
        80
      ],
      "parameters": {
        "options": {}
      },
      "credentials": {
        "googlePalmApi": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 1
    },
    {
      "id": "YOUR_NODE_ID_STICKY_NOTE",
      "name": "Sticky Note",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -624,
        -208
      ],
      "parameters": {
        "width": 2656,
        "height": 848,
        "content": "# Duplicate Submission Detection & Auto Response for Jotform\n\nThis workflow automatically detects duplicate business registrations and handles them intelligently:\n\n**What It Does:**\n- Captures new Jotform submissions instantly\n- Checks if the same email already exists in previous submissions\n- If duplicate found: Deletes the submission and sends a friendly rejection notice\n- If new: Sends a professional welcome confirmation email\n\n---\n\ud83d\udc49 [Get the JotForm from here](https://www.jotform.com/?partner=roshanramanidev)\n\n**How It Works:**\n\n1. **Form Submission Received** \u2192 New registration comes in\n2. **Extract Submission Data** \u2192 Pulls formID, submissionID, and contact details\n3. **Fetch All Form Submissions** \u2192 Retrieves all previous submissions from Jotform API\n4. **Unpack Submission Objects** \u2192 Splits data into individual items\n5. **Filter Active Submissions** \u2192 Keeps only ACTIVE submissions with matching email\n6. **Check Duplicate Count** \u2192 Determines if this is a duplicate or new\n   - **If Duplicate (\u22652 matches):**\n     - Delete Duplicate Submission\n     - Compose Rejection Email (via AI)\n     - Deliver Rejection Notice to submitter\n   - **If New (No matches):**\n     - Generate Welcome Email (via AI)\n     - Send Welcome Email to submitter\n---\n**Key Features:**\n\u2713 Prevents duplicate database entries\n\u2713 Maintains clean, accurate records\n\u2713 Professional AI-generated emails\n\u2713 Automatic email delivery\n\u2713 Instant processing\n\n"
      },
      "typeVersion": 1
    },
    {
      "id": "YOUR_NODE_ID_STICKY_NOTE1",
      "name": "Sticky Note1",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -608,
        704
      ],
      "parameters": {
        "width": 304,
        "height": 208,
        "content": "# Setup Guide\n\n### Step 1: Get Your Form Details\n1. Go to your Jotform form\n2. Copy the Form ID from the URL \n3. Note your email field ID \n\n### Step 2: Get Jotform API Key\n1. Log in to Jotform\n2. Go to **Account \u2192 Settings \u2192 API**\n3. Copy your **API Key**\n4. This key is used in two places:\n   - \"Fetch All Form Submissions\" node (HTTP Request)\n   - \"Delete Duplicate Submission\" node (HTTP Request)\n\n### Step 3: Setup Google Gemini API\n1. Go to [Google Cloud Console](https://console.cloud.google.com)\n2. Create a new project or select existing\n3. Search and enable \"Generative Language API\" (PaLM API)\n4. Go to **Credentials \u2192 Create Credentials \u2192 API Key**\n5. Copy your API Key\n\n### Step 4: Configure Gmail\n1. Go to [Google Cloud Console](https://console.cloud.google.com/)\n2. Enable **Gmail API**\n3. Go to **APIs & Services \u2192 Credentials \u2192 Create Credentials \u2192 OAuth client ID**\n4. Select **Web application**\n5. Copy **Redirect URI** from n8n and paste in **Authorized redirect URIs**\n6. Click **Create**, then copy **Client ID** and **Client Secret**\n7. Open **Send Confirmation Email** node\n8. Click **Credential \u2192 Create New**\n9. Paste **Client ID** and **Client Secret**\n10. Click **Sign in with Google \u2192 Authorize \u2192 Save**\n\n\n### Step 5: Add Credentials to n8n\n1. Go to n8n **Credentials**\n2. Add **Jotform API** credential with your API Key\n3. Add **Google Gemini(PaLM) API** credential\n4. Add **Gmail OAuth2** credential and authorize\n\n### Step 6: Import Workflow & Map Fields\n1. Import this workflow JSON into n8n\n2. Open \"Extract Submission Data\" node\n3. Verify field mappings match your form:\n   - Email field ID \n   - Other relevant fields\n4. Update if your form structure is different\n\n"
      },
      "typeVersion": 1
    }
  ],
  "connections": {
    "Gemini LLM": {
      "ai_languageModel": [
        [
          {
            "node": "Compose Rejection Email",
            "type": "ai_languageModel",
            "index": 0
          }
        ]
      ]
    },
    "Gemini LLM (Welcome)": {
      "ai_languageModel": [
        [
          {
            "node": "Generate Welcome Email",
            "type": "ai_languageModel",
            "index": 0
          }
        ]
      ]
    },
    "Check Duplicate Count": {
      "main": [
        [
          {
            "node": "Delete Duplicate Submission",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Generate Welcome Email",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Validate Email Format": {
      "ai_outputParser": [
        [
          {
            "node": "Compose Rejection Email",
            "type": "ai_outputParser",
            "index": 0
          }
        ]
      ]
    },
    "Generate Welcome Email": {
      "main": [
        [
          {
            "node": "Send Welcome Email",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Compose Rejection Email": {
      "main": [
        [
          {
            "node": "Deliver Rejection Notice",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Extract Submission Data": {
      "main": [
        [
          {
            "node": "Fetch All Form Submissions",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Form Submission Received": {
      "main": [
        [
          {
            "node": "Extract Submission Data",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Parse Welcome Email JSON": {
      "ai_outputParser": [
        [
          {
            "node": "Generate Welcome Email",
            "type": "ai_outputParser",
            "index": 0
          }
        ]
      ]
    },
    "Filter Active Submissions": {
      "main": [
        [
          {
            "node": "Check Duplicate Count",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Unpack Submission Objects": {
      "main": [
        [
          {
            "node": "Filter Active Submissions",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Fetch All Form Submissions": {
      "main": [
        [
          {
            "node": "Unpack Submission Objects",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Delete Duplicate Submission": {
      "main": [
        [
          {
            "node": "Compose Rejection Email",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}