using ComplianceDAL.DTO.ComplianceArtifact; using ComplianceDAL.Query.ComplianceArtifact; using GB5Shared.DTO.Framework.Login; using GB5Shared.QueryExecutor; using Microsoft.Extensions.Logging; using Newtonsoft.Json; namespace ComplianceDAL.CustomCode.ComplianceArtifact; public class ComplianceArtifactDAL : IComplianceArtifactDAL { private readonly IQueryExecutor _queryExecutor; private readonly ILogger _logger; public ComplianceArtifactDAL(IQueryExecutor queryExecutor, ILogger logger) { _queryExecutor = queryExecutor; _logger = logger; } public async Task SaveArtifact(ComplianceArtifactDTO dto, LoginDTO login, CancellationToken ct) { try { return await _queryExecutor.ExecuteIdentityAsync( login, ComplianceArtifactQB.SAVE_ARTIFACT, new { dto.ArtifactType, dto.Framework, dto.SourceEntityType, dto.SourceEntityId, dto.GSTIN, dto.ReturnPeriod, dto.Status, dto.AttemptCount, dto.CreatedById, dto.ModifiedById, login.ClientId, login.DatabaseName }).ConfigureAwait(false); } catch (Exception ex) { _logger.LogError(ex, "ComplianceArtifact DAL: SaveArtifact failed for SourceEntityType {SourceEntityType}, SourceEntityId {SourceEntityId}", dto.SourceEntityType, dto.SourceEntityId); throw; } } public async Task UpdateArtifactStatus(int artifactId, string status, int attemptCount, int modifiedById, LoginDTO login, CancellationToken ct) { try { await _queryExecutor.ExecuteAsync( login, ComplianceArtifactQB.UPDATE_ARTIFACT_STATUS, new { ArtifactId = artifactId, Status = status, AttemptCount = attemptCount, ModifiedById = modifiedById, login.ClientId, login.DatabaseName }, cancellationToken: ct).ConfigureAwait(false); } catch (Exception ex) { _logger.LogError(ex, "ComplianceArtifact DAL: UpdateArtifactStatus failed for ArtifactId {ArtifactId}, Status {Status}", artifactId, status); throw; } } public async Task GetArtifact(int artifactId, LoginDTO login, CancellationToken ct) { try { return await _queryExecutor.QuerySingleAsync( login, ComplianceArtifactQB.GET_ARTIFACT, new { ArtifactId = artifactId }).ConfigureAwait(false); } catch (Exception ex) { _logger.LogError(ex, "ComplianceArtifact DAL: GetArtifact failed for ArtifactId {ArtifactId}", artifactId); throw; } } public async Task GetArtifactBySource(string sourceEntityType, int sourceEntityId, string artifactType, LoginDTO login, CancellationToken ct) { try { return await _queryExecutor.QuerySingleAsync( login, ComplianceArtifactQB.GET_ARTIFACT_BY_SOURCE, new { SourceEntityType = sourceEntityType, SourceEntityId = sourceEntityId, ArtifactType = artifactType, ClientId= login.ClientId }).ConfigureAwait(false); } catch (Exception ex) { _logger.LogError(ex, "ComplianceArtifact DAL: GetArtifactBySource failed for SourceEntityType {SourceEntityType}, SourceEntityId {SourceEntityId}, ArtifactType {ArtifactType}", sourceEntityType, sourceEntityId, artifactType); throw; } } public async Task SavePayload(CompliancePayloadDTO dto, LoginDTO login, CancellationToken ct) { try { await _queryExecutor.ExecuteAsync( login, ComplianceArtifactQB.SAVE_PAYLOAD, new { dto.ArtifactId, dto.PayloadType, dto.PayloadFormat, dto.Payload, dto.AttemptNumber, dto.SignedById, dto.SignedOn, login.ClientId, login.DatabaseName }, cancellationToken: ct).ConfigureAwait(false); } catch (Exception ex) { _logger.LogError(ex, "ComplianceArtifact DAL: SavePayload failed for ArtifactId {ArtifactId}, PayloadType {PayloadType}", dto.ArtifactId, dto.PayloadType); throw; } } public async Task SaveAudit(ComplianceAuditDTO dto, LoginDTO login, CancellationToken ct) { try { await _queryExecutor.ExecuteAsync( login, ComplianceArtifactQB.SAVE_AUDIT, new { dto.ArtifactId, dto.Action, dto.RequestPayload, dto.ResponsePayload, dto.PreviousState, dto.NewState, dto.ProviderCode, dto.HttpStatusCode, dto.ReferenceNumber, dto.ErrorMessage, dto.AttemptNumber, dto.UserId, login.ClientId, login.DatabaseName }, cancellationToken: ct).ConfigureAwait(false); } catch (Exception ex) { _logger.LogError(ex, "ComplianceArtifact DAL: SaveAudit failed for ArtifactId {ArtifactId}, Action {Action}", dto.ArtifactId, dto.Action); throw; } } public async Task GetAuditLog(int artifactId, int page, int pageSize, LoginDTO login, CancellationToken ct) { try { var result = await _queryExecutor.QueryAsync( login, ComplianceArtifactQB.GET_AUDIT_LOG, new { ArtifactId = artifactId, login.ClientId, login.DatabaseName }, cancellationToken: ct).ConfigureAwait(false); return JsonConvert.SerializeObject(result); } catch (Exception ex) { _logger.LogError(ex, "ComplianceArtifact DAL: GetAuditLog failed for ArtifactId {ArtifactId}", artifactId); throw; } } }