Skip to content

Commit

Permalink
(#125) notifications udpate the status update
Browse files Browse the repository at this point in the history
  • Loading branch information
SaintAngeLs committed May 12, 2024
1 parent 90b2a0e commit fce6837
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,31 +21,25 @@ public UpdateNotificationStatusHandler(INotificationRepository notificationRepos

public async Task HandleAsync(UpdateNotificationStatus command, CancellationToken cancellationToken = default)
{
Console.WriteLine($"Received NotificationId: {command.NotificationId}");

var notification = await _notificationRepository.GetAsync(command.NotificationId);
if (notification == null)
{
throw new NotificationNotFoundException(command.NotificationId);
}

if (Enum.TryParse<NotificationStatus>(command.Status, true, out var status))
if (Enum.TryParse<NotificationStatus>(command.Status, true, out var newStatus))
{
switch (status)
{
case NotificationStatus.Read:
notification.MarkAsRead();
break;
case NotificationStatus.Deleted:
notification.MarkAsDeleted();
break;
}
notification.Status = newStatus;
notification.UpdatedAt = DateTime.UtcNow;
await _notificationRepository.UpdateAsync(notification);
}
else
{
throw new InvalidNotificationStatusException($"Invalid status: {command.Status}");
}

await _notificationRepository.UpdateAsync(notification);

var events = _eventMapper.MapAll(notification.Events);
await _messageBroker.PublishAsync(events.ToArray());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ namespace MiniSpace.Services.Notifications.Application.Commands
{
public class UpdateNotificationStatus : ICommand
{
public Guid NotificationId { get; }
public string Status { get; }
public Guid NotificationId { get; set; }
public string Status { get; set; }

public UpdateNotificationStatus(Guid notificationId, string status)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ namespace MiniSpace.Services.Notifications.Core.Entities
{
public class Notification : AggregateRoot
{
public Guid NotificationId { get; private set; }
public string UserId { get; private set; }
public string Message { get; private set; }
public NotificationStatus Status { get; private set; }
public DateTime CreatedAt { get; private set; }
public DateTime? UpdatedAt { get; private set; }
public Guid NotificationId { get; set; }
public string UserId { get; set; }
public string Message { get; set; }
public NotificationStatus Status { get; set; }
public DateTime CreatedAt { get; set; }
public DateTime? UpdatedAt { get; set; }

public Notification(Guid notificationId, string userId, string message, NotificationStatus status, DateTime createdAt, DateTime? updatedAt)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,19 @@ private static IReadOnlyDictionary<Type, HandlerLogTemplate> MessageTemplates
},
};

public HandlerLogTemplate Map<TMessage>(TMessage message) where TMessage : class
public HandlerLogTemplate Map<TMessage>(TMessage message) where TMessage : class
{
var key = message.GetType();
return MessageTemplates.TryGetValue(key, out var template) ? template : null;
if (MessageTemplates.TryGetValue(key, out var template))
{
// Perform string replacement to match actual property names if necessary
if (template.After.Contains("{NewStatus}"))
{
template.After = template.After.Replace("{NewStatus}", "{Status}");
}
return template;
}
return null;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using MiniSpace.Services.Notifications.Core.Entities;
using MiniSpace.Services.Notifications.Core.Repositories;
using MiniSpace.Services.Notifications.Infrastructure.Mongo.Documents;
using MongoDB.Driver;
using System;
using System.Threading.Tasks;

Expand All @@ -25,8 +26,15 @@ public async Task<Notification> GetAsync(Guid id)
public Task AddAsync(Notification notification)
=> _repository.AddAsync(notification.AsDocument());

public Task UpdateAsync(Notification notification)
=> _repository.UpdateAsync(notification.AsDocument());
public async Task UpdateAsync(Notification notification)
{
var filter = Builders<NotificationDocument>.Filter.Eq(doc => doc.NotificationId, notification.NotificationId);
var update = Builders<NotificationDocument>.Update
.Set(doc => doc.Status, notification.Status.ToString())
.Set(doc => doc.UpdatedAt, DateTime.UtcNow);

var updateResult = await _repository.Collection.UpdateOneAsync(filter, update);
}

public Task DeleteAsync(Guid id)
=> _repository.DeleteAsync(id);
Expand Down

0 comments on commit fce6837

Please sign in to comment.