Commit 355c8bdd authored by Vũ Hoàng Anh's avatar Vũ Hoàng Anh

fix: ensure price fields always have numeric values - Add default 0 for...

fix: ensure price fields always have numeric values - Add default 0 for price/sale_price to prevent FE toLocaleString errors - Add debug logging in controller and data_retrieval_tool
parent c05cc0ce
...@@ -124,8 +124,16 @@ async def chat_controller( ...@@ -124,8 +124,16 @@ async def chat_controller(
# Parse Response # Parse Response
all_product_ids = extract_product_ids(result.get("messages", [])) all_product_ids = extract_product_ids(result.get("messages", []))
logger.info("🔍 [DEBUG] all_product_ids count: %s", len(all_product_ids))
if all_product_ids:
logger.info("🔍 [DEBUG] First product from tool: %s", all_product_ids[0])
ai_raw_content = result.get("ai_response").content if result.get("ai_response") else "" ai_raw_content = result.get("ai_response").content if result.get("ai_response") else ""
ai_text_response, final_product_ids = parse_ai_response(ai_raw_content, all_product_ids) ai_text_response, final_product_ids = parse_ai_response(ai_raw_content, all_product_ids)
logger.info("🔍 [DEBUG] final_product_ids count: %s, type: %s", len(final_product_ids), type(final_product_ids[0]) if final_product_ids else "empty")
if final_product_ids:
logger.info("🔍 [DEBUG] First final product: %s", final_product_ids[0])
response_payload = { response_payload = {
"ai_response": ai_text_response, "ai_response": ai_text_response,
......
...@@ -417,4 +417,4 @@ Xem ảnh bên dưới!", ...@@ -417,4 +417,4 @@ Xem ảnh bên dưới!",
✅ Kiểm tra tên SP trước khi giới thiệu ✅ Kiểm tra tên SP trước khi giới thiệu
✅ Sai loại → Nói thẳng "shop chưa có X" ✅ Sai loại → Nói thẳng "shop chưa có X"
✅ Trả lời về SP → **Luôn có `product_ids`** (kể cả không gọi tool) ✅ Trả lời về SP → **Luôn có `product_ids`** (kể cả không gọi tool)
✅ Không có data = Không nói ✅ Không có data = Không nói
\ No newline at end of file \ No newline at end of file
...@@ -192,6 +192,13 @@ async def _execute_single_search(db, item: SearchItem, query_vector: list[float] ...@@ -192,6 +192,13 @@ async def _execute_single_search(db, item: SearchItem, query_vector: list[float]
db_time, db_time,
query_build_time + db_time, query_build_time + db_time,
) )
# Debug: Log first product to see fields
if products:
first_p = products[0]
logger.info("🔍 [DEBUG] First product keys: %s", list(first_p.keys()))
logger.info("🔍 [DEBUG] First product price: %s, sale_price: %s",
first_p.get("original_price"), first_p.get("sale_price"))
return _format_product_results(products) return _format_product_results(products)
except Exception as e: except Exception as e:
...@@ -214,12 +221,12 @@ def _format_product_results(products: list[dict]) -> list[dict]: ...@@ -214,12 +221,12 @@ def _format_product_results(products: list[dict]) -> list[dict]:
{ {
"sku": p.get("internal_ref_code"), "sku": p.get("internal_ref_code"),
"name": parsed.get("product_name", ""), "name": parsed.get("product_name", ""),
"price": p.get("original_price"), "price": p.get("original_price") or 0,
"sale_price": p.get("sale_price"), "sale_price": p.get("sale_price") or 0,
"url": parsed.get("product_web_url", ""), "url": parsed.get("product_web_url", ""),
"thumbnail_image_url": parsed.get("product_image_url_thumbnail", ""), "thumbnail_image_url": parsed.get("product_image_url_thumbnail", ""),
"discount_amount": p.get("discount_amount"), "discount_amount": p.get("discount_amount") or 0,
"max_score": p.get("max_score"), "max_score": p.get("max_score") or 0,
} }
) )
......
...@@ -1232,24 +1232,30 @@ ...@@ -1232,24 +1232,30 @@
const priceDiv = document.createElement('div'); const priceDiv = document.createElement('div');
priceDiv.className = 'product-price'; priceDiv.className = 'product-price';
if (product.sale_price && product.sale_price < product.price) { if (product.sale_price && product.price && product.sale_price < product.price) {
// Show original price with strikethrough // Show original price with strikethrough
const originalPrice = document.createElement('span'); const originalPrice = document.createElement('span');
originalPrice.className = 'price-original'; originalPrice.className = 'price-original';
originalPrice.innerText = product.price.toLocaleString('vi-VN') + 'đ'; originalPrice.innerText = (product.price || 0).toLocaleString('vi-VN') + 'đ';
priceDiv.appendChild(originalPrice); priceDiv.appendChild(originalPrice);
// Show sale price // Show sale price
const salePrice = document.createElement('span'); const salePrice = document.createElement('span');
salePrice.className = 'price-sale'; salePrice.className = 'price-sale';
salePrice.innerText = product.sale_price.toLocaleString('vi-VN') + 'đ'; salePrice.innerText = (product.sale_price || 0).toLocaleString('vi-VN') + 'đ';
priceDiv.appendChild(salePrice); priceDiv.appendChild(salePrice);
} else { } else if (product.price) {
// Show regular price // Show regular price
const regularPrice = document.createElement('span'); const regularPrice = document.createElement('span');
regularPrice.className = 'price-regular'; regularPrice.className = 'price-regular';
regularPrice.innerText = product.price.toLocaleString('vi-VN') + 'đ'; regularPrice.innerText = (product.price || 0).toLocaleString('vi-VN') + 'đ';
priceDiv.appendChild(regularPrice); priceDiv.appendChild(regularPrice);
} else {
// No price available
const noPrice = document.createElement('span');
noPrice.className = 'price-regular';
noPrice.innerText = 'Liên hệ';
priceDiv.appendChild(noPrice);
} }
body.appendChild(priceDiv); body.appendChild(priceDiv);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment