Compare commits
36 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 1c028313ba | |||
| c5eee69018 | |||
| 7ce4087d08 | |||
| 2dd5e318d7 | |||
| d5b7605da5 | |||
| 121ebbfbcd | |||
| 5a465d7257 | |||
| 7b640fe99e | |||
| 8e81572386 | |||
| 81654c0d33 | |||
| 36810dc5f1 | |||
| 8d9ed96f2c | |||
| a684caf7f7 | |||
| 27f3603d8f | |||
| a44b98292f | |||
| b088640e9b | |||
| 26847f1e9f | |||
| 50033a5bd4 | |||
| 01c622038d | |||
| 1ceb71b9a2 | |||
| bb0de0f2c1 | |||
| d5a2e538f0 | |||
| 9d6b0717e6 | |||
| 2757869176 | |||
| b8435ac07b | |||
| cc3857a409 | |||
| 94f0d45aaf | |||
| 07f536938f | |||
| e7d3953747 | |||
| 3381fcc2f8 | |||
| 3bd952ef9e | |||
| 285cc6abb7 | |||
| 9589095760 | |||
| 25fb48ccf0 | |||
| ab042c7617 | |||
| 0861e18cec |
@@ -7,3 +7,4 @@
|
|||||||
**/*.suo
|
**/*.suo
|
||||||
**/TestResults
|
**/TestResults
|
||||||
NuGet.Config
|
NuGet.Config
|
||||||
|
docker/kubernetes/
|
||||||
|
|||||||
@@ -480,3 +480,6 @@ $RECYCLE.BIN/
|
|||||||
|
|
||||||
# Vim temporary swap files
|
# Vim temporary swap files
|
||||||
*.swp
|
*.swp
|
||||||
|
|
||||||
|
# Local Docker Compose secrets and ports
|
||||||
|
docker/environments/.env.local
|
||||||
|
|||||||
@@ -0,0 +1,27 @@
|
|||||||
|
# HrynCo Notification Service Agent Rules
|
||||||
|
|
||||||
|
## Git workflow
|
||||||
|
|
||||||
|
- Treat `main` as the only default and integration branch for this repository.
|
||||||
|
- Start every task branch from the latest `origin/main`.
|
||||||
|
- Before creating a task branch, fetch the remote, switch to `main`, and fast-forward it from `origin/main`.
|
||||||
|
- Create the task branch only after confirming that local `main` matches `origin/main`.
|
||||||
|
- Open pull requests from the task branch into `main`.
|
||||||
|
- Do not use `development` as the base or pull-request target for new work in this repository.
|
||||||
|
- Do not commit, push, publish, deploy, or merge unless the repository owner explicitly requests that step.
|
||||||
|
|
||||||
|
Recommended branch preparation:
|
||||||
|
|
||||||
|
```text
|
||||||
|
git fetch origin
|
||||||
|
git switch main
|
||||||
|
git pull --ff-only origin main
|
||||||
|
git switch -c <task-branch>
|
||||||
|
```
|
||||||
|
|
||||||
|
## Delivery
|
||||||
|
|
||||||
|
- Keep changes focused and preserve backward compatibility where practical.
|
||||||
|
- Update the matching repository documentation when behavior, contracts, configuration, routing, or workflows change.
|
||||||
|
- Run the solution build and relevant tests before handoff.
|
||||||
|
- Use Conventional Commit messages with a lowercase subject and finish the commit body with `Ref: <issue-id>` when an issue exists.
|
||||||
@@ -30,13 +30,15 @@
|
|||||||
<PackageVersion Include="Serilog.Sinks.Seq" Version="9.0.0" />
|
<PackageVersion Include="Serilog.Sinks.Seq" Version="9.0.0" />
|
||||||
<!-- HrynCo shared packages -->
|
<!-- HrynCo shared packages -->
|
||||||
<PackageVersion Include="HrynCo.Common" Version="1.0.11" />
|
<PackageVersion Include="HrynCo.Common" Version="1.0.11" />
|
||||||
<PackageVersion Include="HrynCo.RabbitMq" Version="1.0.15" />
|
<PackageVersion Include="HrynCo.RabbitMq" Version="1.0.17" />
|
||||||
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="17.14.1" />
|
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="17.14.1" />
|
||||||
<PackageVersion Include="coverlet.collector" Version="6.0.4" />
|
<PackageVersion Include="coverlet.collector" Version="6.0.4" />
|
||||||
<PackageVersion Include="xunit" Version="2.9.3" />
|
<PackageVersion Include="xunit" Version="2.9.3" />
|
||||||
<PackageVersion Include="xunit.runner.visualstudio" Version="3.1.4" />
|
<PackageVersion Include="xunit.runner.visualstudio" Version="3.1.4" />
|
||||||
<PackageVersion Include="NSubstitute" Version="5.3.0" />
|
<PackageVersion Include="NSubstitute" Version="5.3.0" />
|
||||||
|
<!-- Patched test-only dependency pulled by Testcontainers. -->
|
||||||
|
<PackageVersion Include="SSH.NET" Version="2026.0.0" />
|
||||||
<PackageVersion Include="Testcontainers.PostgreSql" Version="4.6.0" />
|
<PackageVersion Include="Testcontainers.PostgreSql" Version="4.6.0" />
|
||||||
<PackageVersion Include="Microsoft.AspNetCore.Mvc.Testing" Version="9.0.5" />
|
<PackageVersion Include="Microsoft.AspNetCore.Mvc.Testing" Version="10.0.6" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
</Project>
|
</Project>
|
||||||
@@ -9,7 +9,7 @@
|
|||||||
<Description>RabbitMQ message contracts for HrynCo.NotificationService.</Description>
|
<Description>RabbitMQ message contracts for HrynCo.NotificationService.</Description>
|
||||||
<PackageTags>hrynco notification email rabbitmq contracts</PackageTags>
|
<PackageTags>hrynco notification email rabbitmq contracts</PackageTags>
|
||||||
<RepositoryType>git</RepositoryType>
|
<RepositoryType>git</RepositoryType>
|
||||||
<RepositoryUrl>https://gitea.grynco.com.ua/hrynco/hrynco-notification-service.git</RepositoryUrl>
|
<RepositoryUrl>https://gitea.hrynco.com/hrynco/hrynco-notification-service.git</RepositoryUrl>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
|||||||
+14
@@ -0,0 +1,14 @@
|
|||||||
|
using HrynCo.NotificationService.DAL.Abstract.Templates;
|
||||||
|
|
||||||
|
namespace HrynCo.NotificationService.DAL.Abstract.Repositories;
|
||||||
|
|
||||||
|
public interface IEmailTemplateDefinitionRepository
|
||||||
|
{
|
||||||
|
Task<IReadOnlyList<EmailTemplateDefinition>> GetDefinitionsAsync(string? serviceName = null, string? key = null, CancellationToken ct = default);
|
||||||
|
Task<EmailTemplateDefinition?> GetDefinitionAsync(string serviceName, string key, CancellationToken ct = default);
|
||||||
|
Task<EmailTemplateDefinition?> GetDefinitionAsync(Guid serviceId, string key, CancellationToken ct = default);
|
||||||
|
Task<EmailTemplateDefinition?> UpsertDefinitionAsync(EmailTemplateDefinitionPatch patch, CancellationToken ct = default);
|
||||||
|
Task<IReadOnlyList<EmailTemplateDefinition>> GetDefinitionsAsync(Guid serviceId, CancellationToken ct = default);
|
||||||
|
Task<bool> DeleteDefinitionAsync(Guid serviceId, string key, Guid revision, CancellationToken ct = default);
|
||||||
|
Task SaveDefinitionAsync(EmailTemplateDefinition definition, bool isNew, CancellationToken ct = default);
|
||||||
|
}
|
||||||
@@ -4,7 +4,7 @@ namespace HrynCo.NotificationService.DAL.Abstract.Repositories;
|
|||||||
|
|
||||||
public interface IEmailTemplateRepository
|
public interface IEmailTemplateRepository
|
||||||
{
|
{
|
||||||
Task<IReadOnlyList<EmailTemplate>> GetAllAsync(CancellationToken ct = default);
|
Task<IReadOnlyList<EmailTemplate>> GetAllAsync(string? serviceName = null, string? key = null, CancellationToken ct = default);
|
||||||
Task<IReadOnlyList<EmailTemplate>> GetByServiceAsync(string serviceName, CancellationToken ct = default);
|
Task<IReadOnlyList<EmailTemplate>> GetByServiceAsync(string serviceName, CancellationToken ct = default);
|
||||||
Task<EmailTemplate?> GetAsync(string serviceName, string key, string languageCode, CancellationToken ct = default);
|
Task<EmailTemplate?> GetAsync(string serviceName, string key, string languageCode, CancellationToken ct = default);
|
||||||
Task AddAsync(EmailTemplate template, CancellationToken ct = default);
|
Task AddAsync(EmailTemplate template, CancellationToken ct = default);
|
||||||
|
|||||||
+13
@@ -0,0 +1,13 @@
|
|||||||
|
using HrynCo.NotificationService.DAL.Abstract.Templates;
|
||||||
|
|
||||||
|
namespace HrynCo.NotificationService.DAL.Abstract.Repositories;
|
||||||
|
|
||||||
|
public interface INotificationServiceRepository
|
||||||
|
{
|
||||||
|
Task<IReadOnlyList<NotificationServiceInfo>> GetAllAsync(CancellationToken ct = default);
|
||||||
|
Task<NotificationServiceInfo?> GetAsync(Guid id, CancellationToken ct = default);
|
||||||
|
Task<NotificationServiceInfo?> GetByNameAsync(string name, CancellationToken ct = default);
|
||||||
|
Task<bool> DeleteAsync(Guid id, Guid revision, CancellationToken ct = default);
|
||||||
|
Task SaveAsync(NotificationServiceInfo service, bool isNew, CancellationToken ct = default);
|
||||||
|
Task RemoveLanguageAsync(Guid serviceId, Guid languageId, int expectedVersionCount, Guid expectedRevision, CancellationToken ct = default);
|
||||||
|
}
|
||||||
@@ -0,0 +1,13 @@
|
|||||||
|
using HrynCo.DAL.Abstract.Entities;
|
||||||
|
|
||||||
|
namespace HrynCo.NotificationService.DAL.Abstract.Templates;
|
||||||
|
|
||||||
|
public sealed class EmailTemplateDefinition : Entity
|
||||||
|
{
|
||||||
|
public Guid ServiceId { get; set; }
|
||||||
|
public required string ServiceName { get; set; }
|
||||||
|
public required string Key { get; set; }
|
||||||
|
public Guid Revision { get; set; }
|
||||||
|
public IReadOnlyList<EmailTemplateVariable> Variables { get; set; } = [];
|
||||||
|
public IReadOnlyList<EmailTemplate> Languages { get; set; } = [];
|
||||||
|
}
|
||||||
@@ -0,0 +1,11 @@
|
|||||||
|
namespace HrynCo.NotificationService.DAL.Abstract.Templates;
|
||||||
|
|
||||||
|
public sealed class EmailTemplateDefinitionPatch
|
||||||
|
{
|
||||||
|
public Guid ServiceId { get; init; }
|
||||||
|
public required string Key { get; init; }
|
||||||
|
public Guid? Revision { get; init; }
|
||||||
|
public required IReadOnlyList<EmailTemplateVariable> Variables { get; init; }
|
||||||
|
public required IReadOnlyList<EmailTemplate> Translations { get; init; }
|
||||||
|
public IReadOnlyList<string> RemoveLanguageCodes { get; init; } = [];
|
||||||
|
}
|
||||||
@@ -0,0 +1,76 @@
|
|||||||
|
using System.Text.Json;
|
||||||
|
|
||||||
|
namespace HrynCo.NotificationService.DAL.Abstract.Templates;
|
||||||
|
|
||||||
|
public static class EmailTemplateValidation
|
||||||
|
{
|
||||||
|
private static readonly JsonSerializerOptions JsonOptions = new(JsonSerializerDefaults.Web)
|
||||||
|
{
|
||||||
|
PropertyNameCaseInsensitive = false,
|
||||||
|
UnmappedMemberHandling = System.Text.Json.Serialization.JsonUnmappedMemberHandling.Disallow
|
||||||
|
};
|
||||||
|
|
||||||
|
public static IReadOnlyList<EmailTemplateVariable> ParseVariables(string json)
|
||||||
|
{
|
||||||
|
using var document = JsonDocument.Parse(json);
|
||||||
|
if (document.RootElement.ValueKind == JsonValueKind.Null)
|
||||||
|
throw new EmailTemplateWriteException("Variables must be a JSON array.", false);
|
||||||
|
if (document.RootElement.ValueKind != JsonValueKind.Array)
|
||||||
|
throw new JsonException("Variables must be an array.");
|
||||||
|
foreach (var item in document.RootElement.EnumerateArray())
|
||||||
|
ValidateVariableShape(item);
|
||||||
|
var variables = JsonSerializer.Deserialize<List<EmailTemplateVariable>>(json, JsonOptions);
|
||||||
|
ValidateVariables(variables);
|
||||||
|
return variables!;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void ValidateVariableShape(JsonElement item)
|
||||||
|
{
|
||||||
|
if (item.ValueKind != JsonValueKind.Object)
|
||||||
|
throw new JsonException("Each variable must be an object.");
|
||||||
|
if (!item.TryGetProperty("name", out var name) || name.ValueKind != JsonValueKind.String)
|
||||||
|
throw new JsonException("Every variable needs a name string.");
|
||||||
|
if (!item.TryGetProperty("required", out var required) || required.ValueKind is not (JsonValueKind.True or JsonValueKind.False))
|
||||||
|
throw new JsonException("Every variable needs a required boolean.");
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void ValidateVariables(IReadOnlyList<EmailTemplateVariable>? variables)
|
||||||
|
{
|
||||||
|
if (variables is null)
|
||||||
|
throw new EmailTemplateWriteException("Variables must be a JSON array.", false);
|
||||||
|
if (variables.Any(v => v is null || string.IsNullOrWhiteSpace(v.Name)))
|
||||||
|
throw new EmailTemplateWriteException("Every variable needs a non-empty name.", false);
|
||||||
|
if (variables.Select(v => v.Name).Distinct(StringComparer.Ordinal).Count() != variables.Count)
|
||||||
|
throw new EmailTemplateWriteException("Variable names must be unique.", false);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static bool ContractsEqual(IReadOnlyList<EmailTemplateVariable> left, IReadOnlyList<EmailTemplateVariable> right) =>
|
||||||
|
left.OrderBy(v => v.Name, StringComparer.Ordinal).SequenceEqual(right.OrderBy(v => v.Name, StringComparer.Ordinal));
|
||||||
|
|
||||||
|
public static void ValidateIdentity(string serviceName, string key)
|
||||||
|
{
|
||||||
|
if (string.IsNullOrWhiteSpace(serviceName) || serviceName.Length > 100)
|
||||||
|
throw new EmailTemplateWriteException("Service name is required and must be at most 100 characters.", false);
|
||||||
|
if (string.IsNullOrWhiteSpace(key) || key.Length > 100)
|
||||||
|
throw new EmailTemplateWriteException("Key is required and must be at most 100 characters.", false);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void ValidateLanguage(EmailTemplate template)
|
||||||
|
{
|
||||||
|
if (string.IsNullOrWhiteSpace(template.LanguageCode) || template.LanguageCode.Length > 10)
|
||||||
|
throw new EmailTemplateWriteException("Language code is required and must be at most 10 characters.", false);
|
||||||
|
if (string.IsNullOrWhiteSpace(template.Subject) || string.IsNullOrWhiteSpace(template.HtmlBody) || string.IsNullOrWhiteSpace(template.TextBody))
|
||||||
|
throw new EmailTemplateWriteException("Every language needs a subject, HTML body, and text body.", false);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void ValidateDefinition(EmailTemplateDefinition definition)
|
||||||
|
{
|
||||||
|
ValidateIdentity(definition.ServiceName, definition.Key);
|
||||||
|
ValidateVariables(definition.Variables);
|
||||||
|
foreach (var language in definition.Languages)
|
||||||
|
ValidateLanguage(language);
|
||||||
|
var codes = definition.Languages.Select(t => t.LanguageCode);
|
||||||
|
if (codes.Distinct(StringComparer.Ordinal).Count() != definition.Languages.Count)
|
||||||
|
throw new EmailTemplateWriteException("Language codes must be unique within a template.", false);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
namespace HrynCo.NotificationService.DAL.Abstract.Templates;
|
||||||
|
|
||||||
|
public sealed class EmailTemplateWriteException(string message, bool isConflict = true) : Exception(message)
|
||||||
|
{
|
||||||
|
public bool IsConflict { get; } = isConflict;
|
||||||
|
}
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
namespace HrynCo.NotificationService.DAL.Abstract.Templates;
|
||||||
|
|
||||||
|
public sealed class NotificationServiceInfo
|
||||||
|
{
|
||||||
|
public Guid Id { get; set; }
|
||||||
|
public required string Name { get; set; }
|
||||||
|
public Guid Revision { get; set; }
|
||||||
|
public IReadOnlyList<ServiceLanguageInfo> Languages { get; set; } = [];
|
||||||
|
}
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
namespace HrynCo.NotificationService.DAL.Abstract.Templates;
|
||||||
|
|
||||||
|
public sealed class ServiceLanguageInfo
|
||||||
|
{
|
||||||
|
public Guid Id { get; set; }
|
||||||
|
public required string Code { get; set; }
|
||||||
|
public int VersionCount { get; set; }
|
||||||
|
}
|
||||||
+29
@@ -0,0 +1,29 @@
|
|||||||
|
using HrynCo.NotificationService.DAL.EF.Entities;
|
||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
using Microsoft.EntityFrameworkCore.Metadata.Builders;
|
||||||
|
|
||||||
|
namespace HrynCo.NotificationService.DAL.EF.Configurations;
|
||||||
|
|
||||||
|
internal sealed class EmailTemplateDefinitionEntityConfiguration : IEntityTypeConfiguration<EmailTemplateDefinitionEntity>
|
||||||
|
{
|
||||||
|
public void Configure(EntityTypeBuilder<EmailTemplateDefinitionEntity> builder)
|
||||||
|
{
|
||||||
|
builder.ToTable("email_template_definitions");
|
||||||
|
builder.HasKey(x => x.Id);
|
||||||
|
builder.Property(x => x.Id).HasColumnName("id");
|
||||||
|
builder.Property(x => x.ServiceId).HasColumnName("service_id");
|
||||||
|
builder.Property(x => x.Key).HasColumnName("key").HasMaxLength(100).IsRequired();
|
||||||
|
builder.Property(x => x.Revision).HasColumnName("revision").IsConcurrencyToken();
|
||||||
|
builder.HasIndex(x => new { x.ServiceId, x.Key }).IsUnique();
|
||||||
|
builder.HasAlternateKey(x => new { x.Id, x.ServiceId });
|
||||||
|
builder.HasOne(x => x.Service).WithMany().HasForeignKey(x => x.ServiceId).OnDelete(DeleteBehavior.Restrict);
|
||||||
|
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");
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
+14
-40
@@ -4,53 +4,27 @@ using Microsoft.EntityFrameworkCore.Metadata.Builders;
|
|||||||
|
|
||||||
namespace HrynCo.NotificationService.DAL.EF.Configurations;
|
namespace HrynCo.NotificationService.DAL.EF.Configurations;
|
||||||
|
|
||||||
internal class EmailTemplateEntityConfiguration : IEntityTypeConfiguration<EmailTemplateEntity>
|
internal sealed class EmailTemplateEntityConfiguration : IEntityTypeConfiguration<EmailTemplateEntity>
|
||||||
{
|
{
|
||||||
public void Configure(EntityTypeBuilder<EmailTemplateEntity> builder)
|
public void Configure(EntityTypeBuilder<EmailTemplateEntity> builder)
|
||||||
{
|
{
|
||||||
builder.ToTable("email_templates");
|
builder.ToTable("email_templates");
|
||||||
|
|
||||||
builder.HasKey(x => x.Id);
|
builder.HasKey(x => x.Id);
|
||||||
builder.Property(x => x.Id).HasColumnName("id");
|
builder.Property(x => x.Id).HasColumnName("id");
|
||||||
|
builder.Property(x => x.DefinitionId).HasColumnName("definition_id");
|
||||||
builder.Property(x => x.ServiceName)
|
builder.Property(x => x.ServiceId).HasColumnName("service_id");
|
||||||
.HasColumnName("service_name")
|
builder.Property(x => x.ServiceLanguageId).HasColumnName("service_language_id");
|
||||||
.IsRequired()
|
builder.HasIndex(x => new { x.DefinitionId, x.ServiceLanguageId }).IsUnique();
|
||||||
.HasMaxLength(100);
|
builder.HasOne(x => x.Definition).WithMany(x => x.Languages)
|
||||||
|
.HasForeignKey(x => new { x.DefinitionId, x.ServiceId })
|
||||||
builder.Property(x => x.Key)
|
.HasPrincipalKey(x => new { x.Id, x.ServiceId }).OnDelete(DeleteBehavior.Cascade);
|
||||||
.HasColumnName("key")
|
builder.HasOne(x => x.ServiceLanguage).WithMany()
|
||||||
.IsRequired()
|
.HasForeignKey(x => new { x.ServiceLanguageId, x.ServiceId })
|
||||||
.HasMaxLength(100);
|
.HasPrincipalKey(x => new { x.Id, x.ServiceId }).OnDelete(DeleteBehavior.Restrict);
|
||||||
|
builder.Property(x => x.Subject).HasColumnName("subject").IsRequired();
|
||||||
builder.Property(x => x.LanguageCode)
|
builder.Property(x => x.HtmlBody).HasColumnName("html_body").IsRequired();
|
||||||
.HasColumnName("language_code")
|
builder.Property(x => x.TextBody).HasColumnName("text_body").IsRequired();
|
||||||
.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.Created).HasColumnName("created");
|
||||||
builder.Property(x => x.Updated).HasColumnName("updated");
|
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,20 @@
|
|||||||
|
using HrynCo.NotificationService.DAL.EF.Entities;
|
||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
using Microsoft.EntityFrameworkCore.Metadata.Builders;
|
||||||
|
|
||||||
|
namespace HrynCo.NotificationService.DAL.EF.Configurations;
|
||||||
|
|
||||||
|
internal sealed class ServiceEntityConfiguration : IEntityTypeConfiguration<ServiceEntity>
|
||||||
|
{
|
||||||
|
public void Configure(EntityTypeBuilder<ServiceEntity> builder)
|
||||||
|
{
|
||||||
|
builder.ToTable("services");
|
||||||
|
builder.HasKey(x => x.Id);
|
||||||
|
builder.Property(x => x.Id).HasColumnName("id");
|
||||||
|
builder.Property(x => x.Name).HasColumnName("name").HasMaxLength(100).IsRequired();
|
||||||
|
builder.HasIndex(x => x.Name).IsUnique();
|
||||||
|
builder.Property(x => x.Revision).HasColumnName("revision").IsConcurrencyToken();
|
||||||
|
builder.Property(x => x.Created).HasColumnName("created");
|
||||||
|
builder.Property(x => x.Updated).HasColumnName("updated");
|
||||||
|
}
|
||||||
|
}
|
||||||
+22
@@ -0,0 +1,22 @@
|
|||||||
|
using HrynCo.NotificationService.DAL.EF.Entities;
|
||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
using Microsoft.EntityFrameworkCore.Metadata.Builders;
|
||||||
|
|
||||||
|
namespace HrynCo.NotificationService.DAL.EF.Configurations;
|
||||||
|
|
||||||
|
internal sealed class ServiceLanguageEntityConfiguration : IEntityTypeConfiguration<ServiceLanguageEntity>
|
||||||
|
{
|
||||||
|
public void Configure(EntityTypeBuilder<ServiceLanguageEntity> builder)
|
||||||
|
{
|
||||||
|
builder.ToTable("service_languages");
|
||||||
|
builder.HasKey(x => x.Id);
|
||||||
|
builder.Property(x => x.Id).HasColumnName("id");
|
||||||
|
builder.Property(x => x.ServiceId).HasColumnName("service_id");
|
||||||
|
builder.Property(x => x.Code).HasColumnName("code").HasMaxLength(10).IsRequired();
|
||||||
|
builder.HasIndex(x => new { x.ServiceId, x.Code }).IsUnique();
|
||||||
|
builder.HasAlternateKey(x => new { x.Id, x.ServiceId });
|
||||||
|
builder.HasOne(x => x.Service).WithMany(x => x.Languages).HasForeignKey(x => x.ServiceId).OnDelete(DeleteBehavior.Restrict);
|
||||||
|
builder.Property(x => x.Created).HasColumnName("created");
|
||||||
|
builder.Property(x => x.Updated).HasColumnName("updated");
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,13 @@
|
|||||||
|
using HrynCo.DAL.Abstract.Entities;
|
||||||
|
|
||||||
|
namespace HrynCo.NotificationService.DAL.EF.Entities;
|
||||||
|
|
||||||
|
internal sealed class EmailTemplateDefinitionEntity : Entity
|
||||||
|
{
|
||||||
|
public Guid ServiceId { get; set; }
|
||||||
|
public ServiceEntity Service { get; set; } = null!;
|
||||||
|
public required string Key { get; set; }
|
||||||
|
public Guid Revision { get; set; } = Guid.NewGuid();
|
||||||
|
public List<EmailTemplateVariableData> Variables { get; set; } = [];
|
||||||
|
public List<EmailTemplateEntity> Languages { get; set; } = [];
|
||||||
|
}
|
||||||
@@ -4,17 +4,12 @@ namespace HrynCo.NotificationService.DAL.EF.Entities;
|
|||||||
|
|
||||||
internal class EmailTemplateEntity : Entity
|
internal class EmailTemplateEntity : Entity
|
||||||
{
|
{
|
||||||
public required string ServiceName { get; set; }
|
public Guid DefinitionId { get; set; }
|
||||||
public required string Key { get; set; }
|
public EmailTemplateDefinitionEntity Definition { get; set; } = null!;
|
||||||
public required string LanguageCode { get; set; }
|
public Guid ServiceId { get; set; }
|
||||||
|
public Guid ServiceLanguageId { get; set; }
|
||||||
|
public ServiceLanguageEntity ServiceLanguage { get; set; } = null!;
|
||||||
public required string Subject { get; set; }
|
public required string Subject { get; set; }
|
||||||
public required string HtmlBody { get; set; }
|
public required string HtmlBody { get; set; }
|
||||||
public required string TextBody { 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,7 @@
|
|||||||
|
namespace HrynCo.NotificationService.DAL.EF.Entities;
|
||||||
|
|
||||||
|
internal sealed class EmailTemplateVariableData
|
||||||
|
{
|
||||||
|
public required string Name { get; set; }
|
||||||
|
public bool Required { get; set; }
|
||||||
|
}
|
||||||
@@ -0,0 +1,10 @@
|
|||||||
|
using HrynCo.DAL.Abstract.Entities;
|
||||||
|
|
||||||
|
namespace HrynCo.NotificationService.DAL.EF.Entities;
|
||||||
|
|
||||||
|
internal sealed class ServiceEntity : Entity
|
||||||
|
{
|
||||||
|
public required string Name { get; set; }
|
||||||
|
public Guid Revision { get; set; } = Guid.NewGuid();
|
||||||
|
public List<ServiceLanguageEntity> Languages { get; set; } = [];
|
||||||
|
}
|
||||||
@@ -0,0 +1,10 @@
|
|||||||
|
using HrynCo.DAL.Abstract.Entities;
|
||||||
|
|
||||||
|
namespace HrynCo.NotificationService.DAL.EF.Entities;
|
||||||
|
|
||||||
|
internal sealed class ServiceLanguageEntity : Entity
|
||||||
|
{
|
||||||
|
public Guid ServiceId { get; set; }
|
||||||
|
public ServiceEntity Service { get; set; } = null!;
|
||||||
|
public required string Code { get; set; }
|
||||||
|
}
|
||||||
Generated
+376
@@ -0,0 +1,376 @@
|
|||||||
|
// <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("20261017072410_EmailTemplateLocalizations")]
|
||||||
|
partial class EmailTemplateLocalizations
|
||||||
|
{
|
||||||
|
/// <inheritdoc />
|
||||||
|
protected override void BuildTargetModel(ModelBuilder modelBuilder)
|
||||||
|
{
|
||||||
|
#pragma warning disable 612, 618
|
||||||
|
modelBuilder
|
||||||
|
.HasAnnotation("ProductVersion", "10.0.7")
|
||||||
|
.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.EmailTemplateDefinitionEntity", b =>
|
||||||
|
{
|
||||||
|
b.Property<Guid>("Id")
|
||||||
|
.ValueGeneratedOnAdd()
|
||||||
|
.HasColumnType("uuid")
|
||||||
|
.HasColumnName("id");
|
||||||
|
|
||||||
|
b.Property<DateTimeOffset>("Created")
|
||||||
|
.HasColumnType("timestamp with time zone")
|
||||||
|
.HasColumnName("created");
|
||||||
|
|
||||||
|
b.Property<string>("Key")
|
||||||
|
.IsRequired()
|
||||||
|
.HasMaxLength(100)
|
||||||
|
.HasColumnType("character varying(100)")
|
||||||
|
.HasColumnName("key");
|
||||||
|
|
||||||
|
b.Property<Guid>("Revision")
|
||||||
|
.IsConcurrencyToken()
|
||||||
|
.HasColumnType("uuid")
|
||||||
|
.HasColumnName("revision");
|
||||||
|
|
||||||
|
b.Property<Guid>("ServiceId")
|
||||||
|
.HasColumnType("uuid")
|
||||||
|
.HasColumnName("service_id");
|
||||||
|
|
||||||
|
b.Property<DateTimeOffset?>("Updated")
|
||||||
|
.HasColumnType("timestamp with time zone")
|
||||||
|
.HasColumnName("updated");
|
||||||
|
|
||||||
|
b.HasKey("Id");
|
||||||
|
|
||||||
|
b.HasIndex("ServiceId", "Key")
|
||||||
|
.IsUnique();
|
||||||
|
|
||||||
|
b.ToTable("email_template_definitions", (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<Guid>("DefinitionId")
|
||||||
|
.HasColumnType("uuid")
|
||||||
|
.HasColumnName("definition_id");
|
||||||
|
|
||||||
|
b.Property<string>("HtmlBody")
|
||||||
|
.IsRequired()
|
||||||
|
.HasColumnType("text")
|
||||||
|
.HasColumnName("html_body");
|
||||||
|
|
||||||
|
b.Property<Guid>("ServiceId")
|
||||||
|
.HasColumnType("uuid")
|
||||||
|
.HasColumnName("service_id");
|
||||||
|
|
||||||
|
b.Property<Guid>("ServiceLanguageId")
|
||||||
|
.HasColumnType("uuid")
|
||||||
|
.HasColumnName("service_language_id");
|
||||||
|
|
||||||
|
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("DefinitionId", "ServiceId");
|
||||||
|
|
||||||
|
b.HasIndex("DefinitionId", "ServiceLanguageId")
|
||||||
|
.IsUnique();
|
||||||
|
|
||||||
|
b.HasIndex("ServiceLanguageId", "ServiceId");
|
||||||
|
|
||||||
|
b.ToTable("email_templates", (string)null);
|
||||||
|
});
|
||||||
|
|
||||||
|
modelBuilder.Entity("HrynCo.NotificationService.DAL.EF.Entities.ServiceEntity", b =>
|
||||||
|
{
|
||||||
|
b.Property<Guid>("Id")
|
||||||
|
.ValueGeneratedOnAdd()
|
||||||
|
.HasColumnType("uuid")
|
||||||
|
.HasColumnName("id");
|
||||||
|
|
||||||
|
b.Property<DateTimeOffset>("Created")
|
||||||
|
.HasColumnType("timestamp with time zone")
|
||||||
|
.HasColumnName("created");
|
||||||
|
|
||||||
|
b.Property<string>("Name")
|
||||||
|
.IsRequired()
|
||||||
|
.HasMaxLength(100)
|
||||||
|
.HasColumnType("character varying(100)")
|
||||||
|
.HasColumnName("name");
|
||||||
|
|
||||||
|
b.Property<Guid>("Revision")
|
||||||
|
.IsConcurrencyToken()
|
||||||
|
.HasColumnType("uuid")
|
||||||
|
.HasColumnName("revision");
|
||||||
|
|
||||||
|
b.Property<DateTimeOffset?>("Updated")
|
||||||
|
.HasColumnType("timestamp with time zone")
|
||||||
|
.HasColumnName("updated");
|
||||||
|
|
||||||
|
b.HasKey("Id");
|
||||||
|
|
||||||
|
b.HasIndex("Name")
|
||||||
|
.IsUnique();
|
||||||
|
|
||||||
|
b.ToTable("services", (string)null);
|
||||||
|
});
|
||||||
|
|
||||||
|
modelBuilder.Entity("HrynCo.NotificationService.DAL.EF.Entities.ServiceLanguageEntity", b =>
|
||||||
|
{
|
||||||
|
b.Property<Guid>("Id")
|
||||||
|
.ValueGeneratedOnAdd()
|
||||||
|
.HasColumnType("uuid")
|
||||||
|
.HasColumnName("id");
|
||||||
|
|
||||||
|
b.Property<string>("Code")
|
||||||
|
.IsRequired()
|
||||||
|
.HasMaxLength(10)
|
||||||
|
.HasColumnType("character varying(10)")
|
||||||
|
.HasColumnName("code");
|
||||||
|
|
||||||
|
b.Property<DateTimeOffset>("Created")
|
||||||
|
.HasColumnType("timestamp with time zone")
|
||||||
|
.HasColumnName("created");
|
||||||
|
|
||||||
|
b.Property<Guid>("ServiceId")
|
||||||
|
.HasColumnType("uuid")
|
||||||
|
.HasColumnName("service_id");
|
||||||
|
|
||||||
|
b.Property<DateTimeOffset?>("Updated")
|
||||||
|
.HasColumnType("timestamp with time zone")
|
||||||
|
.HasColumnName("updated");
|
||||||
|
|
||||||
|
b.HasKey("Id");
|
||||||
|
|
||||||
|
b.HasIndex("ServiceId", "Code")
|
||||||
|
.IsUnique();
|
||||||
|
|
||||||
|
b.ToTable("service_languages", (string)null);
|
||||||
|
});
|
||||||
|
|
||||||
|
modelBuilder.Entity("HrynCo.NotificationService.DAL.EF.Entities.EmailChannelUsageEntity", b =>
|
||||||
|
{
|
||||||
|
b.HasOne("HrynCo.NotificationService.DAL.EF.Entities.EmailChannelEntity", null)
|
||||||
|
.WithMany("UsageRecords")
|
||||||
|
.HasForeignKey("ProviderId")
|
||||||
|
.OnDelete(DeleteBehavior.Cascade)
|
||||||
|
.IsRequired();
|
||||||
|
});
|
||||||
|
|
||||||
|
modelBuilder.Entity("HrynCo.NotificationService.DAL.EF.Entities.EmailTemplateDefinitionEntity", b =>
|
||||||
|
{
|
||||||
|
b.HasOne("HrynCo.NotificationService.DAL.EF.Entities.ServiceEntity", "Service")
|
||||||
|
.WithMany()
|
||||||
|
.HasForeignKey("ServiceId")
|
||||||
|
.OnDelete(DeleteBehavior.Restrict)
|
||||||
|
.IsRequired();
|
||||||
|
|
||||||
|
b.OwnsMany("HrynCo.NotificationService.DAL.EF.Entities.EmailTemplateVariableData", "Variables", b1 =>
|
||||||
|
{
|
||||||
|
b1.Property<Guid>("EmailTemplateDefinitionEntityId");
|
||||||
|
|
||||||
|
b1.Property<int>("__synthesizedOrdinal")
|
||||||
|
.ValueGeneratedOnAdd();
|
||||||
|
|
||||||
|
b1.Property<string>("Name")
|
||||||
|
.IsRequired()
|
||||||
|
.HasJsonPropertyName("name");
|
||||||
|
|
||||||
|
b1.Property<bool>("Required")
|
||||||
|
.HasJsonPropertyName("required");
|
||||||
|
|
||||||
|
b1.HasKey("EmailTemplateDefinitionEntityId", "__synthesizedOrdinal");
|
||||||
|
|
||||||
|
b1.ToTable("email_template_definitions");
|
||||||
|
|
||||||
|
b1
|
||||||
|
.ToJson("variables")
|
||||||
|
.HasColumnType("jsonb");
|
||||||
|
|
||||||
|
b1.WithOwner()
|
||||||
|
.HasForeignKey("EmailTemplateDefinitionEntityId");
|
||||||
|
});
|
||||||
|
|
||||||
|
b.Navigation("Service");
|
||||||
|
|
||||||
|
b.Navigation("Variables");
|
||||||
|
});
|
||||||
|
|
||||||
|
modelBuilder.Entity("HrynCo.NotificationService.DAL.EF.Entities.EmailTemplateEntity", b =>
|
||||||
|
{
|
||||||
|
b.HasOne("HrynCo.NotificationService.DAL.EF.Entities.EmailTemplateDefinitionEntity", "Definition")
|
||||||
|
.WithMany("Languages")
|
||||||
|
.HasForeignKey("DefinitionId", "ServiceId")
|
||||||
|
.HasPrincipalKey("Id", "ServiceId")
|
||||||
|
.OnDelete(DeleteBehavior.Cascade)
|
||||||
|
.IsRequired();
|
||||||
|
|
||||||
|
b.HasOne("HrynCo.NotificationService.DAL.EF.Entities.ServiceLanguageEntity", "ServiceLanguage")
|
||||||
|
.WithMany()
|
||||||
|
.HasForeignKey("ServiceLanguageId", "ServiceId")
|
||||||
|
.HasPrincipalKey("Id", "ServiceId")
|
||||||
|
.OnDelete(DeleteBehavior.Restrict)
|
||||||
|
.IsRequired();
|
||||||
|
|
||||||
|
b.Navigation("Definition");
|
||||||
|
|
||||||
|
b.Navigation("ServiceLanguage");
|
||||||
|
});
|
||||||
|
|
||||||
|
modelBuilder.Entity("HrynCo.NotificationService.DAL.EF.Entities.ServiceLanguageEntity", b =>
|
||||||
|
{
|
||||||
|
b.HasOne("HrynCo.NotificationService.DAL.EF.Entities.ServiceEntity", "Service")
|
||||||
|
.WithMany("Languages")
|
||||||
|
.HasForeignKey("ServiceId")
|
||||||
|
.OnDelete(DeleteBehavior.Restrict)
|
||||||
|
.IsRequired();
|
||||||
|
|
||||||
|
b.Navigation("Service");
|
||||||
|
});
|
||||||
|
|
||||||
|
modelBuilder.Entity("HrynCo.NotificationService.DAL.EF.Entities.EmailChannelEntity", b =>
|
||||||
|
{
|
||||||
|
b.Navigation("UsageRecords");
|
||||||
|
});
|
||||||
|
|
||||||
|
modelBuilder.Entity("HrynCo.NotificationService.DAL.EF.Entities.EmailTemplateDefinitionEntity", b =>
|
||||||
|
{
|
||||||
|
b.Navigation("Languages");
|
||||||
|
});
|
||||||
|
|
||||||
|
modelBuilder.Entity("HrynCo.NotificationService.DAL.EF.Entities.ServiceEntity", b =>
|
||||||
|
{
|
||||||
|
b.Navigation("Languages");
|
||||||
|
});
|
||||||
|
#pragma warning restore 612, 618
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
+284
@@ -0,0 +1,284 @@
|
|||||||
|
using Microsoft.EntityFrameworkCore.Migrations;
|
||||||
|
|
||||||
|
#nullable disable
|
||||||
|
|
||||||
|
namespace HrynCo.NotificationService.DAL.EF.Migrations;
|
||||||
|
|
||||||
|
public partial class EmailTemplateLocalizations : Migration
|
||||||
|
{
|
||||||
|
protected override void Up(MigrationBuilder migrationBuilder)
|
||||||
|
{
|
||||||
|
migrationBuilder.Sql(SourceLayoutGuard());
|
||||||
|
migrationBuilder.Sql("DO $upgrade$ BEGIN IF (SELECT layout FROM it1601_migration_origin) = 'flat' THEN\n"
|
||||||
|
+ FlatUpgrade() + "\nELSE\n" + InterimUpgrade() + "\nEND IF; END $upgrade$;");
|
||||||
|
}
|
||||||
|
|
||||||
|
protected override void Down(MigrationBuilder migrationBuilder)
|
||||||
|
{
|
||||||
|
migrationBuilder.Sql("""
|
||||||
|
LOCK TABLE email_templates IN ACCESS EXCLUSIVE MODE;
|
||||||
|
DO $$ BEGIN
|
||||||
|
IF EXISTS (SELECT 1 FROM "__EFMigrationsHistory" WHERE "MigrationId" = '20261017070315_EmailTemplateLocalizations') THEN
|
||||||
|
RAISE EXCEPTION 'Cannot downgrade an interim-origin database to the flat legacy model. Restore a verified backup instead.';
|
||||||
|
END IF;
|
||||||
|
IF EXISTS (SELECT 1 FROM email_template_definitions d WHERE NOT EXISTS (SELECT 1 FROM email_templates t WHERE t.definition_id = d.id))
|
||||||
|
OR EXISTS (SELECT 1 FROM service_languages l WHERE NOT EXISTS (SELECT 1 FROM email_templates t WHERE t.service_language_id = l.id))
|
||||||
|
OR EXISTS (SELECT 1 FROM services s WHERE NOT EXISTS (SELECT 1 FROM email_templates t WHERE t.service_id = s.id)) THEN
|
||||||
|
RAISE EXCEPTION 'Cannot downgrade catalog-only data or empty definitions to the legacy model. Restore a verified backup instead.';
|
||||||
|
END IF;
|
||||||
|
END $$;
|
||||||
|
ALTER TABLE email_templates ADD COLUMN service_name varchar(100), ADD COLUMN key varchar(100),
|
||||||
|
ADD COLUMN language_code varchar(10), ADD COLUMN variables jsonb;
|
||||||
|
UPDATE email_templates t SET service_name = s.name, key = d.key, variables = d.variables, language_code = l.code
|
||||||
|
FROM email_template_definitions d JOIN services s ON s.id = d.service_id
|
||||||
|
JOIN service_languages l ON l.service_id = s.id
|
||||||
|
WHERE d.id = t.definition_id AND l.id = t.service_language_id;
|
||||||
|
ALTER TABLE email_templates ALTER COLUMN service_name SET NOT NULL, ALTER COLUMN key SET NOT NULL, ALTER COLUMN language_code SET NOT NULL;
|
||||||
|
ALTER TABLE email_templates DROP COLUMN definition_id, DROP COLUMN service_id, DROP COLUMN service_language_id;
|
||||||
|
DROP TABLE email_template_definitions;
|
||||||
|
DROP TABLE service_languages;
|
||||||
|
DROP TABLE services;
|
||||||
|
CREATE UNIQUE INDEX "IX_email_templates_service_name_key_language_code" ON email_templates(service_name, key, language_code);
|
||||||
|
""");
|
||||||
|
}
|
||||||
|
|
||||||
|
private static string FlatUpgrade() => """
|
||||||
|
DO $audit$
|
||||||
|
DECLARE row_record record;
|
||||||
|
BEGIN
|
||||||
|
FOR row_record IN SELECT id, service_name, key, language_code, variables FROM email_templates LOOP
|
||||||
|
IF btrim(row_record.service_name) = '' OR btrim(row_record.key) = ''
|
||||||
|
OR btrim(row_record.language_code) = '' OR row_record.variables IS NULL
|
||||||
|
OR jsonb_typeof(row_record.variables) <> 'array' THEN
|
||||||
|
RAISE EXCEPTION 'Invalid template identity or variables array: service=%, key=%, language=%, id=%',
|
||||||
|
row_record.service_name, row_record.key, row_record.language_code, row_record.id;
|
||||||
|
END IF;
|
||||||
|
IF EXISTS (SELECT 1 FROM jsonb_array_elements(row_record.variables) v
|
||||||
|
WHERE jsonb_typeof(v) <> 'object'
|
||||||
|
OR jsonb_typeof(v->'name') IS DISTINCT FROM 'string'
|
||||||
|
OR btrim(v->>'name') = ''
|
||||||
|
OR jsonb_typeof(v->'required') IS DISTINCT FROM 'boolean'
|
||||||
|
OR (v - 'name' - 'required') <> '{}'::jsonb) THEN
|
||||||
|
RAISE EXCEPTION 'Invalid variables shape: service=%, key=%, language=%, id=%',
|
||||||
|
row_record.service_name, row_record.key, row_record.language_code, row_record.id;
|
||||||
|
END IF;
|
||||||
|
IF EXISTS (SELECT 1 FROM jsonb_array_elements(row_record.variables) v GROUP BY v->>'name' HAVING count(*) > 1) THEN
|
||||||
|
RAISE EXCEPTION 'Duplicate variable names: service=%, key=%, language=%, id=%',
|
||||||
|
row_record.service_name, row_record.key, row_record.language_code, row_record.id;
|
||||||
|
END IF;
|
||||||
|
END LOOP;
|
||||||
|
END $audit$;
|
||||||
|
CREATE TEMP TABLE template_contract_audit ON COMMIT DROP AS
|
||||||
|
SELECT t.*, COALESCE((SELECT jsonb_agg(v ORDER BY v->>'name')
|
||||||
|
FROM jsonb_array_elements(t.variables) v), '[]'::jsonb) AS contract
|
||||||
|
FROM email_templates t;
|
||||||
|
DO $conflicts$
|
||||||
|
DECLARE conflict_record record;
|
||||||
|
BEGIN
|
||||||
|
SELECT service_name, key, string_agg(language_code || ':' || id::text, ', ' ORDER BY language_code) AS records
|
||||||
|
INTO conflict_record FROM template_contract_audit
|
||||||
|
GROUP BY service_name, key HAVING count(DISTINCT contract) > 1 LIMIT 1;
|
||||||
|
IF FOUND THEN
|
||||||
|
RAISE EXCEPTION 'Conflicting shared variables: service=%, key=%, records=%',
|
||||||
|
conflict_record.service_name, conflict_record.key, conflict_record.records;
|
||||||
|
END IF;
|
||||||
|
END $conflicts$;
|
||||||
|
CREATE TABLE services (
|
||||||
|
id uuid CONSTRAINT "PK_services" PRIMARY KEY, name varchar(100) NOT NULL, revision uuid NOT NULL,
|
||||||
|
created timestamptz NOT NULL, updated timestamptz NULL);
|
||||||
|
CREATE UNIQUE INDEX "IX_services_name" ON services(name);
|
||||||
|
CREATE TABLE service_languages (
|
||||||
|
id uuid CONSTRAINT "PK_service_languages" PRIMARY KEY, service_id uuid NOT NULL,
|
||||||
|
code varchar(10) NOT NULL, created timestamptz NOT NULL, updated timestamptz NULL,
|
||||||
|
CONSTRAINT "AK_service_languages_id_service_id" UNIQUE(id, service_id),
|
||||||
|
CONSTRAINT "FK_service_languages_services_service_id" FOREIGN KEY(service_id) REFERENCES services(id) ON DELETE RESTRICT);
|
||||||
|
CREATE UNIQUE INDEX "IX_service_languages_service_id_code" ON service_languages(service_id, code);
|
||||||
|
CREATE TABLE email_template_definitions (
|
||||||
|
id uuid CONSTRAINT "PK_email_template_definitions" PRIMARY KEY, service_id uuid NOT NULL,
|
||||||
|
key varchar(100) NOT NULL, revision uuid NOT NULL, variables jsonb NOT NULL,
|
||||||
|
created timestamptz NOT NULL, updated timestamptz NULL,
|
||||||
|
CONSTRAINT "AK_email_template_definitions_id_service_id" UNIQUE(id, service_id),
|
||||||
|
CONSTRAINT "FK_email_template_definitions_services_service_id" FOREIGN KEY(service_id) REFERENCES services(id) ON DELETE RESTRICT);
|
||||||
|
CREATE UNIQUE INDEX "IX_email_template_definitions_service_id_key" ON email_template_definitions(service_id, key);
|
||||||
|
INSERT INTO services(id, name, revision, created)
|
||||||
|
SELECT gen_random_uuid(), service_name, gen_random_uuid(), min(created) FROM email_templates GROUP BY service_name;
|
||||||
|
INSERT INTO service_languages(id, service_id, code, created)
|
||||||
|
SELECT gen_random_uuid(), s.id, t.language_code, min(t.created)
|
||||||
|
FROM email_templates t JOIN services s ON s.name = t.service_name GROUP BY s.id, t.language_code;
|
||||||
|
INSERT INTO email_template_definitions(id, service_id, key, revision, variables, created, updated)
|
||||||
|
SELECT DISTINCT ON (t.service_name, t.key) t.id, s.id, t.key, gen_random_uuid(), t.contract,
|
||||||
|
min(t.created) OVER (PARTITION BY t.service_name, t.key),
|
||||||
|
max(t.updated) OVER (PARTITION BY t.service_name, t.key)
|
||||||
|
FROM template_contract_audit t JOIN services s ON s.name = t.service_name
|
||||||
|
ORDER BY t.service_name, t.key, t.id;
|
||||||
|
ALTER TABLE email_templates ADD COLUMN definition_id uuid, ADD COLUMN service_id uuid, ADD COLUMN service_language_id uuid;
|
||||||
|
UPDATE email_templates t SET definition_id = d.id, service_id = s.id, service_language_id = l.id
|
||||||
|
FROM email_template_definitions d JOIN services s ON s.id = d.service_id
|
||||||
|
JOIN service_languages l ON l.service_id = s.id
|
||||||
|
WHERE t.service_name = s.name AND t.key = d.key AND t.language_code = l.code;
|
||||||
|
ALTER TABLE email_templates ALTER COLUMN definition_id SET NOT NULL,
|
||||||
|
ALTER COLUMN service_id SET NOT NULL, ALTER COLUMN service_language_id SET NOT NULL;
|
||||||
|
ALTER TABLE email_templates ADD CONSTRAINT "FK_email_templates_email_template_definitions_definition_id_se~"
|
||||||
|
FOREIGN KEY (definition_id, service_id) REFERENCES email_template_definitions(id, service_id) ON DELETE CASCADE;
|
||||||
|
ALTER TABLE email_templates ADD CONSTRAINT "FK_email_templates_service_languages_service_language_id_servi~"
|
||||||
|
FOREIGN KEY (service_language_id, service_id) REFERENCES service_languages(id, service_id) ON DELETE RESTRICT;
|
||||||
|
DROP INDEX "IX_email_templates_service_name_key_language_code";
|
||||||
|
ALTER TABLE email_templates DROP COLUMN service_name, DROP COLUMN key, DROP COLUMN variables, DROP COLUMN language_code;
|
||||||
|
CREATE UNIQUE INDEX "IX_email_templates_definition_id_service_language_id" ON email_templates(definition_id, service_language_id);
|
||||||
|
CREATE INDEX "IX_email_templates_definition_id_service_id" ON email_templates(definition_id, service_id);
|
||||||
|
CREATE INDEX "IX_email_templates_service_language_id_service_id" ON email_templates(service_language_id, service_id);
|
||||||
|
""";
|
||||||
|
|
||||||
|
private static string SourceLayoutGuard() => """
|
||||||
|
SET LOCAL search_path = public;
|
||||||
|
DO $layout$
|
||||||
|
DECLARE
|
||||||
|
template_columns text[];
|
||||||
|
service_columns text[];
|
||||||
|
definition_columns text[];
|
||||||
|
source_constraints text[];
|
||||||
|
source_indexes text[];
|
||||||
|
known_interim_history boolean;
|
||||||
|
source_layout text;
|
||||||
|
BEGIN
|
||||||
|
SELECT array_agg(column_name || ':' || udt_name || ':' || coalesce(character_maximum_length::text, '') || ':' || is_nullable ORDER BY column_name)
|
||||||
|
INTO template_columns FROM information_schema.columns WHERE table_schema = 'public' AND table_name = 'email_templates';
|
||||||
|
SELECT array_agg(column_name || ':' || udt_name || ':' || coalesce(character_maximum_length::text, '') || ':' || is_nullable ORDER BY column_name)
|
||||||
|
INTO service_columns FROM information_schema.columns WHERE table_schema = 'public' AND table_name = 'services';
|
||||||
|
SELECT array_agg(column_name || ':' || udt_name || ':' || coalesce(character_maximum_length::text, '') || ':' || is_nullable ORDER BY column_name)
|
||||||
|
INTO definition_columns FROM information_schema.columns WHERE table_schema = 'public' AND table_name = 'email_template_definitions';
|
||||||
|
SELECT EXISTS (SELECT 1 FROM "__EFMigrationsHistory" WHERE "MigrationId" = '20261017070315_EmailTemplateLocalizations') INTO known_interim_history;
|
||||||
|
SELECT array_agg(c.relname || ':' || p.conname || ':' || pg_get_constraintdef(p.oid) ORDER BY c.relname, p.conname)
|
||||||
|
INTO source_constraints FROM pg_constraint p JOIN pg_class c ON c.oid = p.conrelid
|
||||||
|
JOIN pg_namespace n ON n.oid = c.relnamespace WHERE n.nspname = 'public'
|
||||||
|
AND c.relname IN ('services', 'email_template_definitions', 'email_templates');
|
||||||
|
SELECT array_agg(indexdef ORDER BY indexname) INTO source_indexes FROM pg_indexes
|
||||||
|
WHERE schemaname = 'public' AND tablename IN ('services', 'email_template_definitions', 'email_templates');
|
||||||
|
IF to_regclass('public.service_languages') IS NOT NULL THEN
|
||||||
|
RAISE EXCEPTION 'Unsupported email-template source layout: service language catalog already exists without the current migration history.';
|
||||||
|
END IF;
|
||||||
|
IF template_columns = ARRAY['created:timestamptz::NO',
|
||||||
|
'html_body:text::NO',
|
||||||
|
'id:uuid::NO',
|
||||||
|
'key:varchar:100:NO',
|
||||||
|
'language_code:varchar:10:NO',
|
||||||
|
'service_name:varchar:100:NO',
|
||||||
|
'subject:text::NO',
|
||||||
|
'text_body:text::NO',
|
||||||
|
'updated:timestamptz::YES',
|
||||||
|
'variables:jsonb::YES']
|
||||||
|
AND service_columns IS NULL AND definition_columns IS NULL AND NOT known_interim_history
|
||||||
|
AND source_constraints = ARRAY['email_templates:PK_email_templates:PRIMARY KEY (id)']
|
||||||
|
AND source_indexes = ARRAY[
|
||||||
|
'CREATE UNIQUE INDEX "IX_email_templates_service_name_key_language_code" ON public.email_templates USING btree (service_name, key, language_code)',
|
||||||
|
'CREATE UNIQUE INDEX "PK_email_templates" ON public.email_templates USING btree (id)'] THEN
|
||||||
|
source_layout := 'flat';
|
||||||
|
ELSIF template_columns = ARRAY['created:timestamptz::NO',
|
||||||
|
'definition_id:uuid::NO',
|
||||||
|
'html_body:text::NO',
|
||||||
|
'id:uuid::NO',
|
||||||
|
'language_code:varchar:10:NO',
|
||||||
|
'subject:text::NO',
|
||||||
|
'text_body:text::NO',
|
||||||
|
'updated:timestamptz::YES']
|
||||||
|
AND service_columns = ARRAY['created:timestamptz::NO',
|
||||||
|
'id:uuid::NO',
|
||||||
|
'name:varchar:100:NO',
|
||||||
|
'updated:timestamptz::YES']
|
||||||
|
AND definition_columns = ARRAY['created:timestamptz::NO',
|
||||||
|
'id:uuid::NO',
|
||||||
|
'key:varchar:100:NO',
|
||||||
|
'revision:uuid::NO',
|
||||||
|
'service_id:uuid::NO',
|
||||||
|
'updated:timestamptz::YES',
|
||||||
|
'variables:jsonb::NO']
|
||||||
|
AND known_interim_history
|
||||||
|
AND source_constraints = ARRAY[
|
||||||
|
'email_template_definitions:email_template_definitions_pkey:PRIMARY KEY (id)',
|
||||||
|
'email_template_definitions:email_template_definitions_service_id_fkey:FOREIGN KEY (service_id) REFERENCES services(id) ON DELETE RESTRICT',
|
||||||
|
'email_templates:FK_email_templates_email_template_definitions_definition_id:FOREIGN KEY (definition_id) REFERENCES email_template_definitions(id) ON DELETE CASCADE',
|
||||||
|
'email_templates:PK_email_templates:PRIMARY KEY (id)',
|
||||||
|
'services:services_pkey:PRIMARY KEY (id)']
|
||||||
|
AND source_indexes = ARRAY[
|
||||||
|
'CREATE UNIQUE INDEX "IX_email_template_definitions_service_id_key" ON public.email_template_definitions USING btree (service_id, key)',
|
||||||
|
'CREATE UNIQUE INDEX "IX_email_templates_definition_id_language_code" ON public.email_templates USING btree (definition_id, language_code)',
|
||||||
|
'CREATE UNIQUE INDEX "IX_services_name" ON public.services USING btree (name)',
|
||||||
|
'CREATE UNIQUE INDEX "PK_email_templates" ON public.email_templates USING btree (id)',
|
||||||
|
'CREATE UNIQUE INDEX email_template_definitions_pkey ON public.email_template_definitions USING btree (id)',
|
||||||
|
'CREATE UNIQUE INDEX services_pkey ON public.services USING btree (id)'] THEN
|
||||||
|
source_layout := 'interim';
|
||||||
|
ELSE
|
||||||
|
RAISE EXCEPTION 'Unsupported email-template source layout or history. Expected the flat legacy schema or the known 20261017070315 interim schema; no changes were made.';
|
||||||
|
END IF;
|
||||||
|
LOCK TABLE email_templates IN ACCESS EXCLUSIVE MODE;
|
||||||
|
IF source_layout = 'interim' THEN
|
||||||
|
LOCK TABLE services, email_template_definitions IN ACCESS EXCLUSIVE MODE;
|
||||||
|
END IF;
|
||||||
|
CREATE TEMP TABLE it1601_migration_origin (layout text NOT NULL) ON COMMIT DROP;
|
||||||
|
INSERT INTO it1601_migration_origin VALUES (source_layout);
|
||||||
|
END $layout$;
|
||||||
|
""";
|
||||||
|
|
||||||
|
private static string InterimUpgrade() => """
|
||||||
|
DO $interim_audit$
|
||||||
|
DECLARE row_record record;
|
||||||
|
BEGIN
|
||||||
|
IF EXISTS (SELECT 1 FROM services WHERE btrim(name) = '')
|
||||||
|
OR EXISTS (SELECT 1 FROM email_template_definitions WHERE btrim(key) = '')
|
||||||
|
OR EXISTS (SELECT 1 FROM email_templates WHERE btrim(language_code) = '') THEN
|
||||||
|
RAISE EXCEPTION 'Invalid blank identity in interim email-template data; no changes were made.';
|
||||||
|
END IF;
|
||||||
|
IF EXISTS (SELECT 1 FROM email_template_definitions d LEFT JOIN services s ON s.id = d.service_id WHERE s.id IS NULL)
|
||||||
|
OR EXISTS (SELECT 1 FROM email_templates t LEFT JOIN email_template_definitions d ON d.id = t.definition_id WHERE d.id IS NULL) THEN
|
||||||
|
RAISE EXCEPTION 'Invalid orphaned reference in interim email-template data; no changes were made.';
|
||||||
|
END IF;
|
||||||
|
FOR row_record IN SELECT d.id, s.name AS service_name, d.key, d.variables FROM email_template_definitions d JOIN services s ON s.id = d.service_id LOOP
|
||||||
|
IF row_record.variables IS NULL OR jsonb_typeof(row_record.variables) <> 'array' THEN
|
||||||
|
RAISE EXCEPTION 'Invalid interim shared variables array: service=%, key=%, definition=%', row_record.service_name, row_record.key, row_record.id;
|
||||||
|
END IF;
|
||||||
|
IF EXISTS (SELECT 1 FROM jsonb_array_elements(row_record.variables) v
|
||||||
|
WHERE jsonb_typeof(v) <> 'object'
|
||||||
|
OR jsonb_typeof(v->'name') IS DISTINCT FROM 'string'
|
||||||
|
OR btrim(v->>'name') = ''
|
||||||
|
OR jsonb_typeof(v->'required') IS DISTINCT FROM 'boolean'
|
||||||
|
OR (v - 'name' - 'required') <> '{}'::jsonb) THEN
|
||||||
|
RAISE EXCEPTION 'Invalid interim shared variables shape: service=%, key=%, definition=%', row_record.service_name, row_record.key, row_record.id;
|
||||||
|
END IF;
|
||||||
|
IF EXISTS (SELECT 1 FROM jsonb_array_elements(row_record.variables) v GROUP BY v->>'name' HAVING count(*) > 1) THEN
|
||||||
|
RAISE EXCEPTION 'Duplicate interim shared variable names: service=%, key=%, definition=%', row_record.service_name, row_record.key, row_record.id;
|
||||||
|
END IF;
|
||||||
|
END LOOP;
|
||||||
|
END $interim_audit$;
|
||||||
|
ALTER TABLE services ADD COLUMN revision uuid;
|
||||||
|
UPDATE services SET revision = gen_random_uuid();
|
||||||
|
ALTER TABLE services ALTER COLUMN revision SET NOT NULL;
|
||||||
|
ALTER TABLE services RENAME CONSTRAINT services_pkey TO "PK_services";
|
||||||
|
ALTER TABLE email_template_definitions RENAME CONSTRAINT email_template_definitions_pkey TO "PK_email_template_definitions";
|
||||||
|
ALTER TABLE email_template_definitions RENAME CONSTRAINT email_template_definitions_service_id_fkey TO "FK_email_template_definitions_services_service_id";
|
||||||
|
ALTER TABLE email_template_definitions ADD CONSTRAINT "AK_email_template_definitions_id_service_id" UNIQUE(id, service_id);
|
||||||
|
CREATE TABLE service_languages (
|
||||||
|
id uuid CONSTRAINT "PK_service_languages" PRIMARY KEY, service_id uuid NOT NULL,
|
||||||
|
code varchar(10) NOT NULL, created timestamptz NOT NULL, updated timestamptz NULL,
|
||||||
|
CONSTRAINT "AK_service_languages_id_service_id" UNIQUE(id, service_id),
|
||||||
|
CONSTRAINT "FK_service_languages_services_service_id" FOREIGN KEY(service_id) REFERENCES services(id) ON DELETE RESTRICT);
|
||||||
|
CREATE UNIQUE INDEX "IX_service_languages_service_id_code" ON service_languages(service_id, code);
|
||||||
|
INSERT INTO service_languages(id, service_id, code, created, updated)
|
||||||
|
SELECT gen_random_uuid(), d.service_id, t.language_code, min(t.created), max(t.updated)
|
||||||
|
FROM email_templates t JOIN email_template_definitions d ON d.id = t.definition_id GROUP BY d.service_id, t.language_code;
|
||||||
|
ALTER TABLE email_templates ADD COLUMN service_id uuid, ADD COLUMN service_language_id uuid;
|
||||||
|
UPDATE email_templates t SET service_id = d.service_id, service_language_id = l.id
|
||||||
|
FROM email_template_definitions d JOIN service_languages l ON l.service_id = d.service_id
|
||||||
|
WHERE t.definition_id = d.id AND t.language_code = l.code;
|
||||||
|
ALTER TABLE email_templates ALTER COLUMN service_id SET NOT NULL, ALTER COLUMN service_language_id SET NOT NULL;
|
||||||
|
ALTER TABLE email_templates ADD CONSTRAINT "FK_email_templates_email_template_definitions_definition_id_se~"
|
||||||
|
FOREIGN KEY (definition_id, service_id) REFERENCES email_template_definitions(id, service_id) ON DELETE CASCADE;
|
||||||
|
ALTER TABLE email_templates ADD CONSTRAINT "FK_email_templates_service_languages_service_language_id_servi~"
|
||||||
|
FOREIGN KEY (service_language_id, service_id) REFERENCES service_languages(id, service_id) ON DELETE RESTRICT;
|
||||||
|
ALTER TABLE email_templates DROP CONSTRAINT "FK_email_templates_email_template_definitions_definition_id";
|
||||||
|
DROP INDEX "IX_email_templates_definition_id_language_code";
|
||||||
|
ALTER TABLE email_templates DROP COLUMN language_code;
|
||||||
|
CREATE UNIQUE INDEX "IX_email_templates_definition_id_service_language_id" ON email_templates(definition_id, service_language_id);
|
||||||
|
CREATE INDEX "IX_email_templates_definition_id_service_id" ON email_templates(definition_id, service_id);
|
||||||
|
CREATE INDEX "IX_email_templates_service_language_id_service_id" ON email_templates(service_language_id, service_id);
|
||||||
|
""";
|
||||||
|
}
|
||||||
+182
-31
@@ -17,7 +17,7 @@ namespace HrynCo.NotificationService.DAL.EF.Migrations
|
|||||||
{
|
{
|
||||||
#pragma warning disable 612, 618
|
#pragma warning disable 612, 618
|
||||||
modelBuilder
|
modelBuilder
|
||||||
.HasAnnotation("ProductVersion", "9.0.5")
|
.HasAnnotation("ProductVersion", "10.0.7")
|
||||||
.HasAnnotation("Relational:MaxIdentifierLength", 63);
|
.HasAnnotation("Relational:MaxIdentifierLength", 63);
|
||||||
|
|
||||||
NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder);
|
NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder);
|
||||||
@@ -114,6 +114,44 @@ namespace HrynCo.NotificationService.DAL.EF.Migrations
|
|||||||
b.ToTable("email_channel_usage", (string)null);
|
b.ToTable("email_channel_usage", (string)null);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
modelBuilder.Entity("HrynCo.NotificationService.DAL.EF.Entities.EmailTemplateDefinitionEntity", b =>
|
||||||
|
{
|
||||||
|
b.Property<Guid>("Id")
|
||||||
|
.ValueGeneratedOnAdd()
|
||||||
|
.HasColumnType("uuid")
|
||||||
|
.HasColumnName("id");
|
||||||
|
|
||||||
|
b.Property<DateTimeOffset>("Created")
|
||||||
|
.HasColumnType("timestamp with time zone")
|
||||||
|
.HasColumnName("created");
|
||||||
|
|
||||||
|
b.Property<string>("Key")
|
||||||
|
.IsRequired()
|
||||||
|
.HasMaxLength(100)
|
||||||
|
.HasColumnType("character varying(100)")
|
||||||
|
.HasColumnName("key");
|
||||||
|
|
||||||
|
b.Property<Guid>("Revision")
|
||||||
|
.IsConcurrencyToken()
|
||||||
|
.HasColumnType("uuid")
|
||||||
|
.HasColumnName("revision");
|
||||||
|
|
||||||
|
b.Property<Guid>("ServiceId")
|
||||||
|
.HasColumnType("uuid")
|
||||||
|
.HasColumnName("service_id");
|
||||||
|
|
||||||
|
b.Property<DateTimeOffset?>("Updated")
|
||||||
|
.HasColumnType("timestamp with time zone")
|
||||||
|
.HasColumnName("updated");
|
||||||
|
|
||||||
|
b.HasKey("Id");
|
||||||
|
|
||||||
|
b.HasIndex("ServiceId", "Key")
|
||||||
|
.IsUnique();
|
||||||
|
|
||||||
|
b.ToTable("email_template_definitions", (string)null);
|
||||||
|
});
|
||||||
|
|
||||||
modelBuilder.Entity("HrynCo.NotificationService.DAL.EF.Entities.EmailTemplateEntity", b =>
|
modelBuilder.Entity("HrynCo.NotificationService.DAL.EF.Entities.EmailTemplateEntity", b =>
|
||||||
{
|
{
|
||||||
b.Property<Guid>("Id")
|
b.Property<Guid>("Id")
|
||||||
@@ -125,28 +163,22 @@ namespace HrynCo.NotificationService.DAL.EF.Migrations
|
|||||||
.HasColumnType("timestamp with time zone")
|
.HasColumnType("timestamp with time zone")
|
||||||
.HasColumnName("created");
|
.HasColumnName("created");
|
||||||
|
|
||||||
|
b.Property<Guid>("DefinitionId")
|
||||||
|
.HasColumnType("uuid")
|
||||||
|
.HasColumnName("definition_id");
|
||||||
|
|
||||||
b.Property<string>("HtmlBody")
|
b.Property<string>("HtmlBody")
|
||||||
.IsRequired()
|
.IsRequired()
|
||||||
.HasColumnType("text")
|
.HasColumnType("text")
|
||||||
.HasColumnName("html_body");
|
.HasColumnName("html_body");
|
||||||
|
|
||||||
b.Property<string>("Key")
|
b.Property<Guid>("ServiceId")
|
||||||
.IsRequired()
|
.HasColumnType("uuid")
|
||||||
.HasMaxLength(100)
|
.HasColumnName("service_id");
|
||||||
.HasColumnType("character varying(100)")
|
|
||||||
.HasColumnName("key");
|
|
||||||
|
|
||||||
b.Property<string>("LanguageCode")
|
b.Property<Guid>("ServiceLanguageId")
|
||||||
.IsRequired()
|
.HasColumnType("uuid")
|
||||||
.HasMaxLength(10)
|
.HasColumnName("service_language_id");
|
||||||
.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")
|
b.Property<string>("Subject")
|
||||||
.IsRequired()
|
.IsRequired()
|
||||||
@@ -164,12 +196,83 @@ namespace HrynCo.NotificationService.DAL.EF.Migrations
|
|||||||
|
|
||||||
b.HasKey("Id");
|
b.HasKey("Id");
|
||||||
|
|
||||||
b.HasIndex("ServiceName", "Key", "LanguageCode")
|
b.HasIndex("DefinitionId", "ServiceId");
|
||||||
|
|
||||||
|
b.HasIndex("DefinitionId", "ServiceLanguageId")
|
||||||
.IsUnique();
|
.IsUnique();
|
||||||
|
|
||||||
|
b.HasIndex("ServiceLanguageId", "ServiceId");
|
||||||
|
|
||||||
b.ToTable("email_templates", (string)null);
|
b.ToTable("email_templates", (string)null);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
modelBuilder.Entity("HrynCo.NotificationService.DAL.EF.Entities.ServiceEntity", b =>
|
||||||
|
{
|
||||||
|
b.Property<Guid>("Id")
|
||||||
|
.ValueGeneratedOnAdd()
|
||||||
|
.HasColumnType("uuid")
|
||||||
|
.HasColumnName("id");
|
||||||
|
|
||||||
|
b.Property<DateTimeOffset>("Created")
|
||||||
|
.HasColumnType("timestamp with time zone")
|
||||||
|
.HasColumnName("created");
|
||||||
|
|
||||||
|
b.Property<string>("Name")
|
||||||
|
.IsRequired()
|
||||||
|
.HasMaxLength(100)
|
||||||
|
.HasColumnType("character varying(100)")
|
||||||
|
.HasColumnName("name");
|
||||||
|
|
||||||
|
b.Property<Guid>("Revision")
|
||||||
|
.IsConcurrencyToken()
|
||||||
|
.HasColumnType("uuid")
|
||||||
|
.HasColumnName("revision");
|
||||||
|
|
||||||
|
b.Property<DateTimeOffset?>("Updated")
|
||||||
|
.HasColumnType("timestamp with time zone")
|
||||||
|
.HasColumnName("updated");
|
||||||
|
|
||||||
|
b.HasKey("Id");
|
||||||
|
|
||||||
|
b.HasIndex("Name")
|
||||||
|
.IsUnique();
|
||||||
|
|
||||||
|
b.ToTable("services", (string)null);
|
||||||
|
});
|
||||||
|
|
||||||
|
modelBuilder.Entity("HrynCo.NotificationService.DAL.EF.Entities.ServiceLanguageEntity", b =>
|
||||||
|
{
|
||||||
|
b.Property<Guid>("Id")
|
||||||
|
.ValueGeneratedOnAdd()
|
||||||
|
.HasColumnType("uuid")
|
||||||
|
.HasColumnName("id");
|
||||||
|
|
||||||
|
b.Property<string>("Code")
|
||||||
|
.IsRequired()
|
||||||
|
.HasMaxLength(10)
|
||||||
|
.HasColumnType("character varying(10)")
|
||||||
|
.HasColumnName("code");
|
||||||
|
|
||||||
|
b.Property<DateTimeOffset>("Created")
|
||||||
|
.HasColumnType("timestamp with time zone")
|
||||||
|
.HasColumnName("created");
|
||||||
|
|
||||||
|
b.Property<Guid>("ServiceId")
|
||||||
|
.HasColumnType("uuid")
|
||||||
|
.HasColumnName("service_id");
|
||||||
|
|
||||||
|
b.Property<DateTimeOffset?>("Updated")
|
||||||
|
.HasColumnType("timestamp with time zone")
|
||||||
|
.HasColumnName("updated");
|
||||||
|
|
||||||
|
b.HasKey("Id");
|
||||||
|
|
||||||
|
b.HasIndex("ServiceId", "Code")
|
||||||
|
.IsUnique();
|
||||||
|
|
||||||
|
b.ToTable("service_languages", (string)null);
|
||||||
|
});
|
||||||
|
|
||||||
modelBuilder.Entity("HrynCo.NotificationService.DAL.EF.Entities.EmailChannelUsageEntity", b =>
|
modelBuilder.Entity("HrynCo.NotificationService.DAL.EF.Entities.EmailChannelUsageEntity", b =>
|
||||||
{
|
{
|
||||||
b.HasOne("HrynCo.NotificationService.DAL.EF.Entities.EmailChannelEntity", null)
|
b.HasOne("HrynCo.NotificationService.DAL.EF.Entities.EmailChannelEntity", null)
|
||||||
@@ -179,43 +282,91 @@ namespace HrynCo.NotificationService.DAL.EF.Migrations
|
|||||||
.IsRequired();
|
.IsRequired();
|
||||||
});
|
});
|
||||||
|
|
||||||
modelBuilder.Entity("HrynCo.NotificationService.DAL.EF.Entities.EmailTemplateEntity", b =>
|
modelBuilder.Entity("HrynCo.NotificationService.DAL.EF.Entities.EmailTemplateDefinitionEntity", b =>
|
||||||
{
|
{
|
||||||
|
b.HasOne("HrynCo.NotificationService.DAL.EF.Entities.ServiceEntity", "Service")
|
||||||
|
.WithMany()
|
||||||
|
.HasForeignKey("ServiceId")
|
||||||
|
.OnDelete(DeleteBehavior.Restrict)
|
||||||
|
.IsRequired();
|
||||||
|
|
||||||
b.OwnsMany("HrynCo.NotificationService.DAL.EF.Entities.EmailTemplateVariableData", "Variables", b1 =>
|
b.OwnsMany("HrynCo.NotificationService.DAL.EF.Entities.EmailTemplateVariableData", "Variables", b1 =>
|
||||||
{
|
{
|
||||||
b1.Property<Guid>("EmailTemplateEntityId")
|
b1.Property<Guid>("EmailTemplateDefinitionEntityId");
|
||||||
.HasColumnType("uuid");
|
|
||||||
|
|
||||||
b1.Property<int>("__synthesizedOrdinal")
|
b1.Property<int>("__synthesizedOrdinal")
|
||||||
.ValueGeneratedOnAdd()
|
.ValueGeneratedOnAdd();
|
||||||
.HasColumnType("integer");
|
|
||||||
|
|
||||||
b1.Property<string>("Name")
|
b1.Property<string>("Name")
|
||||||
.IsRequired()
|
.IsRequired()
|
||||||
.HasColumnType("text")
|
.HasJsonPropertyName("name");
|
||||||
.HasAnnotation("Relational:JsonPropertyName", "name");
|
|
||||||
|
|
||||||
b1.Property<bool>("Required")
|
b1.Property<bool>("Required")
|
||||||
.HasColumnType("boolean")
|
.HasJsonPropertyName("required");
|
||||||
.HasAnnotation("Relational:JsonPropertyName", "required");
|
|
||||||
|
|
||||||
b1.HasKey("EmailTemplateEntityId", "__synthesizedOrdinal");
|
b1.HasKey("EmailTemplateDefinitionEntityId", "__synthesizedOrdinal");
|
||||||
|
|
||||||
b1.ToTable("email_templates");
|
b1.ToTable("email_template_definitions");
|
||||||
|
|
||||||
b1.ToJson("variables");
|
b1
|
||||||
|
.ToJson("variables")
|
||||||
|
.HasColumnType("jsonb");
|
||||||
|
|
||||||
b1.WithOwner()
|
b1.WithOwner()
|
||||||
.HasForeignKey("EmailTemplateEntityId");
|
.HasForeignKey("EmailTemplateDefinitionEntityId");
|
||||||
});
|
});
|
||||||
|
|
||||||
|
b.Navigation("Service");
|
||||||
|
|
||||||
b.Navigation("Variables");
|
b.Navigation("Variables");
|
||||||
});
|
});
|
||||||
|
|
||||||
|
modelBuilder.Entity("HrynCo.NotificationService.DAL.EF.Entities.EmailTemplateEntity", b =>
|
||||||
|
{
|
||||||
|
b.HasOne("HrynCo.NotificationService.DAL.EF.Entities.EmailTemplateDefinitionEntity", "Definition")
|
||||||
|
.WithMany("Languages")
|
||||||
|
.HasForeignKey("DefinitionId", "ServiceId")
|
||||||
|
.HasPrincipalKey("Id", "ServiceId")
|
||||||
|
.OnDelete(DeleteBehavior.Cascade)
|
||||||
|
.IsRequired();
|
||||||
|
|
||||||
|
b.HasOne("HrynCo.NotificationService.DAL.EF.Entities.ServiceLanguageEntity", "ServiceLanguage")
|
||||||
|
.WithMany()
|
||||||
|
.HasForeignKey("ServiceLanguageId", "ServiceId")
|
||||||
|
.HasPrincipalKey("Id", "ServiceId")
|
||||||
|
.OnDelete(DeleteBehavior.Restrict)
|
||||||
|
.IsRequired();
|
||||||
|
|
||||||
|
b.Navigation("Definition");
|
||||||
|
|
||||||
|
b.Navigation("ServiceLanguage");
|
||||||
|
});
|
||||||
|
|
||||||
|
modelBuilder.Entity("HrynCo.NotificationService.DAL.EF.Entities.ServiceLanguageEntity", b =>
|
||||||
|
{
|
||||||
|
b.HasOne("HrynCo.NotificationService.DAL.EF.Entities.ServiceEntity", "Service")
|
||||||
|
.WithMany("Languages")
|
||||||
|
.HasForeignKey("ServiceId")
|
||||||
|
.OnDelete(DeleteBehavior.Restrict)
|
||||||
|
.IsRequired();
|
||||||
|
|
||||||
|
b.Navigation("Service");
|
||||||
|
});
|
||||||
|
|
||||||
modelBuilder.Entity("HrynCo.NotificationService.DAL.EF.Entities.EmailChannelEntity", b =>
|
modelBuilder.Entity("HrynCo.NotificationService.DAL.EF.Entities.EmailChannelEntity", b =>
|
||||||
{
|
{
|
||||||
b.Navigation("UsageRecords");
|
b.Navigation("UsageRecords");
|
||||||
});
|
});
|
||||||
|
|
||||||
|
modelBuilder.Entity("HrynCo.NotificationService.DAL.EF.Entities.EmailTemplateDefinitionEntity", b =>
|
||||||
|
{
|
||||||
|
b.Navigation("Languages");
|
||||||
|
});
|
||||||
|
|
||||||
|
modelBuilder.Entity("HrynCo.NotificationService.DAL.EF.Entities.ServiceEntity", b =>
|
||||||
|
{
|
||||||
|
b.Navigation("Languages");
|
||||||
|
});
|
||||||
#pragma warning restore 612, 618
|
#pragma warning restore 612, 618
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,6 +10,9 @@ public class NotificationDbContext : DbContext
|
|||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
internal DbSet<ServiceLanguageEntity> ServiceLanguages => Set<ServiceLanguageEntity>();
|
||||||
|
internal DbSet<ServiceEntity> Services => Set<ServiceEntity>();
|
||||||
|
internal DbSet<EmailTemplateDefinitionEntity> TemplateDefinitions => Set<EmailTemplateDefinitionEntity>();
|
||||||
internal DbSet<EmailTemplateEntity> Templates => Set<EmailTemplateEntity>();
|
internal DbSet<EmailTemplateEntity> Templates => Set<EmailTemplateEntity>();
|
||||||
internal DbSet<EmailChannelEntity> Providers => Set<EmailChannelEntity>();
|
internal DbSet<EmailChannelEntity> Providers => Set<EmailChannelEntity>();
|
||||||
internal DbSet<EmailChannelUsageEntity> EmailChannelUsage => Set<EmailChannelUsageEntity>();
|
internal DbSet<EmailChannelUsageEntity> EmailChannelUsage => Set<EmailChannelUsageEntity>();
|
||||||
|
|||||||
@@ -54,7 +54,7 @@ internal sealed class EmailChannelRepository : NotificationBaseRepository<EmailC
|
|||||||
|
|
||||||
public async Task<EmailChannel?> GetByIdAsync(Guid id, CancellationToken ct = default)
|
public async Task<EmailChannel?> GetByIdAsync(Guid id, CancellationToken ct = default)
|
||||||
{
|
{
|
||||||
EmailChannelEntity? entity = await EfRepository.GetByIdAsync(id);
|
EmailChannelEntity? entity = await EfRepository.Get().AsNoTracking().SingleOrDefaultAsync(c => c.Id == id, ct);
|
||||||
return entity is null ? null : MapToDomain(entity);
|
return entity is null ? null : MapToDomain(entity);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,91 +1,324 @@
|
|||||||
using HrynCo.NotificationService.DAL.Abstract.Repositories;
|
using HrynCo.NotificationService.DAL.Abstract.Repositories;
|
||||||
using HrynCo.NotificationService.DAL.Abstract.Templates;
|
using HrynCo.NotificationService.DAL.Abstract.Templates;
|
||||||
using HrynCo.NotificationService.DAL.EF.Core;
|
|
||||||
using HrynCo.NotificationService.DAL.EF.Entities;
|
using HrynCo.NotificationService.DAL.EF.Entities;
|
||||||
using Microsoft.EntityFrameworkCore;
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
|
||||||
namespace HrynCo.NotificationService.DAL.EF.Repositories;
|
namespace HrynCo.NotificationService.DAL.EF.Repositories;
|
||||||
|
|
||||||
internal sealed class EmailTemplateRepository
|
internal sealed class EmailTemplateRepository(NotificationDbContext db, TemplateMutationRunner mutations)
|
||||||
: NotificationBaseRepository<EmailTemplateEntity>, IEmailTemplateRepository
|
: IEmailTemplateRepository, IEmailTemplateDefinitionRepository
|
||||||
{
|
{
|
||||||
public EmailTemplateRepository(NotificationDbContext dbContext) : base(dbContext)
|
private IQueryable<EmailTemplateEntity> Languages => db.Templates
|
||||||
|
.Include(t => t.Definition).ThenInclude(d => d.Service).Include(t => t.ServiceLanguage);
|
||||||
|
private IQueryable<EmailTemplateDefinitionEntity> Definitions => db.TemplateDefinitions
|
||||||
|
.Include(d => d.Service).ThenInclude(s => s.Languages)
|
||||||
|
.Include(d => d.Languages).ThenInclude(t => t.ServiceLanguage);
|
||||||
|
|
||||||
|
public async Task<IReadOnlyList<EmailTemplate>> GetAllAsync(string? serviceName = null, string? key = null, CancellationToken ct = default)
|
||||||
{
|
{
|
||||||
|
var query = Languages.AsNoTracking();
|
||||||
|
if (!string.IsNullOrWhiteSpace(serviceName))
|
||||||
|
query = query.Where(t => t.Definition.Service.Name == serviceName);
|
||||||
|
if (!string.IsNullOrWhiteSpace(key))
|
||||||
|
query = query.Where(t => t.Definition.Key == key);
|
||||||
|
var entities = await query.OrderBy(t => t.Definition.Service.Name).ThenBy(t => t.Definition.Key)
|
||||||
|
.ThenBy(t => t.ServiceLanguage.Code).ToListAsync(ct);
|
||||||
|
return entities.Select(MapLanguage).ToList();
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<IReadOnlyList<EmailTemplate>> GetAllAsync(CancellationToken ct = default)
|
public Task<IReadOnlyList<EmailTemplate>> GetByServiceAsync(string serviceName, CancellationToken ct = default) =>
|
||||||
{
|
GetAllAsync(serviceName, ct: ct);
|
||||||
List<EmailTemplateEntity> entities = await EfRepository.Get()
|
|
||||||
.AsNoTracking()
|
|
||||||
.ToListAsync(ct);
|
|
||||||
return entities.Select(MapToDomain).ToList();
|
|
||||||
}
|
|
||||||
|
|
||||||
public async Task<IReadOnlyList<EmailTemplate>> GetByServiceAsync(string serviceName, CancellationToken ct = default)
|
|
||||||
{
|
|
||||||
List<EmailTemplateEntity> entities = await EfRepository.Get()
|
|
||||||
.AsNoTracking()
|
|
||||||
.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)
|
public async Task<EmailTemplate?> GetAsync(string serviceName, string key, string languageCode, CancellationToken ct = default)
|
||||||
{
|
{
|
||||||
EmailTemplateEntity? entity = await EfRepository.Get()
|
var code = languageCode;
|
||||||
.AsNoTracking()
|
var entity = await Languages.AsNoTracking().SingleOrDefaultAsync(t =>
|
||||||
.FirstOrDefaultAsync(
|
t.Definition.Service.Name == serviceName && t.Definition.Key == key && t.ServiceLanguage.Code == code, ct);
|
||||||
x => x.ServiceName == serviceName && x.Key == key && x.LanguageCode == languageCode, ct);
|
return entity is null ? null : MapLanguage(entity);
|
||||||
|
|
||||||
return entity is null ? null : MapToDomain(entity);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public Task AddAsync(EmailTemplate EmailTemplate, CancellationToken ct = default)
|
public async Task<EmailTemplateDefinition?> GetDefinitionAsync(string serviceName, string key, CancellationToken ct = default)
|
||||||
{
|
{
|
||||||
return EfRepository.AddAsync(MapToEntity(EmailTemplate));
|
var entity = await Definitions.AsNoTracking().SingleOrDefaultAsync(d => d.Service.Name == serviceName && d.Key == key, ct);
|
||||||
|
return entity is null ? null : MapDefinition(entity);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Task UpdateAsync(EmailTemplate EmailTemplate, CancellationToken ct = default)
|
public async Task<EmailTemplateDefinition?> GetDefinitionAsync(Guid serviceId, string key, CancellationToken ct = default)
|
||||||
{
|
{
|
||||||
EmailTemplateEntity entity = MapToEntity(EmailTemplate);
|
var entity = await Definitions.AsNoTracking().SingleOrDefaultAsync(d => d.ServiceId == serviceId && d.Key == key, ct);
|
||||||
entity.Updated = DateTimeOffset.UtcNow;
|
return entity is null ? null : MapDefinition(entity);
|
||||||
return EfRepository.UpdateAsync(entity);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task DeleteAsync(EmailTemplate EmailTemplate, CancellationToken ct = default)
|
public async Task<EmailTemplateDefinition?> UpsertDefinitionAsync(EmailTemplateDefinitionPatch patch, CancellationToken ct = default)
|
||||||
|
{
|
||||||
|
EmailTemplateDefinitionEntity? saved = null;
|
||||||
|
await mutations.ExecuteAsync(async () =>
|
||||||
|
{
|
||||||
|
var service = await db.Services.Include(s => s.Languages).SingleOrDefaultAsync(s => s.Id == patch.ServiceId, ct);
|
||||||
|
if (service is null)
|
||||||
|
return;
|
||||||
|
ValidatePatch(patch, service);
|
||||||
|
var entity = await LoadForUpsertAsync(patch, service, ct);
|
||||||
|
entity.Variables = MapVariables(patch.Variables);
|
||||||
|
var removed = patch.RemoveLanguageCodes.ToHashSet(StringComparer.Ordinal);
|
||||||
|
db.Templates.RemoveRange(entity.Languages.Where(t => removed.Contains(t.ServiceLanguage.Code)));
|
||||||
|
// Omitted translations are retained. Only explicitly supplied content is changed.
|
||||||
|
UpsertLanguages(entity, patch.Translations);
|
||||||
|
Touch(entity);
|
||||||
|
saved = entity;
|
||||||
|
}, ct);
|
||||||
|
return saved is null ? null : MapDefinition(saved);
|
||||||
|
}
|
||||||
|
|
||||||
|
private async Task<EmailTemplateDefinitionEntity> LoadForUpsertAsync(EmailTemplateDefinitionPatch patch, ServiceEntity service, CancellationToken ct)
|
||||||
|
{
|
||||||
|
var entity = await Definitions.SingleOrDefaultAsync(d => d.ServiceId == patch.ServiceId && d.Key == patch.Key, ct);
|
||||||
|
if (patch.Revision is null)
|
||||||
{
|
{
|
||||||
EmailTemplateEntity? entity = await EfRepository.Get()
|
|
||||||
.FirstOrDefaultAsync(x => x.Id == EmailTemplate.Id, ct);
|
|
||||||
if (entity is not null)
|
if (entity is not null)
|
||||||
await EfRepository.DeleteAsync(entity);
|
throw new EmailTemplateWriteException("A definition with this service and key already exists. Read its revision before updating.");
|
||||||
|
entity = new EmailTemplateDefinitionEntity { Service = service, ServiceId = service.Id, Key = patch.Key, Created = DateTimeOffset.UtcNow };
|
||||||
|
db.TemplateDefinitions.Add(entity);
|
||||||
|
return entity;
|
||||||
|
}
|
||||||
|
if (entity is null || entity.Revision != patch.Revision)
|
||||||
|
throw new EmailTemplateWriteException("The definition is missing or its revision changed. Read it again before updating.");
|
||||||
|
return entity;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static EmailTemplate MapToDomain(EmailTemplateEntity e) => new()
|
private static void ValidatePatch(EmailTemplateDefinitionPatch patch, ServiceEntity service)
|
||||||
{
|
{
|
||||||
Id = e.Id,
|
EmailTemplateValidation.ValidateDefinition(new EmailTemplateDefinition
|
||||||
ServiceName = e.ServiceName,
|
{
|
||||||
Key = e.Key,
|
ServiceId = service.Id, ServiceName = service.Name, Key = patch.Key,
|
||||||
LanguageCode = e.LanguageCode,
|
Variables = patch.Variables, Languages = patch.Translations
|
||||||
Subject = e.Subject,
|
});
|
||||||
HtmlBody = e.HtmlBody,
|
if (patch.Revision == Guid.Empty)
|
||||||
TextBody = e.TextBody,
|
throw new EmailTemplateWriteException("Revision must be null for creation or a nonempty revision from the last read for update.", false);
|
||||||
Variables = e.Variables.Select(v => new EmailTemplateVariable { Name = v.Name, Required = v.Required }).ToList(),
|
var removed = patch.RemoveLanguageCodes;
|
||||||
Created = e.Created,
|
if (removed.Any(string.IsNullOrWhiteSpace) || removed.Distinct(StringComparer.Ordinal).Count() != removed.Count)
|
||||||
Updated = e.Updated
|
throw new EmailTemplateWriteException("removeLanguageCodes must contain unique nonblank language codes.", false);
|
||||||
|
var supplied = patch.Translations.Select(t => t.LanguageCode).ToHashSet(StringComparer.Ordinal);
|
||||||
|
if (removed.Any(supplied.Contains))
|
||||||
|
throw new EmailTemplateWriteException("A language cannot be supplied and removed in the same request.", false);
|
||||||
|
var supported = service.Languages.Select(l => l.Code).ToHashSet(StringComparer.Ordinal);
|
||||||
|
if (supplied.Concat(removed).Any(code => !supported.Contains(code)))
|
||||||
|
throw new EmailTemplateWriteException("Every supplied or removed language must exactly match a code in this service's catalog.", false);
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<IReadOnlyList<EmailTemplateDefinition>> GetDefinitionsAsync(Guid serviceId, CancellationToken ct = default)
|
||||||
|
{
|
||||||
|
var entities = await Definitions.AsNoTracking().Where(d => d.ServiceId == serviceId).OrderBy(d => d.Key).ToListAsync(ct);
|
||||||
|
return entities.Select(MapDefinition).ToList();
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<bool> DeleteDefinitionAsync(Guid serviceId, string key, Guid revision, CancellationToken ct = default)
|
||||||
|
{
|
||||||
|
var removed = false;
|
||||||
|
await mutations.ExecuteAsync(async () =>
|
||||||
|
{
|
||||||
|
if (string.IsNullOrWhiteSpace(key) || key.Length > 100 || revision == Guid.Empty)
|
||||||
|
throw new EmailTemplateWriteException("An exact key (at most 100 characters) and a nonempty revision are required.", false);
|
||||||
|
var definition = await Definitions.SingleOrDefaultAsync(d => d.ServiceId == serviceId && d.Key == key, ct);
|
||||||
|
if (definition is null)
|
||||||
|
return;
|
||||||
|
if (definition.Revision != revision)
|
||||||
|
throw new EmailTemplateWriteException("The definition revision changed. Read it again before deleting.");
|
||||||
|
db.TemplateDefinitions.Remove(definition);
|
||||||
|
removed = true;
|
||||||
|
}, ct);
|
||||||
|
return removed;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Task AddAsync(EmailTemplate template, CancellationToken ct = default) => mutations.ExecuteAsync(async () =>
|
||||||
|
{
|
||||||
|
ValidateLegacy(template);
|
||||||
|
var definition = await FindDefinitionAsync(template.ServiceName, template.Key, ct);
|
||||||
|
if (definition is null)
|
||||||
|
{
|
||||||
|
await RequireServiceLanguageAsync(template.ServiceName, template.LanguageCode, ct);
|
||||||
|
definition = await CreateDefinitionAsync(template.ServiceName, template.Key, template.Variables, ct);
|
||||||
|
}
|
||||||
|
EnsureMatchingContract(definition, template.Variables);
|
||||||
|
await RequireServiceLanguageAsync(template.ServiceName, template.LanguageCode, ct);
|
||||||
|
var code = template.LanguageCode;
|
||||||
|
if (definition.Languages.Any(t => t.ServiceLanguage.Code == code))
|
||||||
|
throw new EmailTemplateWriteException("This language already exists for the template.");
|
||||||
|
var language = NewLanguage(template, definition);
|
||||||
|
definition.Languages.Add(language);
|
||||||
|
db.Templates.Add(language);
|
||||||
|
Touch(definition);
|
||||||
|
}, ct);
|
||||||
|
|
||||||
|
public Task UpdateAsync(EmailTemplate template, CancellationToken ct = default) => mutations.ExecuteAsync(async () =>
|
||||||
|
{
|
||||||
|
ValidateLegacy(template);
|
||||||
|
var definition = await RequireDefinitionAsync(template.ServiceName, template.Key, ct);
|
||||||
|
EnsureMatchingContract(definition, template.Variables);
|
||||||
|
var language = definition.Languages.SingleOrDefault(t => t.Id == template.Id)
|
||||||
|
?? throw new EmailTemplateWriteException("The language changed or was removed. Reload the template.");
|
||||||
|
SetContent(language, template);
|
||||||
|
Touch(definition);
|
||||||
|
}, ct);
|
||||||
|
|
||||||
|
public Task DeleteAsync(EmailTemplate template, CancellationToken ct = default) => mutations.ExecuteAsync(async () =>
|
||||||
|
{
|
||||||
|
var definition = await RequireDefinitionAsync(template.ServiceName, template.Key, ct);
|
||||||
|
var language = definition.Languages.SingleOrDefault(t => t.Id == template.Id)
|
||||||
|
?? throw new EmailTemplateWriteException("The language changed or was removed. Reload the template.");
|
||||||
|
db.Templates.Remove(language);
|
||||||
|
Touch(definition);
|
||||||
|
}, ct);
|
||||||
|
|
||||||
|
public Task SaveDefinitionAsync(EmailTemplateDefinition definition, bool isNew, CancellationToken ct = default) => mutations.ExecuteAsync(async () =>
|
||||||
|
{
|
||||||
|
EmailTemplateValidation.ValidateDefinition(definition);
|
||||||
|
await ValidateServiceLanguagesAsync(definition, ct);
|
||||||
|
var entity = await LoadForSaveAsync(definition, isNew, ct);
|
||||||
|
entity.Variables = MapVariables(definition.Variables);
|
||||||
|
SynchronizeLanguages(entity, definition.Languages);
|
||||||
|
Touch(entity);
|
||||||
|
definition.Id = entity.Id;
|
||||||
|
definition.Revision = entity.Revision;
|
||||||
|
}, ct);
|
||||||
|
|
||||||
|
private async Task<EmailTemplateDefinitionEntity> LoadForSaveAsync(EmailTemplateDefinition model, bool isNew, CancellationToken ct)
|
||||||
|
{
|
||||||
|
var entity = await FindDefinitionAsync(model.ServiceName, model.Key, ct);
|
||||||
|
if (isNew)
|
||||||
|
{
|
||||||
|
if (entity is not null)
|
||||||
|
throw new EmailTemplateWriteException("A template with this service and key already exists.");
|
||||||
|
return await CreateDefinitionAsync(model.ServiceName, model.Key, model.Variables, ct);
|
||||||
|
}
|
||||||
|
if (entity is null || entity.Id != model.Id || entity.Revision != model.Revision)
|
||||||
|
throw new EmailTemplateWriteException("This template changed after you opened it. Reload before saving; your entered values are retained.");
|
||||||
|
return entity;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void SynchronizeLanguages(EmailTemplateDefinitionEntity definition, IReadOnlyList<EmailTemplate> languages)
|
||||||
|
{
|
||||||
|
var codes = languages.Select(t => t.LanguageCode).ToHashSet(StringComparer.Ordinal);
|
||||||
|
db.Templates.RemoveRange(definition.Languages.Where(t => !codes.Contains(t.ServiceLanguage.Code)));
|
||||||
|
UpsertLanguages(definition, languages);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void UpsertLanguages(EmailTemplateDefinitionEntity definition, IReadOnlyList<EmailTemplate> languages)
|
||||||
|
{
|
||||||
|
foreach (var model in languages)
|
||||||
|
{
|
||||||
|
var code = model.LanguageCode;
|
||||||
|
var entity = definition.Languages.SingleOrDefault(t => t.ServiceLanguage.Code == code);
|
||||||
|
if (entity is null)
|
||||||
|
{
|
||||||
|
var language = NewLanguage(model, definition);
|
||||||
|
definition.Languages.Add(language);
|
||||||
|
db.Templates.Add(language);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
SetContent(entity, model);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<IReadOnlyList<EmailTemplateDefinition>> GetDefinitionsAsync(string? serviceName = null, string? key = null, CancellationToken ct = default)
|
||||||
|
{
|
||||||
|
var query = Definitions.AsNoTracking();
|
||||||
|
if (!string.IsNullOrWhiteSpace(serviceName))
|
||||||
|
query = query.Where(d => d.Service.Name == serviceName);
|
||||||
|
if (!string.IsNullOrWhiteSpace(key))
|
||||||
|
query = query.Where(d => d.Key == key);
|
||||||
|
var entities = await query.OrderBy(d => d.Service.Name).ThenBy(d => d.Key).ToListAsync(ct);
|
||||||
|
return entities.Select(MapDefinition).ToList();
|
||||||
|
}
|
||||||
|
|
||||||
|
private async Task RequireServiceLanguageAsync(string serviceName, string code, CancellationToken ct)
|
||||||
|
{
|
||||||
|
if (!await db.ServiceLanguages.AnyAsync(l => l.Service.Name == serviceName && l.Code == code, ct))
|
||||||
|
throw new EmailTemplateWriteException("Language is not in this service's catalog. Add it in the service editor first.", false);
|
||||||
|
}
|
||||||
|
|
||||||
|
private async Task ValidateServiceLanguagesAsync(EmailTemplateDefinition definition, CancellationToken ct)
|
||||||
|
{
|
||||||
|
var service = await db.Services.Include(s => s.Languages).SingleOrDefaultAsync(s => s.Name == definition.ServiceName
|
||||||
|
&& (definition.ServiceId == Guid.Empty || s.Id == definition.ServiceId), ct)
|
||||||
|
?? throw new EmailTemplateWriteException("Service does not exist. Create the service first.", false);
|
||||||
|
var codes = service.Languages.Select(l => l.Code).ToHashSet(StringComparer.Ordinal);
|
||||||
|
if (definition.Languages.Any(l => !codes.Contains(l.LanguageCode)))
|
||||||
|
throw new EmailTemplateWriteException("Language is not in this service's catalog. Add it in the service editor first.", false);
|
||||||
|
}
|
||||||
|
|
||||||
|
private Task<EmailTemplateDefinitionEntity?> FindDefinitionAsync(string serviceName, string key, CancellationToken ct) =>
|
||||||
|
Definitions.SingleOrDefaultAsync(d => d.Service.Name == serviceName && d.Key == key, ct);
|
||||||
|
|
||||||
|
private async Task<EmailTemplateDefinitionEntity> RequireDefinitionAsync(string serviceName, string key, CancellationToken ct) =>
|
||||||
|
await FindDefinitionAsync(serviceName, key, ct)
|
||||||
|
?? throw new EmailTemplateWriteException("The template was removed. Reload the template list.");
|
||||||
|
|
||||||
|
private async Task<EmailTemplateDefinitionEntity> CreateDefinitionAsync(string serviceName, string key,
|
||||||
|
IReadOnlyList<EmailTemplateVariable> variables, CancellationToken ct)
|
||||||
|
{
|
||||||
|
var service = await db.Services.Include(s => s.Languages).SingleOrDefaultAsync(s => s.Name == serviceName, ct)
|
||||||
|
?? throw new EmailTemplateWriteException("Service does not exist. Create the service and its language codes first.", false);
|
||||||
|
var entity = new EmailTemplateDefinitionEntity
|
||||||
|
{
|
||||||
|
Service = service, Key = key, Variables = MapVariables(variables), Created = DateTimeOffset.UtcNow
|
||||||
|
};
|
||||||
|
db.TemplateDefinitions.Add(entity);
|
||||||
|
return entity;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void ValidateLegacy(EmailTemplate template)
|
||||||
|
{
|
||||||
|
EmailTemplateValidation.ValidateIdentity(template.ServiceName, template.Key);
|
||||||
|
EmailTemplateValidation.ValidateLanguage(template);
|
||||||
|
EmailTemplateValidation.ValidateVariables(template.Variables);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void EnsureMatchingContract(EmailTemplateDefinitionEntity definition, IReadOnlyList<EmailTemplateVariable> variables)
|
||||||
|
{
|
||||||
|
var shared = definition.Variables.Select(v => new EmailTemplateVariable { Name = v.Name, Required = v.Required }).ToList();
|
||||||
|
if (!EmailTemplateValidation.ContractsEqual(shared, variables))
|
||||||
|
throw new EmailTemplateWriteException("Variables conflict with the shared template contract. Edit shared variables in the template editor.");
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void Touch(EmailTemplateDefinitionEntity definition)
|
||||||
|
{
|
||||||
|
definition.Revision = Guid.NewGuid();
|
||||||
|
definition.Updated = DateTimeOffset.UtcNow;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static List<EmailTemplateVariableData> MapVariables(IReadOnlyList<EmailTemplateVariable> variables) =>
|
||||||
|
variables.Select(v => new EmailTemplateVariableData { Name = v.Name, Required = v.Required }).ToList();
|
||||||
|
|
||||||
|
private static EmailTemplateEntity NewLanguage(EmailTemplate model, EmailTemplateDefinitionEntity definition) => new()
|
||||||
|
{
|
||||||
|
Id = model.Id, Definition = definition, ServiceId = definition.Service.Id,
|
||||||
|
ServiceLanguage = definition.Service.Languages.SingleOrDefault(l => l.Code == model.LanguageCode)
|
||||||
|
?? throw new EmailTemplateWriteException("Language is not in this service's catalog. Add it in the service editor first.", false),
|
||||||
|
Subject = model.Subject, HtmlBody = model.HtmlBody, TextBody = model.TextBody, Created = model.Created
|
||||||
};
|
};
|
||||||
|
|
||||||
private static EmailTemplateEntity MapToEntity(EmailTemplate t) => new()
|
private static void SetContent(EmailTemplateEntity entity, EmailTemplate model)
|
||||||
{
|
{
|
||||||
Id = t.Id,
|
entity.Subject = model.Subject;
|
||||||
ServiceName = t.ServiceName,
|
entity.HtmlBody = model.HtmlBody;
|
||||||
Key = t.Key,
|
entity.TextBody = model.TextBody;
|
||||||
LanguageCode = t.LanguageCode,
|
entity.Updated = DateTimeOffset.UtcNow;
|
||||||
Subject = t.Subject,
|
}
|
||||||
HtmlBody = t.HtmlBody,
|
|
||||||
TextBody = t.TextBody,
|
private static EmailTemplate MapLanguage(EmailTemplateEntity entity) => new()
|
||||||
Variables = t.Variables.Select(v => new EmailTemplateVariableData { Name = v.Name, Required = v.Required }).ToList(),
|
{
|
||||||
Created = t.Created,
|
Id = entity.Id, ServiceName = entity.Definition.Service.Name, Key = entity.Definition.Key,
|
||||||
Updated = t.Updated
|
LanguageCode = entity.ServiceLanguage.Code, Subject = entity.Subject, HtmlBody = entity.HtmlBody, TextBody = entity.TextBody,
|
||||||
|
Variables = entity.Definition.Variables.Select(v => new EmailTemplateVariable { Name = v.Name, Required = v.Required }).ToList(),
|
||||||
|
Created = entity.Created, Updated = entity.Updated
|
||||||
|
};
|
||||||
|
|
||||||
|
private static EmailTemplateDefinition MapDefinition(EmailTemplateDefinitionEntity entity) => new()
|
||||||
|
{
|
||||||
|
Id = entity.Id, ServiceId = entity.ServiceId, ServiceName = entity.Service.Name, Key = entity.Key, Revision = entity.Revision,
|
||||||
|
Variables = entity.Variables.Select(v => new EmailTemplateVariable { Name = v.Name, Required = v.Required }).ToList(),
|
||||||
|
Languages = entity.Languages.OrderBy(t => t.ServiceLanguage.Code).Select(MapLanguage).ToList(),
|
||||||
|
Created = entity.Created, Updated = entity.Updated
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,215 @@
|
|||||||
|
using HrynCo.NotificationService.DAL.Abstract.Repositories;
|
||||||
|
using HrynCo.NotificationService.DAL.Abstract.Templates;
|
||||||
|
using HrynCo.NotificationService.DAL.EF.Entities;
|
||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
|
||||||
|
namespace HrynCo.NotificationService.DAL.EF.Repositories;
|
||||||
|
|
||||||
|
internal sealed class NotificationServiceRepository(NotificationDbContext db, TemplateMutationRunner mutations) : INotificationServiceRepository
|
||||||
|
{
|
||||||
|
public async Task<IReadOnlyList<NotificationServiceInfo>> GetAllAsync(CancellationToken ct = default)
|
||||||
|
{
|
||||||
|
var services = await db.Services.AsNoTracking().Include(s => s.Languages).OrderBy(s => s.Name).ToListAsync(ct);
|
||||||
|
var counts = await VersionCountsAsync(ct);
|
||||||
|
return services.Select(s => Map(s, counts)).ToList();
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<NotificationServiceInfo?> GetAsync(Guid id, CancellationToken ct = default)
|
||||||
|
{
|
||||||
|
var service = await db.Services.AsNoTracking().Include(s => s.Languages).SingleOrDefaultAsync(s => s.Id == id, ct);
|
||||||
|
return service is null ? null : Map(service, await VersionCountsAsync(ct));
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<NotificationServiceInfo?> GetByNameAsync(string name, CancellationToken ct = default)
|
||||||
|
{
|
||||||
|
var service = await db.Services.AsNoTracking().Include(s => s.Languages).SingleOrDefaultAsync(s => s.Name == name, ct);
|
||||||
|
return service is null ? null : Map(service, await VersionCountsAsync(ct));
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<bool> DeleteAsync(Guid id, Guid revision, CancellationToken ct = default)
|
||||||
|
{
|
||||||
|
var removed = false;
|
||||||
|
await mutations.ExecuteAsync(async () =>
|
||||||
|
{
|
||||||
|
if (revision == Guid.Empty)
|
||||||
|
throw new EmailTemplateWriteException("A nonempty service revision is required.", false);
|
||||||
|
var service = await db.Services.Include(s => s.Languages).SingleOrDefaultAsync(s => s.Id == id, ct);
|
||||||
|
if (service is null)
|
||||||
|
return;
|
||||||
|
if (service.Revision != revision)
|
||||||
|
throw new EmailTemplateWriteException("The service revision changed. Read it again before deleting.");
|
||||||
|
if (await db.TemplateDefinitions.AnyAsync(d => d.ServiceId == id, ct))
|
||||||
|
throw new EmailTemplateWriteException("Remove every template definition before deleting this service, including definitions without translations.");
|
||||||
|
db.ServiceLanguages.RemoveRange(service.Languages);
|
||||||
|
db.Services.Remove(service);
|
||||||
|
removed = true;
|
||||||
|
}, ct);
|
||||||
|
return removed;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Task SaveAsync(NotificationServiceInfo model, bool isNew, CancellationToken ct = default) => mutations.ExecuteAsync(async () =>
|
||||||
|
{
|
||||||
|
Validate(model);
|
||||||
|
var service = await LoadForSaveAsync(model, isNew, ct);
|
||||||
|
await ValidateLanguageChangesAsync(service, model, ct);
|
||||||
|
await StageLanguageCodeChangesAsync(service, model.Languages, ct);
|
||||||
|
service.Name = model.Name;
|
||||||
|
SynchronizeLanguages(service, model.Languages);
|
||||||
|
Touch(service);
|
||||||
|
await TouchDefinitionsAsync(service.Id, ct);
|
||||||
|
model.Id = service.Id;
|
||||||
|
model.Revision = service.Revision;
|
||||||
|
}, ct);
|
||||||
|
|
||||||
|
public Task RemoveLanguageAsync(Guid serviceId, Guid languageId, int expectedVersionCount, Guid expectedRevision, CancellationToken ct = default) =>
|
||||||
|
mutations.ExecuteAsync(async () =>
|
||||||
|
{
|
||||||
|
var service = await db.Services.Include(s => s.Languages).SingleOrDefaultAsync(s => s.Id == serviceId, ct)
|
||||||
|
?? throw new EmailTemplateWriteException("Service no longer exists. Reload services.");
|
||||||
|
var language = service.Languages.SingleOrDefault(l => l.Id == languageId)
|
||||||
|
?? throw new EmailTemplateWriteException("Language no longer exists. Reload the service.");
|
||||||
|
var versions = await db.Templates.Where(t => t.ServiceLanguageId == languageId).ToListAsync(ct);
|
||||||
|
if (service.Revision != expectedRevision || versions.Count != expectedVersionCount)
|
||||||
|
throw new EmailTemplateWriteException("The service or affected version count changed. Review the current count and confirm again.");
|
||||||
|
db.Templates.RemoveRange(versions);
|
||||||
|
// Save dependent removals first because language deletion is restricted by its FK.
|
||||||
|
await db.SaveChangesAsync(ct);
|
||||||
|
db.ServiceLanguages.Remove(language);
|
||||||
|
Touch(service);
|
||||||
|
await TouchDefinitionsAsync(serviceId, ct);
|
||||||
|
}, ct);
|
||||||
|
|
||||||
|
private async Task<ServiceEntity> LoadForSaveAsync(NotificationServiceInfo model, bool isNew, CancellationToken ct)
|
||||||
|
{
|
||||||
|
if (await db.Services.AnyAsync(s => s.Name == model.Name && s.Id != model.Id, ct))
|
||||||
|
throw new EmailTemplateWriteException("A service with this name already exists.");
|
||||||
|
if (isNew)
|
||||||
|
{
|
||||||
|
var created = new ServiceEntity { Name = model.Name, Created = DateTimeOffset.UtcNow };
|
||||||
|
db.Services.Add(created);
|
||||||
|
return created;
|
||||||
|
}
|
||||||
|
var service = await db.Services.Include(s => s.Languages).SingleOrDefaultAsync(s => s.Id == model.Id, ct);
|
||||||
|
if (service is null || service.Revision != model.Revision)
|
||||||
|
throw new EmailTemplateWriteException("This service changed after you opened it. Reload before saving.");
|
||||||
|
return service;
|
||||||
|
}
|
||||||
|
|
||||||
|
private async Task ValidateLanguageChangesAsync(ServiceEntity service, NotificationServiceInfo model, CancellationToken ct)
|
||||||
|
{
|
||||||
|
var ids = service.Languages.Select(l => l.Id).ToHashSet();
|
||||||
|
if (model.Languages.Any(l => l.Id != Guid.Empty && !ids.Contains(l.Id)))
|
||||||
|
throw new EmailTemplateWriteException("A language changed or was removed. Reload the service.");
|
||||||
|
ValidateChangedCodes(service, model.Languages);
|
||||||
|
var retained = model.Languages.Select(l => l.Id).ToHashSet();
|
||||||
|
var removed = service.Languages.Where(l => !retained.Contains(l.Id)).ToList();
|
||||||
|
foreach (var language in removed)
|
||||||
|
{
|
||||||
|
var count = await db.Templates.CountAsync(t => t.ServiceLanguageId == language.Id, ct);
|
||||||
|
if (count > 0)
|
||||||
|
throw new EmailTemplateWriteException($"Cannot remove language '{language.Code}': {count} version(s) use it. Use the separate delete-with-versions confirmation.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private async Task StageLanguageCodeChangesAsync(ServiceEntity service, IReadOnlyList<ServiceLanguageInfo> languages, CancellationToken ct)
|
||||||
|
{
|
||||||
|
var requested = languages.Where(l => l.Id != Guid.Empty).ToDictionary(l => l.Id, l => l.Code);
|
||||||
|
var removed = service.Languages.Where(l => !requested.ContainsKey(l.Id)).ToList();
|
||||||
|
var changed = service.Languages.Where(l => requested.TryGetValue(l.Id, out var code) && code != l.Code).ToList();
|
||||||
|
if (removed.Count == 0 && changed.Count == 0)
|
||||||
|
return;
|
||||||
|
foreach (var language in removed)
|
||||||
|
{
|
||||||
|
db.ServiceLanguages.Remove(language);
|
||||||
|
service.Languages.Remove(language);
|
||||||
|
}
|
||||||
|
var reserved = service.Languages.Select(l => l.Code).Concat(languages.Select(l => l.Code)).ToHashSet(StringComparer.Ordinal);
|
||||||
|
foreach (var language in changed)
|
||||||
|
language.Code = TemporaryCode(reserved);
|
||||||
|
// Free old codes before final assignments, supporting swaps and replacing unused rows.
|
||||||
|
// These temporary identifiers remain invisible outside this transaction.
|
||||||
|
await db.SaveChangesAsync(ct);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static string TemporaryCode(HashSet<string> reserved)
|
||||||
|
{
|
||||||
|
string code;
|
||||||
|
do { code = "~" + Guid.NewGuid().ToString("N")[..9]; }
|
||||||
|
while (!reserved.Add(code));
|
||||||
|
return code;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void SynchronizeLanguages(ServiceEntity service, IReadOnlyList<ServiceLanguageInfo> languages)
|
||||||
|
{
|
||||||
|
var retained = languages.Select(l => l.Id).ToHashSet();
|
||||||
|
db.ServiceLanguages.RemoveRange(service.Languages.Where(l => !retained.Contains(l.Id)));
|
||||||
|
foreach (var model in languages)
|
||||||
|
{
|
||||||
|
var entity = service.Languages.SingleOrDefault(l => l.Id == model.Id);
|
||||||
|
if (entity is null)
|
||||||
|
db.ServiceLanguages.Add(new ServiceLanguageEntity { Service = service, Code = model.Code, Created = DateTimeOffset.UtcNow });
|
||||||
|
else
|
||||||
|
entity.Code = model.Code;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private async Task TouchDefinitionsAsync(Guid serviceId, CancellationToken ct)
|
||||||
|
{
|
||||||
|
var definitions = await db.TemplateDefinitions.Where(d => d.ServiceId == serviceId).ToListAsync(ct);
|
||||||
|
foreach (var definition in definitions)
|
||||||
|
{
|
||||||
|
definition.Revision = Guid.NewGuid();
|
||||||
|
definition.Updated = DateTimeOffset.UtcNow;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private Task<Dictionary<Guid, int>> VersionCountsAsync(CancellationToken ct) => db.Templates.AsNoTracking()
|
||||||
|
.GroupBy(t => t.ServiceLanguageId).Select(g => new { Id = g.Key, Count = g.Count() })
|
||||||
|
.ToDictionaryAsync(g => g.Id, g => g.Count, ct);
|
||||||
|
|
||||||
|
private static void Validate(NotificationServiceInfo service)
|
||||||
|
{
|
||||||
|
if (string.IsNullOrWhiteSpace(service.Name) || service.Name.Length > 100)
|
||||||
|
throw new EmailTemplateWriteException("Service name is required and must be at most 100 characters.", false);
|
||||||
|
foreach (var language in service.Languages)
|
||||||
|
ValidateCode(language.Code);
|
||||||
|
if (service.Languages.Select(l => l.Code).Distinct(StringComparer.Ordinal).Count() != service.Languages.Count)
|
||||||
|
throw new EmailTemplateWriteException("Language codes must be unique within the service.", false);
|
||||||
|
var ids = service.Languages.Where(l => l.Id != Guid.Empty).Select(l => l.Id).ToList();
|
||||||
|
if (ids.Distinct().Count() != ids.Count)
|
||||||
|
throw new EmailTemplateWriteException("Each service language can appear only once.", false);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void ValidateCode(string code)
|
||||||
|
{
|
||||||
|
if (string.IsNullOrWhiteSpace(code) || code.Length > 10)
|
||||||
|
throw new EmailTemplateWriteException("Language codes are required and must be at most 10 characters.", false);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void ValidateChangedCodes(ServiceEntity service, IReadOnlyList<ServiceLanguageInfo> languages)
|
||||||
|
{
|
||||||
|
var persisted = service.Languages.ToDictionary(l => l.Id, l => l.Code);
|
||||||
|
foreach (var language in languages)
|
||||||
|
{
|
||||||
|
if (persisted.TryGetValue(language.Id, out var original) && original == language.Code)
|
||||||
|
continue;
|
||||||
|
if (!System.Text.RegularExpressions.Regex.IsMatch(language.Code, "^[A-Za-z0-9]+(-[A-Za-z0-9]+)*$"))
|
||||||
|
throw new EmailTemplateWriteException("Use letters, numbers and hyphens in new or renamed language codes, without spaces. Codes are case-sensitive.", false);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void Touch(ServiceEntity service)
|
||||||
|
{
|
||||||
|
service.Revision = Guid.NewGuid();
|
||||||
|
service.Updated = DateTimeOffset.UtcNow;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static NotificationServiceInfo Map(ServiceEntity service, IReadOnlyDictionary<Guid, int> counts) => new()
|
||||||
|
{
|
||||||
|
Id = service.Id, Name = service.Name, Revision = service.Revision,
|
||||||
|
Languages = service.Languages.OrderBy(l => l.Code).Select(l => new ServiceLanguageInfo
|
||||||
|
{
|
||||||
|
Id = l.Id, Code = l.Code, VersionCount = counts.GetValueOrDefault(l.Id)
|
||||||
|
}).ToList()
|
||||||
|
};
|
||||||
|
}
|
||||||
@@ -16,7 +16,10 @@ public static class ServiceCollectionExtensions
|
|||||||
services.AddDbContext<NotificationDbContext>(options =>
|
services.AddDbContext<NotificationDbContext>(options =>
|
||||||
options.UseNpgsql(connectionString));
|
options.UseNpgsql(connectionString));
|
||||||
|
|
||||||
|
services.AddScoped<TemplateMutationRunner>();
|
||||||
|
services.AddScoped<INotificationServiceRepository, NotificationServiceRepository>();
|
||||||
services.AddScoped<IEmailTemplateRepository, EmailTemplateRepository>();
|
services.AddScoped<IEmailTemplateRepository, EmailTemplateRepository>();
|
||||||
|
services.AddScoped<IEmailTemplateDefinitionRepository, EmailTemplateRepository>();
|
||||||
services.AddScoped<IEmailChannelRepository, EmailChannelRepository>();
|
services.AddScoped<IEmailChannelRepository, EmailChannelRepository>();
|
||||||
services.AddScoped<IEmailChannelUsageRepository, EmailChannelUsageRepository>();
|
services.AddScoped<IEmailChannelUsageRepository, EmailChannelUsageRepository>();
|
||||||
services.AddScoped<IUnitOfWork, NotificationUnitOfWork>();
|
services.AddScoped<IUnitOfWork, NotificationUnitOfWork>();
|
||||||
|
|||||||
@@ -0,0 +1,19 @@
|
|||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
|
||||||
|
namespace HrynCo.NotificationService.DAL.EF;
|
||||||
|
|
||||||
|
internal sealed class TemplateMutationRunner(NotificationDbContext db)
|
||||||
|
{
|
||||||
|
public async Task ExecuteAsync(Func<Task> write, CancellationToken ct)
|
||||||
|
{
|
||||||
|
await using var transaction = db.Database.CurrentTransaction is null
|
||||||
|
? await db.Database.BeginTransactionAsync(ct) : null;
|
||||||
|
// Service/language administration and template writes share the same transaction lock.
|
||||||
|
// Counts, catalog membership and revision checks are therefore valid until commit.
|
||||||
|
await db.Database.ExecuteSqlRawAsync("SELECT pg_advisory_xact_lock(1601)", ct);
|
||||||
|
await write();
|
||||||
|
await db.SaveChangesAsync(ct);
|
||||||
|
if (transaction is not null)
|
||||||
|
await transaction.CommitAsync(ct);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,132 @@
|
|||||||
|
namespace HrynCo.NotificationService.Migrator;
|
||||||
|
|
||||||
|
using HrynCo.NotificationService.DAL.Abstract.Providers;
|
||||||
|
using HrynCo.NotificationService.DAL.Abstract.Repositories;
|
||||||
|
using HrynCo.NotificationService.DAL.Abstract.Templates;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
|
||||||
|
internal sealed class DevelopmentDataSeeder
|
||||||
|
{
|
||||||
|
internal const string DefaultServiceName = "TestService";
|
||||||
|
internal const string DefaultLanguageCode = "en";
|
||||||
|
|
||||||
|
private readonly IEmailChannelRepository _channels;
|
||||||
|
private readonly IEmailTemplateRepository _templates;
|
||||||
|
private readonly INotificationServiceRepository _services;
|
||||||
|
private readonly ILogger<DevelopmentDataSeeder> _logger;
|
||||||
|
|
||||||
|
public DevelopmentDataSeeder(
|
||||||
|
IEmailChannelRepository channels,
|
||||||
|
IEmailTemplateRepository templates,
|
||||||
|
INotificationServiceRepository services,
|
||||||
|
ILogger<DevelopmentDataSeeder> logger)
|
||||||
|
{
|
||||||
|
_channels = channels;
|
||||||
|
_templates = templates;
|
||||||
|
_services = services;
|
||||||
|
_logger = logger;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task SeedAsync(string serviceName, CancellationToken cancellationToken = default)
|
||||||
|
{
|
||||||
|
if (string.IsNullOrWhiteSpace(serviceName))
|
||||||
|
throw new InvalidOperationException("Development seed service name is not configured.");
|
||||||
|
|
||||||
|
await SeedMailpitChannelAsync(serviceName, cancellationToken);
|
||||||
|
await SeedServiceAsync(serviceName, cancellationToken);
|
||||||
|
|
||||||
|
foreach (EmailTemplate template in CreateTemplates(serviceName))
|
||||||
|
{
|
||||||
|
EmailTemplate? existing = await _templates.GetAsync(
|
||||||
|
template.ServiceName, template.Key, template.LanguageCode, cancellationToken);
|
||||||
|
|
||||||
|
if (existing is not null)
|
||||||
|
{
|
||||||
|
_logger.LogInformation(
|
||||||
|
"Development email template already exists; leaving it unchanged [ServiceName={ServiceName}, Key={TemplateKey}, LanguageCode={LanguageCode}]",
|
||||||
|
template.ServiceName, template.Key, template.LanguageCode);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
await _templates.AddAsync(template, cancellationToken);
|
||||||
|
_logger.LogInformation(
|
||||||
|
"Created development email template [ServiceName={ServiceName}, Key={TemplateKey}, LanguageCode={LanguageCode}]",
|
||||||
|
template.ServiceName, template.Key, template.LanguageCode);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private async Task SeedServiceAsync(string serviceName, CancellationToken ct)
|
||||||
|
{
|
||||||
|
var services = await _services.GetAllAsync(ct);
|
||||||
|
var service = services.SingleOrDefault(s => s.Name == serviceName);
|
||||||
|
if (service?.Languages.Any(l => l.Code == DefaultLanguageCode) == true)
|
||||||
|
return;
|
||||||
|
var isNew = service is null;
|
||||||
|
service ??= new NotificationServiceInfo { Name = serviceName };
|
||||||
|
service.Languages = [..service.Languages, new ServiceLanguageInfo { Code = DefaultLanguageCode }];
|
||||||
|
await _services.SaveAsync(service, isNew, ct);
|
||||||
|
}
|
||||||
|
|
||||||
|
private async Task SeedMailpitChannelAsync(string serviceName, CancellationToken cancellationToken)
|
||||||
|
{
|
||||||
|
IReadOnlyList<EmailChannel> existingChannels = await _channels.GetByServiceAsync(
|
||||||
|
serviceName, cancellationToken);
|
||||||
|
|
||||||
|
if (existingChannels.Count > 0)
|
||||||
|
{
|
||||||
|
_logger.LogInformation(
|
||||||
|
"Development email channel already exists; leaving all channels unchanged [ServiceName={ServiceName}]",
|
||||||
|
serviceName);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
var channel = new EmailChannel
|
||||||
|
{
|
||||||
|
ServiceName = serviceName,
|
||||||
|
Priority = 1,
|
||||||
|
EmailChannelType = EmailChannelType.Smtp,
|
||||||
|
Settings = new SmtpChannelSettings
|
||||||
|
{
|
||||||
|
Host = "mailpit",
|
||||||
|
Port = 1025,
|
||||||
|
Username = string.Empty,
|
||||||
|
Password = string.Empty,
|
||||||
|
UseSsl = false,
|
||||||
|
FromEmail = "no-reply@local.hrynco.test",
|
||||||
|
FromName = "HrynCo Development"
|
||||||
|
},
|
||||||
|
WarnThresholdPercent = 90,
|
||||||
|
IsActive = true,
|
||||||
|
Created = DateTimeOffset.UtcNow
|
||||||
|
};
|
||||||
|
|
||||||
|
await _channels.AddAsync(channel, cancellationToken);
|
||||||
|
_logger.LogInformation(
|
||||||
|
"Created Mailpit development email channel [ServiceName={ServiceName}]",
|
||||||
|
serviceName);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static IReadOnlyList<EmailTemplate> CreateTemplates(string serviceName)
|
||||||
|
{
|
||||||
|
DateTimeOffset created = DateTimeOffset.UtcNow;
|
||||||
|
|
||||||
|
return
|
||||||
|
[
|
||||||
|
new EmailTemplate
|
||||||
|
{
|
||||||
|
ServiceName = serviceName,
|
||||||
|
Key = "TestEmail",
|
||||||
|
LanguageCode = DefaultLanguageCode,
|
||||||
|
Subject = "Test notification",
|
||||||
|
HtmlBody = "<p>Hello {{RecipientName}},</p><p>{{Message}}</p>",
|
||||||
|
TextBody = "Hello {{RecipientName}}, {{Message}}",
|
||||||
|
Variables =
|
||||||
|
[
|
||||||
|
new EmailTemplateVariable { Name = "RecipientName", Required = true },
|
||||||
|
new EmailTemplateVariable { Name = "Message", Required = true }
|
||||||
|
],
|
||||||
|
Created = created
|
||||||
|
}
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,5 +1,7 @@
|
|||||||
using HrynCo.NotificationService.DAL.EF;
|
using HrynCo.NotificationService.DAL.EF;
|
||||||
|
using HrynCo.NotificationService.Migrator;
|
||||||
using Microsoft.EntityFrameworkCore;
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
using Microsoft.Extensions.Configuration;
|
||||||
using Microsoft.Extensions.DependencyInjection;
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
using Microsoft.Extensions.Hosting;
|
using Microsoft.Extensions.Hosting;
|
||||||
using Serilog;
|
using Serilog;
|
||||||
@@ -21,8 +23,8 @@ try
|
|||||||
var connectionString = ctx.Configuration["App:ConnectionString"]
|
var connectionString = ctx.Configuration["App:ConnectionString"]
|
||||||
?? throw new InvalidOperationException("App:ConnectionString is not configured.");
|
?? throw new InvalidOperationException("App:ConnectionString is not configured.");
|
||||||
|
|
||||||
services.AddDbContext<NotificationDbContext>(options =>
|
services.AddNotificationDataAccess(connectionString);
|
||||||
options.UseNpgsql(connectionString));
|
services.AddScoped<DevelopmentDataSeeder>();
|
||||||
})
|
})
|
||||||
.Build();
|
.Build();
|
||||||
|
|
||||||
@@ -32,6 +34,18 @@ try
|
|||||||
Log.Information("Applying migrations...");
|
Log.Information("Applying migrations...");
|
||||||
await db.Database.MigrateAsync();
|
await db.Database.MigrateAsync();
|
||||||
Log.Information("Migrations applied successfully.");
|
Log.Information("Migrations applied successfully.");
|
||||||
|
|
||||||
|
var environment = scope.ServiceProvider.GetRequiredService<IHostEnvironment>();
|
||||||
|
if (environment.IsDevelopment())
|
||||||
|
{
|
||||||
|
string serviceName = host.Services.GetRequiredService<IConfiguration>()["DevelopmentSeed:ServiceName"]
|
||||||
|
?? DevelopmentDataSeeder.DefaultServiceName;
|
||||||
|
|
||||||
|
Log.Information("Seeding development email configuration...");
|
||||||
|
var seeder = scope.ServiceProvider.GetRequiredService<DevelopmentDataSeeder>();
|
||||||
|
await seeder.SeedAsync(serviceName);
|
||||||
|
Log.Information("Development email configuration is ready.");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -0,0 +1,3 @@
|
|||||||
|
using System.Runtime.CompilerServices;
|
||||||
|
|
||||||
|
[assembly: InternalsVisibleTo("HrynCo.NotificationService.Services.Tests")]
|
||||||
@@ -0,0 +1,85 @@
|
|||||||
|
namespace HrynCo.NotificationService.Services.Tests;
|
||||||
|
|
||||||
|
using HrynCo.NotificationService.DAL.Abstract.Providers;
|
||||||
|
using HrynCo.NotificationService.DAL.Abstract.Repositories;
|
||||||
|
using HrynCo.NotificationService.DAL.Abstract.Templates;
|
||||||
|
using HrynCo.NotificationService.Migrator;
|
||||||
|
using Microsoft.Extensions.Logging.Abstractions;
|
||||||
|
using NSubstitute;
|
||||||
|
|
||||||
|
public sealed class DevelopmentDataSeederTests
|
||||||
|
{
|
||||||
|
private readonly IEmailChannelRepository _channels = Substitute.For<IEmailChannelRepository>();
|
||||||
|
private readonly IEmailTemplateRepository _templates = Substitute.For<IEmailTemplateRepository>();
|
||||||
|
private readonly INotificationServiceRepository _services = Substitute.For<INotificationServiceRepository>();
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task SeedAsync_WhenConfigurationIsMissing_CreatesMailpitChannelAndTestTemplate()
|
||||||
|
{
|
||||||
|
_channels.GetByServiceAsync("TestService", Arg.Any<CancellationToken>())
|
||||||
|
.Returns(Array.Empty<EmailChannel>());
|
||||||
|
_templates.GetAsync("TestService", "TestEmail", "en", Arg.Any<CancellationToken>())
|
||||||
|
.Returns((EmailTemplate?)null);
|
||||||
|
|
||||||
|
DevelopmentDataSeeder seeder = CreateSeeder();
|
||||||
|
|
||||||
|
await seeder.SeedAsync("TestService", CancellationToken.None);
|
||||||
|
|
||||||
|
await _channels.Received(1).AddAsync(
|
||||||
|
Arg.Is<EmailChannel>(channel => IsMailpitChannel(channel)),
|
||||||
|
Arg.Any<CancellationToken>());
|
||||||
|
await _templates.Received(1).AddAsync(
|
||||||
|
Arg.Is<EmailTemplate>(template =>
|
||||||
|
template.ServiceName == "TestService" &&
|
||||||
|
template.Key == "TestEmail" &&
|
||||||
|
template.Variables.Any(variable => variable.Name == "Message" && variable.Required)),
|
||||||
|
Arg.Any<CancellationToken>());
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task SeedAsync_WhenConfigurationExists_DoesNotDuplicateOrOverwriteIt()
|
||||||
|
{
|
||||||
|
_channels.GetByServiceAsync("TestService", Arg.Any<CancellationToken>())
|
||||||
|
.Returns([new EmailChannel
|
||||||
|
{
|
||||||
|
ServiceName = "TestService",
|
||||||
|
EmailChannelType = EmailChannelType.Smtp,
|
||||||
|
Settings = new SmtpChannelSettings()
|
||||||
|
}]);
|
||||||
|
_templates.GetAsync("TestService", "TestEmail", "en", Arg.Any<CancellationToken>())
|
||||||
|
.Returns(new EmailTemplate
|
||||||
|
{
|
||||||
|
ServiceName = "TestService",
|
||||||
|
Key = "TestEmail",
|
||||||
|
LanguageCode = "en",
|
||||||
|
Subject = "Existing",
|
||||||
|
HtmlBody = "Existing",
|
||||||
|
TextBody = "Existing"
|
||||||
|
});
|
||||||
|
|
||||||
|
DevelopmentDataSeeder seeder = CreateSeeder();
|
||||||
|
|
||||||
|
await seeder.SeedAsync("TestService", CancellationToken.None);
|
||||||
|
|
||||||
|
await _channels.DidNotReceive().AddAsync(Arg.Any<EmailChannel>(), Arg.Any<CancellationToken>());
|
||||||
|
await _templates.DidNotReceive().AddAsync(Arg.Any<EmailTemplate>(), Arg.Any<CancellationToken>());
|
||||||
|
await _channels.DidNotReceive().UpdateAsync(Arg.Any<EmailChannel>(), Arg.Any<CancellationToken>());
|
||||||
|
await _templates.DidNotReceive().UpdateAsync(Arg.Any<EmailTemplate>(), Arg.Any<CancellationToken>());
|
||||||
|
}
|
||||||
|
|
||||||
|
private DevelopmentDataSeeder CreateSeeder() => new(
|
||||||
|
_channels,
|
||||||
|
_templates,
|
||||||
|
_services,
|
||||||
|
NullLogger<DevelopmentDataSeeder>.Instance);
|
||||||
|
|
||||||
|
private static bool IsMailpitChannel(EmailChannel channel)
|
||||||
|
{
|
||||||
|
return channel.ServiceName == "TestService" &&
|
||||||
|
channel.IsActive &&
|
||||||
|
channel.Settings is SmtpChannelSettings smtp &&
|
||||||
|
smtp.Host == "mailpit" &&
|
||||||
|
smtp.Port == 1025 &&
|
||||||
|
!smtp.UseSsl;
|
||||||
|
}
|
||||||
|
}
|
||||||
+88
@@ -0,0 +1,88 @@
|
|||||||
|
namespace HrynCo.NotificationService.Services.Tests.EmailProcessing;
|
||||||
|
|
||||||
|
using HrynCo.NotificationService.Contracts.Messages;
|
||||||
|
using HrynCo.NotificationService.DAL.Abstract.Templates;
|
||||||
|
using HrynCo.NotificationService.Worker.Services.EmailProcessing;
|
||||||
|
|
||||||
|
public sealed class EmailTemplateRenderingServiceTests
|
||||||
|
{
|
||||||
|
private readonly EmailTemplateRenderingService _service = new();
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void Render_InterpolatesItemTrackerVariables()
|
||||||
|
{
|
||||||
|
EmailTemplate template = CreateTemplate();
|
||||||
|
SendEmailMessageData data = CreateData(new Dictionary<string, string>
|
||||||
|
{
|
||||||
|
["AppName"] = "StoreMate",
|
||||||
|
["VerificationUrl"] = "https://example.invalid/verify"
|
||||||
|
});
|
||||||
|
|
||||||
|
RenderedEmail result = _service.Render(template, data);
|
||||||
|
|
||||||
|
Assert.Equal("Verify StoreMate", result.Subject);
|
||||||
|
Assert.Equal("<a href=\"https://example.invalid/verify\">Verify</a>", result.HtmlBody);
|
||||||
|
Assert.Equal("Verify at https://example.invalid/verify", result.TextBody);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void Render_WhenRequiredVariableIsMissing_ThrowsObservableFailure()
|
||||||
|
{
|
||||||
|
EmailTemplate template = CreateTemplate();
|
||||||
|
SendEmailMessageData data = CreateData(new Dictionary<string, string>
|
||||||
|
{
|
||||||
|
["AppName"] = "StoreMate"
|
||||||
|
});
|
||||||
|
|
||||||
|
InvalidDataException exception = Assert.Throws<InvalidDataException>(
|
||||||
|
() => _service.Render(template, data));
|
||||||
|
|
||||||
|
Assert.Equal("Required template variables are missing: VerificationUrl.", exception.Message);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void Render_HtmlEncodesVariablesWithoutChangingSubjectOrTextBody()
|
||||||
|
{
|
||||||
|
EmailTemplate template = CreateTemplate();
|
||||||
|
SendEmailMessageData data = CreateData(new Dictionary<string, string>
|
||||||
|
{
|
||||||
|
["AppName"] = "Invemory <script>alert('xss')</script>",
|
||||||
|
["VerificationUrl"] = "https://example.invalid/verify?next=\" onclick=\"alert('xss')"
|
||||||
|
});
|
||||||
|
|
||||||
|
RenderedEmail result = _service.Render(template, data);
|
||||||
|
|
||||||
|
Assert.Equal("Verify Invemory <script>alert('xss')</script>", result.Subject);
|
||||||
|
Assert.Equal(
|
||||||
|
"<a href=\"https://example.invalid/verify?next=" onclick="alert('xss')\">Verify</a>",
|
||||||
|
result.HtmlBody);
|
||||||
|
Assert.Equal(
|
||||||
|
"Verify at https://example.invalid/verify?next=\" onclick=\"alert('xss')",
|
||||||
|
result.TextBody);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static EmailTemplate CreateTemplate() => new()
|
||||||
|
{
|
||||||
|
ServiceName = "StoreMate-Prod",
|
||||||
|
Key = "EmailVerification",
|
||||||
|
LanguageCode = "uk",
|
||||||
|
Subject = "Verify {{AppName}}",
|
||||||
|
HtmlBody = "<a href=\"{{VerificationUrl}}\">Verify</a>",
|
||||||
|
TextBody = "Verify at {{VerificationUrl}}",
|
||||||
|
Variables =
|
||||||
|
[
|
||||||
|
new EmailTemplateVariable { Name = "AppName", Required = true },
|
||||||
|
new EmailTemplateVariable { Name = "VerificationUrl", Required = true }
|
||||||
|
]
|
||||||
|
};
|
||||||
|
|
||||||
|
private static SendEmailMessageData CreateData(IReadOnlyDictionary<string, string> variables) => new()
|
||||||
|
{
|
||||||
|
ServiceName = "StoreMate-Prod",
|
||||||
|
TemplateKey = "EmailVerification",
|
||||||
|
RecipientEmail = "owner@example.com",
|
||||||
|
RecipientName = "Owner",
|
||||||
|
LanguageCode = "uk",
|
||||||
|
Variables = variables
|
||||||
|
};
|
||||||
|
}
|
||||||
+97
@@ -0,0 +1,97 @@
|
|||||||
|
namespace HrynCo.NotificationService.Services.Tests.EmailProcessing;
|
||||||
|
|
||||||
|
using HrynCo.NotificationService.DAL.Abstract.Repositories;
|
||||||
|
using HrynCo.NotificationService.DAL.Abstract.Templates;
|
||||||
|
using HrynCo.NotificationService.Worker.Services.EmailProcessing;
|
||||||
|
using NSubstitute;
|
||||||
|
|
||||||
|
public sealed class EmailTemplateServiceTests
|
||||||
|
{
|
||||||
|
[Theory]
|
||||||
|
[InlineData("RU")]
|
||||||
|
[InlineData(" ru ")]
|
||||||
|
[InlineData(" RU ")]
|
||||||
|
public async Task GetAsync_WhenExactVersionIsMissing_RetriesLegacyTrimmedLowercaseCode(string requestedCode)
|
||||||
|
{
|
||||||
|
IEmailTemplateRepository repository = Substitute.For<IEmailTemplateRepository>();
|
||||||
|
var expected = Template("ru");
|
||||||
|
repository.GetAsync("TestService", "Welcome", "ru", Arg.Any<CancellationToken>()).Returns(expected);
|
||||||
|
var service = new EmailTemplateService(repository);
|
||||||
|
|
||||||
|
Assert.Same(expected, await service.GetAsync("TestService", "Welcome", requestedCode, default));
|
||||||
|
Assert.Equal(new[] { requestedCode, "ru" }, RequestedCodes(repository));
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task GetAsync_WhenBothExactAndLowercaseVersionsExist_PrefersExactVersion()
|
||||||
|
{
|
||||||
|
IEmailTemplateRepository repository = Substitute.For<IEmailTemplateRepository>();
|
||||||
|
var expected = Template("RU");
|
||||||
|
repository.GetAsync("TestService", "Welcome", "RU", Arg.Any<CancellationToken>()).Returns(expected);
|
||||||
|
repository.GetAsync("TestService", "Welcome", "ru", Arg.Any<CancellationToken>()).Returns(Template("ru"));
|
||||||
|
var service = new EmailTemplateService(repository);
|
||||||
|
|
||||||
|
Assert.Same(expected, await service.GetAsync("TestService", "Welcome", "RU", default));
|
||||||
|
Assert.Equal(new[] { "RU" }, RequestedCodes(repository));
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task GetAsync_WhenExactRegionalVersionExists_DoesNotNormalizeIt()
|
||||||
|
{
|
||||||
|
IEmailTemplateRepository repository = Substitute.For<IEmailTemplateRepository>();
|
||||||
|
var expected = Template("ru-RU");
|
||||||
|
repository.GetAsync("TestService", "Welcome", "ru-RU", Arg.Any<CancellationToken>()).Returns(expected);
|
||||||
|
var service = new EmailTemplateService(repository);
|
||||||
|
|
||||||
|
Assert.Same(expected, await service.GetAsync("TestService", "Welcome", "ru-RU", default));
|
||||||
|
Assert.Equal(new[] { "ru-RU" }, RequestedCodes(repository));
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task GetAsync_WhenMissingCodeIsAlreadyCanonical_QueriesOnlyOnce()
|
||||||
|
{
|
||||||
|
IEmailTemplateRepository repository = Substitute.For<IEmailTemplateRepository>();
|
||||||
|
var service = new EmailTemplateService(repository);
|
||||||
|
|
||||||
|
var error = await Assert.ThrowsAsync<InvalidOperationException>(() => service.GetAsync("TestService", "Welcome", "zz", default));
|
||||||
|
|
||||||
|
Assert.Contains("language='zz'", error.Message);
|
||||||
|
Assert.Equal(new[] { "zz" }, RequestedCodes(repository));
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task GetAsync_WhenRegionalLookupMisses_DoesNotScanOtherCaseOrRegionalVariants()
|
||||||
|
{
|
||||||
|
IEmailTemplateRepository repository = Substitute.For<IEmailTemplateRepository>();
|
||||||
|
repository.GetAsync("TestService", "Welcome", "ru-RU", Arg.Any<CancellationToken>()).Returns(Template("ru-RU"));
|
||||||
|
repository.GetAsync("TestService", "Welcome", "ru", Arg.Any<CancellationToken>()).Returns(Template("ru"));
|
||||||
|
var service = new EmailTemplateService(repository);
|
||||||
|
|
||||||
|
await Assert.ThrowsAsync<InvalidOperationException>(() => service.GetAsync("TestService", "Welcome", "RU-ru", default));
|
||||||
|
|
||||||
|
Assert.Equal(new[] { "RU-ru", "ru-ru" }, RequestedCodes(repository));
|
||||||
|
}
|
||||||
|
|
||||||
|
[Theory]
|
||||||
|
[InlineData(null)]
|
||||||
|
[InlineData("")]
|
||||||
|
[InlineData(" ")]
|
||||||
|
public async Task GetAsync_WhenCodeIsBlank_RejectsBeforeRepositoryLookup(string? code)
|
||||||
|
{
|
||||||
|
IEmailTemplateRepository repository = Substitute.For<IEmailTemplateRepository>();
|
||||||
|
var service = new EmailTemplateService(repository);
|
||||||
|
|
||||||
|
await Assert.ThrowsAsync<InvalidDataException>(() => service.GetAsync("TestService", "Welcome", code, default));
|
||||||
|
|
||||||
|
Assert.Empty(repository.ReceivedCalls());
|
||||||
|
}
|
||||||
|
|
||||||
|
private static EmailTemplate Template(string code) => new()
|
||||||
|
{
|
||||||
|
ServiceName = "TestService", Key = "Welcome", LanguageCode = code, Subject = "Subject " + code,
|
||||||
|
HtmlBody = "<p>Hello</p>", TextBody = "Hello"
|
||||||
|
};
|
||||||
|
|
||||||
|
private static string[] RequestedCodes(IEmailTemplateRepository repository) => repository.ReceivedCalls()
|
||||||
|
.Select(call => (string)call.GetArguments()[2]!).ToArray();
|
||||||
|
}
|
||||||
+58
@@ -0,0 +1,58 @@
|
|||||||
|
namespace HrynCo.NotificationService.Services.Tests.EmailProcessing;
|
||||||
|
|
||||||
|
using HrynCo.NotificationService.Worker.Services.EmailProcessing;
|
||||||
|
using System.Net.Mail;
|
||||||
|
using System.Net.Sockets;
|
||||||
|
|
||||||
|
public sealed class NotificationDeliveryErrorFormatterTests
|
||||||
|
{
|
||||||
|
[Fact]
|
||||||
|
public void Format_UsesInnermostMessageAndRemovesLineBreaks()
|
||||||
|
{
|
||||||
|
var exception = new InvalidOperationException(
|
||||||
|
"outer",
|
||||||
|
new Exception("provider failed\r\nretry rejected"));
|
||||||
|
|
||||||
|
string result = NotificationDeliveryErrorFormatter.Format(exception);
|
||||||
|
|
||||||
|
Assert.Equal(
|
||||||
|
"Notification delivery failed after all retry attempts (provider failed retry rejected).",
|
||||||
|
result);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void Format_UnresolvedSmtpHost_AddsSafeChannelContext()
|
||||||
|
{
|
||||||
|
var socketException = new SocketException((int)SocketError.HostNotFound);
|
||||||
|
var exception = new SmtpException("Failure sending mail.", socketException);
|
||||||
|
|
||||||
|
string result = NotificationDeliveryErrorFormatter.Format(exception);
|
||||||
|
|
||||||
|
Assert.Equal(
|
||||||
|
$"SMTP delivery failed after all retry attempts: the configured SMTP server host could not be resolved ({socketException.Message}).",
|
||||||
|
result);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void Format_RefusedSmtpConnection_AddsSafeChannelContext()
|
||||||
|
{
|
||||||
|
var socketException = new SocketException((int)SocketError.ConnectionRefused);
|
||||||
|
var exception = new SmtpException("Failure sending mail.", socketException);
|
||||||
|
|
||||||
|
string result = NotificationDeliveryErrorFormatter.Format(exception);
|
||||||
|
|
||||||
|
Assert.Equal(
|
||||||
|
$"SMTP delivery failed after all retry attempts: the configured SMTP server refused the connection ({socketException.Message}).",
|
||||||
|
result);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void Format_LongError_IsBoundedToOutboxColumnLength()
|
||||||
|
{
|
||||||
|
var exception = new Exception(new string('x', 2100));
|
||||||
|
|
||||||
|
string result = NotificationDeliveryErrorFormatter.Format(exception);
|
||||||
|
|
||||||
|
Assert.Equal(2000, result.Length);
|
||||||
|
}
|
||||||
|
}
|
||||||
+107
@@ -0,0 +1,107 @@
|
|||||||
|
namespace HrynCo.NotificationService.Services.Tests.EmailProcessing;
|
||||||
|
|
||||||
|
using HrynCo.NotificationService.Contracts.Messages;
|
||||||
|
using HrynCo.NotificationService.Worker.Services.EmailProcessing;
|
||||||
|
using Hrynco.RabbitMq;
|
||||||
|
using Microsoft.Extensions.Logging.Abstractions;
|
||||||
|
using NSubstitute;
|
||||||
|
|
||||||
|
public sealed class NotificationResultPublisherTests
|
||||||
|
{
|
||||||
|
[Fact]
|
||||||
|
public async Task PublishAsync_TerminalFailure_PublishesNeutralFailureResultToReplyQueue()
|
||||||
|
{
|
||||||
|
IRabbitMqPublisher rabbitMqPublisher = Substitute.For<IRabbitMqPublisher>();
|
||||||
|
var publisher = new NotificationResultPublisher(
|
||||||
|
rabbitMqPublisher,
|
||||||
|
NullLogger<NotificationResultPublisher>.Instance);
|
||||||
|
SendEmailMessage message = CreateMessage();
|
||||||
|
NotificationResultMessage? publishedResult = null;
|
||||||
|
rabbitMqPublisher
|
||||||
|
.When(x => x.PublishAsync(
|
||||||
|
"item-tracker.notifications.result",
|
||||||
|
Arg.Any<NotificationResultMessage>(),
|
||||||
|
Arg.Any<CancellationToken>()))
|
||||||
|
.Do(call => publishedResult = call.ArgAt<NotificationResultMessage>(1));
|
||||||
|
|
||||||
|
await publisher.PublishAsync(
|
||||||
|
message,
|
||||||
|
"Email provider could not deliver the notification.",
|
||||||
|
CancellationToken.None);
|
||||||
|
|
||||||
|
await rabbitMqPublisher.Received(1).PublishAsync(
|
||||||
|
"item-tracker.notifications.result",
|
||||||
|
Arg.Any<NotificationResultMessage>(),
|
||||||
|
CancellationToken.None);
|
||||||
|
Assert.NotNull(publishedResult);
|
||||||
|
Assert.Equal(message.CorrelationContext.CorrelationId, publishedResult.CorrelationContext.CorrelationId);
|
||||||
|
Assert.Null(publishedResult.CorrelationContext.ReplyTo);
|
||||||
|
Assert.Equal(message.Data.ServiceName, publishedResult.Data.ServiceName);
|
||||||
|
Assert.Equal(message.Data.TemplateKey, publishedResult.Data.TemplateKey);
|
||||||
|
Assert.Equal(message.Data.RecipientEmail, publishedResult.Data.RecipientEmail);
|
||||||
|
Assert.Equal("Email provider could not deliver the notification.", publishedResult.Data.ErrorMessage);
|
||||||
|
Assert.False(publishedResult.Data.IsSuccess);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task PublishAsync_NoReplyQueue_DoesNotPublish()
|
||||||
|
{
|
||||||
|
IRabbitMqPublisher rabbitMqPublisher = Substitute.For<IRabbitMqPublisher>();
|
||||||
|
var publisher = new NotificationResultPublisher(
|
||||||
|
rabbitMqPublisher,
|
||||||
|
NullLogger<NotificationResultPublisher>.Instance);
|
||||||
|
SendEmailMessage message = CreateMessage();
|
||||||
|
message.CorrelationContext = message.CorrelationContext with { ReplyTo = null };
|
||||||
|
|
||||||
|
await publisher.PublishAsync(message, "delivery failed", CancellationToken.None);
|
||||||
|
|
||||||
|
await rabbitMqPublisher.DidNotReceive()
|
||||||
|
.PublishAsync<NotificationResultData>(
|
||||||
|
Arg.Any<string>(),
|
||||||
|
Arg.Any<NotificationResultMessage>(),
|
||||||
|
Arg.Any<CancellationToken>());
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task PublishAsync_ResultBrokerFailure_IsBestEffort()
|
||||||
|
{
|
||||||
|
IRabbitMqPublisher rabbitMqPublisher = Substitute.For<IRabbitMqPublisher>();
|
||||||
|
rabbitMqPublisher
|
||||||
|
.PublishAsync(
|
||||||
|
Arg.Any<string>(),
|
||||||
|
Arg.Any<NotificationResultMessage>(),
|
||||||
|
Arg.Any<CancellationToken>())
|
||||||
|
.Returns<Task>(_ => throw new InvalidOperationException("reply broker unavailable"));
|
||||||
|
var publisher = new NotificationResultPublisher(
|
||||||
|
rabbitMqPublisher,
|
||||||
|
NullLogger<NotificationResultPublisher>.Instance);
|
||||||
|
|
||||||
|
Exception? exception = await Record.ExceptionAsync(() => publisher.PublishAsync(
|
||||||
|
CreateMessage(),
|
||||||
|
"delivery failed",
|
||||||
|
CancellationToken.None));
|
||||||
|
|
||||||
|
Assert.Null(exception);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static SendEmailMessage CreateMessage()
|
||||||
|
{
|
||||||
|
return new SendEmailMessage
|
||||||
|
{
|
||||||
|
CorrelationContext = new CorrelationContext
|
||||||
|
{
|
||||||
|
CorrelationId = "correlation-id",
|
||||||
|
ReplyTo = "item-tracker.notifications.result"
|
||||||
|
},
|
||||||
|
Data = new SendEmailMessageData
|
||||||
|
{
|
||||||
|
ServiceName = "TestService",
|
||||||
|
TemplateKey = "TestEmail",
|
||||||
|
RecipientEmail = "test.user@itemtracker.local",
|
||||||
|
RecipientName = "Test User",
|
||||||
|
LanguageCode = "en",
|
||||||
|
Variables = new Dictionary<string, string>()
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
+16
@@ -0,0 +1,16 @@
|
|||||||
|
namespace HrynCo.NotificationService.Services.Tests.EmailProcessing;
|
||||||
|
|
||||||
|
using HrynCo.NotificationService.Worker.Services.EmailProcessing;
|
||||||
|
|
||||||
|
public sealed class RecipientAddressRedactorTests
|
||||||
|
{
|
||||||
|
[Theory]
|
||||||
|
[InlineData("owner@example.com", "o***@e***.com")]
|
||||||
|
[InlineData("a@b.test", "a***@b***.test")]
|
||||||
|
[InlineData("invalid", "***")]
|
||||||
|
[InlineData(null, "<missing>")]
|
||||||
|
public void Redact_DoesNotExposeFullAddress(string? address, string expected)
|
||||||
|
{
|
||||||
|
Assert.Equal(expected, RecipientAddressRedactor.Redact(address));
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,60 @@
|
|||||||
|
namespace HrynCo.NotificationService.Services.Tests.EmailProcessing;
|
||||||
|
|
||||||
|
using System.Text.Json;
|
||||||
|
using HrynCo.NotificationService.Contracts.Messages;
|
||||||
|
using HrynCo.NotificationService.Worker.Services.EmailProcessing;
|
||||||
|
|
||||||
|
public sealed class SendEmailContractTests
|
||||||
|
{
|
||||||
|
private const string ItemTrackerPayload = """
|
||||||
|
{
|
||||||
|
"CorrelationContext": {
|
||||||
|
"CorrelationId": "3ec4ad3f-0a8a-49f3-b31d-cdad5d1b95cc",
|
||||||
|
"ReplyTo": "item-tracker.notifications.result"
|
||||||
|
},
|
||||||
|
"Data": {
|
||||||
|
"ServiceName": "StoreMate-Prod",
|
||||||
|
"TemplateKey": "EmailVerification",
|
||||||
|
"RecipientEmail": "owner@example.com",
|
||||||
|
"RecipientName": "StoreMate Owner",
|
||||||
|
"Variables": {
|
||||||
|
"AppName": "StoreMate",
|
||||||
|
"VerificationUrl": "https://example.invalid/sensitive-token"
|
||||||
|
},
|
||||||
|
"LanguageCode": "uk"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
""";
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void PascalCaseItemTrackerPayload_DeserializesWithoutLosingValues()
|
||||||
|
{
|
||||||
|
SendEmailMessage? message = JsonSerializer.Deserialize<SendEmailMessage>(ItemTrackerPayload);
|
||||||
|
|
||||||
|
Assert.NotNull(message);
|
||||||
|
SendEmailMessageValidator.Validate(message);
|
||||||
|
Assert.Equal("3ec4ad3f-0a8a-49f3-b31d-cdad5d1b95cc", message.CorrelationContext.CorrelationId);
|
||||||
|
Assert.Equal("item-tracker.notifications.result", message.CorrelationContext.ReplyTo);
|
||||||
|
Assert.Equal("StoreMate-Prod", message.Data.ServiceName);
|
||||||
|
Assert.Equal("EmailVerification", message.Data.TemplateKey);
|
||||||
|
Assert.Equal("owner@example.com", message.Data.RecipientEmail);
|
||||||
|
Assert.Equal("StoreMate Owner", message.Data.RecipientName);
|
||||||
|
Assert.Equal("uk", message.Data.LanguageCode);
|
||||||
|
Assert.Equal("StoreMate", message.Data.Variables["AppName"]);
|
||||||
|
Assert.Equal(
|
||||||
|
"https://example.invalid/sensitive-token",
|
||||||
|
message.Data.Variables["VerificationUrl"]);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void MissingLanguageCode_IsRejected()
|
||||||
|
{
|
||||||
|
SendEmailMessage? message = JsonSerializer.Deserialize<SendEmailMessage>(
|
||||||
|
ItemTrackerPayload.Replace("\"uk\"", "null", StringComparison.Ordinal));
|
||||||
|
|
||||||
|
InvalidDataException exception = Assert.Throws<InvalidDataException>(
|
||||||
|
() => SendEmailMessageValidator.Validate(message!));
|
||||||
|
|
||||||
|
Assert.Equal("LanguageCode is required.", exception.Message);
|
||||||
|
}
|
||||||
|
}
|
||||||
+77
@@ -0,0 +1,77 @@
|
|||||||
|
namespace HrynCo.NotificationService.Services.Tests.EmailProcessing;
|
||||||
|
|
||||||
|
using HrynCo.NotificationService.Contracts.Messages;
|
||||||
|
using HrynCo.NotificationService.Worker;
|
||||||
|
using Hrynco.RabbitMq;
|
||||||
|
|
||||||
|
public sealed class SendEmailDeliveryValidatorTests
|
||||||
|
{
|
||||||
|
[Fact]
|
||||||
|
public void ValidItemTrackerDelivery_IsAccepted()
|
||||||
|
{
|
||||||
|
string? error = SendEmailDeliveryValidator.GetValidationError(
|
||||||
|
CreateMessage(),
|
||||||
|
CreateContext(),
|
||||||
|
SendEmailConsumer.SupportedMessageType);
|
||||||
|
|
||||||
|
Assert.Null(error);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void MissingMessageId_IsRejected()
|
||||||
|
{
|
||||||
|
RabbitMqMessageContext context = CreateContext() with { MessageId = null };
|
||||||
|
|
||||||
|
string? error = SendEmailDeliveryValidator.GetValidationError(
|
||||||
|
CreateMessage(),
|
||||||
|
context,
|
||||||
|
SendEmailConsumer.SupportedMessageType);
|
||||||
|
|
||||||
|
Assert.Equal("AMQP MessageId is required.", error);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void UnsupportedContractVersion_IsRejected()
|
||||||
|
{
|
||||||
|
RabbitMqMessageContext context = CreateContext() with
|
||||||
|
{
|
||||||
|
MessageType = "Notification.SendEmail.v2"
|
||||||
|
};
|
||||||
|
|
||||||
|
string? error = SendEmailDeliveryValidator.GetValidationError(
|
||||||
|
CreateMessage(),
|
||||||
|
context,
|
||||||
|
SendEmailConsumer.SupportedMessageType);
|
||||||
|
|
||||||
|
Assert.Equal("Unsupported AMQP message type 'Notification.SendEmail.v2'.", error);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static RabbitMqMessageContext CreateContext() => new()
|
||||||
|
{
|
||||||
|
QueueName = SendEmailConsumer.IncomingQueue,
|
||||||
|
Exchange = string.Empty,
|
||||||
|
RoutingKey = SendEmailConsumer.IncomingQueue,
|
||||||
|
MessageId = Guid.NewGuid().ToString("D"),
|
||||||
|
MessageType = SendEmailConsumer.SupportedMessageType,
|
||||||
|
CorrelationId = "correlation-id",
|
||||||
|
ContentType = "application/json"
|
||||||
|
};
|
||||||
|
|
||||||
|
private static SendEmailMessage CreateMessage() => new()
|
||||||
|
{
|
||||||
|
CorrelationContext = new CorrelationContext
|
||||||
|
{
|
||||||
|
CorrelationId = "correlation-id",
|
||||||
|
ReplyTo = "item-tracker.notifications.result"
|
||||||
|
},
|
||||||
|
Data = new SendEmailMessageData
|
||||||
|
{
|
||||||
|
ServiceName = "StoreMate-Prod",
|
||||||
|
TemplateKey = "EmailVerification",
|
||||||
|
RecipientEmail = "owner@example.com",
|
||||||
|
RecipientName = "Owner",
|
||||||
|
LanguageCode = "uk",
|
||||||
|
Variables = new Dictionary<string, string> { ["AppName"] = "StoreMate" }
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
@@ -0,0 +1,123 @@
|
|||||||
|
namespace HrynCo.NotificationService.Services.Tests.EmailProcessing;
|
||||||
|
|
||||||
|
using HrynCo.NotificationService.Contracts.Messages;
|
||||||
|
using HrynCo.NotificationService.DAL.Abstract.Providers;
|
||||||
|
using HrynCo.NotificationService.DAL.Abstract.Repositories;
|
||||||
|
using HrynCo.NotificationService.DAL.Abstract.Templates;
|
||||||
|
using HrynCo.NotificationService.Worker.Services.EmailProcessing;
|
||||||
|
using Hrynco.RabbitMq;
|
||||||
|
using Microsoft.Extensions.Logging.Abstractions;
|
||||||
|
using NSubstitute;
|
||||||
|
|
||||||
|
public sealed class SendEmailServiceTests
|
||||||
|
{
|
||||||
|
[Fact]
|
||||||
|
public async Task ProcessAsync_ValidItemTrackerMessage_SendsOnceAndUpdatesUsage()
|
||||||
|
{
|
||||||
|
Guid channelId = Guid.NewGuid();
|
||||||
|
var settings = new SmtpChannelSettings
|
||||||
|
{
|
||||||
|
Host = "smtp.example.invalid",
|
||||||
|
Port = 587,
|
||||||
|
Username = "smtp-user",
|
||||||
|
Password = "not-a-real-secret",
|
||||||
|
UseSsl = true,
|
||||||
|
FromEmail = "notifications@example.com",
|
||||||
|
FromName = "StoreMate"
|
||||||
|
};
|
||||||
|
var channel = new EmailChannel
|
||||||
|
{
|
||||||
|
Id = channelId,
|
||||||
|
ServiceName = "StoreMate-Prod",
|
||||||
|
Priority = 1,
|
||||||
|
EmailChannelType = EmailChannelType.Smtp,
|
||||||
|
Settings = settings,
|
||||||
|
IsActive = true
|
||||||
|
};
|
||||||
|
var template = new EmailTemplate
|
||||||
|
{
|
||||||
|
ServiceName = "StoreMate-Prod",
|
||||||
|
Key = "EmailVerification",
|
||||||
|
LanguageCode = "uk",
|
||||||
|
Subject = "Verify {{AppName}}",
|
||||||
|
HtmlBody = "<p>{{AppName}}</p>",
|
||||||
|
TextBody = "{{AppName}}",
|
||||||
|
Variables = [new EmailTemplateVariable { Name = "AppName", Required = true }]
|
||||||
|
};
|
||||||
|
|
||||||
|
IEmailChannelRepository channels = Substitute.For<IEmailChannelRepository>();
|
||||||
|
channels.GetByServiceAsync("StoreMate-Prod", Arg.Any<CancellationToken>())
|
||||||
|
.Returns([channel]);
|
||||||
|
IEmailChannelUsageRepository usage = Substitute.For<IEmailChannelUsageRepository>();
|
||||||
|
IEmailTemplateService templates = Substitute.For<IEmailTemplateService>();
|
||||||
|
templates.GetAsync("StoreMate-Prod", "EmailVerification", "uk", Arg.Any<CancellationToken>())
|
||||||
|
.Returns(template);
|
||||||
|
var renderer = new EmailTemplateRenderingService();
|
||||||
|
var smtp = new RecordingSmtpEmailSender();
|
||||||
|
INotificationResultPublisher resultPublisher = Substitute.For<INotificationResultPublisher>();
|
||||||
|
var service = new SendEmailService(
|
||||||
|
channels,
|
||||||
|
usage,
|
||||||
|
templates,
|
||||||
|
renderer,
|
||||||
|
smtp,
|
||||||
|
resultPublisher,
|
||||||
|
NullLogger<SendEmailService>.Instance);
|
||||||
|
var message = new SendEmailMessage
|
||||||
|
{
|
||||||
|
CorrelationContext = new CorrelationContext
|
||||||
|
{
|
||||||
|
CorrelationId = Guid.NewGuid().ToString("D")
|
||||||
|
},
|
||||||
|
Data = new SendEmailMessageData
|
||||||
|
{
|
||||||
|
ServiceName = "StoreMate-Prod",
|
||||||
|
TemplateKey = "EmailVerification",
|
||||||
|
RecipientEmail = "owner@example.com",
|
||||||
|
RecipientName = "Owner",
|
||||||
|
LanguageCode = "uk",
|
||||||
|
Variables = new Dictionary<string, string> { ["AppName"] = "StoreMate" }
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
await service.ProcessAsync(message, CancellationToken.None);
|
||||||
|
|
||||||
|
Assert.Equal(1, smtp.SendCount);
|
||||||
|
Assert.Same(settings, smtp.Settings);
|
||||||
|
Assert.Equal("Verify StoreMate", smtp.Email?.Subject);
|
||||||
|
Assert.Equal("owner@example.com", smtp.RecipientEmail);
|
||||||
|
Assert.Equal("Owner", smtp.RecipientName);
|
||||||
|
await usage.Received(1).IncrementUsageAsync(
|
||||||
|
channelId,
|
||||||
|
Arg.Any<DateOnly>(),
|
||||||
|
CancellationToken.None);
|
||||||
|
await resultPublisher.Received(1).PublishAsync(
|
||||||
|
message,
|
||||||
|
null,
|
||||||
|
CancellationToken.None);
|
||||||
|
}
|
||||||
|
|
||||||
|
private sealed class RecordingSmtpEmailSender : ISmtpEmailSender
|
||||||
|
{
|
||||||
|
public int SendCount { get; private set; }
|
||||||
|
public SmtpChannelSettings? Settings { get; private set; }
|
||||||
|
public RenderedEmail? Email { get; private set; }
|
||||||
|
public string? RecipientEmail { get; private set; }
|
||||||
|
public string? RecipientName { get; private set; }
|
||||||
|
|
||||||
|
public Task SendAsync(
|
||||||
|
SmtpChannelSettings settings,
|
||||||
|
RenderedEmail email,
|
||||||
|
string recipientEmail,
|
||||||
|
string recipientName,
|
||||||
|
CancellationToken cancellationToken)
|
||||||
|
{
|
||||||
|
SendCount++;
|
||||||
|
Settings = settings;
|
||||||
|
Email = email;
|
||||||
|
RecipientEmail = recipientEmail;
|
||||||
|
RecipientName = recipientName;
|
||||||
|
return Task.CompletedTask;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
+5
@@ -10,6 +10,7 @@
|
|||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="coverlet.collector" />
|
<PackageReference Include="coverlet.collector" />
|
||||||
<PackageReference Include="Microsoft.NET.Test.Sdk" />
|
<PackageReference Include="Microsoft.NET.Test.Sdk" />
|
||||||
|
<PackageReference Include="NSubstitute" />
|
||||||
<PackageReference Include="xunit" />
|
<PackageReference Include="xunit" />
|
||||||
<PackageReference Include="xunit.runner.visualstudio" />
|
<PackageReference Include="xunit.runner.visualstudio" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
@@ -21,6 +22,10 @@
|
|||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ProjectReference Include="..\HrynCo.NotificationService.Services\HrynCo.NotificationService.Services.csproj" />
|
<ProjectReference Include="..\HrynCo.NotificationService.Services\HrynCo.NotificationService.Services.csproj" />
|
||||||
<ProjectReference Include="..\HrynCo.NotificationService.DAL.Abstract\HrynCo.NotificationService.DAL.Abstract.csproj" />
|
<ProjectReference Include="..\HrynCo.NotificationService.DAL.Abstract\HrynCo.NotificationService.DAL.Abstract.csproj" />
|
||||||
|
<ProjectReference Include="..\HrynCo.NotificationService.Contracts\HrynCo.NotificationService.Contracts.csproj" />
|
||||||
|
<ProjectReference Include="..\HrynCo.NotificationService.Worker.Services\HrynCo.NotificationService.Worker.Services.csproj" />
|
||||||
|
<ProjectReference Include="..\HrynCo.NotificationService.Worker\HrynCo.NotificationService.Worker.csproj" />
|
||||||
|
<ProjectReference Include="..\HrynCo.NotificationService.Migrator\HrynCo.NotificationService.Migrator.csproj" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
</Project>
|
</Project>
|
||||||
@@ -1,10 +0,0 @@
|
|||||||
namespace HrynCo.NotificationService.Services.Tests;
|
|
||||||
|
|
||||||
public class UnitTest1
|
|
||||||
{
|
|
||||||
[Fact]
|
|
||||||
public void Test1()
|
|
||||||
{
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,5 +1,6 @@
|
|||||||
using System.Net;
|
using System.Net;
|
||||||
using System.Net.Mail;
|
using System.Net.Mail;
|
||||||
|
using System.Text;
|
||||||
using HrynCo.NotificationService.DAL.Abstract.Providers;
|
using HrynCo.NotificationService.DAL.Abstract.Providers;
|
||||||
using HrynCo.NotificationService.DAL.Abstract.Repositories;
|
using HrynCo.NotificationService.DAL.Abstract.Repositories;
|
||||||
using HrynCo.NotificationService.Services.Core;
|
using HrynCo.NotificationService.Services.Core;
|
||||||
@@ -48,14 +49,16 @@ internal sealed class SendEmailHandler
|
|||||||
{
|
{
|
||||||
From = new MailAddress(smtp.FromEmail, smtp.FromName),
|
From = new MailAddress(smtp.FromEmail, smtp.FromName),
|
||||||
Subject = request.Subject,
|
Subject = request.Subject,
|
||||||
Body = request.HtmlBody,
|
Body = request.TextBody ?? string.Empty,
|
||||||
IsBodyHtml = true
|
IsBodyHtml = false,
|
||||||
|
BodyEncoding = Encoding.UTF8,
|
||||||
|
SubjectEncoding = Encoding.UTF8
|
||||||
};
|
};
|
||||||
|
|
||||||
if (!string.IsNullOrWhiteSpace(request.TextBody))
|
if (!string.IsNullOrWhiteSpace(request.HtmlBody))
|
||||||
{
|
{
|
||||||
var plain = AlternateView.CreateAlternateViewFromString(request.TextBody, null, "text/plain");
|
var html = AlternateView.CreateAlternateViewFromString(request.HtmlBody, Encoding.UTF8, "text/html");
|
||||||
mail.AlternateViews.Add(plain);
|
mail.AlternateViews.Add(html);
|
||||||
}
|
}
|
||||||
|
|
||||||
mail.To.Add(new MailAddress(request.RecipientEmail, request.RecipientName));
|
mail.To.Add(new MailAddress(request.RecipientEmail, request.RecipientName));
|
||||||
|
|||||||
+7
@@ -39,7 +39,14 @@ internal sealed class CreateEmailTemplateHandler
|
|||||||
Variables = request.Variables
|
Variables = request.Variables
|
||||||
};
|
};
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
await _templates.AddAsync(template, cancellationToken);
|
await _templates.AddAsync(template, cancellationToken);
|
||||||
|
}
|
||||||
|
catch (EmailTemplateWriteException error)
|
||||||
|
{
|
||||||
|
return Failure<Guid>(error.Message, error.IsConflict ? ServiceErrorCode.Conflict : ServiceErrorCode.InvalidRequest);
|
||||||
|
}
|
||||||
|
|
||||||
return Success(template.Id);
|
return Success(template.Id);
|
||||||
}
|
}
|
||||||
|
|||||||
+2
-1
@@ -7,5 +7,6 @@ namespace HrynCo.NotificationService.Services.EmailTemplates.Delete;
|
|||||||
public sealed record DeleteEmailTemplateCommand(
|
public sealed record DeleteEmailTemplateCommand(
|
||||||
string ServiceName,
|
string ServiceName,
|
||||||
string Key,
|
string Key,
|
||||||
string LanguageCode
|
string LanguageCode,
|
||||||
|
Guid? ExpectedVersionId = null
|
||||||
) : IRequest<ServiceResult<Unit>>;
|
) : IRequest<ServiceResult<Unit>>;
|
||||||
|
|||||||
+10
@@ -1,3 +1,4 @@
|
|||||||
|
using HrynCo.NotificationService.DAL.Abstract.Templates;
|
||||||
using HrynCo.DAL.Abstract;
|
using HrynCo.DAL.Abstract;
|
||||||
using HrynCo.NotificationService.DAL.Abstract.Repositories;
|
using HrynCo.NotificationService.DAL.Abstract.Repositories;
|
||||||
using HrynCo.NotificationService.Services.Core;
|
using HrynCo.NotificationService.Services.Core;
|
||||||
@@ -27,8 +28,17 @@ internal sealed class DeleteEmailTemplateHandler
|
|||||||
|
|
||||||
if (template is null)
|
if (template is null)
|
||||||
return Failure<Unit>("Template not found.", ServiceErrorCode.NotFound);
|
return Failure<Unit>("Template not found.", ServiceErrorCode.NotFound);
|
||||||
|
if (request.ExpectedVersionId is Guid expectedId && template.Id != expectedId)
|
||||||
|
return Failure<Unit>("Template changed after you opened it. Reload before deleting.", ServiceErrorCode.Conflict);
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
await _templates.DeleteAsync(template, cancellationToken);
|
await _templates.DeleteAsync(template, cancellationToken);
|
||||||
|
}
|
||||||
|
catch (EmailTemplateWriteException error)
|
||||||
|
{
|
||||||
|
return Failure<Unit>(error.Message, error.IsConflict ? ServiceErrorCode.Conflict : ServiceErrorCode.InvalidRequest);
|
||||||
|
}
|
||||||
|
|
||||||
return Success(Unit.Value);
|
return Success(Unit.Value);
|
||||||
}
|
}
|
||||||
|
|||||||
+1
-1
@@ -22,7 +22,7 @@ internal sealed class GetAllEmailTemplatesHandler
|
|||||||
protected override async Task<ServiceResult<IReadOnlyList<EmailTemplate>>> DoOnHandle(
|
protected override async Task<ServiceResult<IReadOnlyList<EmailTemplate>>> DoOnHandle(
|
||||||
GetAllEmailTemplatesQuery request, CancellationToken cancellationToken)
|
GetAllEmailTemplatesQuery request, CancellationToken cancellationToken)
|
||||||
{
|
{
|
||||||
var templates = await _templates.GetAllAsync(cancellationToken);
|
var templates = await _templates.GetAllAsync(request.ServiceName, request.Key, cancellationToken);
|
||||||
return Success(templates);
|
return Success(templates);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+2
-1
@@ -4,4 +4,5 @@ using HrynCo.NotificationService.Services.Core;
|
|||||||
|
|
||||||
namespace HrynCo.NotificationService.Services.EmailTemplates.GetAll;
|
namespace HrynCo.NotificationService.Services.EmailTemplates.GetAll;
|
||||||
|
|
||||||
public sealed record GetAllEmailTemplatesQuery : IRequest<ServiceResult<IReadOnlyList<EmailTemplate>>>;
|
public sealed record GetAllEmailTemplatesQuery(string? ServiceName = null, string? Key = null)
|
||||||
|
: IRequest<ServiceResult<IReadOnlyList<EmailTemplate>>>;
|
||||||
|
|||||||
+8
@@ -0,0 +1,8 @@
|
|||||||
|
using HrynCo.NotificationService.DAL.Abstract.Templates;
|
||||||
|
using HrynCo.NotificationService.Services.Core;
|
||||||
|
using MediatR;
|
||||||
|
|
||||||
|
namespace HrynCo.NotificationService.Services.EmailTemplates.SaveDefinition;
|
||||||
|
|
||||||
|
public sealed record SaveEmailTemplateDefinitionCommand(EmailTemplateDefinition Definition, bool IsNew)
|
||||||
|
: IRequest<ServiceResult<Guid>>;
|
||||||
+26
@@ -0,0 +1,26 @@
|
|||||||
|
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.SaveDefinition;
|
||||||
|
|
||||||
|
internal sealed class SaveEmailTemplateDefinitionHandler(
|
||||||
|
IContextualSerilogLogger<SaveEmailTemplateDefinitionCommand> logger,
|
||||||
|
IEmailTemplateDefinitionRepository definitions)
|
||||||
|
: RequestHandler<SaveEmailTemplateDefinitionCommand, ServiceResult<Guid>>(logger)
|
||||||
|
{
|
||||||
|
protected override async Task<ServiceResult<Guid>> DoOnHandle(SaveEmailTemplateDefinitionCommand request, CancellationToken cancellationToken)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
await definitions.SaveDefinitionAsync(request.Definition, request.IsNew, cancellationToken);
|
||||||
|
return Success(request.Definition.Id);
|
||||||
|
}
|
||||||
|
catch (EmailTemplateWriteException error)
|
||||||
|
{
|
||||||
|
return Failure<Guid>(error.Message, error.IsConflict ? ServiceErrorCode.Conflict : ServiceErrorCode.InvalidRequest);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
+8
@@ -1,3 +1,4 @@
|
|||||||
|
using HrynCo.NotificationService.DAL.Abstract.Templates;
|
||||||
using HrynCo.DAL.Abstract;
|
using HrynCo.DAL.Abstract;
|
||||||
using HrynCo.NotificationService.DAL.Abstract.Repositories;
|
using HrynCo.NotificationService.DAL.Abstract.Repositories;
|
||||||
using HrynCo.NotificationService.Services.Core;
|
using HrynCo.NotificationService.Services.Core;
|
||||||
@@ -34,7 +35,14 @@ internal sealed class UpdateEmailTemplateHandler
|
|||||||
template.Variables = request.Variables;
|
template.Variables = request.Variables;
|
||||||
template.Updated = DateTimeOffset.UtcNow;
|
template.Updated = DateTimeOffset.UtcNow;
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
await _templates.UpdateAsync(template, cancellationToken);
|
await _templates.UpdateAsync(template, cancellationToken);
|
||||||
|
}
|
||||||
|
catch (EmailTemplateWriteException error)
|
||||||
|
{
|
||||||
|
return Failure<Unit>(error.Message, error.IsConflict ? ServiceErrorCode.Conflict : ServiceErrorCode.InvalidRequest);
|
||||||
|
}
|
||||||
|
|
||||||
return Success(Unit.Value);
|
return Success(Unit.Value);
|
||||||
}
|
}
|
||||||
|
|||||||
+7
@@ -0,0 +1,7 @@
|
|||||||
|
using HrynCo.NotificationService.Services.Core;
|
||||||
|
using MediatR;
|
||||||
|
using Unit = HrynCo.NotificationService.Services.Core.Unit;
|
||||||
|
|
||||||
|
namespace HrynCo.NotificationService.Services.TemplateDefinitions;
|
||||||
|
|
||||||
|
public sealed record DeleteEmailTemplateDefinitionCommand(Guid ServiceId, string Key, Guid Revision) : IRequest<ServiceResult<Unit>>;
|
||||||
+25
@@ -0,0 +1,25 @@
|
|||||||
|
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.TemplateDefinitions;
|
||||||
|
|
||||||
|
internal sealed class DeleteEmailTemplateDefinitionHandler(
|
||||||
|
IContextualSerilogLogger<DeleteEmailTemplateDefinitionCommand> logger, IEmailTemplateDefinitionRepository repository)
|
||||||
|
: RequestHandler<DeleteEmailTemplateDefinitionCommand, ServiceResult<Unit>>(logger)
|
||||||
|
{
|
||||||
|
protected override async Task<ServiceResult<Unit>> DoOnHandle(DeleteEmailTemplateDefinitionCommand request, CancellationToken cancellationToken)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var removed = await repository.DeleteDefinitionAsync(request.ServiceId, request.Key, request.Revision, cancellationToken);
|
||||||
|
return removed ? Success(Unit.Value) : Failure<Unit>("Template definition not found.", ServiceErrorCode.NotFound);
|
||||||
|
}
|
||||||
|
catch (EmailTemplateWriteException error)
|
||||||
|
{
|
||||||
|
return Failure<Unit>(error.Message, error.IsConflict ? ServiceErrorCode.Conflict : ServiceErrorCode.InvalidRequest);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
+7
@@ -0,0 +1,7 @@
|
|||||||
|
using HrynCo.NotificationService.Services.Core;
|
||||||
|
using MediatR;
|
||||||
|
using Unit = HrynCo.NotificationService.Services.Core.Unit;
|
||||||
|
|
||||||
|
namespace HrynCo.NotificationService.Services.TemplateDefinitions;
|
||||||
|
|
||||||
|
public sealed record DeleteNotificationServiceCommand(Guid ServiceId, Guid Revision) : IRequest<ServiceResult<Unit>>;
|
||||||
+25
@@ -0,0 +1,25 @@
|
|||||||
|
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.TemplateDefinitions;
|
||||||
|
|
||||||
|
internal sealed class DeleteNotificationServiceHandler(
|
||||||
|
IContextualSerilogLogger<DeleteNotificationServiceCommand> logger, INotificationServiceRepository repository)
|
||||||
|
: RequestHandler<DeleteNotificationServiceCommand, ServiceResult<Unit>>(logger)
|
||||||
|
{
|
||||||
|
protected override async Task<ServiceResult<Unit>> DoOnHandle(DeleteNotificationServiceCommand request, CancellationToken cancellationToken)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var removed = await repository.DeleteAsync(request.ServiceId, request.Revision, cancellationToken);
|
||||||
|
return removed ? Success(Unit.Value) : Failure<Unit>("Service not found.", ServiceErrorCode.NotFound);
|
||||||
|
}
|
||||||
|
catch (EmailTemplateWriteException error)
|
||||||
|
{
|
||||||
|
return Failure<Unit>(error.Message, error.IsConflict ? ServiceErrorCode.Conflict : ServiceErrorCode.InvalidRequest);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
+7
@@ -0,0 +1,7 @@
|
|||||||
|
using HrynCo.NotificationService.Services.Core;
|
||||||
|
using MediatR;
|
||||||
|
using Unit = HrynCo.NotificationService.Services.Core.Unit;
|
||||||
|
|
||||||
|
namespace HrynCo.NotificationService.Services.TemplateDefinitions;
|
||||||
|
|
||||||
|
public sealed record DeleteServiceLanguageVersionsCommand(Guid ServiceId, Guid LanguageId, Guid Revision, int ExpectedVersionCount, bool ConfirmDelete) : IRequest<ServiceResult<Unit>>;
|
||||||
+30
@@ -0,0 +1,30 @@
|
|||||||
|
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.TemplateDefinitions;
|
||||||
|
|
||||||
|
internal sealed class DeleteServiceLanguageVersionsHandler(
|
||||||
|
IContextualSerilogLogger<DeleteServiceLanguageVersionsCommand> logger, INotificationServiceRepository services)
|
||||||
|
: RequestHandler<DeleteServiceLanguageVersionsCommand, ServiceResult<Unit>>(logger)
|
||||||
|
{
|
||||||
|
protected override async Task<ServiceResult<Unit>> DoOnHandle(DeleteServiceLanguageVersionsCommand request, CancellationToken cancellationToken)
|
||||||
|
{
|
||||||
|
if (!request.ConfirmDelete || request.ExpectedVersionCount < 0 || request.Revision == Guid.Empty)
|
||||||
|
return Failure<Unit>("ConfirmDelete must be true; a nonnegative expectedVersionCount and nonempty service revision are required.", ServiceErrorCode.InvalidRequest);
|
||||||
|
var service = await services.GetAsync(request.ServiceId, cancellationToken);
|
||||||
|
if (service?.Languages.Any(l => l.Id == request.LanguageId) != true)
|
||||||
|
return Failure<Unit>("Service language not found.", ServiceErrorCode.NotFound);
|
||||||
|
try
|
||||||
|
{
|
||||||
|
await services.RemoveLanguageAsync(request.ServiceId, request.LanguageId, request.ExpectedVersionCount, request.Revision, cancellationToken);
|
||||||
|
return Success(Unit.Value);
|
||||||
|
}
|
||||||
|
catch (EmailTemplateWriteException error)
|
||||||
|
{
|
||||||
|
return Failure<Unit>(error.Message, error.IsConflict ? ServiceErrorCode.Conflict : ServiceErrorCode.InvalidRequest);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
+7
@@ -0,0 +1,7 @@
|
|||||||
|
using HrynCo.NotificationService.Services.Core;
|
||||||
|
using MediatR;
|
||||||
|
using Unit = HrynCo.NotificationService.Services.Core.Unit;
|
||||||
|
|
||||||
|
namespace HrynCo.NotificationService.Services.TemplateDefinitions;
|
||||||
|
|
||||||
|
public sealed record DeleteUnusedServiceLanguageCommand(Guid ServiceId, Guid LanguageId, Guid Revision) : IRequest<ServiceResult<Unit>>;
|
||||||
+34
@@ -0,0 +1,34 @@
|
|||||||
|
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.TemplateDefinitions;
|
||||||
|
|
||||||
|
internal sealed class DeleteUnusedServiceLanguageHandler(
|
||||||
|
IContextualSerilogLogger<DeleteUnusedServiceLanguageCommand> logger, INotificationServiceRepository services)
|
||||||
|
: RequestHandler<DeleteUnusedServiceLanguageCommand, ServiceResult<Unit>>(logger)
|
||||||
|
{
|
||||||
|
protected override async Task<ServiceResult<Unit>> DoOnHandle(DeleteUnusedServiceLanguageCommand request, CancellationToken cancellationToken)
|
||||||
|
{
|
||||||
|
if (request.Revision == Guid.Empty)
|
||||||
|
return Failure<Unit>("A nonempty service revision is required.", ServiceErrorCode.InvalidRequest);
|
||||||
|
var service = await services.GetAsync(request.ServiceId, cancellationToken);
|
||||||
|
var language = service?.Languages.SingleOrDefault(l => l.Id == request.LanguageId);
|
||||||
|
if (language is null)
|
||||||
|
return Failure<Unit>("Service language not found.", ServiceErrorCode.NotFound);
|
||||||
|
if (language.VersionCount > 0)
|
||||||
|
return Failure<Unit>("This language has template versions. Use delete-with-versions with explicit confirmation and the current count.", ServiceErrorCode.Conflict);
|
||||||
|
try
|
||||||
|
{
|
||||||
|
// The repository repeats the zero-count and revision checks under its mutation lock.
|
||||||
|
await services.RemoveLanguageAsync(request.ServiceId, request.LanguageId, 0, request.Revision, cancellationToken);
|
||||||
|
return Success(Unit.Value);
|
||||||
|
}
|
||||||
|
catch (EmailTemplateWriteException error)
|
||||||
|
{
|
||||||
|
return Failure<Unit>(error.Message, error.IsConflict ? ServiceErrorCode.Conflict : ServiceErrorCode.InvalidRequest);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
namespace HrynCo.NotificationService.Services.TemplateDefinitions;
|
||||||
|
|
||||||
|
public sealed record EmailTemplateDefinitionDto(Guid Id, Guid ServiceId, string ServiceName, string Key, Guid Revision,
|
||||||
|
IReadOnlyList<TemplateVariableDto> Variables, IReadOnlyList<TemplateTranslationDto> Translations,
|
||||||
|
DateTimeOffset Created, DateTimeOffset? Updated);
|
||||||
+17
@@ -0,0 +1,17 @@
|
|||||||
|
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.TemplateDefinitions;
|
||||||
|
|
||||||
|
internal sealed class GetAllNotificationServicesHandler(
|
||||||
|
IContextualSerilogLogger<GetAllNotificationServicesQuery> logger, INotificationServiceRepository services)
|
||||||
|
: RequestHandler<GetAllNotificationServicesQuery, ServiceResult<IReadOnlyList<NotificationServiceDto>>>(logger)
|
||||||
|
{
|
||||||
|
protected override async Task<ServiceResult<IReadOnlyList<NotificationServiceDto>>> DoOnHandle(GetAllNotificationServicesQuery request, CancellationToken cancellationToken)
|
||||||
|
{
|
||||||
|
var all = await services.GetAllAsync(cancellationToken);
|
||||||
|
return Success<IReadOnlyList<NotificationServiceDto>>(all.Select(NotificationServiceMapping.Map).ToList());
|
||||||
|
}
|
||||||
|
}
|
||||||
+6
@@ -0,0 +1,6 @@
|
|||||||
|
using HrynCo.NotificationService.Services.Core;
|
||||||
|
using MediatR;
|
||||||
|
|
||||||
|
namespace HrynCo.NotificationService.Services.TemplateDefinitions;
|
||||||
|
|
||||||
|
public sealed record GetAllNotificationServicesQuery() : IRequest<ServiceResult<IReadOnlyList<NotificationServiceDto>>>;
|
||||||
+19
@@ -0,0 +1,19 @@
|
|||||||
|
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.TemplateDefinitions;
|
||||||
|
|
||||||
|
internal sealed class GetEmailTemplateDefinitionHandler(
|
||||||
|
IContextualSerilogLogger<GetEmailTemplateDefinitionQuery> logger, IEmailTemplateDefinitionRepository definitions)
|
||||||
|
: RequestHandler<GetEmailTemplateDefinitionQuery, ServiceResult<EmailTemplateDefinitionDto>>(logger)
|
||||||
|
{
|
||||||
|
protected override async Task<ServiceResult<EmailTemplateDefinitionDto>> DoOnHandle(GetEmailTemplateDefinitionQuery request, CancellationToken cancellationToken)
|
||||||
|
{
|
||||||
|
var definition = await definitions.GetDefinitionAsync(request.ServiceId, request.Key, cancellationToken);
|
||||||
|
return definition is null
|
||||||
|
? Failure<EmailTemplateDefinitionDto>("Template definition not found.", ServiceErrorCode.NotFound)
|
||||||
|
: Success(TemplateDefinitionMapping.Map(definition));
|
||||||
|
}
|
||||||
|
}
|
||||||
+6
@@ -0,0 +1,6 @@
|
|||||||
|
using HrynCo.NotificationService.Services.Core;
|
||||||
|
using MediatR;
|
||||||
|
|
||||||
|
namespace HrynCo.NotificationService.Services.TemplateDefinitions;
|
||||||
|
|
||||||
|
public sealed record GetEmailTemplateDefinitionQuery(Guid ServiceId, string Key) : IRequest<ServiceResult<EmailTemplateDefinitionDto>>;
|
||||||
+23
@@ -0,0 +1,23 @@
|
|||||||
|
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.TemplateDefinitions;
|
||||||
|
|
||||||
|
internal sealed class GetNotificationServiceHandler(
|
||||||
|
IContextualSerilogLogger<GetNotificationServiceQuery> logger, INotificationServiceRepository services)
|
||||||
|
: RequestHandler<GetNotificationServiceQuery, ServiceResult<NotificationServiceDto>>(logger)
|
||||||
|
{
|
||||||
|
protected override async Task<ServiceResult<NotificationServiceDto>> DoOnHandle(GetNotificationServiceQuery request, CancellationToken cancellationToken)
|
||||||
|
{
|
||||||
|
if (request.ServiceId is null && string.IsNullOrWhiteSpace(request.Name))
|
||||||
|
return Failure<NotificationServiceDto>("An exact service name is required.", ServiceErrorCode.InvalidRequest);
|
||||||
|
var service = request.ServiceId is Guid id
|
||||||
|
? await services.GetAsync(id, cancellationToken)
|
||||||
|
: await services.GetByNameAsync(request.Name!, cancellationToken);
|
||||||
|
return service is null
|
||||||
|
? Failure<NotificationServiceDto>("Service not found.", ServiceErrorCode.NotFound)
|
||||||
|
: Success(NotificationServiceMapping.Map(service));
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
using HrynCo.NotificationService.Services.Core;
|
||||||
|
using MediatR;
|
||||||
|
|
||||||
|
namespace HrynCo.NotificationService.Services.TemplateDefinitions;
|
||||||
|
|
||||||
|
public sealed record GetNotificationServiceQuery(Guid? ServiceId, string? Name) : IRequest<ServiceResult<NotificationServiceDto>>;
|
||||||
+20
@@ -0,0 +1,20 @@
|
|||||||
|
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.TemplateDefinitions;
|
||||||
|
|
||||||
|
internal sealed class GetServiceTemplateDefinitionsHandler(
|
||||||
|
IContextualSerilogLogger<GetServiceTemplateDefinitionsQuery> logger,
|
||||||
|
INotificationServiceRepository services, IEmailTemplateDefinitionRepository definitions)
|
||||||
|
: RequestHandler<GetServiceTemplateDefinitionsQuery, ServiceResult<IReadOnlyList<EmailTemplateDefinitionDto>>>(logger)
|
||||||
|
{
|
||||||
|
protected override async Task<ServiceResult<IReadOnlyList<EmailTemplateDefinitionDto>>> DoOnHandle(GetServiceTemplateDefinitionsQuery request, CancellationToken cancellationToken)
|
||||||
|
{
|
||||||
|
if (await services.GetAsync(request.ServiceId, cancellationToken) is null)
|
||||||
|
return Failure<IReadOnlyList<EmailTemplateDefinitionDto>>("Service not found.", ServiceErrorCode.NotFound);
|
||||||
|
var all = await definitions.GetDefinitionsAsync(request.ServiceId, cancellationToken);
|
||||||
|
return Success<IReadOnlyList<EmailTemplateDefinitionDto>>(all.Select(TemplateDefinitionMapping.Map).ToList());
|
||||||
|
}
|
||||||
|
}
|
||||||
+6
@@ -0,0 +1,6 @@
|
|||||||
|
using HrynCo.NotificationService.Services.Core;
|
||||||
|
using MediatR;
|
||||||
|
|
||||||
|
namespace HrynCo.NotificationService.Services.TemplateDefinitions;
|
||||||
|
|
||||||
|
public sealed record GetServiceTemplateDefinitionsQuery(Guid ServiceId) : IRequest<ServiceResult<IReadOnlyList<EmailTemplateDefinitionDto>>>;
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
namespace HrynCo.NotificationService.Services.TemplateDefinitions;
|
||||||
|
|
||||||
|
public sealed record NotificationServiceDto(Guid Id, string Name, Guid Revision, IReadOnlyList<string> SupportedLanguageCodes, IReadOnlyList<ServiceLanguageDto> Languages);
|
||||||
@@ -0,0 +1,10 @@
|
|||||||
|
using HrynCo.NotificationService.DAL.Abstract.Templates;
|
||||||
|
|
||||||
|
namespace HrynCo.NotificationService.Services.TemplateDefinitions;
|
||||||
|
|
||||||
|
internal static class NotificationServiceMapping
|
||||||
|
{
|
||||||
|
public static NotificationServiceDto Map(NotificationServiceInfo service) =>
|
||||||
|
new(service.Id, service.Name, service.Revision, service.Languages.Select(l => l.Code).ToList(),
|
||||||
|
service.Languages.Select(l => new ServiceLanguageDto(l.Id, l.Code, l.VersionCount)).ToList());
|
||||||
|
}
|
||||||
+7
@@ -0,0 +1,7 @@
|
|||||||
|
using HrynCo.NotificationService.DAL.Abstract.Templates;
|
||||||
|
using HrynCo.NotificationService.Services.Core;
|
||||||
|
using MediatR;
|
||||||
|
|
||||||
|
namespace HrynCo.NotificationService.Services.TemplateDefinitions;
|
||||||
|
|
||||||
|
public sealed record SaveNotificationServiceCommand(NotificationServiceInfo Service, bool IsNew) : IRequest<ServiceResult<NotificationServiceDto>>;
|
||||||
+30
@@ -0,0 +1,30 @@
|
|||||||
|
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.TemplateDefinitions;
|
||||||
|
|
||||||
|
internal sealed class SaveNotificationServiceHandler(
|
||||||
|
IContextualSerilogLogger<SaveNotificationServiceCommand> logger, INotificationServiceRepository services)
|
||||||
|
: RequestHandler<SaveNotificationServiceCommand, ServiceResult<NotificationServiceDto>>(logger)
|
||||||
|
{
|
||||||
|
protected override async Task<ServiceResult<NotificationServiceDto>> DoOnHandle(SaveNotificationServiceCommand request, CancellationToken cancellationToken)
|
||||||
|
{
|
||||||
|
if (!request.IsNew && request.Service.Revision == Guid.Empty)
|
||||||
|
return Failure<NotificationServiceDto>("A nonempty service revision is required for update.", ServiceErrorCode.InvalidRequest);
|
||||||
|
if (!request.IsNew && await services.GetAsync(request.Service.Id, cancellationToken) is null)
|
||||||
|
return Failure<NotificationServiceDto>("Service not found.", ServiceErrorCode.NotFound);
|
||||||
|
try
|
||||||
|
{
|
||||||
|
await services.SaveAsync(request.Service, request.IsNew, cancellationToken);
|
||||||
|
var saved = await services.GetAsync(request.Service.Id, cancellationToken);
|
||||||
|
return Success(NotificationServiceMapping.Map(saved!));
|
||||||
|
}
|
||||||
|
catch (EmailTemplateWriteException error)
|
||||||
|
{
|
||||||
|
return Failure<NotificationServiceDto>(error.Message, error.IsConflict ? ServiceErrorCode.Conflict : ServiceErrorCode.InvalidRequest);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
namespace HrynCo.NotificationService.Services.TemplateDefinitions;
|
||||||
|
|
||||||
|
public sealed record ServiceLanguageDto(Guid Id, string Code, int VersionCount);
|
||||||
@@ -0,0 +1,12 @@
|
|||||||
|
using HrynCo.NotificationService.DAL.Abstract.Templates;
|
||||||
|
|
||||||
|
namespace HrynCo.NotificationService.Services.TemplateDefinitions;
|
||||||
|
|
||||||
|
internal static class TemplateDefinitionMapping
|
||||||
|
{
|
||||||
|
public static EmailTemplateDefinitionDto Map(EmailTemplateDefinition definition) => new(
|
||||||
|
definition.Id, definition.ServiceId, definition.ServiceName, definition.Key, definition.Revision,
|
||||||
|
definition.Variables.Select(v => new TemplateVariableDto(v.Name, v.Required)).ToList(),
|
||||||
|
definition.Languages.Select(t => new TemplateTranslationDto(t.Id, t.LanguageCode, t.Subject, t.HtmlBody, t.TextBody, t.Created, t.Updated)).ToList(),
|
||||||
|
definition.Created, definition.Updated);
|
||||||
|
}
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
namespace HrynCo.NotificationService.Services.TemplateDefinitions;
|
||||||
|
|
||||||
|
public sealed record TemplateTranslationDto(Guid Id, string LanguageCode, string Subject, string HtmlBody, string TextBody, DateTimeOffset Created, DateTimeOffset? Updated);
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
namespace HrynCo.NotificationService.Services.TemplateDefinitions;
|
||||||
|
|
||||||
|
public sealed record TemplateVariableDto(string Name, bool Required);
|
||||||
+7
@@ -0,0 +1,7 @@
|
|||||||
|
using HrynCo.NotificationService.DAL.Abstract.Templates;
|
||||||
|
using HrynCo.NotificationService.Services.Core;
|
||||||
|
using MediatR;
|
||||||
|
|
||||||
|
namespace HrynCo.NotificationService.Services.TemplateDefinitions;
|
||||||
|
|
||||||
|
public sealed record UpsertEmailTemplateDefinitionCommand(EmailTemplateDefinitionPatch Patch) : IRequest<ServiceResult<EmailTemplateDefinitionDto>>;
|
||||||
+27
@@ -0,0 +1,27 @@
|
|||||||
|
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.TemplateDefinitions;
|
||||||
|
|
||||||
|
internal sealed class UpsertEmailTemplateDefinitionHandler(
|
||||||
|
IContextualSerilogLogger<UpsertEmailTemplateDefinitionCommand> logger, IEmailTemplateDefinitionRepository definitions)
|
||||||
|
: RequestHandler<UpsertEmailTemplateDefinitionCommand, ServiceResult<EmailTemplateDefinitionDto>>(logger)
|
||||||
|
{
|
||||||
|
protected override async Task<ServiceResult<EmailTemplateDefinitionDto>> DoOnHandle(UpsertEmailTemplateDefinitionCommand request, CancellationToken cancellationToken)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var definition = await definitions.UpsertDefinitionAsync(request.Patch, cancellationToken);
|
||||||
|
return definition is null
|
||||||
|
? Failure<EmailTemplateDefinitionDto>("Service not found.", ServiceErrorCode.NotFound)
|
||||||
|
: Success(TemplateDefinitionMapping.Map(definition));
|
||||||
|
}
|
||||||
|
catch (EmailTemplateWriteException error)
|
||||||
|
{
|
||||||
|
return Failure<EmailTemplateDefinitionDto>(error.Message, error.IsConflict ? ServiceErrorCode.Conflict : ServiceErrorCode.InvalidRequest);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,137 @@
|
|||||||
|
using System.Net;
|
||||||
|
using System.Net.Http.Json;
|
||||||
|
using System.Text.Json;
|
||||||
|
using System.Text.RegularExpressions;
|
||||||
|
using HrynCo.NotificationService.DAL.Abstract.Repositories;
|
||||||
|
using Microsoft.AspNetCore.TestHost;
|
||||||
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
|
|
||||||
|
namespace HrynCo.NotificationService.Web.IntegrationTests;
|
||||||
|
|
||||||
|
public sealed class AdminDeletionTests(TemplateDatabaseFixture fixture) : IClassFixture<TemplateDatabaseFixture>
|
||||||
|
{
|
||||||
|
[Fact]
|
||||||
|
public async Task DefinitionDeleteRequiresAntiforgeryAndConfirmation_AndPreservesFilter()
|
||||||
|
{
|
||||||
|
await using var context = await DefinitionApiTestContext.CreateAsync(fixture);
|
||||||
|
using var client = context.App.CreateClient(new() { AllowAutoRedirect = false });
|
||||||
|
await context.UpsertAsync(DefinitionApiTestContext.Body(null, "en", "uk"));
|
||||||
|
var definition = await context.ReadAsync();
|
||||||
|
var url = $"/admin/services/{context.ServiceId}/templates/{definition.GetProperty("id").GetGuid()}/delete";
|
||||||
|
var html = await client.GetStringAsync(url + "?keyFilter=Welcome");
|
||||||
|
Assert.Contains("2 translation(s)", html);
|
||||||
|
Assert.Contains("Languages: en, uk", html);
|
||||||
|
var fields = TemplateEditorTestFields.Read(html);
|
||||||
|
Assert.Equal(HttpStatusCode.BadRequest, (await client.PostAsync(url, new FormUrlEncodedContent(new Dictionary<string, string> { ["ConfirmDelete"] = "true" }))).StatusCode);
|
||||||
|
var unconfirmed = await (await client.PostAsync(url, new FormUrlEncodedContent(fields))).Content.ReadAsStringAsync();
|
||||||
|
Assert.Contains("Confirm deletion", unconfirmed);
|
||||||
|
AssertConfirmationReset(unconfirmed);
|
||||||
|
fields = TemplateEditorTestFields.Read(unconfirmed);
|
||||||
|
fields["ConfirmDelete"] = "true";
|
||||||
|
fields["Key"] = "Different";
|
||||||
|
var deleted = await client.PostAsync(url, new FormUrlEncodedContent(fields));
|
||||||
|
Assert.Equal(HttpStatusCode.Redirect, deleted.StatusCode);
|
||||||
|
Assert.Equal($"/admin/services/{context.ServiceId}?key=Welcome", deleted.Headers.Location!.OriginalString);
|
||||||
|
Assert.Equal(HttpStatusCode.NotFound, (await client.GetAsync(url)).StatusCode);
|
||||||
|
Assert.Equal(HttpStatusCode.OK, (await client.GetAsync($"/admin/services/{context.ServiceId}")).StatusCode);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task StaleDefinitionConfirmationRefreshesCountRevisionAndUncheckedConfirmation()
|
||||||
|
{
|
||||||
|
await using var context = await DefinitionApiTestContext.CreateAsync(fixture);
|
||||||
|
await context.UpsertAsync(DefinitionApiTestContext.Body(null));
|
||||||
|
var definition = await context.ReadAsync();
|
||||||
|
var url = $"/admin/services/{context.ServiceId}/templates/{definition.GetProperty("id").GetGuid()}/delete";
|
||||||
|
var fields = TemplateEditorTestFields.Read(await context.Client.GetStringAsync(url));
|
||||||
|
var oldRevision = fields["Revision"];
|
||||||
|
await context.UpsertAsync(DefinitionApiTestContext.Body(definition.GetProperty("revision").GetGuid(), "en"));
|
||||||
|
fields["ConfirmDelete"] = "true";
|
||||||
|
var html = await (await context.Client.PostAsync(url, new FormUrlEncodedContent(fields))).Content.ReadAsStringAsync();
|
||||||
|
Assert.Contains("revision changed", html);
|
||||||
|
Assert.Contains("1 translation(s)", html);
|
||||||
|
Assert.NotEqual(oldRevision, TemplateEditorTestFields.Read(html)["Revision"]);
|
||||||
|
AssertConfirmationReset(html);
|
||||||
|
Assert.Single((await context.ReadAsync()).GetProperty("translations").EnumerateArray());
|
||||||
|
var wrongScope = $"/admin/services/{Guid.NewGuid()}/templates/{definition.GetProperty("id").GetGuid()}/delete";
|
||||||
|
Assert.Equal(HttpStatusCode.NotFound, (await context.Client.GetAsync(wrongScope)).StatusCode);
|
||||||
|
Assert.Equal(HttpStatusCode.NotFound, (await context.Client.PostAsync(wrongScope, new FormUrlEncodedContent(fields))).StatusCode);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task ServiceDeleteIsBlockedByEmptyDefinition_AndRequiresFreshConfirmationAfterRename()
|
||||||
|
{
|
||||||
|
await using var context = await DefinitionApiTestContext.CreateAsync(fixture);
|
||||||
|
using var client = context.App.CreateClient(new() { AllowAutoRedirect = false });
|
||||||
|
await context.UpsertAsync(DefinitionApiTestContext.Body(null));
|
||||||
|
var url = $"/admin/services/{context.ServiceId}/delete";
|
||||||
|
var blocked = await client.GetStringAsync(url);
|
||||||
|
Assert.Contains("1 template definition(s)", blocked);
|
||||||
|
Assert.Contains("Email channels are retained", blocked);
|
||||||
|
var fields = TemplateEditorTestFields.Read(blocked);
|
||||||
|
fields["ConfirmDelete"] = "true";
|
||||||
|
Assert.Contains("Remove every template definition", await (await client.PostAsync(url, new FormUrlEncodedContent(fields))).Content.ReadAsStringAsync());
|
||||||
|
var definition = await context.ReadAsync();
|
||||||
|
await client.DeleteAsync(context.DefinitionUrl() + "&revision=" + definition.GetProperty("revision").GetGuid());
|
||||||
|
fields = TemplateEditorTestFields.Read(await client.GetStringAsync(url));
|
||||||
|
var oldRevision = fields["Revision"];
|
||||||
|
using (var scope = context.App.Services.CreateScope())
|
||||||
|
{
|
||||||
|
var services = scope.ServiceProvider.GetRequiredService<INotificationServiceRepository>();
|
||||||
|
var service = (await services.GetAsync(context.ServiceId))!;
|
||||||
|
service.Name = "Renamed Before Delete";
|
||||||
|
await services.SaveAsync(service, false);
|
||||||
|
}
|
||||||
|
fields["ConfirmDelete"] = "true";
|
||||||
|
var stale = await (await client.PostAsync(url, new FormUrlEncodedContent(fields))).Content.ReadAsStringAsync();
|
||||||
|
Assert.Contains("revision changed", stale);
|
||||||
|
Assert.Contains("Renamed Before Delete", stale);
|
||||||
|
Assert.NotEqual(oldRevision, TemplateEditorTestFields.Read(stale)["Revision"]);
|
||||||
|
AssertConfirmationReset(stale);
|
||||||
|
fields = TemplateEditorTestFields.Read(stale);
|
||||||
|
Assert.Contains("Confirm deletion", await (await client.PostAsync(url, new FormUrlEncodedContent(fields))).Content.ReadAsStringAsync());
|
||||||
|
fields["ConfirmDelete"] = "true";
|
||||||
|
var deleted = await client.PostAsync(url, new FormUrlEncodedContent(fields));
|
||||||
|
Assert.Equal(HttpStatusCode.Redirect, deleted.StatusCode);
|
||||||
|
Assert.Equal("/admin/services", deleted.Headers.Location!.OriginalString);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task EmptyStatesOfferScopedFirstCreateOrClearFilterActions()
|
||||||
|
{
|
||||||
|
await using var context = await DefinitionApiTestContext.CreateAsync(fixture);
|
||||||
|
var root = $"/admin/services/{context.ServiceId}";
|
||||||
|
var templates = await context.Client.GetStringAsync(root);
|
||||||
|
Assert.Contains("No email templates for this service yet.", templates);
|
||||||
|
Assert.Contains("Create First Template", templates);
|
||||||
|
Assert.Contains(root + "/templates/create", templates);
|
||||||
|
Assert.Contains("template-key-filter", templates);
|
||||||
|
var filtered = await context.Client.GetStringAsync(root + "?key=Missing");
|
||||||
|
Assert.Contains("No templates match this key.", filtered);
|
||||||
|
Assert.Contains(">Clear filter</a>", filtered);
|
||||||
|
Assert.DoesNotContain("Create First Template", filtered);
|
||||||
|
var service = (await context.Client.GetFromJsonAsync<JsonElement>(context.ServiceRoot)).GetProperty("data");
|
||||||
|
await context.Client.DeleteAsync(context.ServiceRoot + "?revision=" + service.GetProperty("revision").GetGuid());
|
||||||
|
var services = await context.Client.GetStringAsync("/admin/services");
|
||||||
|
Assert.Contains("No services yet.", services);
|
||||||
|
Assert.Contains("Create First Service", services);
|
||||||
|
Assert.Contains("aria-hidden=\"true\"", services);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task TemplateLoadFailureDoesNotPretendTheServiceHasNoTemplates()
|
||||||
|
{
|
||||||
|
await using var context = await DefinitionApiTestContext.CreateAsync(fixture);
|
||||||
|
using var failing = context.App.WithWebHostBuilder(builder => builder.ConfigureTestServices(services =>
|
||||||
|
services.AddScoped<IEmailTemplateDefinitionRepository, UnavailableEmailTemplateDefinitionRepository>()));
|
||||||
|
using var client = failing.CreateClient();
|
||||||
|
var html = await client.GetStringAsync($"/admin/services/{context.ServiceId}");
|
||||||
|
Assert.Contains("Could not load templates", html);
|
||||||
|
Assert.Contains("Templates are unavailable", html);
|
||||||
|
Assert.DoesNotContain("Create First Template", html);
|
||||||
|
Assert.DoesNotContain("No email templates for this service yet.", html);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void AssertConfirmationReset(string html) =>
|
||||||
|
Assert.DoesNotContain("checked", Regex.Match(html, "<input[^>]+type=\"checkbox\"[^>]*>").Value);
|
||||||
|
}
|
||||||
@@ -0,0 +1,60 @@
|
|||||||
|
using System.Net;
|
||||||
|
using System.Text.RegularExpressions;
|
||||||
|
using HrynCo.NotificationService.DAL.Abstract.Providers;
|
||||||
|
using HrynCo.NotificationService.DAL.Abstract.Repositories;
|
||||||
|
using HrynCo.NotificationService.DAL.EF;
|
||||||
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
|
|
||||||
|
namespace HrynCo.NotificationService.Web.IntegrationTests;
|
||||||
|
|
||||||
|
public sealed class AdminNavigationTests(TemplateDatabaseFixture fixture) : IClassFixture<TemplateDatabaseFixture>
|
||||||
|
{
|
||||||
|
[Fact]
|
||||||
|
public async Task LayoutLinksToApiDocumentationInNewTab_AndScalarIsAvailable()
|
||||||
|
{
|
||||||
|
await using var context = await DefinitionApiTestContext.CreateAsync(fixture);
|
||||||
|
var pages = new[] { "/admin/services", "/admin/channels", "/admin/services/create", "/admin/channels/create" };
|
||||||
|
foreach (var page in pages)
|
||||||
|
{
|
||||||
|
var html = await context.Client.GetStringAsync(page);
|
||||||
|
Assert.Single(Regex.Matches(html, "class=\"admin-api-documentation\""));
|
||||||
|
Assert.Contains("href=\"/scalar/v1\" target=\"_blank\" rel=\"noopener noreferrer\"", html);
|
||||||
|
Assert.Contains("aria-label=\"API documentation (opens in a new tab)\">API documentation</a>", html);
|
||||||
|
}
|
||||||
|
Assert.Equal(HttpStatusCode.OK, (await context.Client.GetAsync("/scalar/v1")).StatusCode);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task BothListTablesHaveScopedGridBorders_AndRetainTemplateHeaderOnFilterMiss()
|
||||||
|
{
|
||||||
|
await using var context = await DefinitionApiTestContext.CreateAsync(fixture);
|
||||||
|
var services = await context.Client.GetStringAsync("/admin/services");
|
||||||
|
Assert.Contains("table table-hover admin-data-table", services);
|
||||||
|
var templates = await context.Client.GetStringAsync($"/admin/services/{context.ServiceId}?key=Missing");
|
||||||
|
Assert.Contains("table table-hover admin-data-table service-template-grid", templates);
|
||||||
|
Assert.Contains("class=\"template-actions\"", templates);
|
||||||
|
Assert.Contains("No templates match this key.", templates);
|
||||||
|
Assert.Contains("class=\"template-key-filter\"", templates);
|
||||||
|
}
|
||||||
|
[Fact]
|
||||||
|
public async Task AllFourDataTablesShareTheGridClassAndScopedColumnHeaders()
|
||||||
|
{
|
||||||
|
await using var context = await DefinitionApiTestContext.CreateAsync(fixture);
|
||||||
|
using var scope = context.App.Services.CreateScope();
|
||||||
|
await scope.ServiceProvider.GetRequiredService<IEmailChannelRepository>().AddAsync(new EmailChannel
|
||||||
|
{
|
||||||
|
ServiceName = "TestService", EmailChannelType = EmailChannelType.Smtp,
|
||||||
|
Settings = new SmtpChannelSettings { Host = "localhost", FromEmail = "test@example.invalid" }
|
||||||
|
});
|
||||||
|
await scope.ServiceProvider.GetRequiredService<NotificationDbContext>().SaveChangesAsync();
|
||||||
|
var pages = new[] { "/admin/services", $"/admin/services/{context.ServiceId}", "/admin/channels", $"/admin/services/{context.ServiceId}/edit" };
|
||||||
|
foreach (var page in pages)
|
||||||
|
{
|
||||||
|
var html = await context.Client.GetStringAsync(page);
|
||||||
|
Assert.Single(Regex.Matches(html, "<table[^>]+admin-data-table"));
|
||||||
|
Assert.Equal(Regex.Matches(html, "<th[ >]").Count, Regex.Matches(html, "<th scope=\"col\"").Count);
|
||||||
|
Assert.DoesNotContain("<thead class=\"table-dark\">", html);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,34 @@
|
|||||||
|
using HrynCo.NotificationService.DAL.Abstract.Providers;
|
||||||
|
using HrynCo.NotificationService.DAL.Abstract.Repositories;
|
||||||
|
using HrynCo.NotificationService.DAL.EF;
|
||||||
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
|
|
||||||
|
namespace HrynCo.NotificationService.Web.IntegrationTests;
|
||||||
|
|
||||||
|
public sealed class AdminSmtpEditorViewTests(TemplateDatabaseFixture fixture) : IClassFixture<TemplateDatabaseFixture>
|
||||||
|
{
|
||||||
|
[Fact]
|
||||||
|
public async Task ExistingChannelTestIncludesIdWithoutRenderingSavedPassword()
|
||||||
|
{
|
||||||
|
await using var context = await DefinitionApiTestContext.CreateAsync(fixture);
|
||||||
|
using var scope = context.App.Services.CreateScope();
|
||||||
|
var channel = new EmailChannel
|
||||||
|
{
|
||||||
|
ServiceName = "TestService",
|
||||||
|
EmailChannelType = EmailChannelType.Smtp,
|
||||||
|
Settings = new SmtpChannelSettings
|
||||||
|
{
|
||||||
|
Host = "smtp.example.invalid", Username = "test-user",
|
||||||
|
Password = "saved-secret-must-not-render", FromEmail = "sender@example.invalid"
|
||||||
|
}
|
||||||
|
};
|
||||||
|
await scope.ServiceProvider.GetRequiredService<IEmailChannelRepository>().AddAsync(channel);
|
||||||
|
await scope.ServiceProvider.GetRequiredService<NotificationDbContext>().SaveChangesAsync();
|
||||||
|
|
||||||
|
var html = await context.Client.GetStringAsync($"/admin/channels/{channel.Id}");
|
||||||
|
|
||||||
|
Assert.Contains($"value=\"{channel.Id}\"", html);
|
||||||
|
Assert.Contains("const channelId = form.querySelector('[name=\"Id\"]').value", html);
|
||||||
|
Assert.DoesNotContain("saved-secret-must-not-render", html);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,136 @@
|
|||||||
|
using System.Net;
|
||||||
|
using HrynCo.NotificationService.DAL.Abstract.Providers;
|
||||||
|
using HrynCo.NotificationService.DAL.Abstract.Repositories;
|
||||||
|
using HrynCo.NotificationService.DAL.EF;
|
||||||
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
|
|
||||||
|
namespace HrynCo.NotificationService.Web.IntegrationTests;
|
||||||
|
|
||||||
|
public sealed class AdminSmtpSaveTests(TemplateDatabaseFixture fixture) : IClassFixture<TemplateDatabaseFixture>
|
||||||
|
{
|
||||||
|
[Fact]
|
||||||
|
public async Task BlankPasswordKeepsSavedValueWhenOnlySenderChanges()
|
||||||
|
{
|
||||||
|
await using var context = await DefinitionApiTestContext.CreateAsync(fixture);
|
||||||
|
var channel = await SeedChannel(context);
|
||||||
|
using var client = context.App.CreateClient(new() { AllowAutoRedirect = false });
|
||||||
|
var fields = TemplateEditorTestFields.Read(await client.GetStringAsync($"/admin/channels/{channel.Id}"));
|
||||||
|
fields["UseSsl"] = "true";
|
||||||
|
fields["Password"] = "";
|
||||||
|
fields["FromName"] = "Updated sender";
|
||||||
|
|
||||||
|
var response = await client.PostAsync("/admin/channels/save", new FormUrlEncodedContent(fields));
|
||||||
|
|
||||||
|
Assert.Equal(HttpStatusCode.Redirect, response.StatusCode);
|
||||||
|
var saved = await ReadSettings(context, channel.Id);
|
||||||
|
Assert.Equal("saved-test-secret", saved.Password);
|
||||||
|
Assert.Equal("Updated sender", saved.FromName);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Theory]
|
||||||
|
[InlineData("Host", "other.example.invalid")]
|
||||||
|
[InlineData("Port", "2525")]
|
||||||
|
[InlineData("Username", "other-user")]
|
||||||
|
[InlineData("UseSsl", "false")]
|
||||||
|
public async Task ChangedConnectionWithBlankPasswordIsRejected(string field, string value)
|
||||||
|
{
|
||||||
|
await using var context = await DefinitionApiTestContext.CreateAsync(fixture);
|
||||||
|
var channel = await SeedChannel(context);
|
||||||
|
using var client = context.App.CreateClient(new() { AllowAutoRedirect = false });
|
||||||
|
var fields = TemplateEditorTestFields.Read(await client.GetStringAsync($"/admin/channels/{channel.Id}"));
|
||||||
|
fields["UseSsl"] = "true";
|
||||||
|
fields["Password"] = "";
|
||||||
|
fields[field] = value;
|
||||||
|
|
||||||
|
var response = await client.PostAsync("/admin/channels/save", new FormUrlEncodedContent(fields));
|
||||||
|
var html = await response.Content.ReadAsStringAsync();
|
||||||
|
|
||||||
|
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
|
||||||
|
Assert.Contains("Enter the password to save changed SMTP connection settings.", html);
|
||||||
|
Assert.DoesNotContain("saved-test-secret", html);
|
||||||
|
var saved = await ReadSettings(context, channel.Id);
|
||||||
|
Assert.Equal("saved-test-secret", saved.Password);
|
||||||
|
Assert.Equal("smtp.example.invalid", saved.Host);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task ExplicitPasswordCanReplaceSavedValue()
|
||||||
|
{
|
||||||
|
await using var context = await DefinitionApiTestContext.CreateAsync(fixture);
|
||||||
|
var channel = await SeedChannel(context);
|
||||||
|
using var client = context.App.CreateClient(new() { AllowAutoRedirect = false });
|
||||||
|
var fields = TemplateEditorTestFields.Read(await client.GetStringAsync($"/admin/channels/{channel.Id}"));
|
||||||
|
fields["UseSsl"] = "true";
|
||||||
|
fields["Password"] = "replacement-test-secret";
|
||||||
|
fields["Host"] = "other.example.invalid";
|
||||||
|
|
||||||
|
var response = await client.PostAsync("/admin/channels/save", new FormUrlEncodedContent(fields));
|
||||||
|
|
||||||
|
Assert.Equal(HttpStatusCode.Redirect, response.StatusCode);
|
||||||
|
var saved = await ReadSettings(context, channel.Id);
|
||||||
|
Assert.Equal("replacement-test-secret", saved.Password);
|
||||||
|
Assert.Equal("other.example.invalid", saved.Host);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task NewChannelStillRequiresPassword()
|
||||||
|
{
|
||||||
|
await using var context = await DefinitionApiTestContext.CreateAsync(fixture);
|
||||||
|
using var client = context.App.CreateClient(new() { AllowAutoRedirect = false });
|
||||||
|
var fields = TemplateEditorTestFields.Read(await client.GetStringAsync("/admin/channels/create"));
|
||||||
|
fields["ServiceName"] = "NewTestService";
|
||||||
|
fields["Host"] = "smtp.example.invalid";
|
||||||
|
fields["Username"] = "test-user";
|
||||||
|
fields["FromEmail"] = "sender@example.invalid";
|
||||||
|
fields["FromName"] = "Sender";
|
||||||
|
fields["Password"] = "";
|
||||||
|
|
||||||
|
var response = await client.PostAsync("/admin/channels/save", new FormUrlEncodedContent(fields));
|
||||||
|
|
||||||
|
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
|
||||||
|
Assert.Contains("The Password field is required.", await response.Content.ReadAsStringAsync());
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task InvalidFormDoesNotRenderEnteredPassword()
|
||||||
|
{
|
||||||
|
await using var context = await DefinitionApiTestContext.CreateAsync(fixture);
|
||||||
|
var channel = await SeedChannel(context);
|
||||||
|
using var client = context.App.CreateClient(new() { AllowAutoRedirect = false });
|
||||||
|
var fields = TemplateEditorTestFields.Read(await client.GetStringAsync($"/admin/channels/{channel.Id}"));
|
||||||
|
fields["Password"] = "replacement-test-secret";
|
||||||
|
fields["FromEmail"] = "invalid-address";
|
||||||
|
|
||||||
|
var response = await client.PostAsync("/admin/channels/save", new FormUrlEncodedContent(fields));
|
||||||
|
var html = await response.Content.ReadAsStringAsync();
|
||||||
|
|
||||||
|
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
|
||||||
|
Assert.DoesNotContain("replacement-test-secret", html);
|
||||||
|
Assert.Equal("saved-test-secret", (await ReadSettings(context, channel.Id)).Password);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static async Task<EmailChannel> SeedChannel(DefinitionApiTestContext context)
|
||||||
|
{
|
||||||
|
using var scope = context.App.Services.CreateScope();
|
||||||
|
var channel = new EmailChannel
|
||||||
|
{
|
||||||
|
ServiceName = "TestService", EmailChannelType = EmailChannelType.Smtp,
|
||||||
|
Settings = new SmtpChannelSettings
|
||||||
|
{
|
||||||
|
Host = "smtp.example.invalid", Port = 587, Username = "test-user",
|
||||||
|
Password = "saved-test-secret", UseSsl = true,
|
||||||
|
FromEmail = "sender@example.invalid", FromName = "Original sender"
|
||||||
|
}
|
||||||
|
};
|
||||||
|
await scope.ServiceProvider.GetRequiredService<IEmailChannelRepository>().AddAsync(channel);
|
||||||
|
await scope.ServiceProvider.GetRequiredService<NotificationDbContext>().SaveChangesAsync();
|
||||||
|
return channel;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static async Task<SmtpChannelSettings> ReadSettings(DefinitionApiTestContext context, Guid id)
|
||||||
|
{
|
||||||
|
using var scope = context.App.Services.CreateScope();
|
||||||
|
var channel = await scope.ServiceProvider.GetRequiredService<IEmailChannelRepository>().GetByIdAsync(id);
|
||||||
|
return Assert.IsType<SmtpChannelSettings>(channel?.Settings);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,112 @@
|
|||||||
|
using System.Reflection;
|
||||||
|
using System.Text.Json;
|
||||||
|
using HrynCo.NotificationService.DAL.Abstract.Providers;
|
||||||
|
using HrynCo.NotificationService.Web.Controllers.Admin;
|
||||||
|
using MediatR;
|
||||||
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
|
||||||
|
namespace HrynCo.NotificationService.Web.IntegrationTests;
|
||||||
|
|
||||||
|
public sealed class AdminSmtpTestControllerTests
|
||||||
|
{
|
||||||
|
private static readonly Guid ChannelId = Guid.NewGuid();
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task BlankPasswordUsesSavedSecretAndCurrentUnsavedSender()
|
||||||
|
{
|
||||||
|
var (controller, recorder) = CreateController();
|
||||||
|
var response = await controller.TestSmtp(Request(), CancellationToken.None);
|
||||||
|
|
||||||
|
Assert.True(Succeeded(response));
|
||||||
|
Assert.Equal("saved-test-secret", recorder.SentCommand?.Password);
|
||||||
|
Assert.Equal("changed@example.invalid", recorder.SentCommand?.FromEmail);
|
||||||
|
Assert.Equal("Changed sender", recorder.SentCommand?.FromName);
|
||||||
|
Assert.Equal(1, recorder.LookupCount);
|
||||||
|
Assert.DoesNotContain("saved-test-secret", JsonSerializer.Serialize(((OkObjectResult)response).Value));
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task ExplicitPasswordOverridesSavedWithoutLookup()
|
||||||
|
{
|
||||||
|
var (controller, recorder) = CreateController();
|
||||||
|
var response = await controller.TestSmtp(Request() with { Password = "typed-test-secret" }, CancellationToken.None);
|
||||||
|
|
||||||
|
Assert.True(Succeeded(response));
|
||||||
|
Assert.Equal("typed-test-secret", recorder.SentCommand?.Password);
|
||||||
|
Assert.Equal(0, recorder.LookupCount);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Theory]
|
||||||
|
[InlineData("host")]
|
||||||
|
[InlineData("port")]
|
||||||
|
[InlineData("username")]
|
||||||
|
[InlineData("ssl")]
|
||||||
|
public async Task ChangedConnectionRequiresExplicitPassword(string field)
|
||||||
|
{
|
||||||
|
var (controller, recorder) = CreateController();
|
||||||
|
var request = field switch
|
||||||
|
{
|
||||||
|
"host" => Request() with { Host = "other.example.invalid" },
|
||||||
|
"port" => Request() with { Port = 2525 },
|
||||||
|
"username" => Request() with { Username = "other-user" },
|
||||||
|
_ => Request() with { UseSsl = false }
|
||||||
|
};
|
||||||
|
|
||||||
|
var response = await controller.TestSmtp(request, CancellationToken.None);
|
||||||
|
|
||||||
|
Assert.False(Succeeded(response));
|
||||||
|
Assert.Null(recorder.SentCommand);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task UnknownChannelDoesNotSend()
|
||||||
|
{
|
||||||
|
var (controller, recorder) = CreateController();
|
||||||
|
recorder.Channel = null;
|
||||||
|
|
||||||
|
var response = await controller.TestSmtp(Request(), CancellationToken.None);
|
||||||
|
|
||||||
|
Assert.False(Succeeded(response));
|
||||||
|
Assert.Null(recorder.SentCommand);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task NewChannelKeepsAdHocTestBehavior()
|
||||||
|
{
|
||||||
|
var (controller, recorder) = CreateController();
|
||||||
|
|
||||||
|
var response = await controller.TestSmtp(Request() with { ChannelId = null }, CancellationToken.None);
|
||||||
|
|
||||||
|
Assert.True(Succeeded(response));
|
||||||
|
Assert.Equal(string.Empty, recorder.SentCommand?.Password);
|
||||||
|
Assert.Equal(0, recorder.LookupCount);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static (AdminChannelsController Controller, RecordingAdminSmtpMediator Recorder) CreateController()
|
||||||
|
{
|
||||||
|
var mediator = DispatchProxy.Create<IMediator, RecordingAdminSmtpMediator>();
|
||||||
|
var recorder = (RecordingAdminSmtpMediator)mediator;
|
||||||
|
recorder.Channel = new EmailChannel
|
||||||
|
{
|
||||||
|
Id = ChannelId,
|
||||||
|
ServiceName = "TestService",
|
||||||
|
EmailChannelType = EmailChannelType.Smtp,
|
||||||
|
Settings = new SmtpChannelSettings
|
||||||
|
{
|
||||||
|
Host = "smtp.example.invalid", Port = 587,
|
||||||
|
Username = "test-user", Password = "saved-test-secret", UseSsl = true
|
||||||
|
}
|
||||||
|
};
|
||||||
|
return (new AdminChannelsController(mediator), recorder);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static TestSmtpRequest Request() => new(
|
||||||
|
"smtp.example.invalid", 587, "test-user", "", true,
|
||||||
|
"changed@example.invalid", "Changed sender", "recipient@example.invalid", ChannelId);
|
||||||
|
|
||||||
|
private static bool Succeeded(IActionResult response)
|
||||||
|
{
|
||||||
|
var body = JsonSerializer.Serialize(((OkObjectResult)response).Value);
|
||||||
|
return JsonDocument.Parse(body).RootElement.GetProperty("success").GetBoolean();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,14 @@
|
|||||||
|
namespace HrynCo.NotificationService.Web.IntegrationTests;
|
||||||
|
|
||||||
|
public sealed class AdminTemplatesIndexViewTests
|
||||||
|
{
|
||||||
|
[Fact]
|
||||||
|
public void CreateAndEditLinks_KeepTheirServiceAndKeyFilter()
|
||||||
|
{
|
||||||
|
string view = File.ReadAllText(Path.Combine(AppContext.BaseDirectory, "TestViews", "Index.cshtml"));
|
||||||
|
Assert.Contains("asp-route-serviceId=\"@serviceId\"", view);
|
||||||
|
Assert.Contains("asp-route-keyFilter=\"@keyFilter\"", view);
|
||||||
|
Assert.Contains("new { serviceId, definitionId = template.Id, keyFilter }", view);
|
||||||
|
Assert.DoesNotContain("/admin/templates", view);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,47 @@
|
|||||||
|
using System.Net.Http.Json;
|
||||||
|
using System.Text.Json;
|
||||||
|
using System.Text.Json.Nodes;
|
||||||
|
|
||||||
|
namespace HrynCo.NotificationService.Web.IntegrationTests;
|
||||||
|
|
||||||
|
public sealed class DefinitionApiTestContext : IAsyncDisposable
|
||||||
|
{
|
||||||
|
public required TemplateWebApplicationFactory App { get; init; }
|
||||||
|
public required HttpClient Client { get; init; }
|
||||||
|
public required Guid ServiceId { get; init; }
|
||||||
|
public string ServiceRoot => $"/api/v1/services/{ServiceId}";
|
||||||
|
|
||||||
|
public static async Task<DefinitionApiTestContext> CreateAsync(TemplateDatabaseFixture fixture)
|
||||||
|
{
|
||||||
|
var app = new TemplateWebApplicationFactory(await fixture.CreateDatabaseAsync());
|
||||||
|
var client = app.CreateClient();
|
||||||
|
var response = await client.GetFromJsonAsync<JsonElement>("/api/v1/services?name=TestService");
|
||||||
|
return new DefinitionApiTestContext { App = app, Client = client, ServiceId = response.GetProperty("data").GetProperty("id").GetGuid() };
|
||||||
|
}
|
||||||
|
|
||||||
|
public string DefinitionUrl(string key = "Welcome", bool upsert = false) =>
|
||||||
|
ServiceRoot + "/email-template-definitions" + (upsert ? "/upsert" : "") + "?key=" + Uri.EscapeDataString(key);
|
||||||
|
|
||||||
|
public Task<HttpResponseMessage> UpsertAsync(JsonNode body, string key = "Welcome") =>
|
||||||
|
Client.PostAsJsonAsync(DefinitionUrl(key, upsert: true), body);
|
||||||
|
|
||||||
|
public async Task<JsonElement> ReadAsync(string key = "Welcome") =>
|
||||||
|
(await Client.GetFromJsonAsync<JsonElement>(DefinitionUrl(key))).GetProperty("data");
|
||||||
|
|
||||||
|
public static JsonObject Body(Guid? revision, params string[] codes) => new()
|
||||||
|
{
|
||||||
|
["revision"] = revision?.ToString(),
|
||||||
|
["variables"] = new JsonArray(new JsonObject { ["name"] = "Name", ["required"] = true }),
|
||||||
|
["translations"] = new JsonArray(codes.Select(code => (JsonNode)new JsonObject
|
||||||
|
{
|
||||||
|
["languageCode"] = code, ["subject"] = "Subject " + code,
|
||||||
|
["htmlBody"] = "<p>Hello {{Name}}</p>", ["textBody"] = "Hello {{Name}}"
|
||||||
|
}).ToArray())
|
||||||
|
};
|
||||||
|
|
||||||
|
public async ValueTask DisposeAsync()
|
||||||
|
{
|
||||||
|
Client.Dispose();
|
||||||
|
await App.DisposeAsync();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,68 @@
|
|||||||
|
using System.Text.RegularExpressions;
|
||||||
|
using HrynCo.NotificationService.DAL.Abstract.Repositories;
|
||||||
|
using HrynCo.NotificationService.DAL.Abstract.Templates;
|
||||||
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
|
|
||||||
|
namespace HrynCo.NotificationService.Web.IntegrationTests;
|
||||||
|
|
||||||
|
public sealed class EditorLayoutTests(TemplateDatabaseFixture fixture) : IClassFixture<TemplateDatabaseFixture>
|
||||||
|
{
|
||||||
|
[Fact]
|
||||||
|
public async Task ServiceAndTemplateEditorsRenderOneBreadcrumbBeforeTheCard_IncludingValidationErrors()
|
||||||
|
{
|
||||||
|
var connection = await fixture.CreateDatabaseAsync();
|
||||||
|
await using var app = new TemplateWebApplicationFactory(connection);
|
||||||
|
using var client = app.CreateClient();
|
||||||
|
using var scope = app.Services.CreateScope();
|
||||||
|
var service = (await scope.ServiceProvider.GetRequiredService<INotificationServiceRepository>().GetAllAsync()).Single();
|
||||||
|
var definition = new EmailTemplateDefinition { ServiceName = service.Name, Key = "Layout" };
|
||||||
|
await scope.ServiceProvider.GetRequiredService<IEmailTemplateDefinitionRepository>().SaveDefinitionAsync(definition, true);
|
||||||
|
var root = $"/admin/services/{service.Id}";
|
||||||
|
var editors = new[]
|
||||||
|
{
|
||||||
|
(Url: "/admin/services/create", Title: "Create Service", Form: "serviceForm"),
|
||||||
|
(Url: root + "/edit", Title: "Edit Service", Form: "serviceForm"),
|
||||||
|
(Url: root + "/templates/create", Title: "Create Email Template", Form: "templateForm"),
|
||||||
|
(Url: root + "/templates/" + definition.Id, Title: "Edit Email Template", Form: "templateForm")
|
||||||
|
};
|
||||||
|
foreach (var editor in editors)
|
||||||
|
AssertEditorLayout(await client.GetStringAsync(editor.Url), editor.Title, editor.Form);
|
||||||
|
var fields = TemplateEditorTestFields.Read(await client.GetStringAsync(root + "/templates/create"));
|
||||||
|
fields["Key"] = "InvalidDraft";
|
||||||
|
fields["VariablesJson"] = "{invalid";
|
||||||
|
var errorHtml = await (await client.PostAsync(root + "/templates/save", new FormUrlEncodedContent(fields))).Content.ReadAsStringAsync();
|
||||||
|
Assert.Contains("Invalid variables JSON", errorHtml);
|
||||||
|
AssertEditorLayout(errorHtml, "Create Email Template", "templateForm");
|
||||||
|
Assert.Contains("<dl class=\"template-service-context\">", errorHtml);
|
||||||
|
Assert.Contains("<dt>Service</dt>", errorHtml);
|
||||||
|
Assert.Contains("<dd>TestService</dd>", errorHtml);
|
||||||
|
Assert.DoesNotContain("form-control-plaintext", errorHtml);
|
||||||
|
Assert.Equal(service.Name, TemplateEditorTestFields.Read(errorHtml)["ServiceName"]);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task ChannelEditorWorksWithoutTheOptionalBreadcrumbSection()
|
||||||
|
{
|
||||||
|
var connection = await fixture.CreateDatabaseAsync();
|
||||||
|
await using var app = new TemplateWebApplicationFactory(connection);
|
||||||
|
using var client = app.CreateClient();
|
||||||
|
var html = await client.GetStringAsync("/admin/channels/create");
|
||||||
|
Assert.Contains("<h1 class=\"editor-card-title\">", html);
|
||||||
|
Assert.Contains("<span>Create Channel</span>", html);
|
||||||
|
Assert.Contains("id=\"channelForm\"", html);
|
||||||
|
Assert.DoesNotContain("aria-label=\"Breadcrumb\"", html);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void AssertEditorLayout(string html, string title, string form)
|
||||||
|
{
|
||||||
|
Assert.Single(Regex.Matches(html, "aria-label=\"Breadcrumb\""));
|
||||||
|
var breadcrumb = html.IndexOf("<nav aria-label=\"Breadcrumb\"", StringComparison.Ordinal);
|
||||||
|
var card = html.IndexOf("<div class=\"card editor-card\">", StringComparison.Ordinal);
|
||||||
|
Assert.True(breadcrumb >= 0 && breadcrumb < card);
|
||||||
|
Assert.Single(Regex.Matches(html, "<h1 class=\"editor-card-title\">"));
|
||||||
|
Assert.Contains("aria-hidden=\"true\"", html);
|
||||||
|
Assert.Contains("<span>" + title + "</span>", html);
|
||||||
|
Assert.Contains("id=\"" + form + "\"", html);
|
||||||
|
Assert.Contains("aria-current=\"page\"", html);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,92 @@
|
|||||||
|
using System.Net;
|
||||||
|
using System.Net.Http.Json;
|
||||||
|
using System.Text.Json;
|
||||||
|
using System.Text.Json.Nodes;
|
||||||
|
using HrynCo.NotificationService.DAL.Abstract.Providers;
|
||||||
|
using HrynCo.NotificationService.DAL.Abstract.Repositories;
|
||||||
|
using HrynCo.NotificationService.DAL.EF;
|
||||||
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
|
|
||||||
|
namespace HrynCo.NotificationService.Web.IntegrationTests;
|
||||||
|
|
||||||
|
public sealed class EmailChannelApiTests(TemplateDatabaseFixture fixture) : IClassFixture<TemplateDatabaseFixture>
|
||||||
|
{
|
||||||
|
[Fact]
|
||||||
|
public async Task SmtpSettingsRoundTripThroughCreateReadUpdateListAndDelete()
|
||||||
|
{
|
||||||
|
await using var context = await DefinitionApiTestContext.CreateAsync(fixture);
|
||||||
|
var request = Content("smtp.example.invalid", 2525, "test-only-user");
|
||||||
|
var created = await context.Client.PostAsJsonAsync("/api/v1/email-channels", request);
|
||||||
|
Assert.Equal(HttpStatusCode.Created, created.StatusCode);
|
||||||
|
var id = (await created.Content.ReadFromJsonAsync<JsonElement>()).GetProperty("data").GetGuid();
|
||||||
|
var original = (await context.Client.GetFromJsonAsync<JsonElement>($"/api/v1/email-channels/{id}")).GetProperty("data");
|
||||||
|
Assert.Equal("smtp.example.invalid", original.GetProperty("settings").GetProperty("host").GetString());
|
||||||
|
Assert.Equal(2525, original.GetProperty("settings").GetProperty("port").GetInt32());
|
||||||
|
Assert.Equal("test-only-user", original.GetProperty("settings").GetProperty("username").GetString());
|
||||||
|
Assert.Equal(HttpStatusCode.OK, (await context.Client.PutAsJsonAsync($"/api/v1/email-channels/{id}", Content("updated.example.invalid", 2526, "updated-user"))).StatusCode);
|
||||||
|
var all = (await context.Client.GetFromJsonAsync<JsonElement>("/api/v1/email-channels/all")).GetProperty("data");
|
||||||
|
var updated = Assert.Single(all.EnumerateArray());
|
||||||
|
Assert.Equal("updated.example.invalid", updated.GetProperty("settings").GetProperty("host").GetString());
|
||||||
|
Assert.Equal("updated-user", updated.GetProperty("settings").GetProperty("username").GetString());
|
||||||
|
Assert.Equal(2526, updated.GetProperty("settings").GetProperty("port").GetInt32());
|
||||||
|
Assert.Equal("test-only-value", updated.GetProperty("settings").GetProperty("password").GetString());
|
||||||
|
Assert.False(updated.GetProperty("settings").GetProperty("useSsl").GetBoolean());
|
||||||
|
Assert.Equal("sender@example.invalid", updated.GetProperty("settings").GetProperty("fromEmail").GetString());
|
||||||
|
Assert.Equal("Test sender", updated.GetProperty("settings").GetProperty("fromName").GetString());
|
||||||
|
var filtered = await context.Client.GetFromJsonAsync<JsonElement>("/api/v1/email-channels?serviceName=IndependentChannelService");
|
||||||
|
Assert.Single(filtered.GetProperty("data").EnumerateArray());
|
||||||
|
Assert.Equal(HttpStatusCode.OK, (await context.Client.DeleteAsync($"/api/v1/email-channels/{id}")).StatusCode);
|
||||||
|
Assert.Equal(HttpStatusCode.NotFound, (await context.Client.GetAsync($"/api/v1/email-channels/{id}")).StatusCode);
|
||||||
|
Assert.Empty((await context.Client.GetFromJsonAsync<JsonElement>("/api/v1/email-channels/all")).GetProperty("data").EnumerateArray());
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task GetAllIncludesChannelsWhoseServiceNameHasNoServiceRecord()
|
||||||
|
{
|
||||||
|
await using var context = await DefinitionApiTestContext.CreateAsync(fixture);
|
||||||
|
using var scope = context.App.Services.CreateScope();
|
||||||
|
var channels = scope.ServiceProvider.GetRequiredService<IEmailChannelRepository>();
|
||||||
|
foreach (var serviceName in new[] { "TestService", "OrphanChannelService" })
|
||||||
|
await channels.AddAsync(new EmailChannel
|
||||||
|
{
|
||||||
|
ServiceName = serviceName, EmailChannelType = EmailChannelType.Smtp,
|
||||||
|
Settings = new SmtpChannelSettings { Host = "localhost", FromEmail = "test@example.invalid" }
|
||||||
|
});
|
||||||
|
await scope.ServiceProvider.GetRequiredService<NotificationDbContext>().SaveChangesAsync();
|
||||||
|
var all = (await context.Client.GetFromJsonAsync<JsonElement>("/api/v1/email-channels/all")).GetProperty("data");
|
||||||
|
Assert.Equal(2, all.GetArrayLength());
|
||||||
|
Assert.Contains(all.EnumerateArray(), c => c.GetProperty("serviceName").GetString() == "OrphanChannelService");
|
||||||
|
Assert.All(all.EnumerateArray(), c => Assert.NotEqual(Guid.Empty, c.GetProperty("id").GetGuid()));
|
||||||
|
}
|
||||||
|
|
||||||
|
[Theory]
|
||||||
|
[InlineData("nullSettings")]
|
||||||
|
[InlineData("invalidPort")]
|
||||||
|
[InlineData("blankHost")]
|
||||||
|
[InlineData("unsupportedType")]
|
||||||
|
[InlineData("blankService")]
|
||||||
|
public async Task InvalidSmtpCreateReturnsValidationEnvelopeWithoutPersisting(string scenario)
|
||||||
|
{
|
||||||
|
await using var context = await DefinitionApiTestContext.CreateAsync(fixture);
|
||||||
|
var body = JsonSerializer.SerializeToNode(Content("smtp.example.invalid", 2525, "test-user"), new JsonSerializerOptions(JsonSerializerDefaults.Web))!.AsObject();
|
||||||
|
switch (scenario)
|
||||||
|
{
|
||||||
|
case "nullSettings": body["settings"] = null; break;
|
||||||
|
case "invalidPort": body["settings"]!["port"] = 0; break;
|
||||||
|
case "blankHost": body["settings"]!["host"] = " "; break;
|
||||||
|
case "unsupportedType": body["channelType"] = 99; break;
|
||||||
|
case "blankService": body["serviceName"] = " "; break;
|
||||||
|
}
|
||||||
|
var response = await context.Client.PostAsJsonAsync("/api/v1/email-channels", body);
|
||||||
|
Assert.Equal(HttpStatusCode.BadRequest, response.StatusCode);
|
||||||
|
Assert.Equal("InvalidRequest", (await response.Content.ReadFromJsonAsync<JsonElement>()).GetProperty("error").GetProperty("code").GetString());
|
||||||
|
Assert.Empty((await context.Client.GetFromJsonAsync<JsonElement>("/api/v1/email-channels/all")).GetProperty("data").EnumerateArray());
|
||||||
|
}
|
||||||
|
|
||||||
|
private static object Content(string host, int port, string username) => new
|
||||||
|
{
|
||||||
|
serviceName = "IndependentChannelService", priority = 1, channelType = 1,
|
||||||
|
settings = new { host, port, username, password = "test-only-value", useSsl = false, fromEmail = "sender@example.invalid", fromName = "Test sender" },
|
||||||
|
dailyLimit = 100, monthlyLimit = 1000, warnThresholdPercent = 90, isActive = true
|
||||||
|
};
|
||||||
|
}
|
||||||
@@ -0,0 +1,212 @@
|
|||||||
|
using System.Net;
|
||||||
|
using System.Net.Http.Json;
|
||||||
|
using System.Text.Json;
|
||||||
|
using System.Text.RegularExpressions;
|
||||||
|
using HrynCo.NotificationService.DAL.Abstract.Repositories;
|
||||||
|
using HrynCo.NotificationService.DAL.Abstract.Templates;
|
||||||
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
|
using Microsoft.AspNetCore.TestHost;
|
||||||
|
|
||||||
|
namespace HrynCo.NotificationService.Web.IntegrationTests;
|
||||||
|
|
||||||
|
public sealed class EmailTemplateApiTests(TemplateDatabaseFixture fixture) : IClassFixture<TemplateDatabaseFixture>
|
||||||
|
{
|
||||||
|
[Fact]
|
||||||
|
public async Task LegacyApi_CreatesReadsUpdatesAndDeletesLanguageVersions_WithoutContractDivergence()
|
||||||
|
{
|
||||||
|
var connection = await fixture.CreateDatabaseAsync();
|
||||||
|
await using var app = new TemplateWebApplicationFactory(connection);
|
||||||
|
using var client = app.CreateClient();
|
||||||
|
var created = await client.PostAsJsonAsync("/api/v1/email-templates", Content("en"));
|
||||||
|
Assert.Equal(HttpStatusCode.Created, created.StatusCode);
|
||||||
|
var originalId = (await created.Content.ReadFromJsonAsync<JsonElement>()).GetProperty("data").GetGuid();
|
||||||
|
Assert.Equal(HttpStatusCode.Created, (await client.PostAsJsonAsync("/api/v1/email-templates", Content("uk"))).StatusCode);
|
||||||
|
Assert.Equal(HttpStatusCode.Conflict, (await client.PostAsJsonAsync("/api/v1/email-templates", Content("de", "Other"))).StatusCode);
|
||||||
|
Assert.Equal(HttpStatusCode.Conflict, (await client.PutAsJsonAsync("/api/v1/email-templates/TestService/Welcome/en", Content("en", "Other"))).StatusCode);
|
||||||
|
Assert.Equal(HttpStatusCode.OK, (await client.PutAsJsonAsync("/api/v1/email-templates/TestService/Welcome/uk", Content("uk", subject: "Updated uk"))).StatusCode);
|
||||||
|
var all = await client.GetFromJsonAsync<JsonElement>("/api/v1/email-templates?serviceName=TestService&key=Welcome");
|
||||||
|
Assert.Equal(2, all.GetProperty("data").GetArrayLength());
|
||||||
|
var en = await ReadAsync(client, "en");
|
||||||
|
Assert.Equal(originalId, en.GetProperty("id").GetGuid());
|
||||||
|
Assert.Equal("Subject en", en.GetProperty("subject").GetString());
|
||||||
|
Assert.Equal(HttpStatusCode.OK, (await client.DeleteAsync("/api/v1/email-templates/TestService/Welcome/uk")).StatusCode);
|
||||||
|
Assert.Equal(HttpStatusCode.OK, (await client.DeleteAsync("/api/v1/email-templates/TestService/Welcome/en")).StatusCode);
|
||||||
|
Assert.Contains("data-state=\"missing\"", await client.GetStringAsync("/admin/templates?serviceName=TestService"));
|
||||||
|
Assert.Equal(HttpStatusCode.NotFound, (await client.GetAsync("/api/v1/email-templates/TestService/Welcome/de")).StatusCode);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task ConcurrentLegacyCreates_OnlyPersistOneLanguage()
|
||||||
|
{
|
||||||
|
var connection = await fixture.CreateDatabaseAsync();
|
||||||
|
await using var app = new TemplateWebApplicationFactory(connection);
|
||||||
|
using var client = app.CreateClient();
|
||||||
|
await client.PostAsJsonAsync("/api/v1/email-templates", Content("en"));
|
||||||
|
var responses = await Task.WhenAll(
|
||||||
|
client.PostAsJsonAsync("/api/v1/email-templates", Content("uk")),
|
||||||
|
client.PostAsJsonAsync("/api/v1/email-templates", Content("uk")));
|
||||||
|
Assert.Equal(1, responses.Count(r => r.StatusCode == HttpStatusCode.Created));
|
||||||
|
Assert.Equal(1, responses.Count(r => r.StatusCode == HttpStatusCode.Conflict));
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task GroupEditor_SharedContractIsSavedForAllLanguages_AndPreservesContentIdsAndFilters()
|
||||||
|
{
|
||||||
|
var connection = await fixture.CreateDatabaseAsync();
|
||||||
|
await using var app = new TemplateWebApplicationFactory(connection);
|
||||||
|
using var client = app.CreateClient(new() { AllowAutoRedirect = false });
|
||||||
|
await client.PostAsJsonAsync("/api/v1/email-templates", Content("en"));
|
||||||
|
await client.PostAsJsonAsync("/api/v1/email-templates", Content("uk"));
|
||||||
|
var originalId = (await ReadAsync(client, "uk")).GetProperty("id").GetGuid();
|
||||||
|
var fields = await EditorFieldsAsync(client);
|
||||||
|
fields["VariablesJson"] = """[{"name":"Shared","required":true}]""";
|
||||||
|
fields["KeyFilter"] = "Welcome";
|
||||||
|
fields["Languages[0].Subject"] = "Edited English";
|
||||||
|
var response = await client.PostAsync(SaveUrl(fields), new FormUrlEncodedContent(fields));
|
||||||
|
Assert.Equal(HttpStatusCode.Redirect, response.StatusCode);
|
||||||
|
Assert.Equal($"/admin/services/{fields["ServiceId"]}?key=Welcome", response.Headers.Location!.OriginalString);
|
||||||
|
var uk = await ReadAsync(client, "uk");
|
||||||
|
Assert.Equal(originalId, uk.GetProperty("id").GetGuid());
|
||||||
|
Assert.Equal("Subject uk", uk.GetProperty("subject").GetString());
|
||||||
|
Assert.Equal("Shared", uk.GetProperty("variables")[0].GetProperty("name").GetString());
|
||||||
|
Assert.Equal("Edited English", (await ReadAsync(client, "en")).GetProperty("subject").GetString());
|
||||||
|
var html = await client.GetStringAsync($"/admin/services/{fields["ServiceId"]}?key=Welcome");
|
||||||
|
Assert.Single(Regex.Matches(html, "<td>Welcome</td>"));
|
||||||
|
Assert.Contains("data-language-code=\"en\" data-state=\"present\"", html);
|
||||||
|
Assert.Contains("data-language-code=\"uk\" data-state=\"present\"", html);
|
||||||
|
Assert.Contains("keyFilter=Welcome", html);
|
||||||
|
Assert.DoesNotContain("@filterQuery", html);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Theory]
|
||||||
|
[InlineData("{broken", "Invalid variables JSON")]
|
||||||
|
[InlineData("null", "Variables must be a JSON array")]
|
||||||
|
[InlineData("[{\"name\":\"Name\",\"required\":true},{\"name\":\"Name\",\"required\":false}]", "Variable names must be unique")]
|
||||||
|
public async Task InvalidSharedContract_KeepsEnteredValuesAndPersistsNothing(string json, string error)
|
||||||
|
{
|
||||||
|
var connection = await fixture.CreateDatabaseAsync();
|
||||||
|
await using var app = new TemplateWebApplicationFactory(connection);
|
||||||
|
using var client = app.CreateClient();
|
||||||
|
await client.PostAsJsonAsync("/api/v1/email-templates", Content("en"));
|
||||||
|
await client.PostAsJsonAsync("/api/v1/email-templates", Content("uk"));
|
||||||
|
var fields = await EditorFieldsAsync(client);
|
||||||
|
fields["VariablesJson"] = json;
|
||||||
|
fields["Languages[1].Subject"] = "Unsaved Ukrainian";
|
||||||
|
fields["ActiveLanguageIndex"] = "1";
|
||||||
|
var response = await client.PostAsync(SaveUrl(fields), new FormUrlEncodedContent(fields));
|
||||||
|
var html = await response.Content.ReadAsStringAsync();
|
||||||
|
Assert.Contains(error, html);
|
||||||
|
Assert.Contains("Unsaved Ukrainian", html);
|
||||||
|
Assert.Contains("name=\"ActiveLanguageIndex\" value=\"1\"", html);
|
||||||
|
Assert.Equal("Subject uk", (await ReadAsync(client, "uk")).GetProperty("subject").GetString());
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task DuplicateLanguageIncompleteContentAndStaleSave_AreRejectedAtomically()
|
||||||
|
{
|
||||||
|
var connection = await fixture.CreateDatabaseAsync();
|
||||||
|
await using var app = new TemplateWebApplicationFactory(connection);
|
||||||
|
using var client = app.CreateClient();
|
||||||
|
await client.PostAsJsonAsync("/api/v1/email-templates", Content("en"));
|
||||||
|
await client.PostAsJsonAsync("/api/v1/email-templates", Content("uk"));
|
||||||
|
var fields = await EditorFieldsAsync(client);
|
||||||
|
fields["Languages[1].LanguageCode"] = "en";
|
||||||
|
Assert.Contains("Language codes must be unique", await PostEditorAsync(client, fields));
|
||||||
|
fields["Languages[1].LanguageCode"] = "uk";
|
||||||
|
fields["Languages[1].TextBody"] = "";
|
||||||
|
Assert.Contains("TextBody field is required", await PostEditorAsync(client, fields));
|
||||||
|
fields["Languages[1].TextBody"] = "Restored text";
|
||||||
|
await client.PutAsJsonAsync("/api/v1/email-templates/TestService/Welcome/en", Content("en", subject: "Concurrent change"));
|
||||||
|
Assert.Contains("changed after you opened it", await PostEditorAsync(client, fields));
|
||||||
|
Assert.Equal("Concurrent change", (await ReadAsync(client, "en")).GetProperty("subject").GetString());
|
||||||
|
Assert.Equal("Subject uk", (await ReadAsync(client, "uk")).GetProperty("subject").GetString());
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task GroupEditor_RemovesOneLanguageAndAddsAnother_WithoutChangingRemainingLanguage()
|
||||||
|
{
|
||||||
|
var connection = await fixture.CreateDatabaseAsync();
|
||||||
|
await using var app = new TemplateWebApplicationFactory(connection);
|
||||||
|
using var client = app.CreateClient(new() { AllowAutoRedirect = false });
|
||||||
|
await client.PostAsJsonAsync("/api/v1/email-templates", Content("en"));
|
||||||
|
await client.PostAsJsonAsync("/api/v1/email-templates", Content("uk"));
|
||||||
|
var fields = await EditorFieldsAsync(client);
|
||||||
|
fields["Languages[1].Subject"] = "";
|
||||||
|
fields["Languages[1].HtmlBody"] = "";
|
||||||
|
fields["Languages[1].TextBody"] = "";
|
||||||
|
AddLanguageFields(fields, 2, "de");
|
||||||
|
fields["Languages[2].Subject"] = "German";
|
||||||
|
Assert.Equal(HttpStatusCode.Redirect, (await client.PostAsync(SaveUrl(fields), new FormUrlEncodedContent(fields))).StatusCode);
|
||||||
|
Assert.Equal(HttpStatusCode.NotFound, (await client.GetAsync("/api/v1/email-templates/TestService/Welcome/uk")).StatusCode);
|
||||||
|
Assert.Equal("Subject en", (await ReadAsync(client, "en")).GetProperty("subject").GetString());
|
||||||
|
Assert.Equal("German", (await ReadAsync(client, "de")).GetProperty("subject").GetString());
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task FailedSave_ShowsActionableErrorAndKeepsEnteredValues()
|
||||||
|
{
|
||||||
|
var connection = await fixture.CreateDatabaseAsync();
|
||||||
|
await using var app = new TemplateWebApplicationFactory(connection);
|
||||||
|
using var failingApp = app.WithWebHostBuilder(builder => builder.ConfigureTestServices(services =>
|
||||||
|
services.AddScoped<IEmailTemplateDefinitionRepository, FailingEmailTemplateDefinitionRepository>()));
|
||||||
|
using var client = failingApp.CreateClient();
|
||||||
|
await client.PostAsJsonAsync("/api/v1/email-templates", Content("en"));
|
||||||
|
await client.PostAsJsonAsync("/api/v1/email-templates", Content("uk"));
|
||||||
|
var fields = await EditorFieldsAsync(client);
|
||||||
|
fields["Languages[1].Subject"] = "Unsaved after failure";
|
||||||
|
var html = await PostEditorAsync(client, fields);
|
||||||
|
Assert.Contains("Could not save the template", html);
|
||||||
|
Assert.Contains("Unsaved after failure", html);
|
||||||
|
Assert.Equal("Subject uk", (await ReadAsync(client, "uk")).GetProperty("subject").GetString());
|
||||||
|
}
|
||||||
|
|
||||||
|
private static object Content(string language, string variable = "Name", string? subject = null) => new
|
||||||
|
{
|
||||||
|
serviceName = "TestService", key = "Welcome", languageCode = language,
|
||||||
|
subject = subject ?? "Subject " + language, htmlBody = "<p>Hello {{Name}}</p>", textBody = "Hello {{Name}}",
|
||||||
|
variables = new[] { new { name = variable, required = true } }
|
||||||
|
};
|
||||||
|
|
||||||
|
private static async Task<JsonElement> ReadAsync(HttpClient client, string language) =>
|
||||||
|
(await client.GetFromJsonAsync<JsonElement>("/api/v1/email-templates/TestService/Welcome/" + language)).GetProperty("data");
|
||||||
|
|
||||||
|
private static async Task<Dictionary<string, string>> EditorFieldsAsync(HttpClient client)
|
||||||
|
{
|
||||||
|
var response = await client.GetAsync("/admin/templates/TestService/Welcome/uk");
|
||||||
|
var html = response.Headers.Location is Uri location ? await client.GetStringAsync(location) : await response.Content.ReadAsStringAsync();
|
||||||
|
var fields = new Dictionary<string, string>();
|
||||||
|
foreach (Match input in Regex.Matches(html, "<input[^>]+>"))
|
||||||
|
{
|
||||||
|
var name = Regex.Match(input.Value, "name=\"([^\"]+)\"");
|
||||||
|
var value = Regex.Match(input.Value, "value=\"([^\"]*)\"");
|
||||||
|
if (name.Success && value.Success)
|
||||||
|
fields[WebUtility.HtmlDecode(name.Groups[1].Value)] = WebUtility.HtmlDecode(value.Groups[1].Value);
|
||||||
|
}
|
||||||
|
foreach (var codeField in fields.Keys.Where(k => k.EndsWith(".LanguageCode")).ToList())
|
||||||
|
{
|
||||||
|
var prefix = codeField[..^"LanguageCode".Length];
|
||||||
|
fields.TryAdd(prefix + "Subject", "");
|
||||||
|
fields.TryAdd(prefix + "HtmlBody", "");
|
||||||
|
fields.TryAdd(prefix + "TextBody", "");
|
||||||
|
}
|
||||||
|
fields["ServiceName"] = "TestService";
|
||||||
|
fields["Key"] = "Welcome";
|
||||||
|
fields["VariablesJson"] = """[{"name":"Name","required":true}]""";
|
||||||
|
AddLanguageFields(fields, 0, "en");
|
||||||
|
AddLanguageFields(fields, 1, "uk");
|
||||||
|
return fields;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void AddLanguageFields(Dictionary<string, string> fields, int index, string language)
|
||||||
|
{
|
||||||
|
fields[$"Languages[{index}].LanguageCode"] = language;
|
||||||
|
fields[$"Languages[{index}].Subject"] = "Subject " + language;
|
||||||
|
fields[$"Languages[{index}].HtmlBody"] = "<p>Hello {{Name}}</p>";
|
||||||
|
fields[$"Languages[{index}].TextBody"] = "Hello {{Name}}";
|
||||||
|
}
|
||||||
|
|
||||||
|
private static string SaveUrl(Dictionary<string, string> fields) => $"/admin/services/{fields["ServiceId"]}/templates/save";
|
||||||
|
|
||||||
|
private static async Task<string> PostEditorAsync(HttpClient client, Dictionary<string, string> fields) =>
|
||||||
|
await (await client.PostAsync(SaveUrl(fields), new FormUrlEncodedContent(fields))).Content.ReadAsStringAsync();
|
||||||
|
}
|
||||||
@@ -0,0 +1,228 @@
|
|||||||
|
using System.Net;
|
||||||
|
using System.Net.Http.Json;
|
||||||
|
using System.Text.Json;
|
||||||
|
using System.Text.Json.Nodes;
|
||||||
|
using HrynCo.NotificationService.DAL.Abstract.Repositories;
|
||||||
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
|
|
||||||
|
namespace HrynCo.NotificationService.Web.IntegrationTests;
|
||||||
|
|
||||||
|
public sealed class EmailTemplateDefinitionApiTests(TemplateDatabaseFixture fixture) : IClassFixture<TemplateDatabaseFixture>
|
||||||
|
{
|
||||||
|
[Fact]
|
||||||
|
public async Task ServiceReadsExposeExactCatalogAndStableIdentityThroughRename()
|
||||||
|
{
|
||||||
|
await using var context = await DefinitionApiTestContext.CreateAsync(fixture);
|
||||||
|
var service = (await context.Client.GetFromJsonAsync<JsonElement>(context.ServiceRoot)).GetProperty("data");
|
||||||
|
Assert.Equal(context.ServiceId, service.GetProperty("id").GetGuid());
|
||||||
|
Assert.Equal("TestService", service.GetProperty("name").GetString());
|
||||||
|
Assert.Equal(new[] { "de", "en", "ru", "ru-RU", "uk" }, service.GetProperty("supportedLanguageCodes").EnumerateArray().Select(c => c.GetString()));
|
||||||
|
Assert.Equal(5, service.EnumerateObject().Count());
|
||||||
|
Assert.NotEqual(Guid.Empty, service.GetProperty("revision").GetGuid());
|
||||||
|
Assert.Equal(HttpStatusCode.NotFound, (await context.Client.GetAsync("/api/v1/services?name=testservice")).StatusCode);
|
||||||
|
Assert.Equal(HttpStatusCode.BadRequest, (await context.Client.GetAsync("/api/v1/services")).StatusCode);
|
||||||
|
Assert.Equal(HttpStatusCode.NotFound, (await context.Client.GetAsync($"/api/v1/services/{Guid.NewGuid()}")).StatusCode);
|
||||||
|
using var scope = context.App.Services.CreateScope();
|
||||||
|
var repository = scope.ServiceProvider.GetRequiredService<INotificationServiceRepository>();
|
||||||
|
var model = (await repository.GetAsync(context.ServiceId))!;
|
||||||
|
model.Name = "Renamed Service";
|
||||||
|
await repository.SaveAsync(model, false);
|
||||||
|
Assert.Equal(HttpStatusCode.NotFound, (await context.Client.GetAsync("/api/v1/services?name=TestService")).StatusCode);
|
||||||
|
var renamed = await context.Client.GetFromJsonAsync<JsonElement>("/api/v1/services?name=Renamed%20Service");
|
||||||
|
Assert.Equal(context.ServiceId, renamed.GetProperty("data").GetProperty("id").GetGuid());
|
||||||
|
Assert.Equal("Renamed Service", (await context.Client.GetFromJsonAsync<JsonElement>(context.ServiceRoot)).GetProperty("data").GetProperty("name").GetString());
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task AtomicUpsertReplacesContractAndSentContent_AndRetainsOmittedTranslations()
|
||||||
|
{
|
||||||
|
await using var context = await DefinitionApiTestContext.CreateAsync(fixture);
|
||||||
|
Assert.Equal(HttpStatusCode.Created, (await context.UpsertAsync(DefinitionApiTestContext.Body(null, "en", "uk"))).StatusCode);
|
||||||
|
var original = await context.ReadAsync();
|
||||||
|
var uk = original.GetProperty("translations")[1];
|
||||||
|
Assert.True(uk.GetProperty("created").GetDateTimeOffset().Year >= 2020);
|
||||||
|
var revision = original.GetProperty("revision").GetGuid();
|
||||||
|
var update = DefinitionApiTestContext.Body(revision, "en", "de");
|
||||||
|
update["variables"] = new JsonArray(new JsonObject { ["name"] = "Changed", ["required"] = false });
|
||||||
|
update["translations"]![0]!["subject"] = "Edited English";
|
||||||
|
var response = await context.UpsertAsync(update);
|
||||||
|
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
|
||||||
|
var returned = (await response.Content.ReadFromJsonAsync<JsonElement>()).GetProperty("data");
|
||||||
|
var saved = await context.ReadAsync();
|
||||||
|
Assert.Equal(saved.GetProperty("revision").GetGuid(), returned.GetProperty("revision").GetGuid());
|
||||||
|
Assert.NotEqual(revision, saved.GetProperty("revision").GetGuid());
|
||||||
|
Assert.Equal(original.GetProperty("id").GetGuid(), saved.GetProperty("id").GetGuid());
|
||||||
|
Assert.Equal(3, saved.GetProperty("translations").GetArrayLength());
|
||||||
|
Assert.Equal(uk.GetRawText(), saved.GetProperty("translations")[2].GetRawText());
|
||||||
|
Assert.Equal("Edited English", saved.GetProperty("translations")[1].GetProperty("subject").GetString());
|
||||||
|
Assert.Equal(original.GetProperty("translations")[0].GetProperty("id").GetGuid(), saved.GetProperty("translations")[1].GetProperty("id").GetGuid());
|
||||||
|
var legacy = await context.Client.GetFromJsonAsync<JsonElement>("/api/v1/email-templates/TestService/Welcome/uk");
|
||||||
|
Assert.Equal("Changed", legacy.GetProperty("data").GetProperty("variables")[0].GetProperty("name").GetString());
|
||||||
|
Assert.False(saved.GetProperty("variables")[0].GetProperty("required").GetBoolean());
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task ExplicitRemovalCanRemoveLastVersion_AndKeepDefinitionAndContract()
|
||||||
|
{
|
||||||
|
await using var context = await DefinitionApiTestContext.CreateAsync(fixture);
|
||||||
|
await context.UpsertAsync(DefinitionApiTestContext.Body(null, "en", "uk"));
|
||||||
|
var before = await context.ReadAsync();
|
||||||
|
var request = DefinitionApiTestContext.Body(before.GetProperty("revision").GetGuid());
|
||||||
|
request["removeLanguageCodes"] = new JsonArray("en", "uk", "de");
|
||||||
|
var response = await context.UpsertAsync(request);
|
||||||
|
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
|
||||||
|
var returned = (await response.Content.ReadFromJsonAsync<JsonElement>()).GetProperty("data");
|
||||||
|
Assert.Empty(returned.GetProperty("translations").EnumerateArray());
|
||||||
|
var saved = await context.ReadAsync();
|
||||||
|
Assert.Equal(before.GetProperty("id").GetGuid(), saved.GetProperty("id").GetGuid());
|
||||||
|
Assert.Empty(saved.GetProperty("translations").EnumerateArray());
|
||||||
|
Assert.Single(saved.GetProperty("variables").EnumerateArray());
|
||||||
|
Assert.Equal(HttpStatusCode.NotFound, (await context.Client.GetAsync("/api/v1/email-templates/TestService/Welcome/en")).StatusCode);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task RevisionsEnforceCreateOnlyAndUpdateOnly_WithoutPartialChanges()
|
||||||
|
{
|
||||||
|
await using var context = await DefinitionApiTestContext.CreateAsync(fixture);
|
||||||
|
await context.UpsertAsync(DefinitionApiTestContext.Body(null));
|
||||||
|
var before = await context.ReadAsync();
|
||||||
|
Assert.Equal(HttpStatusCode.Conflict, (await context.UpsertAsync(DefinitionApiTestContext.Body(null, "en"))).StatusCode);
|
||||||
|
Assert.Equal(HttpStatusCode.Conflict, (await context.UpsertAsync(DefinitionApiTestContext.Body(Guid.NewGuid(), "en"))).StatusCode);
|
||||||
|
Assert.Equal(HttpStatusCode.Conflict, (await context.UpsertAsync(DefinitionApiTestContext.Body(Guid.NewGuid(), "en"), "Missing")).StatusCode);
|
||||||
|
Assert.Equal(HttpStatusCode.NotFound, (await context.Client.GetAsync(context.DefinitionUrl("Missing"))).StatusCode);
|
||||||
|
Assert.Equal(HttpStatusCode.BadRequest, (await context.UpsertAsync(DefinitionApiTestContext.Body(Guid.Empty))).StatusCode);
|
||||||
|
Assert.Equal(before.GetRawText(), (await context.ReadAsync()).GetRawText());
|
||||||
|
var revision = before.GetProperty("revision").GetGuid();
|
||||||
|
Assert.Equal(HttpStatusCode.OK, (await context.UpsertAsync(DefinitionApiTestContext.Body(revision))).StatusCode);
|
||||||
|
Assert.Equal(HttpStatusCode.Conflict, (await context.UpsertAsync(DefinitionApiTestContext.Body(revision, "uk"))).StatusCode);
|
||||||
|
var otherRoot = $"/api/v1/services/{Guid.NewGuid()}/email-template-definitions/upsert?key=Welcome";
|
||||||
|
Assert.Equal(HttpStatusCode.NotFound, (await context.Client.PostAsJsonAsync(otherRoot, DefinitionApiTestContext.Body(null))).StatusCode);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Theory]
|
||||||
|
[InlineData("missingVariables")]
|
||||||
|
[InlineData("nullVariables")]
|
||||||
|
[InlineData("missingTranslations")]
|
||||||
|
[InlineData("nullTranslations")]
|
||||||
|
[InlineData("nullVariable")]
|
||||||
|
[InlineData("nullTranslation")]
|
||||||
|
[InlineData("missingRequired")]
|
||||||
|
[InlineData("nonBooleanRequired")]
|
||||||
|
[InlineData("duplicateVariable")]
|
||||||
|
[InlineData("blankVariable")]
|
||||||
|
[InlineData("duplicateLanguage")]
|
||||||
|
[InlineData("unknownLanguage")]
|
||||||
|
[InlineData("blankContent")]
|
||||||
|
[InlineData("missingContent")]
|
||||||
|
[InlineData("duplicateRemoval")]
|
||||||
|
[InlineData("unknownRemoval")]
|
||||||
|
[InlineData("nullRemoval")]
|
||||||
|
[InlineData("overlap")]
|
||||||
|
public async Task InvalidUpdateDoesNotChangeContractTranslationsOrRevision(string scenario)
|
||||||
|
{
|
||||||
|
await using var context = await DefinitionApiTestContext.CreateAsync(fixture);
|
||||||
|
await context.UpsertAsync(DefinitionApiTestContext.Body(null, "en", "uk"));
|
||||||
|
var before = await context.ReadAsync();
|
||||||
|
var request = InvalidDefinitionApiBody.Create(scenario, before.GetProperty("revision").GetGuid());
|
||||||
|
var response = await context.UpsertAsync(request);
|
||||||
|
Assert.Equal(HttpStatusCode.BadRequest, response.StatusCode);
|
||||||
|
var error = await response.Content.ReadFromJsonAsync<JsonElement>();
|
||||||
|
Assert.False(error.GetProperty("success").GetBoolean());
|
||||||
|
Assert.Equal("InvalidRequest", error.GetProperty("error").GetProperty("code").GetString());
|
||||||
|
Assert.Equal(before.GetRawText(), (await context.ReadAsync()).GetRawText());
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task ConcurrentCreatesAndUpdatesHaveOneWinnerAndNoLostMerge()
|
||||||
|
{
|
||||||
|
await using var context = await DefinitionApiTestContext.CreateAsync(fixture);
|
||||||
|
var created = await Task.WhenAll(context.UpsertAsync(DefinitionApiTestContext.Body(null, "en")), context.UpsertAsync(DefinitionApiTestContext.Body(null, "uk")));
|
||||||
|
Assert.Equal(new[] { HttpStatusCode.Created, HttpStatusCode.Conflict }, created.Select(r => r.StatusCode).Order().ToArray());
|
||||||
|
var before = await context.ReadAsync();
|
||||||
|
var revision = before.GetProperty("revision").GetGuid();
|
||||||
|
var responses = await Task.WhenAll(context.UpsertAsync(DefinitionApiTestContext.Body(revision, "de")), context.UpsertAsync(DefinitionApiTestContext.Body(revision, "ru")));
|
||||||
|
Assert.Equal(new[] { HttpStatusCode.OK, HttpStatusCode.Conflict }, responses.Select(r => r.StatusCode).Order().ToArray());
|
||||||
|
var after = await context.ReadAsync();
|
||||||
|
Assert.Equal(2, after.GetProperty("translations").GetArrayLength());
|
||||||
|
var retained = before.GetProperty("translations")[0];
|
||||||
|
Assert.Contains(after.GetProperty("translations").EnumerateArray(), item => item.GetProperty("id").GetGuid() == retained.GetProperty("id").GetGuid());
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task RejectedCreateAndMalformedRequestsLeaveNoDefinition()
|
||||||
|
{
|
||||||
|
await using var context = await DefinitionApiTestContext.CreateAsync(fixture);
|
||||||
|
var definitions = context.ServiceRoot + "/email-template-definitions";
|
||||||
|
Assert.Equal(HttpStatusCode.BadRequest, (await context.Client.GetAsync(definitions)).StatusCode);
|
||||||
|
Assert.Equal(HttpStatusCode.BadRequest, (await context.Client.PostAsJsonAsync(definitions + "/upsert", DefinitionApiTestContext.Body(null))).StatusCode);
|
||||||
|
Assert.Equal(HttpStatusCode.BadRequest, (await context.Client.GetAsync(definitions + "?key=%20")).StatusCode);
|
||||||
|
var invalid = DefinitionApiTestContext.Body(null, "en", "unknown");
|
||||||
|
Assert.Equal(HttpStatusCode.BadRequest, (await context.UpsertAsync(invalid)).StatusCode);
|
||||||
|
Assert.Equal(HttpStatusCode.NotFound, (await context.Client.GetAsync(context.DefinitionUrl())).StatusCode);
|
||||||
|
using var nullBody = new StringContent("null", System.Text.Encoding.UTF8, "application/json");
|
||||||
|
var response = await context.Client.PostAsync(context.DefinitionUrl(upsert: true), nullBody);
|
||||||
|
Assert.Equal(HttpStatusCode.BadRequest, response.StatusCode);
|
||||||
|
Assert.Equal("InvalidRequest", (await response.Content.ReadFromJsonAsync<JsonElement>()).GetProperty("error").GetProperty("code").GetString());
|
||||||
|
var invalidRevision = DefinitionApiTestContext.Body(null);
|
||||||
|
invalidRevision["revision"] = "not-a-guid";
|
||||||
|
Assert.Equal(HttpStatusCode.BadRequest, (await context.UpsertAsync(invalidRevision)).StatusCode);
|
||||||
|
Assert.Equal(HttpStatusCode.NotFound, (await context.Client.GetAsync(context.DefinitionUrl())).StatusCode);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task ExactCaseCatalogCodesCanCoexistAndBeRemovedIndependently()
|
||||||
|
{
|
||||||
|
await using var context = await DefinitionApiTestContext.CreateAsync(fixture);
|
||||||
|
using var scope = context.App.Services.CreateScope();
|
||||||
|
var services = scope.ServiceProvider.GetRequiredService<INotificationServiceRepository>();
|
||||||
|
var service = (await services.GetAsync(context.ServiceId))!;
|
||||||
|
service.Languages = service.Languages.Append(new() { Code = "RU" }).ToList();
|
||||||
|
await services.SaveAsync(service, false);
|
||||||
|
Assert.Equal(HttpStatusCode.Created, (await context.UpsertAsync(DefinitionApiTestContext.Body(null, "RU", "ru", "ru-RU"))).StatusCode);
|
||||||
|
var saved = await context.ReadAsync();
|
||||||
|
var removal = DefinitionApiTestContext.Body(saved.GetProperty("revision").GetGuid());
|
||||||
|
removal["variables"] = new JsonArray();
|
||||||
|
removal["removeLanguageCodes"] = new JsonArray("RU");
|
||||||
|
Assert.Equal(HttpStatusCode.OK, (await context.UpsertAsync(removal)).StatusCode);
|
||||||
|
var after = await context.ReadAsync();
|
||||||
|
Assert.Empty(after.GetProperty("variables").EnumerateArray());
|
||||||
|
Assert.Equal(new[] { "ru", "ru-RU" }, after.GetProperty("translations").EnumerateArray().Select(t => t.GetProperty("languageCode").GetString()));
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task LegacyContentWriteInvalidatesLastReadRevision()
|
||||||
|
{
|
||||||
|
await using var context = await DefinitionApiTestContext.CreateAsync(fixture);
|
||||||
|
await context.UpsertAsync(DefinitionApiTestContext.Body(null, "en"));
|
||||||
|
var before = await context.ReadAsync();
|
||||||
|
var body = DefinitionApiTestContext.Body(null, "en");
|
||||||
|
var legacy = (JsonObject)body["translations"]![0]!.DeepClone();
|
||||||
|
legacy["subject"] = "Legacy edit";
|
||||||
|
legacy["variables"] = body["variables"]!.DeepClone();
|
||||||
|
Assert.Equal(HttpStatusCode.OK, (await context.Client.PutAsJsonAsync("/api/v1/email-templates/TestService/Welcome/en", legacy)).StatusCode);
|
||||||
|
Assert.Equal(HttpStatusCode.Conflict, (await context.UpsertAsync(DefinitionApiTestContext.Body(before.GetProperty("revision").GetGuid(), "uk"))).StatusCode);
|
||||||
|
var after = await context.ReadAsync();
|
||||||
|
Assert.Single(after.GetProperty("translations").EnumerateArray());
|
||||||
|
Assert.Equal("Legacy edit", after.GetProperty("translations")[0].GetProperty("subject").GetString());
|
||||||
|
}
|
||||||
|
|
||||||
|
[Theory]
|
||||||
|
[InlineData("Welcome + % # ? Привіт")]
|
||||||
|
[InlineData("Welcome%2Fexact")]
|
||||||
|
[InlineData("Welcome/child")]
|
||||||
|
public async Task EncodedKeysRoundTripExactly(string key)
|
||||||
|
{
|
||||||
|
await using var context = await DefinitionApiTestContext.CreateAsync(fixture);
|
||||||
|
var created = await context.UpsertAsync(DefinitionApiTestContext.Body(null, "ru", "ru-RU"), key);
|
||||||
|
Assert.Equal(HttpStatusCode.Created, created.StatusCode);
|
||||||
|
Assert.NotNull(created.Headers.Location);
|
||||||
|
var located = await context.Client.GetFromJsonAsync<JsonElement>(created.Headers.Location);
|
||||||
|
Assert.Equal(key, located.GetProperty("data").GetProperty("key").GetString());
|
||||||
|
Assert.Equal(context.ServiceId, located.GetProperty("data").GetProperty("serviceId").GetGuid());
|
||||||
|
var read = await context.ReadAsync(key);
|
||||||
|
Assert.Equal(read.GetProperty("id").GetGuid(), located.GetProperty("data").GetProperty("id").GetGuid());
|
||||||
|
Assert.Equal(key, read.GetProperty("key").GetString());
|
||||||
|
Assert.Equal(new[] { "ru", "ru-RU" }, read.GetProperty("translations").EnumerateArray().Select(t => t.GetProperty("languageCode").GetString()));
|
||||||
|
Assert.Equal(HttpStatusCode.NotFound, (await context.Client.GetAsync(context.DefinitionUrl(key.ToLowerInvariant()))).StatusCode);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,106 @@
|
|||||||
|
using HrynCo.NotificationService.DAL.Abstract.Repositories;
|
||||||
|
using HrynCo.NotificationService.DAL.EF;
|
||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
using Microsoft.EntityFrameworkCore.Infrastructure;
|
||||||
|
using Microsoft.EntityFrameworkCore.Migrations;
|
||||||
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
|
using Npgsql;
|
||||||
|
|
||||||
|
namespace HrynCo.NotificationService.Web.IntegrationTests;
|
||||||
|
|
||||||
|
public sealed class EmailTemplateMigrationTests(TemplateDatabaseFixture fixture) : IClassFixture<TemplateDatabaseFixture>
|
||||||
|
{
|
||||||
|
[Fact]
|
||||||
|
public async Task EquivalentContracts_PreserveLanguageIdsContentAndSharedVariables_AndRerunSafely()
|
||||||
|
{
|
||||||
|
var connection = await fixture.CreateDatabaseAsync(legacy: true);
|
||||||
|
var en = await TemplateDatabaseFixture.InsertLegacyAsync(connection, "en", """[{"name":"Name","required":true},{"name":"Link","required":false}]""");
|
||||||
|
var uk = await TemplateDatabaseFixture.InsertLegacyAsync(connection, "UK", """[{"required":false,"name":"Link"},{"required":true,"name":"Name"}]""");
|
||||||
|
await MigrateAsync(connection);
|
||||||
|
await MigrateAsync(connection);
|
||||||
|
using var services = new ServiceCollection().AddNotificationDataAccess(connection).BuildServiceProvider();
|
||||||
|
using var scope = services.CreateScope();
|
||||||
|
var repository = scope.ServiceProvider.GetRequiredService<IEmailTemplateRepository>();
|
||||||
|
var templates = await repository.GetAllAsync("TestService", "Welcome");
|
||||||
|
Assert.Equal(2, templates.Count);
|
||||||
|
Assert.Contains(templates, t => t.Id == en && t.Subject == "Subject en" && t.HtmlBody == "<p>{{Name}}</p>");
|
||||||
|
Assert.Contains(templates, t => t.Id == uk && t.LanguageCode == "UK" && t.Subject == "Subject UK");
|
||||||
|
Assert.All(templates, t => Assert.Equal(2, t.Variables.Count));
|
||||||
|
await using var db = TemplateDatabaseFixture.CreateContext(connection);
|
||||||
|
await db.GetService<IMigrator>().MigrateAsync(TemplateDatabaseFixture.LegacyMigration);
|
||||||
|
await db.Database.MigrateAsync();
|
||||||
|
Assert.Equal(en, (await repository.GetAsync("TestService", "Welcome", "en"))!.Id);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Theory]
|
||||||
|
[InlineData("[{\"name\":\"Other\",\"required\":true}]", "Conflicting shared variables")]
|
||||||
|
[InlineData("{}", "Invalid template identity or variables array")]
|
||||||
|
[InlineData("[null]", "Invalid variables shape")]
|
||||||
|
[InlineData("[{\"name\":\"Name\",\"required\":\"yes\"}]", "Invalid variables shape")]
|
||||||
|
[InlineData("[{\"name\":\"Name\",\"required\":true},{\"name\":\"Name\",\"required\":true}]", "Duplicate variable names")]
|
||||||
|
public async Task InvalidLegacyData_StopsWithoutChangingSource_ThenCanRecover(string variables, string diagnostic)
|
||||||
|
{
|
||||||
|
var connection = await fixture.CreateDatabaseAsync(legacy: true);
|
||||||
|
await TemplateDatabaseFixture.InsertLegacyAsync(connection, "en", """[{"name":"Name","required":true}]""");
|
||||||
|
await TemplateDatabaseFixture.InsertLegacyAsync(connection, "uk", variables);
|
||||||
|
var error = await Assert.ThrowsAsync<PostgresException>(() => MigrateAsync(connection));
|
||||||
|
Assert.Contains(diagnostic, error.MessageText);
|
||||||
|
Assert.Contains("TestService", error.MessageText);
|
||||||
|
Assert.DoesNotContain("Hello", error.MessageText);
|
||||||
|
await TemplateDatabaseFixture.ExecuteAsync(connection, """
|
||||||
|
DO $$ BEGIN
|
||||||
|
IF to_regclass('public.services') IS NOT NULL OR (SELECT count(*) FROM email_templates) <> 2 THEN
|
||||||
|
RAISE EXCEPTION 'Migration partially changed source';
|
||||||
|
END IF;
|
||||||
|
END $$;
|
||||||
|
UPDATE email_templates SET variables = '[{"name":"Name","required":true}]'::jsonb;
|
||||||
|
""");
|
||||||
|
await MigrateAsync(connection);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task ExactLanguageCodes_RemainDistinctAfterMigration()
|
||||||
|
{
|
||||||
|
var connection = await fixture.CreateDatabaseAsync(legacy: true);
|
||||||
|
await TemplateDatabaseFixture.InsertLegacyAsync(connection, "ru", "[]");
|
||||||
|
await TemplateDatabaseFixture.InsertLegacyAsync(connection, "ru-RU", "[]");
|
||||||
|
await TemplateDatabaseFixture.InsertLegacyAsync(connection, "EN", "[]");
|
||||||
|
await TemplateDatabaseFixture.InsertLegacyAsync(connection, "en", "[]");
|
||||||
|
await MigrateAsync(connection);
|
||||||
|
await TemplateDatabaseFixture.ExecuteAsync(connection, """
|
||||||
|
DO $$ BEGIN
|
||||||
|
IF (SELECT count(*) FROM service_languages) <> 4
|
||||||
|
OR NOT EXISTS (SELECT 1 FROM service_languages WHERE code = 'ru-RU')
|
||||||
|
OR NOT EXISTS (SELECT 1 FROM service_languages WHERE code = 'EN') THEN
|
||||||
|
RAISE EXCEPTION 'Migration changed exact language codes';
|
||||||
|
END IF;
|
||||||
|
END $$;
|
||||||
|
""");
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task InterruptedConversion_RollsBackAndCanBeRetried()
|
||||||
|
{
|
||||||
|
var connectionString = await fixture.CreateDatabaseAsync(legacy: true);
|
||||||
|
await TemplateDatabaseFixture.InsertLegacyAsync(connectionString, "en", "[]");
|
||||||
|
await using var db = TemplateDatabaseFixture.CreateContext(connectionString);
|
||||||
|
var script = db.GetService<IMigrator>().GenerateScript(TemplateDatabaseFixture.LegacyMigration, options: MigrationsSqlGenerationOptions.NoTransactions);
|
||||||
|
script = script.Replace("ALTER TABLE email_templates DROP COLUMN", "PERFORM pg_sleep(2); ALTER TABLE email_templates DROP COLUMN");
|
||||||
|
await using (var connection = new NpgsqlConnection(connectionString))
|
||||||
|
{
|
||||||
|
await connection.OpenAsync();
|
||||||
|
await using var transaction = await connection.BeginTransactionAsync();
|
||||||
|
await using var command = new NpgsqlCommand("SET LOCAL statement_timeout = '500ms'; " + script, connection, transaction);
|
||||||
|
var error = await Assert.ThrowsAsync<PostgresException>(() => command.ExecuteNonQueryAsync());
|
||||||
|
Assert.Equal("57014", error.SqlState);
|
||||||
|
await transaction.RollbackAsync();
|
||||||
|
}
|
||||||
|
await MigrateAsync(connectionString);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static async Task MigrateAsync(string connectionString)
|
||||||
|
{
|
||||||
|
await using var db = TemplateDatabaseFixture.CreateContext(connectionString);
|
||||||
|
await db.Database.MigrateAsync();
|
||||||
|
}
|
||||||
|
}
|
||||||
+28
@@ -0,0 +1,28 @@
|
|||||||
|
using HrynCo.NotificationService.DAL.Abstract.Repositories;
|
||||||
|
using HrynCo.NotificationService.DAL.Abstract.Templates;
|
||||||
|
|
||||||
|
namespace HrynCo.NotificationService.Web.IntegrationTests;
|
||||||
|
|
||||||
|
public sealed class FailingEmailTemplateDefinitionRepository(IEmailTemplateRepository inner) : IEmailTemplateDefinitionRepository
|
||||||
|
{
|
||||||
|
public Task<IReadOnlyList<EmailTemplateDefinition>> GetDefinitionsAsync(string? serviceName = null, string? key = null, CancellationToken ct = default) =>
|
||||||
|
((IEmailTemplateDefinitionRepository)inner).GetDefinitionsAsync(serviceName, key, ct);
|
||||||
|
|
||||||
|
public Task<EmailTemplateDefinition?> GetDefinitionAsync(string serviceName, string key, CancellationToken ct = default) =>
|
||||||
|
((IEmailTemplateDefinitionRepository)inner).GetDefinitionAsync(serviceName, key, ct);
|
||||||
|
|
||||||
|
public Task<EmailTemplateDefinition?> GetDefinitionAsync(Guid serviceId, string key, CancellationToken ct = default) =>
|
||||||
|
((IEmailTemplateDefinitionRepository)inner).GetDefinitionAsync(serviceId, key, ct);
|
||||||
|
|
||||||
|
public Task<EmailTemplateDefinition?> UpsertDefinitionAsync(EmailTemplateDefinitionPatch patch, CancellationToken ct = default) =>
|
||||||
|
throw new IOException("Simulated save failure.");
|
||||||
|
|
||||||
|
public Task<IReadOnlyList<EmailTemplateDefinition>> GetDefinitionsAsync(Guid serviceId, CancellationToken ct = default) =>
|
||||||
|
((IEmailTemplateDefinitionRepository)inner).GetDefinitionsAsync(serviceId, ct);
|
||||||
|
|
||||||
|
public Task<bool> DeleteDefinitionAsync(Guid serviceId, string key, Guid revision, CancellationToken ct = default) =>
|
||||||
|
((IEmailTemplateDefinitionRepository)inner).DeleteDefinitionAsync(serviceId, key, revision, ct);
|
||||||
|
|
||||||
|
public Task SaveDefinitionAsync(EmailTemplateDefinition definition, bool isNew, CancellationToken ct = default) =>
|
||||||
|
throw new IOException("Simulated save failure.");
|
||||||
|
}
|
||||||
+6
@@ -10,16 +10,22 @@
|
|||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="coverlet.collector" />
|
<PackageReference Include="coverlet.collector" />
|
||||||
<PackageReference Include="Microsoft.NET.Test.Sdk" />
|
<PackageReference Include="Microsoft.NET.Test.Sdk" />
|
||||||
|
<PackageReference Include="Testcontainers.PostgreSql" />
|
||||||
|
<PackageReference Include="SSH.NET" />
|
||||||
|
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" />
|
||||||
<PackageReference Include="xunit" />
|
<PackageReference Include="xunit" />
|
||||||
<PackageReference Include="xunit.runner.visualstudio" />
|
<PackageReference Include="xunit.runner.visualstudio" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<Using Include="Xunit" />
|
<Using Include="Xunit" />
|
||||||
|
<None Include="..\HrynCo.NotificationService.Web\Views\AdminTemplates\Index.cshtml"
|
||||||
|
Link="TestViews\Index.cshtml" CopyToOutputDirectory="PreserveNewest" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ProjectReference Include="..\HrynCo.NotificationService.Web\HrynCo.NotificationService.Web.csproj" />
|
<ProjectReference Include="..\HrynCo.NotificationService.Web\HrynCo.NotificationService.Web.csproj" />
|
||||||
|
<ProjectReference Include="..\HrynCo.NotificationService.Worker.Services\HrynCo.NotificationService.Worker.Services.csproj" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
</Project>
|
</Project>
|
||||||
@@ -0,0 +1,41 @@
|
|||||||
|
namespace HrynCo.NotificationService.Web.IntegrationTests;
|
||||||
|
|
||||||
|
public static class InterimEmailTemplateFixture
|
||||||
|
{
|
||||||
|
public const string MigrationId = "20261017070315_EmailTemplateLocalizations";
|
||||||
|
|
||||||
|
public static async Task<string> CreateAsync(TemplateDatabaseFixture fixture)
|
||||||
|
{
|
||||||
|
var connection = await fixture.CreateDatabaseAsync(legacy: true);
|
||||||
|
await TemplateDatabaseFixture.ExecuteAsync(connection, """
|
||||||
|
DROP INDEX "IX_email_templates_service_name_key_language_code";
|
||||||
|
ALTER TABLE email_templates DROP COLUMN service_name, DROP COLUMN key, DROP COLUMN variables;
|
||||||
|
ALTER TABLE email_templates ADD COLUMN definition_id uuid NOT NULL;
|
||||||
|
CREATE TABLE services (id uuid PRIMARY KEY, name varchar(100) NOT NULL, created timestamptz NOT NULL, updated timestamptz);
|
||||||
|
CREATE UNIQUE INDEX "IX_services_name" ON services(name);
|
||||||
|
CREATE TABLE email_template_definitions (
|
||||||
|
id uuid PRIMARY KEY, service_id uuid NOT NULL REFERENCES services(id) ON DELETE RESTRICT,
|
||||||
|
key varchar(100) NOT NULL, revision uuid NOT NULL, variables jsonb NOT NULL,
|
||||||
|
created timestamptz NOT NULL, updated timestamptz);
|
||||||
|
CREATE UNIQUE INDEX "IX_email_template_definitions_service_id_key" ON email_template_definitions(service_id, key);
|
||||||
|
ALTER TABLE email_templates ADD CONSTRAINT "FK_email_templates_email_template_definitions_definition_id"
|
||||||
|
FOREIGN KEY(definition_id) REFERENCES email_template_definitions(id) ON DELETE CASCADE;
|
||||||
|
CREATE UNIQUE INDEX "IX_email_templates_definition_id_language_code" ON email_templates(definition_id, language_code);
|
||||||
|
INSERT INTO "__EFMigrationsHistory" VALUES ('20261017070315_EmailTemplateLocalizations', '10.0.7');
|
||||||
|
INSERT INTO services VALUES
|
||||||
|
('11111111-1111-1111-1111-111111111111', 'InterimService', '2026-01-01Z', '2026-02-01Z'),
|
||||||
|
('88888888-8888-8888-8888-888888888888', 'UnusedService', '2026-01-02Z', null);
|
||||||
|
INSERT INTO email_template_definitions VALUES
|
||||||
|
('22222222-2222-2222-2222-222222222222', '11111111-1111-1111-1111-111111111111', 'Welcome',
|
||||||
|
'44444444-4444-4444-4444-444444444444', '[{"name":"Name","required":true}]', '2026-01-03Z', '2026-02-03Z'),
|
||||||
|
('33333333-3333-3333-3333-333333333333', '11111111-1111-1111-1111-111111111111', 'Empty',
|
||||||
|
'55555555-5555-5555-5555-555555555555', '[]', '2026-01-04Z', null);
|
||||||
|
INSERT INTO email_templates(id, definition_id, language_code, subject, html_body, text_body, created, updated) VALUES
|
||||||
|
('66666666-6666-6666-6666-666666666666', '22222222-2222-2222-2222-222222222222', 'ru-RU',
|
||||||
|
'Hello {{Name}}', '<p>{{Name}}</p>', 'Hello {{Name}}', '2026-01-05Z', '2026-02-05Z'),
|
||||||
|
('77777777-7777-7777-7777-777777777777', '22222222-2222-2222-2222-222222222222', 'ru',
|
||||||
|
'Welcome {{Name}}', '<p>Welcome {{Name}}</p>', 'Welcome {{Name}}', '2026-01-06Z', null);
|
||||||
|
""");
|
||||||
|
return connection;
|
||||||
|
}
|
||||||
|
}
|
||||||
+131
@@ -0,0 +1,131 @@
|
|||||||
|
using HrynCo.NotificationService.DAL.Abstract.Repositories;
|
||||||
|
using HrynCo.NotificationService.DAL.EF;
|
||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
using Microsoft.EntityFrameworkCore.Infrastructure;
|
||||||
|
using Microsoft.EntityFrameworkCore.Migrations;
|
||||||
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
|
using Npgsql;
|
||||||
|
|
||||||
|
namespace HrynCo.NotificationService.Web.IntegrationTests;
|
||||||
|
|
||||||
|
public sealed class InterimEmailTemplateMigrationTests(TemplateDatabaseFixture fixture) : IClassFixture<TemplateDatabaseFixture>
|
||||||
|
{
|
||||||
|
[Fact]
|
||||||
|
public async Task KnownInterimLayout_UpgradesInPlacePreservingEveryExistingFieldAndHistory()
|
||||||
|
{
|
||||||
|
var connection = await InterimEmailTemplateFixture.CreateAsync(fixture);
|
||||||
|
var original = await SourceSnapshotAsync(connection, upgraded: false);
|
||||||
|
await MigrateAsync(connection);
|
||||||
|
Assert.Equal(original, await SourceSnapshotAsync(connection, upgraded: true));
|
||||||
|
var firstServices = await ScalarAsync(connection, "SELECT jsonb_agg(to_jsonb(s) ORDER BY id)::text FROM services s");
|
||||||
|
await MigrateAsync(connection);
|
||||||
|
Assert.Equal(firstServices, await ScalarAsync(connection, "SELECT jsonb_agg(to_jsonb(s) ORDER BY id)::text FROM services s"));
|
||||||
|
await using var db = TemplateDatabaseFixture.CreateContext(connection);
|
||||||
|
var history = (await db.Database.GetAppliedMigrationsAsync()).ToList();
|
||||||
|
Assert.Equal(4, history.Count);
|
||||||
|
Assert.Contains(InterimEmailTemplateFixture.MigrationId, history);
|
||||||
|
using var provider = new ServiceCollection().AddNotificationDataAccess(connection).BuildServiceProvider();
|
||||||
|
using var scope = provider.CreateScope();
|
||||||
|
var repository = scope.ServiceProvider.GetRequiredService<IEmailTemplateRepository>();
|
||||||
|
Assert.Equal(Guid.Parse("66666666-6666-6666-6666-666666666666"), (await repository.GetAsync("InterimService", "Welcome", "ru-RU"))!.Id);
|
||||||
|
Assert.Equal(Guid.Parse("77777777-7777-7777-7777-777777777777"), (await repository.GetAsync("InterimService", "Welcome", "ru"))!.Id);
|
||||||
|
var definitions = scope.ServiceProvider.GetRequiredService<IEmailTemplateDefinitionRepository>();
|
||||||
|
Assert.Empty((await definitions.GetDefinitionAsync("InterimService", "Empty"))!.Languages);
|
||||||
|
var services = await scope.ServiceProvider.GetRequiredService<INotificationServiceRepository>().GetAllAsync();
|
||||||
|
Assert.Contains(services, s => s.Name == "UnusedService" && s.Languages.Count == 0);
|
||||||
|
Assert.All(services, s => Assert.NotEqual(Guid.Empty, s.Revision));
|
||||||
|
}
|
||||||
|
|
||||||
|
[Theory]
|
||||||
|
[InlineData("ALTER TABLE email_templates ADD COLUMN service_name varchar(100)")]
|
||||||
|
[InlineData("ALTER TABLE email_templates ALTER COLUMN language_code TYPE varchar(11)")]
|
||||||
|
[InlineData("DROP INDEX \"IX_email_templates_definition_id_language_code\"")]
|
||||||
|
[InlineData("DROP INDEX \"IX_email_templates_definition_id_language_code\"; CREATE UNIQUE INDEX \"IX_email_templates_definition_id_language_code\" ON email_templates(id)")]
|
||||||
|
[InlineData("ALTER TABLE email_templates DROP CONSTRAINT \"FK_email_templates_email_template_definitions_definition_id\"; ALTER TABLE email_templates ADD CONSTRAINT \"FK_email_templates_email_template_definitions_definition_id\" FOREIGN KEY(definition_id) REFERENCES email_template_definitions(id) ON DELETE RESTRICT")]
|
||||||
|
[InlineData("DELETE FROM \"__EFMigrationsHistory\" WHERE \"MigrationId\" = '20261017070315_EmailTemplateLocalizations'")]
|
||||||
|
[InlineData("CREATE TABLE service_languages(id uuid)")]
|
||||||
|
public async Task HybridOrUnknownLayout_FailsWithoutAdoptingOrChangingIt(string change)
|
||||||
|
{
|
||||||
|
var connection = await InterimEmailTemplateFixture.CreateAsync(fixture);
|
||||||
|
await TemplateDatabaseFixture.ExecuteAsync(connection, change);
|
||||||
|
var original = await SourceSnapshotAsync(connection, upgraded: false);
|
||||||
|
var error = await Assert.ThrowsAsync<PostgresException>(() => MigrateAsync(connection));
|
||||||
|
Assert.Contains("Unsupported email-template source layout", error.MessageText);
|
||||||
|
Assert.Equal(original, await SourceSnapshotAsync(connection, upgraded: false));
|
||||||
|
}
|
||||||
|
|
||||||
|
[Theory]
|
||||||
|
[InlineData("UPDATE email_template_definitions SET variables = '{}'", "Invalid interim shared variables array")]
|
||||||
|
[InlineData("UPDATE email_template_definitions SET variables = '[null]'", "Invalid interim shared variables shape")]
|
||||||
|
[InlineData("UPDATE email_templates SET language_code = '' WHERE id = '66666666-6666-6666-6666-666666666666'", "Invalid blank identity")]
|
||||||
|
public async Task InvalidInterimData_FailsBeforeSchemaChanges(string change, string diagnostic)
|
||||||
|
{
|
||||||
|
var connection = await InterimEmailTemplateFixture.CreateAsync(fixture);
|
||||||
|
await TemplateDatabaseFixture.ExecuteAsync(connection, change);
|
||||||
|
var original = await SourceSnapshotAsync(connection, upgraded: false);
|
||||||
|
var error = await Assert.ThrowsAsync<PostgresException>(() => MigrateAsync(connection));
|
||||||
|
Assert.Contains(diagnostic, error.MessageText);
|
||||||
|
Assert.Equal(original, await SourceSnapshotAsync(connection, upgraded: false));
|
||||||
|
Assert.Equal("0", await ScalarAsync(connection, "SELECT count(*)::text FROM information_schema.columns WHERE table_name = 'services' AND column_name = 'revision'"));
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task InterruptedInterimUpgrade_RollsBackAndCanBeRetried()
|
||||||
|
{
|
||||||
|
var connectionString = await InterimEmailTemplateFixture.CreateAsync(fixture);
|
||||||
|
var original = await SourceSnapshotAsync(connectionString, upgraded: false);
|
||||||
|
await using var db = TemplateDatabaseFixture.CreateContext(connectionString);
|
||||||
|
var script = db.GetService<IMigrator>().GenerateScript(TemplateDatabaseFixture.LegacyMigration, options: MigrationsSqlGenerationOptions.NoTransactions);
|
||||||
|
script = script.Replace("ALTER TABLE email_templates DROP COLUMN language_code;", "PERFORM pg_sleep(2); ALTER TABLE email_templates DROP COLUMN language_code;");
|
||||||
|
await using (var connection = new NpgsqlConnection(connectionString))
|
||||||
|
{
|
||||||
|
await connection.OpenAsync();
|
||||||
|
await using var transaction = await connection.BeginTransactionAsync();
|
||||||
|
await using var command = new NpgsqlCommand("SET LOCAL statement_timeout = '500ms'; " + script, connection, transaction);
|
||||||
|
Assert.Equal("57014", (await Assert.ThrowsAsync<PostgresException>(() => command.ExecuteNonQueryAsync())).SqlState);
|
||||||
|
await transaction.RollbackAsync();
|
||||||
|
}
|
||||||
|
Assert.Equal(original, await SourceSnapshotAsync(connectionString, upgraded: false));
|
||||||
|
await MigrateAsync(connectionString);
|
||||||
|
Assert.Equal(original, await SourceSnapshotAsync(connectionString, upgraded: true));
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task InterimOrigin_DownIsBlockedBeforeAnyChanges()
|
||||||
|
{
|
||||||
|
var connection = await InterimEmailTemplateFixture.CreateAsync(fixture);
|
||||||
|
await MigrateAsync(connection);
|
||||||
|
var original = await SourceSnapshotAsync(connection, upgraded: true);
|
||||||
|
await using var db = TemplateDatabaseFixture.CreateContext(connection);
|
||||||
|
var error = await Assert.ThrowsAsync<PostgresException>(() => db.GetService<IMigrator>().MigrateAsync(TemplateDatabaseFixture.LegacyMigration));
|
||||||
|
Assert.Contains("Cannot downgrade an interim-origin database", error.MessageText);
|
||||||
|
Assert.Equal(original, await SourceSnapshotAsync(connection, upgraded: true));
|
||||||
|
}
|
||||||
|
|
||||||
|
private static async Task MigrateAsync(string connection)
|
||||||
|
{
|
||||||
|
await using var db = TemplateDatabaseFixture.CreateContext(connection);
|
||||||
|
await db.Database.MigrateAsync();
|
||||||
|
}
|
||||||
|
|
||||||
|
private static Task<string> SourceSnapshotAsync(string connection, bool upgraded)
|
||||||
|
{
|
||||||
|
var versions = upgraded
|
||||||
|
? "SELECT (to_jsonb(t) - 'service_id' - 'service_language_id') || jsonb_build_object('language_code', l.code) AS value FROM email_templates t JOIN service_languages l ON l.id = t.service_language_id"
|
||||||
|
: "SELECT to_jsonb(t) AS value FROM email_templates t";
|
||||||
|
return ScalarAsync(connection, $"""
|
||||||
|
SELECT jsonb_build_object(
|
||||||
|
'services', (SELECT jsonb_agg(to_jsonb(s) - 'revision' ORDER BY id) FROM services s),
|
||||||
|
'definitions', (SELECT jsonb_agg(to_jsonb(d) ORDER BY id) FROM email_template_definitions d),
|
||||||
|
'versions', (SELECT jsonb_agg(value ORDER BY value->>'id') FROM ({versions}) q))::text
|
||||||
|
""");
|
||||||
|
}
|
||||||
|
|
||||||
|
private static async Task<string> ScalarAsync(string connectionString, string sql)
|
||||||
|
{
|
||||||
|
await using var connection = new NpgsqlConnection(connectionString);
|
||||||
|
await connection.OpenAsync();
|
||||||
|
await using var command = new NpgsqlCommand(sql, connection);
|
||||||
|
return (string)(await command.ExecuteScalarAsync())!;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,47 @@
|
|||||||
|
using System.Text.Json.Nodes;
|
||||||
|
|
||||||
|
namespace HrynCo.NotificationService.Web.IntegrationTests;
|
||||||
|
|
||||||
|
public static class InvalidDefinitionApiBody
|
||||||
|
{
|
||||||
|
public static JsonObject Create(string scenario, Guid revision)
|
||||||
|
{
|
||||||
|
var body = DefinitionApiTestContext.Body(revision, "en");
|
||||||
|
body["removeLanguageCodes"] = new JsonArray("uk");
|
||||||
|
ApplyVariableScenario(scenario, body, (JsonArray)body["variables"]!);
|
||||||
|
ApplyTranslationScenario(scenario, body, (JsonArray)body["translations"]!);
|
||||||
|
return body;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void ApplyVariableScenario(string scenario, JsonObject body, JsonArray variables)
|
||||||
|
{
|
||||||
|
switch (scenario)
|
||||||
|
{
|
||||||
|
case "missingVariables": body.Remove("variables"); break;
|
||||||
|
case "nullVariables": body["variables"] = null; break;
|
||||||
|
case "nullVariable": variables.Add((JsonNode?)null); break;
|
||||||
|
case "missingRequired": ((JsonObject)variables[0]!).Remove("required"); break;
|
||||||
|
case "nonBooleanRequired": variables[0]!["required"] = "true"; break;
|
||||||
|
case "duplicateVariable": variables.Add(variables[0]!.DeepClone()); break;
|
||||||
|
case "blankVariable": variables[0]!["name"] = " "; break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void ApplyTranslationScenario(string scenario, JsonObject body, JsonArray translations)
|
||||||
|
{
|
||||||
|
switch (scenario)
|
||||||
|
{
|
||||||
|
case "missingTranslations": body.Remove("translations"); break;
|
||||||
|
case "nullTranslations": body["translations"] = null; break;
|
||||||
|
case "nullTranslation": translations.Add((JsonNode?)null); break;
|
||||||
|
case "duplicateLanguage": translations.Add(translations[0]!.DeepClone()); break;
|
||||||
|
case "unknownLanguage": translations[0]!["languageCode"] = "RU"; break;
|
||||||
|
case "blankContent": translations[0]!["textBody"] = " "; break;
|
||||||
|
case "missingContent": ((JsonObject)translations[0]!).Remove("htmlBody"); break;
|
||||||
|
case "duplicateRemoval": body["removeLanguageCodes"] = new JsonArray("uk", "uk"); break;
|
||||||
|
case "unknownRemoval": body["removeLanguageCodes"] = new JsonArray("UK"); break;
|
||||||
|
case "nullRemoval": body["removeLanguageCodes"] = new JsonArray((JsonNode?)null); break;
|
||||||
|
case "overlap": body["removeLanguageCodes"] = new JsonArray("en"); break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user