2 Commits

57 changed files with 394 additions and 858 deletions
+2 -2
View File
@@ -4,8 +4,6 @@
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<!-- Entity Framework Core --> <!-- Entity Framework Core -->
<PackageVersion Include="HrynCo.DAL.Abstract" Version="1.0.11" />
<PackageVersion Include="HrynCo.DAL.EF" Version="1.0.11" />
<PackageVersion Include="Microsoft.EntityFrameworkCore" Version="10.0.7" /> <PackageVersion Include="Microsoft.EntityFrameworkCore" Version="10.0.7" />
<PackageVersion Include="Microsoft.EntityFrameworkCore.Design" Version="10.0.7" /> <PackageVersion Include="Microsoft.EntityFrameworkCore.Design" Version="10.0.7" />
<PackageVersion Include="Microsoft.EntityFrameworkCore.Relational" Version="10.0.7" /> <PackageVersion Include="Microsoft.EntityFrameworkCore.Relational" Version="10.0.7" />
@@ -31,6 +29,8 @@
<!-- HrynCo shared packages --> <!-- HrynCo shared packages -->
<PackageVersion Include="HrynCo.Common" Version="1.0.11" /> <PackageVersion Include="HrynCo.Common" Version="1.0.11" />
<PackageVersion Include="HrynCo.RabbitMq" Version="1.0.15" /> <PackageVersion Include="HrynCo.RabbitMq" Version="1.0.15" />
<PackageVersion Include="HrynCo.DAL.Abstract" Version="1.0.10" />
<PackageVersion Include="HrynCo.DAL.EF" Version="1.0.1" />
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="17.14.1" /> <PackageVersion Include="Microsoft.NET.Test.Sdk" Version="17.14.1" />
<PackageVersion Include="coverlet.collector" Version="6.0.4" /> <PackageVersion Include="coverlet.collector" Version="6.0.4" />
<PackageVersion Include="xunit" Version="2.9.3" /> <PackageVersion Include="xunit" Version="2.9.3" />
@@ -1,13 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk"> <Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<PackageReference Include="HrynCo.DAL.Abstract" />
</ItemGroup>
<PropertyGroup> <PropertyGroup>
<TargetFramework>net10.0</TargetFramework> <TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings> <ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable> <Nullable>enable</Nullable>
</PropertyGroup> </PropertyGroup>
<ItemGroup>
<PackageReference Include="HrynCo.DAL.Abstract" />
</ItemGroup>
</Project> </Project>
@@ -1,14 +1,13 @@
using HrynCo.DAL.Abstract.Entities;
namespace HrynCo.NotificationService.DAL.Abstract.Providers; namespace HrynCo.NotificationService.DAL.Abstract.Providers;
/// <summary> /// <summary>
/// Tracks email send counts per EmailChannel per calendar day. /// Tracks email send counts per EmailChannel per calendar day.
/// Monthly counts are derived by summing daily records within a month. /// Monthly counts are derived by summing daily records within a month.
/// </summary> /// </summary>
public class EmailChannelUsage public class EmailChannelUsage : Entity
{ {
public Guid Id { get; set; }
public DateTimeOffset Created { get; set; }
public DateTimeOffset? Updated { get; set; }
public Guid ProviderId { get; set; } public Guid ProviderId { get; set; }
public DateOnly Date { get; set; } public DateOnly Date { get; set; }
public int SentCount { get; set; } public int SentCount { get; set; }
@@ -4,7 +4,7 @@ namespace HrynCo.NotificationService.DAL.Abstract.Repositories;
public interface IEmailTemplateRepository public interface IEmailTemplateRepository
{ {
Task<IReadOnlyList<EmailTemplate>> GetAllAsync(string? serviceName = null, string? key = null, CancellationToken ct = default); Task<IReadOnlyList<EmailTemplate>> GetAllAsync(CancellationToken ct = default);
Task<IReadOnlyList<EmailTemplate>> GetByServiceAsync(string serviceName, CancellationToken ct = default); Task<IReadOnlyList<EmailTemplate>> GetByServiceAsync(string serviceName, CancellationToken ct = default);
Task<EmailTemplate?> GetAsync(string serviceName, string key, string languageCode, CancellationToken ct = default); Task<EmailTemplate?> GetAsync(string serviceName, string key, string languageCode, CancellationToken ct = default);
Task AddAsync(EmailTemplate template, CancellationToken ct = default); Task AddAsync(EmailTemplate template, CancellationToken ct = default);
@@ -1,20 +0,0 @@
namespace HrynCo.NotificationService.DAL.EF.Core;
using HrynCo.DAL.Abstract.Entities;
using HrynCo.DAL.EF.Core;
public abstract class NotificationBaseRepository<TEntity>
: BaseRepository<NotificationEfRepository<TEntity>, NotificationDbContext, TEntity, Guid> where TEntity : Entity
{
protected NotificationBaseRepository(NotificationDbContext dbContext)
{
DbContext = dbContext;
}
private NotificationDbContext DbContext { get; set; }
protected override NotificationEfRepository<TEntity> CreateEfRepository()
{
return new NotificationEfRepository<TEntity>(DbContext);
}
}
@@ -1,13 +0,0 @@
namespace HrynCo.NotificationService.DAL.EF.Core;
using HrynCo.DAL.Abstract.Entities;
using HrynCo.DAL.EF.Core;
public class NotificationEfRepository<TEntity> : BaseEfRepository<NotificationDbContext, TEntity, Guid>
where TEntity : class, IEntity<Guid>
{
public NotificationEfRepository(NotificationDbContext dbContext) :
base(dbContext)
{
}
}
@@ -0,0 +1,10 @@
using HrynCo.DAL.EF.Core;
namespace HrynCo.NotificationService.DAL.EF.Core;
internal sealed class UnitOfWork : EfUnitOfWork<NotificationDbContext>
{
public UnitOfWork(NotificationDbContext context) : base(context)
{
}
}
@@ -6,6 +6,7 @@
<ItemGroup> <ItemGroup>
<PackageReference Include="HrynCo.DAL.EF" /> <PackageReference Include="HrynCo.DAL.EF" />
<PackageReference Include="Microsoft.EntityFrameworkCore" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design"> <PackageReference Include="Microsoft.EntityFrameworkCore.Design">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets> <PrivateAssets>all</PrivateAssets>
@@ -1,10 +0,0 @@
namespace HrynCo.NotificationService.DAL.EF;
using HrynCo.DAL.EF.Core;
public class NotificationUnitOfWork : EfUnitOfWork<NotificationDbContext>
{
public NotificationUnitOfWork(NotificationDbContext context) : base(context)
{
}
}
@@ -1,13 +1,13 @@
namespace HrynCo.NotificationService.DAL.EF.Repositories;
using System.Text.Json; using System.Text.Json;
using HrynCo.NotificationService.DAL.Abstract.Providers; using HrynCo.NotificationService.DAL.Abstract.Providers;
using HrynCo.NotificationService.DAL.Abstract.Repositories; using HrynCo.NotificationService.DAL.Abstract.Repositories;
using HrynCo.NotificationService.DAL.EF.Core; using HrynCo.DAL.EF.Core;
using HrynCo.NotificationService.DAL.EF.Entities; using HrynCo.NotificationService.DAL.EF.Entities;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
internal sealed class EmailChannelRepository : NotificationBaseRepository<EmailChannelEntity>, IEmailChannelRepository namespace HrynCo.NotificationService.DAL.EF.Repositories;
internal sealed class EmailChannelRepository : EfRepository<NotificationDbContext, EmailChannelEntity>, IEmailChannelRepository
{ {
public EmailChannelRepository(NotificationDbContext dbContext) : base(dbContext) public EmailChannelRepository(NotificationDbContext dbContext) : base(dbContext)
{ {
@@ -15,14 +15,20 @@ internal sealed class EmailChannelRepository : NotificationBaseRepository<EmailC
public async Task<IReadOnlyList<EmailChannel>> GetAllAsync(CancellationToken ct = default) public async Task<IReadOnlyList<EmailChannel>> GetAllAsync(CancellationToken ct = default)
{ {
var entities = await EfRepository.Get().ToListAsync(ct); var entities = await DbSet
.AsNoTracking()
.OrderBy(x => x.ServiceName)
.ThenBy(x => x.Priority)
.ToListAsync(ct);
return entities.Select(MapToDomain).ToList(); return entities.Select(MapToDomain).ToList();
} }
public async Task<IReadOnlyList<EmailChannel>> GetByServiceAsync(string serviceName, CancellationToken ct = default) public async Task<IReadOnlyList<EmailChannel>> GetByServiceAsync(string serviceName, CancellationToken ct = default)
{ {
var entities = await EfRepository.Get(x => x.ServiceName == serviceName) var entities = await DbSet
.AsNoTracking()
.Where(x => x.ServiceName == serviceName)
.OrderBy(x => x.Priority) .OrderBy(x => x.Priority)
.ToListAsync(ct); .ToListAsync(ct);
@@ -32,7 +38,8 @@ internal sealed class EmailChannelRepository : NotificationBaseRepository<EmailC
public async Task<IReadOnlyList<ChannelWithUsage>> GetAllWithUsageSummaryAsync( public async Task<IReadOnlyList<ChannelWithUsage>> GetAllWithUsageSummaryAsync(
DateOnly today, CancellationToken ct = default) DateOnly today, CancellationToken ct = default)
{ {
var rows = await EfRepository.Get() var rows = await DbSet
.AsNoTracking()
.OrderBy(c => c.ServiceName) .OrderBy(c => c.ServiceName)
.ThenBy(c => c.Priority) .ThenBy(c => c.Priority)
.Select(c => new .Select(c => new
@@ -54,24 +61,30 @@ internal sealed class EmailChannelRepository : NotificationBaseRepository<EmailC
public async Task<EmailChannel?> GetByIdAsync(Guid id, CancellationToken ct = default) public async Task<EmailChannel?> GetByIdAsync(Guid id, CancellationToken ct = default)
{ {
EmailChannelEntity? entity = await EfRepository.GetByIdAsync(id); EmailChannelEntity? entity = await DbSet.AsNoTracking().FirstOrDefaultAsync(x => x.Id == id, ct);
return entity is null ? null : MapToDomain(entity); return entity is null ? null : MapToDomain(entity);
} }
public Task AddAsync(EmailChannel channel, CancellationToken ct = default) public Task AddAsync(EmailChannel channel, CancellationToken ct = default)
{ {
return EfRepository.AddAsync(MapToEntity(channel)); return base.AddAsync(MapToEntity(channel), ct);
} }
public async Task UpdateAsync(EmailChannel channel, CancellationToken ct = default) public Task UpdateAsync(EmailChannel channel, CancellationToken ct = default)
{ {
EmailChannelEntity entity = MapToEntity(channel); EmailChannelEntity entity = MapToEntity(channel);
await EfRepository.UpdateAsync(entity); entity.Updated = DateTimeOffset.UtcNow;
Update(entity);
return Task.CompletedTask;
} }
public async Task DeleteAsync(EmailChannel channel, CancellationToken ct = default) public async Task DeleteAsync(EmailChannel channel, CancellationToken ct = default)
{ {
await EfRepository.DeleteAsync(channel.Id); EmailChannelEntity? entity = await DbSet.FindAsync([channel.Id], ct);
if (entity is not null)
{
Delete(entity);
}
} }
private static EmailChannel MapToDomain(EmailChannelEntity e) private static EmailChannel MapToDomain(EmailChannelEntity e)
@@ -1,11 +1,11 @@
using HrynCo.NotificationService.DAL.Abstract.Repositories; using HrynCo.NotificationService.DAL.Abstract.Repositories;
using HrynCo.NotificationService.DAL.EF.Core; using HrynCo.DAL.EF.Core;
using HrynCo.NotificationService.DAL.EF.Entities; using HrynCo.NotificationService.DAL.EF.Entities;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
namespace HrynCo.NotificationService.DAL.EF.Repositories; namespace HrynCo.NotificationService.DAL.EF.Repositories;
internal sealed class EmailChannelUsageRepository : NotificationBaseRepository<EmailChannelUsageEntity>, IEmailChannelUsageRepository internal sealed class EmailChannelUsageRepository : EfRepository<NotificationDbContext, EmailChannelUsageEntity>, IEmailChannelUsageRepository
{ {
public EmailChannelUsageRepository(NotificationDbContext dbContext) : base(dbContext) public EmailChannelUsageRepository(NotificationDbContext dbContext) : base(dbContext)
{ {
@@ -13,8 +13,7 @@ internal sealed class EmailChannelUsageRepository : NotificationBaseRepository<E
public async Task<int> GetDailyCountAsync(Guid providerId, DateOnly date, CancellationToken ct = default) public async Task<int> GetDailyCountAsync(Guid providerId, DateOnly date, CancellationToken ct = default)
{ {
EmailChannelUsageEntity? entity = await EfRepository.Get() EmailChannelUsageEntity? entity = await DbSet
.AsNoTracking()
.FirstOrDefaultAsync(x => x.ProviderId == providerId && x.Date == date, ct); .FirstOrDefaultAsync(x => x.ProviderId == providerId && x.Date == date, ct);
return entity?.SentCount ?? 0; return entity?.SentCount ?? 0;
@@ -22,7 +21,7 @@ internal sealed class EmailChannelUsageRepository : NotificationBaseRepository<E
public async Task<int> GetMonthlyCountAsync(Guid providerId, int year, int month, CancellationToken ct = default) public async Task<int> GetMonthlyCountAsync(Guid providerId, int year, int month, CancellationToken ct = default)
{ {
return await EfRepository.Get() return await DbSet
.Where(x => x.ProviderId == providerId .Where(x => x.ProviderId == providerId
&& x.Date.Year == year && x.Date.Year == year
&& x.Date.Month == month) && x.Date.Month == month)
@@ -31,16 +30,15 @@ internal sealed class EmailChannelUsageRepository : NotificationBaseRepository<E
public async Task IncrementUsageAsync(Guid providerId, DateOnly date, CancellationToken ct = default) public async Task IncrementUsageAsync(Guid providerId, DateOnly date, CancellationToken ct = default)
{ {
EmailChannelUsageEntity? entity = await EfRepository.Get() EmailChannelUsageEntity? entity = await DbSet
.AsNoTracking()
.FirstOrDefaultAsync(x => x.ProviderId == providerId && x.Date == date, ct); .FirstOrDefaultAsync(x => x.ProviderId == providerId && x.Date == date, ct);
if (entity is null) if (entity is null)
await EfRepository.AddAsync(new EmailChannelUsageEntity { ProviderId = providerId, Date = date, SentCount = 1 }); await AddAsync(new EmailChannelUsageEntity { ProviderId = providerId, Date = date, SentCount = 1 }, ct);
else else
{ {
entity.SentCount++; entity.SentCount++;
await EfRepository.UpdateAsync(entity); Update(entity);
} }
} }
} }
@@ -1,34 +1,20 @@
using HrynCo.NotificationService.DAL.Abstract.Repositories; using HrynCo.NotificationService.DAL.Abstract.Repositories;
using HrynCo.NotificationService.DAL.Abstract.Templates; using HrynCo.NotificationService.DAL.Abstract.Templates;
using HrynCo.NotificationService.DAL.EF.Core; using HrynCo.DAL.EF.Core;
using HrynCo.NotificationService.DAL.EF.Entities; using HrynCo.NotificationService.DAL.EF.Entities;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
namespace HrynCo.NotificationService.DAL.EF.Repositories; namespace HrynCo.NotificationService.DAL.EF.Repositories;
internal sealed class EmailTemplateRepository internal sealed class EmailTemplateRepository : EfRepository<NotificationDbContext, EmailTemplateEntity>, IEmailTemplateRepository
: NotificationBaseRepository<EmailTemplateEntity>, IEmailTemplateRepository
{ {
public EmailTemplateRepository(NotificationDbContext dbContext) : base(dbContext) public EmailTemplateRepository(NotificationDbContext dbContext) : base(dbContext)
{ {
} }
public async Task<IReadOnlyList<EmailTemplate>> GetAllAsync(string? serviceName = null, string? key = null, CancellationToken ct = default) public async Task<IReadOnlyList<EmailTemplate>> GetAllAsync(CancellationToken ct = default)
{ {
IQueryable<EmailTemplateEntity> query = EfRepository.Get(); List<EmailTemplateEntity> entities = await DbSet
if (!string.IsNullOrWhiteSpace(serviceName))
{
query = query.Where(x => x.ServiceName == serviceName);
}
if (!string.IsNullOrWhiteSpace(key))
{
query = query.Where(x => x.Key == key);
}
List<EmailTemplateEntity> entities = await query
.OrderBy(x => x.ServiceName).ThenBy(x => x.Key)
.AsNoTracking() .AsNoTracking()
.ToListAsync(ct); .ToListAsync(ct);
return entities.Select(MapToDomain).ToList(); return entities.Select(MapToDomain).ToList();
@@ -36,7 +22,7 @@ internal sealed class EmailTemplateRepository
public async Task<IReadOnlyList<EmailTemplate>> GetByServiceAsync(string serviceName, CancellationToken ct = default) public async Task<IReadOnlyList<EmailTemplate>> GetByServiceAsync(string serviceName, CancellationToken ct = default)
{ {
List<EmailTemplateEntity> entities = await EfRepository.Get() List<EmailTemplateEntity> entities = await DbSet
.AsNoTracking() .AsNoTracking()
.Where(x => x.ServiceName == serviceName) .Where(x => x.ServiceName == serviceName)
.ToListAsync(ct); .ToListAsync(ct);
@@ -46,7 +32,7 @@ internal sealed class EmailTemplateRepository
public async Task<EmailTemplate?> GetAsync(string serviceName, string key, string languageCode, CancellationToken ct = default) public async Task<EmailTemplate?> GetAsync(string serviceName, string key, string languageCode, CancellationToken ct = default)
{ {
EmailTemplateEntity? entity = await EfRepository.Get() EmailTemplateEntity? entity = await DbSet
.AsNoTracking() .AsNoTracking()
.FirstOrDefaultAsync( .FirstOrDefaultAsync(
x => x.ServiceName == serviceName && x.Key == key && x.LanguageCode == languageCode, ct); x => x.ServiceName == serviceName && x.Key == key && x.LanguageCode == languageCode, ct);
@@ -54,40 +40,22 @@ internal sealed class EmailTemplateRepository
return entity is null ? null : MapToDomain(entity); return entity is null ? null : MapToDomain(entity);
} }
public Task AddAsync(EmailTemplate EmailTemplate, CancellationToken ct = default) public Task AddAsync(EmailTemplate EmailTemplate, CancellationToken ct = default) =>
base.AddAsync(MapToEntity(EmailTemplate), ct);
public Task UpdateAsync(EmailTemplate EmailTemplate, CancellationToken ct = default)
{ {
return EfRepository.AddAsync(MapToEntity(EmailTemplate)); EmailTemplateEntity entity = MapToEntity(EmailTemplate);
} entity.Updated = DateTimeOffset.UtcNow;
Update(entity);
public async Task UpdateAsync(EmailTemplate EmailTemplate, CancellationToken ct = default) return Task.CompletedTask;
{
EmailTemplateEntity? entity = await EfRepository.Get()
.FirstOrDefaultAsync(x => x.Id == EmailTemplate.Id, ct);
if (entity is null)
{
return;
}
entity.ServiceName = EmailTemplate.ServiceName;
entity.Key = EmailTemplate.Key;
entity.LanguageCode = EmailTemplate.LanguageCode;
entity.Subject = EmailTemplate.Subject;
entity.HtmlBody = EmailTemplate.HtmlBody;
entity.TextBody = EmailTemplate.TextBody;
entity.Variables = EmailTemplate.Variables
.Select(v => new EmailTemplateVariableData { Name = v.Name, Required = v.Required })
.ToList();
await EfRepository.SaveChangesAsync();
} }
public async Task DeleteAsync(EmailTemplate EmailTemplate, CancellationToken ct = default) public async Task DeleteAsync(EmailTemplate EmailTemplate, CancellationToken ct = default)
{ {
EmailTemplateEntity? entity = await EfRepository.Get() EmailTemplateEntity? entity = await DbSet.FindAsync([EmailTemplate.Id], ct);
.FirstOrDefaultAsync(x => x.Id == EmailTemplate.Id, ct);
if (entity is not null) if (entity is not null)
await EfRepository.DeleteAsync(entity); Delete(entity);
} }
private static EmailTemplate MapToDomain(EmailTemplateEntity e) => new() private static EmailTemplate MapToDomain(EmailTemplateEntity e) => new()
@@ -16,10 +16,10 @@ public static class ServiceCollectionExtensions
services.AddDbContext<NotificationDbContext>(options => services.AddDbContext<NotificationDbContext>(options =>
options.UseNpgsql(connectionString)); options.UseNpgsql(connectionString));
services.AddScoped<IUnitOfWork, UnitOfWork>();
services.AddScoped<IEmailTemplateRepository, EmailTemplateRepository>(); services.AddScoped<IEmailTemplateRepository, EmailTemplateRepository>();
services.AddScoped<IEmailChannelRepository, EmailChannelRepository>(); services.AddScoped<IEmailChannelRepository, EmailChannelRepository>();
services.AddScoped<IEmailChannelUsageRepository, EmailChannelUsageRepository>(); services.AddScoped<IEmailChannelUsageRepository, EmailChannelUsageRepository>();
services.AddScoped<IUnitOfWork, NotificationUnitOfWork>();
return services; return services;
} }
@@ -1,3 +1,4 @@
using HrynCo.DAL.Abstract;
using HrynCo.NotificationService.Services.Logging; using HrynCo.NotificationService.Services.Logging;
using MediatR; using MediatR;
using Serilog; using Serilog;
@@ -7,12 +8,14 @@ namespace HrynCo.NotificationService.Services.Core;
public abstract class RequestHandler<TRequest, TResponse> : IRequestHandler<TRequest, TResponse> public abstract class RequestHandler<TRequest, TResponse> : IRequestHandler<TRequest, TResponse>
where TRequest : IRequest<TResponse> where TRequest : IRequest<TResponse>
{ {
protected RequestHandler(IContextualSerilogLogger<TRequest> logger) protected RequestHandler(IContextualSerilogLogger<TRequest> logger, IUnitOfWork unitOfWork)
{ {
Logger = logger.Logger; Logger = logger.Logger;
UnitOfWork = unitOfWork;
} }
protected ILogger Logger { get; } protected ILogger Logger { get; }
protected IUnitOfWork UnitOfWork { get; }
public Task<TResponse> Handle(TRequest request, CancellationToken cancellationToken) public Task<TResponse> Handle(TRequest request, CancellationToken cancellationToken)
{ {
@@ -14,8 +14,9 @@ internal sealed class CreateEmailChannelHandler
public CreateEmailChannelHandler( public CreateEmailChannelHandler(
IContextualSerilogLogger<CreateEmailChannelCommand> logger, IContextualSerilogLogger<CreateEmailChannelCommand> logger,
IUnitOfWork unitOfWork,
IEmailChannelRepository channels) IEmailChannelRepository channels)
: base(logger) : base(logger, unitOfWork)
{ {
_channels = channels; _channels = channels;
} }
@@ -13,8 +13,9 @@ internal sealed class DeleteEmailChannelHandler
public DeleteEmailChannelHandler( public DeleteEmailChannelHandler(
IContextualSerilogLogger<DeleteEmailChannelCommand> logger, IContextualSerilogLogger<DeleteEmailChannelCommand> logger,
IUnitOfWork unitOfWork,
IEmailChannelRepository channels) IEmailChannelRepository channels)
: base(logger) : base(logger, unitOfWork)
{ {
_channels = channels; _channels = channels;
} }
@@ -1,3 +1,4 @@
using HrynCo.DAL.Abstract;
using HrynCo.NotificationService.DAL.Abstract.Providers; using HrynCo.NotificationService.DAL.Abstract.Providers;
using HrynCo.NotificationService.DAL.Abstract.Repositories; using HrynCo.NotificationService.DAL.Abstract.Repositories;
using HrynCo.NotificationService.Services.Core; using HrynCo.NotificationService.Services.Core;
@@ -13,8 +14,9 @@ internal sealed class GetEmailChannelHandler
public GetEmailChannelHandler( public GetEmailChannelHandler(
IContextualSerilogLogger<GetEmailChannelQuery> logger, IContextualSerilogLogger<GetEmailChannelQuery> logger,
IUnitOfWork unitOfWork,
IEmailChannelRepository channels) IEmailChannelRepository channels)
: base(logger) : base(logger, unitOfWork)
{ {
_channels = channels; _channels = channels;
} }
@@ -1,3 +1,4 @@
using HrynCo.DAL.Abstract;
using HrynCo.NotificationService.DAL.Abstract.Providers; using HrynCo.NotificationService.DAL.Abstract.Providers;
using HrynCo.NotificationService.DAL.Abstract.Repositories; using HrynCo.NotificationService.DAL.Abstract.Repositories;
using HrynCo.NotificationService.Services.Core; using HrynCo.NotificationService.Services.Core;
@@ -13,8 +14,9 @@ internal sealed class GetAllEmailChannelsHandler
public GetAllEmailChannelsHandler( public GetAllEmailChannelsHandler(
IContextualSerilogLogger<GetAllEmailChannelsQuery> logger, IContextualSerilogLogger<GetAllEmailChannelsQuery> logger,
IUnitOfWork unitOfWork,
IEmailChannelRepository channels) IEmailChannelRepository channels)
: base(logger) : base(logger, unitOfWork)
{ {
_channels = channels; _channels = channels;
} }
@@ -1,3 +1,4 @@
using HrynCo.DAL.Abstract;
using HrynCo.NotificationService.DAL.Abstract.Providers; using HrynCo.NotificationService.DAL.Abstract.Providers;
using HrynCo.NotificationService.DAL.Abstract.Repositories; using HrynCo.NotificationService.DAL.Abstract.Repositories;
using HrynCo.NotificationService.Services.Core; using HrynCo.NotificationService.Services.Core;
@@ -13,8 +14,9 @@ internal sealed class GetEmailChannelsHandler
public GetEmailChannelsHandler( public GetEmailChannelsHandler(
IContextualSerilogLogger<GetEmailChannelsQuery> logger, IContextualSerilogLogger<GetEmailChannelsQuery> logger,
IUnitOfWork unitOfWork,
IEmailChannelRepository channels) IEmailChannelRepository channels)
: base(logger) : base(logger, unitOfWork)
{ {
_channels = channels; _channels = channels;
} }
@@ -1,3 +1,4 @@
using HrynCo.DAL.Abstract;
using HrynCo.NotificationService.DAL.Abstract.Repositories; using HrynCo.NotificationService.DAL.Abstract.Repositories;
using HrynCo.NotificationService.Services.Core; using HrynCo.NotificationService.Services.Core;
using HrynCo.NotificationService.Services.Logging; using HrynCo.NotificationService.Services.Logging;
@@ -12,8 +13,9 @@ internal sealed class GetChannelUsageSummaryHandler
public GetChannelUsageSummaryHandler( public GetChannelUsageSummaryHandler(
IContextualSerilogLogger<GetChannelUsageSummaryQuery> logger, IContextualSerilogLogger<GetChannelUsageSummaryQuery> logger,
IUnitOfWork unitOfWork,
IEmailChannelRepository channelsRepository) IEmailChannelRepository channelsRepository)
: base(logger) : base(logger, unitOfWork)
{ {
_channelsRepository = channelsRepository; _channelsRepository = channelsRepository;
} }
@@ -1,6 +1,6 @@
using System.Net; using System.Net;
using System.Net.Mail; using System.Net.Mail;
using System.Text; using HrynCo.DAL.Abstract;
using HrynCo.NotificationService.DAL.Abstract.Providers; using HrynCo.NotificationService.DAL.Abstract.Providers;
using HrynCo.NotificationService.DAL.Abstract.Repositories; using HrynCo.NotificationService.DAL.Abstract.Repositories;
using HrynCo.NotificationService.Services.Core; using HrynCo.NotificationService.Services.Core;
@@ -17,9 +17,10 @@ internal sealed class SendEmailHandler
public SendEmailHandler( public SendEmailHandler(
IContextualSerilogLogger<SendEmailCommand> logger, IContextualSerilogLogger<SendEmailCommand> logger,
IUnitOfWork unitOfWork,
IEmailChannelRepository channels, IEmailChannelRepository channels,
IEmailChannelUsageRepository usage) IEmailChannelUsageRepository usage)
: base(logger) : base(logger, unitOfWork)
{ {
_channels = channels; _channels = channels;
_usage = usage; _usage = usage;
@@ -49,16 +50,14 @@ internal sealed class SendEmailHandler
{ {
From = new MailAddress(smtp.FromEmail, smtp.FromName), From = new MailAddress(smtp.FromEmail, smtp.FromName),
Subject = request.Subject, Subject = request.Subject,
Body = request.TextBody ?? string.Empty, Body = request.HtmlBody,
IsBodyHtml = false, IsBodyHtml = true
BodyEncoding = Encoding.UTF8,
SubjectEncoding = Encoding.UTF8
}; };
if (!string.IsNullOrWhiteSpace(request.HtmlBody)) if (!string.IsNullOrWhiteSpace(request.TextBody))
{ {
var html = AlternateView.CreateAlternateViewFromString(request.HtmlBody, Encoding.UTF8, "text/html"); var plain = AlternateView.CreateAlternateViewFromString(request.TextBody, null, "text/plain");
mail.AlternateViews.Add(html); mail.AlternateViews.Add(plain);
} }
mail.To.Add(new MailAddress(request.RecipientEmail, request.RecipientName)); mail.To.Add(new MailAddress(request.RecipientEmail, request.RecipientName));
@@ -1,5 +1,6 @@
using System.Net; using System.Net;
using System.Net.Mail; using System.Net.Mail;
using HrynCo.DAL.Abstract;
using HrynCo.NotificationService.Services.Core; using HrynCo.NotificationService.Services.Core;
using HrynCo.NotificationService.Services.Logging; using HrynCo.NotificationService.Services.Logging;
using static HrynCo.NotificationService.Services.Core.ServiceResultHelper; using static HrynCo.NotificationService.Services.Core.ServiceResultHelper;
@@ -10,8 +11,9 @@ internal sealed class TestSmtpHandler
: RequestHandler<TestSmtpCommand, ServiceResult<Unit>> : RequestHandler<TestSmtpCommand, ServiceResult<Unit>>
{ {
public TestSmtpHandler( public TestSmtpHandler(
IContextualSerilogLogger<TestSmtpCommand> logger) IContextualSerilogLogger<TestSmtpCommand> logger,
: base(logger) IUnitOfWork unitOfWork)
: base(logger, unitOfWork)
{ {
} }
@@ -13,8 +13,9 @@ internal sealed class UpdateEmailChannelHandler
public UpdateEmailChannelHandler( public UpdateEmailChannelHandler(
IContextualSerilogLogger<UpdateEmailChannelCommand> logger, IContextualSerilogLogger<UpdateEmailChannelCommand> logger,
IUnitOfWork unitOfWork,
IEmailChannelRepository channels) IEmailChannelRepository channels)
: base(logger) : base(logger, unitOfWork)
{ {
_channels = channels; _channels = channels;
} }
@@ -1,3 +1,4 @@
using HrynCo.DAL.Abstract;
using HrynCo.NotificationService.DAL.Abstract.Repositories; using HrynCo.NotificationService.DAL.Abstract.Repositories;
using HrynCo.NotificationService.DAL.Abstract.Templates; using HrynCo.NotificationService.DAL.Abstract.Templates;
using HrynCo.NotificationService.Services.Core; using HrynCo.NotificationService.Services.Core;
@@ -13,8 +14,9 @@ internal sealed class CreateEmailTemplateHandler
public CreateEmailTemplateHandler( public CreateEmailTemplateHandler(
IContextualSerilogLogger<CreateEmailTemplateCommand> logger, IContextualSerilogLogger<CreateEmailTemplateCommand> logger,
IUnitOfWork unitOfWork,
IEmailTemplateRepository templates) IEmailTemplateRepository templates)
: base(logger) : base(logger, unitOfWork)
{ {
_templates = templates; _templates = templates;
} }
@@ -13,8 +13,9 @@ internal sealed class DeleteEmailTemplateHandler
public DeleteEmailTemplateHandler( public DeleteEmailTemplateHandler(
IContextualSerilogLogger<DeleteEmailTemplateCommand> logger, IContextualSerilogLogger<DeleteEmailTemplateCommand> logger,
IUnitOfWork unitOfWork,
IEmailTemplateRepository templates) IEmailTemplateRepository templates)
: base(logger) : base(logger, unitOfWork)
{ {
_templates = templates; _templates = templates;
} }
@@ -1,3 +1,4 @@
using HrynCo.DAL.Abstract;
using HrynCo.NotificationService.DAL.Abstract.Repositories; using HrynCo.NotificationService.DAL.Abstract.Repositories;
using HrynCo.NotificationService.DAL.Abstract.Templates; using HrynCo.NotificationService.DAL.Abstract.Templates;
using HrynCo.NotificationService.Services.Core; using HrynCo.NotificationService.Services.Core;
@@ -13,8 +14,9 @@ internal sealed class GetEmailTemplateHandler
public GetEmailTemplateHandler( public GetEmailTemplateHandler(
IContextualSerilogLogger<GetEmailTemplateQuery> logger, IContextualSerilogLogger<GetEmailTemplateQuery> logger,
IUnitOfWork unitOfWork,
IEmailTemplateRepository templates) IEmailTemplateRepository templates)
: base(logger) : base(logger, unitOfWork)
{ {
_templates = templates; _templates = templates;
} }
@@ -1,3 +1,4 @@
using HrynCo.DAL.Abstract;
using HrynCo.NotificationService.DAL.Abstract.Repositories; using HrynCo.NotificationService.DAL.Abstract.Repositories;
using HrynCo.NotificationService.DAL.Abstract.Templates; using HrynCo.NotificationService.DAL.Abstract.Templates;
using HrynCo.NotificationService.Services.Core; using HrynCo.NotificationService.Services.Core;
@@ -13,8 +14,9 @@ internal sealed class GetAllEmailTemplatesHandler
public GetAllEmailTemplatesHandler( public GetAllEmailTemplatesHandler(
IContextualSerilogLogger<GetAllEmailTemplatesQuery> logger, IContextualSerilogLogger<GetAllEmailTemplatesQuery> logger,
IUnitOfWork unitOfWork,
IEmailTemplateRepository templates) IEmailTemplateRepository templates)
: base(logger) : base(logger, unitOfWork)
{ {
_templates = templates; _templates = templates;
} }
@@ -22,7 +24,7 @@ internal sealed class GetAllEmailTemplatesHandler
protected override async Task<ServiceResult<IReadOnlyList<EmailTemplate>>> DoOnHandle( protected override async Task<ServiceResult<IReadOnlyList<EmailTemplate>>> DoOnHandle(
GetAllEmailTemplatesQuery request, CancellationToken cancellationToken) GetAllEmailTemplatesQuery request, CancellationToken cancellationToken)
{ {
var templates = await _templates.GetAllAsync(request.ServiceName, request.Key, cancellationToken); var templates = await _templates.GetAllAsync(cancellationToken);
return Success(templates); return Success(templates);
} }
} }
@@ -4,5 +4,4 @@ using HrynCo.NotificationService.Services.Core;
namespace HrynCo.NotificationService.Services.EmailTemplates.GetAll; namespace HrynCo.NotificationService.Services.EmailTemplates.GetAll;
public sealed record GetAllEmailTemplatesQuery(string? ServiceName = null, string? Key = null) public sealed record GetAllEmailTemplatesQuery : IRequest<ServiceResult<IReadOnlyList<EmailTemplate>>>;
: IRequest<ServiceResult<IReadOnlyList<EmailTemplate>>>;
@@ -1,3 +1,4 @@
using HrynCo.DAL.Abstract;
using HrynCo.NotificationService.DAL.Abstract.Repositories; using HrynCo.NotificationService.DAL.Abstract.Repositories;
using HrynCo.NotificationService.DAL.Abstract.Templates; using HrynCo.NotificationService.DAL.Abstract.Templates;
using HrynCo.NotificationService.Services.Core; using HrynCo.NotificationService.Services.Core;
@@ -13,8 +14,9 @@ internal sealed class GetEmailTemplatesHandler
public GetEmailTemplatesHandler( public GetEmailTemplatesHandler(
IContextualSerilogLogger<GetEmailTemplatesQuery> logger, IContextualSerilogLogger<GetEmailTemplatesQuery> logger,
IUnitOfWork unitOfWork,
IEmailTemplateRepository templates) IEmailTemplateRepository templates)
: base(logger) : base(logger, unitOfWork)
{ {
_templates = templates; _templates = templates;
} }
@@ -13,8 +13,9 @@ internal sealed class UpdateEmailTemplateHandler
public UpdateEmailTemplateHandler( public UpdateEmailTemplateHandler(
IContextualSerilogLogger<UpdateEmailTemplateCommand> logger, IContextualSerilogLogger<UpdateEmailTemplateCommand> logger,
IUnitOfWork unitOfWork,
IEmailTemplateRepository templates) IEmailTemplateRepository templates)
: base(logger) : base(logger, unitOfWork)
{ {
_templates = templates; _templates = templates;
} }
@@ -23,12 +23,9 @@ public class AdminTemplatesController : Controller
// GET /admin/templates // GET /admin/templates
[HttpGet("")] [HttpGet("")]
public async Task<IActionResult> Index([FromQuery] string? serviceName, [FromQuery] string? key, CancellationToken ct) public async Task<IActionResult> Index(CancellationToken ct)
{ {
ViewData["ServiceNameFilter"] = serviceName; var result = await _mediator.Send(new GetAllEmailTemplatesQuery(), ct);
ViewData["KeyFilter"] = key;
var result = await _mediator.Send(new GetAllEmailTemplatesQuery(serviceName, key), ct);
if (!result.IsSuccess) if (!result.IsSuccess)
{ {
ModelState.AddModelError("", result.Error?.Message ?? "Failed to load templates."); ModelState.AddModelError("", result.Error?.Message ?? "Failed to load templates.");
@@ -40,24 +37,14 @@ public class AdminTemplatesController : Controller
// GET /admin/templates/create // GET /admin/templates/create
[HttpGet("create")] [HttpGet("create")]
public IActionResult Create([FromQuery] string? serviceNameFilter, [FromQuery] string? keyFilter) public IActionResult Create()
{ {
return View("Edit", new EmailTemplateEditViewModel return View("Edit", new EmailTemplateEditViewModel());
{
ServiceNameFilter = serviceNameFilter,
KeyFilter = keyFilter
});
} }
// GET /admin/templates/{serviceName}/{key}/{languageCode} // GET /admin/templates/{serviceName}/{key}/{languageCode}
[HttpGet("{serviceName}/{key}/{languageCode}")] [HttpGet("{serviceName}/{key}/{languageCode}")]
public async Task<IActionResult> Edit( public async Task<IActionResult> Edit(string serviceName, string key, string languageCode, CancellationToken ct)
string serviceName,
string key,
string languageCode,
[FromQuery] string? serviceNameFilter,
[FromQuery] string? keyFilter,
CancellationToken ct)
{ {
var result = await _mediator.Send(new GetEmailTemplateQuery(serviceName, key, languageCode), ct); var result = await _mediator.Send(new GetEmailTemplateQuery(serviceName, key, languageCode), ct);
if (!result.IsSuccess || result.Result is null) if (!result.IsSuccess || result.Result is null)
@@ -73,9 +60,7 @@ public class AdminTemplatesController : Controller
Subject = template.Subject, Subject = template.Subject,
HtmlBody = template.HtmlBody, HtmlBody = template.HtmlBody,
TextBody = template.TextBody, TextBody = template.TextBody,
VariablesJson = JsonSerializer.Serialize(template.Variables), VariablesJson = JsonSerializer.Serialize(template.Variables)
ServiceNameFilter = serviceNameFilter,
KeyFilter = keyFilter
}; };
return View(vm); return View(vm);
@@ -139,21 +124,15 @@ public class AdminTemplatesController : Controller
} }
} }
return RedirectToAction(nameof(Index), new { serviceName = model.ServiceNameFilter, key = model.KeyFilter }); return RedirectToAction(nameof(Index));
} }
// POST /admin/templates/{serviceName}/{key}/{languageCode}/delete // POST /admin/templates/{serviceName}/{key}/{languageCode}/delete
[HttpPost("{serviceName}/{key}/{languageCode}/delete")] [HttpPost("{serviceName}/{key}/{languageCode}/delete")]
[ValidateAntiForgeryToken] [ValidateAntiForgeryToken]
public async Task<IActionResult> Delete( public async Task<IActionResult> Delete(string serviceName, string key, string languageCode, CancellationToken ct)
string serviceName,
string key,
string languageCode,
[FromForm] string? serviceNameFilter,
[FromForm] string? keyFilter,
CancellationToken ct)
{ {
await _mediator.Send(new DeleteEmailTemplateCommand(serviceName, key, languageCode), ct); await _mediator.Send(new DeleteEmailTemplateCommand(serviceName, key, languageCode), ct);
return RedirectToAction(nameof(Index), new { serviceName = serviceNameFilter, key = keyFilter }); return RedirectToAction(nameof(Index));
} }
} }
@@ -25,8 +25,6 @@ public class EmailTemplateEditViewModel
// JSON array: [{"name":"UserName","required":true}, ...] // JSON array: [{"name":"UserName","required":true}, ...]
public string VariablesJson { get; set; } = "[]"; public string VariablesJson { get; set; } = "[]";
public string? ServiceNameFilter { get; set; }
public string? KeyFilter { get; set; }
public bool IsNew => Id == null; public bool IsNew => Id == null;
public string PageTitle => IsNew ? "Create Email Template" : "Edit Email Template"; public string PageTitle => IsNew ? "Create Email Template" : "Edit Email Template";
@@ -1,7 +1,6 @@
using HrynCo.NotificationService.Web.Infrastructure; using HrynCo.NotificationService.Web.Infrastructure;
using HrynCo.NotificationService.Services.EmailTemplates.Create; using HrynCo.NotificationService.Services.EmailTemplates.Create;
using HrynCo.NotificationService.Services.EmailTemplates.Delete; using HrynCo.NotificationService.Services.EmailTemplates.Delete;
using HrynCo.NotificationService.Services.EmailTemplates.GetAll;
using HrynCo.NotificationService.Services.EmailTemplates.Get; using HrynCo.NotificationService.Services.EmailTemplates.Get;
using HrynCo.NotificationService.Services.EmailTemplates.GetByService; using HrynCo.NotificationService.Services.EmailTemplates.GetByService;
using HrynCo.NotificationService.Services.EmailTemplates.Update; using HrynCo.NotificationService.Services.EmailTemplates.Update;
@@ -16,9 +15,9 @@ public sealed class EmailTemplatesController : ApiControllerBase
public EmailTemplatesController(IMediator mediator) : base(mediator) { } public EmailTemplatesController(IMediator mediator) : base(mediator) { }
[HttpGet] [HttpGet]
public async Task<IActionResult> GetAll([FromQuery] string? serviceName, [FromQuery] string? key, CancellationToken cancellationToken) public async Task<IActionResult> GetAll([FromQuery] string serviceName, CancellationToken cancellationToken)
{ {
var result = await Mediator.Send(new GetAllEmailTemplatesQuery(serviceName, key), cancellationToken); var result = await Mediator.Send(new GetEmailTemplatesQuery(serviceName), cancellationToken);
return FromServiceResult(result); return FromServiceResult(result);
} }
@@ -1,47 +1,6 @@
@host = http://localhost:5188 @HrynCo.NotificationService.Api_HostAddress = http://localhost:5188
### Create a new email template GET {{HrynCo.NotificationService.Api_HostAddress}}/weatherforecast/
POST {{host}}/api/v1/email-templates Accept: application/json
Content-Type: application/json
{ ###
"ServiceName": "StoreMate-Prod",
"Key": "ShareInvite",
"LanguageCode": "uk",
"Subject": "Вас запрошено",
"HtmlBody": "<html><body><div style=\"font-family: Arial, sans-serif; color: #1f2937;\"><p>Вітаємо, \u007b\u007bRecipientName\u007d\u007d.</p><h1>Вас запрошено</h1><p>\u007b\u007bInviterName\u007d\u007d запросив вас приєднатися до \u007b\u007bAppName\u007d\u007d, щоб ви могли безпечно співпрацювати.</p><p><a href=\"\u007b\u007bInviteLink\u007d\u007d\">Відкрити запрошення</a></p><p>Запрошення дійсне до <strong>\u007b\u007bValidUntil\u007d\u007d</strong>.</p></div></body></html>",
"TextBody": "Вітаємо, \u007b\u007bRecipientName\u007d\u007d.\n\n\u007b\u007bInviterName\u007d\u007d запросив вас приєднатися до \u007b\u007bAppName\u007d\u007d, щоб ви могли безпечно співпрацювати.\n\nВідкрийте запрошення: \u007b\u007bInviteLink\u007d\u007d\nДійсне до: \u007b\u007bValidUntil\u007d\u007d",
"Variables": [
{ "Name": "RecipientName", "Required": false },
{ "Name": "InviterName", "Required": false },
{ "Name": "AppName", "Required": false },
{ "Name": "InviteLink", "Required": false },
{ "Name": "ValidUntil", "Required": false }
]
}
### Get the created template
GET {{host}}/api/v1/email-templates/StoreMate-Prod/ShareInvite/uk
### List all templates for the service
GET {{host}}/api/v1/email-templates?serviceName=StoreMate-Prod
### Update the template
PUT {{host}}/api/v1/email-templates/StoreMate-Prod/ShareInvite/uk
Content-Type: application/json
{
"Subject": "Вас запрошено",
"HtmlBody": "<html><body><p>Вітаємо, \u007b\u007bRecipientName\u007d\u007d.</p><p>\u007b\u007bInviterName\u007d\u007d запросив вас приєднатися до \u007b\u007bAppName\u007d\u007d.</p><p><a href=\"\u007b\u007bInviteLink\u007d\u007d\">Відкрити запрошення</a></p><p>Дійсне до <strong>\u007b\u007bValidUntil\u007d\u007d</strong>.</p></body></html>",
"TextBody": "Вітаємо, \u007b\u007bRecipientName\u007d\u007d.\n\n\u007b\u007bInviterName\u007d\u007d запросив вас приєднатися до \u007b\u007bAppName\u007d\u007d.\n\nВідкрийте запрошення: \u007b\u007bInviteLink\u007d\u007d\nДійсне до: \u007b\u007bValidUntil\u007d\u007d",
"Variables": [
{ "Name": "RecipientName", "Required": false },
{ "Name": "InviterName", "Required": false },
{ "Name": "AppName", "Required": false },
{ "Name": "InviteLink", "Required": false },
{ "Name": "ValidUntil", "Required": false }
]
}
### Delete the template
DELETE {{host}}/api/v1/email-templates/StoreMate-Prod/ShareInvite/uk
+9 -6
View File
@@ -3,11 +3,11 @@ using HrynCo.NotificationService.DAL.EF;
using HrynCo.NotificationService.Services; using HrynCo.NotificationService.Services;
using Scalar.AspNetCore; using Scalar.AspNetCore;
WebApplicationBuilder builder = WebApplication.CreateBuilder(args); var builder = WebApplication.CreateBuilder(args);
builder.AddSerilog(); builder.AddSerilog();
AppSettings appSettings = builder.Configuration var appSettings = builder.Configuration
.GetSection(AppSettings.SectionName) .GetSection(AppSettings.SectionName)
.Get<AppSettings>() ?? throw new InvalidOperationException("App settings are not configured."); .Get<AppSettings>() ?? throw new InvalidOperationException("App settings are not configured.");
@@ -18,14 +18,17 @@ builder.Services.AddControllersWithViews()
builder.Services.AddNotificationDataAccess(appSettings.ConnectionString); builder.Services.AddNotificationDataAccess(appSettings.ConnectionString);
builder.Services.AddNotificationServices(); builder.Services.AddNotificationServices();
WebApplication app = builder.Build(); var app = builder.Build();
app.MapOpenApi(); if (app.Environment.IsDevelopment())
app.MapScalarApiReference(options =>
{ {
app.MapOpenApi();
app.MapScalarApiReference(options =>
{
options.Title = "HrynCo Notification Service"; options.Title = "HrynCo Notification Service";
options.Theme = ScalarTheme.DeepSpace; options.Theme = ScalarTheme.DeepSpace;
}); });
}
app.UseStaticFiles(); app.UseStaticFiles();
app.UseHttpsRedirection(); app.UseHttpsRedirection();
@@ -10,8 +10,6 @@
@Html.AntiForgeryToken() @Html.AntiForgeryToken()
<input asp-for="Id" type="hidden" /> <input asp-for="Id" type="hidden" />
<input type="hidden" name="IsNew" value="@Model.IsNew" /> <input type="hidden" name="IsNew" value="@Model.IsNew" />
<input asp-for="ServiceNameFilter" type="hidden" />
<input asp-for="KeyFilter" type="hidden" />
@if (!ViewData.ModelState.IsValid) @if (!ViewData.ModelState.IsValid)
{ {
@@ -23,21 +21,6 @@
</div> </div>
} }
<ul class="nav nav-tabs template-editor-tabs mb-3" id="templateEditorTabs" role="tablist">
<li class="nav-item" role="presentation">
<button class="nav-link active" id="edit-tab" data-bs-toggle="tab" data-bs-target="#edit-pane" type="button" role="tab" aria-controls="edit-pane" aria-selected="true">
Edit
</button>
</li>
<li class="nav-item" role="presentation">
<button class="nav-link" id="preview-tab" data-bs-toggle="tab" data-bs-target="#preview-pane" type="button" role="tab" aria-controls="preview-pane" aria-selected="false">
Preview
</button>
</li>
</ul>
<div class="tab-content">
<div class="tab-pane fade show active" id="edit-pane" role="tabpanel" aria-labelledby="edit-tab" tabindex="0">
<div class="row g-3 mb-3"> <div class="row g-3 mb-3">
<div class="col-md-5"> <div class="col-md-5">
<label asp-for="ServiceName" class="form-label fw-semibold">Service Name</label> <label asp-for="ServiceName" class="form-label fw-semibold">Service Name</label>
@@ -80,9 +63,7 @@
<span asp-validation-for="VariablesJson" class="text-danger small"></span> <span asp-validation-for="VariablesJson" class="text-danger small"></span>
<div class="form-text">JSON array of <code>{"name":"...", "required":true|false}</code></div> <div class="form-text">JSON array of <code>{"name":"...", "required":true|false}</code></div>
</div> </div>
</div>
<div class="tab-pane fade" id="preview-pane" role="tabpanel" aria-labelledby="preview-tab" tabindex="0">
<div class="template-preview-panel mb-3"> <div class="template-preview-panel mb-3">
<div class="template-preview-panel-header"> <div class="template-preview-panel-header">
<div> <div>
@@ -92,8 +73,8 @@
<span id="previewStatus" class="badge text-bg-secondary">Ready</span> <span id="previewStatus" class="badge text-bg-secondary">Ready</span>
</div> </div>
<div class="template-preview-body"> <div class="template-preview-grid">
<div class="template-preview-source template-preview-section-block"> <div class="template-preview-source">
<div class="template-preview-section-title">Sample values</div> <div class="template-preview-section-title">Sample values</div>
<div id="previewVariables" class="template-preview-variables"></div> <div id="previewVariables" class="template-preview-variables"></div>
<div class="form-text mt-2">Change these values to see the rendered output update immediately.</div> <div class="form-text mt-2">Change these values to see the rendered output update immediately.</div>
@@ -111,14 +92,12 @@
</div> </div>
</div> </div>
</div> </div>
</div>
</div>
@section FormActions { @section FormActions {
<button type="submit" form="templateForm" class="btn btn-primary"> <button type="submit" form="templateForm" class="btn btn-primary">
<i class="bi bi-floppy me-1"></i> Save <i class="bi bi-floppy me-1"></i> Save
</button> </button>
<a href="/admin/templates@(string.IsNullOrWhiteSpace(Model.ServiceNameFilter) && string.IsNullOrWhiteSpace(Model.KeyFilter) ? string.Empty : $"?serviceName={Uri.EscapeDataString(Model.ServiceNameFilter ?? string.Empty)}&key={Uri.EscapeDataString(Model.KeyFilter ?? string.Empty)}")" class="btn btn-secondary"> <a href="/admin/templates" class="btn btn-secondary">
<i class="bi bi-x-lg me-1"></i> Cancel <i class="bi bi-x-lg me-1"></i> Cancel
</a> </a>
} }
@@ -135,7 +114,6 @@
const previewText = document.getElementById('previewText'); const previewText = document.getElementById('previewText');
const previewFrame = document.getElementById('previewHtmlFrame'); const previewFrame = document.getElementById('previewHtmlFrame');
const previewStatus = document.getElementById('previewStatus'); const previewStatus = document.getElementById('previewStatus');
const previewTab = document.getElementById('preview-tab');
if (!subjectField || !htmlField || !textField || !variablesField || !previewVariablesHost || !previewSubject || !previewText || !previewFrame || !previewStatus) { if (!subjectField || !htmlField || !textField || !variablesField || !previewVariablesHost || !previewSubject || !previewText || !previewFrame || !previewStatus) {
return; return;
@@ -289,9 +267,6 @@
subjectField.addEventListener('input', updatePreview); subjectField.addEventListener('input', updatePreview);
htmlField.addEventListener('input', updatePreview); htmlField.addEventListener('input', updatePreview);
textField.addEventListener('input', updatePreview); textField.addEventListener('input', updatePreview);
if (previewTab) {
previewTab.addEventListener('shown.bs.tab', updatePreview);
}
renderVariableInputs(); renderVariableInputs();
})(); })();
@@ -2,50 +2,15 @@
@model IReadOnlyList<EmailTemplate> @model IReadOnlyList<EmailTemplate>
@{ @{
ViewData["Title"] = "Email Templates"; ViewData["Title"] = "Email Templates";
var serviceNameFilter = ViewData["ServiceNameFilter"] as string ?? string.Empty;
var keyFilter = ViewData["KeyFilter"] as string ?? string.Empty;
var filterQuery = string.IsNullOrWhiteSpace(serviceNameFilter) && string.IsNullOrWhiteSpace(keyFilter)
? string.Empty
: $"?serviceNameFilter={Uri.EscapeDataString(serviceNameFilter)}&keyFilter={Uri.EscapeDataString(keyFilter)}";
var listQuery = string.IsNullOrWhiteSpace(serviceNameFilter) && string.IsNullOrWhiteSpace(keyFilter)
? string.Empty
: $"?serviceName={Uri.EscapeDataString(serviceNameFilter)}&key={Uri.EscapeDataString(keyFilter)}";
} }
<div class="page-header"> <div class="page-header">
<h2><i class="bi bi-envelope-paper"></i> Email Templates</h2> <h2><i class="bi bi-envelope-paper"></i> Email Templates</h2>
<a href="/admin/templates/create@filterQuery" class="btn btn-primary btn-sm"> <a href="/admin/templates/create" class="btn btn-primary btn-sm">
<i class="bi bi-plus-lg me-1"></i> Create New Template <i class="bi bi-plus-lg me-1"></i> Create New Template
</a> </a>
</div> </div>
<div class="card shadow-sm mb-3">
<div class="card-body">
<form id="templateFiltersForm" method="get" action="/admin/templates" class="row g-2 align-items-end">
<div class="col-12 col-md-5">
<label class="form-label fw-semibold" for="serviceName">Service Name</label>
<input id="serviceName"
name="serviceName"
value="@serviceNameFilter"
class="form-control"
placeholder="Filter by service name" />
</div>
<div class="col-12 col-md-5">
<label class="form-label fw-semibold" for="key">Key</label>
<input id="key"
name="key"
value="@keyFilter"
class="form-control"
placeholder="Filter by key" />
</div>
<div class="col-12 col-md-2 d-flex gap-2">
<button type="submit" class="btn btn-primary w-100">Filter</button>
<a id="clearTemplateFilters" href="/admin/templates" class="btn btn-outline-secondary w-100">Clear</a>
</div>
</form>
</div>
</div>
@if (!ViewData.ModelState.IsValid) @if (!ViewData.ModelState.IsValid)
{ {
<div class="alert alert-danger"> <div class="alert alert-danger">
@@ -56,61 +21,6 @@
</div> </div>
} }
<script>
(() => {
const storageKey = 'hrynco.notificationService.adminTemplates.filters';
const form = document.getElementById('templateFiltersForm');
const serviceNameInput = document.getElementById('serviceName');
const keyInput = document.getElementById('key');
const clearLink = document.getElementById('clearTemplateFilters');
if (!form || !serviceNameInput || !keyInput || !clearLink) {
return;
}
const saveState = () => {
const state = {
serviceName: serviceNameInput.value ?? '',
key: keyInput.value ?? ''
};
localStorage.setItem(storageKey, JSON.stringify(state));
};
const restoreState = () => {
const raw = localStorage.getItem(storageKey);
if (!raw) {
return false;
}
try {
const state = JSON.parse(raw);
const serviceName = typeof state.serviceName === 'string' ? state.serviceName : '';
const key = typeof state.key === 'string' ? state.key : '';
serviceNameInput.value = serviceName;
keyInput.value = key;
return serviceName.length > 0 || key.length > 0;
} catch {
localStorage.removeItem(storageKey);
return false;
}
};
form.addEventListener('submit', saveState);
clearLink.addEventListener('click', () => localStorage.removeItem(storageKey));
const hasQueryParams = new URLSearchParams(window.location.search).toString().length > 0;
if (!hasQueryParams && restoreState()) {
form.requestSubmit();
return;
}
saveState();
})();
</script>
@if (Model is null || Model.Count == 0) @if (Model is null || Model.Count == 0)
{ {
<div class="card shadow-sm table-card"> <div class="card shadow-sm table-card">
@@ -146,15 +56,13 @@ else
<td>@t.LanguageCode</td> <td>@t.LanguageCode</td>
<td>@t.Subject</td> <td>@t.Subject</td>
<td class="text-end"> <td class="text-end">
<a href="/admin/templates/@t.ServiceName/@t.Key/@t.LanguageCode@filterQuery" <a href="/admin/templates/@t.ServiceName/@t.Key/@t.LanguageCode"
class="btn btn-sm btn-outline-primary me-1"> class="btn btn-sm btn-outline-primary me-1">
<i class="bi bi-pencil"></i> Edit <i class="bi bi-pencil"></i> Edit
</a> </a>
<form method="post" <form method="post"
action="/admin/templates/@t.ServiceName/@t.Key/@t.LanguageCode/delete" action="/admin/templates/@t.ServiceName/@t.Key/@t.LanguageCode/delete"
class="d-inline"> class="d-inline">
<input type="hidden" name="serviceNameFilter" value="@serviceNameFilter" />
<input type="hidden" name="keyFilter" value="@keyFilter" />
@Html.AntiForgeryToken() @Html.AntiForgeryToken()
<button type="submit" <button type="submit"
class="btn btn-sm btn-outline-danger" class="btn btn-sm btn-outline-danger"
@@ -118,10 +118,9 @@ body {
.empty-state .bi { font-size: 2.5rem; opacity: .35; display: block; margin-bottom: .75rem; } .empty-state .bi { font-size: 2.5rem; opacity: .35; display: block; margin-bottom: .75rem; }
.empty-state p { font-size: .9rem; margin-bottom: 0; } .empty-state p { font-size: .9rem; margin-bottom: 0; }
/* ── Editor wrapper ──────────────────────────────────── */ /* ── Editor wrapper — constrains width ───────────────── */
.editor-wrapper { .editor-wrapper {
width: 100%; max-width: 860px;
max-width: none;
} }
/* ── Editor card ──────────────────────────────────────── */ /* ── Editor card ──────────────────────────────────────── */
@@ -155,26 +154,6 @@ body {
border-radius: 0 0 .5rem .5rem !important; border-radius: 0 0 .5rem .5rem !important;
} }
/* ── Editor tabs ─────────────────────────────────────── */
.template-editor-tabs {
border-bottom-color: #dce3eb;
}
.template-editor-tabs .nav-link {
color: #526072;
font-weight: 600;
border-radius: .5rem .5rem 0 0;
}
.template-editor-tabs .nav-link.active {
color: #0d6efd;
}
/* ── Tab content ──────────────────────────────────────── */
.tab-content {
min-width: 0;
}
/* ── Email template preview ───────────────────────────── */ /* ── Email template preview ───────────────────────────── */
.template-preview-panel { .template-preview-panel {
border: 1px solid #dce3eb; border: 1px solid #dce3eb;
@@ -206,9 +185,9 @@ body {
margin-top: .15rem; margin-top: .15rem;
} }
.template-preview-body { .template-preview-grid {
display: grid; display: grid;
grid-template-columns: minmax(0, 1fr); grid-template-columns: minmax(240px, 300px) minmax(0, 1fr);
gap: 1rem; gap: 1rem;
padding: 1rem 1.25rem 1.25rem; padding: 1rem 1.25rem 1.25rem;
} }
@@ -218,13 +197,6 @@ body {
min-width: 0; min-width: 0;
} }
.template-preview-section-block {
padding: 1rem;
border: 1px solid #e5ebf2;
border-radius: .65rem;
background: #fafcff;
}
.template-preview-section-title { .template-preview-section-title {
font-size: .7rem; font-size: .7rem;
font-weight: 700; font-weight: 700;
@@ -235,26 +207,13 @@ body {
} }
.template-preview-variables { .template-preview-variables {
display: grid; display: flex;
grid-template-columns: repeat(auto-fit, minmax(220px, 1fr)); flex-direction: column;
gap: .75rem 1rem; gap: .75rem;
max-width: none;
}
.template-preview-variables > div {
margin-bottom: 0 !important;
}
.template-preview-variables .form-label {
font-size: .72rem;
margin-bottom: .2rem;
} }
.template-preview-variables .form-control-sm { .template-preview-variables .form-control-sm {
background: #fff; background: #fff;
min-height: calc(1.5em + .45rem + 2px);
padding: .2rem .45rem;
font-size: .82rem;
} }
.template-preview-subject { .template-preview-subject {
@@ -289,7 +248,7 @@ body {
} }
@media (max-width: 992px) { @media (max-width: 992px) {
.template-preview-body { .template-preview-grid {
grid-template-columns: 1fr; grid-template-columns: 1fr;
} }
@@ -1,24 +0,0 @@
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();
}
}
@@ -1,31 +0,0 @@
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}'.");
}
}
@@ -1,9 +0,0 @@
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);
}
@@ -1,12 +0,0 @@
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);
}
@@ -1,8 +0,0 @@
using HrynCo.NotificationService.Contracts.Messages;
namespace HrynCo.NotificationService.Worker.Services.EmailProcessing;
public interface ISendEmailService
{
Task ProcessAsync(SendEmailMessage message, CancellationToken cancellationToken);
}
@@ -1,198 +0,0 @@
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);
}
}
}
@@ -1,21 +0,0 @@
<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>
@@ -1,17 +0,0 @@
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;
}
}
+1 -1
View File
@@ -11,7 +11,7 @@ COPY ["Directory.Build.props", "."]
COPY ["Directory.Packages.props", "."] COPY ["Directory.Packages.props", "."]
COPY ["HrynCo.NotificationService.DAL.Abstract/HrynCo.NotificationService.DAL.Abstract.csproj", "HrynCo.NotificationService.DAL.Abstract/"] COPY ["HrynCo.NotificationService.DAL.Abstract/HrynCo.NotificationService.DAL.Abstract.csproj", "HrynCo.NotificationService.DAL.Abstract/"]
COPY ["HrynCo.NotificationService.DAL.EF/HrynCo.NotificationService.DAL.EF.csproj", "HrynCo.NotificationService.DAL.EF/"] COPY ["HrynCo.NotificationService.DAL.EF/HrynCo.NotificationService.DAL.EF.csproj", "HrynCo.NotificationService.DAL.EF/"]
COPY ["HrynCo.NotificationService.Worker.Services/HrynCo.NotificationService.Worker.Services.csproj", "HrynCo.NotificationService.Worker.Services/"] COPY ["HrynCo.NotificationService.Services/HrynCo.NotificationService.Services.csproj", "HrynCo.NotificationService.Services/"]
COPY ["HrynCo.NotificationService.Worker/HrynCo.NotificationService.Worker.csproj", "HrynCo.NotificationService.Worker/"] COPY ["HrynCo.NotificationService.Worker/HrynCo.NotificationService.Worker.csproj", "HrynCo.NotificationService.Worker/"]
RUN dotnet restore "HrynCo.NotificationService.Worker/HrynCo.NotificationService.Worker.csproj" RUN dotnet restore "HrynCo.NotificationService.Worker/HrynCo.NotificationService.Worker.csproj"
@@ -10,6 +10,7 @@
<ItemGroup> <ItemGroup>
<PackageReference Include="Microsoft.Extensions.Hosting" /> <PackageReference Include="Microsoft.Extensions.Hosting" />
<PackageReference Include="HrynCo.RabbitMq" /> <PackageReference Include="HrynCo.RabbitMq" />
<PackageReference Include="MediatR" />
<PackageReference Include="Serilog.Extensions.Hosting" /> <PackageReference Include="Serilog.Extensions.Hosting" />
<PackageReference Include="Serilog.Settings.Configuration" /> <PackageReference Include="Serilog.Settings.Configuration" />
<PackageReference Include="Serilog.Sinks.Console" /> <PackageReference Include="Serilog.Sinks.Console" />
@@ -18,7 +19,7 @@
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\HrynCo.NotificationService.Contracts\HrynCo.NotificationService.Contracts.csproj" /> <ProjectReference Include="..\HrynCo.NotificationService.Contracts\HrynCo.NotificationService.Contracts.csproj" />
<ProjectReference Include="..\HrynCo.NotificationService.Services\HrynCo.NotificationService.Services.csproj" />
<ProjectReference Include="..\HrynCo.NotificationService.DAL.EF\HrynCo.NotificationService.DAL.EF.csproj" /> <ProjectReference Include="..\HrynCo.NotificationService.DAL.EF\HrynCo.NotificationService.DAL.EF.csproj" />
<ProjectReference Include="..\HrynCo.NotificationService.Worker.Services\HrynCo.NotificationService.Worker.Services.csproj" />
</ItemGroup> </ItemGroup>
</Project> </Project>
+3 -2
View File
@@ -1,7 +1,8 @@
using HrynCo.NotificationService.DAL.EF; using HrynCo.NotificationService.DAL.EF;
using HrynCo.NotificationService.Services;
using HrynCo.NotificationService.Worker; using HrynCo.NotificationService.Worker;
using HrynCo.NotificationService.Worker.Services;
using Hrynco.RabbitMq; using Hrynco.RabbitMq;
using Microsoft.Extensions.Options;
var builder = Host.CreateApplicationBuilder(args); var builder = Host.CreateApplicationBuilder(args);
@@ -13,7 +14,7 @@ var appSettings = builder.Configuration
builder.Services.AddSingleton(appSettings); builder.Services.AddSingleton(appSettings);
builder.Services.AddNotificationDataAccess(appSettings.ConnectionString); builder.Services.AddNotificationDataAccess(appSettings.ConnectionString);
builder.Services.AddNotificationWorkerServices(); builder.Services.AddNotificationServices();
builder.Services.Configure<RabbitMqSettings>( builder.Services.Configure<RabbitMqSettings>(
builder.Configuration.GetSection($"{AppSettings.SectionName}:RabbitMq")); builder.Configuration.GetSection($"{AppSettings.SectionName}:RabbitMq"));
@@ -1,3 +1,3 @@
namespace HrynCo.NotificationService.Worker.Services.EmailProcessing; namespace HrynCo.NotificationService.Worker;
public record RenderedEmail(string Subject, string HtmlBody, string TextBody); public record RenderedEmail(string Subject, string HtmlBody, string TextBody);
@@ -1,32 +1,174 @@
namespace HrynCo.NotificationService.Worker; namespace HrynCo.NotificationService.Worker;
using System.Text;
using System.Text.Json;
using HrynCo.NotificationService.Contracts.Messages; 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.NotificationService.Services.EmailChannels.Send;
using Hrynco.RabbitMq; using Hrynco.RabbitMq;
using Microsoft.Extensions.DependencyInjection; using MediatR;
using Microsoft.Extensions.Options; using Microsoft.Extensions.Options;
using HrynCo.NotificationService.Worker.Services.EmailProcessing; using RabbitMQ.Client;
public sealed class SendEmailConsumer : RabbitMqConsumerBase<SendEmailMessage, SendEmailMessageData> public sealed class SendEmailConsumer(
{
private readonly IServiceScopeFactory _scopeFactory;
public SendEmailConsumer(
IOptionsMonitor<RabbitMqSettings> options, IOptionsMonitor<RabbitMqSettings> options,
IServiceScopeFactory scopeFactory, IEmailChannelRepository channelRepository,
IEmailTemplateRepository templateRepository,
IEmailChannelUsageRepository usageRepository,
IMediator mediator,
AppSettings appSettings,
ILogger<SendEmailConsumer> logger) ILogger<SendEmailConsumer> logger)
: base(options, logger) : RabbitMqConsumerBase<SendEmailMessage, SendEmailMessageData>(options, logger)
{ {
_scopeFactory = scopeFactory;
}
private const string IncomingQueue = "notification.send-email"; private const string IncomingQueue = "notification.send-email";
protected override string QueueName => IncomingQueue; protected override string QueueName => IncomingQueue;
protected override async Task HandleMessageAsync(SendEmailMessage message, CancellationToken cancellationToken) protected override async Task HandleMessageAsync(SendEmailMessage message, CancellationToken cancellationToken)
{ {
using var scope = _scopeFactory.CreateScope(); var data = message.Data;
var service = scope.ServiceProvider.GetRequiredService<ISendEmailService>();
await service.ProcessAsync(message, cancellationToken); logger.LogInformation(
"Processing SendEmail for service={Service} template={Template} recipient={Recipient} [CorrelationId={CorrelationId}]",
data.ServiceName, data.TemplateKey, data.RecipientEmail, message.CorrelationContext?.CorrelationId);
var channel = await ResolveChannelAsync(data.ServiceName, cancellationToken);
var template = await ResolveTemplateAsync(data.ServiceName, data.TemplateKey, data.LanguageCode, cancellationToken);
await EnforceLimitsAsync(channel, cancellationToken);
var rendered = RenderTemplate(template, data);
var sendResult = await mediator.Send(
new SendEmailCommand(channel.Id, data.RecipientEmail, data.RecipientName,
rendered.Subject, rendered.HtmlBody, rendered.TextBody),
cancellationToken);
if (!sendResult.IsSuccess)
throw new InvalidOperationException(sendResult.Error?.Message ?? "Send failed.");
logger.LogInformation(
"Email sent successfully service={Service} template={Template} recipient={Recipient}",
data.ServiceName, data.TemplateKey, data.RecipientEmail);
await PublishResultAsync(message.CorrelationContext, data, errorMessage: null, 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<EmailTemplate> ResolveTemplateAsync(
string serviceName, string templateKey, string? languageCode, CancellationToken ct)
{
var lang = string.IsNullOrWhiteSpace(languageCode) ? "en" : languageCode;
var template = await templateRepository.GetAsync(serviceName, templateKey, lang, ct);
if (template is null && lang != "en")
template = await templateRepository.GetAsync(serviceName, templateKey, "en", ct);
return template
?? throw new InvalidOperationException(
$"Template not found: service='{serviceName}' key='{templateKey}' language='{lang}'.");
}
private async Task EnforceLimitsAsync(EmailChannel channel, CancellationToken ct)
{
var today = DateOnly.FromDateTime(DateTime.UtcNow);
if (channel.DailyLimit.HasValue)
{
var 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)
{
var 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 static RenderedEmail RenderTemplate(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();
}
private async Task PublishResultAsync(
CorrelationContext? correlationContext,
SendEmailMessageData data,
string? errorMessage,
CancellationToken ct)
{
var 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
}
};
byte[] body = Encoding.UTF8.GetBytes(JsonSerializer.Serialize(result));
var factory = new ConnectionFactory
{
HostName = appSettings.RabbitMq.Host,
Port = appSettings.RabbitMq.Port,
UserName = appSettings.RabbitMq.User,
Password = appSettings.RabbitMq.Password,
VirtualHost = appSettings.RabbitMq.VirtualHost
};
await using var conn = await factory.CreateConnectionAsync(ct);
await using var ch = await conn.CreateChannelAsync(cancellationToken: ct);
await ch.QueueDeclareAsync(replyTo, durable: true, exclusive: false, autoDelete: false,
cancellationToken: ct);
await ch.BasicPublishAsync(exchange: string.Empty, routingKey: replyTo, body: body,
cancellationToken: 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);
}
} }
} }
@@ -1,14 +1,4 @@
{ {
"App": {
"ConnectionString": "Host=192.168.2.121;Port=55435;Database=hrynco_ns_prod;Username=ns_user;Password=HAwS0c4A1QmH",
"RabbitMq": {
"Host": "192.168.2.121",
"Port": 5675,
"User": "ns_user",
"Password": "LN22mEWYdfCy",
"VirtualHost": "/"
}
},
"Serilog": { "Serilog": {
"MinimumLevel": { "MinimumLevel": {
"Default": "Debug", "Default": "Debug",
@@ -17,20 +7,6 @@
"Microsoft.EntityFrameworkCore": "Information", "Microsoft.EntityFrameworkCore": "Information",
"Microsoft.AspNetCore": "Information" "Microsoft.AspNetCore": "Information"
} }
},
"WriteTo": [
{ "Name": "Console" },
{
"Name": "Seq",
"Args": {
"serverUrl": "http://192.168.2.121:5341"
}
}
],
"Enrich": [ "FromLogContext" ],
"Properties": {
"Application": "hrynco-notification-service-worker",
"Environment": "Development"
} }
} }
} }
-1
View File
@@ -20,6 +20,5 @@
<Project Path="HrynCo.NotificationService.DAL.EF/HrynCo.NotificationService.DAL.EF.csproj" /> <Project Path="HrynCo.NotificationService.DAL.EF/HrynCo.NotificationService.DAL.EF.csproj" />
<Project Path="HrynCo.NotificationService.Services.Tests/HrynCo.NotificationService.Services.Tests.csproj" /> <Project Path="HrynCo.NotificationService.Services.Tests/HrynCo.NotificationService.Services.Tests.csproj" />
<Project Path="HrynCo.NotificationService.Services/HrynCo.NotificationService.Services.csproj" /> <Project Path="HrynCo.NotificationService.Services/HrynCo.NotificationService.Services.csproj" />
<Project Path="HrynCo.NotificationService.Worker.Services/HrynCo.NotificationService.Worker.Services.csproj" />
<Project Path="HrynCo.NotificationService.Worker/HrynCo.NotificationService.Worker.csproj" /> <Project Path="HrynCo.NotificationService.Worker/HrynCo.NotificationService.Worker.csproj" />
</Solution> </Solution>
+3
View File
@@ -1,5 +1,8 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<configuration> <configuration>
<config>
<add key="globalPackagesFolder" value="%USERPROFILE%\.nuget\packages" />
</config>
<packageSources> <packageSources>
<add key="nuget.org" value="https://api.nuget.org/v3/index.json" protocolVersion="3" /> <add key="nuget.org" value="https://api.nuget.org/v3/index.json" protocolVersion="3" />
</packageSources> </packageSources>
-16
View File
@@ -1,17 +1 @@
# hrynco-notification-service # hrynco-notification-service
## Notification worker flow
```mermaid
flowchart TD
A[Worker host starts] --> B[Load config and register services]
B --> C[Start SendEmailConsumer]
C --> D[Receive message from notification.send-email]
D --> E[Resolve SendEmailService]
E --> F[Pick channel and template]
F --> G[Render email content]
G --> H[Send via SMTP]
H --> I[Update usage counters]
I --> J[Optionally publish result to reply queue]
H -. failure .-> K[Log and rethrow]
```