using Dapr; using EntitlementBLL.Common; using EntitlementBLL.Interfaces; using Microsoft.AspNetCore.Mvc; namespace EntitlementSL.Subscriptions; // idms.engagement.completed → log ENGAGEMENT_COMPLETED only. No status/entitlement change — // engagement completion is an IDMS-side delivery milestone, distinct from GoLive (commercial // activation) and distinct from subscription lifecycle status. [ApiController] public class EngagementCompletedSubscriber : ControllerBase { private readonly IEntitlementAuditBLL _AuditBLL; private readonly ISubscriptionService _SubscriptionService; private readonly IEntitlementLoginFactory _LoginFactory; private readonly ILogger _Logger; public EngagementCompletedSubscriber( IEntitlementAuditBLL auditBLL, ISubscriptionService subscriptionService, IEntitlementLoginFactory loginFactory, ILogger logger) { _AuditBLL = auditBLL; _SubscriptionService = subscriptionService; _LoginFactory = loginFactory; _Logger = logger; } [Topic("goodbooks-pubsub", "idms.engagement.completed")] [HttpPost("/lic/Subscriptions/EngagementCompleted")] public async Task HandleAsync([FromBody] EngagementCompletedEvent evt, CancellationToken ct) { try { var sub = await _SubscriptionService.GetAsync(evt.ClientId, ct); var login = _LoginFactory.Create(evt.ClientId); await _AuditBLL.LogActionAsync(evt.ClientId, sub.SubscriptionId, "ENGAGEMENT_COMPLETED", $"EngagementRef={evt.EngagementRef}", login, ct); _Logger.LogInformation("Engagement completed logged for ClientId {ClientId}", evt.ClientId); return Ok(); } catch (Exception ex) { _Logger.LogError(ex, "EngagementCompletedSubscriber failed for ClientId {ClientId}", evt.ClientId); return StatusCode(500, ex.Message); } } public record EngagementCompletedEvent(int ClientId, string EngagementRef); }