refactor: modularize email processing logic and improve service structure
- Extract email template handling, rendering, and sending code into `Worker.Services` project. - Introduce `EmailTemplateService`, `EmailTemplateRenderingService`, and `SendEmailService`. - Simplify consumer logic by delegating to scoped services. - Update project dependencies and package references accordingly.
This commit is contained in:
+24
@@ -0,0 +1,24 @@
|
||||
namespace HrynCo.NotificationService.Worker.Services.EmailProcessing;
|
||||
|
||||
using System.Text;
|
||||
using HrynCo.NotificationService.Contracts.Messages;
|
||||
using HrynCo.NotificationService.DAL.Abstract.Templates;
|
||||
|
||||
internal sealed class EmailTemplateRenderingService : IEmailTemplateRenderingService
|
||||
{
|
||||
public RenderedEmail Render(EmailTemplate template, SendEmailMessageData data)
|
||||
{
|
||||
return new RenderedEmail(
|
||||
Interpolate(template.Subject, data.Variables),
|
||||
Interpolate(template.HtmlBody, data.Variables),
|
||||
Interpolate(template.TextBody, data.Variables));
|
||||
}
|
||||
|
||||
private static string Interpolate(string text, IReadOnlyDictionary<string, string> variables)
|
||||
{
|
||||
var sb = new StringBuilder(text);
|
||||
foreach (var (key, value) in variables)
|
||||
sb.Replace($"{{{{{key}}}}}", value);
|
||||
return sb.ToString();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
namespace HrynCo.NotificationService.Worker.Services.EmailProcessing;
|
||||
|
||||
using HrynCo.NotificationService.DAL.Abstract.Repositories;
|
||||
using HrynCo.NotificationService.DAL.Abstract.Templates;
|
||||
|
||||
internal sealed class EmailTemplateService : IEmailTemplateService
|
||||
{
|
||||
private readonly IEmailTemplateRepository _templateRepository;
|
||||
|
||||
public EmailTemplateService(IEmailTemplateRepository templateRepository)
|
||||
{
|
||||
_templateRepository = templateRepository;
|
||||
}
|
||||
|
||||
public async Task<EmailTemplate> GetAsync(
|
||||
string serviceName,
|
||||
string templateKey,
|
||||
string? languageCode,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
var lang = string.IsNullOrWhiteSpace(languageCode) ? "en" : languageCode;
|
||||
var template = await _templateRepository.GetAsync(serviceName, templateKey, lang, cancellationToken);
|
||||
|
||||
if (template is null && lang != "en")
|
||||
template = await _templateRepository.GetAsync(serviceName, templateKey, "en", cancellationToken);
|
||||
|
||||
return template
|
||||
?? throw new InvalidOperationException(
|
||||
$"Template not found: service='{serviceName}' key='{templateKey}' language='{lang}'.");
|
||||
}
|
||||
}
|
||||
+9
@@ -0,0 +1,9 @@
|
||||
using HrynCo.NotificationService.Contracts.Messages;
|
||||
using HrynCo.NotificationService.DAL.Abstract.Templates;
|
||||
|
||||
namespace HrynCo.NotificationService.Worker.Services.EmailProcessing;
|
||||
|
||||
public interface IEmailTemplateRenderingService
|
||||
{
|
||||
RenderedEmail Render(EmailTemplate template, SendEmailMessageData data);
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
using HrynCo.NotificationService.DAL.Abstract.Templates;
|
||||
|
||||
namespace HrynCo.NotificationService.Worker.Services.EmailProcessing;
|
||||
|
||||
public interface IEmailTemplateService
|
||||
{
|
||||
Task<EmailTemplate> GetAsync(
|
||||
string serviceName,
|
||||
string templateKey,
|
||||
string? languageCode,
|
||||
CancellationToken cancellationToken);
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
using HrynCo.NotificationService.Contracts.Messages;
|
||||
|
||||
namespace HrynCo.NotificationService.Worker.Services.EmailProcessing;
|
||||
|
||||
public interface ISendEmailService
|
||||
{
|
||||
Task ProcessAsync(SendEmailMessage message, CancellationToken cancellationToken);
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
namespace HrynCo.NotificationService.Worker.Services.EmailProcessing;
|
||||
|
||||
public record RenderedEmail(string Subject, string HtmlBody, string TextBody);
|
||||
@@ -0,0 +1,198 @@
|
||||
namespace HrynCo.NotificationService.Worker.Services.EmailProcessing;
|
||||
|
||||
using System.Net;
|
||||
using System.Net.Mail;
|
||||
using System.Text;
|
||||
using HrynCo.NotificationService.Contracts.Messages;
|
||||
using HrynCo.NotificationService.DAL.Abstract.Providers;
|
||||
using HrynCo.NotificationService.DAL.Abstract.Repositories;
|
||||
using HrynCo.NotificationService.DAL.Abstract.Templates;
|
||||
using Hrynco.RabbitMq;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
internal sealed class SendEmailService : ISendEmailService
|
||||
{
|
||||
private readonly IEmailChannelRepository _channelRepository;
|
||||
private readonly IEmailChannelUsageRepository _usageRepository;
|
||||
private readonly IEmailTemplateService _templateService;
|
||||
private readonly IEmailTemplateRenderingService _templateRenderingService;
|
||||
private readonly IRabbitMqPublisher _publisher;
|
||||
private readonly ILogger<SendEmailService> _logger;
|
||||
|
||||
public SendEmailService(
|
||||
IEmailChannelRepository channelRepository,
|
||||
IEmailChannelUsageRepository usageRepository,
|
||||
IEmailTemplateService templateService,
|
||||
IEmailTemplateRenderingService templateRenderingService,
|
||||
IRabbitMqPublisher publisher,
|
||||
ILogger<SendEmailService> logger)
|
||||
{
|
||||
_channelRepository = channelRepository;
|
||||
_usageRepository = usageRepository;
|
||||
_templateService = templateService;
|
||||
_templateRenderingService = templateRenderingService;
|
||||
_publisher = publisher;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public async Task ProcessAsync(SendEmailMessage message, CancellationToken cancellationToken)
|
||||
{
|
||||
SendEmailMessageData data = message.Data;
|
||||
|
||||
_logger.LogInformation(
|
||||
"Processing SendEmail for service={Service} template={Template} recipient={Recipient} [CorrelationId={CorrelationId}]",
|
||||
data.ServiceName, data.TemplateKey, data.RecipientEmail, message.CorrelationContext?.CorrelationId);
|
||||
|
||||
EmailChannel channel = await ResolveChannelAsync(data.ServiceName, cancellationToken);
|
||||
EmailTemplate template = await GetTemplateAsync(data, cancellationToken);
|
||||
|
||||
await EnforceLimitsAsync(channel, cancellationToken);
|
||||
|
||||
RenderedEmail rendered = _templateRenderingService.Render(template, data);
|
||||
|
||||
SmtpChannelSettings smtpChannel = channel.Settings as SmtpChannelSettings
|
||||
?? throw new InvalidOperationException(
|
||||
$"Channel type '{channel.EmailChannelType}' is not supported for sending.");
|
||||
|
||||
try
|
||||
{
|
||||
using var client = new SmtpClient(smtpChannel.Host, smtpChannel.Port)
|
||||
{
|
||||
EnableSsl = smtpChannel.UseSsl,
|
||||
Credentials = string.IsNullOrWhiteSpace(smtpChannel.Username)
|
||||
? null
|
||||
: new NetworkCredential(smtpChannel.Username, smtpChannel.Password)
|
||||
};
|
||||
|
||||
using var mail = new MailMessage
|
||||
{
|
||||
From = new MailAddress(smtpChannel.FromEmail, smtpChannel.FromName),
|
||||
Subject = rendered.Subject,
|
||||
Body = rendered.TextBody,
|
||||
IsBodyHtml = false,
|
||||
BodyEncoding = Encoding.UTF8,
|
||||
SubjectEncoding = Encoding.UTF8
|
||||
};
|
||||
|
||||
if (!string.IsNullOrWhiteSpace(rendered.HtmlBody))
|
||||
{
|
||||
var html = AlternateView.CreateAlternateViewFromString(
|
||||
rendered.HtmlBody, Encoding.UTF8, "text/html");
|
||||
mail.AlternateViews.Add(html);
|
||||
}
|
||||
|
||||
mail.To.Add(new MailAddress(data.RecipientEmail, data.RecipientName));
|
||||
|
||||
await client.SendMailAsync(mail, cancellationToken);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "SMTP send failed for channel {ChannelId}", channel.Id);
|
||||
throw;
|
||||
}
|
||||
|
||||
await _usageRepository.IncrementUsageAsync(
|
||||
channel.Id,
|
||||
DateOnly.FromDateTime(DateTime.UtcNow),
|
||||
cancellationToken);
|
||||
|
||||
_logger.LogInformation(
|
||||
"Email sent successfully service={Service} template={Template} recipient={Recipient}",
|
||||
data.ServiceName, data.TemplateKey, data.RecipientEmail);
|
||||
|
||||
await PublishResultAsync(message.CorrelationContext, data, null, cancellationToken);
|
||||
}
|
||||
|
||||
private async Task<EmailTemplate> GetTemplateAsync(SendEmailMessageData data, CancellationToken cancellationToken)
|
||||
{
|
||||
return await _templateService.GetAsync(
|
||||
data.ServiceName,
|
||||
data.TemplateKey,
|
||||
data.LanguageCode,
|
||||
cancellationToken);
|
||||
}
|
||||
|
||||
private async Task<EmailChannel> ResolveChannelAsync(string serviceName, CancellationToken ct)
|
||||
{
|
||||
var channels = await _channelRepository.GetByServiceAsync(serviceName, ct);
|
||||
|
||||
return channels
|
||||
.Where(c => c.IsActive)
|
||||
.OrderBy(c => c.Priority)
|
||||
.FirstOrDefault()
|
||||
?? throw new InvalidOperationException(
|
||||
$"No active email channel found for service '{serviceName}'.");
|
||||
}
|
||||
|
||||
private async Task EnforceLimitsAsync(EmailChannel channel, CancellationToken ct)
|
||||
{
|
||||
DateOnly today = DateOnly.FromDateTime(DateTime.UtcNow);
|
||||
|
||||
if (channel.DailyLimit.HasValue)
|
||||
{
|
||||
int daily = await _usageRepository.GetDailyCountAsync(channel.Id, today, ct);
|
||||
|
||||
if (daily >= channel.DailyLimit.Value)
|
||||
{
|
||||
throw new InvalidOperationException(
|
||||
$"Channel '{channel.Id}' daily limit of {channel.DailyLimit.Value} reached ({daily} sent today).");
|
||||
}
|
||||
}
|
||||
|
||||
if (channel.MonthlyLimit.HasValue)
|
||||
{
|
||||
int monthly = await _usageRepository.GetMonthlyCountAsync(channel.Id, today.Year, today.Month, ct);
|
||||
|
||||
if (monthly >= channel.MonthlyLimit.Value)
|
||||
{
|
||||
throw new InvalidOperationException(
|
||||
$"Channel '{channel.Id}' monthly limit of {channel.MonthlyLimit.Value} reached ({monthly} sent this month).");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private async Task PublishResultAsync(
|
||||
CorrelationContext? correlationContext,
|
||||
SendEmailMessageData data,
|
||||
string? errorMessage,
|
||||
CancellationToken ct)
|
||||
{
|
||||
string? replyTo = correlationContext?.ReplyTo;
|
||||
|
||||
if (string.IsNullOrWhiteSpace(replyTo))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
var result = new NotificationResultMessage
|
||||
{
|
||||
CorrelationContext = (correlationContext ?? new CorrelationContext
|
||||
{
|
||||
CorrelationId = Guid.NewGuid().ToString()
|
||||
}) with
|
||||
{
|
||||
ReplyTo = null
|
||||
},
|
||||
Data = new NotificationResultData
|
||||
{
|
||||
ServiceName = data.ServiceName,
|
||||
RecipientEmail = data.RecipientEmail,
|
||||
TemplateKey = data.TemplateKey,
|
||||
Timestamp = DateTimeOffset.UtcNow,
|
||||
ErrorMessage = errorMessage
|
||||
}
|
||||
};
|
||||
|
||||
await _publisher.PublishAsync(replyTo, result, ct);
|
||||
|
||||
_logger.LogDebug("Result published to reply queue '{Queue}' [CorrelationId={CorrelationId}]",
|
||||
replyTo, correlationContext?.CorrelationId);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogWarning(ex, "Failed to publish notification result to reply queue '{Queue}'", replyTo);
|
||||
}
|
||||
}
|
||||
}
|
||||
+21
@@ -0,0 +1,21 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net10.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
<RootNamespace>HrynCo.NotificationService.Worker.Services</RootNamespace>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="HrynCo.RabbitMq" />
|
||||
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" />
|
||||
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\HrynCo.NotificationService.Contracts\HrynCo.NotificationService.Contracts.csproj" />
|
||||
<ProjectReference Include="..\HrynCo.NotificationService.DAL.Abstract\HrynCo.NotificationService.DAL.Abstract.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
@@ -0,0 +1,17 @@
|
||||
using HrynCo.NotificationService.Worker.Services.EmailProcessing;
|
||||
using Hrynco.RabbitMq;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
|
||||
namespace HrynCo.NotificationService.Worker.Services;
|
||||
|
||||
public static class ServiceCollectionExtensions
|
||||
{
|
||||
public static IServiceCollection AddNotificationWorkerServices(this IServiceCollection services)
|
||||
{
|
||||
services.AddSingleton<IRabbitMqPublisher, RabbitMqPublisher>();
|
||||
services.AddScoped<IEmailTemplateService, EmailTemplateService>();
|
||||
services.AddScoped<IEmailTemplateRenderingService, EmailTemplateRenderingService>();
|
||||
services.AddScoped<ISendEmailService, SendEmailService>();
|
||||
return services;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user