diff --git a/verified-reviews/loaders/productDetailsPage.ts b/verified-reviews/loaders/productDetailsPage.ts index 917afa83a..a44bb6615 100644 --- a/verified-reviews/loaders/productDetailsPage.ts +++ b/verified-reviews/loaders/productDetailsPage.ts @@ -6,7 +6,10 @@ import { getProductId, PaginationOptions, } from "../utils/client.ts"; -export type Props = PaginationOptions; + +export type Props = PaginationOptions & { + aggregateSimilarProducts?: boolean; +}; /** * @title OpiniƵes verificadas - Full Review for Product (Ratings and Reviews) @@ -27,8 +30,17 @@ export default function productDetailsPage( } const productId = getProductId(productDetailsPage.product); + let productsToGetReviews = [productId]; + + if (config.aggregateSimilarProducts) { + productsToGetReviews = [ + productId, + ...productDetailsPage.product.isSimilarTo?.map(getProductId) ?? [], + ]; + } + const fullReview = await client.fullReview({ - productId, + productsIds: productsToGetReviews, count: config?.count, offset: config?.offset, order: config?.order, diff --git a/verified-reviews/loaders/productReviews.ts b/verified-reviews/loaders/productReviews.ts index 400e38492..257902017 100644 --- a/verified-reviews/loaders/productReviews.ts +++ b/verified-reviews/loaders/productReviews.ts @@ -4,7 +4,7 @@ import { createClient, PaginationOptions } from "../utils/client.ts"; import { toReview } from "../utils/transform.ts"; export type Props = PaginationOptions & { - productId: string; + productsIds: string[]; }; /** @@ -22,7 +22,7 @@ export default async function productReviews( } const reviewsResponse = await client.reviews({ - productId: config.productId, + productsIds: config.productsIds, count: config?.count, offset: config?.offset, order: config?.order, diff --git a/verified-reviews/utils/client.ts b/verified-reviews/utils/client.ts index 1b9bb98ea..1cd1a4401 100644 --- a/verified-reviews/utils/client.ts +++ b/verified-reviews/utils/client.ts @@ -91,16 +91,16 @@ export const createClient = (params: ConfigVerifiedReviews | undefined) => { /** @description https://documenter.getpostman.com/view/2336519/SVzw6MK5#daf51360-c79e-451a-b627-33bdd0ef66b8 */ const reviews = ({ - productId, + productsIds, count = 5, offset = 0, order = "date_desc", }: PaginationOptions & { - productId: string; + productsIds: string[]; }) => { const payload = { query: "reviews", - product: productId, + product: productsIds, idWebsite: idWebsite, plateforme: "br", offset: offset, @@ -115,17 +115,17 @@ export const createClient = (params: ConfigVerifiedReviews | undefined) => { }; const fullReview = async ({ - productId, + productsIds, count = 5, offset = 0, order, }: PaginationOptions & { - productId: string; + productsIds: string[]; }): Promise => { try { const response = await Promise.all([ - rating({ productId }), - reviews({ productId, count, offset, order }), + ratings({ productsIds }), + reviews({ productsIds, count, offset, order }), ]); const [responseRating, responseReview] = response.flat() as [ @@ -134,10 +134,7 @@ export const createClient = (params: ConfigVerifiedReviews | undefined) => { ]; return { - aggregateRating: getRatingProduct({ - ratings: responseRating, - productId, - }), + aggregateRating: getRatingProduct(responseRating), review: responseReview ? responseReview.reviews?.map(toReview) : [], }; } catch (error) { diff --git a/verified-reviews/utils/transform.ts b/verified-reviews/utils/transform.ts index 67bf683f6..a28260566 100644 --- a/verified-reviews/utils/transform.ts +++ b/verified-reviews/utils/transform.ts @@ -4,24 +4,34 @@ import { } from "../../commerce/types.ts"; import { Ratings, Review } from "./types.ts"; -export const getRatingProduct = ({ - ratings, - productId, -}: { - ratings: Ratings | undefined; - productId: string; -}): AggregateRating | undefined => { - const rating = ratings?.[productId]?.[0]; - if (!rating) { +export const getRatingProduct = ( + ratings: Ratings | undefined, +): AggregateRating | undefined => { + if (!ratings) { return undefined; } - return { + let weightedRating = 0; + let totalRatings = 0; + + Object.entries(ratings ?? {}).forEach(([_, [rating]]) => { + const count = Number(rating.count); + const value = Number(parseFloat(rating.rate).toFixed(1)); + + totalRatings += count; + weightedRating += count * value; + }); + + const aggregateRating: AggregateRating = { "@type": "AggregateRating", - ratingCount: Number(rating.count), - ratingValue: Number(parseFloat(rating.rate).toFixed(1)), - reviewCount: Number(rating.count), + ratingCount: totalRatings, + reviewCount: totalRatings, + ratingValue: totalRatings > 0 + ? Number((weightedRating / totalRatings).toFixed(1)) + : 0, }; + + return aggregateRating; }; export const toReview = (review: Review): CommerceReview => ({