using FrameworkBLL.EventSub;
using GB5Shared.DTO.PubSub;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using System;
using System.Threading;
using System.Threading.Tasks;
namespace FrameworkSL.Controllers.EventSub
{
///
/// Dapr pub/sub subscriber — receives the same EVENTTYPEID:{id} messages as
/// EventActionSubscribeController, but records them to the event log (audit trail).
///
/// Flow:
/// OutBox publishes OutboxEventMessage to Dapr topic "EVENTTYPEID:{id}"
/// → Dapr delivers to BOTH /eventsub/handle AND /eventlog/handle
/// → EventLogBLL writes audit entry to TEVENTLOG
///
/// Subscriptions registered dynamically by DaprSubscribeController (GET /dapr/subscribe).
/// No hardcoded topic names — every EventTypeId in MACTION auto-subscribes.
///
[ApiController]
public class EventLogSubscribeController : ControllerBase
{
private readonly IEventLogSubBLL _bll;
private readonly ILogger _logger;
public EventLogSubscribeController(
IEventLogSubBLL bll,
ILogger logger)
{
_bll = bll;
_logger = logger;
}
[HttpPost("/eventlog/handle")]
public async Task HandleEventLog(
[FromBody] OutboxEventMessage message,
CancellationToken ct)
{
_logger.LogInformation(
"EventLogSubscribe: received | EventTypeId={EventTypeId} TenantId={TenantId} ObjectId={ObjectId}",
message.EventTypeId, message.TenantId, message.ObjectId);
try
{
await _bll.RecordEventAsync(message, ct).ConfigureAwait(false);
return Ok(); // 200 = ACK to Dapr
}
catch (Exception ex)
{
_logger.LogError(ex,
"EventLogSubscribe: failed | EventTypeId={EventTypeId} TenantId={TenantId}",
message.EventTypeId, message.TenantId);
return StatusCode(500); // 500 = NACK → Dapr retries
}
}
}
}