{
  "name": "VietlinhAI | Báo cáo lead mới mỗi sáng qua email",
  "nodes": [
    {
      "parameters": {
        "fromEmail": "you@example.com",
        "toEmail": "you@example.com",
        "subject": "={{ $json.subject }}",
        "html": "={{ $json.html }}",
        "options": {
          "appendAttribution": false
        }
      },
      "id": "b6ab8a11-7148-41bf-9607-4b4b5dc7f3b1",
      "name": "Gửi email SMTP",
      "type": "n8n-nodes-base.emailSend",
      "typeVersion": 2.1,
      "position": [
        -400,
        -80
      ],
      "webhookId": "e6adc37e-02c2-45b1-9f00-9ce8b2f468b2",
      "notes": "Gửi bằng SMTP. Chỉ dùng Gmail thì tắt node này (Deactivate)."
    },
    {
      "parameters": {
        "jsCode": "// Lấy khách có created_at trong 24 giờ qua. created_at dạng \"11:13:04 20/6/2026\" (giờ Việt Nam).\nconst ZONE = 'Asia/Ho_Chi_Minh';\nconst since = DateTime.now().setZone(ZONE).minus({ hours: 24 });\n\nreturn items.filter(item => {\n  const raw = String(item.json.created_at || '').trim();\n  if (!raw) return false;\n  const createdAt = DateTime.fromFormat(raw, 'H:mm:ss d/M/yyyy', { zone: ZONE });\n  return createdAt.isValid && createdAt >= since;\n});"
      },
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        -880,
        80
      ],
      "id": "874a98c5-aa46-4bb6-b45a-353bcb8ad15f",
      "name": "Code lọc khách 24h"
    },
    {
      "parameters": {
        "rule": {
          "interval": [
            {
              "triggerAtHour": 8,
              "triggerAtMinute": 30
            }
          ]
        }
      },
      "type": "n8n-nodes-base.scheduleTrigger",
      "typeVersion": 1.3,
      "position": [
        -1344,
        80
      ],
      "id": "f8bafe33-5da7-4905-a5dd-5d1af9480cc1",
      "name": "Trigger chạy lúc 8'30 sáng"
    },
    {
      "parameters": {
        "documentId": {
          "__rl": true,
          "value": "",
          "mode": "list",
          "cachedResultName": "-- Chọn file của bạn --",
          "cachedResultUrl": ""
        },
        "sheetName": {
          "__rl": true,
          "value": "",
          "mode": "list",
          "cachedResultName": "-- Chọn trang tính CRM của bạn --",
          "cachedResultUrl": ""
        },
        "filtersUI": {
          "values": []
        },
        "options": {}
      },
      "type": "n8n-nodes-base.googleSheets",
      "typeVersion": 4.7,
      "position": [
        -1120,
        80
      ],
      "id": "ebbff8e9-4cdd-46e2-bf87-5523a68780b2",
      "name": "Đọc dữ liệu file Lead CRM"
    },
    {
      "parameters": {
        "jsCode": "// Đổi tên thương hiệu hiển thị trong email báo cáo.\nconst BRAND = '[TÊN THƯƠNG HIỆU]';\n\nconst leads = items;\nconst count = leads.length;\nconst today = new Date().toLocaleDateString('vi-VN', {\n  weekday: 'long', year: 'numeric', month: 'long', day: 'numeric'\n});\n\nconst leadCards = leads.map((item, index) => {\n  const c = item.json;\n  const name = c.customer_name || c.username || c.name || 'Chưa có tên';\n  const phone = c.phone || c.phone_number || 'Chưa có';\n  const platform = c.platform || c.source || 'Chưa rõ';\n  const status = c.customer_status || c.customer_type || c.status || 'Chưa rõ';\n  const need = c.customer_need || c.need || c.conversation_summary || c.note || 'Chưa có ghi chú';\n  return `\n    <div style=\"border:1px solid #e5e7eb;border-radius:10px;padding:16px 18px;margin-top:14px;background:#ffffff;\">\n      <div style=\"font-size:13px;color:#6b7280;margin-bottom:6px;\">Khách hàng #${index + 1}</div>\n      <div style=\"font-size:17px;font-weight:700;color:#0B1F3A;margin-bottom:10px;\">${name}</div>\n      <div style=\"font-size:14px;line-height:1.8;color:#111827;\">\n        <div><strong>Điện thoại:</strong> ${phone}</div>\n        <div><strong>Nền tảng:</strong> ${platform}</div>\n        <div><strong>Hiện trạng:</strong> ${status}</div>\n        <div><strong>Mong muốn:</strong> ${need}</div>\n      </div>\n    </div>\n  `;\n}).join('');\n\nconst html = `\n<!DOCTYPE html>\n<html>\n<head>\n  <link href=\"https://fonts.googleapis.com/css2?family=Be+Vietnam+Pro:wght@400;500;600;700&display=swap\" rel=\"stylesheet\">\n</head>\n<body style=\"margin:0;padding:0;background:#f3f4f6;font-family:'Be Vietnam Pro',Arial,sans-serif;color:#111827;\">\n  <div style=\"max-width:680px;margin:0 auto;padding:24px;\">\n\n    <!-- Header -->\n    <div style=\"background:#0B1F3A;padding:22px 28px;border-radius:14px 14px 0 0;\">\n      \n      <!-- Logo -->\n      <div style=\"margin-bottom:14px;\">\n        <span style=\"font-family:'Be Vietnam Pro',Arial,sans-serif;font-size:22px;font-weight:700;color:#ffffff;letter-spacing:-0.3px;\">${BRAND}</span>\n      </div>\n\n      <!-- Badge -->\n      <div style=\"display:inline-block;background:rgba(53,194,255,0.15);border:1px solid rgba(53,194,255,0.3);border-radius:20px;padding:3px 12px;margin-bottom:10px;\">\n        <span style=\"font-size:11px;font-weight:600;color:#35C2FF;letter-spacing:0.08em;text-transform:uppercase;\">Lead Digest</span>\n      </div>\n\n      <h2 style=\"margin:0 0 6px;font-size:20px;font-weight:700;color:#ffffff;line-height:1.35;\">\n        Báo cáo khách mới trong 24h\n      </h2>\n      <div style=\"font-size:13px;color:#8fafd4;\">${today}</div>\n    </div>\n\n    <!-- Summary -->\n    <div style=\"background:#ffffff;padding:20px 28px;border-left:1px solid #e5e7eb;border-right:1px solid #e5e7eb;\">\n      <p style=\"margin:0;font-size:15px;line-height:1.6;color:#111827;\">\n        Trong 24 giờ qua, hệ thống ghi nhận \n        <strong style=\"color:#0B1F3A;\">${count} khách hàng mới</strong>.\n      </p>\n      ${leadCards}\n    </div>\n\n    <!-- Footer -->\n    <div style=\"background:#f9fafb;border:1px solid #e5e7eb;border-top:0;padding:14px 28px;border-radius:0 0 14px 14px;\">\n      <p style=\"margin:0;font-size:13px;color:#6b7280;\">\n        💡 Gợi ý: ưu tiên liên hệ khách có số điện thoại và mong muốn rõ ràng trong ngày.\n      </p>\n    </div>\n\n    <!-- Signature -->\n    <div style=\"text-align:center;margin-top:20px;\">\n      <span style=\"font-size:12px;color:#9ca3af;\">\n        Tự động gửi bởi \n        <span style=\"color:#0B1F3A;font-weight:600;\">${BRAND}</span> qua n8n\n      </span>\n    </div>\n\n  </div>\n</body>\n</html>\n`;\n\nreturn [{ json: { html, count, subject: `[${BRAND}] ${count} khách mới trong 24h – ${today}` } }];"
      },
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        -672,
        80
      ],
      "id": "f58e0047-8eb7-4328-b415-7a190471714f",
      "name": "Soạn email báo cáo"
    },
    {
      "parameters": {
        "sendTo": "you@example.com",
        "subject": "={{ $json.subject }}",
        "message": "={{ $json.html }}",
        "options": {
          "appendAttribution": false
        }
      },
      "type": "n8n-nodes-base.gmail",
      "typeVersion": 2.2,
      "position": [
        -400,
        224
      ],
      "id": "f468032f-0b41-4e98-aa04-3164e38374ac",
      "name": "Gửi qua Gmail",
      "webhookId": "4ae92e0d-7753-4a39-9b5d-3122194dd633",
      "notes": "Gửi bằng Gmail. Chỉ dùng SMTP thì tắt node này (Deactivate)."
    }
  ],
  "pinData": {},
  "connections": {
    "Trigger chạy lúc 8'30 sáng": {
      "main": [
        [
          {
            "node": "Đọc dữ liệu file Lead CRM",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Đọc dữ liệu file Lead CRM": {
      "main": [
        [
          {
            "node": "Code lọc khách 24h",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Code lọc khách 24h": {
      "main": [
        [
          {
            "node": "Soạn email báo cáo",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Soạn email báo cáo": {
      "main": [
        [
          {
            "node": "Gửi email SMTP",
            "type": "main",
            "index": 0
          },
          {
            "node": "Gửi qua Gmail",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "active": false,
  "settings": {
    "executionOrder": "v1",
    "binaryMode": "separate",
    "availableInMCP": false
  },
  "nodeGroups": [],
  "tags": []
}