Enhance validation and data reading logic
Add validation in CloseWorkflowCommandHandler to reject "Contracts" as a source for closing operations. Modify ReadArray in WorkflowRepository to accept multiple property names, improving flexibility in data retrieval. Refactor database reader logic to extract JSON payloads more robustly, iterating over all fields and results.
This commit is contained in:
@@ -87,6 +87,11 @@ public sealed class CloseWorkflowCommandHandler(IWorkflowRepository workflowRepo
|
||||
/// <returns>Closing result envelope.</returns>
|
||||
public async Task<ApiEnvelope<CloseWorkflowResult>> Handle(CloseWorkflowCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
if (string.Equals(request.Source, "Contracts", StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
throw new InvalidRequestApiException("Quelle 'Contracts' wird fur Closing aktuell nicht unterstutzt");
|
||||
}
|
||||
|
||||
var decision = mapper.Map<CloseWorkflowDecision>(request);
|
||||
|
||||
var result = await workflowRepository.CloseAsync(
|
||||
|
||||
@@ -64,12 +64,12 @@ public sealed class WorkflowRepository(TaskFlowDbContext dbContext, IOptions<Dat
|
||||
sources.Add(new FerdinandSourceBlock
|
||||
{
|
||||
Source = "Ferdinand",
|
||||
Profile = ReadArray(sourceElement, "profile"),
|
||||
Listing = ReadArray(sourceElement, "Listing"),
|
||||
Profile = ReadArray(sourceElement, "profile", "profiles"),
|
||||
Listing = ReadArray(sourceElement, "Listing", "Listings"),
|
||||
Approvals = ReadArray(sourceElement, "approvals"),
|
||||
Document = ReadArray(sourceElement, "document"),
|
||||
ClosingMethod = ReadArray(sourceElement, "closing_method"),
|
||||
RejectionReason = ReadArray(sourceElement, "rejection_reason")
|
||||
Document = ReadArray(sourceElement, "document", "documents"),
|
||||
ClosingMethod = ReadArray(sourceElement, "closing_method", "closing_methods"),
|
||||
RejectionReason = ReadArray(sourceElement, "rejection_reason", "rejection_reasons")
|
||||
});
|
||||
|
||||
continue;
|
||||
@@ -80,11 +80,11 @@ public sealed class WorkflowRepository(TaskFlowDbContext dbContext, IOptions<Dat
|
||||
sources.Add(new ContractsSourceBlock
|
||||
{
|
||||
Source = "Contracts",
|
||||
Profile = ReadArray(sourceElement, "profile"),
|
||||
Listing = ReadArray(sourceElement, "Listing"),
|
||||
Profile = ReadArray(sourceElement, "profile", "profiles"),
|
||||
Listing = ReadArray(sourceElement, "Listing", "Listings"),
|
||||
Approvals = ReadArray(sourceElement, "approvals"),
|
||||
Document = ReadArray(sourceElement, "document"),
|
||||
ClosingMethod = ReadArray(sourceElement, "closing_method")
|
||||
Document = ReadArray(sourceElement, "document", "documents"),
|
||||
ClosingMethod = ReadArray(sourceElement, "closing_method", "closing_methods")
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -219,12 +219,40 @@ public sealed class WorkflowRepository(TaskFlowDbContext dbContext, IOptions<Dat
|
||||
}
|
||||
|
||||
await using var reader = await command.ExecuteReaderAsync(cancellationToken);
|
||||
if (!await reader.ReadAsync(cancellationToken) || reader.FieldCount == 0 || reader.IsDBNull(0))
|
||||
do
|
||||
{
|
||||
return null;
|
||||
}
|
||||
while (await reader.ReadAsync(cancellationToken))
|
||||
{
|
||||
for (var i = 0; i < reader.FieldCount; i++)
|
||||
{
|
||||
if (reader.IsDBNull(i))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
return reader.GetString(0);
|
||||
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)
|
||||
@@ -242,13 +270,19 @@ public sealed class WorkflowRepository(TaskFlowDbContext dbContext, IOptions<Dat
|
||||
return codeElement.GetInt32();
|
||||
}
|
||||
|
||||
private static IReadOnlyList<JsonElement> ReadArray(JsonElement sourceElement, string propertyName)
|
||||
private static IReadOnlyList<JsonElement> ReadArray(JsonElement sourceElement, params string[] propertyNames)
|
||||
{
|
||||
if (!sourceElement.TryGetProperty(propertyName, out var propertyElement) || propertyElement.ValueKind != JsonValueKind.Array)
|
||||
foreach (var propertyName in propertyNames)
|
||||
{
|
||||
return [];
|
||||
if (!sourceElement.TryGetProperty(propertyName, out var propertyElement)
|
||||
|| propertyElement.ValueKind != JsonValueKind.Array)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
return propertyElement.EnumerateArray().Select(item => item.Clone()).ToList();
|
||||
}
|
||||
|
||||
return propertyElement.EnumerateArray().Select(item => item.Clone()).ToList();
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user