namespace HrynCo.NotificationService.Worker.Services.EmailProcessing; using HrynCo.NotificationService.Contracts.Messages; using Hrynco.RabbitMq; using Microsoft.Extensions.Logging; internal sealed class NotificationResultPublisher : INotificationResultPublisher { private readonly ILogger _logger; private readonly IRabbitMqPublisher _publisher; public NotificationResultPublisher( IRabbitMqPublisher publisher, ILogger logger) { _publisher = publisher; _logger = logger; } public async Task PublishAsync( SendEmailMessage message, string? deliveryError, CancellationToken cancellationToken) { CorrelationContext correlationContext = message.CorrelationContext; string? replyTo = correlationContext.ReplyTo; if (string.IsNullOrWhiteSpace(replyTo)) { return; } try { var result = new NotificationResultMessage { CorrelationContext = correlationContext with { ReplyTo = null }, Data = new NotificationResultData { ServiceName = message.Data.ServiceName, RecipientEmail = message.Data.RecipientEmail, TemplateKey = message.Data.TemplateKey, Timestamp = DateTimeOffset.UtcNow, ErrorMessage = deliveryError } }; await _publisher.PublishAsync(replyTo, result, cancellationToken); _logger.LogDebug( "Notification result published to reply queue {Queue} [CorrelationId={CorrelationId}]", replyTo, correlationContext.CorrelationId); } catch (Exception exception) { _logger.LogWarning( exception, "Failed to publish notification result to reply queue {Queue} [CorrelationId={CorrelationId}]", replyTo, correlationContext.CorrelationId); } } }