Skip to content

Commit

Permalink
(#126) add the reportReview started event handler
Browse files Browse the repository at this point in the history
  • Loading branch information
SaintAngeLs committed Jun 1, 2024
1 parent dbfcb99 commit 0279377
Showing 1 changed file with 63 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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<ReportReviewStarted>
{
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<Notification> 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);
}
}
}

0 comments on commit 0279377

Please sign in to comment.