Skip to content

Commit

Permalink
Implementing polly in mongo context and finishing the main flow
Browse files Browse the repository at this point in the history
  • Loading branch information
fernandoareias committed Jan 10, 2024
1 parent 6bbcbc5 commit b2c1283
Show file tree
Hide file tree
Showing 36 changed files with 499 additions and 90 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ public static class SendNotificationFactory
private static Dictionary<ENotificationType, Type> command = new Dictionary<ENotificationType, Type>()
{
{ ENotificationType.SMS, typeof(SendNotificationSMSCommand)},
{ ENotificationType.Email, typeof(SendNotificationEmailCommand)}
{ ENotificationType.Email, typeof(SendNotificationEmailCommand)},
{ ENotificationType.Letter, typeof(SendNotificationLetterCommand)},
{ ENotificationType.Push, typeof(SendNotificationPushCommand)},
{ ENotificationType.WhatsApp, typeof(SendNotificationWhatsAppCommand)},
};

public static SendNotificationCommand Create(CreateNotificationCommand request)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,54 @@
using MediatR;
using Microsoft.AspNetCore.Mvc;
using Notification.Core.Common.Domain;
using Notification.Core.Domain.Enums;
using Notification.Worker.Data.Repositories.Interfaces;
using Notification.Worker.Domain.Entities;
using Notification.Worker.Domain.Services.Interfaces;
using RabbitMQ.Client.Exceptions;

namespace Notification.Worker.Application.Commands.Handlers;

public class SendNotificationEmailCommandHandler : IRequestHandler<SendNotificationEmailCommand, IActionResult>
{
public Task<IActionResult> Handle(SendNotificationEmailCommand request, CancellationToken cancellationToken)
public SendNotificationEmailCommandHandler(INotificationRepository notificationRepository, IEmailServices emailServices)
{
throw new NotImplementedException();
_notificationRepository = notificationRepository;
_emailServices = emailServices;
}

private readonly INotificationRepository _notificationRepository;
private readonly IEmailServices _emailServices;

public async Task<IActionResult> Handle(SendNotificationEmailCommand request, CancellationToken cancellationToken)
{
bool create = false;
Domain.Notification notification = await _notificationRepository.GetByCorrelationId(request.AggregateId);

if (notification != null && notification.Sent.Any(c => !c.Success))
throw new DomainException("Already processed");

if (notification is null)
{
create = true;
notification = Create(request);
}

await notification.Send(_emailServices);

if(create)
_notificationRepository.Add(notification);
else
_notificationRepository.Update(notification);

await _notificationRepository.unitOfWork.Commit();

return new OkResult();
}

private Domain.Notification Create(SendNotificationEmailCommand request)
{
var parameters = request.Params.Select(c => new Parameter(c.Key, c.Value)).ToList();
return new Domain.Notification(request.AggregateId.ToString(), request.Recipient, ENotificationType.Email , parameters);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
using MediatR;
using Microsoft.AspNetCore.Mvc;
using Notification.Core.Common.Domain;
using Notification.Core.Domain.Enums;
using Notification.Worker.Data.Repositories.Interfaces;
using Notification.Worker.Domain.Entities;
using Notification.Worker.Domain.Services.Interfaces;

namespace Notification.Worker.Application.Commands.Handlers;

public class SendNotificationLetterCommandHandler : IRequestHandler<SendNotificationLetterCommand, IActionResult>
{
public SendNotificationLetterCommandHandler(INotificationRepository notificationRepository, ILetterServices letterServices)
{
_notificationRepository = notificationRepository;
_letterServices = letterServices;
}

private readonly INotificationRepository _notificationRepository;
private readonly ILetterServices _letterServices;

public async Task<IActionResult> Handle(SendNotificationLetterCommand request, CancellationToken cancellationToken)
{
bool create = false;
Domain.Notification notification = await _notificationRepository.GetByCorrelationId(request.AggregateId);

if (notification != null && notification.Sent.Any(c => !c.Success))
throw new DomainException("Already processed");

if (notification is null)
{
create = true;
notification = Create(request);
}

await notification.Send(_letterServices);

if(create)
_notificationRepository.Add(notification);
else
_notificationRepository.Update(notification);

await _notificationRepository.unitOfWork.Commit();

return new OkResult();
}

private Domain.Notification Create(SendNotificationLetterCommand request)
{
var parameters = request.Params.Select(c => new Parameter(c.Key, c.Value)).ToList();
return new Domain.Notification(request.AggregateId.ToString(), request.Recipient, ENotificationType.Email , parameters);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
using MediatR;
using Microsoft.AspNetCore.Mvc;
using Notification.Core.Common.Domain;
using Notification.Core.Domain.Enums;
using Notification.Worker.Data.Repositories.Interfaces;
using Notification.Worker.Domain.Entities;
using Notification.Worker.Domain.Services.Interfaces;

namespace Notification.Worker.Application.Commands.Handlers;

public class SendNotificationPushCommandHandler : IRequestHandler<SendNotificationPushCommand, IActionResult>
{
public SendNotificationPushCommandHandler(INotificationRepository notificationRepository, IPushServices pushServices)
{
_notificationRepository = notificationRepository;
_pushServices = pushServices;
}

private readonly INotificationRepository _notificationRepository;
private readonly IPushServices _pushServices;

public async Task<IActionResult> Handle(SendNotificationPushCommand request, CancellationToken cancellationToken)
{
bool create = false;
Domain.Notification notification = await _notificationRepository.GetByCorrelationId(request.AggregateId);

if (notification != null && notification.Sent.Any(c => !c.Success))
throw new DomainException("Already processed");

if (notification is null)
{
create = true;
notification = Create(request);
}

await notification.Send(_pushServices);

if(create)
_notificationRepository.Add(notification);
else
_notificationRepository.Update(notification);

await _notificationRepository.unitOfWork.Commit();

return new OkResult();
}
private Domain.Notification Create(SendNotificationPushCommand request)
{
var parameters = request.Params.Select(c => new Parameter(c.Key, c.Value)).ToList();
return new Domain.Notification(request.AggregateId.ToString(), request.Recipient, ENotificationType.Email , parameters);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
using System.Data;
using MediatR;
using Microsoft.AspNetCore.Mvc;
using Notification.Core.Common.Domain;
using Notification.Core.Domain.Enums;
using Notification.Worker.Data.Repositories.Interfaces;
using Notification.Worker.Domain.Entities;
using Notification.Worker.Domain.Services.Interfaces;

namespace Notification.Worker.Application.Commands.Handlers;
Expand All @@ -15,8 +19,36 @@ public SendNotificationSMSCommandHandler(INotificationRepository notificationRep

private readonly INotificationRepository _notificationRepository;
private readonly ISMSServices _smsServices;
public Task<IActionResult> Handle(SendNotificationSMSCommand request, CancellationToken cancellationToken)
public async Task<IActionResult> Handle(SendNotificationSMSCommand request, CancellationToken cancellationToken)
{
throw new NotImplementedException();
bool create = false;
Domain.Notification notification = await _notificationRepository.GetByCorrelationId(request.AggregateId);

if (notification != null && notification.Sent.Any(c => !c.Success))
throw new DomainException("Already processed");

if (notification is null)
{
create = true;
notification = Create(request);
}

await notification.Send(_smsServices);

if(create)
_notificationRepository.Add(notification);
else
_notificationRepository.Update(notification);

await _notificationRepository.unitOfWork.Commit();

return new OkResult();
}

private Domain.Notification Create(SendNotificationSMSCommand request)
{
var parameters = request.Params.Select(c => new Parameter(c.Key, c.Value)).ToList();
return new Domain.Notification(request.AggregateId.ToString(), request.Recipient, ENotificationType.Email , parameters);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
using MediatR;
using Microsoft.AspNetCore.Mvc;
using Notification.Core.Common.Domain;
using Notification.Core.Domain.Enums;
using Notification.Worker.Data.Repositories.Interfaces;
using Notification.Worker.Domain.Entities;
using Notification.Worker.Domain.Services.Interfaces;

namespace Notification.Worker.Application.Commands.Handlers;

public class SendNotificationWhatsAppCommandHandler: IRequestHandler<SendNotificationWhatsAppCommand, IActionResult>
{
public SendNotificationWhatsAppCommandHandler(INotificationRepository notificationRepository, IWhatsAppServices whatsAppServices)
{
_notificationRepository = notificationRepository;
_whatsAppServices = whatsAppServices;
}

private readonly INotificationRepository _notificationRepository;
private readonly IWhatsAppServices _whatsAppServices;

public async Task<IActionResult> Handle(SendNotificationWhatsAppCommand request, CancellationToken cancellationToken)
{
bool create = false;
Domain.Notification notification = await _notificationRepository.GetByCorrelationId(request.AggregateId);

if (notification != null && notification.Sent.Any(c => !c.Success))
throw new DomainException("Already processed");

if (notification is null)
{
create = true;
notification = Create(request);
}

await notification.Send(_whatsAppServices);

if(create)
_notificationRepository.Add(notification);
else
_notificationRepository.Update(notification);

await _notificationRepository.unitOfWork.Commit();

return new OkResult();
}

private Domain.Notification Create(SendNotificationWhatsAppCommand request)
{
var parameters = request.Params.Select(c => new Parameter(c.Key, c.Value)).ToList();
return new Domain.Notification(request.AggregateId.ToString(), request.Recipient, ENotificationType.Email , parameters);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using Notification.Worker.Application.Commands.Send.Base;

namespace Notification.Worker.Application.Commands;

public class SendNotificationLetterCommand : SendNotificationCommand
{
public SendNotificationLetterCommand(CreateNotificationCommand request) : base(request)
{

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using Notification.Worker.Application.Commands.Send.Base;

namespace Notification.Worker.Application.Commands;

public class SendNotificationPushCommand : SendNotificationCommand
{
public SendNotificationPushCommand(CreateNotificationCommand request) : base(request)
{

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using Notification.Worker.Application.Commands.Send.Base;

namespace Notification.Worker.Application.Commands;

public class SendNotificationWhatsAppCommand : SendNotificationCommand
{
public SendNotificationWhatsAppCommand(CreateNotificationCommand request) : base(request)
{

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ public BaseWorker(IServiceProvider serviceProvider)

}

protected void Process(CreateNotificationCommand request)
protected async Task Process(CreateNotificationCommand request)
{
using (var scope = _serviceProvider.CreateScope())
{
var mediatorHandler = scope.ServiceProvider.GetRequiredService<IMediatorHandler>();
var command = SendNotificationFactory.Create(request);
mediatorHandler.Send<CreateNotificationCommand>(command).GetAwaiter();
await mediatorHandler.Send<CreateNotificationCommand>(command);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ protected override async Task ExecuteAsync(CancellationToken stoppingToken)

messageBus.Subscribe<CreateNotificationCommand>("notifications", "send-notification-Email", Process,
stoppingToken);

await Task.Delay(-1, stoppingToken);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ protected override async Task ExecuteAsync(CancellationToken stoppingToken)
using (var scope = _serviceProvider.CreateScope())
{
var messageBus = scope.ServiceProvider.GetRequiredService<IMessageBus>();
messageBus.Subscribe<CreateNotificationCommand>("notifications", "send-notification-PushNotification", Process,
messageBus.Subscribe<CreateNotificationCommand>("notifications", "send-notification-Push", Process,
stoppingToken);

Console.WriteLine("[WORKER[SEND-Push] - Awaiting process...");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,15 @@ namespace Notification.Worker.Domain.Events.Common;
[DataContract]
public abstract class NotificationDeliveryFailureEvent : Event
{
protected NotificationDeliveryFailureEvent(BsonObjectId aggregateId)
protected NotificationDeliveryFailureEvent()
{
AggregateId = aggregateId;

}
protected NotificationDeliveryFailureEvent(string correlationId)
{
CorrelationId = correlationId;
}

[DataMember]
public BsonObjectId AggregateId { get; private set; }
public string CorrelationId { get; private set; }
}
Loading

0 comments on commit b2c1283

Please sign in to comment.