This commit is contained in:
@@ -0,0 +1,4 @@
|
||||
namespace LiteCharms.Features.TechShop.Notifications.Entities;
|
||||
|
||||
[EntityTypeConfiguration<NotificationConfiguration, Notification>]
|
||||
public class Notification : Models.Notification;
|
||||
@@ -0,0 +1,28 @@
|
||||
namespace LiteCharms.Features.TechShop.Notifications.Entities;
|
||||
|
||||
public class NotificationConfiguration : IEntityTypeConfiguration<Notification>
|
||||
{
|
||||
public void Configure(EntityTypeBuilder<Notification> builder)
|
||||
{
|
||||
builder.ToTable("Notification");
|
||||
|
||||
builder.HasKey(f => f.Id);
|
||||
builder.Property(f => f.CreatedAt).IsRequired().ValueGeneratedOnAdd().HasDefaultValueSql("now()");
|
||||
builder.Property(f => f.UpdatedAt).IsRequired(false).HasDefaultValueSql(null);
|
||||
builder.Property(f => f.Direction).IsRequired().HasConversion<int>();
|
||||
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.SenderAddress).IsRequired();
|
||||
builder.Property(f => f.Subject).IsRequired();
|
||||
builder.Property(f => f.Message).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);
|
||||
builder.Property(f => f.IsInternal).HasDefaultValue(true);
|
||||
builder.Property(f => f.Processed).HasDefaultValue(false);
|
||||
builder.Property(f => f.HasError).HasDefaultValue(false);
|
||||
builder.Property(f => f.Errors).HasColumnType("jsonb").IsRequired(false);
|
||||
}
|
||||
}
|
||||
+113
@@ -0,0 +1,113 @@
|
||||
using LiteCharms.Features.Email;
|
||||
using LiteCharms.Features.TechShop.Notifications.Models;
|
||||
using LiteCharms.Features.TechShop.Postgres;
|
||||
|
||||
namespace LiteCharms.Features.TechShop.Notifications.Events.Handlers;
|
||||
|
||||
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
|
||||
{
|
||||
using var context = await contextFactory.CreateDbContextAsync(cancellationToken);
|
||||
|
||||
if (emailService.Status != EmailStatuses.Connected)
|
||||
await emailService.ConnectAsync(cancellationToken);
|
||||
|
||||
var notifications = await context.Notifications
|
||||
.OrderByDescending(o => o.CreatedAt)
|
||||
.ThenBy(o => o.Priority)
|
||||
.Where(n => n.Platform == NotificationPlatforms.Email &&
|
||||
n.Direction == NotificationDirection.Outgoing && n.Processed == false)
|
||||
.Take(message.MaxRecords)
|
||||
.ToListAsync(cancellationToken);
|
||||
|
||||
foreach (var notification in notifications)
|
||||
{
|
||||
if (dropBatch) break;
|
||||
|
||||
var sendResult = await SendEmailAsync(notification,emailService, cancellationToken);
|
||||
|
||||
if(sendResult.IsFailed)
|
||||
{
|
||||
var errors = new List<string>(1000);
|
||||
|
||||
errors.AddRange(sendResult.Errors.Select(e => e.Message));
|
||||
|
||||
if (sendResult.Reasons?.Count > 0)
|
||||
errors.AddRange(sendResult.Reasons.Select(e => e.Message));
|
||||
|
||||
notification.HasError = true;
|
||||
notification.Errors = [.. errors];
|
||||
}
|
||||
|
||||
notification.Processed = true;
|
||||
notification.UpdatedAt = DateTime.UtcNow;
|
||||
}
|
||||
|
||||
await context.SaveChangesAsync(cancellationToken);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.LogError(ex, ex.Message);
|
||||
}
|
||||
finally
|
||||
{
|
||||
await emailService.DisconnectAsync(cancellationToken);
|
||||
}
|
||||
}
|
||||
|
||||
private async Task<Result> SendEmailAsync(Notification notification, EmailService service, CancellationToken cancellationToken = default)
|
||||
{
|
||||
try
|
||||
{
|
||||
using Email.Models.Message message = CreateMessage(notification);
|
||||
|
||||
var sendResult = await service.SendEmailAsync(message, cancellationToken);
|
||||
|
||||
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)
|
||||
{
|
||||
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
|
||||
}
|
||||
};
|
||||
}
|
||||
+25
@@ -0,0 +1,25 @@
|
||||
using static LiteCharms.Features.Extensions.Email;
|
||||
|
||||
namespace LiteCharms.Features.TechShop.Notifications.Events.Handlers;
|
||||
|
||||
public class SendShopEmailEnquiryEventHandler(NotificationService notificationService) :
|
||||
INotificationHandler<SendShopEmailEnquiryEvent>
|
||||
{
|
||||
public async ValueTask Handle(SendShopEmailEnquiryEvent notification, CancellationToken cancellationToken) =>
|
||||
await notificationService.CreateNotificationAsync(new Models.CreateNotification
|
||||
{
|
||||
CorrelationId = notification.CorrelationId,
|
||||
CorrelationIdType = CorrelationIdTypes.None,
|
||||
Direction = NotificationDirection.Outgoing,
|
||||
IsHtml = false,
|
||||
IsInternal = true,
|
||||
Message = notification.Message,
|
||||
Platform = NotificationPlatforms.Email,
|
||||
Priority = notification.Priority,
|
||||
Subject = notification.Subject!,
|
||||
Sender = notification.SenderName!,
|
||||
SenderAddress = notification.SenderAddress!,
|
||||
Recipient = ShopEmailFromName,
|
||||
RecipientAddress = ShopEmailFromAddress
|
||||
}, cancellationToken);
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
using LiteCharms.Features.Abstractions;
|
||||
|
||||
namespace LiteCharms.Features.TechShop.Notifications.Events;
|
||||
|
||||
public class ProcessEmailNotificationsEvent : EventBase, IEvent
|
||||
{
|
||||
public string Name { get; set; } = nameof(ProcessEmailNotificationsEvent);
|
||||
|
||||
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);
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
using LiteCharms.Features.Abstractions;
|
||||
|
||||
namespace LiteCharms.Features.TechShop.Notifications.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);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
using LiteCharms.Features.TechShop;
|
||||
|
||||
namespace LiteCharms.Features.TechShop.Notifications.Models;
|
||||
|
||||
public class Notification
|
||||
{
|
||||
public Guid Id { get; set; }
|
||||
|
||||
public DateTime CreatedAt { get; set; }
|
||||
|
||||
public DateTime? UpdatedAt { get; set; }
|
||||
|
||||
public NotificationDirection Direction { get; set; }
|
||||
|
||||
public NotificationPlatforms Platform { get; set; }
|
||||
|
||||
public Priorities Priority { get; set; }
|
||||
|
||||
public CorrelationIdTypes CorrelationIdType { get; set; }
|
||||
|
||||
public string? SenderAddress { get; set; }
|
||||
|
||||
public string? SenderName { get; set; }
|
||||
|
||||
public string? Subject { get; set; }
|
||||
|
||||
public string? Message { get; set; }
|
||||
|
||||
public string? RecipientName { get; set; }
|
||||
|
||||
public string? RecipientAddress { get; set; }
|
||||
|
||||
public string? CorrelationId { get; set; }
|
||||
|
||||
public bool IsHtml { get; set; }
|
||||
|
||||
public bool IsInternal { get; set; }
|
||||
|
||||
public bool Processed { get; set; }
|
||||
|
||||
public bool HasError { get; set; }
|
||||
|
||||
public string[]? Errors { get; set; }
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
namespace LiteCharms.Features.TechShop.Notifications.Models;
|
||||
|
||||
public record CreateNotification
|
||||
{
|
||||
public required NotificationDirection Direction { get; set; }
|
||||
|
||||
public required string Sender { get; set; }
|
||||
|
||||
public required string SenderAddress { get; set; }
|
||||
|
||||
public required string Subject { get; set; }
|
||||
|
||||
public string? Message { get; set; }
|
||||
|
||||
public required NotificationPlatforms Platform { get; set; }
|
||||
|
||||
public required Priorities Priority { get; set; }
|
||||
|
||||
public required string Recipient { get; set; }
|
||||
|
||||
public required string RecipientAddress { get; set; }
|
||||
|
||||
public string? CorrelationId { get; set; }
|
||||
|
||||
public CorrelationIdTypes CorrelationIdType { get; set; }
|
||||
|
||||
public bool IsInternal { get; set; }
|
||||
|
||||
public bool IsHtml { get; set; }
|
||||
}
|
||||
|
||||
public class UpdateNotification
|
||||
{
|
||||
public required Guid NotificationId { get; set; }
|
||||
|
||||
public required bool Processed { get; set; }
|
||||
|
||||
public bool HasError { get; set; }
|
||||
|
||||
public string[]? Errors { get; set; }
|
||||
}
|
||||
@@ -0,0 +1,113 @@
|
||||
using LiteCharms.Features.Extensions;
|
||||
using LiteCharms.Features.Models;
|
||||
using LiteCharms.Features.TechShop.Extensions;
|
||||
using LiteCharms.Features.TechShop.Notifications.Models;
|
||||
using LiteCharms.Features.TechShop.Postgres;
|
||||
|
||||
namespace LiteCharms.Features.TechShop.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 await context.SaveChangesAsync(cancellationToken) > 0
|
||||
? 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, DateTimeKind.Utc);
|
||||
var toDate = range.To.ToDateTime(TimeOnly.MaxValue, DateTimeKind.Utc);
|
||||
|
||||
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;
|
||||
notification.UpdatedAt = DateTime.UtcNow;
|
||||
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));
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user