-
Notifications
You must be signed in to change notification settings - Fork 63
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: rating distribution #2687
base: feat/reviews-and-ratings
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,9 @@ | ||
export interface ProductRating { | ||
average: number | ||
totalCount: number | ||
starsOne: number | ||
starsTwo: number | ||
starsThree: number | ||
starsFour: number | ||
starsFive: number | ||
} |
Original file line number | Diff line number | Diff line change | ||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -31,6 +31,7 @@ import { | |||||||||||||||||
ProductReviewsInputOrderBy, | ||||||||||||||||||
type ProductReviewsInputOrderWay, | ||||||||||||||||||
} from '../clients/commerce/types/ProductReview' | ||||||||||||||||||
import { buildRatingDistribution } from '../utils/rating' | ||||||||||||||||||
|
||||||||||||||||||
export const Query = { | ||||||||||||||||||
product: async (_: unknown, { locator }: QueryProductArgs, ctx: Context) => { | ||||||||||||||||||
|
@@ -81,7 +82,7 @@ export const Query = { | |||||||||||||||||
|
||||||||||||||||||
const rating = await commerce.rating(sku.itemId) | ||||||||||||||||||
|
||||||||||||||||||
sku.rating = rating | ||||||||||||||||||
sku.rating = buildRatingDistribution(rating) | ||||||||||||||||||
|
||||||||||||||||||
return sku | ||||||||||||||||||
} catch (err) { | ||||||||||||||||||
|
@@ -113,7 +114,7 @@ export const Query = { | |||||||||||||||||
|
||||||||||||||||||
const enhancedSku = enhanceSku(sku, product) | ||||||||||||||||||
|
||||||||||||||||||
enhancedSku.rating = rating | ||||||||||||||||||
enhancedSku.rating = buildRatingDistribution(rating) | ||||||||||||||||||
|
||||||||||||||||||
return enhancedSku | ||||||||||||||||||
} | ||||||||||||||||||
|
@@ -137,6 +138,15 @@ export const Query = { | |||||||||||||||||
}: QuerySearchArgs, | ||||||||||||||||||
ctx: Context | ||||||||||||||||||
) => { | ||||||||||||||||||
console.log('search', { | ||||||||||||||||||
first, | ||||||||||||||||||
after: maybeAfter, | ||||||||||||||||||
sort, | ||||||||||||||||||
term, | ||||||||||||||||||
selectedFacets, | ||||||||||||||||||
sponsoredCount, | ||||||||||||||||||
}) | ||||||||||||||||||
|
||||||||||||||||||
Comment on lines
+141
to
+149
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. todo: remember to delete the console log
Suggested change
|
||||||||||||||||||
// Insert channel in context for later usage | ||||||||||||||||||
const channel = findChannel(selectedFacets) | ||||||||||||||||||
const locale = findLocale(selectedFacets) | ||||||||||||||||||
|
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
@@ -0,0 +1,68 @@ | ||||
import type { ProductRating as ApiClientProductRating } from '../clients/commerce/types/ProductRating' | ||||
import type { ProductRating } from './enhanceSku' | ||||
|
||||
export function buildRatingDistribution( | ||||
apiClientRating: ApiClientProductRating | ||||
): ProductRating { | ||||
const rating: ProductRating = { | ||||
average: apiClientRating.average, | ||||
totalCount: apiClientRating.totalCount, | ||||
distribution: { | ||||
1: 0, | ||||
2: 0, | ||||
3: 0, | ||||
4: 0, | ||||
5: 0, | ||||
}, | ||||
} | ||||
|
||||
if (rating.totalCount === 0) { | ||||
return rating | ||||
} | ||||
|
||||
const percentages = [ | ||||
calculateIntegerPercentage(apiClientRating.starsOne, rating.totalCount), | ||||
calculateIntegerPercentage(apiClientRating.starsTwo, rating.totalCount), | ||||
calculateIntegerPercentage(apiClientRating.starsThree, rating.totalCount), | ||||
calculateIntegerPercentage(apiClientRating.starsFour, rating.totalCount), | ||||
calculateIntegerPercentage(apiClientRating.starsFive, rating.totalCount), | ||||
] | ||||
|
||||
const totalPercentage = percentages.reduce((acc, curr) => acc + curr, 0) | ||||
|
||||
if (totalPercentage !== 100) { | ||||
const missingPercentage = 100 - totalPercentage | ||||
const [maxValue, matchedIndexes] = findMaxInArray(percentages) | ||||
|
||||
const changingIndex = | ||||
missingPercentage > 0 | ||||
? Math.max(...matchedIndexes) | ||||
: Math.min(...matchedIndexes) | ||||
|
||||
percentages[changingIndex] = maxValue + missingPercentage | ||||
} | ||||
|
||||
percentages.forEach( | ||||
(percentage, index) => (rating.distribution[index + 1] = percentage) | ||||
) | ||||
|
||||
console.log('distribution', rating.distribution) | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. todo: remember to delete the console log
Suggested change
|
||||
|
||||
return rating | ||||
} | ||||
|
||||
function calculateIntegerPercentage(value: number, total: number): number { | ||||
return Math.round((value / total) * 100) | ||||
} | ||||
|
||||
function findMaxInArray(arr: number[]): [number, number[]] { | ||||
const maxValue = Math.max(...arr) | ||||
const matchedIndexes = arr.reduce((acc: number[], curr, index) => { | ||||
if (curr === maxValue) { | ||||
acc.push(index) | ||||
} | ||||
return acc | ||||
}, []) | ||||
|
||||
return [maxValue, matchedIndexes] | ||||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -156,6 +156,19 @@ export type DeliveryIds = { | |
warehouseId: Maybe<Scalars['String']['output']> | ||
} | ||
|
||
export type ICreateProductReview = { | ||
/** Product ID. */ | ||
productId: Scalars['String']['input'] | ||
/** Review rating. */ | ||
rating: Scalars['Int']['input'] | ||
/** Review author name. */ | ||
reviewerName: Scalars['String']['input'] | ||
/** Review content. */ | ||
text: Scalars['String']['input'] | ||
/** Review title. */ | ||
title: Scalars['String']['input'] | ||
} | ||
|
||
export type IGeoCoordinates = { | ||
/** The latitude of the geographic coordinates. */ | ||
latitude: Scalars['Float']['input'] | ||
|
@@ -954,6 +967,8 @@ export type StoreProduct = { | |
offers: StoreAggregateOffer | ||
/** Product ID, such as [ISBN](https://www.isbn-international.org/content/what-isbn) or similar global IDs. */ | ||
productID: Scalars['String']['output'] | ||
/** Product rating. */ | ||
rating: StoreProductRating | ||
/** The product's release date. Formatted using https://en.wikipedia.org/wiki/ISO_8601 */ | ||
releaseDate: Scalars['String']['output'] | ||
/** Array with review information. */ | ||
|
@@ -1008,6 +1023,13 @@ export type StoreProductGroup = { | |
skuVariants: Maybe<SkuVariants> | ||
} | ||
|
||
export type StoreProductRating = { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. chore: This file is outdated, could you run the generate command again to update it pls? |
||
/** Product average rating. */ | ||
average: Scalars['Float']['output'] | ||
/** Product amount of ratings received. */ | ||
totalCount: Scalars['Int']['output'] | ||
} | ||
|
||
/** Properties that can be associated with products and products groups. */ | ||
export type StorePropertyValue = { | ||
/** Property name. */ | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
suggestion: use destructuring to make it more direct, what do you think?