review
This commit is contained in:
@@ -3,6 +3,7 @@ import { Cron } from '@nestjs/schedule';
|
||||
import { EntityManager } from '@mikro-orm/postgresql';
|
||||
import { FoodRepository } from '../../foods/repositories/food.repository';
|
||||
import { ReviewRepository } from '../repositories/review.repository';
|
||||
import { ReviewStatus } from '../enums/review-status.enum';
|
||||
|
||||
@Injectable()
|
||||
export class FoodRatingCronService {
|
||||
@@ -41,7 +42,7 @@ export class FoodRatingCronService {
|
||||
const reviews = await this.reviewRepository.find(
|
||||
{
|
||||
food: { id: food.id },
|
||||
isApproved: true,
|
||||
status: ReviewStatus.APPROVED,
|
||||
deletedAt: null,
|
||||
},
|
||||
{ fields: ['rating'] },
|
||||
|
||||
@@ -12,6 +12,7 @@ import { FoodMessage, UserMessage, ReviewMessage } from 'src/common/enums/messag
|
||||
import { Order } from '../../orders/entities/order.entity';
|
||||
import { OrderItem } from '../../orders/entities/order-item.entity';
|
||||
import { Restaurant } from 'src/modules/restaurants/entities/restaurant.entity';
|
||||
import { ReviewStatus } from '../enums/review-status.enum';
|
||||
|
||||
@Injectable()
|
||||
export class ReviewService {
|
||||
@@ -69,7 +70,7 @@ export class ReviewService {
|
||||
comment: comment || undefined,
|
||||
positivePoints: positivePoints || undefined,
|
||||
negativePoints: negativePoints || undefined,
|
||||
isApproved: false,
|
||||
status: ReviewStatus.PENDING,
|
||||
};
|
||||
|
||||
const review = this.reviewRepository.create(data);
|
||||
@@ -117,9 +118,9 @@ export class ReviewService {
|
||||
throw new BadRequestException(ReviewMessage.CAN_ONLY_UPDATE_OWN);
|
||||
}
|
||||
|
||||
// Users can only update rating and comment, not isApproved
|
||||
if (!isAdmin && dto.isApproved !== undefined) {
|
||||
throw new BadRequestException('Only admins can change approval status');
|
||||
// Users can only update rating and comment, not status
|
||||
if (!isAdmin && dto.status !== undefined) {
|
||||
throw new BadRequestException('Only admins can change review status');
|
||||
}
|
||||
|
||||
const oldRating = review.rating;
|
||||
@@ -134,6 +135,16 @@ export class ReviewService {
|
||||
return review;
|
||||
}
|
||||
|
||||
async changeStatus(reviewId: string, status: ReviewStatus, restaurantId: string): Promise<Review> {
|
||||
const review = await this.reviewRepository.findOne({ id: reviewId, order: { restaurant: { id: restaurantId } } });
|
||||
if (!review) {
|
||||
throw new NotFoundException(ReviewMessage.NOT_FOUND);
|
||||
}
|
||||
review.status = status;
|
||||
await this.em.persistAndFlush(review);
|
||||
return review;
|
||||
}
|
||||
|
||||
async remove(id: string, userId: string, isAdmin: boolean = false) {
|
||||
const review = await this.reviewRepository.findOne({ id }, { populate: ['food', 'user', 'order'] });
|
||||
if (!review) {
|
||||
@@ -155,7 +166,7 @@ export class ReviewService {
|
||||
|
||||
private async updateFoodRating(foodId: string): Promise<void> {
|
||||
const reviews = await this.reviewRepository.find(
|
||||
{ food: { id: foodId }, isApproved: true, deletedAt: null },
|
||||
{ food: { id: foodId }, status: ReviewStatus.APPROVED, deletedAt: null },
|
||||
{ fields: ['rating'] },
|
||||
);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user