using ComplianceBLL.ComplianceArtifact; using ComplianceDAL.DTO.CanonicalInvoice; using Dapr; using GB5Shared.DTO.Framework.Login; using Microsoft.AspNetCore.Mvc; namespace ComplianceSL.Subscriptions; [ApiController] public class InvoicePostedSubscriber : ControllerBase { private readonly IComplianceArtifactBLL _artifactBll; private readonly ILogger _logger; public InvoicePostedSubscriber(IComplianceArtifactBLL artifactBll, ILogger logger) { _artifactBll = artifactBll; _logger = logger; } // Triggered when MM/Accounts publishes compliance.invoice.posted. // Creates a ComplianceArtifact + Canonical payload for the invoice. [Topic("pubsub", "compliance.invoice.posted")] [HttpPost("/Compliance/Subscriptions/InvoicePosted")] public async Task HandleInvoicePostedAsync( [FromBody] InvoicePostedEvent evt, CancellationToken ct) { _logger.LogInformation( "Invoice posted event received for DocumentId {DocumentId} GSTIN {GSTIN}", evt.DocumentId, evt.GSTIN); try { var login = new LoginDTO { ClientId = evt.ClientId, DatabaseName = evt.DatabaseName, UserId = evt.UserId }; var artifactId = await _artifactBll.CreateArtifactFromInvoiceAsync(evt, login, ct); _logger.LogInformation( "Artifact {ArtifactId} created for DocumentId {DocumentId}", artifactId, evt.DocumentId); return Ok(new { ArtifactId = artifactId }); } catch (Exception ex) { _logger.LogError(ex, "Failed to process invoice.posted for DocumentId {DocumentId}", evt.DocumentId); return StatusCode(500, ex.Message); } } }