phanquocdat123 commited on
Commit
f18917e
·
verified ·
1 Parent(s): 1868e4f

Create commandTraps.js

Browse files
Files changed (1) hide show
  1. processors/commandTraps.js +114 -0
processors/commandTraps.js ADDED
@@ -0,0 +1,114 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ const { buildCartReply } = require('./cartFormatter');
2
+
3
+ async function handleTraps(text, userId, session, db, groups, sessionManager) {
4
+ const rawText = (text || "").toUpperCase().trim();
5
+
6
+ // --- 1. BẪY ĐỔI SIZE (Ưu tiên cao nhất để tránh bị trôi xuống hiển thị mẫu) ---
7
+ // Cấu trúc: ACTION_SIZE:MÃ:SIZE (Ví dụ: ACTION_SIZE:G02:12)
8
+ if (rawText.includes("ACTION_SIZE:")) {
9
+ const parts = rawText.split(':');
10
+ const pCode = parts[1]?.trim();
11
+ const newSize = parts[2]?.trim();
12
+
13
+ if (pCode && newSize && Array.isArray(session.cart)) {
14
+ // Tìm món đồ cuối cùng hoặc món đồ có mã pCode trong giỏ để đổi size
15
+ const itemIndex = session.cart.findLastIndex(i => i.code.toUpperCase() === pCode.toUpperCase());
16
+
17
+ if (itemIndex !== -1) {
18
+ session.cart[itemIndex].size = newSize;
19
+
20
+ // Tính toán lại tiền (đề phòng giá có thể thay đổi theo size nếu sau này Đạt cần)
21
+ const subTotal = session.cart.reduce((sum, item) => sum + (Number(item.price) || 0), 0);
22
+ session.totals = {
23
+ subTotal: subTotal,
24
+ shipFee: 30000,
25
+ finalTotal: subTotal + 30000
26
+ };
27
+
28
+ await sessionManager.update(userId, { cart: session.cart, totals: session.totals });
29
+
30
+ return {
31
+ reply: buildCartReply(session, `✅ Đã đổi mã **${pCode}** sang **Size ${newSize}** thành công cho Mẹ nhen!`, db)
32
+ };
33
+ }
34
+ }
35
+ }
36
+
37
+ // --- 2. BẪY THÊM HÀNG (Hỗ trợ: "chọn G02" hoặc bấm nút ACTION_ADD:G02) ---
38
+ if (rawText.startsWith("CHỌN ") || rawText.includes("ACTION_ADD:")) {
39
+ const code = rawText.replace("CHỌN ", "").replace("ACTION_ADD:", "").split(/\s/)[0];
40
+
41
+ if (db[code]) {
42
+ const product = db[code];
43
+ const availableSizes = String(product.category || "").split(',').map(s => s.trim());
44
+
45
+ // Ưu tiên size mẹ đã chọn trước đó, nếu không lấy size tư vấn hoặc size mặc định
46
+ const finalSize = session.entities?.size || availableSizes[0] || "10";
47
+
48
+ if (!Array.isArray(session.cart)) session.cart = [];
49
+
50
+ session.cart.push({
51
+ code,
52
+ name: product.name,
53
+ price: Number(product.price) || 0,
54
+ size: finalSize
55
+ });
56
+
57
+ const subTotal = session.cart.reduce((sum, item) => sum + item.price, 0);
58
+ session.totals = {
59
+ subTotal: subTotal,
60
+ shipFee: 30000,
61
+ finalTotal: subTotal + 30000
62
+ };
63
+
64
+ await sessionManager.update(userId, { cart: session.cart, totals: session.totals });
65
+
66
+ return { reply: buildCartReply(session, `✅ Đã thêm mã **${code}** vào giỏ cho Mẹ nhen!`, db) };
67
+ }
68
+ }
69
+
70
+ // --- 3. BẪY XÓA MÓN (Hỗ trợ nút bấm ACTION_REMOVE) ---
71
+ if (rawText.includes("XOA:") || rawText.includes("ACTION_REMOVE:")) {
72
+ const pCode = rawText.split(":")[1]?.trim().replace(/[\[\]]/g, "");
73
+ if (pCode && Array.isArray(session.cart)) {
74
+ session.cart = session.cart.filter(item => item.code.toUpperCase() !== pCode);
75
+
76
+ const subTotal = session.cart.reduce((sum, item) => sum + (Number(item.price) || 0), 0);
77
+ session.totals = { subTotal, shipFee: 30000, finalTotal: subTotal + 30000 };
78
+
79
+ await sessionManager.update(userId, { cart: session.cart, totals: session.totals });
80
+ return { reply: buildCartReply(session, `🗑️ Đã xóa mã **${pCode}** khỏi giỏ hàng ạ!`, db) };
81
+ }
82
+ }
83
+
84
+ // --- 4. BẪY XEM GIỎ HÀNG ---
85
+ if (rawText === "GIỎ HÀNG" || rawText.includes("ACTION_VIEW_CART")) {
86
+ return { reply: buildCartReply(session, "🌸 Giỏ hàng của Mẹ hiện tại nè:", db) };
87
+ }
88
+
89
+ // --- 5. BẪY DANH MỤC/MENU ---
90
+
91
+ if (rawText === "DANH MỤC" || rawText === "MENU" || rawText.includes("ACTION_CONSULT")) {
92
+
93
+ // 1. Tự động lấy danh sách nhóm từ kho hàng (db) nếu groups truyền vào bị thiếu
94
+ let currentGroups = groups;
95
+ if (!Array.isArray(currentGroups) || currentGroups.length === 0) {
96
+ currentGroups = [...new Set(Object.values(db).map(item => item.group).filter(Boolean))];
97
+ }
98
+
99
+ // 2. Tạo chuỗi nút bấm
100
+ if (currentGroups.length > 0) {
101
+ const menuButtons = currentGroups.map(g => `[${g.toUpperCase()}]`).join(" ");
102
+ return {
103
+ reply: `Dạ shop Hương Kid có các nhóm hàng này, Mẹ chọn xem nhóm nào để em gửi mẫu nhen:\n\n${menuButtons}\n\n*(Mẹ bấm vào nút để xem mẫu ạ!)*`
104
+ };
105
+ } else {
106
+ // Trường hợp kho hàng gặp sự cố không lấy được nhóm
107
+ return { reply: "Dạ hiện shop đang cập nhật mẫu mới, Mẹ nhắn 'Bé trai' hoặc 'Bé gái' để em tư vấn trực tiếp nhen! ❤️" };
108
+ }
109
+ }
110
+
111
+ return null;
112
+ }
113
+
114
+ module.exports = { handleTraps };