feat: add RabbitMQ worker, contracts, usage UI in channels screen

- Add HrynCo.NotificationService.Contracts project with SendEmailMessage and NotificationResultMessage
- Add SendEmailConsumer (RabbitMQ worker) with reply-to pattern via CorrelationContext.ReplyTo
- Add SendEmailHandler owning SMTP send + usage increment as business logic
- Add GetChannelUsageSummaryHandler with single DB query via navigation property
- Merge usage stats inline into channels list (daily/monthly with progress bars)
- Refactor AdminChannelsController.Index to use GetChannelUsageSummaryQuery
- Add RabbitMQ service to docker-compose files
- Remove dead AdminChannelUsageController, ChannelUsageViewModel, ChannelUsageSummary

Ref: IT-628

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This commit is contained in:
Anatolii Grynchuk
2026-05-02 14:00:58 +03:00
parent 395f5573a1
commit b0996833bc
29 changed files with 569 additions and 78 deletions
@@ -0,0 +1,80 @@
using System.Net;
using System.Net.Mail;
using HrynCo.NotificationService.DAL.Abstract;
using HrynCo.NotificationService.DAL.Abstract.Providers;
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.EmailChannels.Send;
internal sealed class SendEmailHandler
: RequestHandler<SendEmailCommand, ServiceResult<Core.Unit>>
{
private readonly IEmailChannelRepository _channels;
private readonly IEmailChannelUsageRepository _usage;
public SendEmailHandler(
IContextualSerilogLogger<SendEmailCommand> logger,
IUnitOfWork unitOfWork,
IEmailChannelRepository channels,
IEmailChannelUsageRepository usage)
: base(logger, unitOfWork)
{
_channels = channels;
_usage = usage;
}
protected override async Task<ServiceResult<Core.Unit>> DoOnHandle(
SendEmailCommand request, CancellationToken cancellationToken)
{
var channel = await _channels.GetByIdAsync(request.ChannelId, cancellationToken);
if (channel is null)
return Failure<Core.Unit>($"Channel '{request.ChannelId}' not found.");
if (channel.Settings is not SmtpChannelSettings smtp)
return Failure<Core.Unit>($"Channel type '{channel.EmailChannelType}' is not supported for sending.");
try
{
using var client = new SmtpClient(smtp.Host, smtp.Port)
{
EnableSsl = smtp.UseSsl,
Credentials = string.IsNullOrWhiteSpace(smtp.Username)
? null
: new NetworkCredential(smtp.Username, smtp.Password)
};
using var mail = new MailMessage
{
From = new MailAddress(smtp.FromEmail, smtp.FromName),
Subject = request.Subject,
Body = request.HtmlBody,
IsBodyHtml = true
};
if (!string.IsNullOrWhiteSpace(request.TextBody))
{
var plain = AlternateView.CreateAlternateViewFromString(request.TextBody, null, "text/plain");
mail.AlternateViews.Add(plain);
}
mail.To.Add(new MailAddress(request.RecipientEmail, request.RecipientName));
await client.SendMailAsync(mail, cancellationToken);
}
catch (Exception ex)
{
Logger.Error(ex, "SMTP send failed for channel {ChannelId}", request.ChannelId);
return Failure<Core.Unit>(ex.Message);
}
await _usage.IncrementUsageAsync(
request.ChannelId,
DateOnly.FromDateTime(DateTime.UtcNow),
cancellationToken);
return Success(Unit.Value);
}
}