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));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
@using HrynCo.NotificationService.DAL.Abstract.Providers
|
||||
@model IReadOnlyList<EmailChannel>
|
||||
@using HrynCo.NotificationService.Services.EmailChannels.GetUsageSummary
|
||||
@model IReadOnlyList<ChannelUsageEntry>
|
||||
@{
|
||||
ViewData["Title"] = "Email Channels";
|
||||
}
|
||||
@@ -40,12 +40,12 @@ else
|
||||
<table class="table table-hover table-sm mb-0">
|
||||
<thead class="table-dark">
|
||||
<tr>
|
||||
<th>Service Name</th>
|
||||
<th>Service</th>
|
||||
<th>Type</th>
|
||||
<th>Priority</th>
|
||||
<th>Status</th>
|
||||
<th>Daily Limit</th>
|
||||
<th>Monthly Limit</th>
|
||||
<th>Today</th>
|
||||
<th>This Month</th>
|
||||
<th class="text-end">Actions</th>
|
||||
</tr>
|
||||
</thead>
|
||||
@@ -54,7 +54,7 @@ else
|
||||
{
|
||||
<tr>
|
||||
<td>@c.ServiceName</td>
|
||||
<td>@c.EmailChannelType</td>
|
||||
<td>@c.ChannelType</td>
|
||||
<td>@c.Priority</td>
|
||||
<td>
|
||||
@if (c.IsActive)
|
||||
@@ -66,15 +66,53 @@ else
|
||||
<span class="badge bg-secondary text-muted">Inactive</span>
|
||||
}
|
||||
</td>
|
||||
<td>@(c.DailyLimit.HasValue ? c.DailyLimit.ToString() : "—")</td>
|
||||
<td>@(c.MonthlyLimit.HasValue ? c.MonthlyLimit.ToString() : "—")</td>
|
||||
<td style="min-width:130px">
|
||||
@{
|
||||
var dailyLabel = c.DailyLimit.HasValue ? $"{c.DailySent} / {c.DailyLimit}" : c.DailySent.ToString();
|
||||
}
|
||||
@if (c.DailyLimit.HasValue && c.DailyLimit > 0)
|
||||
{
|
||||
var pct = Math.Min((double)c.DailySent / c.DailyLimit.Value * 100, 100);
|
||||
var color = pct >= 100 ? "danger" : pct >= 90 ? "warning" : "success";
|
||||
<div class="d-flex align-items-center gap-2">
|
||||
<div class="progress flex-grow-1" style="height:6px; min-width:60px">
|
||||
<div class="progress-bar bg-@color" style="width:@pct.ToString("F0")%"></div>
|
||||
</div>
|
||||
<small class="text-nowrap text-muted">@dailyLabel</small>
|
||||
</div>
|
||||
}
|
||||
else
|
||||
{
|
||||
<small class="text-muted">@dailyLabel</small>
|
||||
}
|
||||
</td>
|
||||
<td style="min-width:130px">
|
||||
@{
|
||||
var monthlyLabel = c.MonthlyLimit.HasValue ? $"{c.MonthlySent} / {c.MonthlyLimit}" : c.MonthlySent.ToString();
|
||||
}
|
||||
@if (c.MonthlyLimit.HasValue && c.MonthlyLimit > 0)
|
||||
{
|
||||
var pct = Math.Min((double)c.MonthlySent / c.MonthlyLimit.Value * 100, 100);
|
||||
var color = pct >= 100 ? "danger" : pct >= 90 ? "warning" : "success";
|
||||
<div class="d-flex align-items-center gap-2">
|
||||
<div class="progress flex-grow-1" style="height:6px; min-width:60px">
|
||||
<div class="progress-bar bg-@color" style="width:@pct.ToString("F0")%"></div>
|
||||
</div>
|
||||
<small class="text-nowrap text-muted">@monthlyLabel</small>
|
||||
</div>
|
||||
}
|
||||
else
|
||||
{
|
||||
<small class="text-muted">@monthlyLabel</small>
|
||||
}
|
||||
</td>
|
||||
<td class="text-end">
|
||||
<a href="/admin/channels/@c.Id"
|
||||
<a href="/admin/channels/@c.ChannelId"
|
||||
class="btn btn-sm btn-outline-primary me-1">
|
||||
<i class="bi bi-pencil"></i> Edit
|
||||
</a>
|
||||
<form method="post"
|
||||
action="/admin/channels/@c.Id/delete"
|
||||
action="/admin/channels/@c.ChannelId/delete"
|
||||
class="d-inline">
|
||||
@Html.AntiForgeryToken()
|
||||
<button type="submit"
|
||||
|
||||
Reference in New Issue
Block a user