refactor(infrastructure): align persistence with procedure result models
This commit is contained in:
@@ -1,288 +1,22 @@
|
||||
using DigitalData.TaskFlow.Application.Common.Interfaces;
|
||||
using DigitalData.TaskFlow.Domain.Closing;
|
||||
using DigitalData.TaskFlow.Domain.Workflows;
|
||||
using DigitalData.TaskFlow.Infrastructure.Options;
|
||||
using DigitalData.TaskFlow.Domain.ProcedureResults;
|
||||
using DigitalData.TaskFlow.Infrastructure.Persistence;
|
||||
using DigitalData.TaskFlow.Infrastructure.Persistence.Models;
|
||||
using Microsoft.Data.SqlClient;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.Options;
|
||||
using System.Text.Json;
|
||||
|
||||
namespace DigitalData.TaskFlow.Infrastructure.Repositories;
|
||||
|
||||
/// <summary>
|
||||
/// Workflow repository implementation for reading workflow listing data and applying close operations.
|
||||
/// </summary>
|
||||
public sealed class WorkflowRepository(TaskFlowDbContext dbContext, IOptions<DatabaseOptions> databaseOptions) : IWorkflowRepository
|
||||
public sealed class WorkflowRepository(TaskFlowDbContext context) : IWorkflowRepository
|
||||
{
|
||||
/// <summary>
|
||||
/// Retrieves grouped workflow data for a user.
|
||||
/// </summary>
|
||||
/// <param name="wisagId">WISAG user identifier.</param>
|
||||
/// <param name="cancellationToken">Cancellation token.</param>
|
||||
/// <returns>Grouped workflow response.</returns>
|
||||
public async Task<WorkflowSourcesResponse> GetAsync(int wisagId, CancellationToken cancellationToken)
|
||||
public async Task<Workflow?> GetByWisagIdAsync(int wisagId, CancellationToken cancellationToken)
|
||||
{
|
||||
var objectType = string.IsNullOrWhiteSpace(databaseOptions.Value.WorkflowListingObjectType)
|
||||
? "Ferdinand"
|
||||
: databaseOptions.Value.WorkflowListingObjectType;
|
||||
|
||||
var jsonPayload = await ExecuteListingJsonAsync(wisagId, objectType, cancellationToken);
|
||||
if (string.IsNullOrWhiteSpace(jsonPayload))
|
||||
{
|
||||
return new WorkflowSourcesResponse { Sources = [] };
|
||||
}
|
||||
|
||||
using var document = JsonDocument.Parse(jsonPayload);
|
||||
if (!document.RootElement.TryGetProperty("response", out var responseElement))
|
||||
{
|
||||
return new WorkflowSourcesResponse { Sources = [] };
|
||||
}
|
||||
|
||||
if (TryReadMetaCode(responseElement) == 2)
|
||||
{
|
||||
return new WorkflowSourcesResponse { Sources = [] };
|
||||
}
|
||||
|
||||
var sources = new List<SourceBlock>();
|
||||
if (!responseElement.TryGetProperty("sources", out var sourcesElement) || sourcesElement.ValueKind != JsonValueKind.Array)
|
||||
{
|
||||
return new WorkflowSourcesResponse { Sources = sources };
|
||||
}
|
||||
|
||||
foreach (var sourceElement in sourcesElement.EnumerateArray())
|
||||
{
|
||||
if (!sourceElement.TryGetProperty("source", out var sourceNameElement))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
var sourceName = sourceNameElement.GetString();
|
||||
if (string.Equals(sourceName, "Ferdinand", StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
sources.Add(new FerdinandSourceBlock
|
||||
{
|
||||
Source = "Ferdinand",
|
||||
Profile = ReadArray(sourceElement, "profile", "profiles"),
|
||||
Listing = ReadArray(sourceElement, "Listing", "Listings"),
|
||||
Approvals = ReadArray(sourceElement, "approvals"),
|
||||
Document = ReadArray(sourceElement, "document", "documents"),
|
||||
ClosingMethod = ReadArray(sourceElement, "closing_method", "closing_methods"),
|
||||
RejectionReason = ReadArray(sourceElement, "rejection_reason", "rejection_reasons")
|
||||
});
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
if (string.Equals(sourceName, "Contracts", StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
sources.Add(new ContractsSourceBlock
|
||||
{
|
||||
Source = "Contracts",
|
||||
Profile = ReadArray(sourceElement, "profile", "profiles"),
|
||||
Listing = ReadArray(sourceElement, "Listing", "Listings"),
|
||||
Approvals = ReadArray(sourceElement, "approvals"),
|
||||
Document = ReadArray(sourceElement, "document", "documents"),
|
||||
ClosingMethod = ReadArray(sourceElement, "closing_method", "closing_methods")
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
return new WorkflowSourcesResponse { Sources = sources };
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Applies a closing decision to a workflow item.
|
||||
/// </summary>
|
||||
/// <param name="wisagId">WISAG user identifier.</param>
|
||||
/// <param name="source">Source system name.</param>
|
||||
/// <param name="processId">Process identifier.</param>
|
||||
/// <param name="decision">Closing decision payload.</param>
|
||||
/// <param name="cancellationToken">Cancellation token.</param>
|
||||
/// <returns>Result of the close operation.</returns>
|
||||
public Task<CloseWorkflowResult> CloseAsync(
|
||||
int wisagId,
|
||||
string source,
|
||||
int processId,
|
||||
CloseWorkflowDecision decision,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
_ = decision;
|
||||
|
||||
if (!string.Equals(source, "Ferdinand", StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
return Task.FromResult(new CloseWorkflowResult
|
||||
{
|
||||
ProcessId = processId,
|
||||
IsClosed = false,
|
||||
Outcome = CloseWorkflowOutcome.NotFound
|
||||
});
|
||||
}
|
||||
|
||||
return EvaluateCloseOutcomeAsync(wisagId, source, processId, cancellationToken);
|
||||
}
|
||||
|
||||
private async Task<CloseWorkflowResult> EvaluateCloseOutcomeAsync(
|
||||
int wisagId,
|
||||
string source,
|
||||
int processId,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
var rows = await ExecuteListingTableAsync(wisagId, source, cancellationToken);
|
||||
if (rows.Any(row => row.ProcessId == processId))
|
||||
{
|
||||
return new CloseWorkflowResult
|
||||
{
|
||||
ProcessId = processId,
|
||||
IsClosed = true,
|
||||
Outcome = CloseWorkflowOutcome.Closed
|
||||
};
|
||||
}
|
||||
|
||||
var metaCode = await TryGetWorkflowMetaCodeAsync(wisagId, source, cancellationToken);
|
||||
if (metaCode == 2)
|
||||
{
|
||||
return new CloseWorkflowResult
|
||||
{
|
||||
ProcessId = processId,
|
||||
IsClosed = false,
|
||||
Outcome = CloseWorkflowOutcome.NoAccess
|
||||
};
|
||||
}
|
||||
|
||||
return new CloseWorkflowResult
|
||||
{
|
||||
ProcessId = 0,
|
||||
IsClosed = false,
|
||||
Outcome = CloseWorkflowOutcome.NotFound
|
||||
};
|
||||
}
|
||||
|
||||
private async Task<int?> TryGetWorkflowMetaCodeAsync(int wisagId, string objectType, CancellationToken cancellationToken)
|
||||
{
|
||||
var jsonPayload = await ExecuteListingJsonAsync(wisagId, objectType, cancellationToken);
|
||||
|
||||
if (string.IsNullOrWhiteSpace(jsonPayload))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
using var document = JsonDocument.Parse(jsonPayload);
|
||||
|
||||
return document.RootElement.TryGetProperty("response", out var responseElement)
|
||||
? TryReadMetaCode(responseElement)
|
||||
: null;
|
||||
}
|
||||
catch (JsonException)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private async Task<IReadOnlyList<WorkflowListingReadModel>> ExecuteListingTableAsync(
|
||||
int wisagId,
|
||||
string objectType,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
const string resultType = "table";
|
||||
|
||||
return await dbContext
|
||||
.WorkflowListingReadModels
|
||||
.FromSqlInterpolated($"EXEC dbo.PRCUST_F19_GET_LISTING_DATA @pUSER_ID={wisagId}, @pRESULT_TYPE={resultType}, @pOBJECT_TYPE={objectType}")
|
||||
var rows = await context.Workflows
|
||||
.FromSqlInterpolated(@$"EXEC [PRCUST_F19_GET_LISTING_DATA] @pUSER_ID = {wisagId}, @pRESULT_TYPE = 'json', @pOBJECT_TYPE = 'Ferdinand'")
|
||||
.AsNoTracking()
|
||||
.ToListAsync(cancellationToken);
|
||||
}
|
||||
|
||||
private async Task<string?> ExecuteListingJsonAsync(int wisagId, string objectType, CancellationToken cancellationToken)
|
||||
{
|
||||
const string commandText = "EXEC dbo.PRCUST_F19_GET_LISTING_DATA @pUSER_ID, @pRESULT_TYPE, @pOBJECT_TYPE";
|
||||
const string resultType = "json";
|
||||
|
||||
await using var command = dbContext.Database.GetDbConnection().CreateCommand();
|
||||
command.CommandText = commandText;
|
||||
|
||||
command.Parameters.Add(new SqlParameter("@pUSER_ID", wisagId));
|
||||
command.Parameters.Add(new SqlParameter("@pRESULT_TYPE", resultType));
|
||||
command.Parameters.Add(new SqlParameter("@pOBJECT_TYPE", objectType));
|
||||
|
||||
if (command.Connection is null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
if (command.Connection.State != System.Data.ConnectionState.Open)
|
||||
{
|
||||
await command.Connection.OpenAsync(cancellationToken);
|
||||
}
|
||||
|
||||
await using var reader = await command.ExecuteReaderAsync(cancellationToken);
|
||||
do
|
||||
{
|
||||
while (await reader.ReadAsync(cancellationToken))
|
||||
{
|
||||
for (var i = 0; i < reader.FieldCount; i++)
|
||||
{
|
||||
if (reader.IsDBNull(i))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
var candidate = reader.GetValue(i) as string;
|
||||
if (string.IsNullOrWhiteSpace(candidate))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
var jsonStart = candidate.IndexOf('{');
|
||||
if (jsonStart < 0)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
var jsonPayload = candidate[jsonStart..].Trim();
|
||||
if (!string.IsNullOrWhiteSpace(jsonPayload))
|
||||
{
|
||||
return jsonPayload;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
while (await reader.NextResultAsync(cancellationToken));
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
private static int? TryReadMetaCode(JsonElement responseElement)
|
||||
{
|
||||
if (!responseElement.TryGetProperty("meta", out var metaElement) || metaElement.ValueKind != JsonValueKind.Array)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
if (metaElement.GetArrayLength() == 0 || !metaElement[0].TryGetProperty("code", out var codeElement))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
return codeElement.GetInt32();
|
||||
}
|
||||
|
||||
private static IReadOnlyList<JsonElement> ReadArray(JsonElement sourceElement, params string[] propertyNames)
|
||||
{
|
||||
foreach (var propertyName in propertyNames)
|
||||
{
|
||||
if (!sourceElement.TryGetProperty(propertyName, out var propertyElement)
|
||||
|| propertyElement.ValueKind != JsonValueKind.Array)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
return propertyElement.EnumerateArray().Select(item => item.Clone()).ToList();
|
||||
}
|
||||
|
||||
return [];
|
||||
return rows.FirstOrDefault();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user