{
  "name": "Lead scoring 0-100",
  "nodes": [
    {
      "parameters": {
        "httpMethod": "POST",
        "path": "lead",
        "responseMode": "responseNode",
        "options": {}
      },
      "id": "n-webhook",
      "name": "Webhook",
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 2.1,
      "position": [
        0,
        300
      ],
      "webhookId": "lead-scoring-lab"
    },
    {
      "parameters": {
        "jsCode": "const b = $input.item.json.body || {};\nconst s = (v) => String(v ?? '').toLowerCase().trim();\nlet score = 0;\nconst reasons = [];\nconst add = (pts, why) => { score += pts; reasons.push(`${why} (${pts > 0 ? '+' : ''}${pts})`); };\n\n// 1. Nguồn lead\nconst src = s(b.source);\nadd({ referral: 25, website_form: 20, facebook_lead_ads: 15 }[src] ?? 5, `Nguồn: ${src || 'khác'}`);\n\n// 2. Quy mô đội ngũ\nconst team = Number(b.team_size);\nif (team >= 50) add(20, `Đội ${team} người`);\nelse if (team >= 10) add(15, `Đội ${team} người`);\nelse if (team > 0) add(5, `Đội ${team} người`);\n\n// 3. Ngân sách\nconst budget = s(b.budget);\nif (budget === 'chưa rõ') add(5, 'Ngân sách chưa rõ');\nelse if (budget) add(20, 'Đã có ngân sách');\n\n// 4. Số điện thoại\nif (s(b.phone)) add(10, 'Có số điện thoại');\n\n// 5. Từ khóa mua hàng trong tin nhắn\nif (/báo giá|giá|demo|triển khai|quote|pricing/i.test(b.message || '')) add(15, 'Hỏi giá/demo/triển khai');\n\n// 6. Email cá nhân thay vì email công ty\nif (/@(gmail|yahoo|hotmail)\\./.test(s(b.email))) add(-10, 'Email cá nhân');\n\nscore = Math.max(0, Math.min(100, score));\nreturn { json: { name: b.name || '', source: src || 'khác', score, reasons } };"
      },
      "id": "n-score",
      "name": "Score",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        240,
        300
      ]
    },
    {
      "parameters": {
        "rules": {
          "values": [
            {
              "conditions": {
                "options": {
                  "caseSensitive": true,
                  "leftValue": "",
                  "typeValidation": "strict",
                  "version": 2
                },
                "conditions": [
                  {
                    "id": "vcmc8hjilmb",
                    "leftValue": "={{ $json.score }}",
                    "rightValue": 70,
                    "operator": {
                      "type": "number",
                      "operation": "gte"
                    }
                  }
                ],
                "combinator": "and"
              },
              "renameOutput": true,
              "outputKey": "hot"
            },
            {
              "conditions": {
                "options": {
                  "caseSensitive": true,
                  "leftValue": "",
                  "typeValidation": "strict",
                  "version": 2
                },
                "conditions": [
                  {
                    "id": "7se1pdyoww9",
                    "leftValue": "={{ $json.score }}",
                    "rightValue": 40,
                    "operator": {
                      "type": "number",
                      "operation": "gte"
                    }
                  },
                  {
                    "id": "21g7qtmgctai",
                    "leftValue": "={{ $json.score }}",
                    "rightValue": 70,
                    "operator": {
                      "type": "number",
                      "operation": "lt"
                    }
                  }
                ],
                "combinator": "and"
              },
              "renameOutput": true,
              "outputKey": "warm"
            },
            {
              "conditions": {
                "options": {
                  "caseSensitive": true,
                  "leftValue": "",
                  "typeValidation": "strict",
                  "version": 2
                },
                "conditions": [
                  {
                    "id": "cwyb40ua3ue",
                    "leftValue": "={{ $json.score }}",
                    "rightValue": 40,
                    "operator": {
                      "type": "number",
                      "operation": "lt"
                    }
                  }
                ],
                "combinator": "and"
              },
              "renameOutput": true,
              "outputKey": "cold"
            }
          ]
        },
        "options": {}
      },
      "id": "n-tier",
      "name": "Tier",
      "type": "n8n-nodes-base.switch",
      "typeVersion": 3.2,
      "position": [
        480,
        300
      ]
    },
    {
      "parameters": {
        "assignments": {
          "assignments": [
            {
              "id": "a1hot",
              "name": "tier",
              "value": "hot",
              "type": "string"
            },
            {
              "id": "a2hot",
              "name": "next_action",
              "value": "Gọi trong 1 giờ",
              "type": "string"
            }
          ]
        },
        "includeOtherFields": true,
        "options": {}
      },
      "id": "set-hot",
      "name": "Hot",
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "position": [
        760,
        140
      ]
    },
    {
      "parameters": {
        "assignments": {
          "assignments": [
            {
              "id": "a1warm",
              "name": "tier",
              "value": "warm",
              "type": "string"
            },
            {
              "id": "a2warm",
              "name": "next_action",
              "value": "Gửi email trong ngày",
              "type": "string"
            }
          ]
        },
        "includeOtherFields": true,
        "options": {}
      },
      "id": "set-warm",
      "name": "Warm",
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "position": [
        760,
        300
      ]
    },
    {
      "parameters": {
        "assignments": {
          "assignments": [
            {
              "id": "a1cold",
              "name": "tier",
              "value": "cold",
              "type": "string"
            },
            {
              "id": "a2cold",
              "name": "next_action",
              "value": "Đưa vào danh sách nuôi dưỡng",
              "type": "string"
            }
          ]
        },
        "includeOtherFields": true,
        "options": {}
      },
      "id": "set-cold",
      "name": "Cold",
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "position": [
        760,
        460
      ]
    },
    {
      "parameters": {},
      "id": "n-join",
      "name": "Join",
      "type": "n8n-nodes-base.noOp",
      "typeVersion": 1,
      "position": [
        1000,
        300
      ]
    },
    {
      "parameters": {
        "resource": "sheet",
        "operation": "append",
        "documentId": {
          "__rl": true,
          "value": "",
          "mode": "id"
        },
        "sheetName": {
          "__rl": true,
          "value": "Sheet1",
          "mode": "name"
        },
        "columns": {
          "mappingMode": "defineBelow",
          "value": {
            "time": "={{ $now.toFormat('yyyy-MM-dd HH:mm:ss') }}",
            "name": "={{ $json.name }}",
            "source": "={{ $json.source }}",
            "score": "={{ $json.score }}",
            "tier": "={{ $json.tier }}",
            "next_action": "={{ $json.next_action }}",
            "reasons": "={{ $json.reasons.join('; ') }}"
          },
          "matchingColumns": [],
          "schema": [
            {
              "id": "time",
              "displayName": "time",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            },
            {
              "id": "name",
              "displayName": "name",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            },
            {
              "id": "source",
              "displayName": "source",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            },
            {
              "id": "score",
              "displayName": "score",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            },
            {
              "id": "tier",
              "displayName": "tier",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            },
            {
              "id": "next_action",
              "displayName": "next_action",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            },
            {
              "id": "reasons",
              "displayName": "reasons",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            }
          ],
          "attemptToConvertTypes": false,
          "convertFieldsToString": false
        },
        "options": {}
      },
      "id": "n-sheets",
      "name": "Append row",
      "type": "n8n-nodes-base.googleSheets",
      "typeVersion": 4.7,
      "position": [
        1240,
        300
      ],
      "disabled": true
    },
    {
      "parameters": {
        "respondWith": "json",
        "responseBody": "={{ { score: $('Join').item.json.score, tier: $('Join').item.json.tier, next_action: $('Join').item.json.next_action, reasons: $('Join').item.json.reasons } }}",
        "options": {}
      },
      "id": "n-respond",
      "name": "Respond to Webhook",
      "type": "n8n-nodes-base.respondToWebhook",
      "typeVersion": 1.5,
      "position": [
        1480,
        300
      ]
    }
  ],
  "connections": {
    "Webhook": {
      "main": [
        [
          {
            "node": "Score",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Score": {
      "main": [
        [
          {
            "node": "Tier",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Tier": {
      "main": [
        [
          {
            "node": "Hot",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Warm",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Cold",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Hot": {
      "main": [
        [
          {
            "node": "Join",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Warm": {
      "main": [
        [
          {
            "node": "Join",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Cold": {
      "main": [
        [
          {
            "node": "Join",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Join": {
      "main": [
        [
          {
            "node": "Append row",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Append row": {
      "main": [
        [
          {
            "node": "Respond to Webhook",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "settings": {
    "executionOrder": "v1"
  }
}