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:
@@ -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}'.");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user