6dcc911fc2
- Rename Template -> EmailTemplate, Provider -> EmailChannel, ProviderSettings -> EmailChannelSettings, ProviderType -> EmailChannelType, ProviderUsage -> EmailChannelUsage throughout all layers - Add Undefined = 0 to EmailChannelType enum for safe default handling - Remove SaveChangesAsync from EfRepository methods — repositories now only stage changes - Add SaveChangesAsync to IUnitOfWork and EfUnitOfWork - Add TransactionBehavior MediatR pipeline: wraps every handler in a transaction, saves and commits on success, rolls back on exception - Add MediatR package reference to Services project Ref: IT-628 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
100 lines
3.4 KiB
C#
100 lines
3.4 KiB
C#
using System.Text.Json;
|
|
using HrynCo.NotificationService.DAL.Abstract.Providers;
|
|
using HrynCo.NotificationService.DAL.Abstract.Repositories;
|
|
using HrynCo.NotificationService.DAL.EF.Core;
|
|
using HrynCo.NotificationService.DAL.EF.Entities;
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
namespace HrynCo.NotificationService.DAL.EF.Repositories;
|
|
|
|
internal sealed class EmailChannelRepository : EfRepository<EmailChannelEntity>, IEmailChannelRepository
|
|
{
|
|
public EmailChannelRepository(NotificationDbContext dbContext) : base(dbContext)
|
|
{
|
|
}
|
|
|
|
public async Task<IReadOnlyList<EmailChannel>> GetByServiceAsync(string serviceName, CancellationToken ct = default)
|
|
{
|
|
var entities = await DbSet
|
|
.Where(x => x.ServiceName == serviceName)
|
|
.OrderBy(x => x.Priority)
|
|
.ToListAsync(ct);
|
|
|
|
return entities.Select(MapToDomain).ToList();
|
|
}
|
|
|
|
public async Task<EmailChannel?> GetByIdAsync(Guid id, CancellationToken ct = default)
|
|
{
|
|
EmailChannelEntity? entity = await DbSet.FindAsync([id], ct);
|
|
return entity is null ? null : MapToDomain(entity);
|
|
}
|
|
|
|
public Task AddAsync(EmailChannel channel, CancellationToken ct = default)
|
|
{
|
|
return base.AddAsync(MapToEntity(channel), ct);
|
|
}
|
|
|
|
public Task UpdateAsync(EmailChannel channel, CancellationToken ct = default)
|
|
{
|
|
EmailChannelEntity entity = MapToEntity(channel);
|
|
entity.Updated = DateTimeOffset.UtcNow;
|
|
Update(entity);
|
|
return Task.CompletedTask;
|
|
}
|
|
|
|
public async Task DeleteAsync(EmailChannel channel, CancellationToken ct = default)
|
|
{
|
|
EmailChannelEntity? entity = await DbSet.FindAsync([channel.Id], ct);
|
|
if (entity is not null)
|
|
{
|
|
Delete(entity);
|
|
}
|
|
}
|
|
|
|
private static EmailChannel MapToDomain(EmailChannelEntity e)
|
|
{
|
|
return new EmailChannel
|
|
{
|
|
Id = e.Id,
|
|
ServiceName = e.ServiceName,
|
|
Priority = e.Priority,
|
|
EmailChannelType = e.EmailChannelType,
|
|
Settings = DeserializeSettings(e.EmailChannelType, e.SettingsJson),
|
|
DailyLimit = e.DailyLimit,
|
|
MonthlyLimit = e.MonthlyLimit,
|
|
WarnThresholdPercent = e.WarnThresholdPercent,
|
|
IsActive = e.IsActive,
|
|
Created = e.Created,
|
|
Updated = e.Updated
|
|
};
|
|
}
|
|
|
|
private static EmailChannelEntity MapToEntity(EmailChannel p)
|
|
{
|
|
return new EmailChannelEntity
|
|
{
|
|
Id = p.Id,
|
|
ServiceName = p.ServiceName,
|
|
Priority = p.Priority,
|
|
EmailChannelType = p.EmailChannelType,
|
|
SettingsJson = JsonSerializer.Serialize(p.Settings),
|
|
DailyLimit = p.DailyLimit,
|
|
MonthlyLimit = p.MonthlyLimit,
|
|
WarnThresholdPercent = p.WarnThresholdPercent,
|
|
IsActive = p.IsActive,
|
|
Created = p.Created,
|
|
Updated = p.Updated
|
|
};
|
|
}
|
|
|
|
private static EmailChannelSettings DeserializeSettings(EmailChannelType type, string json)
|
|
{
|
|
return type switch
|
|
{
|
|
EmailChannelType.Smtp => JsonSerializer.Deserialize<SmtpChannelSettings>(json)
|
|
?? throw new InvalidOperationException(
|
|
"Failed to deserialize SMTP EmailChannel settings."),
|
|
_ => throw new InvalidOperationException($"Unknown or undefined email channel type: {type}")
|
|
};
|
|
}
|
|
} |