Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add option to aggregate similar products to verified reviews #634

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

pedrobernardina
Copy link
Contributor

@pedrobernardina pedrobernardina commented May 31, 2024

In some segments it's common to register product variations as similar products instead of different SKUs. For instance, it's useful to register different colours of t-shirts as different products, otherwise the "colour - size" sku variations would be greater than some e-commerce support, although, in the end, they are the same product (e.g Camiseta Manga Curta Algodão Peruano). In these cases it's useful to aggregate the reviews of all similar products.

This PR makes some changes on the verified reviews loader and client in order to support this feature. The final rating is an weighted average of the rating of all similar products by their reviews count.

Obs: This branch was created from the one of #633 , so checking only the last commit will be easier to review the changes.

Obs2: There is a change on one method (client.reviews) that is breaking. The argument name was changed from "productId" to "productsIds". We can (i) add the latter as a new argument and change the logic a bit for it no to be breaking anymore; OR just keep the argument name as "productId" and extend it's type to "string | string[]", and check it before passing it to the function. The latter is cleaner, but the argument name is misleading.

This can be tested in the Admin by enabling the new option "aggregateSimilarProducts" and running the loader.

A local version (created on our repository) of both #633 and this PR can be seen in the preview bellow.

https://deco-sites-simples-f7ec095wncm9.deno.dev/camiseta-manga-curta-algodao-peruano-branca-0077065014/p

@pedrobernardina pedrobernardina force-pushed the feat/verified-reviews-aggregate-similars branch from b729cb7 to d05d067 Compare June 5, 2024 14:52
Comment on lines +14 to +23
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;
});
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use reduce:

const { weightedRating, totalRatings } = Object.entries(ratings ?? {}).reduce(
  (acc, [_, ratingDetails]) => {
    const count = Number(ratingDetails.count);
    const value = Number(parseFloat(ratingDetails.rate).toFixed(1));

    acc.totalRatings += count;
    acc.weightedRating += count * value;

    return acc;
  },
  { weightedRating: 0, totalRatings: 0 }
);

@matheusgr
Copy link
Contributor

@pedrobernardina Can you apply the @gabrielMatosBoubee suggestion?

@guitavano
Copy link
Contributor

Up @pedrobernardina

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants