Create cartFormatter.js
Browse files- processors/cartFormatter.js +53 -0
processors/cartFormatter.js
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
/**
|
| 2 |
+
* Xây dựng nội dung giỏ hàng cho shop Hương Kid
|
| 3 |
+
* Đã fix lỗi hiển thị Size và tối ưu nút bấm
|
| 4 |
+
*/
|
| 5 |
+
function buildCartReply(session, header, db) {
|
| 6 |
+
const { subTotal = 0, shipFee = 30000, finalTotal = 0 } = session.totals || {};
|
| 7 |
+
const inventory = db || {};
|
| 8 |
+
let summary = `${header}\n\n🛒 **GIỎ HÀNG CỦA MẸ:**\n────────────────\n`;
|
| 9 |
+
|
| 10 |
+
if (!session.cart || session.cart.length === 0) {
|
| 11 |
+
return summary + "Dạ hiện tại giỏ hàng đang trống nhen Mẹ! [🛍️ XEM MẪU | ACTION_CONSULT]";
|
| 12 |
+
}
|
| 13 |
+
|
| 14 |
+
session.cart.forEach((item, index) => {
|
| 15 |
+
const cleanCode = item.code.trim().toUpperCase();
|
| 16 |
+
const productInStock = inventory[cleanCode];
|
| 17 |
+
|
| 18 |
+
// FIX: Chỉ lấy phần số của Size (Ví dụ "G02:6" -> "6")
|
| 19 |
+
let displaySize = String(item.size || "⚠️");
|
| 20 |
+
if (displaySize.includes(':')) {
|
| 21 |
+
displaySize = displaySize.split(':').pop();
|
| 22 |
+
}
|
| 23 |
+
|
| 24 |
+
summary += `${index + 1}. ${item.name}\n`;
|
| 25 |
+
summary += `🔹 Mã: **${item.code}** - Size: **${displaySize}**\n`;
|
| 26 |
+
summary += `💰 Giá: ${item.price.toLocaleString('vi-VN')}đ\n`;
|
| 27 |
+
|
| 28 |
+
// Render nút đổi size siêu gọn
|
| 29 |
+
if (productInStock && productInStock.category) {
|
| 30 |
+
const availableSizes = String(productInStock.category).split(',').map(s => s.trim()).filter(Boolean);
|
| 31 |
+
const otherSizes = availableSizes.filter(s => s !== displaySize);
|
| 32 |
+
|
| 33 |
+
if (otherSizes.length > 0) {
|
| 34 |
+
// Nhãn chỉ để số s, lệnh ẩn phía sau dấu |
|
| 35 |
+
const sizeButtons = otherSizes.map(s => `[${s}|ACTION_SIZE:${item.code}:${s}]`).join(" ");
|
| 36 |
+
summary += `🛠️ Đổi size: ${sizeButtons}\n`;
|
| 37 |
+
}
|
| 38 |
+
}
|
| 39 |
+
|
| 40 |
+
summary += `🗑️ [Xóa|ACTION_REMOVE:${item.code}]\n────────────────\n`;
|
| 41 |
+
});
|
| 42 |
+
|
| 43 |
+
summary += `💵 Tạm tính: ${subTotal.toLocaleString('vi-VN')}đ\n`;
|
| 44 |
+
summary += `🚚 Ship: ${shipFee.toLocaleString('vi-VN')}đ\n`;
|
| 45 |
+
summary += `👉 **TỔNG: ${finalTotal.toLocaleString('vi-VN')}đ**\n\n`;
|
| 46 |
+
|
| 47 |
+
// Nút điều hướng chính
|
| 48 |
+
summary += `[✅ CHỐT ĐƠN|ACTION_CONFIRM] [🛍️ XEM TIẾP|ACTION_CONSULT]`;
|
| 49 |
+
|
| 50 |
+
return summary;
|
| 51 |
+
}
|
| 52 |
+
|
| 53 |
+
module.exports = { buildCartReply };
|