Skip to content

Commit f5adfdf

Browse files
Merge remote-tracking branch 'origin/LNK-4524-fix-LogOutInAdminUi' into LNK-4524-fix-LogOutInAdminUi
2 parents dad342e + 94d774e commit f5adfdf

8 files changed

Lines changed: 282 additions & 197 deletions

File tree

DotNet/Census/Application/Factories/PatientEncounterBuilder.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public PatientEncounterBuilder(string facilityId, string MRN, DateTime admitDate
1313
_patientEncounter.MedicalRecordNumber = MRN;
1414
_patientEncounter.AdmitDate = admitDate;
1515

16-
if (dischargeDate != null)
16+
if (dischargeDate != default)
1717
{
1818
_patientEncounter.DischargeDate = dischargeDate;
1919
}

DotNet/Census/Controllers/PatientEncountersController.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ public async Task<ActionResult<PagedConfigModel<PatientEncounterModel>>> GetHist
155155
[HttpPost("rebuild")]
156156
public async Task<IActionResult> RebuildMaterializedView(
157157
string facilityId,
158-
string? correlationId = null,
158+
string? correlationId = default,
159159
CancellationToken cancellationToken = default)
160160
{
161161
facilityId = HtmlInputSanitizer.SanitizeAndRemove(facilityId ?? string.Empty);
@@ -166,7 +166,7 @@ public async Task<IActionResult> RebuildMaterializedView(
166166

167167
try
168168
{
169-
await _patientEncounterQueries.RebuildPatientEncounterTable(cancellationToken);
169+
await _patientEncounterQueries.RebuildPatientEncounterTable(facilityId, correlationId,cancellationToken: cancellationToken);
170170
}
171171
catch (Exception ex)
172172
{

DotNet/Census/Controllers/PatientEventsController.cs

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -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

DotNet/Census/Domain/Converters/PayloadJsonConverter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ public override IPayload Read(ref Utf8JsonReader reader, Type typeToConvert, Jso
7272
if (rootElement.TryGetProperty("patientId", out patientIdProp))
7373
patientId = patientIdProp.GetString();
7474

75-
if (rootElement.TryGetProperty("admitDate", out var dischargeDateProp) &&
75+
if (rootElement.TryGetProperty("dischargeDate", out var dischargeDateProp) &&
7676
dischargeDateProp.ValueKind == JsonValueKind.String)
7777
{
7878
DateTime.TryParse(dischargeDateProp.GetString(), out dischargeDate);

DotNet/Census/Domain/Managers/PatientEventManager.cs

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,15 @@ public interface IPatientEventManager
1212
public class PatientEventManager : IPatientEventManager
1313
{
1414
private readonly IBaseEntityRepository<PatientEvent> _patientEventRepository;
15+
private readonly IPatientEncounterManager _patientEncounterManager;
1516

16-
public PatientEventManager(IBaseEntityRepository<PatientEvent> patientEventRepository)
17+
public PatientEventManager(
18+
IBaseEntityRepository<PatientEvent> patientEventRepository,
19+
IPatientEncounterManager patientEncounterManager
20+
)
1721
{
1822
_patientEventRepository = patientEventRepository ?? throw new ArgumentNullException(nameof(patientEventRepository));
23+
_patientEncounterManager = patientEncounterManager ?? throw new ArgumentNullException(nameof(patientEncounterManager));
1924
}
2025

2126
public async Task<List<PatientEvent>> GetByFacilityIdAndPatientId(string facilityId, string patientId, CancellationToken cancellationToken)
@@ -45,12 +50,20 @@ public async Task<PatientEvent> AddPatientEvent(PatientEvent patientEvent, Cance
4550
return await _patientEventRepository.AddAsync(patientEvent, cancellationToken);
4651
}
4752

48-
public Task DeletePatientEventById(string id, CancellationToken cancellationToken)
53+
public async Task DeletePatientEventById(string id, CancellationToken cancellationToken)
4954
{
5055
if (string.IsNullOrWhiteSpace(id))
5156
{
5257
throw new ArgumentException("Patient event ID cannot be null or empty.", nameof(id));
5358
}
54-
return _patientEventRepository.DeleteAsync(id, cancellationToken);
59+
60+
var toBeDeletedEvent = await _patientEventRepository.GetAsync(id, cancellationToken);
61+
62+
if (toBeDeletedEvent == null)
63+
{
64+
throw new Exception($"Patient event with ID {id} not found.");
65+
}
66+
67+
await _patientEventRepository.DeleteAsync(id, cancellationToken);
5568
}
5669
}

0 commit comments

Comments
 (0)