fix(catalog): compute real review rating and review count from product reviews
This commit is contained in:
parent
200b936eb5
commit
4f3163d14c
2 changed files with 36 additions and 2 deletions
|
|
@ -959,6 +959,23 @@ def get_products(
|
||||||
page_variant_ids.extend([v.variant_id for v in (p.variants or [])])
|
page_variant_ids.extend([v.variant_id for v in (p.variants or [])])
|
||||||
stock_map = get_available_stock_map(page_variant_ids, db)
|
stock_map = get_available_stock_map(page_variant_ids, db)
|
||||||
|
|
||||||
|
# Review statistics map for products on this page
|
||||||
|
ratings_map = {}
|
||||||
|
prod_ids = [p.product_id for p in products_list]
|
||||||
|
if prod_ids:
|
||||||
|
from app.models.ProductReviewModel import ProductReview
|
||||||
|
review_stats = db.execute(
|
||||||
|
select(
|
||||||
|
ProductReview.product_id,
|
||||||
|
func.avg(ProductReview.rating),
|
||||||
|
func.count(ProductReview.review_id)
|
||||||
|
)
|
||||||
|
.where(ProductReview.product_id.in_(prod_ids), ProductReview.is_approved == True)
|
||||||
|
.group_by(ProductReview.product_id)
|
||||||
|
).all()
|
||||||
|
for pid, avg_r, cnt in review_stats:
|
||||||
|
ratings_map[pid] = (round(float(avg_r), 1) if avg_r is not None else 5.0, int(cnt))
|
||||||
|
|
||||||
# Map to lightweight DTO schema
|
# Map to lightweight DTO schema
|
||||||
def _variant_label(v) -> str:
|
def _variant_label(v) -> str:
|
||||||
"""Build a human-readable variant label from its attributes (e.g. 'Moonlight Silver')."""
|
"""Build a human-readable variant label from its attributes (e.g. 'Moonlight Silver')."""
|
||||||
|
|
@ -1025,6 +1042,7 @@ def get_products(
|
||||||
elif first_var and first_var.images and first_var.images[0].image_url:
|
elif first_var and first_var.images and first_var.images[0].image_url:
|
||||||
images_list = [{"image_url": first_var.images[0].image_url}]
|
images_list = [{"image_url": first_var.images[0].image_url}]
|
||||||
|
|
||||||
|
prod_rating, prod_rev_count = ratings_map.get(p.product_id, (5.0, 0))
|
||||||
return ProductCardResponse(
|
return ProductCardResponse(
|
||||||
product_id=p.product_id,
|
product_id=p.product_id,
|
||||||
category_id=p.category_id,
|
category_id=p.category_id,
|
||||||
|
|
@ -1035,7 +1053,8 @@ def get_products(
|
||||||
price=price,
|
price=price,
|
||||||
compare_price=compare_price,
|
compare_price=compare_price,
|
||||||
discount_percent=discount_percent,
|
discount_percent=discount_percent,
|
||||||
rating=p.rating if hasattr(p, "rating") and p.rating is not None else 4.8,
|
rating=prod_rating,
|
||||||
|
review_count=prod_rev_count,
|
||||||
stock_count=stock_count,
|
stock_count=stock_count,
|
||||||
first_variant_id=first_var.variant_id if first_var else None,
|
first_variant_id=first_var.variant_id if first_var else None,
|
||||||
badge=p.badge if hasattr(p, "badge") else None,
|
badge=p.badge if hasattr(p, "badge") else None,
|
||||||
|
|
@ -1144,6 +1163,17 @@ def get_product_detail_by_slug(slug: str, db: Session = Depends(get_db)):
|
||||||
for r in reviews
|
for r in reviews
|
||||||
]
|
]
|
||||||
|
|
||||||
|
valid_ratings = [float(r.rating) for r in reviews if r.rating is not None]
|
||||||
|
if valid_ratings:
|
||||||
|
avg_rating = round(sum(valid_ratings) / len(valid_ratings), 1)
|
||||||
|
total_reviews_count = len(valid_ratings)
|
||||||
|
else:
|
||||||
|
avg_rating = 5.0
|
||||||
|
total_reviews_count = 0
|
||||||
|
|
||||||
|
product_serialized["rating"] = avg_rating
|
||||||
|
product_serialized["reviews_count"] = total_reviews_count
|
||||||
|
|
||||||
# Find related products prioritizing same brand and products with verified images on disk
|
# Find related products prioritizing same brand and products with verified images on disk
|
||||||
def _is_image_verified(p):
|
def _is_image_verified(p):
|
||||||
if not p.images:
|
if not p.images:
|
||||||
|
|
@ -1218,6 +1248,8 @@ def get_product_detail_by_slug(slug: str, db: Session = Depends(get_db)):
|
||||||
response_payload = {
|
response_payload = {
|
||||||
"product": product_serialized,
|
"product": product_serialized,
|
||||||
"reviews": reviews_serialized,
|
"reviews": reviews_serialized,
|
||||||
|
"average_rating": avg_rating,
|
||||||
|
"total_reviews": total_reviews_count,
|
||||||
"related_products": related_serialized,
|
"related_products": related_serialized,
|
||||||
"compatible_devices": compatibles,
|
"compatible_devices": compatibles,
|
||||||
"specifications": [
|
"specifications": [
|
||||||
|
|
|
||||||
|
|
@ -617,6 +617,8 @@ class ProductResponse(BaseModel):
|
||||||
variants: List[ProductVariantResponse] = []
|
variants: List[ProductVariantResponse] = []
|
||||||
locations: List[ProductLocationResponse] = []
|
locations: List[ProductLocationResponse] = []
|
||||||
thumbnail_url: Optional[str] = None
|
thumbnail_url: Optional[str] = None
|
||||||
|
rating: Optional[float] = 5.0
|
||||||
|
reviews_count: Optional[int] = 0
|
||||||
|
|
||||||
@model_validator(mode="after")
|
@model_validator(mode="after")
|
||||||
def populate_images(self) -> 'ProductResponse':
|
def populate_images(self) -> 'ProductResponse':
|
||||||
|
|
@ -668,11 +670,11 @@ class ProductCardResponse(BaseModel):
|
||||||
slug: str
|
slug: str
|
||||||
name: str
|
name: str
|
||||||
thumbnail_url: Optional[str] = None
|
thumbnail_url: Optional[str] = None
|
||||||
images: List[Dict[str, Any]] = []
|
|
||||||
price: Decimal
|
price: Decimal
|
||||||
compare_price: Optional[Decimal] = None
|
compare_price: Optional[Decimal] = None
|
||||||
discount_percent: int = 0
|
discount_percent: int = 0
|
||||||
rating: float = 5.0
|
rating: float = 5.0
|
||||||
|
review_count: int = 0
|
||||||
stock_count: int = 10
|
stock_count: int = 10
|
||||||
first_variant_id: Optional[str] = None
|
first_variant_id: Optional[str] = None
|
||||||
badge: Optional[str] = None
|
badge: Optional[str] = None
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue