Implemented service bus handling of emails and notification processing
continuous-integration/drone/pr Build is passing
continuous-integration/drone/pr Build is passing
This commit is contained in:
@@ -0,0 +1,18 @@
|
||||
using LiteCharms.Features.Notifications.Commands;
|
||||
using static LiteCharms.Abstractions.Constants;
|
||||
|
||||
namespace LiteCharms.Features.Email.Events.Handlers;
|
||||
|
||||
public class SendShopEmailEnquiryEventHandler(ISender mediator) :
|
||||
INotificationHandler<SendShopEmailEnquiryEvent>
|
||||
{
|
||||
public async ValueTask Handle(SendShopEmailEnquiryEvent notification, CancellationToken cancellationToken)
|
||||
{
|
||||
var command = CreateNotificationCommand.Create(Models.NotificationDirection.Outgoing, notification.SenderName!,
|
||||
notification.SenderAddress!, notification.Subject!, notification.Message!, Models.NotificationPlatforms.Email,
|
||||
notification.Priority, ShopEmailFromName, ShopEmailFromAddress, Guid.CreateVersion7().ToString(),
|
||||
Models.CorrelationIdTypes.None, isInternal: true, isHtml: false);
|
||||
|
||||
await mediator.Send(command, cancellationToken);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
using LiteCharms.Abstractions;
|
||||
using LiteCharms.Models;
|
||||
|
||||
namespace LiteCharms.Features.Email.Events;
|
||||
|
||||
public class SendShopEmailEnquiryEvent : EventBase, IEvent
|
||||
{
|
||||
public string Name { get; set; } = nameof(SendShopEmailEnquiryEvent);
|
||||
|
||||
public string? SenderName { get; set; }
|
||||
|
||||
public string? SenderAddress { get; set; }
|
||||
|
||||
public string? Subject { get; set; }
|
||||
|
||||
public string? Message { get; set; }
|
||||
|
||||
public Priorities Priority { get; set; }
|
||||
|
||||
public SendShopEmailEnquiryEvent() { }
|
||||
|
||||
private SendShopEmailEnquiryEvent(string senderName, string senderAddress, string subject, string message, Priorities priority = Priorities.Medium)
|
||||
{
|
||||
SenderName = senderName;
|
||||
SenderAddress = senderAddress;
|
||||
Subject = subject;
|
||||
Message = message;
|
||||
Priority = priority;
|
||||
}
|
||||
|
||||
public static SendShopEmailEnquiryEvent Create(string senderName, string senderAddress, string subject, string message, Priorities priority = Priorities.Medium)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNullOrWhiteSpace(senderName, nameof(senderName));
|
||||
ArgumentNullException.ThrowIfNullOrWhiteSpace(senderAddress, nameof(senderAddress));
|
||||
ArgumentNullException.ThrowIfNullOrWhiteSpace(subject, nameof(subject));
|
||||
ArgumentNullException.ThrowIfNullOrWhiteSpace(message, nameof(message));
|
||||
|
||||
return new(senderName, senderAddress, subject, message, priority);
|
||||
}
|
||||
}
|
||||
@@ -24,13 +24,13 @@ public class CreateNotificationCommand : IRequest<Result<Guid>>
|
||||
|
||||
public string? CorrelationId { get; set; }
|
||||
|
||||
public string? CorrelationIdType { get; set; }
|
||||
public CorrelationIdTypes CorrelationIdType { get; set; }
|
||||
|
||||
public bool IsInternal { get; set; }
|
||||
|
||||
public bool IsHtml { get; set; }
|
||||
|
||||
private CreateNotificationCommand(NotificationDirection direction, string sender, string senderAddress, string subject, string message, NotificationPlatforms platform, Priorities priority, string recipient, string recipientAddress, string correlationId, string correlationIdType, bool isInternal, bool isHtml = false)
|
||||
private CreateNotificationCommand(NotificationDirection direction, string sender, string senderAddress, string subject, string message, NotificationPlatforms platform, Priorities priority, string recipient, string recipientAddress, string correlationId, CorrelationIdTypes correlationIdType, bool isInternal, bool isHtml = false)
|
||||
{
|
||||
Direction = direction;
|
||||
Sender = sender;
|
||||
@@ -47,7 +47,7 @@ public class CreateNotificationCommand : IRequest<Result<Guid>>
|
||||
IsHtml = isHtml;
|
||||
}
|
||||
|
||||
public static CreateNotificationCommand Create(NotificationDirection direction, string sender, string senderAddress, string subject, string message, NotificationPlatforms platform, Priorities priority, string recipient, string recipientAddress, string correlationId, string correlationIdType, bool isInternal, bool isHtml = false)
|
||||
public static CreateNotificationCommand Create(NotificationDirection direction, string sender, string senderAddress, string subject, string message, NotificationPlatforms platform, Priorities priority, string recipient, string recipientAddress, string correlationId, CorrelationIdTypes correlationIdType, bool isInternal, bool isHtml = false)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(sender))
|
||||
throw new ArgumentException("Sender name is required.", nameof(sender));
|
||||
@@ -67,9 +67,6 @@ public class CreateNotificationCommand : IRequest<Result<Guid>>
|
||||
if (string.IsNullOrWhiteSpace(correlationId))
|
||||
throw new ArgumentException("CorrelationId is required.", nameof(correlationId));
|
||||
|
||||
if (string.IsNullOrWhiteSpace(correlationIdType))
|
||||
throw new ArgumentException("CorrelationIdType is required.", nameof(correlationIdType));
|
||||
|
||||
return new(direction, sender, senderAddress, subject, message, platform, priority, recipient, recipientAddress, correlationId, correlationIdType, isInternal, isHtml);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,8 +8,6 @@ public class ProcessEmailNotificationsEvent : EventBase, IEvent
|
||||
|
||||
public int MaxRecords { get; set; }
|
||||
|
||||
public ProcessEmailNotificationsEvent() => MaxRecords = 1000;
|
||||
|
||||
private ProcessEmailNotificationsEvent(int maxRecords = 1000) => MaxRecords = maxRecords;
|
||||
|
||||
public static ProcessEmailNotificationsEvent Create(int maxRecords = 1000) => new(maxRecords);
|
||||
|
||||
@@ -89,10 +89,11 @@
|
||||
<ProjectReference Include="..\LiteCharms.Models\LiteCharms.Models.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
<!-- Global Usings -->
|
||||
<!-- Global Shared Usings -->
|
||||
<ItemGroup>
|
||||
<Using Include="System.Text.Json" />
|
||||
<Using Include="Microsoft.Extensions.Hosting" />
|
||||
<Using Include="Microsoft.Extensions.Logging" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
||||
@@ -2,11 +2,36 @@
|
||||
|
||||
namespace LiteCharms.Infrastructure.ServiceBus.Exchanges;
|
||||
|
||||
public class EmailExchange(EmailQueue messages) : BackgroundService
|
||||
public class EmailExchange(EmailQueue messages, ILogger<EmailExchange> logger, IPublisher mediator) : BackgroundService
|
||||
{
|
||||
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
|
||||
{
|
||||
if(messages.Incoming.CanCount)
|
||||
while (!stoppingToken.IsCancellationRequested)
|
||||
{
|
||||
while (messages.Incoming.TryRead(out var message))
|
||||
{
|
||||
try
|
||||
{
|
||||
switch (message.Name)
|
||||
{
|
||||
case "SendShopEmailEnquiryEvent":
|
||||
await mediator.Publish(message, stoppingToken);
|
||||
break;
|
||||
case "ProcessEmailNotificationsEvent":
|
||||
await mediator.Publish(message, stoppingToken);
|
||||
break;
|
||||
default:
|
||||
logger.LogWarning("Unsupported email event {Event}", message.Name);
|
||||
break;
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.LogError(ex, ex.Message);
|
||||
}
|
||||
}
|
||||
|
||||
await Task.Delay(1000, stoppingToken);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user