Compare commits
40 Commits
IT-628
...
e7d3953747
| Author | SHA1 | Date | |
|---|---|---|---|
| e7d3953747 | |||
| 3381fcc2f8 | |||
| 3bd952ef9e | |||
| 285cc6abb7 | |||
| 9589095760 | |||
| 25fb48ccf0 | |||
| ab042c7617 | |||
| 0861e18cec | |||
| dfa097eb43 | |||
| c18f0b7fb1 | |||
| 50828d23ec | |||
| f3966705ad | |||
| b4d8497ea7 | |||
| 7b77062aa7 | |||
| 9490718c04 | |||
| 0d1aa4f6be | |||
| 637af06a9c | |||
| 334bf2a567 | |||
| ee4c988a0d | |||
| 9c2edd4712 | |||
| 49982fc27f | |||
| 5c7b5f7b10 | |||
| 6302a07178 | |||
| b07cd06477 | |||
| 3e1cc696c1 | |||
| 9a0aaf629b | |||
| d71c3513a5 | |||
| 859ae0b50d | |||
| c5528b253d | |||
| 09c3985fad | |||
| 166b1a6103 | |||
| 8dab3c0dc0 | |||
| c88511ce3b | |||
| a6f9a0a530 | |||
| ae119d1a3d | |||
| c303514414 | |||
| 74211f0a4a | |||
| 18f7981ccc | |||
| 5003ab8764 | |||
| 4a431ec6c6 |
@@ -1,5 +1 @@
|
|||||||
<Project>
|
<Project />
|
||||||
<PropertyGroup>
|
|
||||||
<ManagePackageVersionsCentrally>true</ManagePackageVersionsCentrally>
|
|
||||||
</PropertyGroup>
|
|
||||||
</Project>
|
|
||||||
|
|||||||
+41
-43
@@ -1,44 +1,42 @@
|
|||||||
<Project>
|
<Project>
|
||||||
|
<PropertyGroup>
|
||||||
<ItemGroup>
|
<ManagePackageVersionsCentrally>true</ManagePackageVersionsCentrally>
|
||||||
<!-- Entity Framework Core -->
|
</PropertyGroup>
|
||||||
<PackageVersion Include="Microsoft.EntityFrameworkCore" Version="9.0.5" />
|
<ItemGroup>
|
||||||
<PackageVersion Include="Microsoft.EntityFrameworkCore.Design" Version="9.0.5" />
|
<!-- Entity Framework Core -->
|
||||||
<PackageVersion Include="Microsoft.EntityFrameworkCore.Relational" Version="9.0.5" />
|
<PackageVersion Include="HrynCo.DAL.Abstract" Version="1.0.11" />
|
||||||
<PackageVersion Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="9.0.4" />
|
<PackageVersion Include="HrynCo.DAL.EF" Version="1.0.11" />
|
||||||
|
<PackageVersion Include="Microsoft.EntityFrameworkCore" Version="10.0.7" />
|
||||||
<!-- MediatR -->
|
<PackageVersion Include="Microsoft.EntityFrameworkCore.Design" Version="10.0.7" />
|
||||||
<PackageVersion Include="MediatR" Version="12.4.1" />
|
<PackageVersion Include="Microsoft.EntityFrameworkCore.Relational" Version="10.0.7" />
|
||||||
<PackageVersion Include="MediatR.Extensions.Microsoft.DependencyInjection" Version="11.1.0" />
|
<PackageVersion Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="10.0.1" />
|
||||||
|
<!-- MediatR -->
|
||||||
<!-- Microsoft.Extensions -->
|
<PackageVersion Include="MediatR" Version="12.4.1" />
|
||||||
<PackageVersion Include="Microsoft.Extensions.Hosting" Version="10.0.6" />
|
<PackageVersion Include="MediatR.Extensions.Microsoft.DependencyInjection" Version="11.1.0" />
|
||||||
<PackageVersion Include="Microsoft.Extensions.Hosting.Abstractions" Version="10.0.6" />
|
<!-- Microsoft.Extensions -->
|
||||||
<PackageVersion Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="10.0.6" />
|
<PackageVersion Include="Microsoft.Extensions.Hosting" Version="10.0.6" />
|
||||||
<PackageVersion Include="Microsoft.Extensions.Logging.Abstractions" Version="10.0.6" />
|
<PackageVersion Include="Microsoft.Extensions.Hosting.Abstractions" Version="10.0.6" />
|
||||||
<PackageVersion Include="Scalar.AspNetCore" Version="2.14.9" />
|
<PackageVersion Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="10.0.6" />
|
||||||
<PackageVersion Include="Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation" Version="10.0.6" />
|
<PackageVersion Include="Microsoft.Extensions.Logging.Abstractions" Version="10.0.6" />
|
||||||
<PackageVersion Include="Microsoft.AspNetCore.OpenApi" Version="10.0.6" />
|
<PackageVersion Include="Scalar.AspNetCore" Version="2.14.9" />
|
||||||
|
<PackageVersion Include="Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation" Version="10.0.6" />
|
||||||
<!-- Serilog -->
|
<PackageVersion Include="Microsoft.AspNetCore.OpenApi" Version="10.0.6" />
|
||||||
<PackageVersion Include="Serilog" Version="4.2.0" />
|
<!-- Serilog -->
|
||||||
<PackageVersion Include="Serilog.AspNetCore" Version="9.0.0" />
|
<PackageVersion Include="Serilog" Version="4.2.0" />
|
||||||
<PackageVersion Include="Serilog.Extensions.Hosting" Version="9.0.0" />
|
<PackageVersion Include="Serilog.AspNetCore" Version="9.0.0" />
|
||||||
<PackageVersion Include="Serilog.Settings.Configuration" Version="9.0.0" />
|
<PackageVersion Include="Serilog.Extensions.Hosting" Version="9.0.0" />
|
||||||
<PackageVersion Include="Serilog.Sinks.Console" Version="6.0.0" />
|
<PackageVersion Include="Serilog.Settings.Configuration" Version="9.0.0" />
|
||||||
<PackageVersion Include="Serilog.Sinks.Seq" Version="9.0.0" />
|
<PackageVersion Include="Serilog.Sinks.Console" Version="6.0.0" />
|
||||||
|
<PackageVersion Include="Serilog.Sinks.Seq" Version="9.0.0" />
|
||||||
<!-- HrynCo shared packages -->
|
<!-- HrynCo shared packages -->
|
||||||
<PackageVersion Include="HrynCo.Common" Version="1.0.0" />
|
<PackageVersion Include="HrynCo.Common" Version="1.0.11" />
|
||||||
<PackageVersion Include="HrynCo.RabbitMq" Version="1.0.14" />
|
<PackageVersion Include="HrynCo.RabbitMq" Version="1.0.15" />
|
||||||
|
<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" />
|
<PackageVersion Include="xunit.runner.visualstudio" Version="3.1.4" />
|
||||||
<PackageVersion Include="xunit.runner.visualstudio" Version="3.1.4" />
|
<PackageVersion Include="NSubstitute" Version="5.3.0" />
|
||||||
<PackageVersion Include="NSubstitute" Version="5.3.0" />
|
<PackageVersion Include="Testcontainers.PostgreSql" Version="4.6.0" />
|
||||||
<PackageVersion Include="Testcontainers.PostgreSql" Version="4.6.0" />
|
<PackageVersion Include="Microsoft.AspNetCore.Mvc.Testing" Version="9.0.5" />
|
||||||
<PackageVersion Include="Microsoft.AspNetCore.Mvc.Testing" Version="9.0.5" />
|
</ItemGroup>
|
||||||
</ItemGroup>
|
</Project>
|
||||||
|
|
||||||
</Project>
|
|
||||||
@@ -4,6 +4,12 @@
|
|||||||
<TargetFramework>net10.0</TargetFramework>
|
<TargetFramework>net10.0</TargetFramework>
|
||||||
<ImplicitUsings>enable</ImplicitUsings>
|
<ImplicitUsings>enable</ImplicitUsings>
|
||||||
<Nullable>enable</Nullable>
|
<Nullable>enable</Nullable>
|
||||||
|
<PackageId>HrynCo.NotificationService.Contracts</PackageId>
|
||||||
|
<Authors>HrynCo</Authors>
|
||||||
|
<Description>RabbitMQ message contracts for HrynCo.NotificationService.</Description>
|
||||||
|
<PackageTags>hrynco notification email rabbitmq contracts</PackageTags>
|
||||||
|
<RepositoryType>git</RepositoryType>
|
||||||
|
<RepositoryUrl>https://gitea.grynco.com.ua/hrynco/hrynco-notification-service.git</RepositoryUrl>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
|||||||
@@ -1,17 +0,0 @@
|
|||||||
namespace HrynCo.NotificationService.DAL.Abstract.Entities;
|
|
||||||
|
|
||||||
public abstract class Entity<TId> : IEntity<TId> where TId : struct
|
|
||||||
{
|
|
||||||
public TId Id { get; set; }
|
|
||||||
public DateTimeOffset Created { get; set; }
|
|
||||||
public DateTimeOffset? Updated { get; set; }
|
|
||||||
}
|
|
||||||
|
|
||||||
public abstract class Entity : Entity<Guid>
|
|
||||||
{
|
|
||||||
protected Entity()
|
|
||||||
{
|
|
||||||
Id = Guid.NewGuid();
|
|
||||||
Created = DateTimeOffset.UtcNow;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,8 +0,0 @@
|
|||||||
namespace HrynCo.NotificationService.DAL.Abstract.Entities;
|
|
||||||
|
|
||||||
public interface IEntity<TId> where TId : struct
|
|
||||||
{
|
|
||||||
TId Id { get; set; }
|
|
||||||
DateTimeOffset Created { get; set; }
|
|
||||||
DateTimeOffset? Updated { get; set; }
|
|
||||||
}
|
|
||||||
@@ -6,4 +6,8 @@
|
|||||||
<Nullable>enable</Nullable>
|
<Nullable>enable</Nullable>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<PackageReference Include="HrynCo.DAL.Abstract" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
</Project>
|
</Project>
|
||||||
|
|||||||
@@ -1,7 +0,0 @@
|
|||||||
namespace HrynCo.NotificationService.DAL.Abstract;
|
|
||||||
|
|
||||||
public interface ITransaction : IAsyncDisposable
|
|
||||||
{
|
|
||||||
Task CommitAsync(CancellationToken cancellationToken = default);
|
|
||||||
Task RollbackAsync(CancellationToken cancellationToken = default);
|
|
||||||
}
|
|
||||||
@@ -1,11 +0,0 @@
|
|||||||
namespace HrynCo.NotificationService.DAL.Abstract;
|
|
||||||
|
|
||||||
public interface IUnitOfWork
|
|
||||||
{
|
|
||||||
Task SaveChangesAsync(CancellationToken cancellationToken = default);
|
|
||||||
Task<ITransaction> BeginTransactionAsync(CancellationToken cancellationToken = default);
|
|
||||||
ITransaction? GetCurrentTransaction();
|
|
||||||
|
|
||||||
Task ExecuteInTransactionAsync(Func<Task> action);
|
|
||||||
Task<TResult> ExecuteInTransactionAsync<TResult>(Func<Task<TResult>> action);
|
|
||||||
}
|
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
using HrynCo.NotificationService.DAL.Abstract.Entities;
|
using HrynCo.DAL.Abstract.Entities;
|
||||||
|
|
||||||
namespace HrynCo.NotificationService.DAL.Abstract.Providers;
|
namespace HrynCo.NotificationService.DAL.Abstract.Providers;
|
||||||
|
|
||||||
@@ -12,4 +12,4 @@ public class EmailChannel : Entity
|
|||||||
public int? MonthlyLimit { get; set; }
|
public int? MonthlyLimit { get; set; }
|
||||||
public int WarnThresholdPercent { get; set; } = 90;
|
public int WarnThresholdPercent { get; set; } = 90;
|
||||||
public bool IsActive { get; set; } = true;
|
public bool IsActive { get; set; } = true;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,13 +1,14 @@
|
|||||||
using HrynCo.NotificationService.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 : Entity
|
public class EmailChannelUsage
|
||||||
{
|
{
|
||||||
|
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; }
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
using HrynCo.NotificationService.DAL.Abstract.Entities;
|
using HrynCo.DAL.Abstract.Entities;
|
||||||
|
|
||||||
namespace HrynCo.NotificationService.DAL.Abstract.Templates;
|
namespace HrynCo.NotificationService.DAL.Abstract.Templates;
|
||||||
|
|
||||||
|
|||||||
@@ -1,45 +0,0 @@
|
|||||||
using System.Linq.Expressions;
|
|
||||||
using Microsoft.EntityFrameworkCore;
|
|
||||||
|
|
||||||
namespace HrynCo.NotificationService.DAL.EF.Core;
|
|
||||||
|
|
||||||
internal abstract class EfRepository<TEntity>
|
|
||||||
where TEntity : class
|
|
||||||
{
|
|
||||||
protected NotificationDbContext DbContext { get; }
|
|
||||||
protected DbSet<TEntity> DbSet { get; }
|
|
||||||
|
|
||||||
protected EfRepository(NotificationDbContext dbContext)
|
|
||||||
{
|
|
||||||
DbContext = dbContext;
|
|
||||||
DbSet = dbContext.Set<TEntity>();
|
|
||||||
}
|
|
||||||
|
|
||||||
protected async Task AddAsync(TEntity entity, CancellationToken ct = default)
|
|
||||||
{
|
|
||||||
await DbSet.AddAsync(entity, ct);
|
|
||||||
}
|
|
||||||
|
|
||||||
protected async Task AddRangeAsync(IEnumerable<TEntity> entities, CancellationToken ct = default)
|
|
||||||
{
|
|
||||||
await DbSet.AddRangeAsync(entities, ct);
|
|
||||||
}
|
|
||||||
|
|
||||||
protected void Update(TEntity entity)
|
|
||||||
{
|
|
||||||
DbSet.Update(entity);
|
|
||||||
}
|
|
||||||
|
|
||||||
protected void Delete(TEntity entity)
|
|
||||||
{
|
|
||||||
DbSet.Remove(entity);
|
|
||||||
}
|
|
||||||
|
|
||||||
protected void DeleteRange(IEnumerable<TEntity> entities)
|
|
||||||
{
|
|
||||||
DbSet.RemoveRange(entities);
|
|
||||||
}
|
|
||||||
|
|
||||||
protected Task<bool> ExistsAsync(Expression<Func<TEntity, bool>> predicate, CancellationToken ct = default) =>
|
|
||||||
DbSet.AnyAsync(predicate, ct);
|
|
||||||
}
|
|
||||||
@@ -1,29 +0,0 @@
|
|||||||
using HrynCo.NotificationService.DAL.Abstract;
|
|
||||||
using Microsoft.EntityFrameworkCore.Storage;
|
|
||||||
|
|
||||||
namespace HrynCo.NotificationService.DAL.EF.Core;
|
|
||||||
|
|
||||||
internal sealed class EfTransactionAdapter : ITransaction
|
|
||||||
{
|
|
||||||
private readonly IDbContextTransaction _transaction;
|
|
||||||
|
|
||||||
internal EfTransactionAdapter(IDbContextTransaction transaction)
|
|
||||||
{
|
|
||||||
_transaction = transaction;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Task CommitAsync(CancellationToken cancellationToken = default)
|
|
||||||
{
|
|
||||||
return _transaction.CommitAsync(cancellationToken);
|
|
||||||
}
|
|
||||||
|
|
||||||
public Task RollbackAsync(CancellationToken cancellationToken = default)
|
|
||||||
{
|
|
||||||
return _transaction.RollbackAsync(cancellationToken);
|
|
||||||
}
|
|
||||||
|
|
||||||
public ValueTask DisposeAsync()
|
|
||||||
{
|
|
||||||
return _transaction.DisposeAsync();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,105 +0,0 @@
|
|||||||
using HrynCo.NotificationService.DAL.Abstract;
|
|
||||||
using Microsoft.EntityFrameworkCore;
|
|
||||||
using Microsoft.EntityFrameworkCore.Storage;
|
|
||||||
|
|
||||||
namespace HrynCo.NotificationService.DAL.EF.Core;
|
|
||||||
|
|
||||||
internal abstract class EfUnitOfWork<TDbContext> : IUnitOfWork
|
|
||||||
where TDbContext : DbContext
|
|
||||||
{
|
|
||||||
private readonly TDbContext _context;
|
|
||||||
private EfTransactionAdapter? _currentTransaction;
|
|
||||||
|
|
||||||
protected EfUnitOfWork(TDbContext context)
|
|
||||||
{
|
|
||||||
_context = context;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Task SaveChangesAsync(CancellationToken cancellationToken = default)
|
|
||||||
{
|
|
||||||
return _context.SaveChangesAsync(cancellationToken);
|
|
||||||
}
|
|
||||||
|
|
||||||
public async Task<ITransaction> BeginTransactionAsync(CancellationToken cancellationToken = default)
|
|
||||||
{
|
|
||||||
if (_currentTransaction != null)
|
|
||||||
{
|
|
||||||
return _currentTransaction;
|
|
||||||
}
|
|
||||||
|
|
||||||
IDbContextTransaction tx = await _context.Database.BeginTransactionAsync(cancellationToken);
|
|
||||||
_currentTransaction = new EfTransactionAdapter(tx);
|
|
||||||
return _currentTransaction;
|
|
||||||
}
|
|
||||||
|
|
||||||
public ITransaction? GetCurrentTransaction()
|
|
||||||
{
|
|
||||||
return _currentTransaction;
|
|
||||||
}
|
|
||||||
|
|
||||||
public async Task ExecuteInTransactionAsync(Func<Task> action)
|
|
||||||
{
|
|
||||||
ITransaction? existing = GetCurrentTransaction();
|
|
||||||
bool ownsTransaction = existing is null;
|
|
||||||
ITransaction tx = existing ?? await BeginTransactionAsync();
|
|
||||||
|
|
||||||
try
|
|
||||||
{
|
|
||||||
await action();
|
|
||||||
if (ownsTransaction)
|
|
||||||
{
|
|
||||||
await tx.CommitAsync();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
catch
|
|
||||||
{
|
|
||||||
if (ownsTransaction)
|
|
||||||
{
|
|
||||||
await tx.RollbackAsync();
|
|
||||||
}
|
|
||||||
|
|
||||||
throw;
|
|
||||||
}
|
|
||||||
finally
|
|
||||||
{
|
|
||||||
if (ownsTransaction)
|
|
||||||
{
|
|
||||||
await tx.DisposeAsync();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public async Task<TResult> ExecuteInTransactionAsync<TResult>(Func<Task<TResult>> action)
|
|
||||||
{
|
|
||||||
ITransaction? existing = GetCurrentTransaction();
|
|
||||||
bool ownsTransaction = existing is null;
|
|
||||||
ITransaction tx = existing ?? await BeginTransactionAsync();
|
|
||||||
|
|
||||||
try
|
|
||||||
{
|
|
||||||
TResult result = await action();
|
|
||||||
if (ownsTransaction)
|
|
||||||
{
|
|
||||||
await tx.CommitAsync();
|
|
||||||
}
|
|
||||||
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
catch
|
|
||||||
{
|
|
||||||
if (ownsTransaction)
|
|
||||||
{
|
|
||||||
await tx.RollbackAsync();
|
|
||||||
}
|
|
||||||
|
|
||||||
throw;
|
|
||||||
}
|
|
||||||
finally
|
|
||||||
{
|
|
||||||
if (ownsTransaction)
|
|
||||||
{
|
|
||||||
await tx.DisposeAsync();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -0,0 +1,20 @@
|
|||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,13 @@
|
|||||||
|
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)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,8 +0,0 @@
|
|||||||
namespace HrynCo.NotificationService.DAL.EF.Core;
|
|
||||||
|
|
||||||
internal sealed class UnitOfWork : EfUnitOfWork<NotificationDbContext>
|
|
||||||
{
|
|
||||||
public UnitOfWork(NotificationDbContext context) : base(context)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
using HrynCo.NotificationService.DAL.Abstract.Entities;
|
using HrynCo.DAL.Abstract.Entities;
|
||||||
using HrynCo.NotificationService.DAL.Abstract.Providers;
|
using HrynCo.NotificationService.DAL.Abstract.Providers;
|
||||||
|
|
||||||
namespace HrynCo.NotificationService.DAL.EF.Entities;
|
namespace HrynCo.NotificationService.DAL.EF.Entities;
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
using HrynCo.NotificationService.DAL.Abstract.Entities;
|
using HrynCo.DAL.Abstract.Entities;
|
||||||
|
|
||||||
namespace HrynCo.NotificationService.DAL.EF.Entities;
|
namespace HrynCo.NotificationService.DAL.EF.Entities;
|
||||||
|
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
using HrynCo.NotificationService.DAL.Abstract.Entities;
|
using HrynCo.DAL.Abstract.Entities;
|
||||||
|
|
||||||
namespace HrynCo.NotificationService.DAL.EF.Entities;
|
namespace HrynCo.NotificationService.DAL.EF.Entities;
|
||||||
|
|
||||||
|
|||||||
@@ -5,7 +5,7 @@
|
|||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="Microsoft.EntityFrameworkCore" />
|
<PackageReference Include="HrynCo.DAL.EF" />
|
||||||
<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>
|
||||||
|
|||||||
+225
@@ -0,0 +1,225 @@
|
|||||||
|
// <auto-generated />
|
||||||
|
using System;
|
||||||
|
using HrynCo.NotificationService.DAL.EF;
|
||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
using Microsoft.EntityFrameworkCore.Infrastructure;
|
||||||
|
using Microsoft.EntityFrameworkCore.Migrations;
|
||||||
|
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
|
||||||
|
using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata;
|
||||||
|
|
||||||
|
#nullable disable
|
||||||
|
|
||||||
|
namespace HrynCo.NotificationService.DAL.EF.Migrations
|
||||||
|
{
|
||||||
|
[DbContext(typeof(NotificationDbContext))]
|
||||||
|
[Migration("20260502154249_PendingChanges")]
|
||||||
|
partial class PendingChanges
|
||||||
|
{
|
||||||
|
/// <inheritdoc />
|
||||||
|
protected override void BuildTargetModel(ModelBuilder modelBuilder)
|
||||||
|
{
|
||||||
|
#pragma warning disable 612, 618
|
||||||
|
modelBuilder
|
||||||
|
.HasAnnotation("ProductVersion", "9.0.5")
|
||||||
|
.HasAnnotation("Relational:MaxIdentifierLength", 63);
|
||||||
|
|
||||||
|
NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder);
|
||||||
|
|
||||||
|
modelBuilder.Entity("HrynCo.NotificationService.DAL.EF.Entities.EmailChannelEntity", b =>
|
||||||
|
{
|
||||||
|
b.Property<Guid>("Id")
|
||||||
|
.ValueGeneratedOnAdd()
|
||||||
|
.HasColumnType("uuid")
|
||||||
|
.HasColumnName("id");
|
||||||
|
|
||||||
|
b.Property<DateTimeOffset>("Created")
|
||||||
|
.HasColumnType("timestamp with time zone")
|
||||||
|
.HasColumnName("created");
|
||||||
|
|
||||||
|
b.Property<int?>("DailyLimit")
|
||||||
|
.HasColumnType("integer")
|
||||||
|
.HasColumnName("daily_limit");
|
||||||
|
|
||||||
|
b.Property<int>("EmailChannelType")
|
||||||
|
.HasColumnType("integer")
|
||||||
|
.HasColumnName("provider_type");
|
||||||
|
|
||||||
|
b.Property<bool>("IsActive")
|
||||||
|
.HasColumnType("boolean")
|
||||||
|
.HasColumnName("is_active");
|
||||||
|
|
||||||
|
b.Property<int?>("MonthlyLimit")
|
||||||
|
.HasColumnType("integer")
|
||||||
|
.HasColumnName("monthly_limit");
|
||||||
|
|
||||||
|
b.Property<int>("Priority")
|
||||||
|
.HasColumnType("integer")
|
||||||
|
.HasColumnName("priority");
|
||||||
|
|
||||||
|
b.Property<string>("ServiceName")
|
||||||
|
.IsRequired()
|
||||||
|
.HasMaxLength(100)
|
||||||
|
.HasColumnType("character varying(100)")
|
||||||
|
.HasColumnName("service_name");
|
||||||
|
|
||||||
|
b.Property<string>("SettingsJson")
|
||||||
|
.IsRequired()
|
||||||
|
.HasColumnType("jsonb")
|
||||||
|
.HasColumnName("settings");
|
||||||
|
|
||||||
|
b.Property<DateTimeOffset?>("Updated")
|
||||||
|
.HasColumnType("timestamp with time zone")
|
||||||
|
.HasColumnName("updated");
|
||||||
|
|
||||||
|
b.Property<int>("WarnThresholdPercent")
|
||||||
|
.HasColumnType("integer")
|
||||||
|
.HasColumnName("warn_threshold_percent");
|
||||||
|
|
||||||
|
b.HasKey("Id");
|
||||||
|
|
||||||
|
b.HasIndex("ServiceName", "Priority");
|
||||||
|
|
||||||
|
b.ToTable("email_channels", (string)null);
|
||||||
|
});
|
||||||
|
|
||||||
|
modelBuilder.Entity("HrynCo.NotificationService.DAL.EF.Entities.EmailChannelUsageEntity", b =>
|
||||||
|
{
|
||||||
|
b.Property<Guid>("Id")
|
||||||
|
.ValueGeneratedOnAdd()
|
||||||
|
.HasColumnType("uuid")
|
||||||
|
.HasColumnName("id");
|
||||||
|
|
||||||
|
b.Property<DateTimeOffset>("Created")
|
||||||
|
.HasColumnType("timestamp with time zone")
|
||||||
|
.HasColumnName("created");
|
||||||
|
|
||||||
|
b.Property<DateOnly>("Date")
|
||||||
|
.HasColumnType("date")
|
||||||
|
.HasColumnName("date");
|
||||||
|
|
||||||
|
b.Property<Guid>("ProviderId")
|
||||||
|
.HasColumnType("uuid")
|
||||||
|
.HasColumnName("provider_id");
|
||||||
|
|
||||||
|
b.Property<int>("SentCount")
|
||||||
|
.HasColumnType("integer")
|
||||||
|
.HasColumnName("sent_count");
|
||||||
|
|
||||||
|
b.Property<DateTimeOffset?>("Updated")
|
||||||
|
.HasColumnType("timestamp with time zone")
|
||||||
|
.HasColumnName("updated");
|
||||||
|
|
||||||
|
b.HasKey("Id");
|
||||||
|
|
||||||
|
b.HasIndex("ProviderId", "Date")
|
||||||
|
.IsUnique();
|
||||||
|
|
||||||
|
b.ToTable("email_channel_usage", (string)null);
|
||||||
|
});
|
||||||
|
|
||||||
|
modelBuilder.Entity("HrynCo.NotificationService.DAL.EF.Entities.EmailTemplateEntity", b =>
|
||||||
|
{
|
||||||
|
b.Property<Guid>("Id")
|
||||||
|
.ValueGeneratedOnAdd()
|
||||||
|
.HasColumnType("uuid")
|
||||||
|
.HasColumnName("id");
|
||||||
|
|
||||||
|
b.Property<DateTimeOffset>("Created")
|
||||||
|
.HasColumnType("timestamp with time zone")
|
||||||
|
.HasColumnName("created");
|
||||||
|
|
||||||
|
b.Property<string>("HtmlBody")
|
||||||
|
.IsRequired()
|
||||||
|
.HasColumnType("text")
|
||||||
|
.HasColumnName("html_body");
|
||||||
|
|
||||||
|
b.Property<string>("Key")
|
||||||
|
.IsRequired()
|
||||||
|
.HasMaxLength(100)
|
||||||
|
.HasColumnType("character varying(100)")
|
||||||
|
.HasColumnName("key");
|
||||||
|
|
||||||
|
b.Property<string>("LanguageCode")
|
||||||
|
.IsRequired()
|
||||||
|
.HasMaxLength(10)
|
||||||
|
.HasColumnType("character varying(10)")
|
||||||
|
.HasColumnName("language_code");
|
||||||
|
|
||||||
|
b.Property<string>("ServiceName")
|
||||||
|
.IsRequired()
|
||||||
|
.HasMaxLength(100)
|
||||||
|
.HasColumnType("character varying(100)")
|
||||||
|
.HasColumnName("service_name");
|
||||||
|
|
||||||
|
b.Property<string>("Subject")
|
||||||
|
.IsRequired()
|
||||||
|
.HasColumnType("text")
|
||||||
|
.HasColumnName("subject");
|
||||||
|
|
||||||
|
b.Property<string>("TextBody")
|
||||||
|
.IsRequired()
|
||||||
|
.HasColumnType("text")
|
||||||
|
.HasColumnName("text_body");
|
||||||
|
|
||||||
|
b.Property<DateTimeOffset?>("Updated")
|
||||||
|
.HasColumnType("timestamp with time zone")
|
||||||
|
.HasColumnName("updated");
|
||||||
|
|
||||||
|
b.HasKey("Id");
|
||||||
|
|
||||||
|
b.HasIndex("ServiceName", "Key", "LanguageCode")
|
||||||
|
.IsUnique();
|
||||||
|
|
||||||
|
b.ToTable("email_templates", (string)null);
|
||||||
|
});
|
||||||
|
|
||||||
|
modelBuilder.Entity("HrynCo.NotificationService.DAL.EF.Entities.EmailChannelUsageEntity", b =>
|
||||||
|
{
|
||||||
|
b.HasOne("HrynCo.NotificationService.DAL.EF.Entities.EmailChannelEntity", null)
|
||||||
|
.WithMany("UsageRecords")
|
||||||
|
.HasForeignKey("ProviderId")
|
||||||
|
.OnDelete(DeleteBehavior.Cascade)
|
||||||
|
.IsRequired();
|
||||||
|
});
|
||||||
|
|
||||||
|
modelBuilder.Entity("HrynCo.NotificationService.DAL.EF.Entities.EmailTemplateEntity", b =>
|
||||||
|
{
|
||||||
|
b.OwnsMany("HrynCo.NotificationService.DAL.EF.Entities.EmailTemplateVariableData", "Variables", b1 =>
|
||||||
|
{
|
||||||
|
b1.Property<Guid>("EmailTemplateEntityId")
|
||||||
|
.HasColumnType("uuid");
|
||||||
|
|
||||||
|
b1.Property<int>("__synthesizedOrdinal")
|
||||||
|
.ValueGeneratedOnAdd()
|
||||||
|
.HasColumnType("integer");
|
||||||
|
|
||||||
|
b1.Property<string>("Name")
|
||||||
|
.IsRequired()
|
||||||
|
.HasColumnType("text")
|
||||||
|
.HasAnnotation("Relational:JsonPropertyName", "name");
|
||||||
|
|
||||||
|
b1.Property<bool>("Required")
|
||||||
|
.HasColumnType("boolean")
|
||||||
|
.HasAnnotation("Relational:JsonPropertyName", "required");
|
||||||
|
|
||||||
|
b1.HasKey("EmailTemplateEntityId", "__synthesizedOrdinal");
|
||||||
|
|
||||||
|
b1.ToTable("email_templates");
|
||||||
|
|
||||||
|
b1.ToJson("variables");
|
||||||
|
|
||||||
|
b1.WithOwner()
|
||||||
|
.HasForeignKey("EmailTemplateEntityId");
|
||||||
|
});
|
||||||
|
|
||||||
|
b.Navigation("Variables");
|
||||||
|
});
|
||||||
|
|
||||||
|
modelBuilder.Entity("HrynCo.NotificationService.DAL.EF.Entities.EmailChannelEntity", b =>
|
||||||
|
{
|
||||||
|
b.Navigation("UsageRecords");
|
||||||
|
});
|
||||||
|
#pragma warning restore 612, 618
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,30 @@
|
|||||||
|
using Microsoft.EntityFrameworkCore.Migrations;
|
||||||
|
|
||||||
|
#nullable disable
|
||||||
|
|
||||||
|
namespace HrynCo.NotificationService.DAL.EF.Migrations
|
||||||
|
{
|
||||||
|
/// <inheritdoc />
|
||||||
|
public partial class PendingChanges : Migration
|
||||||
|
{
|
||||||
|
/// <inheritdoc />
|
||||||
|
protected override void Up(MigrationBuilder migrationBuilder)
|
||||||
|
{
|
||||||
|
migrationBuilder.AddForeignKey(
|
||||||
|
name: "FK_email_channel_usage_email_channels_provider_id",
|
||||||
|
table: "email_channel_usage",
|
||||||
|
column: "provider_id",
|
||||||
|
principalTable: "email_channels",
|
||||||
|
principalColumn: "id",
|
||||||
|
onDelete: ReferentialAction.Cascade);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
|
protected override void Down(MigrationBuilder migrationBuilder)
|
||||||
|
{
|
||||||
|
migrationBuilder.DropForeignKey(
|
||||||
|
name: "FK_email_channel_usage_email_channels_provider_id",
|
||||||
|
table: "email_channel_usage");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -170,6 +170,15 @@ namespace HrynCo.NotificationService.DAL.EF.Migrations
|
|||||||
b.ToTable("email_templates", (string)null);
|
b.ToTable("email_templates", (string)null);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
modelBuilder.Entity("HrynCo.NotificationService.DAL.EF.Entities.EmailChannelUsageEntity", b =>
|
||||||
|
{
|
||||||
|
b.HasOne("HrynCo.NotificationService.DAL.EF.Entities.EmailChannelEntity", null)
|
||||||
|
.WithMany("UsageRecords")
|
||||||
|
.HasForeignKey("ProviderId")
|
||||||
|
.OnDelete(DeleteBehavior.Cascade)
|
||||||
|
.IsRequired();
|
||||||
|
});
|
||||||
|
|
||||||
modelBuilder.Entity("HrynCo.NotificationService.DAL.EF.Entities.EmailTemplateEntity", b =>
|
modelBuilder.Entity("HrynCo.NotificationService.DAL.EF.Entities.EmailTemplateEntity", b =>
|
||||||
{
|
{
|
||||||
b.OwnsMany("HrynCo.NotificationService.DAL.EF.Entities.EmailTemplateVariableData", "Variables", b1 =>
|
b.OwnsMany("HrynCo.NotificationService.DAL.EF.Entities.EmailTemplateVariableData", "Variables", b1 =>
|
||||||
@@ -202,6 +211,11 @@ namespace HrynCo.NotificationService.DAL.EF.Migrations
|
|||||||
|
|
||||||
b.Navigation("Variables");
|
b.Navigation("Variables");
|
||||||
});
|
});
|
||||||
|
|
||||||
|
modelBuilder.Entity("HrynCo.NotificationService.DAL.EF.Entities.EmailChannelEntity", b =>
|
||||||
|
{
|
||||||
|
b.Navigation("UsageRecords");
|
||||||
|
});
|
||||||
#pragma warning restore 612, 618
|
#pragma warning restore 612, 618
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,10 @@
|
|||||||
|
namespace HrynCo.NotificationService.DAL.EF;
|
||||||
|
|
||||||
|
using HrynCo.DAL.EF.Core;
|
||||||
|
|
||||||
|
public class NotificationUnitOfWork : EfUnitOfWork<NotificationDbContext>
|
||||||
|
{
|
||||||
|
public NotificationUnitOfWork(NotificationDbContext context) : base(context)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,3 +1,5 @@
|
|||||||
|
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;
|
||||||
@@ -5,9 +7,7 @@ using HrynCo.NotificationService.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;
|
internal sealed class EmailChannelRepository : NotificationBaseRepository<EmailChannelEntity>, IEmailChannelRepository
|
||||||
|
|
||||||
internal sealed class EmailChannelRepository : EfRepository<EmailChannelEntity>, IEmailChannelRepository
|
|
||||||
{
|
{
|
||||||
public EmailChannelRepository(NotificationDbContext dbContext) : base(dbContext)
|
public EmailChannelRepository(NotificationDbContext dbContext) : base(dbContext)
|
||||||
{
|
{
|
||||||
@@ -15,20 +15,14 @@ internal sealed class EmailChannelRepository : EfRepository<EmailChannelEntity>,
|
|||||||
|
|
||||||
public async Task<IReadOnlyList<EmailChannel>> GetAllAsync(CancellationToken ct = default)
|
public async Task<IReadOnlyList<EmailChannel>> GetAllAsync(CancellationToken ct = default)
|
||||||
{
|
{
|
||||||
var entities = await DbSet
|
var entities = await EfRepository.Get().ToListAsync(ct);
|
||||||
.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 DbSet
|
var entities = await EfRepository.Get(x => x.ServiceName == serviceName)
|
||||||
.AsNoTracking()
|
|
||||||
.Where(x => x.ServiceName == serviceName)
|
|
||||||
.OrderBy(x => x.Priority)
|
.OrderBy(x => x.Priority)
|
||||||
.ToListAsync(ct);
|
.ToListAsync(ct);
|
||||||
|
|
||||||
@@ -38,8 +32,7 @@ internal sealed class EmailChannelRepository : EfRepository<EmailChannelEntity>,
|
|||||||
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 DbSet
|
var rows = await EfRepository.Get()
|
||||||
.AsNoTracking()
|
|
||||||
.OrderBy(c => c.ServiceName)
|
.OrderBy(c => c.ServiceName)
|
||||||
.ThenBy(c => c.Priority)
|
.ThenBy(c => c.Priority)
|
||||||
.Select(c => new
|
.Select(c => new
|
||||||
@@ -61,30 +54,24 @@ internal sealed class EmailChannelRepository : EfRepository<EmailChannelEntity>,
|
|||||||
|
|
||||||
public async Task<EmailChannel?> GetByIdAsync(Guid id, CancellationToken ct = default)
|
public async Task<EmailChannel?> GetByIdAsync(Guid id, CancellationToken ct = default)
|
||||||
{
|
{
|
||||||
EmailChannelEntity? entity = await DbSet.AsNoTracking().FirstOrDefaultAsync(x => x.Id == id, ct);
|
EmailChannelEntity? entity = await EfRepository.GetByIdAsync(id);
|
||||||
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 base.AddAsync(MapToEntity(channel), ct);
|
return EfRepository.AddAsync(MapToEntity(channel));
|
||||||
}
|
}
|
||||||
|
|
||||||
public Task UpdateAsync(EmailChannel channel, CancellationToken ct = default)
|
public async Task UpdateAsync(EmailChannel channel, CancellationToken ct = default)
|
||||||
{
|
{
|
||||||
EmailChannelEntity entity = MapToEntity(channel);
|
EmailChannelEntity entity = MapToEntity(channel);
|
||||||
entity.Updated = DateTimeOffset.UtcNow;
|
await EfRepository.UpdateAsync(entity);
|
||||||
Update(entity);
|
|
||||||
return Task.CompletedTask;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task DeleteAsync(EmailChannel channel, CancellationToken ct = default)
|
public async Task DeleteAsync(EmailChannel channel, CancellationToken ct = default)
|
||||||
{
|
{
|
||||||
EmailChannelEntity? entity = await DbSet.FindAsync([channel.Id], ct);
|
await EfRepository.DeleteAsync(channel.Id);
|
||||||
if (entity is not null)
|
|
||||||
{
|
|
||||||
Delete(entity);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private static EmailChannel MapToDomain(EmailChannelEntity e)
|
private static EmailChannel MapToDomain(EmailChannelEntity e)
|
||||||
@@ -128,8 +115,8 @@ internal sealed class EmailChannelRepository : EfRepository<EmailChannelEntity>,
|
|||||||
return type switch
|
return type switch
|
||||||
{
|
{
|
||||||
EmailChannelType.Smtp => JsonSerializer.Deserialize<SmtpChannelSettings>(json)
|
EmailChannelType.Smtp => JsonSerializer.Deserialize<SmtpChannelSettings>(json)
|
||||||
?? throw new InvalidOperationException(
|
?? throw new InvalidOperationException(
|
||||||
"Failed to deserialize SMTP EmailChannel settings."),
|
"Failed to deserialize SMTP EmailChannel settings."),
|
||||||
_ => throw new InvalidOperationException($"Unknown or undefined email channel type: {type}")
|
_ => throw new InvalidOperationException($"Unknown or undefined email channel type: {type}")
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ using Microsoft.EntityFrameworkCore;
|
|||||||
|
|
||||||
namespace HrynCo.NotificationService.DAL.EF.Repositories;
|
namespace HrynCo.NotificationService.DAL.EF.Repositories;
|
||||||
|
|
||||||
internal sealed class EmailChannelUsageRepository : EfRepository<EmailChannelUsageEntity>, IEmailChannelUsageRepository
|
internal sealed class EmailChannelUsageRepository : NotificationBaseRepository<EmailChannelUsageEntity>, IEmailChannelUsageRepository
|
||||||
{
|
{
|
||||||
public EmailChannelUsageRepository(NotificationDbContext dbContext) : base(dbContext)
|
public EmailChannelUsageRepository(NotificationDbContext dbContext) : base(dbContext)
|
||||||
{
|
{
|
||||||
@@ -13,7 +13,8 @@ internal sealed class EmailChannelUsageRepository : EfRepository<EmailChannelUsa
|
|||||||
|
|
||||||
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 DbSet
|
EmailChannelUsageEntity? entity = await EfRepository.Get()
|
||||||
|
.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;
|
||||||
@@ -21,7 +22,7 @@ internal sealed class EmailChannelUsageRepository : EfRepository<EmailChannelUsa
|
|||||||
|
|
||||||
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 DbSet
|
return await EfRepository.Get()
|
||||||
.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)
|
||||||
@@ -30,15 +31,16 @@ internal sealed class EmailChannelUsageRepository : EfRepository<EmailChannelUsa
|
|||||||
|
|
||||||
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 DbSet
|
EmailChannelUsageEntity? entity = await EfRepository.Get()
|
||||||
|
.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 AddAsync(new EmailChannelUsageEntity { ProviderId = providerId, Date = date, SentCount = 1 }, ct);
|
await EfRepository.AddAsync(new EmailChannelUsageEntity { ProviderId = providerId, Date = date, SentCount = 1 });
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
entity.SentCount++;
|
entity.SentCount++;
|
||||||
Update(entity);
|
await EfRepository.UpdateAsync(entity);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -6,7 +6,8 @@ using Microsoft.EntityFrameworkCore;
|
|||||||
|
|
||||||
namespace HrynCo.NotificationService.DAL.EF.Repositories;
|
namespace HrynCo.NotificationService.DAL.EF.Repositories;
|
||||||
|
|
||||||
internal sealed class EmailTemplateRepository : EfRepository<EmailTemplateEntity>, IEmailTemplateRepository
|
internal sealed class EmailTemplateRepository
|
||||||
|
: NotificationBaseRepository<EmailTemplateEntity>, IEmailTemplateRepository
|
||||||
{
|
{
|
||||||
public EmailTemplateRepository(NotificationDbContext dbContext) : base(dbContext)
|
public EmailTemplateRepository(NotificationDbContext dbContext) : base(dbContext)
|
||||||
{
|
{
|
||||||
@@ -14,13 +15,17 @@ internal sealed class EmailTemplateRepository : EfRepository<EmailTemplateEntity
|
|||||||
|
|
||||||
public async Task<IReadOnlyList<EmailTemplate>> GetAllAsync(CancellationToken ct = default)
|
public async Task<IReadOnlyList<EmailTemplate>> GetAllAsync(CancellationToken ct = default)
|
||||||
{
|
{
|
||||||
List<EmailTemplateEntity> entities = await DbSet.ToListAsync(ct);
|
List<EmailTemplateEntity> entities = await EfRepository.Get()
|
||||||
|
.OrderBy(x => x.ServiceName).ThenBy(x => x.Key)
|
||||||
|
.AsNoTracking()
|
||||||
|
.ToListAsync(ct);
|
||||||
return entities.Select(MapToDomain).ToList();
|
return entities.Select(MapToDomain).ToList();
|
||||||
}
|
}
|
||||||
|
|
||||||
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 DbSet
|
List<EmailTemplateEntity> entities = await EfRepository.Get()
|
||||||
|
.AsNoTracking()
|
||||||
.Where(x => x.ServiceName == serviceName)
|
.Where(x => x.ServiceName == serviceName)
|
||||||
.ToListAsync(ct);
|
.ToListAsync(ct);
|
||||||
|
|
||||||
@@ -29,28 +34,48 @@ internal sealed class EmailTemplateRepository : EfRepository<EmailTemplateEntity
|
|||||||
|
|
||||||
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 DbSet.FirstOrDefaultAsync(
|
EmailTemplateEntity? entity = await EfRepository.Get()
|
||||||
x => x.ServiceName == serviceName && x.Key == key && x.LanguageCode == languageCode, ct);
|
.AsNoTracking()
|
||||||
|
.FirstOrDefaultAsync(
|
||||||
|
x => x.ServiceName == serviceName && x.Key == key && x.LanguageCode == languageCode, ct);
|
||||||
|
|
||||||
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)
|
|
||||||
{
|
{
|
||||||
EmailTemplateEntity entity = MapToEntity(EmailTemplate);
|
return EfRepository.AddAsync(MapToEntity(EmailTemplate));
|
||||||
entity.Updated = DateTimeOffset.UtcNow;
|
}
|
||||||
Update(entity);
|
|
||||||
return Task.CompletedTask;
|
public async Task UpdateAsync(EmailTemplate EmailTemplate, CancellationToken ct = default)
|
||||||
|
{
|
||||||
|
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 DbSet.FindAsync([EmailTemplate.Id], ct);
|
EmailTemplateEntity? entity = await EfRepository.Get()
|
||||||
|
.FirstOrDefaultAsync(x => x.Id == EmailTemplate.Id, ct);
|
||||||
if (entity is not null)
|
if (entity is not null)
|
||||||
Delete(entity);
|
await EfRepository.DeleteAsync(entity);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static EmailTemplate MapToDomain(EmailTemplateEntity e) => new()
|
private static EmailTemplate MapToDomain(EmailTemplateEntity e) => new()
|
||||||
@@ -80,4 +105,4 @@ internal sealed class EmailTemplateRepository : EfRepository<EmailTemplateEntity
|
|||||||
Created = t.Created,
|
Created = t.Created,
|
||||||
Updated = t.Updated
|
Updated = t.Updated
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
using HrynCo.NotificationService.DAL.Abstract;
|
using HrynCo.DAL.Abstract;
|
||||||
using HrynCo.NotificationService.DAL.Abstract.Repositories;
|
using HrynCo.NotificationService.DAL.Abstract.Repositories;
|
||||||
using HrynCo.NotificationService.DAL.EF.Core;
|
using HrynCo.NotificationService.DAL.EF.Core;
|
||||||
using HrynCo.NotificationService.DAL.EF.Repositories;
|
using HrynCo.NotificationService.DAL.EF.Repositories;
|
||||||
@@ -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,5 +1,5 @@
|
|||||||
using HrynCo.Common;
|
using HrynCo.Common;
|
||||||
using HrynCo.NotificationService.DAL.Abstract;
|
using HrynCo.DAL.Abstract;
|
||||||
using MediatR;
|
using MediatR;
|
||||||
|
|
||||||
namespace HrynCo.NotificationService.Services.Behaviors;
|
namespace HrynCo.NotificationService.Services.Behaviors;
|
||||||
@@ -18,11 +18,15 @@ public class TransactionBehavior<TRequest, TResponse> : IPipelineBehavior<TReque
|
|||||||
|
|
||||||
public Task<TResponse> Handle(TRequest request, RequestHandlerDelegate<TResponse> next, CancellationToken cancellationToken) =>
|
public Task<TResponse> Handle(TRequest request, RequestHandlerDelegate<TResponse> next, CancellationToken cancellationToken) =>
|
||||||
_profiler.MeasureExecutionAsync(
|
_profiler.MeasureExecutionAsync(
|
||||||
() => _unitOfWork.ExecuteInTransactionAsync(async () =>
|
async () =>
|
||||||
{
|
{
|
||||||
TResponse response = await next();
|
TResponse? response = default;
|
||||||
await _unitOfWork.SaveChangesAsync(cancellationToken);
|
await _unitOfWork.ExecuteInTransactionAsync(async () =>
|
||||||
return response;
|
{
|
||||||
}),
|
response = await next();
|
||||||
|
});
|
||||||
|
|
||||||
|
return response!;
|
||||||
|
},
|
||||||
typeof(TRequest).Name);
|
typeof(TRequest).Name);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,4 +1,3 @@
|
|||||||
using HrynCo.NotificationService.DAL.Abstract;
|
|
||||||
using HrynCo.NotificationService.Services.Logging;
|
using HrynCo.NotificationService.Services.Logging;
|
||||||
using MediatR;
|
using MediatR;
|
||||||
using Serilog;
|
using Serilog;
|
||||||
@@ -8,14 +7,12 @@ 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, IUnitOfWork unitOfWork)
|
protected RequestHandler(IContextualSerilogLogger<TRequest> logger)
|
||||||
{
|
{
|
||||||
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)
|
||||||
{
|
{
|
||||||
@@ -23,4 +20,4 @@ public abstract class RequestHandler<TRequest, TResponse> : IRequestHandler<TReq
|
|||||||
}
|
}
|
||||||
|
|
||||||
protected abstract Task<TResponse> DoOnHandle(TRequest request, CancellationToken cancellationToken);
|
protected abstract Task<TResponse> DoOnHandle(TRequest request, CancellationToken cancellationToken);
|
||||||
}
|
}
|
||||||
|
|||||||
+2
-3
@@ -1,4 +1,4 @@
|
|||||||
using HrynCo.NotificationService.DAL.Abstract;
|
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;
|
||||||
@@ -14,9 +14,8 @@ internal sealed class CreateEmailChannelHandler
|
|||||||
|
|
||||||
public CreateEmailChannelHandler(
|
public CreateEmailChannelHandler(
|
||||||
IContextualSerilogLogger<CreateEmailChannelCommand> logger,
|
IContextualSerilogLogger<CreateEmailChannelCommand> logger,
|
||||||
IUnitOfWork unitOfWork,
|
|
||||||
IEmailChannelRepository channels)
|
IEmailChannelRepository channels)
|
||||||
: base(logger, unitOfWork)
|
: base(logger)
|
||||||
{
|
{
|
||||||
_channels = channels;
|
_channels = channels;
|
||||||
}
|
}
|
||||||
|
|||||||
+2
-3
@@ -1,4 +1,4 @@
|
|||||||
using HrynCo.NotificationService.DAL.Abstract;
|
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;
|
||||||
@@ -13,9 +13,8 @@ internal sealed class DeleteEmailChannelHandler
|
|||||||
|
|
||||||
public DeleteEmailChannelHandler(
|
public DeleteEmailChannelHandler(
|
||||||
IContextualSerilogLogger<DeleteEmailChannelCommand> logger,
|
IContextualSerilogLogger<DeleteEmailChannelCommand> logger,
|
||||||
IUnitOfWork unitOfWork,
|
|
||||||
IEmailChannelRepository channels)
|
IEmailChannelRepository channels)
|
||||||
: base(logger, unitOfWork)
|
: base(logger)
|
||||||
{
|
{
|
||||||
_channels = channels;
|
_channels = channels;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,4 +1,3 @@
|
|||||||
using HrynCo.NotificationService.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;
|
||||||
@@ -14,9 +13,8 @@ internal sealed class GetEmailChannelHandler
|
|||||||
|
|
||||||
public GetEmailChannelHandler(
|
public GetEmailChannelHandler(
|
||||||
IContextualSerilogLogger<GetEmailChannelQuery> logger,
|
IContextualSerilogLogger<GetEmailChannelQuery> logger,
|
||||||
IUnitOfWork unitOfWork,
|
|
||||||
IEmailChannelRepository channels)
|
IEmailChannelRepository channels)
|
||||||
: base(logger, unitOfWork)
|
: base(logger)
|
||||||
{
|
{
|
||||||
_channels = channels;
|
_channels = channels;
|
||||||
}
|
}
|
||||||
|
|||||||
+1
-3
@@ -1,4 +1,3 @@
|
|||||||
using HrynCo.NotificationService.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;
|
||||||
@@ -14,9 +13,8 @@ internal sealed class GetAllEmailChannelsHandler
|
|||||||
|
|
||||||
public GetAllEmailChannelsHandler(
|
public GetAllEmailChannelsHandler(
|
||||||
IContextualSerilogLogger<GetAllEmailChannelsQuery> logger,
|
IContextualSerilogLogger<GetAllEmailChannelsQuery> logger,
|
||||||
IUnitOfWork unitOfWork,
|
|
||||||
IEmailChannelRepository channels)
|
IEmailChannelRepository channels)
|
||||||
: base(logger, unitOfWork)
|
: base(logger)
|
||||||
{
|
{
|
||||||
_channels = channels;
|
_channels = channels;
|
||||||
}
|
}
|
||||||
|
|||||||
+1
-3
@@ -1,4 +1,3 @@
|
|||||||
using HrynCo.NotificationService.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;
|
||||||
@@ -14,9 +13,8 @@ internal sealed class GetEmailChannelsHandler
|
|||||||
|
|
||||||
public GetEmailChannelsHandler(
|
public GetEmailChannelsHandler(
|
||||||
IContextualSerilogLogger<GetEmailChannelsQuery> logger,
|
IContextualSerilogLogger<GetEmailChannelsQuery> logger,
|
||||||
IUnitOfWork unitOfWork,
|
|
||||||
IEmailChannelRepository channels)
|
IEmailChannelRepository channels)
|
||||||
: base(logger, unitOfWork)
|
: base(logger)
|
||||||
{
|
{
|
||||||
_channels = channels;
|
_channels = channels;
|
||||||
}
|
}
|
||||||
|
|||||||
+1
-3
@@ -1,4 +1,3 @@
|
|||||||
using HrynCo.NotificationService.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;
|
||||||
@@ -13,9 +12,8 @@ internal sealed class GetChannelUsageSummaryHandler
|
|||||||
|
|
||||||
public GetChannelUsageSummaryHandler(
|
public GetChannelUsageSummaryHandler(
|
||||||
IContextualSerilogLogger<GetChannelUsageSummaryQuery> logger,
|
IContextualSerilogLogger<GetChannelUsageSummaryQuery> logger,
|
||||||
IUnitOfWork unitOfWork,
|
|
||||||
IEmailChannelRepository channelsRepository)
|
IEmailChannelRepository channelsRepository)
|
||||||
: base(logger, unitOfWork)
|
: base(logger)
|
||||||
{
|
{
|
||||||
_channelsRepository = channelsRepository;
|
_channelsRepository = channelsRepository;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
using System.Net;
|
using System.Net;
|
||||||
using System.Net.Mail;
|
using System.Net.Mail;
|
||||||
using HrynCo.NotificationService.DAL.Abstract;
|
using System.Text;
|
||||||
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,10 +17,9 @@ 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, unitOfWork)
|
: base(logger)
|
||||||
{
|
{
|
||||||
_channels = channels;
|
_channels = channels;
|
||||||
_usage = usage;
|
_usage = usage;
|
||||||
@@ -50,14 +49,16 @@ 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.HtmlBody,
|
Body = request.TextBody ?? string.Empty,
|
||||||
IsBodyHtml = true
|
IsBodyHtml = false,
|
||||||
|
BodyEncoding = Encoding.UTF8,
|
||||||
|
SubjectEncoding = Encoding.UTF8
|
||||||
};
|
};
|
||||||
|
|
||||||
if (!string.IsNullOrWhiteSpace(request.TextBody))
|
if (!string.IsNullOrWhiteSpace(request.HtmlBody))
|
||||||
{
|
{
|
||||||
var plain = AlternateView.CreateAlternateViewFromString(request.TextBody, null, "text/plain");
|
var html = AlternateView.CreateAlternateViewFromString(request.HtmlBody, Encoding.UTF8, "text/html");
|
||||||
mail.AlternateViews.Add(plain);
|
mail.AlternateViews.Add(html);
|
||||||
}
|
}
|
||||||
|
|
||||||
mail.To.Add(new MailAddress(request.RecipientEmail, request.RecipientName));
|
mail.To.Add(new MailAddress(request.RecipientEmail, request.RecipientName));
|
||||||
|
|||||||
@@ -0,0 +1,18 @@
|
|||||||
|
using HrynCo.NotificationService.Services.Core;
|
||||||
|
using MediatR;
|
||||||
|
|
||||||
|
namespace HrynCo.NotificationService.Services.EmailChannels.TestSmtp;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Sends a test email using the provided SMTP settings without persisting anything.
|
||||||
|
/// </summary>
|
||||||
|
public sealed record TestSmtpCommand(
|
||||||
|
string Host,
|
||||||
|
int Port,
|
||||||
|
string Username,
|
||||||
|
string Password,
|
||||||
|
bool UseSsl,
|
||||||
|
string FromEmail,
|
||||||
|
string FromName,
|
||||||
|
string ToEmail
|
||||||
|
) : IRequest<ServiceResult<Core.Unit>>;
|
||||||
@@ -0,0 +1,50 @@
|
|||||||
|
using System.Net;
|
||||||
|
using System.Net.Mail;
|
||||||
|
using HrynCo.NotificationService.Services.Core;
|
||||||
|
using HrynCo.NotificationService.Services.Logging;
|
||||||
|
using static HrynCo.NotificationService.Services.Core.ServiceResultHelper;
|
||||||
|
|
||||||
|
namespace HrynCo.NotificationService.Services.EmailChannels.TestSmtp;
|
||||||
|
|
||||||
|
internal sealed class TestSmtpHandler
|
||||||
|
: RequestHandler<TestSmtpCommand, ServiceResult<Unit>>
|
||||||
|
{
|
||||||
|
public TestSmtpHandler(
|
||||||
|
IContextualSerilogLogger<TestSmtpCommand> logger)
|
||||||
|
: base(logger)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
protected override async Task<ServiceResult<Unit>> DoOnHandle(
|
||||||
|
TestSmtpCommand request, CancellationToken cancellationToken)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using var client = new SmtpClient(request.Host, request.Port)
|
||||||
|
{
|
||||||
|
EnableSsl = request.UseSsl,
|
||||||
|
Credentials = string.IsNullOrWhiteSpace(request.Username)
|
||||||
|
? null
|
||||||
|
: new NetworkCredential(request.Username, request.Password)
|
||||||
|
};
|
||||||
|
|
||||||
|
using var mail = new MailMessage
|
||||||
|
{
|
||||||
|
From = new MailAddress(request.FromEmail, request.FromName),
|
||||||
|
Subject = "✅ Test email from Notification Service",
|
||||||
|
Body = "<p>This is a test email sent from the <b>Notification Service</b> admin panel to verify the channel settings.</p>",
|
||||||
|
IsBodyHtml = true
|
||||||
|
};
|
||||||
|
mail.To.Add(new MailAddress(request.ToEmail));
|
||||||
|
|
||||||
|
await client.SendMailAsync(mail, cancellationToken);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
Logger.Error(ex, "Ad-hoc SMTP test failed for host {Host}", request.Host);
|
||||||
|
return Failure<Unit>(ex.Message);
|
||||||
|
}
|
||||||
|
|
||||||
|
return Success(Unit.Value);
|
||||||
|
}
|
||||||
|
}
|
||||||
+2
-3
@@ -1,4 +1,4 @@
|
|||||||
using HrynCo.NotificationService.DAL.Abstract;
|
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;
|
||||||
@@ -13,9 +13,8 @@ internal sealed class UpdateEmailChannelHandler
|
|||||||
|
|
||||||
public UpdateEmailChannelHandler(
|
public UpdateEmailChannelHandler(
|
||||||
IContextualSerilogLogger<UpdateEmailChannelCommand> logger,
|
IContextualSerilogLogger<UpdateEmailChannelCommand> logger,
|
||||||
IUnitOfWork unitOfWork,
|
|
||||||
IEmailChannelRepository channels)
|
IEmailChannelRepository channels)
|
||||||
: base(logger, unitOfWork)
|
: base(logger)
|
||||||
{
|
{
|
||||||
_channels = channels;
|
_channels = channels;
|
||||||
}
|
}
|
||||||
|
|||||||
+1
-3
@@ -1,4 +1,3 @@
|
|||||||
using HrynCo.NotificationService.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;
|
||||||
@@ -14,9 +13,8 @@ internal sealed class CreateEmailTemplateHandler
|
|||||||
|
|
||||||
public CreateEmailTemplateHandler(
|
public CreateEmailTemplateHandler(
|
||||||
IContextualSerilogLogger<CreateEmailTemplateCommand> logger,
|
IContextualSerilogLogger<CreateEmailTemplateCommand> logger,
|
||||||
IUnitOfWork unitOfWork,
|
|
||||||
IEmailTemplateRepository templates)
|
IEmailTemplateRepository templates)
|
||||||
: base(logger, unitOfWork)
|
: base(logger)
|
||||||
{
|
{
|
||||||
_templates = templates;
|
_templates = templates;
|
||||||
}
|
}
|
||||||
|
|||||||
+2
-3
@@ -1,4 +1,4 @@
|
|||||||
using HrynCo.NotificationService.DAL.Abstract;
|
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;
|
||||||
@@ -13,9 +13,8 @@ internal sealed class DeleteEmailTemplateHandler
|
|||||||
|
|
||||||
public DeleteEmailTemplateHandler(
|
public DeleteEmailTemplateHandler(
|
||||||
IContextualSerilogLogger<DeleteEmailTemplateCommand> logger,
|
IContextualSerilogLogger<DeleteEmailTemplateCommand> logger,
|
||||||
IUnitOfWork unitOfWork,
|
|
||||||
IEmailTemplateRepository templates)
|
IEmailTemplateRepository templates)
|
||||||
: base(logger, unitOfWork)
|
: base(logger)
|
||||||
{
|
{
|
||||||
_templates = templates;
|
_templates = templates;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,4 +1,3 @@
|
|||||||
using HrynCo.NotificationService.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;
|
||||||
@@ -14,9 +13,8 @@ internal sealed class GetEmailTemplateHandler
|
|||||||
|
|
||||||
public GetEmailTemplateHandler(
|
public GetEmailTemplateHandler(
|
||||||
IContextualSerilogLogger<GetEmailTemplateQuery> logger,
|
IContextualSerilogLogger<GetEmailTemplateQuery> logger,
|
||||||
IUnitOfWork unitOfWork,
|
|
||||||
IEmailTemplateRepository templates)
|
IEmailTemplateRepository templates)
|
||||||
: base(logger, unitOfWork)
|
: base(logger)
|
||||||
{
|
{
|
||||||
_templates = templates;
|
_templates = templates;
|
||||||
}
|
}
|
||||||
|
|||||||
+1
-3
@@ -1,4 +1,3 @@
|
|||||||
using HrynCo.NotificationService.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;
|
||||||
@@ -14,9 +13,8 @@ internal sealed class GetAllEmailTemplatesHandler
|
|||||||
|
|
||||||
public GetAllEmailTemplatesHandler(
|
public GetAllEmailTemplatesHandler(
|
||||||
IContextualSerilogLogger<GetAllEmailTemplatesQuery> logger,
|
IContextualSerilogLogger<GetAllEmailTemplatesQuery> logger,
|
||||||
IUnitOfWork unitOfWork,
|
|
||||||
IEmailTemplateRepository templates)
|
IEmailTemplateRepository templates)
|
||||||
: base(logger, unitOfWork)
|
: base(logger)
|
||||||
{
|
{
|
||||||
_templates = templates;
|
_templates = templates;
|
||||||
}
|
}
|
||||||
|
|||||||
+1
-3
@@ -1,4 +1,3 @@
|
|||||||
using HrynCo.NotificationService.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;
|
||||||
@@ -14,9 +13,8 @@ internal sealed class GetEmailTemplatesHandler
|
|||||||
|
|
||||||
public GetEmailTemplatesHandler(
|
public GetEmailTemplatesHandler(
|
||||||
IContextualSerilogLogger<GetEmailTemplatesQuery> logger,
|
IContextualSerilogLogger<GetEmailTemplatesQuery> logger,
|
||||||
IUnitOfWork unitOfWork,
|
|
||||||
IEmailTemplateRepository templates)
|
IEmailTemplateRepository templates)
|
||||||
: base(logger, unitOfWork)
|
: base(logger)
|
||||||
{
|
{
|
||||||
_templates = templates;
|
_templates = templates;
|
||||||
}
|
}
|
||||||
|
|||||||
+2
-3
@@ -1,4 +1,4 @@
|
|||||||
using HrynCo.NotificationService.DAL.Abstract;
|
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;
|
||||||
@@ -13,9 +13,8 @@ internal sealed class UpdateEmailTemplateHandler
|
|||||||
|
|
||||||
public UpdateEmailTemplateHandler(
|
public UpdateEmailTemplateHandler(
|
||||||
IContextualSerilogLogger<UpdateEmailTemplateCommand> logger,
|
IContextualSerilogLogger<UpdateEmailTemplateCommand> logger,
|
||||||
IUnitOfWork unitOfWork,
|
|
||||||
IEmailTemplateRepository templates)
|
IEmailTemplateRepository templates)
|
||||||
: base(logger, unitOfWork)
|
: base(logger)
|
||||||
{
|
{
|
||||||
_templates = templates;
|
_templates = templates;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ using HrynCo.NotificationService.Services.EmailChannels.Delete;
|
|||||||
using HrynCo.NotificationService.Services.EmailChannels.Get;
|
using HrynCo.NotificationService.Services.EmailChannels.Get;
|
||||||
using HrynCo.NotificationService.Services.EmailChannels.GetUsageSummary;
|
using HrynCo.NotificationService.Services.EmailChannels.GetUsageSummary;
|
||||||
using HrynCo.NotificationService.Services.EmailChannels.Send;
|
using HrynCo.NotificationService.Services.EmailChannels.Send;
|
||||||
|
using HrynCo.NotificationService.Services.EmailChannels.TestSmtp;
|
||||||
using HrynCo.NotificationService.Services.EmailChannels.Update;
|
using HrynCo.NotificationService.Services.EmailChannels.Update;
|
||||||
using HrynCo.NotificationService.Web.Controllers.Admin.ViewModels;
|
using HrynCo.NotificationService.Web.Controllers.Admin.ViewModels;
|
||||||
using MediatR;
|
using MediatR;
|
||||||
@@ -133,6 +134,20 @@ public class AdminChannelsController(IMediator mediator) : Controller
|
|||||||
return RedirectToAction(nameof(Index));
|
return RedirectToAction(nameof(Index));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// POST /admin/channels/test-smtp
|
||||||
|
[HttpPost("test-smtp")]
|
||||||
|
public async Task<IActionResult> TestSmtp([FromBody] TestSmtpRequest request, CancellationToken ct)
|
||||||
|
{
|
||||||
|
var result = await mediator.Send(new TestSmtpCommand(
|
||||||
|
request.Host, request.Port, request.Username, request.Password,
|
||||||
|
request.UseSsl, request.FromEmail, request.FromName, request.ToEmail), ct);
|
||||||
|
|
||||||
|
if (!result.IsSuccess)
|
||||||
|
return Ok(new { success = false, message = result.Error?.Message });
|
||||||
|
|
||||||
|
return Ok(new { success = true, message = $"Test email sent to {request.ToEmail}." });
|
||||||
|
}
|
||||||
|
|
||||||
// POST /admin/channels/{id}/test
|
// POST /admin/channels/{id}/test
|
||||||
[HttpPost("{id:guid}/test")]
|
[HttpPost("{id:guid}/test")]
|
||||||
public async Task<IActionResult> Test(Guid id, [FromBody] TestChannelRequest request, CancellationToken ct)
|
public async Task<IActionResult> Test(Guid id, [FromBody] TestChannelRequest request, CancellationToken ct)
|
||||||
@@ -165,3 +180,6 @@ public class AdminChannelsController(IMediator mediator) : Controller
|
|||||||
}
|
}
|
||||||
|
|
||||||
public record TestChannelRequest(string ToEmail);
|
public record TestChannelRequest(string ToEmail);
|
||||||
|
public record TestSmtpRequest(
|
||||||
|
string Host, int Port, string Username, string Password,
|
||||||
|
bool UseSsl, string FromEmail, string FromName, string ToEmail);
|
||||||
|
|||||||
@@ -1,6 +1,47 @@
|
|||||||
@HrynCo.NotificationService.Api_HostAddress = http://localhost:5188
|
@host = http://localhost:5188
|
||||||
|
|
||||||
GET {{HrynCo.NotificationService.Api_HostAddress}}/weatherforecast/
|
### Create a new email template
|
||||||
Accept: application/json
|
POST {{host}}/api/v1/email-templates
|
||||||
|
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
|
||||||
|
|||||||
@@ -3,11 +3,11 @@ using HrynCo.NotificationService.DAL.EF;
|
|||||||
using HrynCo.NotificationService.Services;
|
using HrynCo.NotificationService.Services;
|
||||||
using Scalar.AspNetCore;
|
using Scalar.AspNetCore;
|
||||||
|
|
||||||
var builder = WebApplication.CreateBuilder(args);
|
WebApplicationBuilder builder = WebApplication.CreateBuilder(args);
|
||||||
|
|
||||||
builder.AddSerilog();
|
builder.AddSerilog();
|
||||||
|
|
||||||
var appSettings = builder.Configuration
|
AppSettings 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,17 +18,14 @@ builder.Services.AddControllersWithViews()
|
|||||||
builder.Services.AddNotificationDataAccess(appSettings.ConnectionString);
|
builder.Services.AddNotificationDataAccess(appSettings.ConnectionString);
|
||||||
builder.Services.AddNotificationServices();
|
builder.Services.AddNotificationServices();
|
||||||
|
|
||||||
var app = builder.Build();
|
WebApplication app = builder.Build();
|
||||||
|
|
||||||
if (app.Environment.IsDevelopment())
|
app.MapOpenApi();
|
||||||
|
app.MapScalarApiReference(options =>
|
||||||
{
|
{
|
||||||
app.MapOpenApi();
|
options.Title = "HrynCo Notification Service";
|
||||||
app.MapScalarApiReference(options =>
|
options.Theme = ScalarTheme.DeepSpace;
|
||||||
{
|
});
|
||||||
options.Title = "HrynCo Notification Service";
|
|
||||||
options.Theme = ScalarTheme.DeepSpace;
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
app.UseStaticFiles();
|
app.UseStaticFiles();
|
||||||
app.UseHttpsRedirection();
|
app.UseHttpsRedirection();
|
||||||
|
|||||||
@@ -121,82 +121,90 @@
|
|||||||
<button type="submit" form="channelForm" class="btn btn-primary">
|
<button type="submit" form="channelForm" class="btn btn-primary">
|
||||||
<i class="bi bi-floppy me-1"></i> Save
|
<i class="bi bi-floppy me-1"></i> Save
|
||||||
</button>
|
</button>
|
||||||
@if (!Model.IsNew)
|
<button type="button" class="btn btn-success" id="testModalBtn">
|
||||||
{
|
<i class="bi bi-send me-1"></i> Test
|
||||||
<button type="button" class="btn btn-success" id="testModalBtn">
|
</button>
|
||||||
<i class="bi bi-send me-1"></i> Test
|
|
||||||
</button>
|
|
||||||
}
|
|
||||||
<a href="/admin/channels" class="btn btn-secondary">
|
<a href="/admin/channels" 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>
|
||||||
}
|
}
|
||||||
</form>
|
</form>
|
||||||
|
|
||||||
@if (!Model.IsNew)
|
<div class="modal fade" id="testModal" tabindex="-1">
|
||||||
{
|
<div class="modal-dialog modal-dialog-centered">
|
||||||
<div class="modal fade" id="testModal" tabindex="-1">
|
<div class="modal-content">
|
||||||
<div class="modal-dialog modal-dialog-centered">
|
<div class="modal-header">
|
||||||
<div class="modal-content">
|
<h5 class="modal-title"><i class="bi bi-send me-2"></i>Send Test Email</h5>
|
||||||
<div class="modal-header">
|
<button type="button" class="btn-close" data-bs-dismiss="modal"></button>
|
||||||
<h5 class="modal-title"><i class="bi bi-send me-2"></i>Send Test Email</h5>
|
</div>
|
||||||
<button type="button" class="btn-close" data-bs-dismiss="modal"></button>
|
<div class="modal-body">
|
||||||
</div>
|
<p class="text-muted small">Tests the current form settings — no need to save first.</p>
|
||||||
<div class="modal-body">
|
<label for="testToEmail" class="form-label fw-semibold">Recipient Email</label>
|
||||||
<label for="testToEmail" class="form-label fw-semibold">Recipient Email</label>
|
<input type="email" id="testToEmail" class="form-control" placeholder="you@example.com" />
|
||||||
<input type="email" id="testToEmail" class="form-control" placeholder="you@example.com" />
|
<div id="testResult" class="mt-3" style="display:none"></div>
|
||||||
<div id="testResult" class="mt-3" style="display:none"></div>
|
</div>
|
||||||
</div>
|
<div class="modal-footer">
|
||||||
<div class="modal-footer">
|
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
|
||||||
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
|
<button type="button" class="btn btn-success" id="testSendBtn" onclick="sendTestEmail()">
|
||||||
<button type="button" class="btn btn-success" id="testSendBtn" onclick="sendTestEmail('@Model.Id')">
|
<i class="bi bi-send me-1"></i> Send
|
||||||
<i class="bi bi-send me-1"></i> Send
|
</button>
|
||||||
</button>
|
|
||||||
</div>
|
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
<script>
|
<script>
|
||||||
document.addEventListener('click', function (e) {
|
document.addEventListener('click', function (e) {
|
||||||
if (e.target.closest('#testModalBtn')) {
|
if (e.target.closest('#testModalBtn')) {
|
||||||
bootstrap.Modal.getOrCreateInstance(document.getElementById('testModal')).show();
|
document.getElementById('testResult').style.display = 'none';
|
||||||
}
|
bootstrap.Modal.getOrCreateInstance(document.getElementById('testModal')).show();
|
||||||
});
|
|
||||||
|
|
||||||
async function sendTestEmail(channelId) {
|
|
||||||
const toEmail = document.getElementById('testToEmail').value.trim();
|
|
||||||
const resultDiv = document.getElementById('testResult');
|
|
||||||
const sendBtn = document.getElementById('testSendBtn');
|
|
||||||
|
|
||||||
if (!toEmail) {
|
|
||||||
resultDiv.style.display = 'block';
|
|
||||||
resultDiv.innerHTML = '<div class="alert alert-warning mb-0">Please enter a recipient email.</div>';
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
sendBtn.disabled = true;
|
|
||||||
sendBtn.innerHTML = '<span class="spinner-border spinner-border-sm me-1"></span> Sending…';
|
|
||||||
resultDiv.style.display = 'none';
|
|
||||||
|
|
||||||
try {
|
|
||||||
const resp = await fetch(`/admin/channels/${channelId}/test`, {
|
|
||||||
method: 'POST',
|
|
||||||
headers: { 'Content-Type': 'application/json' },
|
|
||||||
body: JSON.stringify({ toEmail })
|
|
||||||
});
|
|
||||||
const data = await resp.json();
|
|
||||||
resultDiv.style.display = 'block';
|
|
||||||
resultDiv.innerHTML = data.success
|
|
||||||
? `<div class="alert alert-success mb-0"><i class="bi bi-check-circle me-1"></i>${data.message}</div>`
|
|
||||||
: `<div class="alert alert-danger mb-0"><i class="bi bi-x-circle me-1"></i>${data.message}</div>`;
|
|
||||||
} catch (e) {
|
|
||||||
resultDiv.style.display = 'block';
|
|
||||||
resultDiv.innerHTML = `<div class="alert alert-danger mb-0">Request failed: ${e.message}</div>`;
|
|
||||||
} finally {
|
|
||||||
sendBtn.disabled = false;
|
|
||||||
sendBtn.innerHTML = '<i class="bi bi-send me-1"></i> Send';
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
</script>
|
});
|
||||||
}
|
|
||||||
|
async function sendTestEmail() {
|
||||||
|
const toEmail = document.getElementById('testToEmail').value.trim();
|
||||||
|
const resultDiv = document.getElementById('testResult');
|
||||||
|
const sendBtn = document.getElementById('testSendBtn');
|
||||||
|
|
||||||
|
if (!toEmail) {
|
||||||
|
resultDiv.style.display = 'block';
|
||||||
|
resultDiv.innerHTML = '<div class="alert alert-warning mb-0">Please enter a recipient email.</div>';
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const form = document.getElementById('channelForm');
|
||||||
|
const payload = {
|
||||||
|
host: form.querySelector('[name="Host"]').value,
|
||||||
|
port: parseInt(form.querySelector('[name="Port"]').value, 10),
|
||||||
|
username: form.querySelector('[name="Username"]').value,
|
||||||
|
password: form.querySelector('[name="Password"]').value,
|
||||||
|
useSsl: form.querySelector('[name="UseSsl"]').checked,
|
||||||
|
fromEmail: form.querySelector('[name="FromEmail"]').value,
|
||||||
|
fromName: form.querySelector('[name="FromName"]').value,
|
||||||
|
toEmail
|
||||||
|
};
|
||||||
|
|
||||||
|
sendBtn.disabled = true;
|
||||||
|
sendBtn.innerHTML = '<span class="spinner-border spinner-border-sm me-1"></span> Sending…';
|
||||||
|
resultDiv.style.display = 'none';
|
||||||
|
|
||||||
|
try {
|
||||||
|
const resp = await fetch('/admin/channels/test-smtp', {
|
||||||
|
method: 'POST',
|
||||||
|
headers: { 'Content-Type': 'application/json' },
|
||||||
|
body: JSON.stringify(payload)
|
||||||
|
});
|
||||||
|
const data = await resp.json();
|
||||||
|
resultDiv.style.display = 'block';
|
||||||
|
resultDiv.innerHTML = data.success
|
||||||
|
? `<div class="alert alert-success mb-0"><i class="bi bi-check-circle me-1"></i>${data.message}</div>`
|
||||||
|
: `<div class="alert alert-danger mb-0"><i class="bi bi-x-circle me-1"></i>${data.message}</div>`;
|
||||||
|
} catch (e) {
|
||||||
|
resultDiv.style.display = 'block';
|
||||||
|
resultDiv.innerHTML = `<div class="alert alert-danger mb-0">Request failed: ${e.message}</div>`;
|
||||||
|
} finally {
|
||||||
|
sendBtn.disabled = false;
|
||||||
|
sendBtn.innerHTML = '<i class="bi bi-send me-1"></i> Send';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
</script>
|
||||||
|
|||||||
@@ -21,47 +21,95 @@
|
|||||||
</div>
|
</div>
|
||||||
}
|
}
|
||||||
|
|
||||||
<div class="row g-3 mb-3">
|
<ul class="nav nav-tabs template-editor-tabs mb-3" id="templateEditorTabs" role="tablist">
|
||||||
<div class="col-md-5">
|
<li class="nav-item" role="presentation">
|
||||||
<label asp-for="ServiceName" class="form-label fw-semibold">Service Name</label>
|
<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">
|
||||||
<input asp-for="ServiceName" class="form-control" readonly="@(!Model.IsNew)" />
|
Edit
|
||||||
<span asp-validation-for="ServiceName" class="text-danger small"></span>
|
</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="col-md-5">
|
||||||
|
<label asp-for="ServiceName" class="form-label fw-semibold">Service Name</label>
|
||||||
|
<input asp-for="ServiceName" class="form-control" readonly="@(!Model.IsNew)" />
|
||||||
|
<span asp-validation-for="ServiceName" class="text-danger small"></span>
|
||||||
|
</div>
|
||||||
|
<div class="col-md-5">
|
||||||
|
<label asp-for="Key" class="form-label fw-semibold">Key</label>
|
||||||
|
<input asp-for="Key" class="form-control" readonly="@(!Model.IsNew)" />
|
||||||
|
<span asp-validation-for="Key" class="text-danger small"></span>
|
||||||
|
</div>
|
||||||
|
<div class="col-md-2">
|
||||||
|
<label asp-for="LanguageCode" class="form-label fw-semibold">Language</label>
|
||||||
|
<input asp-for="LanguageCode" class="form-control" readonly="@(!Model.IsNew)" />
|
||||||
|
<span asp-validation-for="LanguageCode" class="text-danger small"></span>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="mb-3">
|
||||||
|
<label asp-for="Subject" class="form-label fw-semibold">Subject</label>
|
||||||
|
<input asp-for="Subject" class="form-control" />
|
||||||
|
<span asp-validation-for="Subject" class="text-danger small"></span>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="mb-3">
|
||||||
|
<label asp-for="HtmlBody" class="form-label fw-semibold">HTML Body</label>
|
||||||
|
<textarea asp-for="HtmlBody" class="form-control font-monospace" rows="10"></textarea>
|
||||||
|
<span asp-validation-for="HtmlBody" class="text-danger small"></span>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="mb-3">
|
||||||
|
<label asp-for="TextBody" class="form-label fw-semibold">Text Body</label>
|
||||||
|
<textarea asp-for="TextBody" class="form-control font-monospace" rows="5"></textarea>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="mb-3">
|
||||||
|
<label asp-for="VariablesJson" class="form-label fw-semibold">Variables (JSON)</label>
|
||||||
|
<textarea asp-for="VariablesJson" class="form-control font-monospace" rows="4"
|
||||||
|
placeholder='[{"name":"UserName","required":true}]'></textarea>
|
||||||
|
<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>
|
||||||
</div>
|
</div>
|
||||||
<div class="col-md-5">
|
|
||||||
<label asp-for="Key" class="form-label fw-semibold">Key</label>
|
<div class="tab-pane fade" id="preview-pane" role="tabpanel" aria-labelledby="preview-tab" tabindex="0">
|
||||||
<input asp-for="Key" class="form-control" readonly="@(!Model.IsNew)" />
|
<div class="template-preview-panel mb-3">
|
||||||
<span asp-validation-for="Key" class="text-danger small"></span>
|
<div class="template-preview-panel-header">
|
||||||
|
<div>
|
||||||
|
<div class="template-preview-title">Preview</div>
|
||||||
|
<div class="template-preview-subtitle">Rendered with sample values from the variable list.</div>
|
||||||
|
</div>
|
||||||
|
<span id="previewStatus" class="badge text-bg-secondary">Ready</span>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="template-preview-body">
|
||||||
|
<div class="template-preview-source template-preview-section-block">
|
||||||
|
<div class="template-preview-section-title">Sample values</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>
|
||||||
|
|
||||||
|
<div class="template-preview-output">
|
||||||
|
<div class="template-preview-section-title">Rendered subject</div>
|
||||||
|
<div id="previewSubject" class="template-preview-subject"></div>
|
||||||
|
|
||||||
|
<div class="template-preview-section-title mt-3">Rendered HTML</div>
|
||||||
|
<iframe id="previewHtmlFrame" class="template-preview-frame" title="Email HTML preview"></iframe>
|
||||||
|
|
||||||
|
<div class="template-preview-section-title mt-3">Rendered text</div>
|
||||||
|
<pre id="previewText" class="template-preview-text mb-0"></pre>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<div class="col-md-2">
|
|
||||||
<label asp-for="LanguageCode" class="form-label fw-semibold">Language</label>
|
|
||||||
<input asp-for="LanguageCode" class="form-control" readonly="@(!Model.IsNew)" />
|
|
||||||
<span asp-validation-for="LanguageCode" class="text-danger small"></span>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<div class="mb-3">
|
|
||||||
<label asp-for="Subject" class="form-label fw-semibold">Subject</label>
|
|
||||||
<input asp-for="Subject" class="form-control" />
|
|
||||||
<span asp-validation-for="Subject" class="text-danger small"></span>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<div class="mb-3">
|
|
||||||
<label asp-for="HtmlBody" class="form-label fw-semibold">HTML Body</label>
|
|
||||||
<textarea asp-for="HtmlBody" class="form-control font-monospace" rows="10"></textarea>
|
|
||||||
<span asp-validation-for="HtmlBody" class="text-danger small"></span>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<div class="mb-3">
|
|
||||||
<label asp-for="TextBody" class="form-label fw-semibold">Text Body</label>
|
|
||||||
<textarea asp-for="TextBody" class="form-control font-monospace" rows="5"></textarea>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<div class="mb-3">
|
|
||||||
<label asp-for="VariablesJson" class="form-label fw-semibold">Variables (JSON)</label>
|
|
||||||
<textarea asp-for="VariablesJson" class="form-control font-monospace" rows="4"
|
|
||||||
placeholder='[{"name":"UserName","required":true}]'></textarea>
|
|
||||||
<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>
|
</div>
|
||||||
|
|
||||||
@section FormActions {
|
@section FormActions {
|
||||||
@@ -72,4 +120,179 @@
|
|||||||
<i class="bi bi-x-lg me-1"></i> Cancel
|
<i class="bi bi-x-lg me-1"></i> Cancel
|
||||||
</a>
|
</a>
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@section Scripts {
|
||||||
|
<script>
|
||||||
|
(function () {
|
||||||
|
const subjectField = document.getElementById('Subject');
|
||||||
|
const htmlField = document.getElementById('HtmlBody');
|
||||||
|
const textField = document.getElementById('TextBody');
|
||||||
|
const variablesField = document.getElementById('VariablesJson');
|
||||||
|
const previewVariablesHost = document.getElementById('previewVariables');
|
||||||
|
const previewSubject = document.getElementById('previewSubject');
|
||||||
|
const previewText = document.getElementById('previewText');
|
||||||
|
const previewFrame = document.getElementById('previewHtmlFrame');
|
||||||
|
const previewStatus = document.getElementById('previewStatus');
|
||||||
|
const previewTab = document.getElementById('preview-tab');
|
||||||
|
|
||||||
|
if (!subjectField || !htmlField || !textField || !variablesField || !previewVariablesHost || !previewSubject || !previewText || !previewFrame || !previewStatus) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
function escapeHtml(value) {
|
||||||
|
return String(value)
|
||||||
|
.replace(/&/g, '&')
|
||||||
|
.replace(/</g, '<')
|
||||||
|
.replace(/>/g, '>')
|
||||||
|
.replace(/"/g, '"')
|
||||||
|
.replace(/'/g, ''');
|
||||||
|
}
|
||||||
|
|
||||||
|
function parseVariables() {
|
||||||
|
const raw = variablesField.value?.trim() || '[]';
|
||||||
|
const parsed = JSON.parse(raw);
|
||||||
|
|
||||||
|
if (!Array.isArray(parsed)) {
|
||||||
|
throw new Error('Variables JSON must be an array.');
|
||||||
|
}
|
||||||
|
|
||||||
|
return parsed
|
||||||
|
.filter(item => item && typeof item.Name === 'string' && item.Name.trim().length > 0)
|
||||||
|
.map(item => ({
|
||||||
|
name: item.Name.trim(),
|
||||||
|
required: !!item.Required
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
|
||||||
|
function buildSampleValue(name) {
|
||||||
|
return `Sample ${name}`;
|
||||||
|
}
|
||||||
|
|
||||||
|
function renderVariableInputs() {
|
||||||
|
let variables = [];
|
||||||
|
|
||||||
|
try {
|
||||||
|
variables = parseVariables();
|
||||||
|
previewStatus.className = 'badge text-bg-secondary';
|
||||||
|
previewStatus.textContent = 'Ready';
|
||||||
|
} catch (error) {
|
||||||
|
previewVariablesHost.innerHTML = `<div class="alert alert-warning mb-0">${escapeHtml(error.message || 'Invalid variables JSON')}</div>`;
|
||||||
|
previewStatus.className = 'badge text-bg-danger';
|
||||||
|
previewStatus.textContent = 'Invalid JSON';
|
||||||
|
updatePreview();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (variables.length === 0) {
|
||||||
|
previewVariablesHost.innerHTML = '<div class="text-muted small">No variables defined.</div>';
|
||||||
|
updatePreview();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const currentValues = readVariableValues();
|
||||||
|
previewVariablesHost.innerHTML = variables.map(variable => {
|
||||||
|
const value = Object.prototype.hasOwnProperty.call(currentValues, variable.name)
|
||||||
|
? currentValues[variable.name]
|
||||||
|
: buildSampleValue(variable.name);
|
||||||
|
return `
|
||||||
|
<div class="mb-2">
|
||||||
|
<label class="form-label small fw-semibold mb-1">${escapeHtml(variable.name)}${variable.required ? ' *' : ''}</label>
|
||||||
|
<input type="text" class="form-control form-control-sm preview-variable-input" data-variable-name="${escapeHtml(variable.name)}" value="${escapeHtml(value)}" />
|
||||||
|
</div>
|
||||||
|
`;
|
||||||
|
}).join('');
|
||||||
|
|
||||||
|
previewVariablesHost.querySelectorAll('.preview-variable-input').forEach(input => {
|
||||||
|
input.addEventListener('input', updatePreview);
|
||||||
|
});
|
||||||
|
|
||||||
|
updatePreview();
|
||||||
|
}
|
||||||
|
|
||||||
|
function readVariableValues() {
|
||||||
|
const values = {};
|
||||||
|
previewVariablesHost.querySelectorAll('.preview-variable-input').forEach(input => {
|
||||||
|
values[input.dataset.variableName] = input.value;
|
||||||
|
});
|
||||||
|
return values;
|
||||||
|
}
|
||||||
|
|
||||||
|
function interpolate(text, values) {
|
||||||
|
let result = text || '';
|
||||||
|
|
||||||
|
Object.keys(values).forEach(key => {
|
||||||
|
const token = new RegExp(`\\{\\{${key.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')}\\}\\}`, 'g');
|
||||||
|
result = result.replace(token, values[key] ?? '');
|
||||||
|
});
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
function updatePreview() {
|
||||||
|
let values = {};
|
||||||
|
|
||||||
|
try {
|
||||||
|
values = readVariableValues();
|
||||||
|
} catch (error) {
|
||||||
|
values = {};
|
||||||
|
}
|
||||||
|
|
||||||
|
const renderedSubject = interpolate(subjectField.value, values);
|
||||||
|
const renderedHtml = interpolate(htmlField.value, values);
|
||||||
|
const renderedText = interpolate(textField.value, values);
|
||||||
|
|
||||||
|
previewSubject.textContent = renderedSubject || '(empty subject)';
|
||||||
|
previewText.textContent = renderedText || '(empty text body)';
|
||||||
|
previewFrame.srcdoc = `
|
||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="en">
|
||||||
|
<head>
|
||||||
|
<meta charset="utf-8" />
|
||||||
|
<meta name="viewport" content="width=device-width, initial-scale=1" />
|
||||||
|
<style>
|
||||||
|
body {
|
||||||
|
margin: 0;
|
||||||
|
padding: 24px;
|
||||||
|
background: #eef2f7;
|
||||||
|
color: #1f2937;
|
||||||
|
font-family: Arial, Helvetica, sans-serif;
|
||||||
|
}
|
||||||
|
.email-shell {
|
||||||
|
max-width: 640px;
|
||||||
|
margin: 0 auto;
|
||||||
|
background: #ffffff;
|
||||||
|
border: 1px solid #dbe3ee;
|
||||||
|
border-radius: 20px;
|
||||||
|
overflow: hidden;
|
||||||
|
box-shadow: 0 12px 30px rgba(15, 23, 42, .08);
|
||||||
|
}
|
||||||
|
.email-body {
|
||||||
|
padding: 28px 36px;
|
||||||
|
}
|
||||||
|
img { max-width: 100%; height: auto; }
|
||||||
|
a { color: #2563eb; }
|
||||||
|
</style>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<div class="email-shell">
|
||||||
|
<div class="email-body">
|
||||||
|
${renderedHtml || '<div style="color:#6b7280">No HTML body provided.</div>'}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</body>
|
||||||
|
</html>`;
|
||||||
|
}
|
||||||
|
|
||||||
|
variablesField.addEventListener('input', renderVariableInputs);
|
||||||
|
subjectField.addEventListener('input', updatePreview);
|
||||||
|
htmlField.addEventListener('input', updatePreview);
|
||||||
|
textField.addEventListener('input', updatePreview);
|
||||||
|
if (previewTab) {
|
||||||
|
previewTab.addEventListener('shown.bs.tab', updatePreview);
|
||||||
|
}
|
||||||
|
|
||||||
|
renderVariableInputs();
|
||||||
|
})();
|
||||||
|
</script>
|
||||||
|
}
|
||||||
</form>
|
</form>
|
||||||
|
|||||||
@@ -14,3 +14,4 @@
|
|||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
@RenderSection("Scripts", required: false)
|
||||||
|
|||||||
@@ -20,7 +20,10 @@
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"Enrich": [ "FromLogContext" ]
|
"Enrich": [ "FromLogContext" ],
|
||||||
|
"Properties": {
|
||||||
|
"Application": "hrynco-notification-service-web"
|
||||||
|
}
|
||||||
},
|
},
|
||||||
"AllowedHosts": "*"
|
"AllowedHosts": "*"
|
||||||
}
|
}
|
||||||
@@ -118,9 +118,10 @@ 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 — constrains width ───────────────── */
|
/* ── Editor wrapper ──────────────────────────────────── */
|
||||||
.editor-wrapper {
|
.editor-wrapper {
|
||||||
max-width: 860px;
|
width: 100%;
|
||||||
|
max-width: none;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* ── Editor card ──────────────────────────────────────── */
|
/* ── Editor card ──────────────────────────────────────── */
|
||||||
@@ -154,6 +155,149 @@ 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 ───────────────────────────── */
|
||||||
|
.template-preview-panel {
|
||||||
|
border: 1px solid #dce3eb;
|
||||||
|
border-radius: .75rem;
|
||||||
|
background: #fff;
|
||||||
|
overflow: hidden;
|
||||||
|
box-shadow: 0 10px 24px rgba(15, 23, 42, .06);
|
||||||
|
}
|
||||||
|
|
||||||
|
.template-preview-panel-header {
|
||||||
|
display: flex;
|
||||||
|
justify-content: space-between;
|
||||||
|
align-items: flex-start;
|
||||||
|
gap: 1rem;
|
||||||
|
padding: 1rem 1.25rem;
|
||||||
|
background: linear-gradient(180deg, #f9fbff 0%, #f3f6fb 100%);
|
||||||
|
border-bottom: 1px solid #e1e7ef;
|
||||||
|
}
|
||||||
|
|
||||||
|
.template-preview-title {
|
||||||
|
font-size: .9rem;
|
||||||
|
font-weight: 700;
|
||||||
|
color: #1f2937;
|
||||||
|
}
|
||||||
|
|
||||||
|
.template-preview-subtitle {
|
||||||
|
font-size: .82rem;
|
||||||
|
color: #6b7280;
|
||||||
|
margin-top: .15rem;
|
||||||
|
}
|
||||||
|
|
||||||
|
.template-preview-body {
|
||||||
|
display: grid;
|
||||||
|
grid-template-columns: minmax(0, 1fr);
|
||||||
|
gap: 1rem;
|
||||||
|
padding: 1rem 1.25rem 1.25rem;
|
||||||
|
}
|
||||||
|
|
||||||
|
.template-preview-source,
|
||||||
|
.template-preview-output {
|
||||||
|
min-width: 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
.template-preview-section-block {
|
||||||
|
padding: 1rem;
|
||||||
|
border: 1px solid #e5ebf2;
|
||||||
|
border-radius: .65rem;
|
||||||
|
background: #fafcff;
|
||||||
|
}
|
||||||
|
|
||||||
|
.template-preview-section-title {
|
||||||
|
font-size: .7rem;
|
||||||
|
font-weight: 700;
|
||||||
|
letter-spacing: .08em;
|
||||||
|
text-transform: uppercase;
|
||||||
|
color: #6b7280;
|
||||||
|
margin-bottom: .5rem;
|
||||||
|
}
|
||||||
|
|
||||||
|
.template-preview-variables {
|
||||||
|
display: grid;
|
||||||
|
grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
|
||||||
|
gap: .75rem 1rem;
|
||||||
|
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 {
|
||||||
|
background: #fff;
|
||||||
|
min-height: calc(1.5em + .45rem + 2px);
|
||||||
|
padding: .2rem .45rem;
|
||||||
|
font-size: .82rem;
|
||||||
|
}
|
||||||
|
|
||||||
|
.template-preview-subject {
|
||||||
|
padding: .75rem 1rem;
|
||||||
|
border: 1px dashed #cfd8e3;
|
||||||
|
border-radius: .5rem;
|
||||||
|
background: #f8fafc;
|
||||||
|
min-height: 3rem;
|
||||||
|
font-weight: 600;
|
||||||
|
color: #111827;
|
||||||
|
white-space: pre-wrap;
|
||||||
|
word-break: break-word;
|
||||||
|
}
|
||||||
|
|
||||||
|
.template-preview-frame {
|
||||||
|
width: 100%;
|
||||||
|
height: 420px;
|
||||||
|
border: 1px solid #cfd8e3;
|
||||||
|
border-radius: .5rem;
|
||||||
|
background: #eef2f7;
|
||||||
|
}
|
||||||
|
|
||||||
|
.template-preview-text {
|
||||||
|
padding: .75rem 1rem;
|
||||||
|
border: 1px solid #cfd8e3;
|
||||||
|
border-radius: .5rem;
|
||||||
|
background: #0f172a;
|
||||||
|
color: #e2e8f0;
|
||||||
|
min-height: 120px;
|
||||||
|
white-space: pre-wrap;
|
||||||
|
word-break: break-word;
|
||||||
|
}
|
||||||
|
|
||||||
|
@media (max-width: 992px) {
|
||||||
|
.template-preview-body {
|
||||||
|
grid-template-columns: 1fr;
|
||||||
|
}
|
||||||
|
|
||||||
|
.template-preview-frame {
|
||||||
|
height: 360px;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/* ── Form-section divider ─────────────────────────────── */
|
/* ── Form-section divider ─────────────────────────────── */
|
||||||
.form-section-title {
|
.form-section-title {
|
||||||
font-size: .68rem;
|
font-size: .68rem;
|
||||||
|
|||||||
+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);
|
||||||
|
}
|
||||||
+1
-1
@@ -1,3 +1,3 @@
|
|||||||
namespace HrynCo.NotificationService.Worker;
|
namespace HrynCo.NotificationService.Worker.Services.EmailProcessing;
|
||||||
|
|
||||||
public record RenderedEmail(string Subject, string HtmlBody, string TextBody);
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -8,4 +8,4 @@ public sealed class AppSettings
|
|||||||
|
|
||||||
public string ConnectionString { get; init; } = string.Empty;
|
public string ConnectionString { get; init; } = string.Empty;
|
||||||
public RabbitMqSettings RabbitMq { get; init; } = null!;
|
public RabbitMqSettings RabbitMq { get; init; } = null!;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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.Services/HrynCo.NotificationService.Services.csproj", "HrynCo.NotificationService.Services/"]
|
COPY ["HrynCo.NotificationService.Worker.Services/HrynCo.NotificationService.Worker.Services.csproj", "HrynCo.NotificationService.Worker.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"
|
||||||
@@ -28,4 +28,4 @@ RUN dotnet publish "./HrynCo.NotificationService.Worker.csproj" -c $BUILD_CONFIG
|
|||||||
FROM base AS final
|
FROM base AS final
|
||||||
WORKDIR /app
|
WORKDIR /app
|
||||||
COPY --from=publish /app/publish .
|
COPY --from=publish /app/publish .
|
||||||
ENTRYPOINT ["dotnet", "HrynCo.NotificationService.Worker.dll"]
|
ENTRYPOINT ["dotnet", "HrynCo.NotificationService.Worker.dll"]
|
||||||
|
|||||||
@@ -10,7 +10,6 @@
|
|||||||
<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" />
|
||||||
@@ -19,7 +18,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>
|
||||||
|
|||||||
@@ -1,8 +1,7 @@
|
|||||||
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);
|
||||||
|
|
||||||
@@ -14,7 +13,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.AddNotificationServices();
|
builder.Services.AddNotificationWorkerServices();
|
||||||
|
|
||||||
builder.Services.Configure<RabbitMqSettings>(
|
builder.Services.Configure<RabbitMqSettings>(
|
||||||
builder.Configuration.GetSection($"{AppSettings.SectionName}:RabbitMq"));
|
builder.Configuration.GetSection($"{AppSettings.SectionName}:RabbitMq"));
|
||||||
@@ -22,4 +21,4 @@ builder.Services.Configure<RabbitMqSettings>(
|
|||||||
builder.Services.AddHostedService<SendEmailConsumer>();
|
builder.Services.AddHostedService<SendEmailConsumer>();
|
||||||
|
|
||||||
var host = builder.Build();
|
var host = builder.Build();
|
||||||
host.Run();
|
host.Run();
|
||||||
|
|||||||
@@ -1,174 +1,32 @@
|
|||||||
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 MediatR;
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
using Microsoft.Extensions.Options;
|
using Microsoft.Extensions.Options;
|
||||||
using RabbitMQ.Client;
|
using HrynCo.NotificationService.Worker.Services.EmailProcessing;
|
||||||
|
|
||||||
public sealed class SendEmailConsumer(
|
public sealed class SendEmailConsumer : RabbitMqConsumerBase<SendEmailMessage, SendEmailMessageData>
|
||||||
IOptionsMonitor<RabbitMqSettings> options,
|
|
||||||
IEmailChannelRepository channelRepository,
|
|
||||||
IEmailTemplateRepository templateRepository,
|
|
||||||
IEmailChannelUsageRepository usageRepository,
|
|
||||||
IMediator mediator,
|
|
||||||
AppSettings appSettings,
|
|
||||||
ILogger<SendEmailConsumer> logger)
|
|
||||||
: RabbitMqConsumerBase<SendEmailMessage, SendEmailMessageData>(options, logger)
|
|
||||||
{
|
{
|
||||||
|
private readonly IServiceScopeFactory _scopeFactory;
|
||||||
|
|
||||||
|
public SendEmailConsumer(
|
||||||
|
IOptionsMonitor<RabbitMqSettings> options,
|
||||||
|
IServiceScopeFactory scopeFactory,
|
||||||
|
ILogger<SendEmailConsumer> logger)
|
||||||
|
: base(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)
|
||||||
{
|
{
|
||||||
var data = message.Data;
|
using var scope = _scopeFactory.CreateScope();
|
||||||
|
var service = scope.ServiceProvider.GetRequiredService<ISendEmailService>();
|
||||||
logger.LogInformation(
|
await service.ProcessAsync(message, cancellationToken);
|
||||||
"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,4 +1,14 @@
|
|||||||
{
|
{
|
||||||
|
"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",
|
||||||
@@ -7,6 +17,20 @@
|
|||||||
"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"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -27,6 +27,9 @@
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"Enrich": [ "FromLogContext" ]
|
"Enrich": [ "FromLogContext" ],
|
||||||
|
"Properties": {
|
||||||
|
"Application": "hrynco-notification-service-worker"
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -20,5 +20,6 @@
|
|||||||
<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>
|
||||||
|
|||||||
@@ -1,8 +1,5 @@
|
|||||||
<?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>
|
||||||
|
|||||||
@@ -1 +1,17 @@
|
|||||||
# 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]
|
||||||
|
```
|
||||||
|
|||||||
@@ -0,0 +1,9 @@
|
|||||||
|
DB_NAME=notification_service
|
||||||
|
DB_USER=postgres
|
||||||
|
DB_PASS=postgres
|
||||||
|
VOLUME_PREFIX=ns-dev
|
||||||
|
RABBITMQ_USER=guest
|
||||||
|
RABBITMQ_PASSWORD=guest
|
||||||
|
RABBITMQ_AMQP_PORT=5672
|
||||||
|
RABBITMQ_MANAGEMENT_PORT=15672
|
||||||
|
WEB_PORT=5200
|
||||||
@@ -5,7 +5,7 @@ services:
|
|||||||
environment:
|
environment:
|
||||||
- App__ConnectionString=Host=db;Port=5432;Database=notification_service;Username=postgres;Password=postgres
|
- App__ConnectionString=Host=db;Port=5432;Database=notification_service;Username=postgres;Password=postgres
|
||||||
|
|
||||||
api:
|
web:
|
||||||
environment:
|
environment:
|
||||||
- ASPNETCORE_ENVIRONMENT=Development
|
- ASPNETCORE_ENVIRONMENT=Development
|
||||||
- App__ConnectionString=Host=db;Port=5432;Database=notification_service;Username=postgres;Password=postgres
|
- App__ConnectionString=Host=db;Port=5432;Database=notification_service;Username=postgres;Password=postgres
|
||||||
@@ -26,31 +26,22 @@ services:
|
|||||||
condition: service_healthy
|
condition: service_healthy
|
||||||
|
|
||||||
rabbitmq:
|
rabbitmq:
|
||||||
image: rabbitmq:4-management-alpine
|
|
||||||
environment:
|
environment:
|
||||||
RABBITMQ_DEFAULT_USER: guest
|
RABBITMQ_DEFAULT_USER: guest
|
||||||
RABBITMQ_DEFAULT_PASS: guest
|
RABBITMQ_DEFAULT_PASS: guest
|
||||||
ports:
|
ports:
|
||||||
- "5672:5672"
|
- "5672:5672"
|
||||||
- "15672:15672"
|
- "15672:15672"
|
||||||
healthcheck:
|
networks:
|
||||||
test: ["CMD", "rabbitmq-diagnostics", "ping"]
|
- internal
|
||||||
interval: 10s
|
|
||||||
timeout: 5s
|
|
||||||
retries: 5
|
|
||||||
volumes:
|
|
||||||
- notification_rabbitmq:/var/lib/rabbitmq
|
|
||||||
|
|
||||||
db:
|
db:
|
||||||
image: postgres:17
|
|
||||||
environment:
|
|
||||||
POSTGRES_DB: notification_service
|
|
||||||
POSTGRES_USER: postgres
|
|
||||||
POSTGRES_PASSWORD: postgres
|
|
||||||
ports:
|
ports:
|
||||||
- "5433:5432"
|
- "5433:5432"
|
||||||
volumes:
|
volumes:
|
||||||
- notification_db:/var/lib/postgresql/data
|
- pgdata:/var/lib/postgresql/data
|
||||||
|
networks:
|
||||||
|
- internal
|
||||||
|
|
||||||
seq:
|
seq:
|
||||||
image: datalust/seq:2024
|
image: datalust/seq:2024
|
||||||
@@ -60,9 +51,14 @@ services:
|
|||||||
ports:
|
ports:
|
||||||
- "5342:80"
|
- "5342:80"
|
||||||
volumes:
|
volumes:
|
||||||
- notification_seq:/data
|
- seq_data:/data
|
||||||
|
networks:
|
||||||
|
- internal
|
||||||
|
|
||||||
volumes:
|
volumes:
|
||||||
notification_db:
|
pgdata:
|
||||||
notification_seq:
|
name: ns-dev-pgdata
|
||||||
notification_rabbitmq:
|
rabbitmq_data:
|
||||||
|
name: ns-dev-rabbitmq-data
|
||||||
|
seq_data:
|
||||||
|
name: ns-dev-seq
|
||||||
@@ -0,0 +1,37 @@
|
|||||||
|
services:
|
||||||
|
migrator:
|
||||||
|
build: {}
|
||||||
|
image: registry.grynco.com.ua/hrynco.notification-service.migrator:${MIGRATOR_IMAGE_TAG:?MIGRATOR_IMAGE_TAG is required}
|
||||||
|
|
||||||
|
web:
|
||||||
|
build: {}
|
||||||
|
image: registry.grynco.com.ua/hrynco.notification-service.web:${WEB_IMAGE_TAG:?WEB_IMAGE_TAG is required}
|
||||||
|
ports:
|
||||||
|
- "${WEB_PORT:?WEB_PORT is required}:8080"
|
||||||
|
environment:
|
||||||
|
- Serilog__WriteTo__1__Args__serverUrl=${SEQ_URL:-}
|
||||||
|
restart: always
|
||||||
|
|
||||||
|
worker:
|
||||||
|
build: {}
|
||||||
|
image: registry.grynco.com.ua/hrynco.notification-service.worker:${WORKER_IMAGE_TAG:?WORKER_IMAGE_TAG is required}
|
||||||
|
environment:
|
||||||
|
- Serilog__WriteTo__1__Args__serverUrl=${SEQ_URL:-}
|
||||||
|
restart: always
|
||||||
|
|
||||||
|
rabbitmq:
|
||||||
|
restart: always
|
||||||
|
networks:
|
||||||
|
- internal
|
||||||
|
- hrynco-services
|
||||||
|
|
||||||
|
db:
|
||||||
|
ports:
|
||||||
|
- "${DB_PORT:?DB_PORT is required}:5432"
|
||||||
|
restart: always
|
||||||
|
|
||||||
|
networks:
|
||||||
|
internal: {}
|
||||||
|
hrynco-services:
|
||||||
|
external: true
|
||||||
|
name: hrynco-services
|
||||||
@@ -6,24 +6,28 @@ services:
|
|||||||
context: ../..
|
context: ../..
|
||||||
dockerfile: HrynCo.NotificationService.Migrator/Dockerfile
|
dockerfile: HrynCo.NotificationService.Migrator/Dockerfile
|
||||||
environment:
|
environment:
|
||||||
- App__ConnectionString=${CONNECTION_STRING}
|
- App__ConnectionString=Host=db;Port=5432;Database=${DB_NAME:?DB_NAME is required};Username=${DB_USER:?DB_USER is required};Password=${DB_PASS:?DB_PASS is required}
|
||||||
depends_on:
|
depends_on:
|
||||||
db:
|
db:
|
||||||
condition: service_started
|
condition: service_healthy
|
||||||
|
networks:
|
||||||
|
- internal
|
||||||
restart: "no"
|
restart: "no"
|
||||||
|
|
||||||
api:
|
web:
|
||||||
build:
|
build:
|
||||||
context: ../..
|
context: ../..
|
||||||
dockerfile: HrynCo.NotificationService.Web/Dockerfile
|
dockerfile: HrynCo.NotificationService.Web/Dockerfile
|
||||||
environment:
|
environment:
|
||||||
- ASPNETCORE_ENVIRONMENT=Production
|
- ASPNETCORE_ENVIRONMENT=Production
|
||||||
- App__ConnectionString=${CONNECTION_STRING}
|
- App__ConnectionString=Host=db;Port=5432;Database=${DB_NAME:?DB_NAME is required};Username=${DB_USER:?DB_USER is required};Password=${DB_PASS:?DB_PASS is required}
|
||||||
depends_on:
|
depends_on:
|
||||||
db:
|
db:
|
||||||
condition: service_started
|
condition: service_healthy
|
||||||
migrator:
|
migrator:
|
||||||
condition: service_completed_successfully
|
condition: service_completed_successfully
|
||||||
|
networks:
|
||||||
|
- internal
|
||||||
|
|
||||||
worker:
|
worker:
|
||||||
build:
|
build:
|
||||||
@@ -31,25 +35,61 @@ services:
|
|||||||
dockerfile: HrynCo.NotificationService.Worker/Dockerfile
|
dockerfile: HrynCo.NotificationService.Worker/Dockerfile
|
||||||
environment:
|
environment:
|
||||||
- DOTNET_ENVIRONMENT=Production
|
- DOTNET_ENVIRONMENT=Production
|
||||||
- App__ConnectionString=${CONNECTION_STRING}
|
- App__ConnectionString=Host=db;Port=5432;Database=${DB_NAME:?DB_NAME is required};Username=${DB_USER:?DB_USER is required};Password=${DB_PASS:?DB_PASS is required}
|
||||||
- App__RabbitMq__Host=rabbitmq
|
- App__RabbitMq__Host=rabbitmq
|
||||||
- App__RabbitMq__User=${RABBITMQ_USER:-guest}
|
- App__RabbitMq__Port=5672
|
||||||
- App__RabbitMq__Password=${RABBITMQ_PASSWORD:-guest}
|
- App__RabbitMq__User=${RABBITMQ_USER:?RABBITMQ_USER is required}
|
||||||
|
- App__RabbitMq__Password=${RABBITMQ_PASSWORD:?RABBITMQ_PASSWORD is required}
|
||||||
depends_on:
|
depends_on:
|
||||||
db:
|
db:
|
||||||
condition: service_started
|
condition: service_healthy
|
||||||
migrator:
|
migrator:
|
||||||
condition: service_completed_successfully
|
condition: service_completed_successfully
|
||||||
rabbitmq:
|
rabbitmq:
|
||||||
condition: service_healthy
|
condition: service_healthy
|
||||||
|
networks:
|
||||||
|
- internal
|
||||||
|
|
||||||
rabbitmq:
|
rabbitmq:
|
||||||
image: rabbitmq:4-management-alpine
|
image: rabbitmq:4-management-alpine
|
||||||
environment:
|
environment:
|
||||||
RABBITMQ_DEFAULT_USER: ${RABBITMQ_USER:-guest}
|
RABBITMQ_DEFAULT_USER: ${RABBITMQ_USER:?RABBITMQ_USER is required}
|
||||||
RABBITMQ_DEFAULT_PASS: ${RABBITMQ_PASSWORD:-guest}
|
RABBITMQ_DEFAULT_PASS: ${RABBITMQ_PASSWORD:?RABBITMQ_PASSWORD is required}
|
||||||
|
ports:
|
||||||
|
- "${RABBITMQ_AMQP_PORT:?RABBITMQ_AMQP_PORT is required}:5672"
|
||||||
|
- "${RABBITMQ_MANAGEMENT_PORT:?RABBITMQ_MANAGEMENT_PORT is required}:15672"
|
||||||
|
volumes:
|
||||||
|
- rabbitmq_data:/var/lib/rabbitmq
|
||||||
|
networks:
|
||||||
|
- internal
|
||||||
healthcheck:
|
healthcheck:
|
||||||
test: ["CMD", "rabbitmq-diagnostics", "ping"]
|
test: ["CMD", "rabbitmq-diagnostics", "ping"]
|
||||||
interval: 10s
|
interval: 10s
|
||||||
timeout: 5s
|
timeout: 5s
|
||||||
retries: 5
|
retries: 5
|
||||||
|
|
||||||
|
db:
|
||||||
|
image: postgres:17
|
||||||
|
environment:
|
||||||
|
- POSTGRES_DB=${DB_NAME:?DB_NAME is required}
|
||||||
|
- POSTGRES_USER=${DB_USER:?DB_USER is required}
|
||||||
|
- POSTGRES_PASSWORD=${DB_PASS:?DB_PASS is required}
|
||||||
|
volumes:
|
||||||
|
- pgdata:/var/lib/postgresql/data
|
||||||
|
networks:
|
||||||
|
- internal
|
||||||
|
healthcheck:
|
||||||
|
test: ["CMD-SHELL", "pg_isready -U ${DB_USER}"]
|
||||||
|
interval: 5s
|
||||||
|
timeout: 5s
|
||||||
|
retries: 10
|
||||||
|
|
||||||
|
volumes:
|
||||||
|
pgdata:
|
||||||
|
name: ${VOLUME_PREFIX:?VOLUME_PREFIX is required}-pgdata
|
||||||
|
rabbitmq_data:
|
||||||
|
name: ${VOLUME_PREFIX:?VOLUME_PREFIX is required}-rabbitmq-data
|
||||||
|
|
||||||
|
networks:
|
||||||
|
internal:
|
||||||
|
driver: bridge
|
||||||
Reference in New Issue
Block a user