File size: 3,687 Bytes
5d7d009
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
const sessions = new Map();

// Cấu hình phí ship của Hương Kid
const SHIPPING_CONFIG = {
    FEE: 30000,           // Phí ship mặc định 30k
    FREESHIP_LIMIT: 500000 // Đơn từ 500k được freeship
};

const sessionManager = {
    // --- 1. QUẢN LÝ PHIÊN CHAT (SESSION) ---
    
    // Lấy session hiện tại
    get: (id) => sessions.get(id),

    /**
     * Cập nhật session theo kiểu "Bổ sung" (Patch)
     * Đảm bảo không làm mất dữ liệu cũ (SĐT, Địa chỉ, Cân nặng) của khách
     */
    update: (id, newData) => {
        let session = sessions.get(id) || { 
            id: id,
            cart: [], 
            entities: {}, 
            flags: {}, 
            totals: {}, 
            isRecognized: false,
            executionPath: [] 
        };
        session.id = id;
        // A. XỬ LÝ RIÊNG CHO ENTITIES (Thông tin cá nhân khách hàng)
        if (newData.entities) {
            if (!session.entities) session.entities = {};
            
            for (let key in newData.entities) {
                let newVal = newData.entities[key];
                if (newVal !== undefined && newVal !== null && newVal !== "") {
                    session.entities[key] = newVal;
                }
            }
            delete newData.entities; 
        }

        // B. XỬ LÝ CHO GIỎ HÀNG
        if (newData.cart) {
            session.cart = newData.cart;
            delete newData.cart;
        }

        // C. CẬP NHẬT CÁC THÀNH PHẦN KHÁC (winner, flags, zones...)
        Object.assign(session, newData);

        // D. TỰ ĐỘNG TÍNH TOÁN TÀI CHÍNH (Totals)
        if (session.cart) {
            const subTotal = session.cart.reduce((sum, item) => {
                const price = parseInt(item.price?.toString().replace(/\D/g, '')) || 0;
                return sum + price;
            }, 0);

            const isEligibleForFreeShip = subTotal >= SHIPPING_CONFIG.FREESHIP_LIMIT;
            const shipFee = (isEligibleForFreeShip || subTotal === 0) ? 0 : SHIPPING_CONFIG.FEE;

            session.totals = {
                subTotal: subTotal,
                shipFee: shipFee,
                finalTotal: subTotal + shipFee,
                isFreeShip: isEligibleForFreeShip,
                needMoreForFreeShip: isEligibleForFreeShip ? 0 : (SHIPPING_CONFIG.FREESHIP_LIMIT - subTotal)
            };
        }

        // E. GIỚI HẠN LỊCH SỬ (Tránh tràn RAM)
        if (session.executionPath && session.executionPath.length > 10) {
            session.executionPath = session.executionPath.slice(-10);
        }

        sessions.set(id, session);
        return session;
    },

    // Xóa session khi khách chốt đơn thành công
    clear: (id) => {
        sessions.delete(id);
        console.log(`[Session] 🗑️ Đã xóa bộ nhớ phiên của khách: ${id}`);
    },

    // --- 2. QUẢN LÝ DỮ LIỆU TỔNG (GLOBAL DATA - RAM) ---

    // KHO HÀNG (Inventory)
    setInventory: (data) => {
        sessions.set('global_inventory', data);
        console.log(`[Inventory] 📦 Đã nạp kho hàng vào RAM hệ thống.`);
    },

    getInventory: () => {
        return sessions.get('global_inventory') || { inventory: {}, groups: [] };
    },

    // QUY TẮC / KÍCH THƯỚC (Dimensions) - Sửa lỗi "is not a function"
    setDimensions: (data) => {
        sessions.set('global_dimensions', data);
        console.log(`[Dimensions] 📏 Đã nạp ${data.length || 0} quy tắc vào RAM.`);
    },

    getDimensions: () => {
        return sessions.get('global_dimensions') || [];
    }
};

module.exports = sessionManager;