phanquocdat123 commited on
Commit
760daa2
·
verified ·
1 Parent(s): 827d885

Create utils/helpers.js

Browse files
Files changed (1) hide show
  1. utils/helpers.js +67 -0
utils/helpers.js ADDED
@@ -0,0 +1,67 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ const helpers = {
2
+ removeAccents: (str) => {
3
+ if (!str) return "";
4
+ return str.normalize('NFD').replace(/[\u0300-\u036f]/g, '').replace(/đ/g, 'd').replace(/Đ/g, 'D');
5
+ },
6
+
7
+ mapWeightToSize: (weightStr) => {
8
+ const w = parseInt(weightStr);
9
+ if (isNaN(w) || w < 5) return null;
10
+ if (w >= 5 && w <= 10) return "1";
11
+ if (w > 10 && w <= 12) return "2";
12
+ if (w > 12 && w <= 14) return "3";
13
+ if (w > 14 && w <= 16) return "4";
14
+ if (w > 16 && w <= 18) return "5";
15
+ if (w > 18 && w <= 20) return "6";
16
+ if (w > 20 && w <= 22) return "7";
17
+ if (w > 22 && w <= 25) return "8";
18
+ if (w > 25 && w <= 30) return "10";
19
+ if (w > 30 && w <= 35) return "12";
20
+ if (w > 35 && w <= 40) return "14";
21
+ if (w > 40 && w <= 48) return "S";
22
+ if (w > 48 && w <= 55) return "M";
23
+ if (w > 55 && w <= 62) return "L";
24
+ if (w > 62 && w <= 70) return "XL";
25
+ return "2XL";
26
+ },
27
+
28
+ formatReply: (reply, session, globalInventory = null) => {
29
+ if (!reply) return "";
30
+ let finalReply = reply;
31
+
32
+ const entities = session.entities || {};
33
+ const { productCode, weight, phone, address } = entities;
34
+ const inventory = (globalInventory && globalInventory.inventory) ? globalInventory.inventory : (globalInventory || {});
35
+
36
+ // A. Xử lý Địa chỉ & SĐT
37
+ finalReply = finalReply.replace(/{{dia_chi}}|{address}/g, address || "địa chỉ cũ của Mẹ");
38
+ finalReply = finalReply.replace(/{{sdt}}|{phone}/g, phone || "số điện thoại nhen");
39
+
40
+ // B. Xử lý Mã hàng
41
+ const code = productCode ? String(productCode).toUpperCase() : null;
42
+ finalReply = finalReply.replace(/{productCode}|{{ma_hang}}/g, code || "mẫu này");
43
+
44
+ // C. Xử lý Size
45
+ const size = weight ? helpers.mapWeightToSize(weight) : null;
46
+ finalReply = finalReply.replace(/{size}|{{size}}/g, size ? `Size ${size}` : "size chuẩn");
47
+
48
+ // D. Xử lý Ảnh
49
+ if (finalReply.includes("{image_link}")) {
50
+ const img = (code && inventory[code]) ? inventory[code].image : null;
51
+ finalReply = finalReply.replace(/{image_link}/g, img ? `📸 Ảnh: ${img}` : "tại kho nhen!");
52
+ }
53
+
54
+ // --- E. FIX NÚT BẤM DÀI & LẶP ICON ---
55
+ // 1. Dọn dẹp icon ✨ bị nhân đôi (lỗi trong ảnh Đạt gửi)
56
+ finalReply = finalReply.replace(/✨\s*✨/g, '✨');
57
+
58
+ // 2. Ép cấu trúc [Label|Command] - Xóa khoảng trắng để nút co lại tối đa
59
+ finalReply = finalReply.replace(/\[\s*([^\]|]+)\s*\|\s*([^\]]+)\s*\]/g, (match, label, command) => {
60
+ return `[${label.trim()}|${command.trim()}]`;
61
+ });
62
+
63
+ return finalReply.replace(/\s\s+/g, ' ').trim();
64
+ }
65
+ };
66
+
67
+ module.exports = helpers;