Refactor email handling for improved structure

Refactored `Email` and `SendingEmailEvent` to use `record` types, consolidating email-related data into the `Email` class. Updated `SendEmailCommand` to return a `Guid` and simplified mapping logic in `EmailMappingProfile`. Adjusted `SendEmailCommandHandler` to construct `SendingEmailEvent` manually.

Updated `SendingEmailConsumer`, `EmailsController`, and `EmailSender` to reflect the new structure. Removed the old `Email` implementation. Improved logging to reference the `Mail` property.

Revised tests to align with the new structure, ensuring immutability and better separation of concerns.
This commit is contained in:
2026-08-05 13:59:54 +02:00
parent 66afdefbd8
commit c6e67c0f99
11 changed files with 128 additions and 110 deletions

View File

@@ -48,11 +48,11 @@ public sealed class SendingEmailConsumer : IAsyncDisposable
{
// Send email via SMTP (SMTP config is injected in IEmailService via IOptions)
await EmailService.SendEmailAsync(
oMailEvent.Sender,
oMailEvent.Recipients,
oMailEvent.Subject,
oMailEvent.Body,
isHtml: oMailEvent.IsHtml);
oMailEvent.Mail.Sender,
oMailEvent.Mail.Recipients,
oMailEvent.Mail.Subject,
oMailEvent.Mail.Body,
isHtml: oMailEvent.Mail.IsHtml);
// Acknowledge message after successful processing
await channel.BasicAckAsync(args.DeliveryTag, false, args.CancellationToken);
@@ -65,7 +65,7 @@ public sealed class SendingEmailConsumer : IAsyncDisposable
}
catch (Exception ex)
{
logger?.LogError(ex, "Failed to process email [To={To}, Subject={Subject}] message: DeliveryTag={DeliveryTag}. Moving to DLQ (NO retry).", oMailEvent?.Recipients, oMailEvent?.Subject, args.DeliveryTag);
logger?.LogError(ex, "Failed to process email [To={To}, Subject={Subject}] message: DeliveryTag={DeliveryTag}. Moving to DLQ (NO retry).", oMailEvent?.Mail.Recipients, oMailEvent?.Mail.Subject, args.DeliveryTag);
// TODO: Error Reporting Strategy
// Option 1: Separate RabbitMQ Queue (emailprofiler.errors)