{
  "name": "Gmail Job Position Classifier - Fixed",
  "nodes": [
    {
      "parameters": {},
      "id": "d04c946a-8425-41cb-b570-fe60e9ef7834",
      "name": "Start",
      "type": "n8n-nodes-base.manualTrigger",
      "typeVersion": 1,
      "position": [
        -672,
        720
      ]
    },
    {
      "parameters": {
        "assignments": {
          "assignments": [
            {
              "id": "start-date-field",
              "name": "startDate",
              "value": "2026/03/01",
              "type": "string"
            }
          ]
        },
        "options": {}
      },
      "id": "d67fdfff-e71a-45ce-a808-3a800ac7601f",
      "name": "⚙️ Set Start Date",
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "position": [
        -432,
        720
      ]
    },
    {
      "parameters": {
        "operation": "getAll",
        "returnAll": true,
        "filters": {
          "q": "={{ 'after:' + $json.startDate }}"
        }
      },
      "id": "7f81479c-698e-4e2a-98ba-28924f1130ea",
      "name": "Gmail - Get Inbox1",
      "type": "n8n-nodes-base.gmail",
      "typeVersion": 2.1,
      "position": [
        -224,
        576
      ],
      "credentials": {
        "gmailOAuth2": {
          "id": "gO8EWg5jZuSVkFxz",
          "name": "Gmail account"
        }
      }
    },
    {
      "parameters": {
        "assignments": {
          "assignments": [
            {
              "id": "direction-field",
              "name": "direction",
              "value": "Inbox",
              "type": "string"
            }
          ]
        },
        "options": {}
      },
      "id": "6531b886-0513-4fa1-a5ea-6a12ca1b602b",
      "name": "Tag - Inbox1",
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "position": [
        0,
        576
      ]
    },
    {
      "parameters": {
        "operation": "getAll",
        "returnAll": true,
        "filters": {
          "q": "={{ 'in:sent after:' + $json.startDate }}"
        }
      },
      "id": "11110f0b-0a62-41e4-8989-081d016ada3f",
      "name": "Gmail - Get Sent1",
      "type": "n8n-nodes-base.gmail",
      "typeVersion": 2.1,
      "position": [
        -224,
        848
      ],
      "credentials": {
        "gmailOAuth2": {
          "id": "gO8EWg5jZuSVkFxz",
          "name": "Gmail account"
        }
      }
    },
    {
      "parameters": {
        "assignments": {
          "assignments": [
            {
              "id": "direction-field",
              "name": "direction",
              "value": "Sent",
              "type": "string"
            }
          ]
        },
        "options": {}
      },
      "id": "6628186c-c7dc-4e74-9fdb-ecafa9dbed58",
      "name": "Tag - Sent1",
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "position": [
        0,
        848
      ]
    },
    {
      "parameters": {},
      "id": "62a5f5c2-3264-4866-ab4b-adcb724e8b91",
      "name": "Merge Emails1",
      "type": "n8n-nodes-base.merge",
      "typeVersion": 3,
      "position": [
        224,
        720
      ]
    },
    {
      "parameters": {
        "jsCode": "const items = $input.all();\nconst results = [];\n\nfor (const item of items) {\n  const headers = item.json.payload?.headers || [];\n  const getHeader = (name) => {\n    const h = headers.find(h => h.name.toLowerCase() === name.toLowerCase());\n    return h ? h.value : '';\n  };\n\n  const rawDate = getHeader('Date');\n  let parsedDate = '';\ntry {\n  parsedDate = rawDate ? new Date(rawDate).toISOString().split('T')[0] : '';\n} catch(e) {\n  parsedDate = rawDate;\n}\n\n  results.push({\n    json: {\n      emailId: item.json.id || '',\n      direction: item.json.direction || '',\n      from: getHeader('From'),\n      to: getHeader('To'),\n      subject: getHeader('Subject'),\n      date: parsedDate,\n      snippet: (item.json.snippet || '').substring(0, 500)\n    }\n  });\n}\nreturn results;"
      },
      "id": "76fe7398-47de-474d-9f42-5e0b839ebecd",
      "name": "Extract Email Fields1",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        416,
        720
      ]
    },
    {
      "parameters": {
        "promptType": "define",
        "text": "=Analyze this email and classify it for a job search.\n\nDetails:\n- Direction: {{ $json.direction }}\n- From: {{ $json.from }}\n- Subject: {{ $json.subject }}\n- Body: {{ $json.snippet }}\n\nReturn ONLY JSON:\n{\n  \"isJobRelated\": true/false,\n  \"category\": \"Category Name\",\n  \"company\": \"Company Name\",\n  \"position\": \"Job Title\",\n  \"confidence\": 0.95\n}",
        "messages": {
          "messageValues": []
        }
      },
      "id": "0c86343e-e10a-458d-89a9-fa02b32a8762",
      "name": "extract-tracking-info",
      "type": "@n8n/n8n-nodes-langchain.chainLlm",
      "position": [
        620,
        720
      ],
      "typeVersion": 1.4
    },
    {
      "parameters": {
        "model": "llama3.1:latest",
        "options": {}
      },
      "id": "94440736-fddc-4657-8442-6c7954b08ea2",
      "name": "ollama-model",
      "type": "@n8n/n8n-nodes-langchain.lmChatOllama",
      "position": [
        620,
        920
      ],
      "typeVersion": 1,
      "credentials": {
        "ollamaApi": {
          "id": "9k920xXzUND3cX1z",
          "name": "Ollama account"
        }
      }
    },
    {
      "parameters": {
        "jsCode": "const items = $input.all();\nconst results = [];\n\nfor (const item of items) {\n  const responseData = item.json.output || \"\";\n  let classification = { isJobRelated: false };\n\n  try {\n    const jsonMatch = responseData.match(/\\{[\\s\\S]*\\}/);\n    if (jsonMatch) {\n      classification = JSON.parse(jsonMatch[0]);\n    }\n  } catch(e) {}\n\n  results.push({\n    json: {\n      ...item.json,\n      ...classification\n    }\n  });\n}\nreturn results;"
      },
      "id": "cbe7c441-0722-4542-9d73-022a180ea4a1",
      "name": "Parse Classification1",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        832,
        720
      ]
    },
    {
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "leftValue": "",
            "typeValidation": "strict",
            "version": 2
          },
          "conditions": [
            {
              "id": "job-related-check",
              "leftValue": "={{ $json.isJobRelated }}",
              "rightValue": true,
              "operator": {
                "type": "boolean",
                "operation": "equal"
              }
            }
          ],
          "combinator": "and"
        }
      },
      "id": "c9df3099-36d7-468e-a39f-23181727cd1f",
      "name": "Is Job Related?1",
      "type": "n8n-nodes-base.if",
      "typeVersion": 2.2,
      "position": [
        1024,
        720
      ]
    },
    {
      "parameters": {
        "operation": "append",
        "documentId": {
          "__rl": true,
          "value": "1FNgfZxGDwexb86dQ9czR0nK9qNBZ3yULOB",
          "mode": "id"
        },
        "sheetName": {
          "__rl": true,
          "mode": "list",
          "value": "Sheet1"
        },
        "columns": {
          "mappingMode": "autoMapInputData"
        }
      },
      "id": "1e916081-d026-48e9-83f9-b96dbf8b2afc",
      "name": "Save to Google Sheets1",
      "type": "n8n-nodes-base.googleSheets",
      "typeVersion": 4.4,
      "position": [
        1280,
        600
      ],
      "credentials": {
        "googleSheetsOAuth2Api": {
          "id": "hHAfsVGhuZEemmJV",
          "name": "Google Sheets account"
        }
      }
    },
    {
      "parameters": {
        "chatId": "-1003803550245",
        "text": "=📧 *Job Email Classification*\n\n*Category:* {{ $json.category }}\n*Company:* {{ $json.company }}\n*Position:* {{ $json.position }}\n*Confidence:* {{ ($json.confidence * 100).toFixed(1) }}%",
        "additionalFields": {
          "parse_mode": "Markdown"
        }
      },
      "id": "1d04f27f-0a1c-489f-9b97-88d44492bee7",
      "name": "Send a Telegram message",
      "type": "n8n-nodes-base.telegram",
      "typeVersion": 1.2,
      "position": [
        1280,
        800
      ],
      "credentials": {
        "telegramApi": {
          "id": "bnyBxist2KsEGbou",
          "name": "Gmail Organizer"
        }
      }
    }
  ],
  "connections": {
    "Start": { "main": [[{ "node": "⚙️ Set Start Date", "type": "main", "index": 0 }]] },
    "⚙️ Set Start Date": { "main": [[{ "node": "Gmail - Get Inbox1", "type": "main", "index": 0 }, { "node": "Gmail - Get Sent1", "type": "main", "index": 0 }]] },
    "Gmail - Get Inbox1": { "main": [[{ "node": "Tag - Inbox1", "type": "main", "index": 0 }]] },
    "Tag - Inbox1": { "main": [[{ "node": "Merge Emails1", "type": "main", "index": 0 }]] },
    "Gmail - Get Sent1": { "main": [[{ "node": "Tag - Sent1", "type": "main", "index": 0 }]] },
    "Tag - Sent1": { "main": [[{ "node": "Merge Emails1", "type": "main", "index": 1 }]] },
    "Merge Emails1": { "main": [[{ "node": "Extract Email Fields1", "type": "main", "index": 0 }]] },
    "Extract Email Fields1": { "main": [[{ "node": "extract-tracking-info", "type": "main", "index": 0 }]] },
    "ollama-model": { "ai_languageModel": [[{ "node": "extract-tracking-info", "type": "ai_languageModel", "index": 0 }]] },
    "extract-tracking-info": { "main": [[{ "node": "Parse Classification1", "type": "main", "index": 0 }]] },
    "Parse Classification1": { "main": [[{ "node": "Is Job Related?1", "type": "main", "index": 0 }]] },
    "Is Job Related?1": { "main": [[{ "node": "Save to Google Sheets1", "type": "main", "index": 0 }, { "node": "Send a Telegram message", "type": "main", "index": 0 }]] }
  }
}