{
  "name": "Auto Label Success Cases v2",
  "nodes": [
    {
      "parameters": {},
      "id": "manual-trigger",
      "name": "Manual Trigger",
      "type": "n8n-nodes-base.manualTrigger",
      "typeVersion": 1,
      "position": [
        0,
        300
      ]
    },
    {
      "parameters": {
        "operation": "read",
        "documentId": {
          "__rl": true,
          "value": "13iOvpUd6OjZPgMeFKL42RlPk326JCLNzT8tyEM-SY8I",
          "mode": "id"
        },
        "sheetName": {
          "__rl": true,
          "value": "OK",
          "mode": "name"
        },
        "options": {}
      },
      "id": "read-ok-sheet",
      "name": "Read OK Sheet",
      "type": "n8n-nodes-base.googleSheets",
      "typeVersion": 4.5,
      "position": [
        240,
        160
      ]
    },
    {
      "parameters": {
        "operation": "read",
        "documentId": {
          "__rl": true,
          "value": "13iOvpUd6OjZPgMeFKL42RlPk326JCLNzT8tyEM-SY8I",
          "mode": "id"
        },
        "sheetName": {
          "__rl": true,
          "value": "CHECK",
          "mode": "name"
        },
        "options": {}
      },
      "id": "read-check-sheet",
      "name": "Read CHECK Sheet",
      "type": "n8n-nodes-base.googleSheets",
      "typeVersion": 4.5,
      "position": [
        240,
        320
      ]
    },
    {
      "parameters": {
        "operation": "read",
        "documentId": {
          "__rl": true,
          "value": "13iOvpUd6OjZPgMeFKL42RlPk326JCLNzT8tyEM-SY8I",
          "mode": "id"
        },
        "sheetName": {
          "__rl": true,
          "value": "Enquiries",
          "mode": "name"
        },
        "options": {}
      },
      "id": "read-enquiries-sheet",
      "name": "Read Enquiries Sheet",
      "type": "n8n-nodes-base.googleSheets",
      "typeVersion": 4.5,
      "position": [
        240,
        480
      ]
    },
    {
      "parameters": {
        "jsCode": "// Combine all inputs and extract unique emails\nconst allItems = $input.all();\nconst emails = new Set();\n\nfor (const item of allItems) {\n  const data = item.json;\n  // Try various email field names\n  const possibleFields = ['Email', 'email', 'Email Address', '\u30e1\u30fc\u30eb\u30a2\u30c9\u30ec\u30b9', 'User Email', 'Contact Email'];\n  \n  for (const field of possibleFields) {\n    if (data[field] && typeof data[field] === 'string' && data[field].includes('@')) {\n      emails.add(data[field].toLowerCase().trim());\n      break;\n    }\n  }\n  \n  // Also check all fields for email pattern\n  for (const [key, value] of Object.entries(data)) {\n    if (typeof value === 'string' && value.includes('@') && value.includes('.')) {\n      const emailMatch = value.match(/[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}/);\n      if (emailMatch) {\n        emails.add(emailMatch[0].toLowerCase().trim());\n      }\n    }\n  }\n}\n\nconsole.log(`Found ${emails.size} unique emails`);\n\n// Return as array\nreturn Array.from(emails).map(email => ({ json: { email } }));"
      },
      "id": "extract-emails",
      "name": "Extract Unique Emails",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        520,
        320
      ]
    },
    {
      "parameters": {
        "resource": "message",
        "operation": "getAll",
        "returnAll": false,
        "limit": 50,
        "filters": {
          "q": "=from:{{ $json.email }} OR to:{{ $json.email }}"
        },
        "options": {}
      },
      "id": "search-gmail",
      "name": "Search Gmail",
      "type": "n8n-nodes-base.gmail",
      "typeVersion": 2.1,
      "position": [
        760,
        320
      ]
    },
    {
      "parameters": {
        "resource": "message",
        "operation": "addLabels",
        "messageId": "={{ $json.id }}",
        "labelIds": [
          "REPLACE_WITH_LABEL_ID"
        ]
      },
      "id": "add-label",
      "name": "Add Label",
      "type": "n8n-nodes-base.gmail",
      "typeVersion": 2.1,
      "position": [
        1000,
        320
      ]
    },
    {
      "parameters": {
        "content": "## \u8a2d\u5b9a\u624b\u9806\n\n1. **Google Sheets\u8a8d\u8a3c**\n   \u5404Read Sheet\u30ce\u30fc\u30c9\u306b\u8a8d\u8a3c\u8a2d\u5b9a\n\n2. **Gmail\u8a8d\u8a3c**\n   Search Gmail, Add Label \u30ce\u30fc\u30c9\u306b\u8a8d\u8a3c\u8a2d\u5b9a\n\n3. **Label ID\u8a2d\u5b9a**\n   Add Label \u30ce\u30fc\u30c9 \u2192 Label IDs \u2192 \u300csuccess cases\u300d\u9078\u629e\n\n4. **\u5b9f\u884c**\n   \u300cTest workflow\u300d\u30dc\u30bf\u30f3\u3092\u30af\u30ea\u30c3\u30af",
        "height": 280,
        "width": 300
      },
      "id": "sticky-note",
      "name": "Setup",
      "type": "n8n-nodes-base.stickyNote",
      "typeVersion": 1,
      "position": [
        0,
        0
      ]
    }
  ],
  "connections": {
    "Manual Trigger": {
      "main": [
        [
          {
            "node": "Read OK Sheet",
            "type": "main",
            "index": 0
          },
          {
            "node": "Read CHECK Sheet",
            "type": "main",
            "index": 0
          },
          {
            "node": "Read Enquiries Sheet",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Read OK Sheet": {
      "main": [
        [
          {
            "node": "Extract Unique Emails",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Read CHECK Sheet": {
      "main": [
        [
          {
            "node": "Extract Unique Emails",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Read Enquiries Sheet": {
      "main": [
        [
          {
            "node": "Extract Unique Emails",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Extract Unique Emails": {
      "main": [
        [
          {
            "node": "Search Gmail",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Search Gmail": {
      "main": [
        [
          {
            "node": "Add Label",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "settings": {
    "executionOrder": "v1"
  }
}