-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Notification audit package - log sent notifications (#14)
Co-authored-by: Khaperskaia, Anna <[email protected]> Co-authored-by: Artem Leshchev <[email protected]>
- Loading branch information
1 parent
a47c471
commit 806477a
Showing
14 changed files
with
245 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
src/Bss.Platform.Notifications.Audit/Bss.Platform.Notifications.Audit.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
<PropertyGroup> | ||
<PackageId>Luxoft.Bss.Platform.Notifications.Audit</PackageId> | ||
</PropertyGroup> | ||
<ItemGroup> | ||
<ProjectReference Include="..\Bss.Platform.Notifications\Bss.Platform.Notifications.csproj"/> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<PackageReference Include="Dapper"/> | ||
<PackageReference Include="Microsoft.SqlServer.SqlManagementObjects"/> | ||
</ItemGroup> | ||
</Project> |
24 changes: 24 additions & 0 deletions
24
src/Bss.Platform.Notifications.Audit/DependencyInjection.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
using Bss.Platform.Notifications.Audit.Models; | ||
using Bss.Platform.Notifications.Audit.Services; | ||
using Bss.Platform.Notifications.Interfaces; | ||
|
||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.Options; | ||
|
||
namespace Bss.Platform.Notifications.Audit; | ||
|
||
public static class DependencyInjection | ||
{ | ||
public static IServiceCollection AddPlatformNotificationsAudit( | ||
this IServiceCollection services, | ||
Action<NotificationAuditOptions>? setup = null) | ||
{ | ||
var settings = new NotificationAuditOptions(); | ||
setup?.Invoke(settings); | ||
|
||
return services | ||
.AddHostedService<AuditSchemaMigrationService>() | ||
.AddScoped<IAuditService, AuditService>() | ||
.AddSingleton<IOptions<NotificationAuditOptions>>(_ => Options.Create(settings)); | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
src/Bss.Platform.Notifications.Audit/Models/NotificationAuditOptions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
namespace Bss.Platform.Notifications.Audit.Models; | ||
|
||
public class NotificationAuditOptions | ||
{ | ||
public string Schema { get; set; } = "notifications"; | ||
|
||
public string Table { get; set; } = "SentMessages"; | ||
|
||
public string ConnectionString { get; set; } = default!; | ||
} |
65 changes: 65 additions & 0 deletions
65
src/Bss.Platform.Notifications.Audit/Services/AuditSchemaMigrationService.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
using Bss.Platform.Notifications.Audit.Models; | ||
|
||
using Microsoft.Data.SqlClient; | ||
using Microsoft.Extensions.Hosting; | ||
using Microsoft.Extensions.Logging; | ||
using Microsoft.Extensions.Options; | ||
using Microsoft.SqlServer.Management.Common; | ||
using Microsoft.SqlServer.Management.Smo; | ||
|
||
namespace Bss.Platform.Notifications.Audit.Services; | ||
|
||
public class AuditSchemaMigrationService(ILogger<AuditSchemaMigrationService> logger, IOptions<NotificationAuditOptions> settings) | ||
: BackgroundService | ||
{ | ||
protected override async Task ExecuteAsync(CancellationToken stoppingToken) | ||
{ | ||
try | ||
{ | ||
await using var connection = new SqlConnection(settings.Value.ConnectionString); | ||
await connection.OpenAsync(stoppingToken); | ||
|
||
var server = new Server(new ServerConnection(connection)); | ||
var catalog = server.ConnectionContext.CurrentDatabase; | ||
|
||
if (string.IsNullOrWhiteSpace(catalog) || !server.Databases.Contains(catalog)) | ||
{ | ||
throw new ArgumentException("Initial catalog not provided or does not exist"); | ||
} | ||
|
||
var database = server.Databases[catalog]; | ||
|
||
if (!database.Tables.Contains(settings.Value.Table, settings.Value.Schema)) | ||
{ | ||
if (!database.Schemas.Contains(settings.Value.Schema)) | ||
{ | ||
logger.LogInformation("Creating schema [{Schema}] ...", settings.Value.Schema); | ||
server.ConnectionContext.ExecuteNonQuery($"CREATE SCHEMA [{settings.Value.Schema}]"); | ||
} | ||
|
||
logger.LogInformation("Creating table [{Table}] ...", settings.Value.Table); | ||
server.ConnectionContext.ExecuteNonQuery( | ||
$""" | ||
CREATE TABLE [{settings.Value.Schema}].[{settings.Value.Table}] ( | ||
[id] [uniqueidentifier] NOT NULL PRIMARY KEY, | ||
[from] [nvarchar](255) NOT NULL, | ||
[to] [nvarchar](max) NULL, | ||
[copy] [nvarchar](max) NULL, | ||
[replyTo] [nvarchar](max) NULL, | ||
[subject] [nvarchar](max) NULL, | ||
[message] [nvarchar](max) NULL, | ||
[timestamp] [datetime2](7) NOT NULL) | ||
"""); | ||
} | ||
|
||
await connection.CloseAsync(); | ||
|
||
logger.LogInformation("Schema migration successfully complete"); | ||
} | ||
catch (Exception e) | ||
{ | ||
logger.LogError(e, "Schema migration was failed"); | ||
throw; | ||
} | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
src/Bss.Platform.Notifications.Audit/Services/AuditService.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
using System.Net.Mail; | ||
|
||
using Bss.Platform.Notifications.Audit.Models; | ||
using Bss.Platform.Notifications.Interfaces; | ||
|
||
using Dapper; | ||
|
||
using Microsoft.Data.SqlClient; | ||
using Microsoft.Extensions.Logging; | ||
using Microsoft.Extensions.Options; | ||
|
||
namespace Bss.Platform.Notifications.Audit.Services; | ||
|
||
public class AuditService(ILogger<AuditService> logger, IOptions<NotificationAuditOptions> settings) : IAuditService | ||
{ | ||
private const string Sql = """ | ||
insert into [{0}].[{1}] | ||
([id], [from], [to], [copy], [replyTo], [subject], [message], [timestamp]) | ||
values | ||
(newid(), @from, @to, @copy, @replyTo, @subject, @message, getdate()) | ||
"""; | ||
|
||
public async Task LogAsync(MailMessage message, CancellationToken token) | ||
{ | ||
try | ||
{ | ||
await using var db = new SqlConnection(settings.Value.ConnectionString); | ||
await db.OpenAsync(token); | ||
|
||
await db.ExecuteAsync( | ||
string.Format(Sql, settings.Value.Schema, settings.Value.Table), | ||
new | ||
{ | ||
from = message.From!.Address, | ||
to = string.Join(";", message.To.Select(x => x.Address)), | ||
copy = string.Join(";", message.CC.Select(x => x.Address)), | ||
replyTo = string.Join(";", message.ReplyToList.Select(x => x.Address)), | ||
subject = message.Subject, | ||
message = message.Body | ||
}); | ||
} | ||
catch (Exception e) | ||
{ | ||
logger.LogError(e, "Failed to log sent message"); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
using System.Net.Mail; | ||
|
||
namespace Bss.Platform.Notifications.Interfaces; | ||
|
||
public interface IAuditService | ||
{ | ||
Task LogAsync(MailMessage message, CancellationToken token); | ||
} |
8 changes: 0 additions & 8 deletions
8
src/Bss.Platform.Notifications/Interfaces/IRedirectService.cs
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.