phanquocdat123 commited on
Commit
8b8fbc5
·
verified ·
1 Parent(s): 0408a89

Create cart.js

Browse files
Files changed (1) hide show
  1. processors/logicHandler/cart.js +33 -0
processors/logicHandler/cart.js ADDED
@@ -0,0 +1,33 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ const cartHandler = {
2
+ handle: (session) => {
3
+ // 1. Kiểm tra giỏ hàng
4
+ if (!session.cart || session.cart.length === 0) {
5
+ return "Dạ giỏ hàng của Mẹ đang trống trơn à. Mẹ xem mẫu nào ưng thì nhấn **[CHỌN]** để em bỏ vào giỏ cho nhen! 🛒";
6
+ }
7
+
8
+ let total = 0;
9
+ let summary = "🛒 **GIỎ HÀNG CỦA MẸ:**\n";
10
+ summary += "──────────────────\n";
11
+
12
+ session.cart.forEach((item, index) => {
13
+ // Xử lý giá: Nếu item.price là số thì dùng luôn, nếu là chuỗi (150k, 150.000đ) thì mới lọc số
14
+ const priceString = String(item.price || "0");
15
+ const priceValue = parseInt(priceString.replace(/\D/g, '')) || 0;
16
+
17
+ summary += `${index + 1}. **${item.code}** - ${item.name}\n 💰 Giá: ${item.price}\n`;
18
+ total += priceValue;
19
+ });
20
+
21
+ // 2. Định dạng hiển thị tổng tiền
22
+ const formattedTotal = total > 0 ? `${total.toLocaleString('vi-VN')}đ` : "Liên hệ shop";
23
+
24
+ summary += `──────────────────\n`;
25
+ summary += `💰 **TỔNG CỘNG: ${formattedTotal}**\n\n`;
26
+ summary += `👉 Mẹ nhắn **"Chốt đơn"** để em lên đơn giao ngay nhen!\n`;
27
+ summary += `*(Hoặc nhắn "Xóa" nếu Mẹ muốn chọn lại từ đầu ạ)*`;
28
+
29
+ return summary;
30
+ }
31
+ };
32
+
33
+ module.exports = cartHandler;