Skip to content

Commit

Permalink
(#408) notifications: update comment created and udpated external evn…
Browse files Browse the repository at this point in the history
…et shnadlers
  • Loading branch information
SaintAngeLs committed Sep 7, 2024
1 parent 7162d28 commit cb57472
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 28 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,15 @@
using MiniSpace.Services.Notifications.Application.Hubs;
using MiniSpace.Services.Notifications.Application.Dto;
using Microsoft.AspNetCore.SignalR;
using MiniSpace.Services.Notifications.Core.Entities;

namespace MiniSpace.Services.Notifications.Application.Events.External.Comments.Handlers
{
public class CommentCreatedHandler : IEventHandler<CommentCreated>
{
private readonly ICommentsServiceClient _commentsServiceClient;
private readonly IEventsServiceClient _eventsServiceClient;
private readonly IPostsServiceClient _postsServiceClient;
private readonly IOrganizationsServiceClient _organizationsServiceClient;
private readonly IUserNotificationsRepository _userNotificationsRepository;
private readonly ILogger<CommentCreatedHandler> _logger;
Expand All @@ -24,13 +26,15 @@ public class CommentCreatedHandler : IEventHandler<CommentCreated>
public CommentCreatedHandler(
ICommentsServiceClient commentsServiceClient,
IEventsServiceClient eventsServiceClient,
IPostsServiceClient postsServiceClient,
IOrganizationsServiceClient organizationsServiceClient,
IUserNotificationsRepository userNotificationsRepository,
ILogger<CommentCreatedHandler> logger,
IHubContext<NotificationHub> hubContext)
{
_commentsServiceClient = commentsServiceClient;
_eventsServiceClient = eventsServiceClient;
_postsServiceClient = postsServiceClient;
_organizationsServiceClient = organizationsServiceClient;
_userNotificationsRepository = userNotificationsRepository;
_logger = logger;
Expand All @@ -41,37 +45,45 @@ public async Task HandleAsync(CommentCreated @event, CancellationToken cancellat
{
try
{
// Fetch comment details from the comment service
var commentDetails = await _commentsServiceClient.GetCommentAsync(@event.CommentId);
if (commentDetails == null)
{
_logger.LogError("No comment details found for CommentCreated event.");
return;
}

// Initialize entity owner and name variables
var entityOwnerId = Guid.Empty;
string entityName = string.Empty;

// Check if it's an event or post
if (@event.CommentContext.Equals("OrganizationEvent", StringComparison.OrdinalIgnoreCase))
{
// Fetch organization event details
var organizationEvent = await _eventsServiceClient.GetEventAsync(@event.ContextId);
if (organizationEvent != null)
{
entityOwnerId = organizationEvent.Organizer.Id;
entityOwnerId = organizationEvent.Organizer.OrganizationId != Guid.Empty
? organizationEvent.Organizer.OrganizationId
: organizationEvent.Organizer.Id;
entityName = organizationEvent.Name;
}
}
else if (@event.CommentContext.Equals("OrganizationPost", StringComparison.OrdinalIgnoreCase))
{
var organizationPost = await _organizationsServiceClient.GetOrganizationPostAsync(@event.ContextId);
// Fetch organization post details
var organizationPost = await _postsServiceClient.GetPostAsync(@event.ContextId);
if (organizationPost != null)
{
entityOwnerId = organizationPost.Organization.OwnerId;
entityName = organizationPost.Title;
entityOwnerId = organizationPost.UserId.HasValue ? organizationPost.UserId.Value : Guid.Empty;
entityName = organizationPost.TextContent;
}
}
else if (@event.CommentContext.Equals("UserEvent", StringComparison.OrdinalIgnoreCase))
{
var userEvent = await _eventsServiceClient.GetUserEventAsync(@event.ContextId);
// Fetch user event details
var userEvent = await _eventsServiceClient.GetEventAsync(@event.ContextId);
if (userEvent != null)
{
entityOwnerId = userEvent.Organizer.Id;
Expand All @@ -80,11 +92,12 @@ public async Task HandleAsync(CommentCreated @event, CancellationToken cancellat
}
else if (@event.CommentContext.Equals("UserPost", StringComparison.OrdinalIgnoreCase))
{
var userPost = await _commentsServiceClient.GetUserPostAsync(@event.ContextId);
// Fetch user post details
var userPost = await _postsServiceClient.GetPostAsync(@event.ContextId);
if (userPost != null)
{
entityOwnerId = userPost.UserId;
entityName = userPost.Title;
entityOwnerId = userPost.UserId.HasValue ? userPost.UserId.Value : Guid.Empty;
entityName = userPost.TextContent;
}
}
else
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
using System;
using System.Threading;
using System.Threading.Tasks;
using Convey.CQRS.Events;
using MiniSpace.Services.Notifications.Core.Repositories;
using MiniSpace.Services.Notifications.Application.Services;
using MiniSpace.Services.Notifications.Core.Entities;
using System.Threading.Tasks;
using System.Threading;
using MiniSpace.Services.Notifications.Application.Services.Clients;
using Microsoft.Extensions.Logging;
using Microsoft.AspNetCore.SignalR;
using MiniSpace.Services.Notifications.Application.Hubs;
using MiniSpace.Services.Notifications.Application.Dto;
using MiniSpace.Services.Notifications.Application.Events.External.Comments;
using MiniSpace.Services.Notifications.Application.Dto.Events;

namespace MiniSpace.Services.Notifications.Application.Events.External.Handlers
{
Expand Down Expand Up @@ -87,7 +89,7 @@ public async Task HandleAsync(CommentUpdated eventArgs, CancellationToken cancel
message: $"Your updated comment on the event '{eventDetails.Name}' has been processed.",
status: NotificationStatus.Unread,
createdAt: DateTime.UtcNow,
updatedAt: DateTime.UtcNow,
updatedAt: DateTime.UtcNow,
relatedEntityId: eventArgs.CommentId,
eventType: NotificationEventType.CommentUpdated
);
Expand All @@ -109,6 +111,7 @@ public async Task HandleAsync(CommentUpdated eventArgs, CancellationToken cancel

await _messageBroker.PublishAsync(notificationUpdatedEvent);

// Broadcast the updated notification via SignalR
var notificationDto = new NotificationDto
{
UserId = commentDetails.StudentId,
Expand All @@ -134,14 +137,15 @@ public async Task HandleAsync(CommentUpdated eventArgs, CancellationToken cancel
message: $"{commentDetails.StudentName} has updated their comment on your event '{eventDetails.Name}'.",
status: NotificationStatus.Unread,
createdAt: DateTime.UtcNow,
updatedAt: DateTime.UtcNow,
updatedAt: DateTime.UtcNow,
relatedEntityId: eventArgs.CommentId,
eventType: NotificationEventType.CommentUpdated
);

organizerNotifications.AddNotification(organizerNotification);
await _studentNotificationsRepository.AddOrUpdateAsync(organizerNotifications);

// Prepare and send organizer notification details HTML
var organizerNotificationDetailsHtml = $"<p>{commentDetails.StudentName} updated their comment on your event '{eventDetails.Name}': {commentDetails.CommentContext}</p>";

var organizerNotificationUpdatedEvent = new NotificationCreated(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Threading.Tasks;
using MiniSpace.Services.Notifications.Application.Dto;
using MiniSpace.Services.Notifications.Application.Dto.Events;

namespace MiniSpace.Services.Notifications.Application.Services.Clients
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using MiniSpace.Services.Notifications.Application.Dto;
using MiniSpace.Services.Notifications.Application.Dto.Posts;

namespace MiniSpace.Services.Notifications.Application.Services.Clients
{
Expand Down

0 comments on commit cb57472

Please sign in to comment.