review notification

This commit is contained in:
2025-12-25 11:15:51 +03:30
parent 4716620a43
commit 9f7c695ed1
4 changed files with 115 additions and 4 deletions
+20 -2
View File
@@ -13,6 +13,8 @@ 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';
import { EventEmitter2 } from '@nestjs/event-emitter';
import { ReviewCreatedEvent } from '../events/review.events';
@Injectable()
export class ReviewService {
@@ -21,6 +23,7 @@ export class ReviewService {
private readonly foodRepository: FoodRepository,
private readonly userRepository: UserRepository,
private readonly em: EntityManager,
private readonly eventEmitter: EventEmitter2,
) { }
async create(userId: string, createReviewDto: CreateReviewDto): Promise<Review> {
@@ -36,8 +39,8 @@ export class ReviewService {
throw new NotFoundException(UserMessage.USER_NOT_FOUND);
}
// Find order and verify it belongs to the user
const order = await this.em.findOne(Order, { id: orderId, user: { id: userId } });
// Find order and verify it belongs to the user, populate restaurant to get restaurantId
const order = await this.em.findOne(Order, { id: orderId, user: { id: userId } }, { populate: ['restaurant'] });
if (!order) {
throw new NotFoundException('Order not found or does not belong to the current user');
}
@@ -83,6 +86,21 @@ export class ReviewService {
// Update food rating average
await this.updateFoodRating(foodId);
// Emit ReviewCreatedEvent
this.eventEmitter.emit(
ReviewCreatedEvent.name,
new ReviewCreatedEvent(
review.id,
order.restaurant.id,
userId,
foodId,
food.title || 'Unknown Food',
orderId,
order.orderNumber || '',
rating,
),
);
return review;
}