feat: add test button to create channel form using ad-hoc smtp test endpoint

- Add TestSmtpCommand and TestSmtpHandler for ad-hoc smtp testing without saving
- Add POST /admin/channels/test-smtp endpoint accepting raw smtp settings
- Show Test button on both Create and Edit forms
- Test reads current form values so channel can be tested before saving
This commit is contained in:
Anatolii Grynchuk
2026-05-02 19:53:20 +03:00
parent 3e1cc696c1
commit 6302a07178
4 changed files with 164 additions and 68 deletions
@@ -5,6 +5,7 @@ using HrynCo.NotificationService.Services.EmailChannels.Delete;
using HrynCo.NotificationService.Services.EmailChannels.Get;
using HrynCo.NotificationService.Services.EmailChannels.GetUsageSummary;
using HrynCo.NotificationService.Services.EmailChannels.Send;
using HrynCo.NotificationService.Services.EmailChannels.TestSmtp;
using HrynCo.NotificationService.Services.EmailChannels.Update;
using HrynCo.NotificationService.Web.Controllers.Admin.ViewModels;
using MediatR;
@@ -133,6 +134,20 @@ public class AdminChannelsController(IMediator mediator) : Controller
return RedirectToAction(nameof(Index));
}
// POST /admin/channels/test-smtp
[HttpPost("test-smtp")]
public async Task<IActionResult> TestSmtp([FromBody] TestSmtpRequest request, CancellationToken ct)
{
var result = await mediator.Send(new TestSmtpCommand(
request.Host, request.Port, request.Username, request.Password,
request.UseSsl, request.FromEmail, request.FromName, request.ToEmail), ct);
if (!result.IsSuccess)
return Ok(new { success = false, message = result.Error?.Message });
return Ok(new { success = true, message = $"Test email sent to {request.ToEmail}." });
}
// POST /admin/channels/{id}/test
[HttpPost("{id:guid}/test")]
public async Task<IActionResult> Test(Guid id, [FromBody] TestChannelRequest request, CancellationToken ct)
@@ -165,3 +180,6 @@ public class AdminChannelsController(IMediator mediator) : Controller
}
public record TestChannelRequest(string ToEmail);
public record TestSmtpRequest(
string Host, int Port, string Username, string Password,
bool UseSsl, string FromEmail, string FromName, string ToEmail);