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:
@@ -1,36 +1,30 @@
|
||||
using HrynCo.NotificationService.DAL.Abstract.Providers;
|
||||
using HrynCo.NotificationService.DAL.Abstract.Repositories;
|
||||
using HrynCo.NotificationService.Services.EmailChannels.Create;
|
||||
using HrynCo.NotificationService.Services.EmailChannels.Delete;
|
||||
using HrynCo.NotificationService.Services.EmailChannels.Get;
|
||||
using HrynCo.NotificationService.Services.EmailChannels.GetAll;
|
||||
using HrynCo.NotificationService.Services.EmailChannels.GetUsageSummary;
|
||||
using HrynCo.NotificationService.Services.EmailChannels.Send;
|
||||
using HrynCo.NotificationService.Services.EmailChannels.Update;
|
||||
using HrynCo.NotificationService.Web.Controllers.Admin.ViewModels;
|
||||
using MediatR;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using System.Net;
|
||||
using System.Net.Mail;
|
||||
|
||||
namespace HrynCo.NotificationService.Web.Controllers.Admin;
|
||||
|
||||
[Route("admin/channels")]
|
||||
public class AdminChannelsController : Controller
|
||||
public class AdminChannelsController(IMediator mediator) : Controller
|
||||
{
|
||||
private readonly IMediator _mediator;
|
||||
|
||||
public AdminChannelsController(IMediator mediator)
|
||||
{
|
||||
_mediator = mediator;
|
||||
}
|
||||
|
||||
// GET /admin/channels
|
||||
[HttpGet("")]
|
||||
public async Task<IActionResult> Index(CancellationToken ct)
|
||||
{
|
||||
var result = await _mediator.Send(new GetAllEmailChannelsQuery(), ct);
|
||||
var result = await mediator.Send(new GetChannelUsageSummaryQuery(), ct);
|
||||
if (!result.IsSuccess)
|
||||
{
|
||||
ModelState.AddModelError("", result.Error?.Message ?? "Failed to load channels.");
|
||||
return View(Array.Empty<EmailChannel>());
|
||||
return View(Array.Empty<ChannelUsageEntry>());
|
||||
}
|
||||
|
||||
return View(result.Result);
|
||||
@@ -47,7 +41,7 @@ public class AdminChannelsController : Controller
|
||||
[HttpGet("{id:guid}")]
|
||||
public async Task<IActionResult> Edit(Guid id, CancellationToken ct)
|
||||
{
|
||||
var result = await _mediator.Send(new GetEmailChannelQuery(id), ct);
|
||||
var result = await mediator.Send(new GetEmailChannelQuery(id), ct);
|
||||
if (!result.IsSuccess || result.Result is null)
|
||||
return NotFound();
|
||||
|
||||
@@ -110,7 +104,7 @@ public class AdminChannelsController : Controller
|
||||
model.WarnThresholdPercent,
|
||||
model.IsActive);
|
||||
|
||||
var result = await _mediator.Send(command, ct);
|
||||
var result = await mediator.Send(command, ct);
|
||||
if (!result.IsSuccess)
|
||||
{
|
||||
ModelState.AddModelError("", result.Error?.Message ?? "Failed to create channel.");
|
||||
@@ -128,7 +122,7 @@ public class AdminChannelsController : Controller
|
||||
model.WarnThresholdPercent,
|
||||
model.IsActive);
|
||||
|
||||
var result = await _mediator.Send(command, ct);
|
||||
var result = await mediator.Send(command, ct);
|
||||
if (!result.IsSuccess)
|
||||
{
|
||||
ModelState.AddModelError("", result.Error?.Message ?? "Failed to update channel.");
|
||||
@@ -143,40 +137,21 @@ public class AdminChannelsController : Controller
|
||||
[HttpPost("{id:guid}/test")]
|
||||
public async Task<IActionResult> Test(Guid id, [FromBody] TestChannelRequest request, CancellationToken ct)
|
||||
{
|
||||
var result = await _mediator.Send(new GetEmailChannelQuery(id), ct);
|
||||
if (!result.IsSuccess || result.Result is null)
|
||||
var channelResult = await mediator.Send(new GetEmailChannelQuery(id), ct);
|
||||
if (!channelResult.IsSuccess || channelResult.Result is null)
|
||||
return NotFound(new { success = false, message = "Channel not found." });
|
||||
|
||||
if (result.Result.Settings is not SmtpChannelSettings smtp)
|
||||
return BadRequest(new { success = false, message = "Only SMTP channels are supported." });
|
||||
var channel = channelResult.Result;
|
||||
var subject = "✅ Test email from Notification Service";
|
||||
var body = $"This is a test email sent from the Notification Service admin panel.\n\nChannel: {channel.ServiceName}";
|
||||
|
||||
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)
|
||||
};
|
||||
var sendResult = await mediator.Send(
|
||||
new SendEmailCommand(id, request.ToEmail, request.ToEmail, subject, body, null), ct);
|
||||
|
||||
var message = new MailMessage
|
||||
{
|
||||
From = new MailAddress(smtp.FromEmail, smtp.FromName),
|
||||
Subject = "✅ Test email from Notification Service",
|
||||
Body = $"This is a test email sent from the Notification Service admin panel.\n\nChannel: {result.Result.ServiceName}\nHost: {smtp.Host}:{smtp.Port}",
|
||||
IsBodyHtml = false
|
||||
};
|
||||
message.To.Add(request.ToEmail);
|
||||
if (!sendResult.IsSuccess)
|
||||
return Ok(new { success = false, message = sendResult.Error?.Message });
|
||||
|
||||
await client.SendMailAsync(message, ct);
|
||||
|
||||
return Ok(new { success = true, message = $"Test email sent to {request.ToEmail}." });
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return Ok(new { success = false, message = ex.Message });
|
||||
}
|
||||
return Ok(new { success = true, message = $"Test email sent to {request.ToEmail}." });
|
||||
}
|
||||
|
||||
// POST /admin/channels/{id}/delete
|
||||
@@ -184,7 +159,7 @@ public class AdminChannelsController : Controller
|
||||
[ValidateAntiForgeryToken]
|
||||
public async Task<IActionResult> Delete(Guid id, CancellationToken ct)
|
||||
{
|
||||
await _mediator.Send(new DeleteEmailChannelCommand(id), ct);
|
||||
await mediator.Send(new DeleteEmailChannelCommand(id), ct);
|
||||
return RedirectToAction(nameof(Index));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user