feat(templates): add service language template management
Add normalized service and template catalogs, scoped administration, migration, validation, and regression coverage. Ref: IT-1601
This commit is contained in:
+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;
|
||||
|
||||
internal class EmailTemplateEntityConfiguration : IEntityTypeConfiguration<EmailTemplateEntity>
|
||||
internal sealed class EmailTemplateEntityConfiguration : IEntityTypeConfiguration<EmailTemplateEntity>
|
||||
{
|
||||
public void Configure(EntityTypeBuilder<EmailTemplateEntity> builder)
|
||||
{
|
||||
builder.ToTable("email_templates");
|
||||
|
||||
builder.HasKey(x => x.Id);
|
||||
builder.Property(x => x.Id).HasColumnName("id");
|
||||
|
||||
builder.Property(x => x.ServiceName)
|
||||
.HasColumnName("service_name")
|
||||
.IsRequired()
|
||||
.HasMaxLength(100);
|
||||
|
||||
builder.Property(x => x.Key)
|
||||
.HasColumnName("key")
|
||||
.IsRequired()
|
||||
.HasMaxLength(100);
|
||||
|
||||
builder.Property(x => x.LanguageCode)
|
||||
.HasColumnName("language_code")
|
||||
.IsRequired()
|
||||
.HasMaxLength(10);
|
||||
|
||||
builder.HasIndex(x => new { x.ServiceName, x.Key, x.LanguageCode })
|
||||
.IsUnique();
|
||||
|
||||
builder.Property(x => x.Subject)
|
||||
.HasColumnName("subject")
|
||||
.IsRequired();
|
||||
|
||||
builder.Property(x => x.HtmlBody)
|
||||
.HasColumnName("html_body")
|
||||
.IsRequired();
|
||||
|
||||
builder.Property(x => x.TextBody)
|
||||
.HasColumnName("text_body")
|
||||
.IsRequired();
|
||||
|
||||
builder.Property(x => x.DefinitionId).HasColumnName("definition_id");
|
||||
builder.Property(x => x.ServiceId).HasColumnName("service_id");
|
||||
builder.Property(x => x.ServiceLanguageId).HasColumnName("service_language_id");
|
||||
builder.HasIndex(x => new { x.DefinitionId, x.ServiceLanguageId }).IsUnique();
|
||||
builder.HasOne(x => x.Definition).WithMany(x => x.Languages)
|
||||
.HasForeignKey(x => new { x.DefinitionId, x.ServiceId })
|
||||
.HasPrincipalKey(x => new { x.Id, x.ServiceId }).OnDelete(DeleteBehavior.Cascade);
|
||||
builder.HasOne(x => x.ServiceLanguage).WithMany()
|
||||
.HasForeignKey(x => new { x.ServiceLanguageId, x.ServiceId })
|
||||
.HasPrincipalKey(x => new { x.Id, x.ServiceId }).OnDelete(DeleteBehavior.Restrict);
|
||||
builder.Property(x => x.Subject).HasColumnName("subject").IsRequired();
|
||||
builder.Property(x => x.HtmlBody).HasColumnName("html_body").IsRequired();
|
||||
builder.Property(x => x.TextBody).HasColumnName("text_body").IsRequired();
|
||||
builder.Property(x => x.Created).HasColumnName("created");
|
||||
builder.Property(x => x.Updated).HasColumnName("updated");
|
||||
|
||||
builder.OwnsMany(x => x.Variables, v =>
|
||||
{
|
||||
v.ToJson("variables");
|
||||
v.Property(x => x.Name).HasJsonPropertyName("name");
|
||||
v.Property(x => x.Required).HasJsonPropertyName("required");
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,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
|
||||
{
|
||||
public required string ServiceName { get; set; }
|
||||
public required string Key { get; set; }
|
||||
public required string LanguageCode { get; set; }
|
||||
public Guid DefinitionId { get; set; }
|
||||
public EmailTemplateDefinitionEntity Definition { get; set; } = null!;
|
||||
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 HtmlBody { get; set; }
|
||||
public required string TextBody { get; set; }
|
||||
public List<EmailTemplateVariableData> Variables { get; set; } = [];
|
||||
}
|
||||
|
||||
internal class EmailTemplateVariableData
|
||||
{
|
||||
public required string Name { get; set; }
|
||||
public bool Required { get; set; }
|
||||
}
|
||||
|
||||
@@ -0,0 +1,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
|
||||
modelBuilder
|
||||
.HasAnnotation("ProductVersion", "9.0.5")
|
||||
.HasAnnotation("ProductVersion", "10.0.7")
|
||||
.HasAnnotation("Relational:MaxIdentifierLength", 63);
|
||||
|
||||
NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder);
|
||||
@@ -114,6 +114,44 @@ namespace HrynCo.NotificationService.DAL.EF.Migrations
|
||||
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")
|
||||
@@ -125,28 +163,22 @@ namespace HrynCo.NotificationService.DAL.EF.Migrations
|
||||
.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<string>("Key")
|
||||
.IsRequired()
|
||||
.HasMaxLength(100)
|
||||
.HasColumnType("character varying(100)")
|
||||
.HasColumnName("key");
|
||||
b.Property<Guid>("ServiceId")
|
||||
.HasColumnType("uuid")
|
||||
.HasColumnName("service_id");
|
||||
|
||||
b.Property<string>("LanguageCode")
|
||||
.IsRequired()
|
||||
.HasMaxLength(10)
|
||||
.HasColumnType("character varying(10)")
|
||||
.HasColumnName("language_code");
|
||||
|
||||
b.Property<string>("ServiceName")
|
||||
.IsRequired()
|
||||
.HasMaxLength(100)
|
||||
.HasColumnType("character varying(100)")
|
||||
.HasColumnName("service_name");
|
||||
b.Property<Guid>("ServiceLanguageId")
|
||||
.HasColumnType("uuid")
|
||||
.HasColumnName("service_language_id");
|
||||
|
||||
b.Property<string>("Subject")
|
||||
.IsRequired()
|
||||
@@ -164,12 +196,83 @@ namespace HrynCo.NotificationService.DAL.EF.Migrations
|
||||
|
||||
b.HasKey("Id");
|
||||
|
||||
b.HasIndex("ServiceName", "Key", "LanguageCode")
|
||||
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)
|
||||
@@ -179,43 +282,91 @@ namespace HrynCo.NotificationService.DAL.EF.Migrations
|
||||
.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 =>
|
||||
{
|
||||
b1.Property<Guid>("EmailTemplateEntityId")
|
||||
.HasColumnType("uuid");
|
||||
b1.Property<Guid>("EmailTemplateDefinitionEntityId");
|
||||
|
||||
b1.Property<int>("__synthesizedOrdinal")
|
||||
.ValueGeneratedOnAdd()
|
||||
.HasColumnType("integer");
|
||||
.ValueGeneratedOnAdd();
|
||||
|
||||
b1.Property<string>("Name")
|
||||
.IsRequired()
|
||||
.HasColumnType("text")
|
||||
.HasAnnotation("Relational:JsonPropertyName", "name");
|
||||
.HasJsonPropertyName("name");
|
||||
|
||||
b1.Property<bool>("Required")
|
||||
.HasColumnType("boolean")
|
||||
.HasAnnotation("Relational:JsonPropertyName", "required");
|
||||
.HasJsonPropertyName("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()
|
||||
.HasForeignKey("EmailTemplateEntityId");
|
||||
.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
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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<EmailChannelEntity> Providers => Set<EmailChannelEntity>();
|
||||
internal DbSet<EmailChannelUsageEntity> EmailChannelUsage => Set<EmailChannelUsageEntity>();
|
||||
|
||||
@@ -1,120 +1,232 @@
|
||||
using HrynCo.NotificationService.DAL.Abstract.Repositories;
|
||||
using HrynCo.NotificationService.DAL.Abstract.Templates;
|
||||
using HrynCo.NotificationService.DAL.EF.Core;
|
||||
using HrynCo.NotificationService.DAL.EF.Entities;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace HrynCo.NotificationService.DAL.EF.Repositories;
|
||||
|
||||
internal sealed class EmailTemplateRepository
|
||||
: NotificationBaseRepository<EmailTemplateEntity>, IEmailTemplateRepository
|
||||
internal sealed class EmailTemplateRepository(NotificationDbContext db, TemplateMutationRunner mutations)
|
||||
: 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)
|
||||
{
|
||||
IQueryable<EmailTemplateEntity> query = EfRepository.Get();
|
||||
|
||||
var query = Languages.AsNoTracking();
|
||||
if (!string.IsNullOrWhiteSpace(serviceName))
|
||||
{
|
||||
query = query.Where(x => x.ServiceName == serviceName);
|
||||
}
|
||||
|
||||
query = query.Where(t => t.Definition.Service.Name == serviceName);
|
||||
if (!string.IsNullOrWhiteSpace(key))
|
||||
{
|
||||
query = query.Where(x => x.Key == key);
|
||||
}
|
||||
|
||||
List<EmailTemplateEntity> entities = await query
|
||||
.OrderBy(x => x.ServiceName).ThenBy(x => x.Key)
|
||||
.AsNoTracking()
|
||||
.ToListAsync(ct);
|
||||
return entities.Select(MapToDomain).ToList();
|
||||
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>> 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 Task<IReadOnlyList<EmailTemplate>> GetByServiceAsync(string serviceName, CancellationToken ct = default) =>
|
||||
GetAllAsync(serviceName, ct: ct);
|
||||
|
||||
public async Task<EmailTemplate?> GetAsync(string serviceName, string key, string languageCode, CancellationToken ct = default)
|
||||
{
|
||||
EmailTemplateEntity? entity = await EfRepository.Get()
|
||||
.AsNoTracking()
|
||||
.FirstOrDefaultAsync(
|
||||
x => x.ServiceName == serviceName && x.Key == key && x.LanguageCode == languageCode, ct);
|
||||
|
||||
return entity is null ? null : MapToDomain(entity);
|
||||
var code = languageCode;
|
||||
var entity = await Languages.AsNoTracking().SingleOrDefaultAsync(t =>
|
||||
t.Definition.Service.Name == serviceName && t.Definition.Key == key && t.ServiceLanguage.Code == code, ct);
|
||||
return entity is null ? null : MapLanguage(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 async Task UpdateAsync(EmailTemplate EmailTemplate, CancellationToken ct = default)
|
||||
public Task AddAsync(EmailTemplate template, CancellationToken ct = default) => mutations.ExecuteAsync(async () =>
|
||||
{
|
||||
EmailTemplateEntity? entity = await EfRepository.Get()
|
||||
.FirstOrDefaultAsync(x => x.Id == EmailTemplate.Id, ct);
|
||||
|
||||
if (entity is null)
|
||||
ValidateLegacy(template);
|
||||
var definition = await FindDefinitionAsync(template.ServiceName, template.Key, ct);
|
||||
if (definition is null)
|
||||
{
|
||||
return;
|
||||
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);
|
||||
|
||||
entity.ServiceName = EmailTemplate.ServiceName;
|
||||
entity.Key = EmailTemplate.Key;
|
||||
entity.LanguageCode = EmailTemplate.LanguageCode;
|
||||
entity.Subject = EmailTemplate.Subject;
|
||||
entity.HtmlBody = EmailTemplate.HtmlBody;
|
||||
entity.TextBody = EmailTemplate.TextBody;
|
||||
entity.Variables = EmailTemplate.Variables
|
||||
.Select(v => new EmailTemplateVariableData { Name = v.Name, Required = v.Required })
|
||||
.ToList();
|
||||
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);
|
||||
|
||||
await EfRepository.SaveChangesAsync();
|
||||
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;
|
||||
}
|
||||
|
||||
public async Task DeleteAsync(EmailTemplate EmailTemplate, CancellationToken ct = default)
|
||||
private void SynchronizeLanguages(EmailTemplateDefinitionEntity definition, IReadOnlyList<EmailTemplate> languages)
|
||||
{
|
||||
EmailTemplateEntity? entity = await EfRepository.Get()
|
||||
.FirstOrDefaultAsync(x => x.Id == EmailTemplate.Id, ct);
|
||||
if (entity is not null)
|
||||
await EfRepository.DeleteAsync(entity);
|
||||
var codes = languages.Select(t => t.LanguageCode).ToHashSet(StringComparer.Ordinal);
|
||||
db.Templates.RemoveRange(definition.Languages.Where(t => !codes.Contains(t.ServiceLanguage.Code)));
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
private static EmailTemplate MapToDomain(EmailTemplateEntity e) => new()
|
||||
public async Task<IReadOnlyList<EmailTemplateDefinition>> GetDefinitionsAsync(string? serviceName = null, string? key = null, CancellationToken ct = default)
|
||||
{
|
||||
Id = e.Id,
|
||||
ServiceName = e.ServiceName,
|
||||
Key = e.Key,
|
||||
LanguageCode = e.LanguageCode,
|
||||
Subject = e.Subject,
|
||||
HtmlBody = e.HtmlBody,
|
||||
TextBody = e.TextBody,
|
||||
Variables = e.Variables.Select(v => new EmailTemplateVariable { Name = v.Name, Required = v.Required }).ToList(),
|
||||
Created = e.Created,
|
||||
Updated = e.Updated
|
||||
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,
|
||||
ServiceName = t.ServiceName,
|
||||
Key = t.Key,
|
||||
LanguageCode = t.LanguageCode,
|
||||
Subject = t.Subject,
|
||||
HtmlBody = t.HtmlBody,
|
||||
TextBody = t.TextBody,
|
||||
Variables = t.Variables.Select(v => new EmailTemplateVariableData { Name = v.Name, Required = v.Required }).ToList(),
|
||||
Created = t.Created,
|
||||
Updated = t.Updated
|
||||
entity.Subject = model.Subject;
|
||||
entity.HtmlBody = model.HtmlBody;
|
||||
entity.TextBody = model.TextBody;
|
||||
entity.Updated = DateTimeOffset.UtcNow;
|
||||
}
|
||||
|
||||
private static EmailTemplate MapLanguage(EmailTemplateEntity entity) => new()
|
||||
{
|
||||
Id = entity.Id, ServiceName = entity.Definition.Service.Name, Key = entity.Definition.Key,
|
||||
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,188 @@
|
||||
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 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 =>
|
||||
options.UseNpgsql(connectionString));
|
||||
|
||||
services.AddScoped<TemplateMutationRunner>();
|
||||
services.AddScoped<INotificationServiceRepository, NotificationServiceRepository>();
|
||||
services.AddScoped<IEmailTemplateRepository, EmailTemplateRepository>();
|
||||
services.AddScoped<IEmailTemplateDefinitionRepository, EmailTemplateRepository>();
|
||||
services.AddScoped<IEmailChannelRepository, EmailChannelRepository>();
|
||||
services.AddScoped<IEmailChannelUsageRepository, EmailChannelUsageRepository>();
|
||||
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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user