diff --git a/MiniSpace.Services.Notifications/src/MiniSpace.Services.Notifications.Application/Events/External/Handlers/ReportReviewStartedHandler.cs b/MiniSpace.Services.Notifications/src/MiniSpace.Services.Notifications.Application/Events/External/Handlers/ReportReviewStartedHandler.cs index e69de29bb..b9e18a602 100644 --- a/MiniSpace.Services.Notifications/src/MiniSpace.Services.Notifications.Application/Events/External/Handlers/ReportReviewStartedHandler.cs +++ b/MiniSpace.Services.Notifications/src/MiniSpace.Services.Notifications.Application/Events/External/Handlers/ReportReviewStartedHandler.cs @@ -0,0 +1,63 @@ +using Convey.CQRS.Events; +using System; +using System.Threading.Tasks; +using System.Threading; +using MiniSpace.Services.Notifications.Core.Entities; +using MiniSpace.Services.Notifications.Core.Repositories; +using MiniSpace.Services.Notifications.Application.Services; + +namespace MiniSpace.Services.Notifications.Application.Events.External.Handlers +{ + public class ReportReviewStartedHandler : IEventHandler + { + private readonly IMessageBroker _messageBroker; + private readonly IStudentNotificationsRepository _studentNotificationsRepository; + + public ReportReviewStartedHandler( + IMessageBroker messageBroker, + IStudentNotificationsRepository studentNotificationsRepository) + { + _messageBroker = messageBroker; + _studentNotificationsRepository = studentNotificationsRepository; + } + + public async Task HandleAsync(ReportReviewStarted eventArgs, CancellationToken cancellationToken) + { + var issuerNotification = await CreateNotificationForUser(eventArgs.IssuerId, eventArgs, "The review of your report has started."); + await PublishAndSaveNotification(issuerNotification, eventArgs.IssuerId, "ReportReviewStarted"); + } + + private async Task CreateNotificationForUser(Guid userId, ReportReviewStarted eventArgs, string message) + { + var notifications = await _studentNotificationsRepository.GetByStudentIdAsync(userId) ?? new StudentNotifications(userId); + var notification = new Notification( + notificationId: Guid.NewGuid(), + userId: userId, + message: message, + status: NotificationStatus.Unread, + createdAt: DateTime.UtcNow, + updatedAt: null, + relatedEntityId: eventArgs.ReportId, + eventType: NotificationEventType.ReportReviewStarted + ); + notifications.AddNotification(notification); + await _studentNotificationsRepository.UpdateAsync(notifications); + return notification; + } + + private async Task PublishAndSaveNotification(Notification notification, Guid userId, string eventType) + { + var notificationCreatedEvent = new NotificationCreated( + notificationId: notification.NotificationId, + userId: notification.UserId, + message: notification.Message, + createdAt: notification.CreatedAt, + eventType: eventType, + relatedEntityId: notification.RelatedEntityId, + details: $"Notification for user {userId}. Message: {notification.Message}" + ); + + await _messageBroker.PublishAsync(notificationCreatedEvent); + } + } +}