Skip to content

Commit

Permalink
(#412) notifications: update PostCreatedHandler
Browse files Browse the repository at this point in the history
  • Loading branch information
SaintAngeLs committed Sep 16, 2024
1 parent 578d52e commit c3aee1a
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 83 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,18 @@ namespace MiniSpace.Services.Notifications.Application.Events.External.Handlers
{
public class EventDeletedHandler : IEventHandler<EventDeleted>
{
private readonly IFriendsServiceClient _friendsServiceClient;
private readonly IOrganizationsServiceClient _organizationsServiceClient;
private readonly IEventsServiceClient _eventsServiceClient;
private readonly IUserNotificationsRepository _userNotificationsRepository;
private readonly ILogger<EventDeletedHandler> _logger;
private readonly IHubContext<NotificationHub> _hubContext;

public EventDeletedHandler(
IFriendsServiceClient friendsServiceClient,
IOrganizationsServiceClient organizationsServiceClient,
IEventsServiceClient eventsServiceClient,
IUserNotificationsRepository userNotificationsRepository,
ILogger<EventDeletedHandler> logger,
IHubContext<NotificationHub> hubContext)
{
_friendsServiceClient = friendsServiceClient;
_organizationsServiceClient = organizationsServiceClient;
_eventsServiceClient = eventsServiceClient;
_userNotificationsRepository = userNotificationsRepository;
_logger = logger;
_hubContext = hubContext;
Expand All @@ -39,85 +36,26 @@ public async Task HandleAsync(EventDeleted eventDeleted, CancellationToken cance
{
try
{
if (eventDeleted.OrganizerType.Equals("Organization", StringComparison.OrdinalIgnoreCase))
{
await NotifyOrganizationMembersAsync(eventDeleted);
}
else if (eventDeleted.OrganizerType.Equals("User", StringComparison.OrdinalIgnoreCase))
{
await NotifyUserFriendsAndFollowersAsync(eventDeleted);
}
}
catch (Exception ex)
{
_logger.LogError($"Failed to handle EventDeleted event: {ex.Message}");
}
}

private async Task NotifyOrganizationMembersAsync(EventDeleted eventDeleted)
{
try
{
var organizationUsers = await _organizationsServiceClient.GetOrganizationMembersAsync(eventDeleted.OrganizerId);
// Fetch participants and interested users for the event
var eventParticipants = await _eventsServiceClient.GetParticipantsAsync(eventDeleted.EventId);

if (organizationUsers != null && organizationUsers.Users != null)
if (eventParticipants == null)
{
foreach (var user in organizationUsers.Users)
{
var notificationMessage = $"The event '{eventDeleted.EventName}' (Event ID: {eventDeleted.EventId}) scheduled from {eventDeleted.StartDate:yyyy-MM-dd} to {eventDeleted.EndDate:yyyy-MM-dd} has been cancelled.";

var notification = new Notification(
notificationId: Guid.NewGuid(),
userId: user.Id,
message: notificationMessage,
status: NotificationStatus.Unread,
createdAt: DateTime.UtcNow,
updatedAt: null,
relatedEntityId: eventDeleted.EventId,
eventType: NotificationEventType.EventDeleted
);

var userNotifications = await _userNotificationsRepository.GetByUserIdAsync(user.Id)
?? new UserNotifications(user.Id);

userNotifications.AddNotification(notification);
await _userNotificationsRepository.AddOrUpdateAsync(userNotifications);

var notificationDto = new NotificationDto
{
UserId = user.Id,
Message = notificationMessage,
CreatedAt = notification.CreatedAt,
EventType = NotificationEventType.EventDeleted,
RelatedEntityId = eventDeleted.EventId,
Details = $"<p>The event '{eventDeleted.EventName}' has been cancelled by the organization.</p>"
};

await NotificationHub.BroadcastNotification(_hubContext, notificationDto, _logger);
}
_logger.LogWarning($"No participants found for event {eventDeleted.EventId}");
return;
}
}
catch (Exception ex)
{
_logger.LogError($"Failed to notify organization members about the deleted event: {ex.Message}");
}
}

private async Task NotifyUserFriendsAndFollowersAsync(EventDeleted eventDeleted)
{
try
{
var friends = await _friendsServiceClient.GetFriendsAsync(eventDeleted.OrganizerId);
var friendIds = friends.Select(f => f.FriendId).ToList();
// Combine signed-up users and interested users
var allUsersToNotify = eventParticipants.SignedUpStudents
.Concat(eventParticipants.InterestedStudents)
.Distinct()
.Select(p => p.UserId)
.ToList();

var followers = await _friendsServiceClient.GetRequestsAsync(eventDeleted.OrganizerId);
var followerIds = followers.Select(f => f.InviterId).ToList();

var userIdsToNotify = friendIds.Concat(followerIds).Distinct().ToList();

foreach (var userId in userIdsToNotify)
// Notify all relevant users
foreach (var userId in allUsersToNotify)
{
var notificationMessage = $"An event '{eventDeleted.EventName}' (Event ID: {eventDeleted.EventId}) scheduled from {eventDeleted.StartDate:yyyy-MM-dd} to {eventDeleted.EndDate:yyyy-MM-dd} created by your friend or someone you follow has been cancelled.";
var notificationMessage = $"The event '{eventDeleted.EventName}' (Event ID: {eventDeleted.EventId}) scheduled from {eventDeleted.StartDate:yyyy-MM-dd} to {eventDeleted.EndDate:yyyy-MM-dd} has been cancelled.";

var notification = new Notification(
notificationId: Guid.NewGuid(),
Expand All @@ -136,22 +74,23 @@ private async Task NotifyUserFriendsAndFollowersAsync(EventDeleted eventDeleted)
userNotifications.AddNotification(notification);
await _userNotificationsRepository.AddOrUpdateAsync(userNotifications);

// Broadcast the notification to the user using SignalR
var notificationDto = new NotificationDto
{
UserId = userId,
Message = notificationMessage,
CreatedAt = notification.CreatedAt,
EventType = NotificationEventType.EventDeleted,
RelatedEntityId = eventDeleted.EventId,
Details = $"<p>The event '{eventDeleted.EventName}' has been cancelled by the organizer.</p>"
Details = $"<p>The event '{eventDeleted.EventName}' has been cancelled.</p>"
};

await NotificationHub.BroadcastNotification(_hubContext, notificationDto, _logger);
}
}
catch (Exception ex)
{
_logger.LogError($"Failed to notify friends and followers about the deleted event: {ex.Message}");
_logger.LogError($"Failed to handle EventDeleted event: {ex.Message}");
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public async Task HandleAsync(PostCreated eventArgs, CancellationToken cancellat

foreach (var studentParticipant in eventParticipants.InterestedStudents)
{
var student = await _studentsServiceClient.GetAsync(studentParticipant.StudentId);
var student = await _studentsServiceClient.GetAsync(studentParticipant.UserId);
if (student != null)
{
await NotifyStudent(student, eventDetails, post);
Expand All @@ -78,7 +78,7 @@ public async Task HandleAsync(PostCreated eventArgs, CancellationToken cancellat

foreach (var studentParticipant in eventParticipants.SignedUpStudents)
{
var student = await _studentsServiceClient.GetAsync(studentParticipant.StudentId);
var student = await _studentsServiceClient.GetAsync(studentParticipant.UserId);
if (student != null)
{
await NotifyStudent(student, eventDetails, post);
Expand Down

0 comments on commit c3aee1a

Please sign in to comment.