Skip to content

Commit

Permalink
feat: add option to aggregate similar products reviews
Browse files Browse the repository at this point in the history
  • Loading branch information
pedrobernardina committed Jun 5, 2024
1 parent a23edca commit d05d067
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 28 deletions.
16 changes: 14 additions & 2 deletions verified-reviews/loaders/productDetailsPage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions verified-reviews/loaders/productReviews.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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[];
};

/**
Expand All @@ -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,
Expand Down
19 changes: 8 additions & 11 deletions verified-reviews/utils/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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<VerifiedReviewsFullReview> => {
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 [
Expand All @@ -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) {
Expand Down
36 changes: 23 additions & 13 deletions verified-reviews/utils/transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 => ({
Expand Down

0 comments on commit d05d067

Please sign in to comment.