File size: 2,659 Bytes
77e46d0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
// processors/recognizer.js
const googleSheets = require('../services/googleSheets');

/**
 * Hàm nhận diện khách hàng từ Excel
 * Trả về session đã được nạp dữ liệu cũ (nếu có)
 */
async function identify(userId, session) {
    // 1. Nếu đã nhận diện trong phiên này rồi thì thôi
    if (!session || session.isRecognized) return session;

    try {
        // Tìm thông tin khách trong Sheet bằng userId
        const customerData = await googleSheets.findCustomerById(userId);

        if (customerData) {
            if (!session.entities) session.entities = {};

            // Dữ liệu từ Excel làm "nền"
            const excelData = {
                name: customerData.Ho_Ten || "",
                phone: String(customerData.SDT || ""),
                address: customerData.Dia_Chi || "",
                weight: customerData.Can_Nang_1 || customerData.Can_Nang || "",
            };

            /**
             * CƠ CHẾ GỘP DỮ LIỆU AN TOÀN:
             * - Nếu trong Session hiện tại đang trống (null/rỗng), ta mới lấy từ Excel đắp vào.
             * - Nếu Session hiện tại đã có dữ liệu (do khách vừa nhắn), ta giữ nguyên dữ liệu đó.
             * - Tuyệt đối không để giá trị rỗng từ tin nhắn mới ghi đè lên giá trị đã có từ Excel.
             */
            for (const key in excelData) {
                const currentValue = session.entities[key];
                const hasNoCurrentValue = !currentValue || currentValue === "" || currentValue === "null";

                if (hasNoCurrentValue && excelData[key] !== "") {
                    session.entities[key] = excelData[key];
                }
            }

            // Đồng bộ ngược lại các biến trực tiếp trên session để các file khác dễ dùng
            session.phone = session.entities.phone;
            session.address = session.entities.address;
            session.weight = session.entities.weight;

            // Đánh dấu đã nhận diện thành công
            session.isRecognized = true;
            
            console.log(`\x1b[32m🔍 [Recognizer] Khách quen quay lại: ${session.entities.name || userId} (${session.entities.weight}kg)\x1b[0m`);
        } else {
            // Khách mới hoàn toàn
            session.isRecognized = true; 
            console.log(`🔍 [Recognizer] Khách mới hoàn toàn: ${userId}`);
        }
    } catch (err) {
        console.error("❌ [Recognizer Error]:", err.message);
    }

    return session;
}

module.exports = { identify };