Completed refactor

This commit is contained in:
Khwezi Mngoma
2026-05-14 01:33:21 +02:00
parent 42001998d6
commit 134d8429c0
129 changed files with 1870 additions and 3165 deletions
@@ -1,73 +0,0 @@
using LiteCharms.Features.Shop;
using LiteCharms.Models;
namespace LiteCharms.Features.Notifications.Commands;
public class CreateNotification : IRequest<Result<Guid>>
{
public NotificationDirection Direction { get; set; }
public string? Sender { get; set; }
public string? SenderAddress { get; set; }
public string? Subject { get; set; }
public string? Message { get; set; }
public NotificationPlatforms Platform { get; set; }
public Priorities Priority { get; set; }
public string? Recipient { get; set; }
public string? RecipientAddress { get; set; }
public string? CorrelationId { get; set; }
public CorrelationIdTypes CorrelationIdType { get; set; }
public bool IsInternal { get; set; }
public bool IsHtml { get; set; }
private CreateNotification(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;
SenderAddress = senderAddress;
Subject = subject;
Message = message;
Platform = platform;
Priority = priority;
Recipient = recipient;
RecipientAddress = recipientAddress;
CorrelationId = correlationId;
CorrelationIdType = correlationIdType;
IsInternal = isInternal;
IsHtml = isHtml;
}
public static CreateNotification 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));
if (string.IsNullOrWhiteSpace(subject))
throw new ArgumentException("Subject is required.", nameof(subject));
if (string.IsNullOrWhiteSpace(message))
throw new ArgumentException("Message is required.", nameof(message));
if (string.IsNullOrWhiteSpace(recipient))
throw new ArgumentException("Recipient name is required.", nameof(recipient));
if (string.IsNullOrWhiteSpace(recipientAddress))
throw new ArgumentException("Recipient address is required.", nameof(recipientAddress));
if (string.IsNullOrWhiteSpace(correlationId))
throw new ArgumentException("CorrelationId is required.", nameof(correlationId));
return new(direction, sender, senderAddress, subject, message, platform, priority, recipient, recipientAddress, correlationId, correlationIdType, isInternal, isHtml);
}
}
@@ -1,40 +0,0 @@
using LiteCharms.Features.Shop.Postgres;
namespace LiteCharms.Features.Notifications.Commands.Handlers;
public class CreateNotificationCommandHandler(IDbContextFactory<ShopDbContext> contextFactory) : IRequestHandler<CreateNotification, Result<Guid>>
{
public async ValueTask<Result<Guid>> Handle(CreateNotification request, CancellationToken cancellationToken)
{
try
{
using var context = await contextFactory.CreateDbContextAsync(cancellationToken);
var newNotification = context.Notifications.Add(new Entities.Notification
{
Direction = request.Direction,
SenderName = request.Sender,
Sender = request.SenderAddress,
Recipient = request.Recipient,
RecipientAddress = request.RecipientAddress,
Subject = request.Subject,
Message = request.Message,
Platform = request.Platform,
Priority = request.Priority,
CorrelationId = request.CorrelationId,
CorrelationIdType = request.CorrelationIdType,
IsInternal = request.IsInternal,
IsHtml = request.IsHtml,
Processed = false
});
return newNotification is not null
? Result.Ok(newNotification.Entity.Id)
: Result.Fail(new Error("Failed to create notification"));
}
catch (Exception ex)
{
return Result.Fail(new Error(ex.Message).CausedBy(ex));
}
}
}
@@ -1,35 +0,0 @@
using LiteCharms.Features.Shop.Postgres;
namespace LiteCharms.Features.Notifications.Commands.Handlers;
public class UpdateNotificationCommandHandler(IDbContextFactory<ShopDbContext> contextFactory) : IRequestHandler<UpdateNotificationCommand, Result>
{
public async ValueTask<Result> Handle(UpdateNotificationCommand request, CancellationToken cancellationToken)
{
try
{
using var context = await contextFactory.CreateDbContextAsync(cancellationToken);
var notification = await context.Notifications.FirstOrDefaultAsync(n => n.Id == request.NotificationId, cancellationToken);
if(notification is null)
return Result.Fail(new Error($"Notification with id {request.NotificationId} not found."));
notification.Processed = request.Processed;
if (request.HasError)
{
notification.HasError = request.HasError;
notification.Errors = request.Errors;
}
return await context.SaveChangesAsync(cancellationToken) > 0
? Result.Ok()
: Result.Fail(new Error($"Failed to update notification with id {request.NotificationId}."));
}
catch (Exception ex)
{
return Result.Fail(new Error(ex.Message).CausedBy(ex));
}
}
}
@@ -1,28 +0,0 @@
namespace LiteCharms.Features.Notifications.Commands;
public class UpdateNotificationCommand : IRequest<Result>
{
public Guid NotificationId { get; set; }
public bool Processed { get; set; }
public bool HasError { get; set; }
public string[]? Errors { get; set; }
private UpdateNotificationCommand(Guid notificationId, bool processed, bool hasError = false, string[]? errors = null)
{
NotificationId = notificationId;
Processed = processed;
HasError = hasError;
Errors = errors;
}
public static UpdateNotificationCommand Create(Guid notificationId, bool processed, bool hasError = false, string[]? errors = null)
{
if(notificationId == Guid.Empty)
throw new ArgumentException("Notification ID cannot be empty.", nameof(notificationId));
return new(notificationId, processed);
}
}
@@ -13,10 +13,10 @@ public class NotificationConfiguration : IEntityTypeConfiguration<Notification>
builder.Property(f => f.Platform).IsRequired().HasConversion<int>();
builder.Property(f => f.Priority).IsRequired().HasConversion<int>();
builder.Property(f => f.CorrelationIdType).IsRequired().HasConversion<int>();
builder.Property(f => f.Sender).IsRequired();
builder.Property(f => f.SenderAddress).IsRequired();
builder.Property(f => f.Subject).IsRequired();
builder.Property(f => f.Message).IsRequired();
builder.Property(f => f.Recipient).IsRequired();
builder.Property(f => f.RecipientName).IsRequired();
builder.Property(f => f.RecipientAddress).IsRequired();
builder.Property(f => f.CorrelationId).IsRequired();
builder.Property(f => f.IsHtml).HasDefaultValue(false);
@@ -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
}
};
}
@@ -1,6 +1,6 @@
using LiteCharms.Features.Abstractions;
namespace LiteCharms.Features.Notifications.Events;
namespace LiteCharms.Features.Shop.Notifications.Events;
public class ProcessEmailNotificationsEvent : EventBase, IEvent
{
@@ -1,9 +0,0 @@
using LiteCharms.Features.Shop.Notifications.Models;
namespace LiteCharms.Features.Shop.Notifications;
public interface INotificationService
{
Task<Result<Guid>> CreateNotificationAsync(CreateNotification request, CancellationToken cancellationToken = default);
Task<Result> UpdateNotificationAsync(UpdateNotification request, CancellationToken cancellationToken = default);
}
@@ -16,7 +16,7 @@ public class Notification
public CorrelationIdTypes CorrelationIdType { get; set; }
public string? Sender { get; set; }
public string? SenderAddress { get; set; }
public string? SenderName { get; set; }
@@ -24,7 +24,7 @@ public class Notification
public string? Message { get; set; }
public string? Recipient { get; set; }
public string? RecipientName { get; set; }
public string? RecipientAddress { get; set; }
@@ -2,23 +2,23 @@
public record CreateNotification
{
public NotificationDirection Direction { get; set; }
public required NotificationDirection Direction { get; set; }
public string? Sender { get; set; }
public required string Sender { get; set; }
public string? SenderAddress { get; set; }
public required string SenderAddress { get; set; }
public string? Subject { get; set; }
public required string Subject { get; set; }
public string? Message { get; set; }
public NotificationPlatforms Platform { get; set; }
public required NotificationPlatforms Platform { get; set; }
public Priorities Priority { get; set; }
public required Priorities Priority { get; set; }
public string? Recipient { get; set; }
public required string Recipient { get; set; }
public string? RecipientAddress { get; set; }
public required string RecipientAddress { get; set; }
public string? CorrelationId { get; set; }
@@ -31,9 +31,9 @@ public record CreateNotification
public class UpdateNotification
{
public Guid NotificationId { get; set; }
public required Guid NotificationId { get; set; }
public bool Processed { get; set; }
public required bool Processed { get; set; }
public bool HasError { get; set; }
@@ -1,5 +1,115 @@
namespace LiteCharms.Features.Shop.Notifications;
using LiteCharms.Features.Extensions;
using LiteCharms.Features.Models;
using LiteCharms.Features.Shop.Notifications.Models;
using LiteCharms.Features.Shop.Postgres;
public class NotificationService : INotificationService
namespace LiteCharms.Features.Shop.Notifications;
public class NotificationService(IDbContextFactory<ShopDbContext> contextFactory)
{
public async ValueTask<Result<Guid>> CreateNotificationAsync(CreateNotification request, CancellationToken cancellationToken = default)
{
try
{
using var context = await contextFactory.CreateDbContextAsync(cancellationToken);
var newNotification = context.Notifications.Add(new Entities.Notification
{
Direction = request.Direction,
SenderName = request.Sender,
SenderAddress = request.SenderAddress,
RecipientName = request.Recipient,
RecipientAddress = request.RecipientAddress,
Subject = request.Subject,
Message = request.Message,
Platform = request.Platform,
Priority = request.Priority,
CorrelationId = request.CorrelationId,
CorrelationIdType = request.CorrelationIdType,
IsInternal = request.IsInternal,
IsHtml = request.IsHtml,
Processed = false
});
return newNotification is not null
? Result.Ok(newNotification.Entity.Id)
: Result.Fail(new Error("Failed to create notification"));
}
catch (Exception ex)
{
return Result.Fail(new Error(ex.Message).CausedBy(ex));
}
}
public async ValueTask<Result<Notification>> GetNotificationAsync(Guid notificationId, CancellationToken cancellationToken = default)
{
try
{
await using var context = await contextFactory.CreateDbContextAsync(cancellationToken);
var notification = await context.Notifications.FirstOrDefaultAsync(n => n.Id == notificationId, cancellationToken);
return notification is not null
? Result.Ok(notification.ToModel())
: Result.Fail<Notification>(new Error($"Notification with id {notificationId} not found"));
}
catch (Exception ex)
{
return Result.Fail<Notification>(new Error(ex.Message).CausedBy(ex));
}
}
public async ValueTask<Result<Notification[]>> GetNotificationsAsync(DateRange range, CancellationToken cancellationToken = default)
{
try
{
var fromDate = range.From.ToDateTime(TimeOnly.MinValue);
var toDate = range.To.ToDateTime(TimeOnly.MaxValue);
using var context = await contextFactory.CreateDbContextAsync(cancellationToken);
var notifications = await context.Notifications.AsNoTracking()
.Where(n => n.CreatedAt >= fromDate && n.CreatedAt <= toDate)
.OrderByDescending(n => n.CreatedAt)
.Take(range.MaxRecords)
.ToArrayAsync(cancellationToken);
return notifications?.Length > 0
? Result.Ok(notifications.Select(n => n.ToModel()).ToArray())
: Result.Fail(new Error($"No notifications found for the specified date range {range.From} to {range.To}."));
}
catch (Exception ex)
{
return Result.Fail(new Error(ex.Message).CausedBy(ex));
}
}
public async ValueTask<Result> UpdateNotificationAsync(UpdateNotification request, CancellationToken cancellationToken = default)
{
try
{
using var context = await contextFactory.CreateDbContextAsync(cancellationToken);
var notification = await context.Notifications.FirstOrDefaultAsync(n => n.Id == request.NotificationId, cancellationToken);
if (notification is null)
return Result.Fail(new Error($"Notification with id {request.NotificationId} not found."));
notification.Processed = request.Processed;
if (request.HasError)
{
notification.HasError = request.HasError;
notification.Errors = request.Errors;
}
return await context.SaveChangesAsync(cancellationToken) > 0
? Result.Ok()
: Result.Fail(new Error($"Failed to update notification with id {request.NotificationId}."));
}
catch (Exception ex)
{
return Result.Fail(new Error(ex.Message).CausedBy(ex));
}
}
}
@@ -1,18 +0,0 @@
using LiteCharms.Features.Shop.Notifications.Models;
namespace LiteCharms.Features.Notifications.Queries;
public class GetNotificationQuery : IRequest<Result<Notification>>
{
public Guid NotificationId { get; set; }
private GetNotificationQuery(Guid notificationId) => NotificationId = notificationId;
public static GetNotificationQuery Create(Guid notificationId)
{
if (notificationId == Guid.Empty)
throw new ArgumentException("Notification ID is required.", nameof(notificationId));
return new(notificationId);
}
}
@@ -1,30 +0,0 @@
using LiteCharms.Features.Shop.Notifications.Models;
namespace LiteCharms.Features.Notifications.Queries;
public class GetNotificationsQuery : IRequest<Result<Notification[]>>
{
public DateOnly From { get; set; }
public DateOnly To { get; set; }
public int MaxRecords { get; set; }
private GetNotificationsQuery(DateOnly from, DateOnly to, int maxRecords = 1000)
{
From = from;
To = to;
MaxRecords = maxRecords;
}
public static GetNotificationsQuery Create(DateOnly from, DateOnly to, int maxRecords = 1000)
{
if (from > to)
throw new ArgumentException("From date cannot be greater than To date.");
if(maxRecords <= 0)
throw new ArgumentException("MaxRecords must be a positive integer.", nameof(maxRecords));
return new(from, to, maxRecords);
}
}
@@ -1,26 +0,0 @@
using LiteCharms.Extensions;
using LiteCharms.Features.Shop.Notifications.Models;
using LiteCharms.Features.Shop.Postgres;
namespace LiteCharms.Features.Notifications.Queries.Handlers;
public class GetNotificationQueryHandler(IDbContextFactory<ShopDbContext> contextFactory) : IRequestHandler<GetNotificationQuery, Result<Notification>>
{
public async ValueTask<Result<Notification>> Handle(GetNotificationQuery request, CancellationToken cancellationToken)
{
try
{
await using var context = await contextFactory.CreateDbContextAsync(cancellationToken);
var notification = await context.Notifications.FirstOrDefaultAsync(n => n.Id == request.NotificationId, cancellationToken);
return notification is not null
? Result.Ok(notification.ToModel())
: Result.Fail<Notification>(new Error($"Notification with id {request.NotificationId} not found"));
}
catch (Exception ex)
{
return Result.Fail<Notification>(new Error(ex.Message).CausedBy(ex));
}
}
}
@@ -1,33 +0,0 @@
using LiteCharms.Extensions;
using LiteCharms.Features.Shop.Notifications.Models;
using LiteCharms.Features.Shop.Postgres;
namespace LiteCharms.Features.Notifications.Queries.Handlers;
public class GetNotificationsQueryHandler(IDbContextFactory<ShopDbContext> contextFactory) : IRequestHandler<GetNotificationsQuery, Result<Notification[]>>
{
public async ValueTask<Result<Notification[]>> Handle(GetNotificationsQuery request, CancellationToken cancellationToken)
{
try
{
var fromDate = request.From.ToDateTime(TimeOnly.MinValue);
var toDate = request.To.ToDateTime(TimeOnly.MaxValue);
using var context = await contextFactory.CreateDbContextAsync(cancellationToken);
var notifications = await context.Notifications.AsNoTracking()
.Where(n => n.CreatedAt >= fromDate && n.CreatedAt <= toDate)
.OrderByDescending(n => n.CreatedAt)
.Take(request.MaxRecords)
.ToArrayAsync(cancellationToken);
return notifications?.Length > 0
? Result.Ok(notifications.Select(n => n.ToModel()).ToArray())
: Result.Fail(new Error($"No notifications found for the specified date range {request.From} to {request.To}."));
}
catch (Exception ex)
{
return Result.Fail(new Error(ex.Message).CausedBy(ex));
}
}
}