Skip to content

Commit

Permalink
chore: Add ratingCount product
Browse files Browse the repository at this point in the history
  • Loading branch information
MinhhTien committed Jun 16, 2024
1 parent 78acebf commit 4d8a98c
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 13 deletions.
1 change: 0 additions & 1 deletion src/common/services/discord.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
5 changes: 4 additions & 1 deletion src/product/dto/product.dto.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -218,6 +218,9 @@ export class ProductDetailDto {

@ApiProperty({ type: Category, isArray: true })
categories: Category[]

@ApiProperty({ type: RatingCounting })
ratingCount: RatingCounting
}

export class FilterProductDto {
Expand Down
48 changes: 44 additions & 4 deletions src/product/schemas/product.schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,33 @@ export class Variant {
keyValue: Map<string, string>
}

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,
Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion src/product/services/product.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ export class ProductService {
arPlacement: 1,
variants: 1,
categories: 1,
slug: 1
slug: 1,
}
}
)
Expand Down
19 changes: 13 additions & 6 deletions src/review/services/review.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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'])
}
)
}
Expand Down

0 comments on commit 4d8a98c

Please sign in to comment.