refactor(api): Update EmailsController and EmailSenderWorker for simplified email sending
This commit is contained in:
@@ -23,15 +23,14 @@ public class EmailsController(IMediator mediator) : ControllerBase
|
|||||||
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
||||||
public async Task<IActionResult> SendEmail([FromBody] SendEmailCommand command, CancellationToken cancellationToken)
|
public async Task<IActionResult> SendEmail([FromBody] SendEmailCommand command, CancellationToken cancellationToken)
|
||||||
{
|
{
|
||||||
var emailOutboxId = await mediator.Send(command, cancellationToken);
|
var outgoingEmailEvent = await mediator.Send(command, cancellationToken);
|
||||||
|
|
||||||
return Accepted(new
|
return Accepted(new
|
||||||
{
|
{
|
||||||
Message = "Email queued for sending",
|
CommandId = outgoingEmailEvent.Id,
|
||||||
EmailOutboxId = emailOutboxId,
|
To = outgoingEmailEvent.Recipient,
|
||||||
To = command.Recipient,
|
outgoingEmailEvent.Subject,
|
||||||
command.Subject,
|
outgoingEmailEvent.QueuedAt
|
||||||
QueuedAt = DateTime.Now
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
|
using DigitalData.EmailProfiler.Application.Common.Events;
|
||||||
using DigitalData.EmailProfiler.Application.Common.Interfaces;
|
using DigitalData.EmailProfiler.Application.Common.Interfaces;
|
||||||
using DigitalData.EmailProfiler.Domain.Entities;
|
using DigitalData.EmailProfiler.Application.EmailSending.Commands;
|
||||||
|
|
||||||
namespace DigitalData.EmailProfiler.API.Workers;
|
namespace DigitalData.EmailProfiler.API.Workers;
|
||||||
|
|
||||||
@@ -53,27 +54,27 @@ public class EmailSenderWorker(
|
|||||||
/// Process single email message (callback from RabbitMQ consumer)
|
/// Process single email message (callback from RabbitMQ consumer)
|
||||||
/// NO database operations - only send email and log result
|
/// NO database operations - only send email and log result
|
||||||
/// </summary>
|
/// </summary>
|
||||||
private async Task ProcessEmailAsync(EmailOutbox emailOutbox)
|
private async Task ProcessEmailAsync(OutgoingEmailEvent outgoingEmailEvent)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
Logger.LogInformation("Processing outgoing email: To={To}, Subject={Subject}",
|
Logger.LogInformation("Processing outgoing email: To={To}, Subject={Subject}",
|
||||||
emailOutbox.Recipient, emailOutbox.Subject);
|
outgoingEmailEvent.Recipient, outgoingEmailEvent.Subject);
|
||||||
|
|
||||||
// Send email via SMTP (SMTP config is injected in IEmailService via IOptions)
|
// Send email via SMTP (SMTP config is injected in IEmailService via IOptions)
|
||||||
await EmailService.SendEmailAsync(
|
await EmailService.SendEmailAsync(
|
||||||
emailOutbox.Recipient,
|
outgoingEmailEvent.Recipient,
|
||||||
emailOutbox.Subject,
|
outgoingEmailEvent.Subject,
|
||||||
emailOutbox.Body,
|
outgoingEmailEvent.Body,
|
||||||
isHtml: emailOutbox.IsHtml);
|
isHtml: outgoingEmailEvent.IsHtml);
|
||||||
|
|
||||||
Logger.LogInformation("Email sent successfully: To={To}, Subject={Subject}",
|
Logger.LogInformation("Email sent successfully: To={To}, Subject={Subject}",
|
||||||
emailOutbox.Recipient, emailOutbox.Subject);
|
outgoingEmailEvent.Recipient, outgoingEmailEvent.Subject);
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
Logger.LogError(ex, "Failed to send email: To={To}, Subject={Subject}. Moving to DLQ.",
|
Logger.LogError(ex, "Failed to send email: To={To}, Subject={Subject}. Moving to DLQ.",
|
||||||
emailOutbox.Recipient, emailOutbox.Subject);
|
outgoingEmailEvent.Recipient, outgoingEmailEvent.Subject);
|
||||||
|
|
||||||
// Re-throw to trigger NACK in RabbitMQ consumer (requeue=false → DLQ)
|
// Re-throw to trigger NACK in RabbitMQ consumer (requeue=false → DLQ)
|
||||||
throw;
|
throw;
|
||||||
|
|||||||
Reference in New Issue
Block a user