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:
@@ -0,0 +1,39 @@
|
||||
using DigitalData.MessagingService.Application.Common.Dtos;
|
||||
using DigitalData.MessagingService.Application.Common.Options;
|
||||
using MediatR;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Microsoft.Extensions.Options;
|
||||
|
||||
namespace DigitalData.MessagingService.Application.EmailAccount.Queries;
|
||||
|
||||
public record GetSenderQuery : IRequest<EmailAccountDto?>
|
||||
{
|
||||
public int? Id { get; init; }
|
||||
|
||||
public string? Username { get; init; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
/// <param name="Options"></param>
|
||||
/// <param name="Logger"></param>
|
||||
public class GetSenderQueryHandler(IOptions<EmailAccountsOptions> Options, ILogger<GetSenderQueryHandler> Logger) : IRequestHandler<GetSenderQuery, EmailAccountDto?>
|
||||
{
|
||||
public Task<EmailAccountDto?> Handle(GetSenderQuery request, CancellationToken cancellationToken)
|
||||
{
|
||||
var accounts = request.Id is not null
|
||||
? Options.Value.Accounts.Where(a => a.Id == request.Id)
|
||||
: Options.Value.Accounts.Where(a => a.Username == request.Username);
|
||||
|
||||
if(accounts.Count() > 1)
|
||||
{
|
||||
Logger.LogWarning(
|
||||
"Multiple email accounts found for the given criteria ({Criteria}). Returning the first one.",
|
||||
request.Id is not null ? $"Id: {request.Id}" : $"Username: {request.Username}"
|
||||
);
|
||||
}
|
||||
|
||||
return Task.FromResult(accounts.FirstOrDefault());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user