using FrameworkBLL.SchedulerTaskGeneratorPublisher;
using GB5Shared.ActionProcessor;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using System.Text.Json;
namespace FrameworkSL.Controllers.ActionProcessor
{
[ApiController]
[Route("ActionProcessor")]
public class EmailActionTestController : ControllerBase
{
private readonly IServiceProvider _sp;
private readonly IConfiguration _config;
public EmailActionTestController(IServiceProvider sp, IConfiguration config)
{
_sp = sp;
_config = config;
}
///
/// Full flow: publishes ActionEventDto (ActionType=0) to action-exec-p{n}
/// → ActionProcessorWorker → EmailActionHandler → SMTP.
/// TenantId is read from SchedulerRun:ClientId in config.
///
[HttpPost("TestEmailAction")]
public async Task TestEmailAction(
[FromBody] TestEmailRequest req,
CancellationToken ct)
{
var publisher = _sp.GetService();
if (publisher is null)
return StatusCode(503, "IRabbitMqPublisher not registered. Set SchedulerRun:Enable=Y in SchedulerRun.json.");
if (string.IsNullOrWhiteSpace(req.To))
return BadRequest("'To' email is required.");
// Read TenantId from config — same ClientId ActionProcessorWorker resolves against MSERVERCONFIG
if (!int.TryParse(_config["SchedulerRun:ClientId"], out int tenantId) || tenantId == 0)
return StatusCode(500, "SchedulerRun:ClientId is missing or invalid in SchedulerRun.json.");
var actionRunId = (int)(DateTimeOffset.UtcNow.ToUnixTimeMilliseconds() % int.MaxValue);
var partition = Math.Abs(tenantId % 5);
var queue = $"action-exec-p{partition}";
var dto = new ActionEventDto
{
ActionRunId = actionRunId,
ActionId = 0,
ActionType = 0, // Email
TenantId = tenantId,
SendTo = req.To,
MailCc = req.Cc,
MailBcc = req.Bcc,
CorrelationKey = $"email-test-{actionRunId}",
Payload = JsonSerializer.SerializeToElement(new
{
Subject = req.Subject ?? "GB5 Email Action Test",
Body = req.Body ?? "Test email from GB5 Action Processor via RabbitMQ.",
IsHtml = req.IsHtml
})
};
await publisher.PublishAsync(queue, dto);
return Ok(new
{
Message = $"Published to '{queue}' → ActionProcessorWorker → EmailActionHandler → SMTP",
ActionRunId = actionRunId,
Queue = queue,
TenantId = tenantId,
SendTo = req.To
});
}
}
public class TestEmailRequest
{
public string To { get; set; } = string.Empty;
public string? Cc { get; set; }
public string? Bcc { get; set; }
public string? Subject { get; set; }
public string? Body { get; set; }
public bool IsHtml { get; set; } = false;
}
}