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:
2026-10-26 10:12:02 +01:00
parent 232491d4ac
commit 405215a8bf
2 changed files with 56 additions and 17 deletions

View File

@@ -87,6 +87,11 @@ public sealed class CloseWorkflowCommandHandler(IWorkflowRepository workflowRepo
/// <returns>Closing result envelope.</returns> /// <returns>Closing result envelope.</returns>
public async Task<ApiEnvelope<CloseWorkflowResult>> Handle(CloseWorkflowCommand request, CancellationToken cancellationToken) 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 decision = mapper.Map<CloseWorkflowDecision>(request);
var result = await workflowRepository.CloseAsync( var result = await workflowRepository.CloseAsync(

View File

@@ -64,12 +64,12 @@ public sealed class WorkflowRepository(TaskFlowDbContext dbContext, IOptions<Dat
sources.Add(new FerdinandSourceBlock sources.Add(new FerdinandSourceBlock
{ {
Source = "Ferdinand", Source = "Ferdinand",
Profile = ReadArray(sourceElement, "profile"), Profile = ReadArray(sourceElement, "profile", "profiles"),
Listing = ReadArray(sourceElement, "Listing"), Listing = ReadArray(sourceElement, "Listing", "Listings"),
Approvals = ReadArray(sourceElement, "approvals"), Approvals = ReadArray(sourceElement, "approvals"),
Document = ReadArray(sourceElement, "document"), Document = ReadArray(sourceElement, "document", "documents"),
ClosingMethod = ReadArray(sourceElement, "closing_method"), ClosingMethod = ReadArray(sourceElement, "closing_method", "closing_methods"),
RejectionReason = ReadArray(sourceElement, "rejection_reason") RejectionReason = ReadArray(sourceElement, "rejection_reason", "rejection_reasons")
}); });
continue; continue;
@@ -80,11 +80,11 @@ public sealed class WorkflowRepository(TaskFlowDbContext dbContext, IOptions<Dat
sources.Add(new ContractsSourceBlock sources.Add(new ContractsSourceBlock
{ {
Source = "Contracts", Source = "Contracts",
Profile = ReadArray(sourceElement, "profile"), Profile = ReadArray(sourceElement, "profile", "profiles"),
Listing = ReadArray(sourceElement, "Listing"), Listing = ReadArray(sourceElement, "Listing", "Listings"),
Approvals = ReadArray(sourceElement, "approvals"), Approvals = ReadArray(sourceElement, "approvals"),
Document = ReadArray(sourceElement, "document"), Document = ReadArray(sourceElement, "document", "documents"),
ClosingMethod = ReadArray(sourceElement, "closing_method") 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); 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) private static int? TryReadMetaCode(JsonElement responseElement)
@@ -242,13 +270,19 @@ public sealed class WorkflowRepository(TaskFlowDbContext dbContext, IOptions<Dat
return codeElement.GetInt32(); 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 [];
} }
} }