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,9 +124,17 @@ async def chat_controller(
# Parse Response
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_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 = {
"ai_response": ai_text_response,
"product_ids": final_product_ids,
......
......@@ -193,6 +193,13 @@ async def _execute_single_search(db, item: SearchItem, query_vector: list[float]
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)
except Exception as e:
logger.exception("Single search error for item %r: %s", item, e)
......@@ -214,12 +221,12 @@ def _format_product_results(products: list[dict]) -> list[dict]:
{
"sku": p.get("internal_ref_code"),
"name": parsed.get("product_name", ""),
"price": p.get("original_price"),
"sale_price": p.get("sale_price"),
"price": p.get("original_price") or 0,
"sale_price": p.get("sale_price") or 0,
"url": parsed.get("product_web_url", ""),
"thumbnail_image_url": parsed.get("product_image_url_thumbnail", ""),
"discount_amount": p.get("discount_amount"),
"max_score": p.get("max_score"),
"discount_amount": p.get("discount_amount") or 0,
"max_score": p.get("max_score") or 0,
}
)
......
......@@ -1232,24 +1232,30 @@
const priceDiv = document.createElement('div');
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
const originalPrice = document.createElement('span');
originalPrice.className = 'price-original';
originalPrice.innerText = product.price.toLocaleString('vi-VN') + 'đ';
originalPrice.innerText = (product.price || 0).toLocaleString('vi-VN') + 'đ';
priceDiv.appendChild(originalPrice);
// Show sale price
const salePrice = document.createElement('span');
salePrice.className = 'price-sale';
salePrice.innerText = product.sale_price.toLocaleString('vi-VN') + 'đ';
salePrice.innerText = (product.sale_price || 0).toLocaleString('vi-VN') + 'đ';
priceDiv.appendChild(salePrice);
} else {
} else if (product.price) {
// Show regular price
const regularPrice = document.createElement('span');
regularPrice.className = 'price-regular';
regularPrice.innerText = product.price.toLocaleString('vi-VN') + 'đ';
regularPrice.innerText = (product.price || 0).toLocaleString('vi-VN') + 'đ';
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);
......
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