Files
hrynco-notification-service/HrynCo.NotificationService.Web/Controllers/Admin/AdminChannelsController.cs
T
Anatolii Grynchuk 61ccf9c777 feat: add test channel feature in admin UI
- POST /admin/channels/{id}/test — direct SMTP send via MailKit
- Test button shown only on existing channels (not create)
- Bootstrap modal with recipient email input and spinner
- Inline success/error result inside the modal

Ref: IT-628

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
2026-05-02 02:57:36 +03:00

199 lines
6.9 KiB
C#

using HrynCo.NotificationService.DAL.Abstract.Providers;
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.Update;
using HrynCo.NotificationService.Web.Controllers.Admin.ViewModels;
using MailKit.Net.Smtp;
using MailKit.Security;
using MediatR;
using Microsoft.AspNetCore.Mvc;
using MimeKit;
namespace HrynCo.NotificationService.Web.Controllers.Admin;
[Route("admin/channels")]
public class AdminChannelsController : 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);
if (!result.IsSuccess)
{
ModelState.AddModelError("", result.Error?.Message ?? "Failed to load channels.");
return View(Array.Empty<EmailChannel>());
}
return View(result.Result);
}
// GET /admin/channels/create
[HttpGet("create")]
public IActionResult Create()
{
return View("Edit", new EmailChannelEditViewModel());
}
// GET /admin/channels/{id}
[HttpGet("{id:guid}")]
public async Task<IActionResult> Edit(Guid id, CancellationToken ct)
{
var result = await _mediator.Send(new GetEmailChannelQuery(id), ct);
if (!result.IsSuccess || result.Result is null)
return NotFound();
var channel = result.Result;
var smtp = channel.Settings as SmtpChannelSettings ?? new SmtpChannelSettings
{
Host = "", Username = "", Password = "",
FromEmail = "", FromName = "", AppDisplayName = "", AppBaseUrl = ""
};
var vm = new EmailChannelEditViewModel
{
Id = channel.Id,
ServiceName = channel.ServiceName,
Priority = channel.Priority,
EmailChannelType = channel.EmailChannelType,
DailyLimit = channel.DailyLimit,
MonthlyLimit = channel.MonthlyLimit,
WarnThresholdPercent = channel.WarnThresholdPercent,
IsActive = channel.IsActive,
Host = smtp.Host,
Port = smtp.Port,
Username = smtp.Username,
Password = smtp.Password,
UseSsl = smtp.UseSsl,
FromEmail = smtp.FromEmail,
FromName = smtp.FromName,
AppDisplayName = smtp.AppDisplayName,
AppBaseUrl = smtp.AppBaseUrl
};
return View(vm);
}
// POST /admin/channels/save
[HttpPost("save")]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Save(EmailChannelEditViewModel model, CancellationToken ct)
{
if (!ModelState.IsValid)
return View("Edit", model);
var smtpSettings = new SmtpChannelSettings
{
Host = model.Host,
Port = model.Port,
Username = model.Username,
Password = model.Password,
UseSsl = model.UseSsl,
FromEmail = model.FromEmail,
FromName = model.FromName,
AppDisplayName = model.AppDisplayName,
AppBaseUrl = model.AppBaseUrl
};
if (model.IsNew)
{
var command = new CreateEmailChannelCommand(
model.ServiceName,
model.Priority,
EmailChannelType.Smtp,
smtpSettings,
model.DailyLimit,
model.MonthlyLimit,
model.WarnThresholdPercent,
model.IsActive);
var result = await _mediator.Send(command, ct);
if (!result.IsSuccess)
{
ModelState.AddModelError("", result.Error?.Message ?? "Failed to create channel.");
return View("Edit", model);
}
}
else
{
var command = new UpdateEmailChannelCommand(
model.Id,
model.Priority,
smtpSettings,
model.DailyLimit,
model.MonthlyLimit,
model.WarnThresholdPercent,
model.IsActive);
var result = await _mediator.Send(command, ct);
if (!result.IsSuccess)
{
ModelState.AddModelError("", result.Error?.Message ?? "Failed to update channel.");
return View("Edit", model);
}
}
return RedirectToAction(nameof(Index));
}
// POST /admin/channels/{id}/test
[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)
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." });
try
{
var message = new MimeMessage();
message.From.Add(new MailboxAddress(smtp.FromName, smtp.FromEmail));
message.To.Add(MailboxAddress.Parse(request.ToEmail));
message.Subject = "✅ Test email from Notification Service";
message.Body = new TextPart("plain")
{
Text = $"This is a test email sent from the Notification Service admin panel.\n\nChannel: {result.Result.ServiceName}\nHost: {smtp.Host}:{smtp.Port}"
};
using var client = new SmtpClient();
var secureSocket = smtp.UseSsl ? SecureSocketOptions.SslOnConnect : SecureSocketOptions.StartTlsWhenAvailable;
await client.ConnectAsync(smtp.Host, smtp.Port, secureSocket, ct);
if (!string.IsNullOrWhiteSpace(smtp.Username))
await client.AuthenticateAsync(smtp.Username, smtp.Password, ct);
await client.SendAsync(message, ct);
await client.DisconnectAsync(true, ct);
return Ok(new { success = true, message = $"Test email sent to {request.ToEmail}." });
}
catch (Exception ex)
{
return Ok(new { success = false, message = ex.Message });
}
}
// POST /admin/channels/{id}/delete
[HttpPost("{id:guid}/delete")]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Delete(Guid id, CancellationToken ct)
{
await _mediator.Send(new DeleteEmailChannelCommand(id), ct);
return RedirectToAction(nameof(Index));
}
}
public record TestChannelRequest(string ToEmail);