phanquocdat123 commited on
Commit
4d5eeb5
·
verified ·
1 Parent(s): 511c028

Create sessionManager.js

Browse files
Files changed (1) hide show
  1. core /sessionManager.js +109 -0
core /sessionManager.js ADDED
@@ -0,0 +1,109 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ const sessions = new Map();
2
+
3
+ // Cấu hình phí ship của Hương Kid
4
+ const SHIPPING_CONFIG = {
5
+ FEE: 30000, // Phí ship mặc định 30k
6
+ FREESHIP_LIMIT: 500000 // Đơn từ 500k được freeship
7
+ };
8
+
9
+ const sessionManager = {
10
+ // --- 1. QUẢN LÝ PHIÊN CHAT (SESSION) ---
11
+
12
+ // Lấy session hiện tại
13
+ get: (id) => sessions.get(id),
14
+
15
+ /**
16
+ * Cập nhật session theo kiểu "Bổ sung" (Patch)
17
+ * Đả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
18
+ */
19
+ update: (id, newData) => {
20
+ let session = sessions.get(id) || {
21
+ id: id,
22
+ cart: [],
23
+ entities: {},
24
+ flags: {},
25
+ totals: {},
26
+ isRecognized: false,
27
+ executionPath: []
28
+ };
29
+ session.id = id;
30
+ // A. XỬ LÝ RIÊNG CHO ENTITIES (Thông tin cá nhân khách hàng)
31
+ if (newData.entities) {
32
+ if (!session.entities) session.entities = {};
33
+
34
+ for (let key in newData.entities) {
35
+ let newVal = newData.entities[key];
36
+ if (newVal !== undefined && newVal !== null && newVal !== "") {
37
+ session.entities[key] = newVal;
38
+ }
39
+ }
40
+ delete newData.entities;
41
+ }
42
+
43
+ // B. XỬ LÝ CHO GIỎ HÀNG
44
+ if (newData.cart) {
45
+ session.cart = newData.cart;
46
+ delete newData.cart;
47
+ }
48
+
49
+ // C. CẬP NHẬT CÁC THÀNH PHẦN KHÁC (winner, flags, zones...)
50
+ Object.assign(session, newData);
51
+
52
+ // D. TỰ ĐỘNG TÍNH TOÁN TÀI CHÍNH (Totals)
53
+ if (session.cart) {
54
+ const subTotal = session.cart.reduce((sum, item) => {
55
+ const price = parseInt(item.price?.toString().replace(/\D/g, '')) || 0;
56
+ return sum + price;
57
+ }, 0);
58
+
59
+ const isEligibleForFreeShip = subTotal >= SHIPPING_CONFIG.FREESHIP_LIMIT;
60
+ const shipFee = (isEligibleForFreeShip || subTotal === 0) ? 0 : SHIPPING_CONFIG.FEE;
61
+
62
+ session.totals = {
63
+ subTotal: subTotal,
64
+ shipFee: shipFee,
65
+ finalTotal: subTotal + shipFee,
66
+ isFreeShip: isEligibleForFreeShip,
67
+ needMoreForFreeShip: isEligibleForFreeShip ? 0 : (SHIPPING_CONFIG.FREESHIP_LIMIT - subTotal)
68
+ };
69
+ }
70
+
71
+ // E. GIỚI HẠN LỊCH SỬ (Tránh tràn RAM)
72
+ if (session.executionPath && session.executionPath.length > 10) {
73
+ session.executionPath = session.executionPath.slice(-10);
74
+ }
75
+
76
+ sessions.set(id, session);
77
+ return session;
78
+ },
79
+
80
+ // Xóa session khi khách chốt đơn thành công
81
+ clear: (id) => {
82
+ sessions.delete(id);
83
+ console.log(`[Session] 🗑️ Đã xóa bộ nhớ phiên của khách: ${id}`);
84
+ },
85
+
86
+ // --- 2. QUẢN LÝ DỮ LIỆU TỔNG (GLOBAL DATA - RAM) ---
87
+
88
+ // KHO HÀNG (Inventory)
89
+ setInventory: (data) => {
90
+ sessions.set('global_inventory', data);
91
+ console.log(`[Inventory] 📦 Đã nạp kho hàng vào RAM hệ thống.`);
92
+ },
93
+
94
+ getInventory: () => {
95
+ return sessions.get('global_inventory') || { inventory: {}, groups: [] };
96
+ },
97
+
98
+ // QUY TẮC / KÍCH THƯỚC (Dimensions) - Sửa lỗi "is not a function"
99
+ setDimensions: (data) => {
100
+ sessions.set('global_dimensions', data);
101
+ console.log(`[Dimensions] 📏 Đã nạp ${data.length || 0} quy tắc vào RAM.`);
102
+ },
103
+
104
+ getDimensions: () => {
105
+ return sessions.get('global_dimensions') || [];
106
+ }
107
+ };
108
+
109
+ module.exports = sessionManager;