From 4d8a98c821982ddf44bdf28bdd7cbee1b3dbc5aa Mon Sep 17 00:00:00 2001 From: MinhhTien <92145479+MinhhTien@users.noreply.github.com> Date: Sun, 16 Jun 2024 23:11:41 +0700 Subject: [PATCH] chore: Add ratingCount product --- src/common/services/discord.service.ts | 1 - src/product/dto/product.dto.ts | 5 ++- src/product/schemas/product.schema.ts | 48 ++++++++++++++++++++++--- src/product/services/product.service.ts | 2 +- src/review/services/review.service.ts | 19 ++++++---- 5 files changed, 62 insertions(+), 13 deletions(-) diff --git a/src/common/services/discord.service.ts b/src/common/services/discord.service.ts index a5b9144..72986d4 100644 --- a/src/common/services/discord.service.ts +++ b/src/common/services/discord.service.ts @@ -18,7 +18,6 @@ export class DiscordService { await this.webhookClient.send({ content, username: 'Furnique Bot', - //avatarURL: 'https://imgur.com/GHh8QIp.png', avatarURL: 'https://nftcalendar.io/storage/uploads/2021/11/30/webp_net-gifmaker__1__1130202114500961a63a2147d4d.gif', embeds: [fields ? this.embed.setFields(fields) : this.embed] diff --git a/src/product/dto/product.dto.ts b/src/product/dto/product.dto.ts index 2ef5014..6b2f84d 100644 --- a/src/product/dto/product.dto.ts +++ b/src/product/dto/product.dto.ts @@ -1,5 +1,5 @@ import { ApiProperty, ApiPropertyOptional } from '@nestjs/swagger' -import { Product, Variant } from '@product/schemas/product.schema' +import { Product, RatingCounting, Variant } from '@product/schemas/product.schema' import { Category } from '@src/category/schemas/category.schema' import { DataResponse, PaginateResponse } from '@src/common/contracts/openapi-builder' import { Transform, Type } from 'class-transformer' @@ -218,6 +218,9 @@ export class ProductDetailDto { @ApiProperty({ type: Category, isArray: true }) categories: Category[] + + @ApiProperty({ type: RatingCounting }) + ratingCount: RatingCounting } export class FilterProductDto { diff --git a/src/product/schemas/product.schema.ts b/src/product/schemas/product.schema.ts index 2ceb0fa..989fde1 100644 --- a/src/product/schemas/product.schema.ts +++ b/src/product/schemas/product.schema.ts @@ -64,6 +64,33 @@ export class Variant { keyValue: Map } +export class RatingCounting { + @ApiProperty({ + example: 5 + }) + 1: number + + @ApiProperty({ + example: 5 + }) + 2: number + + @ApiProperty({ + example: 5 + }) + 3: number + + @ApiProperty({ + example: 5 + }) + 4: number + + @ApiProperty({ + example: 5 + }) + 5: number +} + @Schema({ collection: 'products', timestamps: true, @@ -110,10 +137,6 @@ export class Product { @Prop({ type: String, required: false }) arPlacement?: string - @ApiProperty() - @Prop({ type: Number, default: 0 }) - rate: number - @ApiProperty() @Prop({ type: String }) brand: string @@ -136,6 +159,23 @@ export class Product { default: ProductStatus.ACTIVE }) status: ProductStatus + + @ApiProperty() + @Prop({ type: Number, default: 0 }) + rate: number + + @ApiProperty() + @Prop({ + type: RatingCounting, + default: { + 1: 0, + 2: 0, + 3: 0, + 4: 0, + 5: 0 + } + }) + ratingCount: RatingCounting } export const ProductSchema = SchemaFactory.createForClass(Product) diff --git a/src/product/services/product.service.ts b/src/product/services/product.service.ts index d0becc4..2a2cf7c 100644 --- a/src/product/services/product.service.ts +++ b/src/product/services/product.service.ts @@ -63,7 +63,7 @@ export class ProductService { arPlacement: 1, variants: 1, categories: 1, - slug: 1 + slug: 1, } } ) diff --git a/src/review/services/review.service.ts b/src/review/services/review.service.ts index dc17182..8dfe0c9 100644 --- a/src/review/services/review.service.ts +++ b/src/review/services/review.service.ts @@ -9,6 +9,7 @@ import { ProductStatus } from '@common/contracts/constant' import { AppException } from '@common/exceptions/app.exception' import { Errors } from '@common/contracts/error' import { SuccessResponse } from '@common/contracts/dto' +import { pick } from 'lodash' @Injectable() export class ReviewService { @@ -81,22 +82,28 @@ export class ReviewService { }) // 3. Update rating product - const avgRate = await this.reviewRepository.model.aggregate([ + const rateSummary = await this.reviewRepository.model.aggregate([ { $group: { _id: '$product', - avgRating: { $avg: '$rate' } + avgRating: { $avg: '$rate' }, + 1: { $sum: { $cond: [{ $eq: ['$rate', 1] }, 1, 0] } }, + 2: { $sum: { $cond: [{ $eq: ['$rate', 2] }, 1, 0] } }, + 3: { $sum: { $cond: [{ $eq: ['$rate', 3] }, 1, 0] } }, + 4: { $sum: { $cond: [{ $eq: ['$rate', 4] }, 1, 0] } }, + 5: { $sum: { $cond: [{ $eq: ['$rate', 5] }, 1, 0] } } } }, - { $project: { roundedAvgRating: { $round: ['$avgRating', 1] } } } + { $project: { roundedAvgRating: { $round: ['$avgRating', 1] }, 1: 1, 2: 1, 3: 1, 4: 1, 5: 1 } } ]) - this.logger.debug('ReviewService.createReview: ', avgRate[0]?.roundedAvgRating) - if (!!avgRate[0]?.roundedAvgRating) { + this.logger.debug('ReviewService.createReview: ', rateSummary[0]) + if (!!rateSummary[0]?.roundedAvgRating) { await this.productRepository.findOneAndUpdate( { _id: createReviewDto.productId }, { - rate: avgRate[0]?.roundedAvgRating + rate: rateSummary[0]?.roundedAvgRating, + ratingCount: pick(rateSummary[0], ['1', '2', '3', '4', '5']) } ) }