-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
(#126) add the reportReview started event handler
- Loading branch information
1 parent
dbfcb99
commit 0279377
Showing
1 changed file
with
63 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |