merge: IT-628 RabbitMQ worker, contracts, usage UI in channels screen
This commit is contained in:
@@ -0,0 +1,9 @@
|
||||
**/.git
|
||||
**/.vs
|
||||
**/.idea
|
||||
**/bin
|
||||
**/obj
|
||||
**/*.user
|
||||
**/*.suo
|
||||
**/TestResults
|
||||
NuGet.Config
|
||||
+482
@@ -0,0 +1,482 @@
|
||||
## Ignore Visual Studio temporary files, build results, and
|
||||
## files generated by popular Visual Studio add-ons.
|
||||
##
|
||||
## Get latest from `dotnet new gitignore`
|
||||
|
||||
# dotenv files
|
||||
.env
|
||||
|
||||
# User-specific files
|
||||
*.rsuser
|
||||
*.suo
|
||||
*.user
|
||||
*.userosscache
|
||||
*.sln.docstates
|
||||
|
||||
# User-specific files (MonoDevelop/Xamarin Studio)
|
||||
*.userprefs
|
||||
|
||||
# Mono auto generated files
|
||||
mono_crash.*
|
||||
|
||||
# Build results
|
||||
[Dd]ebug/
|
||||
[Dd]ebugPublic/
|
||||
[Rr]elease/
|
||||
[Rr]eleases/
|
||||
x64/
|
||||
x86/
|
||||
[Ww][Ii][Nn]32/
|
||||
[Aa][Rr][Mm]/
|
||||
[Aa][Rr][Mm]64/
|
||||
bld/
|
||||
[Bb]in/
|
||||
[Oo]bj/
|
||||
[Ll]og/
|
||||
[Ll]ogs/
|
||||
|
||||
# Visual Studio 2015/2017 cache/options directory
|
||||
.vs/
|
||||
# Uncomment if you have tasks that create the project's static files in wwwroot
|
||||
#wwwroot/
|
||||
|
||||
# Visual Studio 2017 auto generated files
|
||||
Generated\ Files/
|
||||
|
||||
# MSTest test Results
|
||||
[Tt]est[Rr]esult*/
|
||||
[Bb]uild[Ll]og.*
|
||||
|
||||
# NUnit
|
||||
*.VisualState.xml
|
||||
TestResult.xml
|
||||
nunit-*.xml
|
||||
|
||||
# Build Results of an ATL Project
|
||||
[Dd]ebugPS/
|
||||
[Rr]eleasePS/
|
||||
dlldata.c
|
||||
|
||||
# Benchmark Results
|
||||
BenchmarkDotNet.Artifacts/
|
||||
|
||||
# .NET
|
||||
project.lock.json
|
||||
project.fragment.lock.json
|
||||
artifacts/
|
||||
|
||||
# Tye
|
||||
.tye/
|
||||
|
||||
# ASP.NET Scaffolding
|
||||
ScaffoldingReadMe.txt
|
||||
|
||||
# StyleCop
|
||||
StyleCopReport.xml
|
||||
|
||||
# Files built by Visual Studio
|
||||
*_i.c
|
||||
*_p.c
|
||||
*_h.h
|
||||
*.ilk
|
||||
*.meta
|
||||
*.obj
|
||||
*.iobj
|
||||
*.pch
|
||||
*.pdb
|
||||
*.ipdb
|
||||
*.pgc
|
||||
*.pgd
|
||||
*.rsp
|
||||
# but not Directory.Build.rsp, as it configures directory-level build defaults
|
||||
!Directory.Build.rsp
|
||||
*.sbr
|
||||
*.tlb
|
||||
*.tli
|
||||
*.tlh
|
||||
*.tmp
|
||||
*.tmp_proj
|
||||
*_wpftmp.csproj
|
||||
*.log
|
||||
*.tlog
|
||||
*.vspscc
|
||||
*.vssscc
|
||||
.builds
|
||||
*.pidb
|
||||
*.svclog
|
||||
*.scc
|
||||
|
||||
# Chutzpah Test files
|
||||
_Chutzpah*
|
||||
|
||||
# Visual C++ cache files
|
||||
ipch/
|
||||
*.aps
|
||||
*.ncb
|
||||
*.opendb
|
||||
*.opensdf
|
||||
*.sdf
|
||||
*.cachefile
|
||||
*.VC.db
|
||||
*.VC.VC.opendb
|
||||
|
||||
# Visual Studio profiler
|
||||
*.psess
|
||||
*.vsp
|
||||
*.vspx
|
||||
*.sap
|
||||
|
||||
# Visual Studio Trace Files
|
||||
*.e2e
|
||||
|
||||
# TFS 2012 Local Workspace
|
||||
$tf/
|
||||
|
||||
# Guidance Automation Toolkit
|
||||
*.gpState
|
||||
|
||||
# ReSharper is a .NET coding add-in
|
||||
_ReSharper*/
|
||||
*.[Rr]e[Ss]harper
|
||||
*.DotSettings.user
|
||||
|
||||
# TeamCity is a build add-in
|
||||
_TeamCity*
|
||||
|
||||
# DotCover is a Code Coverage Tool
|
||||
*.dotCover
|
||||
|
||||
# AxoCover is a Code Coverage Tool
|
||||
.axoCover/*
|
||||
!.axoCover/settings.json
|
||||
|
||||
# Coverlet is a free, cross platform Code Coverage Tool
|
||||
coverage*.json
|
||||
coverage*.xml
|
||||
coverage*.info
|
||||
|
||||
# Visual Studio code coverage results
|
||||
*.coverage
|
||||
*.coveragexml
|
||||
|
||||
# NCrunch
|
||||
_NCrunch_*
|
||||
.*crunch*.local.xml
|
||||
nCrunchTemp_*
|
||||
|
||||
# MightyMoose
|
||||
*.mm.*
|
||||
AutoTest.Net/
|
||||
|
||||
# Web workbench (sass)
|
||||
.sass-cache/
|
||||
|
||||
# Installshield output folder
|
||||
[Ee]xpress/
|
||||
|
||||
# DocProject is a documentation generator add-in
|
||||
DocProject/buildhelp/
|
||||
DocProject/Help/*.HxT
|
||||
DocProject/Help/*.HxC
|
||||
DocProject/Help/*.hhc
|
||||
DocProject/Help/*.hhk
|
||||
DocProject/Help/*.hhp
|
||||
DocProject/Help/Html2
|
||||
DocProject/Help/html
|
||||
|
||||
# Click-Once directory
|
||||
publish/
|
||||
|
||||
# Publish Web Output
|
||||
*.[Pp]ublish.xml
|
||||
*.azurePubxml
|
||||
# Note: Comment the next line if you want to checkin your web deploy settings,
|
||||
# but database connection strings (with potential passwords) will be unencrypted
|
||||
*.pubxml
|
||||
*.publishproj
|
||||
|
||||
# Microsoft Azure Web App publish settings. Comment the next line if you want to
|
||||
# checkin your Azure Web App publish settings, but sensitive information contained
|
||||
# in these scripts will be unencrypted
|
||||
PublishScripts/
|
||||
|
||||
# NuGet Packages
|
||||
*.nupkg
|
||||
# NuGet Symbol Packages
|
||||
*.snupkg
|
||||
# The packages folder can be ignored because of Package Restore
|
||||
**/[Pp]ackages/*
|
||||
# except build/, which is used as an MSBuild target.
|
||||
!**/[Pp]ackages/build/
|
||||
# Uncomment if necessary however generally it will be regenerated when needed
|
||||
#!**/[Pp]ackages/repositories.config
|
||||
# NuGet v3's project.json files produces more ignorable files
|
||||
*.nuget.props
|
||||
*.nuget.targets
|
||||
|
||||
# Microsoft Azure Build Output
|
||||
csx/
|
||||
*.build.csdef
|
||||
|
||||
# Microsoft Azure Emulator
|
||||
ecf/
|
||||
rcf/
|
||||
|
||||
# Windows Store app package directories and files
|
||||
AppPackages/
|
||||
BundleArtifacts/
|
||||
Package.StoreAssociation.xml
|
||||
_pkginfo.txt
|
||||
*.appx
|
||||
*.appxbundle
|
||||
*.appxupload
|
||||
|
||||
# Visual Studio cache files
|
||||
# files ending in .cache can be ignored
|
||||
*.[Cc]ache
|
||||
# but keep track of directories ending in .cache
|
||||
!?*.[Cc]ache/
|
||||
|
||||
# Others
|
||||
ClientBin/
|
||||
~$*
|
||||
*~
|
||||
*.dbmdl
|
||||
*.dbproj.schemaview
|
||||
*.jfm
|
||||
*.pfx
|
||||
*.publishsettings
|
||||
orleans.codegen.cs
|
||||
|
||||
# Including strong name files can present a security risk
|
||||
# (https://github.com/github/gitignore/pull/2483#issue-259490424)
|
||||
#*.snk
|
||||
|
||||
# Since there are multiple workflows, uncomment next line to ignore bower_components
|
||||
# (https://github.com/github/gitignore/pull/1529#issuecomment-104372622)
|
||||
#bower_components/
|
||||
|
||||
# RIA/Silverlight projects
|
||||
Generated_Code/
|
||||
|
||||
# Backup & report files from converting an old project file
|
||||
# to a newer Visual Studio version. Backup files are not needed,
|
||||
# because we have git ;-)
|
||||
_UpgradeReport_Files/
|
||||
Backup*/
|
||||
UpgradeLog*.XML
|
||||
UpgradeLog*.htm
|
||||
ServiceFabricBackup/
|
||||
*.rptproj.bak
|
||||
|
||||
# SQL Server files
|
||||
*.mdf
|
||||
*.ldf
|
||||
*.ndf
|
||||
|
||||
# Business Intelligence projects
|
||||
*.rdl.data
|
||||
*.bim.layout
|
||||
*.bim_*.settings
|
||||
*.rptproj.rsuser
|
||||
*- [Bb]ackup.rdl
|
||||
*- [Bb]ackup ([0-9]).rdl
|
||||
*- [Bb]ackup ([0-9][0-9]).rdl
|
||||
|
||||
# Microsoft Fakes
|
||||
FakesAssemblies/
|
||||
|
||||
# GhostDoc plugin setting file
|
||||
*.GhostDoc.xml
|
||||
|
||||
# Node.js Tools for Visual Studio
|
||||
.ntvs_analysis.dat
|
||||
node_modules/
|
||||
|
||||
# Visual Studio 6 build log
|
||||
*.plg
|
||||
|
||||
# Visual Studio 6 workspace options file
|
||||
*.opt
|
||||
|
||||
# Visual Studio 6 auto-generated workspace file (contains which files were open etc.)
|
||||
*.vbw
|
||||
|
||||
# Visual Studio 6 auto-generated project file (contains which files were open etc.)
|
||||
*.vbp
|
||||
|
||||
# Visual Studio 6 workspace and project file (working project files containing files to include in project)
|
||||
*.dsw
|
||||
*.dsp
|
||||
|
||||
# Visual Studio 6 technical files
|
||||
*.ncb
|
||||
*.aps
|
||||
|
||||
# Visual Studio LightSwitch build output
|
||||
**/*.HTMLClient/GeneratedArtifacts
|
||||
**/*.DesktopClient/GeneratedArtifacts
|
||||
**/*.DesktopClient/ModelManifest.xml
|
||||
**/*.Server/GeneratedArtifacts
|
||||
**/*.Server/ModelManifest.xml
|
||||
_Pvt_Extensions
|
||||
|
||||
# Paket dependency manager
|
||||
.paket/paket.exe
|
||||
paket-files/
|
||||
|
||||
# FAKE - F# Make
|
||||
.fake/
|
||||
|
||||
# CodeRush personal settings
|
||||
.cr/personal
|
||||
|
||||
# Python Tools for Visual Studio (PTVS)
|
||||
__pycache__/
|
||||
*.pyc
|
||||
|
||||
# Cake - Uncomment if you are using it
|
||||
# tools/**
|
||||
# !tools/packages.config
|
||||
|
||||
# Tabs Studio
|
||||
*.tss
|
||||
|
||||
# Telerik's JustMock configuration file
|
||||
*.jmconfig
|
||||
|
||||
# BizTalk build output
|
||||
*.btp.cs
|
||||
*.btm.cs
|
||||
*.odx.cs
|
||||
*.xsd.cs
|
||||
|
||||
# OpenCover UI analysis results
|
||||
OpenCover/
|
||||
|
||||
# Azure Stream Analytics local run output
|
||||
ASALocalRun/
|
||||
|
||||
# MSBuild Binary and Structured Log
|
||||
*.binlog
|
||||
|
||||
# NVidia Nsight GPU debugger configuration file
|
||||
*.nvuser
|
||||
|
||||
# MFractors (Xamarin productivity tool) working folder
|
||||
.mfractor/
|
||||
|
||||
# Local History for Visual Studio
|
||||
.localhistory/
|
||||
|
||||
# Visual Studio History (VSHistory) files
|
||||
.vshistory/
|
||||
|
||||
# BeatPulse healthcheck temp database
|
||||
healthchecksdb
|
||||
|
||||
# Backup folder for Package Reference Convert tool in Visual Studio 2017
|
||||
MigrationBackup/
|
||||
|
||||
# Ionide (cross platform F# VS Code tools) working folder
|
||||
.ionide/
|
||||
|
||||
# Fody - auto-generated XML schema
|
||||
FodyWeavers.xsd
|
||||
|
||||
# VS Code files for those working on multiple tools
|
||||
.vscode/*
|
||||
!.vscode/settings.json
|
||||
!.vscode/tasks.json
|
||||
!.vscode/launch.json
|
||||
!.vscode/extensions.json
|
||||
*.code-workspace
|
||||
|
||||
# Local History for Visual Studio Code
|
||||
.history/
|
||||
|
||||
# Windows Installer files from build outputs
|
||||
*.cab
|
||||
*.msi
|
||||
*.msix
|
||||
*.msm
|
||||
*.msp
|
||||
|
||||
# JetBrains Rider
|
||||
*.sln.iml
|
||||
.idea/
|
||||
|
||||
##
|
||||
## Visual studio for Mac
|
||||
##
|
||||
|
||||
|
||||
# globs
|
||||
Makefile.in
|
||||
*.userprefs
|
||||
*.usertasks
|
||||
config.make
|
||||
config.status
|
||||
aclocal.m4
|
||||
install-sh
|
||||
autom4te.cache/
|
||||
*.tar.gz
|
||||
tarballs/
|
||||
test-results/
|
||||
|
||||
# content below from: https://github.com/github/gitignore/blob/main/Global/macOS.gitignore
|
||||
# General
|
||||
.DS_Store
|
||||
.AppleDouble
|
||||
.LSOverride
|
||||
|
||||
# Icon must end with two \r
|
||||
Icon
|
||||
|
||||
|
||||
# Thumbnails
|
||||
._*
|
||||
|
||||
# Files that might appear in the root of a volume
|
||||
.DocumentRevisions-V100
|
||||
.fseventsd
|
||||
.Spotlight-V100
|
||||
.TemporaryItems
|
||||
.Trashes
|
||||
.VolumeIcon.icns
|
||||
.com.apple.timemachine.donotpresent
|
||||
|
||||
# Directories potentially created on remote AFP share
|
||||
.AppleDB
|
||||
.AppleDesktop
|
||||
Network Trash Folder
|
||||
Temporary Items
|
||||
.apdisk
|
||||
|
||||
# content below from: https://github.com/github/gitignore/blob/main/Global/Windows.gitignore
|
||||
# Windows thumbnail cache files
|
||||
Thumbs.db
|
||||
ehthumbs.db
|
||||
ehthumbs_vista.db
|
||||
|
||||
# Dump file
|
||||
*.stackdump
|
||||
|
||||
# Folder config file
|
||||
[Dd]esktop.ini
|
||||
|
||||
# Recycle Bin used on file shares
|
||||
$RECYCLE.BIN/
|
||||
|
||||
# Windows Installer files
|
||||
*.cab
|
||||
*.msi
|
||||
*.msix
|
||||
*.msm
|
||||
*.msp
|
||||
|
||||
# Windows shortcuts
|
||||
*.lnk
|
||||
|
||||
# Vim temporary swap files
|
||||
*.swp
|
||||
@@ -0,0 +1,5 @@
|
||||
<Project>
|
||||
<PropertyGroup>
|
||||
<ManagePackageVersionsCentrally>true</ManagePackageVersionsCentrally>
|
||||
</PropertyGroup>
|
||||
</Project>
|
||||
@@ -0,0 +1,44 @@
|
||||
<Project>
|
||||
|
||||
<ItemGroup>
|
||||
<!-- Entity Framework Core -->
|
||||
<PackageVersion Include="Microsoft.EntityFrameworkCore" Version="9.0.5" />
|
||||
<PackageVersion Include="Microsoft.EntityFrameworkCore.Design" Version="9.0.5" />
|
||||
<PackageVersion Include="Microsoft.EntityFrameworkCore.Relational" Version="9.0.5" />
|
||||
<PackageVersion Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="9.0.4" />
|
||||
|
||||
<!-- MediatR -->
|
||||
<PackageVersion Include="MediatR" Version="12.4.1" />
|
||||
<PackageVersion Include="MediatR.Extensions.Microsoft.DependencyInjection" Version="11.1.0" />
|
||||
|
||||
<!-- Microsoft.Extensions -->
|
||||
<PackageVersion Include="Microsoft.Extensions.Hosting" Version="10.0.6" />
|
||||
<PackageVersion Include="Microsoft.Extensions.Hosting.Abstractions" Version="10.0.6" />
|
||||
<PackageVersion Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="10.0.6" />
|
||||
<PackageVersion Include="Microsoft.Extensions.Logging.Abstractions" Version="10.0.6" />
|
||||
<PackageVersion Include="Scalar.AspNetCore" Version="2.14.9" />
|
||||
<PackageVersion Include="Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation" Version="10.0.6" />
|
||||
<PackageVersion Include="Microsoft.AspNetCore.OpenApi" Version="10.0.6" />
|
||||
|
||||
<!-- Serilog -->
|
||||
<PackageVersion Include="Serilog" Version="4.2.0" />
|
||||
<PackageVersion Include="Serilog.AspNetCore" Version="9.0.0" />
|
||||
<PackageVersion Include="Serilog.Extensions.Hosting" Version="9.0.0" />
|
||||
<PackageVersion Include="Serilog.Settings.Configuration" 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 -->
|
||||
<PackageVersion Include="HrynCo.Common" Version="1.0.0" />
|
||||
<PackageVersion Include="HrynCo.RabbitMq" Version="1.0.14" />
|
||||
|
||||
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="17.14.1" />
|
||||
<PackageVersion Include="coverlet.collector" Version="6.0.4" />
|
||||
<PackageVersion Include="xunit" Version="2.9.3" />
|
||||
<PackageVersion Include="xunit.runner.visualstudio" Version="3.1.4" />
|
||||
<PackageVersion Include="NSubstitute" Version="5.3.0" />
|
||||
<PackageVersion Include="Testcontainers.PostgreSql" Version="4.6.0" />
|
||||
<PackageVersion Include="Microsoft.AspNetCore.Mvc.Testing" Version="9.0.5" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
@@ -0,0 +1,13 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net10.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="HrynCo.RabbitMq" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
@@ -0,0 +1,14 @@
|
||||
namespace HrynCo.NotificationService.Contracts.Messages;
|
||||
|
||||
public record NotificationResultData
|
||||
{
|
||||
public required string ServiceName { get; init; }
|
||||
public required string RecipientEmail { get; init; }
|
||||
public required string TemplateKey { get; init; }
|
||||
public required DateTimeOffset Timestamp { get; init; }
|
||||
|
||||
/// <summary>Null when delivery succeeded; contains error details on failure.</summary>
|
||||
public string? ErrorMessage { get; init; }
|
||||
|
||||
public bool IsSuccess => ErrorMessage is null;
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
namespace HrynCo.NotificationService.Contracts.Messages;
|
||||
|
||||
using Hrynco.RabbitMq;
|
||||
|
||||
public record NotificationResultMessage : IRabbitMqMessage<NotificationResultData>
|
||||
{
|
||||
public CorrelationContext CorrelationContext { get; set; } = null!;
|
||||
public NotificationResultData Data { get; set; } = null!;
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
namespace HrynCo.NotificationService.Contracts.Messages;
|
||||
|
||||
using Hrynco.RabbitMq;
|
||||
|
||||
public record SendEmailMessage : IRabbitMqMessage<SendEmailMessageData>
|
||||
{
|
||||
public CorrelationContext CorrelationContext { get; set; } = default!;
|
||||
public SendEmailMessageData Data { get; set; } = default!;
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
namespace HrynCo.NotificationService.Contracts.Messages;
|
||||
|
||||
public record SendEmailMessageData
|
||||
{
|
||||
public required string ServiceName { get; init; }
|
||||
public required string TemplateKey { get; init; }
|
||||
public required string RecipientEmail { get; init; }
|
||||
public required string RecipientName { get; init; }
|
||||
public required IReadOnlyDictionary<string, string> Variables { get; init; }
|
||||
public string? LanguageCode { get; init; }
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
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;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
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; }
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net10.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
</Project>
|
||||
@@ -0,0 +1,7 @@
|
||||
namespace HrynCo.NotificationService.DAL.Abstract;
|
||||
|
||||
public interface ITransaction : IAsyncDisposable
|
||||
{
|
||||
Task CommitAsync(CancellationToken cancellationToken = default);
|
||||
Task RollbackAsync(CancellationToken cancellationToken = default);
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
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);
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
namespace HrynCo.NotificationService.DAL.Abstract.Providers;
|
||||
|
||||
public record ChannelWithUsage(
|
||||
EmailChannel Channel,
|
||||
int DailySent,
|
||||
int MonthlySent);
|
||||
@@ -0,0 +1,15 @@
|
||||
using HrynCo.NotificationService.DAL.Abstract.Entities;
|
||||
|
||||
namespace HrynCo.NotificationService.DAL.Abstract.Providers;
|
||||
|
||||
public class EmailChannel : Entity
|
||||
{
|
||||
public required string ServiceName { get; set; }
|
||||
public int Priority { get; set; }
|
||||
public EmailChannelType EmailChannelType { get; set; }
|
||||
public required EmailChannelSettings Settings { get; set; }
|
||||
public int? DailyLimit { get; set; }
|
||||
public int? MonthlyLimit { get; set; }
|
||||
public int WarnThresholdPercent { get; set; } = 90;
|
||||
public bool IsActive { get; set; } = true;
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
namespace HrynCo.NotificationService.DAL.Abstract.Providers;
|
||||
|
||||
public abstract class EmailChannelSettings
|
||||
{
|
||||
public abstract EmailChannelType EmailChannelType { get; }
|
||||
}
|
||||
|
||||
public class SmtpChannelSettings : EmailChannelSettings
|
||||
{
|
||||
public override EmailChannelType EmailChannelType => EmailChannelType.Smtp;
|
||||
|
||||
public string Host { get; set; } = string.Empty;
|
||||
public int Port { get; set; } = 587;
|
||||
public string Username { get; set; } = string.Empty;
|
||||
public string Password { get; set; } = string.Empty;
|
||||
public bool UseSsl { get; set; } = true;
|
||||
public string FromEmail { get; set; } = string.Empty;
|
||||
public string FromName { get; set; } = string.Empty;
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
namespace HrynCo.NotificationService.DAL.Abstract.Providers;
|
||||
|
||||
public enum EmailChannelType
|
||||
{
|
||||
Undefined = 0,
|
||||
Smtp = 1
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
using HrynCo.NotificationService.DAL.Abstract.Entities;
|
||||
|
||||
namespace HrynCo.NotificationService.DAL.Abstract.Providers;
|
||||
|
||||
/// <summary>
|
||||
/// Tracks email send counts per EmailChannel per calendar day.
|
||||
/// Monthly counts are derived by summing daily records within a month.
|
||||
/// </summary>
|
||||
public class EmailChannelUsage : Entity
|
||||
{
|
||||
public Guid ProviderId { get; set; }
|
||||
public DateOnly Date { get; set; }
|
||||
public int SentCount { get; set; }
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
using HrynCo.NotificationService.DAL.Abstract.Providers;
|
||||
|
||||
namespace HrynCo.NotificationService.DAL.Abstract.Repositories;
|
||||
|
||||
public interface IEmailChannelRepository
|
||||
{
|
||||
Task<IReadOnlyList<EmailChannel>> GetAllAsync(CancellationToken ct = default);
|
||||
Task<IReadOnlyList<EmailChannel>> GetByServiceAsync(string serviceName, CancellationToken ct = default);
|
||||
Task<EmailChannel?> GetByIdAsync(Guid id, CancellationToken ct = default);
|
||||
Task<IReadOnlyList<ChannelWithUsage>> GetAllWithUsageSummaryAsync(DateOnly today, CancellationToken ct = default);
|
||||
Task AddAsync(EmailChannel channel, CancellationToken ct = default);
|
||||
Task UpdateAsync(EmailChannel channel, CancellationToken ct = default);
|
||||
Task DeleteAsync(EmailChannel channel, CancellationToken ct = default);
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
namespace HrynCo.NotificationService.DAL.Abstract.Repositories;
|
||||
|
||||
public interface IEmailChannelUsageRepository
|
||||
{
|
||||
Task<int> GetDailyCountAsync(Guid providerId, DateOnly date, CancellationToken ct = default);
|
||||
Task<int> GetMonthlyCountAsync(Guid providerId, int year, int month, CancellationToken ct = default);
|
||||
Task IncrementUsageAsync(Guid providerId, DateOnly date, CancellationToken ct = default);
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
using HrynCo.NotificationService.DAL.Abstract.Templates;
|
||||
|
||||
namespace HrynCo.NotificationService.DAL.Abstract.Repositories;
|
||||
|
||||
public interface IEmailTemplateRepository
|
||||
{
|
||||
Task<IReadOnlyList<EmailTemplate>> GetAllAsync(CancellationToken ct = default);
|
||||
Task<IReadOnlyList<EmailTemplate>> GetByServiceAsync(string serviceName, CancellationToken ct = default);
|
||||
Task<EmailTemplate?> GetAsync(string serviceName, string key, string languageCode, CancellationToken ct = default);
|
||||
Task AddAsync(EmailTemplate template, CancellationToken ct = default);
|
||||
Task UpdateAsync(EmailTemplate template, CancellationToken ct = default);
|
||||
Task DeleteAsync(EmailTemplate template, CancellationToken ct = default);
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
using HrynCo.NotificationService.DAL.Abstract.Entities;
|
||||
|
||||
namespace HrynCo.NotificationService.DAL.Abstract.Templates;
|
||||
|
||||
public class EmailTemplate : Entity
|
||||
{
|
||||
public required string ServiceName { get; set; }
|
||||
public required string Key { get; set; }
|
||||
public required string LanguageCode { get; set; }
|
||||
public required string Subject { get; set; }
|
||||
public required string HtmlBody { get; set; }
|
||||
public required string TextBody { get; set; }
|
||||
public IReadOnlyList<EmailTemplateVariable> Variables { get; set; } = [];
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
namespace HrynCo.NotificationService.DAL.Abstract.Templates;
|
||||
|
||||
public record EmailTemplateVariable
|
||||
{
|
||||
public required string Name { get; init; }
|
||||
public bool Required { get; init; }
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
using HrynCo.NotificationService.DAL.EF.Entities;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Metadata.Builders;
|
||||
|
||||
namespace HrynCo.NotificationService.DAL.EF.Configurations;
|
||||
|
||||
internal class EmailChannelEntityConfiguration : IEntityTypeConfiguration<EmailChannelEntity>
|
||||
{
|
||||
public void Configure(EntityTypeBuilder<EmailChannelEntity> builder)
|
||||
{
|
||||
builder.ToTable("email_channels");
|
||||
|
||||
builder.HasKey(x => x.Id);
|
||||
builder.Property(x => x.Id).HasColumnName("id");
|
||||
|
||||
builder.Property(x => x.ServiceName)
|
||||
.HasColumnName("service_name")
|
||||
.IsRequired()
|
||||
.HasMaxLength(100);
|
||||
|
||||
builder.HasIndex(x => new { x.ServiceName, x.Priority });
|
||||
|
||||
builder.Property(x => x.Priority).HasColumnName("priority");
|
||||
|
||||
builder.Property(x => x.EmailChannelType).HasColumnName("provider_type");
|
||||
|
||||
builder.Property(x => x.SettingsJson)
|
||||
.HasColumnName("settings")
|
||||
.HasColumnType("jsonb")
|
||||
.IsRequired();
|
||||
|
||||
builder.Property(x => x.DailyLimit).HasColumnName("daily_limit");
|
||||
builder.Property(x => x.MonthlyLimit).HasColumnName("monthly_limit");
|
||||
builder.Property(x => x.WarnThresholdPercent).HasColumnName("warn_threshold_percent");
|
||||
builder.Property(x => x.IsActive).HasColumnName("is_active");
|
||||
builder.Property(x => x.Created).HasColumnName("created");
|
||||
builder.Property(x => x.Updated).HasColumnName("updated");
|
||||
|
||||
builder.HasMany(x => x.UsageRecords)
|
||||
.WithOne()
|
||||
.HasForeignKey(u => u.ProviderId);
|
||||
}
|
||||
}
|
||||
+25
@@ -0,0 +1,25 @@
|
||||
using HrynCo.NotificationService.DAL.EF.Entities;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Metadata.Builders;
|
||||
|
||||
namespace HrynCo.NotificationService.DAL.EF.Configurations;
|
||||
|
||||
internal class EmailChannelUsageEntityConfiguration : IEntityTypeConfiguration<EmailChannelUsageEntity>
|
||||
{
|
||||
public void Configure(EntityTypeBuilder<EmailChannelUsageEntity> builder)
|
||||
{
|
||||
builder.ToTable("email_channel_usage");
|
||||
|
||||
builder.HasKey(x => x.Id);
|
||||
builder.Property(x => x.Id).HasColumnName("id");
|
||||
|
||||
builder.Property(x => x.ProviderId).HasColumnName("provider_id");
|
||||
|
||||
builder.HasIndex(x => new { x.ProviderId, x.Date }).IsUnique();
|
||||
|
||||
builder.Property(x => x.Date).HasColumnName("date");
|
||||
builder.Property(x => x.SentCount).HasColumnName("sent_count");
|
||||
builder.Property(x => x.Created).HasColumnName("created");
|
||||
builder.Property(x => x.Updated).HasColumnName("updated");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
using HrynCo.NotificationService.DAL.EF.Entities;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Metadata.Builders;
|
||||
|
||||
namespace HrynCo.NotificationService.DAL.EF.Configurations;
|
||||
|
||||
internal class EmailTemplateEntityConfiguration : IEntityTypeConfiguration<EmailTemplateEntity>
|
||||
{
|
||||
public void Configure(EntityTypeBuilder<EmailTemplateEntity> builder)
|
||||
{
|
||||
builder.ToTable("email_templates");
|
||||
|
||||
builder.HasKey(x => x.Id);
|
||||
builder.Property(x => x.Id).HasColumnName("id");
|
||||
|
||||
builder.Property(x => x.ServiceName)
|
||||
.HasColumnName("service_name")
|
||||
.IsRequired()
|
||||
.HasMaxLength(100);
|
||||
|
||||
builder.Property(x => x.Key)
|
||||
.HasColumnName("key")
|
||||
.IsRequired()
|
||||
.HasMaxLength(100);
|
||||
|
||||
builder.Property(x => x.LanguageCode)
|
||||
.HasColumnName("language_code")
|
||||
.IsRequired()
|
||||
.HasMaxLength(10);
|
||||
|
||||
builder.HasIndex(x => new { x.ServiceName, x.Key, x.LanguageCode })
|
||||
.IsUnique();
|
||||
|
||||
builder.Property(x => x.Subject)
|
||||
.HasColumnName("subject")
|
||||
.IsRequired();
|
||||
|
||||
builder.Property(x => x.HtmlBody)
|
||||
.HasColumnName("html_body")
|
||||
.IsRequired();
|
||||
|
||||
builder.Property(x => x.TextBody)
|
||||
.HasColumnName("text_body")
|
||||
.IsRequired();
|
||||
|
||||
builder.Property(x => x.Created).HasColumnName("created");
|
||||
builder.Property(x => x.Updated).HasColumnName("updated");
|
||||
|
||||
builder.OwnsMany(x => x.Variables, v =>
|
||||
{
|
||||
v.ToJson("variables");
|
||||
v.Property(x => x.Name).HasJsonPropertyName("name");
|
||||
v.Property(x => x.Required).HasJsonPropertyName("required");
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
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);
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
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();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,105 @@
|
||||
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,8 @@
|
||||
namespace HrynCo.NotificationService.DAL.EF.Core;
|
||||
|
||||
internal sealed class UnitOfWork : EfUnitOfWork<NotificationDbContext>
|
||||
{
|
||||
public UnitOfWork(NotificationDbContext context) : base(context)
|
||||
{
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
using HrynCo.NotificationService.DAL.Abstract.Entities;
|
||||
using HrynCo.NotificationService.DAL.Abstract.Providers;
|
||||
|
||||
namespace HrynCo.NotificationService.DAL.EF.Entities;
|
||||
|
||||
internal class EmailChannelEntity : Entity
|
||||
{
|
||||
public required string ServiceName { get; set; }
|
||||
public int Priority { get; set; }
|
||||
public EmailChannelType EmailChannelType { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// EmailChannel-specific credentials and settings stored as JSONB.
|
||||
/// Deserialized based on <see cref="EmailChannelType"/> in the repository.
|
||||
/// </summary>
|
||||
public required string SettingsJson { get; set; }
|
||||
|
||||
public int? DailyLimit { get; set; }
|
||||
public int? MonthlyLimit { get; set; }
|
||||
public int WarnThresholdPercent { get; set; }
|
||||
public bool IsActive { get; set; }
|
||||
|
||||
public ICollection<EmailChannelUsageEntity> UsageRecords { get; set; } = [];
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
using HrynCo.NotificationService.DAL.Abstract.Entities;
|
||||
|
||||
namespace HrynCo.NotificationService.DAL.EF.Entities;
|
||||
|
||||
internal class EmailChannelUsageEntity : Entity
|
||||
{
|
||||
public Guid ProviderId { get; set; }
|
||||
public DateOnly Date { get; set; }
|
||||
public int SentCount { get; set; }
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
using HrynCo.NotificationService.DAL.Abstract.Entities;
|
||||
|
||||
namespace HrynCo.NotificationService.DAL.EF.Entities;
|
||||
|
||||
internal class EmailTemplateEntity : Entity
|
||||
{
|
||||
public required string ServiceName { get; set; }
|
||||
public required string Key { get; set; }
|
||||
public required string LanguageCode { get; set; }
|
||||
public required string Subject { get; set; }
|
||||
public required string HtmlBody { get; set; }
|
||||
public required string TextBody { get; set; }
|
||||
public List<EmailTemplateVariableData> Variables { get; set; } = [];
|
||||
}
|
||||
|
||||
internal class EmailTemplateVariableData
|
||||
{
|
||||
public required string Name { get; set; }
|
||||
public bool Required { get; set; }
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\HrynCo.NotificationService.DAL.Abstract\HrynCo.NotificationService.DAL.Abstract.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Design">
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
</PackageReference>
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Relational" />
|
||||
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" />
|
||||
</ItemGroup>
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net10.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
</Project>
|
||||
+211
@@ -0,0 +1,211 @@
|
||||
// <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("20260501214629_InitialCreate")]
|
||||
partial class InitialCreate
|
||||
{
|
||||
/// <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.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");
|
||||
});
|
||||
#pragma warning restore 612, 618
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,102 @@
|
||||
using System;
|
||||
using Microsoft.EntityFrameworkCore.Migrations;
|
||||
|
||||
#nullable disable
|
||||
|
||||
namespace HrynCo.NotificationService.DAL.EF.Migrations
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public partial class InitialCreate : Migration
|
||||
{
|
||||
/// <inheritdoc />
|
||||
protected override void Up(MigrationBuilder migrationBuilder)
|
||||
{
|
||||
migrationBuilder.CreateTable(
|
||||
name: "email_channel_usage",
|
||||
columns: table => new
|
||||
{
|
||||
id = table.Column<Guid>(type: "uuid", nullable: false),
|
||||
provider_id = table.Column<Guid>(type: "uuid", nullable: false),
|
||||
date = table.Column<DateOnly>(type: "date", nullable: false),
|
||||
sent_count = table.Column<int>(type: "integer", nullable: false),
|
||||
created = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: false),
|
||||
updated = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: true)
|
||||
},
|
||||
constraints: table =>
|
||||
{
|
||||
table.PrimaryKey("PK_email_channel_usage", x => x.id);
|
||||
});
|
||||
|
||||
migrationBuilder.CreateTable(
|
||||
name: "email_channels",
|
||||
columns: table => new
|
||||
{
|
||||
id = table.Column<Guid>(type: "uuid", nullable: false),
|
||||
service_name = table.Column<string>(type: "character varying(100)", maxLength: 100, nullable: false),
|
||||
priority = table.Column<int>(type: "integer", nullable: false),
|
||||
provider_type = table.Column<int>(type: "integer", nullable: false),
|
||||
settings = table.Column<string>(type: "jsonb", nullable: false),
|
||||
daily_limit = table.Column<int>(type: "integer", nullable: true),
|
||||
monthly_limit = table.Column<int>(type: "integer", nullable: true),
|
||||
warn_threshold_percent = table.Column<int>(type: "integer", nullable: false),
|
||||
is_active = table.Column<bool>(type: "boolean", nullable: false),
|
||||
created = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: false),
|
||||
updated = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: true)
|
||||
},
|
||||
constraints: table =>
|
||||
{
|
||||
table.PrimaryKey("PK_email_channels", x => x.id);
|
||||
});
|
||||
|
||||
migrationBuilder.CreateTable(
|
||||
name: "email_templates",
|
||||
columns: table => new
|
||||
{
|
||||
id = table.Column<Guid>(type: "uuid", nullable: false),
|
||||
service_name = table.Column<string>(type: "character varying(100)", maxLength: 100, nullable: false),
|
||||
key = table.Column<string>(type: "character varying(100)", maxLength: 100, nullable: false),
|
||||
language_code = table.Column<string>(type: "character varying(10)", maxLength: 10, nullable: false),
|
||||
subject = table.Column<string>(type: "text", nullable: false),
|
||||
html_body = table.Column<string>(type: "text", nullable: false),
|
||||
text_body = table.Column<string>(type: "text", nullable: false),
|
||||
created = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: false),
|
||||
updated = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: true),
|
||||
variables = table.Column<string>(type: "jsonb", nullable: true)
|
||||
},
|
||||
constraints: table =>
|
||||
{
|
||||
table.PrimaryKey("PK_email_templates", x => x.id);
|
||||
});
|
||||
|
||||
migrationBuilder.CreateIndex(
|
||||
name: "IX_email_channel_usage_provider_id_date",
|
||||
table: "email_channel_usage",
|
||||
columns: new[] { "provider_id", "date" },
|
||||
unique: true);
|
||||
|
||||
migrationBuilder.CreateIndex(
|
||||
name: "IX_email_channels_service_name_priority",
|
||||
table: "email_channels",
|
||||
columns: new[] { "service_name", "priority" });
|
||||
|
||||
migrationBuilder.CreateIndex(
|
||||
name: "IX_email_templates_service_name_key_language_code",
|
||||
table: "email_templates",
|
||||
columns: new[] { "service_name", "key", "language_code" },
|
||||
unique: true);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
protected override void Down(MigrationBuilder migrationBuilder)
|
||||
{
|
||||
migrationBuilder.DropTable(
|
||||
name: "email_channel_usage");
|
||||
|
||||
migrationBuilder.DropTable(
|
||||
name: "email_channels");
|
||||
|
||||
migrationBuilder.DropTable(
|
||||
name: "email_templates");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,208 @@
|
||||
// <auto-generated />
|
||||
using System;
|
||||
using HrynCo.NotificationService.DAL.EF;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Infrastructure;
|
||||
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
|
||||
using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata;
|
||||
|
||||
#nullable disable
|
||||
|
||||
namespace HrynCo.NotificationService.DAL.EF.Migrations
|
||||
{
|
||||
[DbContext(typeof(NotificationDbContext))]
|
||||
partial class NotificationDbContextModelSnapshot : ModelSnapshot
|
||||
{
|
||||
protected override void BuildModel(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.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");
|
||||
});
|
||||
#pragma warning restore 612, 618
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
using HrynCo.NotificationService.DAL.EF.Entities;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace HrynCo.NotificationService.DAL.EF;
|
||||
|
||||
public class NotificationDbContext : DbContext
|
||||
{
|
||||
public NotificationDbContext(DbContextOptions<NotificationDbContext> options)
|
||||
: base(options)
|
||||
{
|
||||
}
|
||||
|
||||
internal DbSet<EmailTemplateEntity> Templates => Set<EmailTemplateEntity>();
|
||||
internal DbSet<EmailChannelEntity> Providers => Set<EmailChannelEntity>();
|
||||
internal DbSet<EmailChannelUsageEntity> EmailChannelUsage => Set<EmailChannelUsageEntity>();
|
||||
|
||||
protected override void OnModelCreating(ModelBuilder modelBuilder)
|
||||
{
|
||||
modelBuilder.ApplyConfigurationsFromAssembly(typeof(NotificationDbContext).Assembly);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Design;
|
||||
|
||||
namespace HrynCo.NotificationService.DAL.EF;
|
||||
|
||||
internal sealed class NotificationDbContextFactory : IDesignTimeDbContextFactory<NotificationDbContext>
|
||||
{
|
||||
public NotificationDbContext CreateDbContext(string[] args)
|
||||
{
|
||||
var options = new DbContextOptionsBuilder<NotificationDbContext>()
|
||||
.UseNpgsql("Host=localhost;Port=5432;Database=notification_service;Username=postgres;Password=postgres")
|
||||
.Options;
|
||||
|
||||
return new NotificationDbContext(options);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,136 @@
|
||||
using System.Text.Json;
|
||||
using HrynCo.NotificationService.DAL.Abstract.Providers;
|
||||
using HrynCo.NotificationService.DAL.Abstract.Repositories;
|
||||
using HrynCo.NotificationService.DAL.EF.Core;
|
||||
using HrynCo.NotificationService.DAL.EF.Entities;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace HrynCo.NotificationService.DAL.EF.Repositories;
|
||||
|
||||
internal sealed class EmailChannelRepository : EfRepository<EmailChannelEntity>, IEmailChannelRepository
|
||||
{
|
||||
public EmailChannelRepository(NotificationDbContext dbContext) : base(dbContext)
|
||||
{
|
||||
}
|
||||
|
||||
public async Task<IReadOnlyList<EmailChannel>> GetAllAsync(CancellationToken ct = default)
|
||||
{
|
||||
var entities = await DbSet
|
||||
.AsNoTracking()
|
||||
.OrderBy(x => x.ServiceName)
|
||||
.ThenBy(x => x.Priority)
|
||||
.ToListAsync(ct);
|
||||
|
||||
return entities.Select(MapToDomain).ToList();
|
||||
}
|
||||
|
||||
public async Task<IReadOnlyList<EmailChannel>> GetByServiceAsync(string serviceName, CancellationToken ct = default)
|
||||
{
|
||||
var entities = await DbSet
|
||||
.AsNoTracking()
|
||||
.Where(x => x.ServiceName == serviceName)
|
||||
.OrderBy(x => x.Priority)
|
||||
.ToListAsync(ct);
|
||||
|
||||
return entities.Select(MapToDomain).ToList();
|
||||
}
|
||||
|
||||
public async Task<IReadOnlyList<ChannelWithUsage>> GetAllWithUsageSummaryAsync(
|
||||
DateOnly today, CancellationToken ct = default)
|
||||
{
|
||||
var rows = await DbSet
|
||||
.AsNoTracking()
|
||||
.OrderBy(c => c.ServiceName)
|
||||
.ThenBy(c => c.Priority)
|
||||
.Select(c => new
|
||||
{
|
||||
Channel = c,
|
||||
DailySent = c.UsageRecords
|
||||
.Where(u => u.Date == today)
|
||||
.Sum(u => (int?)u.SentCount) ?? 0,
|
||||
MonthlySent = c.UsageRecords
|
||||
.Where(u => u.Date.Year == today.Year && u.Date.Month == today.Month)
|
||||
.Sum(u => (int?)u.SentCount) ?? 0
|
||||
})
|
||||
.ToListAsync(ct);
|
||||
|
||||
return rows
|
||||
.Select(r => new ChannelWithUsage(MapToDomain(r.Channel), r.DailySent, r.MonthlySent))
|
||||
.ToList();
|
||||
}
|
||||
|
||||
public async Task<EmailChannel?> GetByIdAsync(Guid id, CancellationToken ct = default)
|
||||
{
|
||||
EmailChannelEntity? entity = await DbSet.AsNoTracking().FirstOrDefaultAsync(x => x.Id == id, ct);
|
||||
return entity is null ? null : MapToDomain(entity);
|
||||
}
|
||||
|
||||
public Task AddAsync(EmailChannel channel, CancellationToken ct = default)
|
||||
{
|
||||
return base.AddAsync(MapToEntity(channel), ct);
|
||||
}
|
||||
|
||||
public Task UpdateAsync(EmailChannel channel, CancellationToken ct = default)
|
||||
{
|
||||
EmailChannelEntity entity = MapToEntity(channel);
|
||||
entity.Updated = DateTimeOffset.UtcNow;
|
||||
Update(entity);
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
public async Task DeleteAsync(EmailChannel channel, CancellationToken ct = default)
|
||||
{
|
||||
EmailChannelEntity? entity = await DbSet.FindAsync([channel.Id], ct);
|
||||
if (entity is not null)
|
||||
{
|
||||
Delete(entity);
|
||||
}
|
||||
}
|
||||
|
||||
private static EmailChannel MapToDomain(EmailChannelEntity e)
|
||||
{
|
||||
return new EmailChannel
|
||||
{
|
||||
Id = e.Id,
|
||||
ServiceName = e.ServiceName,
|
||||
Priority = e.Priority,
|
||||
EmailChannelType = e.EmailChannelType,
|
||||
Settings = DeserializeSettings(e.EmailChannelType, e.SettingsJson),
|
||||
DailyLimit = e.DailyLimit,
|
||||
MonthlyLimit = e.MonthlyLimit,
|
||||
WarnThresholdPercent = e.WarnThresholdPercent,
|
||||
IsActive = e.IsActive,
|
||||
Created = e.Created,
|
||||
Updated = e.Updated
|
||||
};
|
||||
}
|
||||
|
||||
private static EmailChannelEntity MapToEntity(EmailChannel p)
|
||||
{
|
||||
return new EmailChannelEntity
|
||||
{
|
||||
Id = p.Id,
|
||||
ServiceName = p.ServiceName,
|
||||
Priority = p.Priority,
|
||||
EmailChannelType = p.EmailChannelType,
|
||||
SettingsJson = JsonSerializer.Serialize(p.Settings, p.Settings.GetType()),
|
||||
DailyLimit = p.DailyLimit,
|
||||
MonthlyLimit = p.MonthlyLimit,
|
||||
WarnThresholdPercent = p.WarnThresholdPercent,
|
||||
IsActive = p.IsActive,
|
||||
Created = p.Created,
|
||||
Updated = p.Updated
|
||||
};
|
||||
}
|
||||
|
||||
private static EmailChannelSettings DeserializeSettings(EmailChannelType type, string json)
|
||||
{
|
||||
return type switch
|
||||
{
|
||||
EmailChannelType.Smtp => JsonSerializer.Deserialize<SmtpChannelSettings>(json)
|
||||
?? throw new InvalidOperationException(
|
||||
"Failed to deserialize SMTP EmailChannel settings."),
|
||||
_ => throw new InvalidOperationException($"Unknown or undefined email channel type: {type}")
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
using HrynCo.NotificationService.DAL.Abstract.Repositories;
|
||||
using HrynCo.NotificationService.DAL.EF.Core;
|
||||
using HrynCo.NotificationService.DAL.EF.Entities;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace HrynCo.NotificationService.DAL.EF.Repositories;
|
||||
|
||||
internal sealed class EmailChannelUsageRepository : EfRepository<EmailChannelUsageEntity>, IEmailChannelUsageRepository
|
||||
{
|
||||
public EmailChannelUsageRepository(NotificationDbContext dbContext) : base(dbContext)
|
||||
{
|
||||
}
|
||||
|
||||
public async Task<int> GetDailyCountAsync(Guid providerId, DateOnly date, CancellationToken ct = default)
|
||||
{
|
||||
EmailChannelUsageEntity? entity = await DbSet
|
||||
.FirstOrDefaultAsync(x => x.ProviderId == providerId && x.Date == date, ct);
|
||||
|
||||
return entity?.SentCount ?? 0;
|
||||
}
|
||||
|
||||
public async Task<int> GetMonthlyCountAsync(Guid providerId, int year, int month, CancellationToken ct = default)
|
||||
{
|
||||
return await DbSet
|
||||
.Where(x => x.ProviderId == providerId
|
||||
&& x.Date.Year == year
|
||||
&& x.Date.Month == month)
|
||||
.SumAsync(x => x.SentCount, ct);
|
||||
}
|
||||
|
||||
public async Task IncrementUsageAsync(Guid providerId, DateOnly date, CancellationToken ct = default)
|
||||
{
|
||||
EmailChannelUsageEntity? entity = await DbSet
|
||||
.FirstOrDefaultAsync(x => x.ProviderId == providerId && x.Date == date, ct);
|
||||
|
||||
if (entity is null)
|
||||
await AddAsync(new EmailChannelUsageEntity { ProviderId = providerId, Date = date, SentCount = 1 }, ct);
|
||||
else
|
||||
{
|
||||
entity.SentCount++;
|
||||
Update(entity);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,83 @@
|
||||
using HrynCo.NotificationService.DAL.Abstract.Repositories;
|
||||
using HrynCo.NotificationService.DAL.Abstract.Templates;
|
||||
using HrynCo.NotificationService.DAL.EF.Core;
|
||||
using HrynCo.NotificationService.DAL.EF.Entities;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace HrynCo.NotificationService.DAL.EF.Repositories;
|
||||
|
||||
internal sealed class EmailTemplateRepository : EfRepository<EmailTemplateEntity>, IEmailTemplateRepository
|
||||
{
|
||||
public EmailTemplateRepository(NotificationDbContext dbContext) : base(dbContext)
|
||||
{
|
||||
}
|
||||
|
||||
public async Task<IReadOnlyList<EmailTemplate>> GetAllAsync(CancellationToken ct = default)
|
||||
{
|
||||
List<EmailTemplateEntity> entities = await DbSet.ToListAsync(ct);
|
||||
return entities.Select(MapToDomain).ToList();
|
||||
}
|
||||
|
||||
public async Task<IReadOnlyList<EmailTemplate>> GetByServiceAsync(string serviceName, CancellationToken ct = default)
|
||||
{
|
||||
List<EmailTemplateEntity> entities = await DbSet
|
||||
.Where(x => x.ServiceName == serviceName)
|
||||
.ToListAsync(ct);
|
||||
|
||||
return entities.Select(MapToDomain).ToList();
|
||||
}
|
||||
|
||||
public async Task<EmailTemplate?> GetAsync(string serviceName, string key, string languageCode, CancellationToken ct = default)
|
||||
{
|
||||
EmailTemplateEntity? entity = await DbSet.FirstOrDefaultAsync(
|
||||
x => x.ServiceName == serviceName && x.Key == key && x.LanguageCode == languageCode, ct);
|
||||
|
||||
return entity is null ? null : MapToDomain(entity);
|
||||
}
|
||||
|
||||
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);
|
||||
entity.Updated = DateTimeOffset.UtcNow;
|
||||
Update(entity);
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
public async Task DeleteAsync(EmailTemplate EmailTemplate, CancellationToken ct = default)
|
||||
{
|
||||
EmailTemplateEntity? entity = await DbSet.FindAsync([EmailTemplate.Id], ct);
|
||||
if (entity is not null)
|
||||
Delete(entity);
|
||||
}
|
||||
|
||||
private static EmailTemplate MapToDomain(EmailTemplateEntity e) => new()
|
||||
{
|
||||
Id = e.Id,
|
||||
ServiceName = e.ServiceName,
|
||||
Key = e.Key,
|
||||
LanguageCode = e.LanguageCode,
|
||||
Subject = e.Subject,
|
||||
HtmlBody = e.HtmlBody,
|
||||
TextBody = e.TextBody,
|
||||
Variables = e.Variables.Select(v => new EmailTemplateVariable { Name = v.Name, Required = v.Required }).ToList(),
|
||||
Created = e.Created,
|
||||
Updated = e.Updated
|
||||
};
|
||||
|
||||
private static EmailTemplateEntity MapToEntity(EmailTemplate t) => new()
|
||||
{
|
||||
Id = t.Id,
|
||||
ServiceName = t.ServiceName,
|
||||
Key = t.Key,
|
||||
LanguageCode = t.LanguageCode,
|
||||
Subject = t.Subject,
|
||||
HtmlBody = t.HtmlBody,
|
||||
TextBody = t.TextBody,
|
||||
Variables = t.Variables.Select(v => new EmailTemplateVariableData { Name = v.Name, Required = v.Required }).ToList(),
|
||||
Created = t.Created,
|
||||
Updated = t.Updated
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
using HrynCo.NotificationService.DAL.Abstract;
|
||||
using HrynCo.NotificationService.DAL.Abstract.Repositories;
|
||||
using HrynCo.NotificationService.DAL.EF.Core;
|
||||
using HrynCo.NotificationService.DAL.EF.Repositories;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
|
||||
namespace HrynCo.NotificationService.DAL.EF;
|
||||
|
||||
public static class ServiceCollectionExtensions
|
||||
{
|
||||
public static IServiceCollection AddNotificationDataAccess(
|
||||
this IServiceCollection services,
|
||||
string connectionString)
|
||||
{
|
||||
services.AddDbContext<NotificationDbContext>(options =>
|
||||
options.UseNpgsql(connectionString));
|
||||
|
||||
services.AddScoped<IUnitOfWork, UnitOfWork>();
|
||||
services.AddScoped<IEmailTemplateRepository, EmailTemplateRepository>();
|
||||
services.AddScoped<IEmailChannelRepository, EmailChannelRepository>();
|
||||
services.AddScoped<IEmailChannelUsageRepository, EmailChannelUsageRepository>();
|
||||
|
||||
return services;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
FROM mcr.microsoft.com/dotnet/sdk:10.0 AS build
|
||||
WORKDIR /src
|
||||
|
||||
COPY ["HrynCo.NotificationService.Migrator/HrynCo.NotificationService.Migrator.csproj", "HrynCo.NotificationService.Migrator/"]
|
||||
COPY ["HrynCo.NotificationService.DAL.EF/HrynCo.NotificationService.DAL.EF.csproj", "HrynCo.NotificationService.DAL.EF/"]
|
||||
COPY ["HrynCo.NotificationService.DAL.Abstract/HrynCo.NotificationService.DAL.Abstract.csproj", "HrynCo.NotificationService.DAL.Abstract/"]
|
||||
COPY ["Directory.Packages.props", "./"]
|
||||
COPY ["Directory.Build.props", "./"]
|
||||
RUN dotnet restore "HrynCo.NotificationService.Migrator/HrynCo.NotificationService.Migrator.csproj"
|
||||
|
||||
COPY . .
|
||||
RUN dotnet publish "HrynCo.NotificationService.Migrator/HrynCo.NotificationService.Migrator.csproj" -c Release -o /app/publish --no-restore
|
||||
|
||||
FROM mcr.microsoft.com/dotnet/runtime:10.0
|
||||
WORKDIR /app
|
||||
COPY --from=build /app/publish .
|
||||
ENTRYPOINT ["dotnet", "HrynCo.NotificationService.Migrator.dll"]
|
||||
@@ -0,0 +1,25 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net10.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Design">
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
</PackageReference>
|
||||
<PackageReference Include="Microsoft.Extensions.Hosting" />
|
||||
<PackageReference Include="Serilog.Extensions.Hosting" />
|
||||
<PackageReference Include="Serilog.Settings.Configuration" />
|
||||
<PackageReference Include="Serilog.Sinks.Console" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\HrynCo.NotificationService.DAL.EF\HrynCo.NotificationService.DAL.EF.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
@@ -0,0 +1,46 @@
|
||||
using HrynCo.NotificationService.DAL.EF;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using Serilog;
|
||||
|
||||
Log.Logger = new LoggerConfiguration()
|
||||
.WriteTo.Console()
|
||||
.CreateBootstrapLogger();
|
||||
|
||||
try
|
||||
{
|
||||
Log.Information("🚀 Notification Service Migrator starting...");
|
||||
|
||||
var host = Host.CreateDefaultBuilder(args)
|
||||
.UseSerilog((ctx, cfg) => cfg
|
||||
.ReadFrom.Configuration(ctx.Configuration)
|
||||
.WriteTo.Console())
|
||||
.ConfigureServices((ctx, services) =>
|
||||
{
|
||||
var connectionString = ctx.Configuration["App:ConnectionString"]
|
||||
?? throw new InvalidOperationException("App:ConnectionString is not configured.");
|
||||
|
||||
services.AddDbContext<NotificationDbContext>(options =>
|
||||
options.UseNpgsql(connectionString));
|
||||
})
|
||||
.Build();
|
||||
|
||||
using var scope = host.Services.CreateScope();
|
||||
var db = scope.ServiceProvider.GetRequiredService<NotificationDbContext>();
|
||||
|
||||
Log.Information("Applying migrations...");
|
||||
await db.Database.MigrateAsync();
|
||||
Log.Information("Migrations applied successfully.");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Log.Fatal(ex, "Migration failed.");
|
||||
return 1;
|
||||
}
|
||||
finally
|
||||
{
|
||||
await Log.CloseAndFlushAsync();
|
||||
}
|
||||
|
||||
return 0;
|
||||
@@ -0,0 +1,18 @@
|
||||
{
|
||||
"App": {
|
||||
"ConnectionString": ""
|
||||
},
|
||||
"Serilog": {
|
||||
"MinimumLevel": {
|
||||
"Default": "Information",
|
||||
"Override": {
|
||||
"Microsoft": "Warning",
|
||||
"Microsoft.EntityFrameworkCore": "Information"
|
||||
}
|
||||
},
|
||||
"WriteTo": [
|
||||
{ "Name": "Console" }
|
||||
],
|
||||
"Enrich": [ "FromLogContext" ]
|
||||
}
|
||||
}
|
||||
+26
@@ -0,0 +1,26 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net10.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
<IsPackable>false</IsPackable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="coverlet.collector" />
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" />
|
||||
<PackageReference Include="xunit" />
|
||||
<PackageReference Include="xunit.runner.visualstudio" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Using Include="Xunit" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\HrynCo.NotificationService.Services\HrynCo.NotificationService.Services.csproj" />
|
||||
<ProjectReference Include="..\HrynCo.NotificationService.DAL.Abstract\HrynCo.NotificationService.DAL.Abstract.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
@@ -0,0 +1,10 @@
|
||||
namespace HrynCo.NotificationService.Services.Tests;
|
||||
|
||||
public class UnitTest1
|
||||
{
|
||||
[Fact]
|
||||
public void Test1()
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
using HrynCo.Common;
|
||||
using HrynCo.NotificationService.DAL.Abstract;
|
||||
using MediatR;
|
||||
|
||||
namespace HrynCo.NotificationService.Services.Behaviors;
|
||||
|
||||
public class TransactionBehavior<TRequest, TResponse> : IPipelineBehavior<TRequest, TResponse>
|
||||
where TRequest : notnull
|
||||
{
|
||||
private readonly IUnitOfWork _unitOfWork;
|
||||
private readonly IProfiler _profiler;
|
||||
|
||||
public TransactionBehavior(IUnitOfWork unitOfWork, IProfiler profiler)
|
||||
{
|
||||
_unitOfWork = unitOfWork;
|
||||
_profiler = profiler;
|
||||
}
|
||||
|
||||
public Task<TResponse> Handle(TRequest request, RequestHandlerDelegate<TResponse> next, CancellationToken cancellationToken) =>
|
||||
_profiler.MeasureExecutionAsync(
|
||||
() => _unitOfWork.ExecuteInTransactionAsync(async () =>
|
||||
{
|
||||
TResponse response = await next();
|
||||
await _unitOfWork.SaveChangesAsync(cancellationToken);
|
||||
return response;
|
||||
}),
|
||||
typeof(TRequest).Name);
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
using HrynCo.NotificationService.DAL.Abstract;
|
||||
using HrynCo.NotificationService.Services.Logging;
|
||||
using MediatR;
|
||||
using Serilog;
|
||||
|
||||
namespace HrynCo.NotificationService.Services.Core;
|
||||
|
||||
public abstract class RequestHandler<TRequest, TResponse> : IRequestHandler<TRequest, TResponse>
|
||||
where TRequest : IRequest<TResponse>
|
||||
{
|
||||
protected RequestHandler(IContextualSerilogLogger<TRequest> logger, IUnitOfWork unitOfWork)
|
||||
{
|
||||
Logger = logger.Logger;
|
||||
UnitOfWork = unitOfWork;
|
||||
}
|
||||
|
||||
protected ILogger Logger { get; }
|
||||
protected IUnitOfWork UnitOfWork { get; }
|
||||
|
||||
public Task<TResponse> Handle(TRequest request, CancellationToken cancellationToken)
|
||||
{
|
||||
return DoOnHandle(request, cancellationToken);
|
||||
}
|
||||
|
||||
protected abstract Task<TResponse> DoOnHandle(TRequest request, CancellationToken cancellationToken);
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
namespace HrynCo.NotificationService.Services.Core;
|
||||
|
||||
public sealed record ServiceError
|
||||
{
|
||||
public ServiceError(string message, ServiceErrorCode? code = null)
|
||||
{
|
||||
Message = message;
|
||||
Code = code;
|
||||
}
|
||||
|
||||
public ServiceErrorCode? Code { get; set; }
|
||||
public string Message { get; set; }
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
namespace HrynCo.NotificationService.Services.Core;
|
||||
|
||||
public enum ServiceErrorCode
|
||||
{
|
||||
NotFound = 1,
|
||||
Conflict = 2,
|
||||
InvalidRequest = 3,
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
namespace HrynCo.NotificationService.Services.Core;
|
||||
|
||||
public record ServiceResult<TResult>
|
||||
{
|
||||
public ServiceError? Error { get; private set; }
|
||||
public bool IsSuccess { get; private set; }
|
||||
public TResult? Result { get; private set; }
|
||||
|
||||
public static ServiceResult<TResult> Success(TResult result) =>
|
||||
new() { IsSuccess = true, Result = result };
|
||||
|
||||
public static ServiceResult<TResult> Failure(ServiceError error) =>
|
||||
new() { IsSuccess = false, Error = error };
|
||||
|
||||
public static ServiceResult<TResult> Failure(string message, ServiceErrorCode? code = null) =>
|
||||
Failure(new ServiceError(message, code));
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
namespace HrynCo.NotificationService.Services.Core;
|
||||
|
||||
public static class ServiceResultHelper
|
||||
{
|
||||
public static ServiceResult<T> Success<T>(T result) => ServiceResult<T>.Success(result);
|
||||
|
||||
public static ServiceResult<T> Failure<T>(string errorMessage, ServiceErrorCode? errorCode = null) =>
|
||||
ServiceResult<T>.Failure(errorMessage, errorCode);
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
namespace HrynCo.NotificationService.Services.Core;
|
||||
|
||||
public readonly struct Unit
|
||||
{
|
||||
public static readonly Unit Value = new();
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
using HrynCo.NotificationService.DAL.Abstract.Providers;
|
||||
using MediatR;
|
||||
using HrynCo.NotificationService.Services.Core;
|
||||
|
||||
namespace HrynCo.NotificationService.Services.EmailChannels.Create;
|
||||
|
||||
public sealed record CreateEmailChannelCommand(
|
||||
string ServiceName,
|
||||
int Priority,
|
||||
EmailChannelType ChannelType,
|
||||
EmailChannelSettings Settings,
|
||||
int? DailyLimit,
|
||||
int? MonthlyLimit,
|
||||
int WarnThresholdPercent,
|
||||
bool IsActive
|
||||
) : IRequest<ServiceResult<Guid>>;
|
||||
@@ -0,0 +1,43 @@
|
||||
using HrynCo.NotificationService.DAL.Abstract;
|
||||
using HrynCo.NotificationService.DAL.Abstract.Providers;
|
||||
using HrynCo.NotificationService.DAL.Abstract.Repositories;
|
||||
using HrynCo.NotificationService.Services.Core;
|
||||
using HrynCo.NotificationService.Services.Logging;
|
||||
using static HrynCo.NotificationService.Services.Core.ServiceResultHelper;
|
||||
|
||||
namespace HrynCo.NotificationService.Services.EmailChannels.Create;
|
||||
|
||||
internal sealed class CreateEmailChannelHandler
|
||||
: RequestHandler<CreateEmailChannelCommand, ServiceResult<Guid>>
|
||||
{
|
||||
private readonly IEmailChannelRepository _channels;
|
||||
|
||||
public CreateEmailChannelHandler(
|
||||
IContextualSerilogLogger<CreateEmailChannelCommand> logger,
|
||||
IUnitOfWork unitOfWork,
|
||||
IEmailChannelRepository channels)
|
||||
: base(logger, unitOfWork)
|
||||
{
|
||||
_channels = channels;
|
||||
}
|
||||
|
||||
protected override async Task<ServiceResult<Guid>> DoOnHandle(
|
||||
CreateEmailChannelCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
var channel = new EmailChannel
|
||||
{
|
||||
ServiceName = request.ServiceName,
|
||||
Priority = request.Priority,
|
||||
EmailChannelType = request.ChannelType,
|
||||
Settings = request.Settings,
|
||||
DailyLimit = request.DailyLimit,
|
||||
MonthlyLimit = request.MonthlyLimit,
|
||||
WarnThresholdPercent = request.WarnThresholdPercent,
|
||||
IsActive = request.IsActive
|
||||
};
|
||||
|
||||
await _channels.AddAsync(channel, cancellationToken);
|
||||
|
||||
return Success(channel.Id);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
using MediatR;
|
||||
using HrynCo.NotificationService.Services.Core;
|
||||
using Unit = HrynCo.NotificationService.Services.Core.Unit;
|
||||
|
||||
namespace HrynCo.NotificationService.Services.EmailChannels.Delete;
|
||||
|
||||
public sealed record DeleteEmailChannelCommand(Guid Id)
|
||||
: IRequest<ServiceResult<Unit>>;
|
||||
@@ -0,0 +1,35 @@
|
||||
using HrynCo.NotificationService.DAL.Abstract;
|
||||
using HrynCo.NotificationService.DAL.Abstract.Repositories;
|
||||
using HrynCo.NotificationService.Services.Core;
|
||||
using HrynCo.NotificationService.Services.Logging;
|
||||
using static HrynCo.NotificationService.Services.Core.ServiceResultHelper;
|
||||
|
||||
namespace HrynCo.NotificationService.Services.EmailChannels.Delete;
|
||||
|
||||
internal sealed class DeleteEmailChannelHandler
|
||||
: RequestHandler<DeleteEmailChannelCommand, ServiceResult<Unit>>
|
||||
{
|
||||
private readonly IEmailChannelRepository _channels;
|
||||
|
||||
public DeleteEmailChannelHandler(
|
||||
IContextualSerilogLogger<DeleteEmailChannelCommand> logger,
|
||||
IUnitOfWork unitOfWork,
|
||||
IEmailChannelRepository channels)
|
||||
: base(logger, unitOfWork)
|
||||
{
|
||||
_channels = channels;
|
||||
}
|
||||
|
||||
protected override async Task<ServiceResult<Unit>> DoOnHandle(
|
||||
DeleteEmailChannelCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
var channel = await _channels.GetByIdAsync(request.Id, cancellationToken);
|
||||
|
||||
if (channel is null)
|
||||
return Failure<Unit>("Email channel not found.", ServiceErrorCode.NotFound);
|
||||
|
||||
await _channels.DeleteAsync(channel, cancellationToken);
|
||||
|
||||
return Success(Unit.Value);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
using HrynCo.NotificationService.DAL.Abstract;
|
||||
using HrynCo.NotificationService.DAL.Abstract.Providers;
|
||||
using HrynCo.NotificationService.DAL.Abstract.Repositories;
|
||||
using HrynCo.NotificationService.Services.Core;
|
||||
using HrynCo.NotificationService.Services.Logging;
|
||||
using static HrynCo.NotificationService.Services.Core.ServiceResultHelper;
|
||||
|
||||
namespace HrynCo.NotificationService.Services.EmailChannels.Get;
|
||||
|
||||
internal sealed class GetEmailChannelHandler
|
||||
: RequestHandler<GetEmailChannelQuery, ServiceResult<EmailChannel>>
|
||||
{
|
||||
private readonly IEmailChannelRepository _channels;
|
||||
|
||||
public GetEmailChannelHandler(
|
||||
IContextualSerilogLogger<GetEmailChannelQuery> logger,
|
||||
IUnitOfWork unitOfWork,
|
||||
IEmailChannelRepository channels)
|
||||
: base(logger, unitOfWork)
|
||||
{
|
||||
_channels = channels;
|
||||
}
|
||||
|
||||
protected override async Task<ServiceResult<EmailChannel>> DoOnHandle(
|
||||
GetEmailChannelQuery request, CancellationToken cancellationToken)
|
||||
{
|
||||
var channel = await _channels.GetByIdAsync(request.Id, cancellationToken);
|
||||
|
||||
return channel is null
|
||||
? Failure<EmailChannel>("Email channel not found.", ServiceErrorCode.NotFound)
|
||||
: Success(channel);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
using HrynCo.NotificationService.DAL.Abstract.Providers;
|
||||
using MediatR;
|
||||
using HrynCo.NotificationService.Services.Core;
|
||||
|
||||
namespace HrynCo.NotificationService.Services.EmailChannels.Get;
|
||||
|
||||
public sealed record GetEmailChannelQuery(Guid Id)
|
||||
: IRequest<ServiceResult<EmailChannel>>;
|
||||
+30
@@ -0,0 +1,30 @@
|
||||
using HrynCo.NotificationService.DAL.Abstract;
|
||||
using HrynCo.NotificationService.DAL.Abstract.Providers;
|
||||
using HrynCo.NotificationService.DAL.Abstract.Repositories;
|
||||
using HrynCo.NotificationService.Services.Core;
|
||||
using HrynCo.NotificationService.Services.Logging;
|
||||
using static HrynCo.NotificationService.Services.Core.ServiceResultHelper;
|
||||
|
||||
namespace HrynCo.NotificationService.Services.EmailChannels.GetAll;
|
||||
|
||||
internal sealed class GetAllEmailChannelsHandler
|
||||
: RequestHandler<GetAllEmailChannelsQuery, ServiceResult<IReadOnlyList<EmailChannel>>>
|
||||
{
|
||||
private readonly IEmailChannelRepository _channels;
|
||||
|
||||
public GetAllEmailChannelsHandler(
|
||||
IContextualSerilogLogger<GetAllEmailChannelsQuery> logger,
|
||||
IUnitOfWork unitOfWork,
|
||||
IEmailChannelRepository channels)
|
||||
: base(logger, unitOfWork)
|
||||
{
|
||||
_channels = channels;
|
||||
}
|
||||
|
||||
protected override async Task<ServiceResult<IReadOnlyList<EmailChannel>>> DoOnHandle(
|
||||
GetAllEmailChannelsQuery request, CancellationToken cancellationToken)
|
||||
{
|
||||
var channels = await _channels.GetAllAsync(cancellationToken);
|
||||
return Success(channels);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
using HrynCo.NotificationService.DAL.Abstract.Providers;
|
||||
using MediatR;
|
||||
using HrynCo.NotificationService.Services.Core;
|
||||
|
||||
namespace HrynCo.NotificationService.Services.EmailChannels.GetAll;
|
||||
|
||||
public sealed record GetAllEmailChannelsQuery : IRequest<ServiceResult<IReadOnlyList<EmailChannel>>>;
|
||||
+30
@@ -0,0 +1,30 @@
|
||||
using HrynCo.NotificationService.DAL.Abstract;
|
||||
using HrynCo.NotificationService.DAL.Abstract.Providers;
|
||||
using HrynCo.NotificationService.DAL.Abstract.Repositories;
|
||||
using HrynCo.NotificationService.Services.Core;
|
||||
using HrynCo.NotificationService.Services.Logging;
|
||||
using static HrynCo.NotificationService.Services.Core.ServiceResultHelper;
|
||||
|
||||
namespace HrynCo.NotificationService.Services.EmailChannels.GetByService;
|
||||
|
||||
internal sealed class GetEmailChannelsHandler
|
||||
: RequestHandler<GetEmailChannelsQuery, ServiceResult<IReadOnlyList<EmailChannel>>>
|
||||
{
|
||||
private readonly IEmailChannelRepository _channels;
|
||||
|
||||
public GetEmailChannelsHandler(
|
||||
IContextualSerilogLogger<GetEmailChannelsQuery> logger,
|
||||
IUnitOfWork unitOfWork,
|
||||
IEmailChannelRepository channels)
|
||||
: base(logger, unitOfWork)
|
||||
{
|
||||
_channels = channels;
|
||||
}
|
||||
|
||||
protected override async Task<ServiceResult<IReadOnlyList<EmailChannel>>> DoOnHandle(
|
||||
GetEmailChannelsQuery request, CancellationToken cancellationToken)
|
||||
{
|
||||
var channels = await _channels.GetByServiceAsync(request.ServiceName, cancellationToken);
|
||||
return Success(channels);
|
||||
}
|
||||
}
|
||||
+8
@@ -0,0 +1,8 @@
|
||||
using HrynCo.NotificationService.DAL.Abstract.Providers;
|
||||
using MediatR;
|
||||
using HrynCo.NotificationService.Services.Core;
|
||||
|
||||
namespace HrynCo.NotificationService.Services.EmailChannels.GetByService;
|
||||
|
||||
public sealed record GetEmailChannelsQuery(string ServiceName)
|
||||
: IRequest<ServiceResult<IReadOnlyList<EmailChannel>>>;
|
||||
+45
@@ -0,0 +1,45 @@
|
||||
using HrynCo.NotificationService.DAL.Abstract;
|
||||
using HrynCo.NotificationService.DAL.Abstract.Repositories;
|
||||
using HrynCo.NotificationService.Services.Core;
|
||||
using HrynCo.NotificationService.Services.Logging;
|
||||
using static HrynCo.NotificationService.Services.Core.ServiceResultHelper;
|
||||
|
||||
namespace HrynCo.NotificationService.Services.EmailChannels.GetUsageSummary;
|
||||
|
||||
internal sealed class GetChannelUsageSummaryHandler
|
||||
: RequestHandler<GetChannelUsageSummaryQuery, ServiceResult<IReadOnlyList<ChannelUsageEntry>>>
|
||||
{
|
||||
private readonly IEmailChannelRepository _channelsRepository;
|
||||
|
||||
public GetChannelUsageSummaryHandler(
|
||||
IContextualSerilogLogger<GetChannelUsageSummaryQuery> logger,
|
||||
IUnitOfWork unitOfWork,
|
||||
IEmailChannelRepository channelsRepository)
|
||||
: base(logger, unitOfWork)
|
||||
{
|
||||
_channelsRepository = channelsRepository;
|
||||
}
|
||||
|
||||
protected override async Task<ServiceResult<IReadOnlyList<ChannelUsageEntry>>> DoOnHandle(
|
||||
GetChannelUsageSummaryQuery request, CancellationToken cancellationToken)
|
||||
{
|
||||
var today = DateOnly.FromDateTime(DateTime.UtcNow);
|
||||
|
||||
var rows = await _channelsRepository.GetAllWithUsageSummaryAsync(today, cancellationToken);
|
||||
|
||||
var entries = rows
|
||||
.Select(r => new ChannelUsageEntry(
|
||||
ChannelId: r.Channel.Id,
|
||||
ServiceName: r.Channel.ServiceName,
|
||||
ChannelType: r.Channel.EmailChannelType.ToString(),
|
||||
IsActive: r.Channel.IsActive,
|
||||
Priority: r.Channel.Priority,
|
||||
DailyLimit: r.Channel.DailyLimit,
|
||||
MonthlyLimit: r.Channel.MonthlyLimit,
|
||||
DailySent: r.DailySent,
|
||||
MonthlySent: r.MonthlySent))
|
||||
.ToList();
|
||||
|
||||
return Success<IReadOnlyList<ChannelUsageEntry>>(entries);
|
||||
}
|
||||
}
|
||||
+19
@@ -0,0 +1,19 @@
|
||||
using HrynCo.NotificationService.DAL.Abstract.Providers;
|
||||
using HrynCo.NotificationService.Services.Core;
|
||||
using MediatR;
|
||||
|
||||
namespace HrynCo.NotificationService.Services.EmailChannels.GetUsageSummary;
|
||||
|
||||
public sealed record GetChannelUsageSummaryQuery
|
||||
: IRequest<ServiceResult<IReadOnlyList<ChannelUsageEntry>>>;
|
||||
|
||||
public sealed record ChannelUsageEntry(
|
||||
Guid ChannelId,
|
||||
string ServiceName,
|
||||
string ChannelType,
|
||||
bool IsActive,
|
||||
int Priority,
|
||||
int? DailyLimit,
|
||||
int? MonthlyLimit,
|
||||
int DailySent,
|
||||
int MonthlySent);
|
||||
@@ -0,0 +1,17 @@
|
||||
using HrynCo.NotificationService.Services.Core;
|
||||
using MediatR;
|
||||
|
||||
namespace HrynCo.NotificationService.Services.EmailChannels.Send;
|
||||
|
||||
/// <summary>
|
||||
/// Sends an email via the channel associated with the given channel ID,
|
||||
/// then increments the usage counter for that channel.
|
||||
/// </summary>
|
||||
public sealed record SendEmailCommand(
|
||||
Guid ChannelId,
|
||||
string RecipientEmail,
|
||||
string RecipientName,
|
||||
string Subject,
|
||||
string HtmlBody,
|
||||
string? TextBody
|
||||
) : IRequest<ServiceResult<Core.Unit>>;
|
||||
@@ -0,0 +1,80 @@
|
||||
using System.Net;
|
||||
using System.Net.Mail;
|
||||
using HrynCo.NotificationService.DAL.Abstract;
|
||||
using HrynCo.NotificationService.DAL.Abstract.Providers;
|
||||
using HrynCo.NotificationService.DAL.Abstract.Repositories;
|
||||
using HrynCo.NotificationService.Services.Core;
|
||||
using HrynCo.NotificationService.Services.Logging;
|
||||
using static HrynCo.NotificationService.Services.Core.ServiceResultHelper;
|
||||
|
||||
namespace HrynCo.NotificationService.Services.EmailChannels.Send;
|
||||
|
||||
internal sealed class SendEmailHandler
|
||||
: RequestHandler<SendEmailCommand, ServiceResult<Core.Unit>>
|
||||
{
|
||||
private readonly IEmailChannelRepository _channels;
|
||||
private readonly IEmailChannelUsageRepository _usage;
|
||||
|
||||
public SendEmailHandler(
|
||||
IContextualSerilogLogger<SendEmailCommand> logger,
|
||||
IUnitOfWork unitOfWork,
|
||||
IEmailChannelRepository channels,
|
||||
IEmailChannelUsageRepository usage)
|
||||
: base(logger, unitOfWork)
|
||||
{
|
||||
_channels = channels;
|
||||
_usage = usage;
|
||||
}
|
||||
|
||||
protected override async Task<ServiceResult<Core.Unit>> DoOnHandle(
|
||||
SendEmailCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
var channel = await _channels.GetByIdAsync(request.ChannelId, cancellationToken);
|
||||
if (channel is null)
|
||||
return Failure<Core.Unit>($"Channel '{request.ChannelId}' not found.");
|
||||
|
||||
if (channel.Settings is not SmtpChannelSettings smtp)
|
||||
return Failure<Core.Unit>($"Channel type '{channel.EmailChannelType}' is not supported for sending.");
|
||||
|
||||
try
|
||||
{
|
||||
using var client = new SmtpClient(smtp.Host, smtp.Port)
|
||||
{
|
||||
EnableSsl = smtp.UseSsl,
|
||||
Credentials = string.IsNullOrWhiteSpace(smtp.Username)
|
||||
? null
|
||||
: new NetworkCredential(smtp.Username, smtp.Password)
|
||||
};
|
||||
|
||||
using var mail = new MailMessage
|
||||
{
|
||||
From = new MailAddress(smtp.FromEmail, smtp.FromName),
|
||||
Subject = request.Subject,
|
||||
Body = request.HtmlBody,
|
||||
IsBodyHtml = true
|
||||
};
|
||||
|
||||
if (!string.IsNullOrWhiteSpace(request.TextBody))
|
||||
{
|
||||
var plain = AlternateView.CreateAlternateViewFromString(request.TextBody, null, "text/plain");
|
||||
mail.AlternateViews.Add(plain);
|
||||
}
|
||||
|
||||
mail.To.Add(new MailAddress(request.RecipientEmail, request.RecipientName));
|
||||
|
||||
await client.SendMailAsync(mail, cancellationToken);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Logger.Error(ex, "SMTP send failed for channel {ChannelId}", request.ChannelId);
|
||||
return Failure<Core.Unit>(ex.Message);
|
||||
}
|
||||
|
||||
await _usage.IncrementUsageAsync(
|
||||
request.ChannelId,
|
||||
DateOnly.FromDateTime(DateTime.UtcNow),
|
||||
cancellationToken);
|
||||
|
||||
return Success(Unit.Value);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
using HrynCo.NotificationService.DAL.Abstract.Providers;
|
||||
using MediatR;
|
||||
using HrynCo.NotificationService.Services.Core;
|
||||
using Unit = HrynCo.NotificationService.Services.Core.Unit;
|
||||
|
||||
namespace HrynCo.NotificationService.Services.EmailChannels.Update;
|
||||
|
||||
public sealed record UpdateEmailChannelCommand(
|
||||
Guid Id,
|
||||
int Priority,
|
||||
EmailChannelSettings Settings,
|
||||
int? DailyLimit,
|
||||
int? MonthlyLimit,
|
||||
int WarnThresholdPercent,
|
||||
bool IsActive
|
||||
) : IRequest<ServiceResult<Unit>>;
|
||||
@@ -0,0 +1,43 @@
|
||||
using HrynCo.NotificationService.DAL.Abstract;
|
||||
using HrynCo.NotificationService.DAL.Abstract.Repositories;
|
||||
using HrynCo.NotificationService.Services.Core;
|
||||
using HrynCo.NotificationService.Services.Logging;
|
||||
using static HrynCo.NotificationService.Services.Core.ServiceResultHelper;
|
||||
|
||||
namespace HrynCo.NotificationService.Services.EmailChannels.Update;
|
||||
|
||||
internal sealed class UpdateEmailChannelHandler
|
||||
: RequestHandler<UpdateEmailChannelCommand, ServiceResult<Unit>>
|
||||
{
|
||||
private readonly IEmailChannelRepository _channels;
|
||||
|
||||
public UpdateEmailChannelHandler(
|
||||
IContextualSerilogLogger<UpdateEmailChannelCommand> logger,
|
||||
IUnitOfWork unitOfWork,
|
||||
IEmailChannelRepository channels)
|
||||
: base(logger, unitOfWork)
|
||||
{
|
||||
_channels = channels;
|
||||
}
|
||||
|
||||
protected override async Task<ServiceResult<Unit>> DoOnHandle(
|
||||
UpdateEmailChannelCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
var channel = await _channels.GetByIdAsync(request.Id, cancellationToken);
|
||||
|
||||
if (channel is null)
|
||||
return Failure<Unit>("Email channel not found.", ServiceErrorCode.NotFound);
|
||||
|
||||
channel.Priority = request.Priority;
|
||||
channel.Settings = request.Settings;
|
||||
channel.DailyLimit = request.DailyLimit;
|
||||
channel.MonthlyLimit = request.MonthlyLimit;
|
||||
channel.WarnThresholdPercent = request.WarnThresholdPercent;
|
||||
channel.IsActive = request.IsActive;
|
||||
channel.Updated = DateTimeOffset.UtcNow;
|
||||
|
||||
await _channels.UpdateAsync(channel, cancellationToken);
|
||||
|
||||
return Success(Unit.Value);
|
||||
}
|
||||
}
|
||||
+15
@@ -0,0 +1,15 @@
|
||||
using HrynCo.NotificationService.DAL.Abstract.Templates;
|
||||
using MediatR;
|
||||
using HrynCo.NotificationService.Services.Core;
|
||||
|
||||
namespace HrynCo.NotificationService.Services.EmailTemplates.Create;
|
||||
|
||||
public sealed record CreateEmailTemplateCommand(
|
||||
string ServiceName,
|
||||
string Key,
|
||||
string LanguageCode,
|
||||
string Subject,
|
||||
string HtmlBody,
|
||||
string TextBody,
|
||||
IReadOnlyList<EmailTemplateVariable> Variables
|
||||
) : IRequest<ServiceResult<Guid>>;
|
||||
+48
@@ -0,0 +1,48 @@
|
||||
using HrynCo.NotificationService.DAL.Abstract;
|
||||
using HrynCo.NotificationService.DAL.Abstract.Repositories;
|
||||
using HrynCo.NotificationService.DAL.Abstract.Templates;
|
||||
using HrynCo.NotificationService.Services.Core;
|
||||
using HrynCo.NotificationService.Services.Logging;
|
||||
using static HrynCo.NotificationService.Services.Core.ServiceResultHelper;
|
||||
|
||||
namespace HrynCo.NotificationService.Services.EmailTemplates.Create;
|
||||
|
||||
internal sealed class CreateEmailTemplateHandler
|
||||
: RequestHandler<CreateEmailTemplateCommand, ServiceResult<Guid>>
|
||||
{
|
||||
private readonly IEmailTemplateRepository _templates;
|
||||
|
||||
public CreateEmailTemplateHandler(
|
||||
IContextualSerilogLogger<CreateEmailTemplateCommand> logger,
|
||||
IUnitOfWork unitOfWork,
|
||||
IEmailTemplateRepository templates)
|
||||
: base(logger, unitOfWork)
|
||||
{
|
||||
_templates = templates;
|
||||
}
|
||||
|
||||
protected override async Task<ServiceResult<Guid>> DoOnHandle(
|
||||
CreateEmailTemplateCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
var existing = await _templates.GetAsync(
|
||||
request.ServiceName, request.Key, request.LanguageCode, cancellationToken);
|
||||
|
||||
if (existing is not null)
|
||||
return Failure<Guid>("Template already exists.", ServiceErrorCode.Conflict);
|
||||
|
||||
var template = new EmailTemplate
|
||||
{
|
||||
ServiceName = request.ServiceName,
|
||||
Key = request.Key,
|
||||
LanguageCode = request.LanguageCode,
|
||||
Subject = request.Subject,
|
||||
HtmlBody = request.HtmlBody,
|
||||
TextBody = request.TextBody,
|
||||
Variables = request.Variables
|
||||
};
|
||||
|
||||
await _templates.AddAsync(template, cancellationToken);
|
||||
|
||||
return Success(template.Id);
|
||||
}
|
||||
}
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
using MediatR;
|
||||
using HrynCo.NotificationService.Services.Core;
|
||||
using Unit = HrynCo.NotificationService.Services.Core.Unit;
|
||||
|
||||
namespace HrynCo.NotificationService.Services.EmailTemplates.Delete;
|
||||
|
||||
public sealed record DeleteEmailTemplateCommand(
|
||||
string ServiceName,
|
||||
string Key,
|
||||
string LanguageCode
|
||||
) : IRequest<ServiceResult<Unit>>;
|
||||
+36
@@ -0,0 +1,36 @@
|
||||
using HrynCo.NotificationService.DAL.Abstract;
|
||||
using HrynCo.NotificationService.DAL.Abstract.Repositories;
|
||||
using HrynCo.NotificationService.Services.Core;
|
||||
using HrynCo.NotificationService.Services.Logging;
|
||||
using static HrynCo.NotificationService.Services.Core.ServiceResultHelper;
|
||||
|
||||
namespace HrynCo.NotificationService.Services.EmailTemplates.Delete;
|
||||
|
||||
internal sealed class DeleteEmailTemplateHandler
|
||||
: RequestHandler<DeleteEmailTemplateCommand, ServiceResult<Unit>>
|
||||
{
|
||||
private readonly IEmailTemplateRepository _templates;
|
||||
|
||||
public DeleteEmailTemplateHandler(
|
||||
IContextualSerilogLogger<DeleteEmailTemplateCommand> logger,
|
||||
IUnitOfWork unitOfWork,
|
||||
IEmailTemplateRepository templates)
|
||||
: base(logger, unitOfWork)
|
||||
{
|
||||
_templates = templates;
|
||||
}
|
||||
|
||||
protected override async Task<ServiceResult<Unit>> DoOnHandle(
|
||||
DeleteEmailTemplateCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
var template = await _templates.GetAsync(
|
||||
request.ServiceName, request.Key, request.LanguageCode, cancellationToken);
|
||||
|
||||
if (template is null)
|
||||
return Failure<Unit>("Template not found.", ServiceErrorCode.NotFound);
|
||||
|
||||
await _templates.DeleteAsync(template, cancellationToken);
|
||||
|
||||
return Success(Unit.Value);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
using HrynCo.NotificationService.DAL.Abstract;
|
||||
using HrynCo.NotificationService.DAL.Abstract.Repositories;
|
||||
using HrynCo.NotificationService.DAL.Abstract.Templates;
|
||||
using HrynCo.NotificationService.Services.Core;
|
||||
using HrynCo.NotificationService.Services.Logging;
|
||||
using static HrynCo.NotificationService.Services.Core.ServiceResultHelper;
|
||||
|
||||
namespace HrynCo.NotificationService.Services.EmailTemplates.Get;
|
||||
|
||||
internal sealed class GetEmailTemplateHandler
|
||||
: RequestHandler<GetEmailTemplateQuery, ServiceResult<EmailTemplate>>
|
||||
{
|
||||
private readonly IEmailTemplateRepository _templates;
|
||||
|
||||
public GetEmailTemplateHandler(
|
||||
IContextualSerilogLogger<GetEmailTemplateQuery> logger,
|
||||
IUnitOfWork unitOfWork,
|
||||
IEmailTemplateRepository templates)
|
||||
: base(logger, unitOfWork)
|
||||
{
|
||||
_templates = templates;
|
||||
}
|
||||
|
||||
protected override async Task<ServiceResult<EmailTemplate>> DoOnHandle(
|
||||
GetEmailTemplateQuery request, CancellationToken cancellationToken)
|
||||
{
|
||||
var template = await _templates.GetAsync(
|
||||
request.ServiceName, request.Key, request.LanguageCode, cancellationToken);
|
||||
|
||||
return template is null
|
||||
? Failure<EmailTemplate>("Template not found.", ServiceErrorCode.NotFound)
|
||||
: Success(template);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
using HrynCo.NotificationService.DAL.Abstract.Templates;
|
||||
using MediatR;
|
||||
using HrynCo.NotificationService.Services.Core;
|
||||
|
||||
namespace HrynCo.NotificationService.Services.EmailTemplates.Get;
|
||||
|
||||
public sealed record GetEmailTemplateQuery(string ServiceName, string Key, string LanguageCode)
|
||||
: IRequest<ServiceResult<EmailTemplate>>;
|
||||
+30
@@ -0,0 +1,30 @@
|
||||
using HrynCo.NotificationService.DAL.Abstract;
|
||||
using HrynCo.NotificationService.DAL.Abstract.Repositories;
|
||||
using HrynCo.NotificationService.DAL.Abstract.Templates;
|
||||
using HrynCo.NotificationService.Services.Core;
|
||||
using HrynCo.NotificationService.Services.Logging;
|
||||
using static HrynCo.NotificationService.Services.Core.ServiceResultHelper;
|
||||
|
||||
namespace HrynCo.NotificationService.Services.EmailTemplates.GetAll;
|
||||
|
||||
internal sealed class GetAllEmailTemplatesHandler
|
||||
: RequestHandler<GetAllEmailTemplatesQuery, ServiceResult<IReadOnlyList<EmailTemplate>>>
|
||||
{
|
||||
private readonly IEmailTemplateRepository _templates;
|
||||
|
||||
public GetAllEmailTemplatesHandler(
|
||||
IContextualSerilogLogger<GetAllEmailTemplatesQuery> logger,
|
||||
IUnitOfWork unitOfWork,
|
||||
IEmailTemplateRepository templates)
|
||||
: base(logger, unitOfWork)
|
||||
{
|
||||
_templates = templates;
|
||||
}
|
||||
|
||||
protected override async Task<ServiceResult<IReadOnlyList<EmailTemplate>>> DoOnHandle(
|
||||
GetAllEmailTemplatesQuery request, CancellationToken cancellationToken)
|
||||
{
|
||||
var templates = await _templates.GetAllAsync(cancellationToken);
|
||||
return Success(templates);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
using HrynCo.NotificationService.DAL.Abstract.Templates;
|
||||
using MediatR;
|
||||
using HrynCo.NotificationService.Services.Core;
|
||||
|
||||
namespace HrynCo.NotificationService.Services.EmailTemplates.GetAll;
|
||||
|
||||
public sealed record GetAllEmailTemplatesQuery : IRequest<ServiceResult<IReadOnlyList<EmailTemplate>>>;
|
||||
+30
@@ -0,0 +1,30 @@
|
||||
using HrynCo.NotificationService.DAL.Abstract;
|
||||
using HrynCo.NotificationService.DAL.Abstract.Repositories;
|
||||
using HrynCo.NotificationService.DAL.Abstract.Templates;
|
||||
using HrynCo.NotificationService.Services.Core;
|
||||
using HrynCo.NotificationService.Services.Logging;
|
||||
using static HrynCo.NotificationService.Services.Core.ServiceResultHelper;
|
||||
|
||||
namespace HrynCo.NotificationService.Services.EmailTemplates.GetByService;
|
||||
|
||||
internal sealed class GetEmailTemplatesHandler
|
||||
: RequestHandler<GetEmailTemplatesQuery, ServiceResult<IReadOnlyList<EmailTemplate>>>
|
||||
{
|
||||
private readonly IEmailTemplateRepository _templates;
|
||||
|
||||
public GetEmailTemplatesHandler(
|
||||
IContextualSerilogLogger<GetEmailTemplatesQuery> logger,
|
||||
IUnitOfWork unitOfWork,
|
||||
IEmailTemplateRepository templates)
|
||||
: base(logger, unitOfWork)
|
||||
{
|
||||
_templates = templates;
|
||||
}
|
||||
|
||||
protected override async Task<ServiceResult<IReadOnlyList<EmailTemplate>>> DoOnHandle(
|
||||
GetEmailTemplatesQuery request, CancellationToken cancellationToken)
|
||||
{
|
||||
var templates = await _templates.GetByServiceAsync(request.ServiceName, cancellationToken);
|
||||
return Success(templates);
|
||||
}
|
||||
}
|
||||
+8
@@ -0,0 +1,8 @@
|
||||
using HrynCo.NotificationService.DAL.Abstract.Templates;
|
||||
using MediatR;
|
||||
using HrynCo.NotificationService.Services.Core;
|
||||
|
||||
namespace HrynCo.NotificationService.Services.EmailTemplates.GetByService;
|
||||
|
||||
public sealed record GetEmailTemplatesQuery(string ServiceName)
|
||||
: IRequest<ServiceResult<IReadOnlyList<EmailTemplate>>>;
|
||||
+16
@@ -0,0 +1,16 @@
|
||||
using HrynCo.NotificationService.DAL.Abstract.Templates;
|
||||
using MediatR;
|
||||
using HrynCo.NotificationService.Services.Core;
|
||||
using Unit = HrynCo.NotificationService.Services.Core.Unit;
|
||||
|
||||
namespace HrynCo.NotificationService.Services.EmailTemplates.Update;
|
||||
|
||||
public sealed record UpdateEmailTemplateCommand(
|
||||
string ServiceName,
|
||||
string Key,
|
||||
string LanguageCode,
|
||||
string Subject,
|
||||
string HtmlBody,
|
||||
string TextBody,
|
||||
IReadOnlyList<EmailTemplateVariable> Variables
|
||||
) : IRequest<ServiceResult<Unit>>;
|
||||
+42
@@ -0,0 +1,42 @@
|
||||
using HrynCo.NotificationService.DAL.Abstract;
|
||||
using HrynCo.NotificationService.DAL.Abstract.Repositories;
|
||||
using HrynCo.NotificationService.Services.Core;
|
||||
using HrynCo.NotificationService.Services.Logging;
|
||||
using static HrynCo.NotificationService.Services.Core.ServiceResultHelper;
|
||||
|
||||
namespace HrynCo.NotificationService.Services.EmailTemplates.Update;
|
||||
|
||||
internal sealed class UpdateEmailTemplateHandler
|
||||
: RequestHandler<UpdateEmailTemplateCommand, ServiceResult<Unit>>
|
||||
{
|
||||
private readonly IEmailTemplateRepository _templates;
|
||||
|
||||
public UpdateEmailTemplateHandler(
|
||||
IContextualSerilogLogger<UpdateEmailTemplateCommand> logger,
|
||||
IUnitOfWork unitOfWork,
|
||||
IEmailTemplateRepository templates)
|
||||
: base(logger, unitOfWork)
|
||||
{
|
||||
_templates = templates;
|
||||
}
|
||||
|
||||
protected override async Task<ServiceResult<Unit>> DoOnHandle(
|
||||
UpdateEmailTemplateCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
var template = await _templates.GetAsync(
|
||||
request.ServiceName, request.Key, request.LanguageCode, cancellationToken);
|
||||
|
||||
if (template is null)
|
||||
return Failure<Unit>("Template not found.", ServiceErrorCode.NotFound);
|
||||
|
||||
template.Subject = request.Subject;
|
||||
template.HtmlBody = request.HtmlBody;
|
||||
template.TextBody = request.TextBody;
|
||||
template.Variables = request.Variables;
|
||||
template.Updated = DateTimeOffset.UtcNow;
|
||||
|
||||
await _templates.UpdateAsync(template, cancellationToken);
|
||||
|
||||
return Success(Unit.Value);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="HrynCo.Common" />
|
||||
<PackageReference Include="MediatR" />
|
||||
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" />
|
||||
<PackageReference Include="Serilog" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\HrynCo.NotificationService.DAL.Abstract\HrynCo.NotificationService.DAL.Abstract.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net10.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
</Project>
|
||||
@@ -0,0 +1,8 @@
|
||||
using Serilog;
|
||||
|
||||
namespace HrynCo.NotificationService.Services.Logging;
|
||||
|
||||
public sealed class ContextualSerilogLogger<TContext> : IContextualSerilogLogger<TContext>
|
||||
{
|
||||
public ILogger Logger { get; } = Log.ForContext<TContext>();
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using Serilog;
|
||||
|
||||
namespace HrynCo.NotificationService.Services.Logging;
|
||||
|
||||
[SuppressMessage("Major Code Smell", "S2326:Unused type parameters should be removed",
|
||||
Justification = "Generic parameter used in implementation via ForContext<T>.")]
|
||||
public interface IContextualSerilogLogger<TContext>
|
||||
{
|
||||
ILogger Logger { get; }
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
using HrynCo.Common;
|
||||
using HrynCo.NotificationService.Services.Behaviors;
|
||||
using HrynCo.NotificationService.Services.Logging;
|
||||
using MediatR;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Serilog;
|
||||
|
||||
namespace HrynCo.NotificationService.Services;
|
||||
|
||||
public static class ServiceCollectionExtensions
|
||||
{
|
||||
public static IServiceCollection AddNotificationServices(this IServiceCollection services)
|
||||
{
|
||||
services.AddMediatR(cfg =>
|
||||
{
|
||||
cfg.RegisterServicesFromAssembly(typeof(ServiceCollectionExtensions).Assembly);
|
||||
cfg.AddBehavior(typeof(IPipelineBehavior<,>), typeof(TransactionBehavior<,>));
|
||||
});
|
||||
|
||||
services.AddTransient(typeof(IContextualSerilogLogger<>), typeof(ContextualSerilogLogger<>));
|
||||
services.AddSingleton<IProfiler>(new Profiler(Log.Logger));
|
||||
|
||||
return services;
|
||||
}
|
||||
}
|
||||
+25
@@ -0,0 +1,25 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net10.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
<IsPackable>false</IsPackable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="coverlet.collector" />
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" />
|
||||
<PackageReference Include="xunit" />
|
||||
<PackageReference Include="xunit.runner.visualstudio" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Using Include="Xunit" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\HrynCo.NotificationService.Web\HrynCo.NotificationService.Web.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
@@ -0,0 +1,10 @@
|
||||
namespace HrynCo.NotificationService.Web.IntegrationTests;
|
||||
|
||||
public class UnitTest1
|
||||
{
|
||||
[Fact]
|
||||
public void Test1()
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
namespace HrynCo.NotificationService.Web;
|
||||
|
||||
public sealed class AppSettings
|
||||
{
|
||||
public const string SectionName = "App";
|
||||
|
||||
public string ConnectionString { get; init; } = string.Empty;
|
||||
}
|
||||
@@ -0,0 +1,167 @@
|
||||
using HrynCo.NotificationService.DAL.Abstract.Providers;
|
||||
using HrynCo.NotificationService.DAL.Abstract.Repositories;
|
||||
using HrynCo.NotificationService.Services.EmailChannels.Create;
|
||||
using HrynCo.NotificationService.Services.EmailChannels.Delete;
|
||||
using HrynCo.NotificationService.Services.EmailChannels.Get;
|
||||
using HrynCo.NotificationService.Services.EmailChannels.GetUsageSummary;
|
||||
using HrynCo.NotificationService.Services.EmailChannels.Send;
|
||||
using HrynCo.NotificationService.Services.EmailChannels.Update;
|
||||
using HrynCo.NotificationService.Web.Controllers.Admin.ViewModels;
|
||||
using MediatR;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
namespace HrynCo.NotificationService.Web.Controllers.Admin;
|
||||
|
||||
[Route("admin/channels")]
|
||||
public class AdminChannelsController(IMediator mediator) : Controller
|
||||
{
|
||||
|
||||
// GET /admin/channels
|
||||
[HttpGet("")]
|
||||
public async Task<IActionResult> Index(CancellationToken ct)
|
||||
{
|
||||
var result = await mediator.Send(new GetChannelUsageSummaryQuery(), ct);
|
||||
if (!result.IsSuccess)
|
||||
{
|
||||
ModelState.AddModelError("", result.Error?.Message ?? "Failed to load channels.");
|
||||
return View(Array.Empty<ChannelUsageEntry>());
|
||||
}
|
||||
|
||||
return View(result.Result);
|
||||
}
|
||||
|
||||
// GET /admin/channels/create
|
||||
[HttpGet("create")]
|
||||
public IActionResult Create()
|
||||
{
|
||||
return View("Edit", new EmailChannelEditViewModel());
|
||||
}
|
||||
|
||||
// GET /admin/channels/{id}
|
||||
[HttpGet("{id:guid}")]
|
||||
public async Task<IActionResult> Edit(Guid id, CancellationToken ct)
|
||||
{
|
||||
var result = await mediator.Send(new GetEmailChannelQuery(id), ct);
|
||||
if (!result.IsSuccess || result.Result is null)
|
||||
return NotFound();
|
||||
|
||||
var channel = result.Result;
|
||||
var smtp = channel.Settings as SmtpChannelSettings ?? new SmtpChannelSettings
|
||||
{
|
||||
Host = "", Username = "", Password = "", FromEmail = "", FromName = ""
|
||||
};
|
||||
|
||||
var vm = new EmailChannelEditViewModel
|
||||
{
|
||||
Id = channel.Id,
|
||||
ServiceName = channel.ServiceName,
|
||||
Priority = channel.Priority,
|
||||
EmailChannelType = channel.EmailChannelType,
|
||||
DailyLimit = channel.DailyLimit,
|
||||
MonthlyLimit = channel.MonthlyLimit,
|
||||
WarnThresholdPercent = channel.WarnThresholdPercent,
|
||||
IsActive = channel.IsActive,
|
||||
Host = smtp.Host,
|
||||
Port = smtp.Port,
|
||||
Username = smtp.Username,
|
||||
Password = smtp.Password,
|
||||
UseSsl = smtp.UseSsl,
|
||||
FromEmail = smtp.FromEmail,
|
||||
FromName = smtp.FromName
|
||||
};
|
||||
|
||||
return View(vm);
|
||||
}
|
||||
|
||||
// POST /admin/channels/save
|
||||
[HttpPost("save")]
|
||||
[ValidateAntiForgeryToken]
|
||||
public async Task<IActionResult> Save(EmailChannelEditViewModel model, CancellationToken ct)
|
||||
{
|
||||
if (!ModelState.IsValid)
|
||||
return View("Edit", model);
|
||||
|
||||
var smtpSettings = new SmtpChannelSettings
|
||||
{
|
||||
Host = model.Host,
|
||||
Port = model.Port,
|
||||
Username = model.Username,
|
||||
Password = model.Password,
|
||||
UseSsl = model.UseSsl,
|
||||
FromEmail = model.FromEmail,
|
||||
FromName = model.FromName
|
||||
};
|
||||
|
||||
if (model.IsNew)
|
||||
{
|
||||
var command = new CreateEmailChannelCommand(
|
||||
model.ServiceName,
|
||||
model.Priority,
|
||||
EmailChannelType.Smtp,
|
||||
smtpSettings,
|
||||
model.DailyLimit,
|
||||
model.MonthlyLimit,
|
||||
model.WarnThresholdPercent,
|
||||
model.IsActive);
|
||||
|
||||
var result = await mediator.Send(command, ct);
|
||||
if (!result.IsSuccess)
|
||||
{
|
||||
ModelState.AddModelError("", result.Error?.Message ?? "Failed to create channel.");
|
||||
return View("Edit", model);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
var command = new UpdateEmailChannelCommand(
|
||||
model.Id,
|
||||
model.Priority,
|
||||
smtpSettings,
|
||||
model.DailyLimit,
|
||||
model.MonthlyLimit,
|
||||
model.WarnThresholdPercent,
|
||||
model.IsActive);
|
||||
|
||||
var result = await mediator.Send(command, ct);
|
||||
if (!result.IsSuccess)
|
||||
{
|
||||
ModelState.AddModelError("", result.Error?.Message ?? "Failed to update channel.");
|
||||
return View("Edit", model);
|
||||
}
|
||||
}
|
||||
|
||||
return RedirectToAction(nameof(Index));
|
||||
}
|
||||
|
||||
// POST /admin/channels/{id}/test
|
||||
[HttpPost("{id:guid}/test")]
|
||||
public async Task<IActionResult> Test(Guid id, [FromBody] TestChannelRequest request, CancellationToken ct)
|
||||
{
|
||||
var channelResult = await mediator.Send(new GetEmailChannelQuery(id), ct);
|
||||
if (!channelResult.IsSuccess || channelResult.Result is null)
|
||||
return NotFound(new { success = false, message = "Channel not found." });
|
||||
|
||||
var channel = channelResult.Result;
|
||||
var subject = "✅ Test email from Notification Service";
|
||||
var body = $"This is a test email sent from the Notification Service admin panel.\n\nChannel: {channel.ServiceName}";
|
||||
|
||||
var sendResult = await mediator.Send(
|
||||
new SendEmailCommand(id, request.ToEmail, request.ToEmail, subject, body, null), ct);
|
||||
|
||||
if (!sendResult.IsSuccess)
|
||||
return Ok(new { success = false, message = sendResult.Error?.Message });
|
||||
|
||||
return Ok(new { success = true, message = $"Test email sent to {request.ToEmail}." });
|
||||
}
|
||||
|
||||
// POST /admin/channels/{id}/delete
|
||||
[HttpPost("{id:guid}/delete")]
|
||||
[ValidateAntiForgeryToken]
|
||||
public async Task<IActionResult> Delete(Guid id, CancellationToken ct)
|
||||
{
|
||||
await mediator.Send(new DeleteEmailChannelCommand(id), ct);
|
||||
return RedirectToAction(nameof(Index));
|
||||
}
|
||||
}
|
||||
|
||||
public record TestChannelRequest(string ToEmail);
|
||||
@@ -0,0 +1,138 @@
|
||||
using System.Text.Json;
|
||||
using HrynCo.NotificationService.DAL.Abstract.Templates;
|
||||
using HrynCo.NotificationService.Services.EmailTemplates.Create;
|
||||
using HrynCo.NotificationService.Services.EmailTemplates.Delete;
|
||||
using HrynCo.NotificationService.Services.EmailTemplates.Get;
|
||||
using HrynCo.NotificationService.Services.EmailTemplates.GetAll;
|
||||
using HrynCo.NotificationService.Services.EmailTemplates.Update;
|
||||
using HrynCo.NotificationService.Web.Controllers.Admin.ViewModels;
|
||||
using MediatR;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
namespace HrynCo.NotificationService.Web.Controllers.Admin;
|
||||
|
||||
[Route("admin/templates")]
|
||||
public class AdminTemplatesController : Controller
|
||||
{
|
||||
private readonly IMediator _mediator;
|
||||
|
||||
public AdminTemplatesController(IMediator mediator)
|
||||
{
|
||||
_mediator = mediator;
|
||||
}
|
||||
|
||||
// GET /admin/templates
|
||||
[HttpGet("")]
|
||||
public async Task<IActionResult> Index(CancellationToken ct)
|
||||
{
|
||||
var result = await _mediator.Send(new GetAllEmailTemplatesQuery(), ct);
|
||||
if (!result.IsSuccess)
|
||||
{
|
||||
ModelState.AddModelError("", result.Error?.Message ?? "Failed to load templates.");
|
||||
return View(Array.Empty<EmailTemplate>());
|
||||
}
|
||||
|
||||
return View(result.Result);
|
||||
}
|
||||
|
||||
// GET /admin/templates/create
|
||||
[HttpGet("create")]
|
||||
public IActionResult Create()
|
||||
{
|
||||
return View("Edit", new EmailTemplateEditViewModel());
|
||||
}
|
||||
|
||||
// GET /admin/templates/{serviceName}/{key}/{languageCode}
|
||||
[HttpGet("{serviceName}/{key}/{languageCode}")]
|
||||
public async Task<IActionResult> Edit(string serviceName, string key, string languageCode, CancellationToken ct)
|
||||
{
|
||||
var result = await _mediator.Send(new GetEmailTemplateQuery(serviceName, key, languageCode), ct);
|
||||
if (!result.IsSuccess || result.Result is null)
|
||||
return NotFound();
|
||||
|
||||
var template = result.Result;
|
||||
var vm = new EmailTemplateEditViewModel
|
||||
{
|
||||
Id = template.Id,
|
||||
ServiceName = template.ServiceName,
|
||||
Key = template.Key,
|
||||
LanguageCode = template.LanguageCode,
|
||||
Subject = template.Subject,
|
||||
HtmlBody = template.HtmlBody,
|
||||
TextBody = template.TextBody,
|
||||
VariablesJson = JsonSerializer.Serialize(template.Variables)
|
||||
};
|
||||
|
||||
return View(vm);
|
||||
}
|
||||
|
||||
// POST /admin/templates/save
|
||||
[HttpPost("save")]
|
||||
[ValidateAntiForgeryToken]
|
||||
public async Task<IActionResult> Save(EmailTemplateEditViewModel model, CancellationToken ct)
|
||||
{
|
||||
if (!ModelState.IsValid)
|
||||
return View("Edit", model);
|
||||
|
||||
List<EmailTemplateVariable> variables;
|
||||
try
|
||||
{
|
||||
variables = JsonSerializer.Deserialize<List<EmailTemplateVariable>>(model.VariablesJson,
|
||||
new JsonSerializerOptions { PropertyNameCaseInsensitive = true })
|
||||
?? [];
|
||||
}
|
||||
catch (JsonException)
|
||||
{
|
||||
ModelState.AddModelError(nameof(model.VariablesJson), "Invalid JSON format for variables.");
|
||||
return View("Edit", model);
|
||||
}
|
||||
|
||||
if (model.IsNew)
|
||||
{
|
||||
var command = new CreateEmailTemplateCommand(
|
||||
model.ServiceName,
|
||||
model.Key,
|
||||
model.LanguageCode,
|
||||
model.Subject,
|
||||
model.HtmlBody,
|
||||
model.TextBody,
|
||||
variables);
|
||||
|
||||
var result = await _mediator.Send(command, ct);
|
||||
if (!result.IsSuccess)
|
||||
{
|
||||
ModelState.AddModelError("", result.Error?.Message ?? "Failed to create template.");
|
||||
return View("Edit", model);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
var command = new UpdateEmailTemplateCommand(
|
||||
model.ServiceName,
|
||||
model.Key,
|
||||
model.LanguageCode,
|
||||
model.Subject,
|
||||
model.HtmlBody,
|
||||
model.TextBody,
|
||||
variables);
|
||||
|
||||
var result = await _mediator.Send(command, ct);
|
||||
if (!result.IsSuccess)
|
||||
{
|
||||
ModelState.AddModelError("", result.Error?.Message ?? "Failed to update template.");
|
||||
return View("Edit", model);
|
||||
}
|
||||
}
|
||||
|
||||
return RedirectToAction(nameof(Index));
|
||||
}
|
||||
|
||||
// POST /admin/templates/{serviceName}/{key}/{languageCode}/delete
|
||||
[HttpPost("{serviceName}/{key}/{languageCode}/delete")]
|
||||
[ValidateAntiForgeryToken]
|
||||
public async Task<IActionResult> Delete(string serviceName, string key, string languageCode, CancellationToken ct)
|
||||
{
|
||||
await _mediator.Send(new DeleteEmailTemplateCommand(serviceName, key, languageCode), ct);
|
||||
return RedirectToAction(nameof(Index));
|
||||
}
|
||||
}
|
||||
+51
@@ -0,0 +1,51 @@
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using HrynCo.NotificationService.DAL.Abstract.Providers;
|
||||
|
||||
namespace HrynCo.NotificationService.Web.Controllers.Admin.ViewModels;
|
||||
|
||||
public class EmailChannelEditViewModel
|
||||
{
|
||||
public Guid Id { get; set; }
|
||||
|
||||
// ── Channel fields ─────────────────────────────────────────────────
|
||||
[Required]
|
||||
public string ServiceName { get; set; } = "";
|
||||
|
||||
public int Priority { get; set; } = 1;
|
||||
|
||||
public EmailChannelType EmailChannelType { get; set; } = EmailChannelType.Smtp;
|
||||
|
||||
public int? DailyLimit { get; set; }
|
||||
|
||||
public int? MonthlyLimit { get; set; }
|
||||
|
||||
[Range(1, 100)]
|
||||
public int WarnThresholdPercent { get; set; } = 90;
|
||||
|
||||
public bool IsActive { get; set; } = true;
|
||||
|
||||
// ── SMTP Settings ──────────────────────────────────────────────────
|
||||
[Required]
|
||||
public string Host { get; set; } = "";
|
||||
|
||||
[Range(1, 65535)]
|
||||
public int Port { get; set; } = 587;
|
||||
|
||||
[Required]
|
||||
public string Username { get; set; } = "";
|
||||
|
||||
[Required]
|
||||
public string Password { get; set; } = "";
|
||||
|
||||
public bool UseSsl { get; set; } = true;
|
||||
|
||||
[Required, EmailAddress]
|
||||
public string FromEmail { get; set; } = "";
|
||||
|
||||
[Required]
|
||||
public string FromName { get; set; } = "";
|
||||
|
||||
// ── Computed ───────────────────────────────────────────────────────
|
||||
public bool IsNew => Id == Guid.Empty;
|
||||
public string PageTitle => IsNew ? "Create Channel" : "Edit Channel";
|
||||
}
|
||||
+31
@@ -0,0 +1,31 @@
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
|
||||
namespace HrynCo.NotificationService.Web.Controllers.Admin.ViewModels;
|
||||
|
||||
public class EmailTemplateEditViewModel
|
||||
{
|
||||
public Guid? Id { get; set; }
|
||||
|
||||
[Required]
|
||||
public string ServiceName { get; set; } = "";
|
||||
|
||||
[Required]
|
||||
public string Key { get; set; } = "";
|
||||
|
||||
[Required]
|
||||
public string LanguageCode { get; set; } = "";
|
||||
|
||||
[Required]
|
||||
public string Subject { get; set; } = "";
|
||||
|
||||
[Required]
|
||||
public string HtmlBody { get; set; } = "";
|
||||
|
||||
public string TextBody { get; set; } = "";
|
||||
|
||||
// JSON array: [{"name":"UserName","required":true}, ...]
|
||||
public string VariablesJson { get; set; } = "[]";
|
||||
|
||||
public bool IsNew => Id == null;
|
||||
public string PageTitle => IsNew ? "Create Email Template" : "Edit Email Template";
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
using HrynCo.NotificationService.Web.Infrastructure;
|
||||
using HrynCo.NotificationService.Services.Core;
|
||||
using MediatR;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
namespace HrynCo.NotificationService.Web.Controllers.Api;
|
||||
|
||||
[Route("api/v1/[controller]")]
|
||||
[ApiController]
|
||||
public abstract class ApiControllerBase : ControllerBase
|
||||
{
|
||||
protected ApiControllerBase(IMediator mediator)
|
||||
{
|
||||
Mediator = mediator;
|
||||
}
|
||||
|
||||
protected IMediator Mediator { get; }
|
||||
|
||||
protected IActionResult FromServiceResult<T>(ServiceResult<T> result) =>
|
||||
result.IsSuccess
|
||||
? Ok(new ApiResponse<T> { Success = true, Data = result.Result })
|
||||
: MapServiceError(result.Error!);
|
||||
|
||||
protected IActionResult CreatedFromServiceResult<T>(ServiceResult<Guid> result, string actionName, Func<Guid, T> routeValues) =>
|
||||
result.IsSuccess
|
||||
? CreatedAtAction(actionName, routeValues(result.Result), new ApiResponse<Guid> { Success = true, Data = result.Result })
|
||||
: MapServiceError(result.Error!);
|
||||
|
||||
protected IActionResult MapServiceError(ServiceError error)
|
||||
{
|
||||
string code = error.Code?.ToString() ?? "Unknown";
|
||||
|
||||
return error.Code switch
|
||||
{
|
||||
ServiceErrorCode.NotFound => NotFound(ErrorResponse(code, error.Message)),
|
||||
ServiceErrorCode.Conflict => Conflict(ErrorResponse(code, error.Message)),
|
||||
ServiceErrorCode.InvalidRequest => BadRequest(ErrorResponse(code, error.Message)),
|
||||
null => throw new InvalidOperationException("Error code was null for failed result."),
|
||||
_ => throw new ArgumentOutOfRangeException(nameof(error), error, "Unexpected error code.")
|
||||
};
|
||||
}
|
||||
|
||||
private static ApiResponse<object> ErrorResponse(string code, string message) =>
|
||||
new() { Success = false, Error = new ApiError { Code = code, Message = message } };
|
||||
}
|
||||
+14
@@ -0,0 +1,14 @@
|
||||
using HrynCo.NotificationService.DAL.Abstract.Providers;
|
||||
|
||||
namespace HrynCo.NotificationService.Web.Controllers.Api.EmailChannels;
|
||||
|
||||
public sealed record CreateEmailChannelRequest(
|
||||
string ServiceName,
|
||||
int Priority,
|
||||
EmailChannelType ChannelType,
|
||||
EmailChannelSettings Settings,
|
||||
int? DailyLimit,
|
||||
int? MonthlyLimit,
|
||||
int WarnThresholdPercent,
|
||||
bool IsActive
|
||||
);
|
||||
+77
@@ -0,0 +1,77 @@
|
||||
using HrynCo.NotificationService.Web.Infrastructure;
|
||||
using HrynCo.NotificationService.Services.EmailChannels.Create;
|
||||
using HrynCo.NotificationService.Services.EmailChannels.Delete;
|
||||
using HrynCo.NotificationService.Services.EmailChannels.Get;
|
||||
using HrynCo.NotificationService.Services.EmailChannels.GetByService;
|
||||
using HrynCo.NotificationService.Services.EmailChannels.Update;
|
||||
using MediatR;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
namespace HrynCo.NotificationService.Web.Controllers.Api.EmailChannels;
|
||||
|
||||
[Route("api/v1/email-channels")]
|
||||
public sealed class EmailChannelsController : ApiControllerBase
|
||||
{
|
||||
public EmailChannelsController(IMediator mediator) : base(mediator) { }
|
||||
|
||||
[HttpGet]
|
||||
public async Task<IActionResult> GetAll([FromQuery] string serviceName, CancellationToken cancellationToken)
|
||||
{
|
||||
var result = await Mediator.Send(new GetEmailChannelsQuery(serviceName), cancellationToken);
|
||||
return FromServiceResult(result);
|
||||
}
|
||||
|
||||
[HttpGet("{id:guid}")]
|
||||
public async Task<IActionResult> Get(Guid id, CancellationToken cancellationToken)
|
||||
{
|
||||
var result = await Mediator.Send(new GetEmailChannelQuery(id), cancellationToken);
|
||||
return FromServiceResult(result);
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
public async Task<IActionResult> Create([FromBody] CreateEmailChannelRequest request, CancellationToken cancellationToken)
|
||||
{
|
||||
var command = new CreateEmailChannelCommand(
|
||||
request.ServiceName,
|
||||
request.Priority,
|
||||
request.ChannelType,
|
||||
request.Settings,
|
||||
request.DailyLimit,
|
||||
request.MonthlyLimit,
|
||||
request.WarnThresholdPercent,
|
||||
request.IsActive
|
||||
);
|
||||
|
||||
var result = await Mediator.Send(command, cancellationToken);
|
||||
|
||||
if (!result.IsSuccess)
|
||||
return MapServiceError(result.Error!);
|
||||
|
||||
return CreatedAtAction(nameof(Get), new { id = result.Result },
|
||||
new ApiResponse<Guid> { Success = true, Data = result.Result });
|
||||
}
|
||||
|
||||
[HttpPut("{id:guid}")]
|
||||
public async Task<IActionResult> Update(Guid id, [FromBody] UpdateEmailChannelRequest request, CancellationToken cancellationToken)
|
||||
{
|
||||
var command = new UpdateEmailChannelCommand(
|
||||
id,
|
||||
request.Priority,
|
||||
request.Settings,
|
||||
request.DailyLimit,
|
||||
request.MonthlyLimit,
|
||||
request.WarnThresholdPercent,
|
||||
request.IsActive
|
||||
);
|
||||
|
||||
var result = await Mediator.Send(command, cancellationToken);
|
||||
return FromServiceResult(result);
|
||||
}
|
||||
|
||||
[HttpDelete("{id:guid}")]
|
||||
public async Task<IActionResult> Delete(Guid id, CancellationToken cancellationToken)
|
||||
{
|
||||
var result = await Mediator.Send(new DeleteEmailChannelCommand(id), cancellationToken);
|
||||
return FromServiceResult(result);
|
||||
}
|
||||
}
|
||||
+12
@@ -0,0 +1,12 @@
|
||||
using HrynCo.NotificationService.DAL.Abstract.Providers;
|
||||
|
||||
namespace HrynCo.NotificationService.Web.Controllers.Api.EmailChannels;
|
||||
|
||||
public sealed record UpdateEmailChannelRequest(
|
||||
int Priority,
|
||||
EmailChannelSettings Settings,
|
||||
int? DailyLimit,
|
||||
int? MonthlyLimit,
|
||||
int WarnThresholdPercent,
|
||||
bool IsActive
|
||||
);
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user