The `DigitalData.MessagingService.Publisher.Abstraction` project has been removed, and its functionality has been merged into a new project named `DigitalData.MessagingService.Abstraction`. - Updated namespaces from `Publisher.Abstraction` to `Abstraction` across all relevant files, including DTOs, interfaces, and classes. - Modified the solution file to remove `Publisher.Abstraction` and add `Abstraction`, updating solution configurations and nested project mappings. - Replaced project references to `Publisher.Abstraction` with `Abstraction` in all affected project files. - Updated tests and integration tests to reflect the namespace and project changes. - Refactored application-level files such as `EmailMappingProfile` and `DependencyInjection.cs` to use the new namespace. This refactor simplifies the project structure and ensures consistency across the solution.
59 lines
1.9 KiB
C#
59 lines
1.9 KiB
C#
using AutoMapper;
|
|
using DigitalData.MessagingService.Application.EmailAccount.Queries;
|
|
using DigitalData.MessagingService.Domain.Exceptions;
|
|
using DigitalData.MessagingService.Abstraction;
|
|
using MediatR;
|
|
|
|
namespace DigitalData.MessagingService.Application.EmailSending.Commands;
|
|
|
|
/// <summary>
|
|
/// Command to send an email (enqueue to RabbitMQ)
|
|
/// </summary>
|
|
public record SendEmailCommand : IRequest<OutgoingEmailEvent>
|
|
{
|
|
public required GetSenderQuery Sender { get; init; }
|
|
|
|
/// <summary>
|
|
/// Recipient email addresses
|
|
/// </summary>
|
|
public required IEnumerable<string> Recipients { get; init; }
|
|
|
|
/// <summary>
|
|
/// Email subject
|
|
/// </summary>
|
|
public required string Subject { get; init; }
|
|
|
|
/// <summary>
|
|
/// Email body (HTML or plain text)
|
|
/// </summary>
|
|
public required string Body { get; init; }
|
|
|
|
/// <summary>
|
|
/// Is HTML email (default: true)
|
|
/// </summary>
|
|
public bool IsHtml { get; init; } = true;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Handler for SendEmailCommand
|
|
/// Resolves the sender account via MediatR, maps to OutgoingEmailEvent and enqueues to RabbitMQ
|
|
/// </summary>
|
|
public class SendEmailCommandHandler(
|
|
ISender Sender,
|
|
IOutgoingEmailPublisher Publisher,
|
|
IMapper Mapper) : IRequestHandler<SendEmailCommand, OutgoingEmailEvent>
|
|
{
|
|
public async Task<OutgoingEmailEvent> Handle(SendEmailCommand request, CancellationToken cancellationToken)
|
|
{
|
|
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);
|
|
return outgoingEmailEvent;
|
|
}
|
|
}
|