feat(notification): add admin rest management and deletion

Add revision-safe service and template APIs, channel mapping, and validated admin controls.

Ref: IT-1601
This commit is contained in:
2026-10-17 22:51:01 +03:00
parent 81654c0d33
commit 8e81572386
80 changed files with 2253 additions and 54 deletions
@@ -1,12 +1,13 @@
using System.ComponentModel.DataAnnotations;
using HrynCo.NotificationService.DAL.Abstract.Providers;
namespace HrynCo.NotificationService.Web.Controllers.Api.EmailChannels;
public sealed record CreateEmailChannelRequest(
string ServiceName,
[Required, StringLength(100)] string ServiceName,
int Priority,
EmailChannelType ChannelType,
EmailChannelSettings Settings,
SmtpChannelSettingsDto Settings,
int? DailyLimit,
int? MonthlyLimit,
int WarnThresholdPercent,
@@ -0,0 +1,7 @@
using HrynCo.NotificationService.DAL.Abstract.Providers;
namespace HrynCo.NotificationService.Web.Controllers.Api.EmailChannels;
public sealed record EmailChannelDto(Guid Id, string ServiceName, int Priority, EmailChannelType EmailChannelType,
SmtpChannelSettingsDto Settings, int? DailyLimit, int? MonthlyLimit, int WarnThresholdPercent,
bool IsActive, DateTimeOffset Created, DateTimeOffset? Updated);
@@ -0,0 +1,23 @@
using HrynCo.NotificationService.DAL.Abstract.Providers;
namespace HrynCo.NotificationService.Web.Controllers.Api.EmailChannels;
internal static class EmailChannelMapping
{
public static SmtpChannelSettings ToSettings(SmtpChannelSettingsDto settings) => new()
{
Host = settings.Host, Port = settings.Port, Username = settings.Username, Password = settings.Password,
UseSsl = settings.UseSsl, FromEmail = settings.FromEmail, FromName = settings.FromName
};
public static EmailChannelDto Map(EmailChannel channel)
{
var smtp = (SmtpChannelSettings)channel.Settings;
return new EmailChannelDto(channel.Id, channel.ServiceName, channel.Priority, channel.EmailChannelType,
new SmtpChannelSettingsDto
{
Host = smtp.Host, Port = smtp.Port, Username = smtp.Username, Password = smtp.Password,
UseSsl = smtp.UseSsl, FromEmail = smtp.FromEmail, FromName = smtp.FromName
}, channel.DailyLimit, channel.MonthlyLimit, channel.WarnThresholdPercent, channel.IsActive, channel.Created, channel.Updated);
}
}
@@ -1,8 +1,12 @@
using HrynCo.NotificationService.DAL.Abstract.Providers;
using HrynCo.NotificationService.Services.Core;
using HrynCo.NotificationService.Web.Controllers.Api.Services;
using HrynCo.NotificationService.Web.Infrastructure;
using HrynCo.NotificationService.Services.EmailChannels.Create;
using HrynCo.NotificationService.Services.EmailChannels.Delete;
using HrynCo.NotificationService.Services.EmailChannels.Get;
using HrynCo.NotificationService.Services.EmailChannels.GetByService;
using HrynCo.NotificationService.Services.EmailChannels.GetAll;
using HrynCo.NotificationService.Services.EmailChannels.Update;
using MediatR;
using Microsoft.AspNetCore.Mvc;
@@ -10,32 +14,44 @@ using Microsoft.AspNetCore.Mvc;
namespace HrynCo.NotificationService.Web.Controllers.Api.EmailChannels;
[Route("api/v1/email-channels")]
[TypeFilter(typeof(ApiRequestValidationFilter), Order = -3000)]
public sealed class EmailChannelsController : ApiControllerBase
{
public EmailChannelsController(IMediator mediator) : base(mediator) { }
[HttpGet]
[ProducesResponseType<ApiResponse<IReadOnlyList<EmailChannelDto>>>(StatusCodes.Status200OK)]
public async Task<IActionResult> GetAll([FromQuery] string serviceName, CancellationToken cancellationToken)
{
var result = await Mediator.Send(new GetEmailChannelsQuery(serviceName), cancellationToken);
return FromServiceResult(result);
return FromChannels(result);
}
[HttpGet("all")]
[ProducesResponseType<ApiResponse<IReadOnlyList<EmailChannelDto>>>(StatusCodes.Status200OK)]
public async Task<IActionResult> GetAllServices(CancellationToken cancellationToken) =>
FromChannels(await Mediator.Send(new GetAllEmailChannelsQuery(), cancellationToken));
[HttpGet("{id:guid}")]
public async Task<IActionResult> Get(Guid id, CancellationToken cancellationToken)
[ProducesResponseType<ApiResponse<EmailChannelDto>>(StatusCodes.Status200OK)]
public async Task<IActionResult> Get([FromRoute] Guid id, CancellationToken cancellationToken)
{
var result = await Mediator.Send(new GetEmailChannelQuery(id), cancellationToken);
return FromServiceResult(result);
return result.IsSuccess
? Ok(new ApiResponse<EmailChannelDto> { Success = true, Data = EmailChannelMapping.Map(result.Result!) })
: MapServiceError(result.Error!);
}
[HttpPost]
public async Task<IActionResult> Create([FromBody] CreateEmailChannelRequest request, CancellationToken cancellationToken)
{
if (request.ChannelType != EmailChannelType.Smtp)
return MapServiceError(new ServiceError("Only SMTP channels are supported.", ServiceErrorCode.InvalidRequest));
var command = new CreateEmailChannelCommand(
request.ServiceName,
request.Priority,
request.ChannelType,
request.Settings,
EmailChannelMapping.ToSettings(request.Settings),
request.DailyLimit,
request.MonthlyLimit,
request.WarnThresholdPercent,
@@ -52,12 +68,12 @@ public sealed class EmailChannelsController : ApiControllerBase
}
[HttpPut("{id:guid}")]
public async Task<IActionResult> Update(Guid id, [FromBody] UpdateEmailChannelRequest request, CancellationToken cancellationToken)
public async Task<IActionResult> Update([FromRoute] Guid id, [FromBody] UpdateEmailChannelRequest request, CancellationToken cancellationToken)
{
var command = new UpdateEmailChannelCommand(
id,
request.Priority,
request.Settings,
EmailChannelMapping.ToSettings(request.Settings),
request.DailyLimit,
request.MonthlyLimit,
request.WarnThresholdPercent,
@@ -69,9 +85,14 @@ public sealed class EmailChannelsController : ApiControllerBase
}
[HttpDelete("{id:guid}")]
public async Task<IActionResult> Delete(Guid id, CancellationToken cancellationToken)
public async Task<IActionResult> Delete([FromRoute] Guid id, CancellationToken cancellationToken)
{
var result = await Mediator.Send(new DeleteEmailChannelCommand(id), cancellationToken);
return FromServiceResult(result);
}
private IActionResult FromChannels(ServiceResult<IReadOnlyList<EmailChannel>> result) =>
result.IsSuccess
? Ok(new ApiResponse<IReadOnlyList<EmailChannelDto>> { Success = true, Data = result.Result!.Select(EmailChannelMapping.Map).ToList() })
: MapServiceError(result.Error!);
}
@@ -0,0 +1,19 @@
using System.ComponentModel.DataAnnotations;
using HrynCo.NotificationService.DAL.Abstract.Providers;
namespace HrynCo.NotificationService.Web.Controllers.Api.EmailChannels;
public sealed class SmtpChannelSettingsDto
{
public EmailChannelType EmailChannelType => EmailChannelType.Smtp;
[Required]
public string Host { get; init; } = "";
[Range(1, 65535)]
public int Port { get; init; } = 587;
public string Username { get; init; } = "";
public string Password { get; init; } = "";
public bool UseSsl { get; init; } = true;
[Required, EmailAddress]
public string FromEmail { get; init; } = "";
public string FromName { get; init; } = "";
}
@@ -4,7 +4,7 @@ namespace HrynCo.NotificationService.Web.Controllers.Api.EmailChannels;
public sealed record UpdateEmailChannelRequest(
int Priority,
EmailChannelSettings Settings,
SmtpChannelSettingsDto Settings,
int? DailyLimit,
int? MonthlyLimit,
int WarnThresholdPercent,