@@ -17,16 +17,19 @@ public class PatientEventsController : Controller
1717 private readonly ILogger < PatientEventsController > _logger ;
1818 private readonly IPatientEventManager _patientEventManager ;
1919 private readonly IPatientEventQueries _patientEventQueries ;
20+ private readonly IPatientEncounterQueries _patientEncounterQueries ;
2021
2122 public PatientEventsController (
2223 ILogger < PatientEventsController > logger ,
2324 IPatientEventManager patientEventManager ,
24- IPatientEventQueries patientEventQueries
25+ IPatientEventQueries patientEventQueries ,
26+ IPatientEncounterQueries patientEncounterQueries
2527 )
2628 {
2729 _logger = logger ?? throw new ArgumentNullException ( nameof ( logger ) ) ;
2830 _patientEventManager = patientEventManager ?? throw new ArgumentNullException ( nameof ( patientEventManager ) ) ;
2931 _patientEventQueries = patientEventQueries ?? throw new ArgumentNullException ( nameof ( patientEventQueries ) ) ;
32+ _patientEncounterQueries = patientEncounterQueries ?? throw new ArgumentNullException ( nameof ( patientEncounterQueries ) ) ;
3033 }
3134
3235 /// <summary>
@@ -88,7 +91,7 @@ public async Task<ActionResult<PagedConfigModel<PatientEventModel>>> GetPatientE
8891 }
8992
9093 /// <summary>
91- /// Soft deletes a patient event and rebuilds the materialized view for the related correlation id.
94+ /// Deletes a patient event and rebuilds the materialized view for the related correlation id.
9295 /// </summary>
9396 /// <remarks>
9497 /// DELETE: api/patient-events/{id}
@@ -104,13 +107,31 @@ public async Task<IActionResult> DeletePatientEvent(string id, CancellationToken
104107 return BadRequest ( "Patient event ID is required." ) ;
105108 }
106109
110+ using var transaction = await _patientEventQueries . StartTransaction ( ) ;
107111 try
108112 {
113+ var patientEventFaciltyId = ( await _patientEventQueries . GetPatientEventById ( id , cancellationToken ) ) ? . FacilityId ;
114+
115+ if ( string . IsNullOrWhiteSpace ( patientEventFaciltyId ) )
116+ {
117+ await transaction . RollbackAsync ( cancellationToken ) ;
118+ return NotFound ( $ "Patient event with ID { id } not found.") ;
119+ }
120+
109121 await _patientEventManager . DeletePatientEventById ( id , cancellationToken ) ;
122+ await _patientEncounterQueries . RebuildPatientEncounterTable (
123+ facilityId : patientEventFaciltyId ,
124+ correlationId : null ,
125+ useTransaction : false ,
126+ cancellationToken : cancellationToken
127+ ) ;
128+
129+ await transaction . CommitAsync ( cancellationToken ) ;
110130 }
111131 catch ( Exception ex )
112132 {
113133 _logger . LogError ( ex , "Error deleting patient event with ID {Id}" , id ? . Replace ( "\r " , "" ) . Replace ( "\n " , "" ) ) ;
134+ await transaction . RollbackAsync ( cancellationToken ) ;
114135 return Problem (
115136 detail : "An error occurred while processing your request." ,
116137 statusCode : StatusCodes . Status500InternalServerError
@@ -121,7 +142,7 @@ public async Task<IActionResult> DeletePatientEvent(string id, CancellationToken
121142 }
122143
123144 /// <summary>
124- /// Soft deletes the patient event store for the given correlation id and removes the corresponding materialized view.
145+ /// Deletes the patient event store for the given correlation id and removes the corresponding materialized view.
125146 /// </summary>
126147 /// <remarks>
127148 /// DELETE: api/patient-events/visit/{correlationId}
@@ -136,15 +157,24 @@ public async Task<IActionResult> DeletePatientEventsByCorrelation(string correla
136157 {
137158 return BadRequest ( "Correlation ID is required." ) ;
138159 }
139-
160+
161+ using var transaction = await _patientEventQueries . StartTransaction ( ) ;
140162 try
141163 {
142164 await _patientEventQueries . DeletePatientEventByCorrelationId ( correlationId , cancellationToken ) ;
165+ await _patientEncounterQueries . RebuildPatientEncounterTable (
166+ facilityId : null ,
167+ correlationId : correlationId ,
168+ useTransaction : false ,
169+ cancellationToken : cancellationToken
170+ ) ;
171+ await transaction . CommitAsync ( cancellationToken ) ;
143172
144173 }
145174 catch ( Exception ex )
146175 {
147176 _logger . LogError ( ex , "Error deleting patient events for correlation ID {CorrelationId}" , correlationId ? . Replace ( "\r " , "" ) . Replace ( "\n " , "" ) ) ;
177+ await transaction . RollbackAsync ( cancellationToken ) ;
148178 return Problem (
149179 detail : "An error occurred while processing your request." ,
150180 statusCode : StatusCodes . Status500InternalServerError
0 commit comments