Completed refactor
This commit is contained in:
+50
-14
@@ -1,13 +1,15 @@
|
||||
using LiteCharms.Features.Email.Commands;
|
||||
using LiteCharms.Features.Email;
|
||||
using LiteCharms.Features.Shop.Notifications.Entities;
|
||||
using LiteCharms.Features.Shop.Postgres;
|
||||
using static LiteCharms.Features.ServiceBus.Constants;
|
||||
using static LiteCharms.Features.Extensions.Timezones;
|
||||
|
||||
namespace LiteCharms.Features.Notifications.Events.Handlers;
|
||||
namespace LiteCharms.Features.Shop.Notifications.Events.Handlers;
|
||||
|
||||
public class ProcessEmailNotificationsEventHandler(IDbContextFactory<ShopDbContext> contextFactory, ILogger<ProcessEmailNotificationsEvent> logger, ISender mediator) :
|
||||
INotificationHandler<ProcessEmailNotificationsEvent>
|
||||
public class ProcessEmailNotificationsEventHandler(IDbContextFactory<ShopDbContext> contextFactory, ILogger<ProcessEmailNotificationsEvent> logger,
|
||||
EmailService emailService) : INotificationHandler<ProcessEmailNotificationsEvent>
|
||||
{
|
||||
private bool dropBatch = false;
|
||||
|
||||
public async ValueTask Handle(ProcessEmailNotificationsEvent message, CancellationToken cancellationToken)
|
||||
{
|
||||
try
|
||||
@@ -17,14 +19,16 @@ public class ProcessEmailNotificationsEventHandler(IDbContextFactory<ShopDbConte
|
||||
var notifications = await context.Notifications
|
||||
.OrderByDescending(o => o.Priority)
|
||||
.ThenBy(o => o.CreatedAt)
|
||||
.Where(n => n.CorrelationIdType == Models.CorrelationIdTypes.Email)
|
||||
.Where(n => n.Direction == Models.NotificationDirection.Outgoing)
|
||||
.Where(n => n.CorrelationIdType == CorrelationIdTypes.Email)
|
||||
.Where(n => n.Direction == NotificationDirection.Outgoing)
|
||||
.Take(message.MaxRecords)
|
||||
.ToListAsync(cancellationToken);
|
||||
|
||||
foreach (var notification in notifications)
|
||||
{
|
||||
var sendResult = await SendEmailAsync(notification, cancellationToken);
|
||||
if (dropBatch || cancellationToken.IsCancellationRequested) break;
|
||||
|
||||
var sendResult = await SendEmailAsync(notification,emailService, cancellationToken);
|
||||
|
||||
if(sendResult.IsFailed)
|
||||
{
|
||||
@@ -40,6 +44,7 @@ public class ProcessEmailNotificationsEventHandler(IDbContextFactory<ShopDbConte
|
||||
}
|
||||
|
||||
notification.Processed = true;
|
||||
notification.UpdatedAt = SouthAfricanTimeZone.UtcNow();
|
||||
}
|
||||
|
||||
await context.SaveChangesAsync(cancellationToken);
|
||||
@@ -50,17 +55,23 @@ public class ProcessEmailNotificationsEventHandler(IDbContextFactory<ShopDbConte
|
||||
}
|
||||
}
|
||||
|
||||
private async Task<Result> SendEmailAsync(Notification notification, CancellationToken cancellationToken = default)
|
||||
private async Task<Result> SendEmailAsync(Notification notification, EmailService service, CancellationToken cancellationToken = default)
|
||||
{
|
||||
try
|
||||
{
|
||||
var request = SendEmailCommand.Create(notification.Sender!, notification.SenderName!, ShopEmailFromAddress,
|
||||
ShopEmailFromName, notification.Subject!, notification.Message!);
|
||||
using Email.Models.Message message = CreateMessage(notification);
|
||||
|
||||
var result = await mediator.Send(request, cancellationToken);
|
||||
var sendResult = await service.SendEmailAsync(message, cancellationToken);
|
||||
|
||||
return result.IsFailed
|
||||
? Result.Fail(result.Errors)
|
||||
if (sendResult.IsFailed)
|
||||
{
|
||||
if (emailService.Status != EmailStatuses.Success && emailService.Status != EmailStatuses.Connected) dropBatch = true;
|
||||
|
||||
return Result.Fail(sendResult.Errors);
|
||||
}
|
||||
|
||||
return sendResult.IsFailed
|
||||
? Result.Fail(sendResult.Errors)
|
||||
: Result.Ok();
|
||||
}
|
||||
catch (Exception ex)
|
||||
@@ -68,4 +79,29 @@ public class ProcessEmailNotificationsEventHandler(IDbContextFactory<ShopDbConte
|
||||
return Result.Fail(new Error(ex.Message).CausedBy(ex));
|
||||
}
|
||||
}
|
||||
|
||||
private static Email.Models.Message CreateMessage(Notification notification) =>
|
||||
new()
|
||||
{
|
||||
Sender = new Email.Models.Party
|
||||
{
|
||||
Name = notification.SenderName,
|
||||
Address = notification.SenderAddress
|
||||
},
|
||||
Recipient = new Email.Models.Party
|
||||
{
|
||||
Name = notification.RecipientName,
|
||||
Address = notification.RecipientAddress
|
||||
},
|
||||
Subject = notification.Subject,
|
||||
Body = new Email.Models.Body
|
||||
{
|
||||
Properties = new Email.Models.BodyProperties
|
||||
{
|
||||
HasAttachments = false,
|
||||
IsHtml = notification.IsHtml
|
||||
},
|
||||
Message = notification.Message
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user