Refactor email account handling for dynamic resolution

Reintroduced `EmailAccountDto` with conditional compilation to support both .NET and non-.NET environments. Updated `IEmailService` to accept `EmailAccountDto` as the sender, replacing reliance on pre-configured SMTP credentials.

Added `GetSenderQuery` and its handler to dynamically resolve email accounts based on `Id` or `Username`. Introduced `GetSenderQueryValidator` for validation, ensuring proper usage of the query.

Modified `SendEmailCommand` to include sender resolution via MediatR. Updated `OutgoingEmailEvent` to include sender information and adjusted `OutgoingEmailConsumer` and `LimilabsEmailService` to use the dynamically resolved sender.

Updated `EmailMappingProfile` to ignore the `Sender` property during mapping. Replaced `Name` with `Id` in `appsettings.Secrets.json` for email accounts. Removed the old `EmailAccountDto` folder and performed general cleanup and restructuring.
This commit is contained in:
2026-08-05 12:32:08 +02:00
parent 7fa3c4888a
commit 740bb8c313
12 changed files with 136 additions and 45 deletions

View File

@@ -1,4 +1,7 @@
using AutoMapper;
using DigitalData.MessagingService.Application.Common.Dtos;
using DigitalData.MessagingService.Application.EmailAccount.Queries;
using DigitalData.MessagingService.Domain.Exceptions;
using DigitalData.MessagingService.Publisher.Abstraction;
using MediatR;
@@ -9,6 +12,8 @@ namespace DigitalData.MessagingService.Application.EmailSending.Commands;
/// </summary>
public record SendEmailCommand : IRequest<OutgoingEmailEvent>
{
public required GetSenderQuery Sender { get; init; }
/// <summary>
/// Recipient email address
/// </summary>
@@ -32,13 +37,20 @@ public record SendEmailCommand : IRequest<OutgoingEmailEvent>
/// <summary>
/// Handler for SendEmailCommand
/// Creates EmailOutbox entity via AutoMapper and enqueues to RabbitMQ
/// Resolves the sender account via MediatR, maps to OutgoingEmailEvent and enqueues to RabbitMQ
/// </summary>
public class SendEmailCommandHandler(IOutgoingEmailPublisher Publisher, IMapper Mapper) : IRequestHandler<SendEmailCommand, OutgoingEmailEvent>
public class SendEmailCommandHandler(
ISender Sender,
IOutgoingEmailPublisher Publisher,
IMapper Mapper) : IRequestHandler<SendEmailCommand, OutgoingEmailEvent>
{
public async Task<OutgoingEmailEvent> Handle(SendEmailCommand request, CancellationToken cancellationToken)
{
var outgoingEmailEvent = Mapper.Map<OutgoingEmailEvent>(request);
var senderAccount = await Sender.Send(request.Sender, cancellationToken)
?? throw new NotFoundException(
$"No email account found for the given sender criteria (Id: {request.Sender.Id}, Username: {request.Sender.Username}).");
var outgoingEmailEvent = Mapper.Map<OutgoingEmailEvent>(request) with { Sender = senderAccount };
// Enqueue to RabbitMQ
await Publisher.EnqueueAsync(outgoingEmailEvent, cancellationToken);