Fix: FHIR bundle processing has no rollback on partial failure
This commit is contained in:
@@ -1,7 +1,9 @@
|
||||
using Hl7.Fhir.Model;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
public class FhirBundleProcessor
|
||||
{
|
||||
private readonly AppDbContext _db;
|
||||
private readonly IPatientService _patients;
|
||||
private readonly IEncounterService _encounters;
|
||||
private readonly IObservationService _observations;
|
||||
@@ -12,6 +14,7 @@ public class FhirBundleProcessor
|
||||
private readonly MedicationAdministrationFhirMapper _medMapper;
|
||||
|
||||
public FhirBundleProcessor(
|
||||
AppDbContext db,
|
||||
IPatientService patients,
|
||||
IEncounterService encounters,
|
||||
IObservationService observations,
|
||||
@@ -21,6 +24,7 @@ public class FhirBundleProcessor
|
||||
ObservationFhirMapper observationMapper,
|
||||
MedicationAdministrationFhirMapper medMapper)
|
||||
{
|
||||
_db = db;
|
||||
_patients = patients;
|
||||
_encounters = encounters;
|
||||
_observations = observations;
|
||||
@@ -35,11 +39,12 @@ public class FhirBundleProcessor
|
||||
{
|
||||
var response = new Bundle { Type = Bundle.BundleType.TransactionResponse };
|
||||
|
||||
// Process in dependency order: Patient → Encounter → Observation/MedAdmin
|
||||
var entries = transaction.Entry
|
||||
.OrderBy(e => Priority(e.Resource))
|
||||
.ToList();
|
||||
|
||||
await using var tx = await _db.Database.BeginTransactionAsync();
|
||||
|
||||
foreach (var entry in entries)
|
||||
{
|
||||
var resource = entry.Resource;
|
||||
@@ -66,6 +71,8 @@ public class FhirBundleProcessor
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
await tx.RollbackAsync();
|
||||
|
||||
response.Entry.Add(new Bundle.EntryComponent
|
||||
{
|
||||
Response = new Bundle.ResponseComponent
|
||||
@@ -74,10 +81,11 @@ public class FhirBundleProcessor
|
||||
Outcome = FhirOperationOutcomeBuilder.FromException(ex)
|
||||
}
|
||||
});
|
||||
break; // transaction semantics — stop on first failure
|
||||
return response;
|
||||
}
|
||||
}
|
||||
|
||||
await tx.CommitAsync();
|
||||
return response;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user