From e4379ad2f46ce4bf97c113a9d52c6e9f2a6854cd Mon Sep 17 00:00:00 2001 From: Steffen Date: Fri, 2 Oct 2026 09:31:58 +0200 Subject: [PATCH] Implement US-08 blood-pressure observation corrections --- .../Controllers/ObservationController.cs | 6 +- .../ObservationApplicationService.cs | 10 ++ .../Application/ObservationRepository.cs | 2 + .../Contracts/UpdateObservationRequest.cs | 15 +++ .../Controllers/ObservationController.cs | 8 ++ .../Persistence/SqlObservationRepository.cs | 16 +++ src/README.md | 6 +- .../UpdateObservationTests.cs | 121 ++++++++++++++++++ 8 files changed, 182 insertions(+), 2 deletions(-) create mode 100644 src/ObservationService/Contracts/UpdateObservationRequest.cs create mode 100644 tests/Observation.AcceptanceTests/UpdateObservationTests.cs diff --git a/src/ApiGateway/Controllers/ObservationController.cs b/src/ApiGateway/Controllers/ObservationController.cs index 614c6e4..428e2af 100644 --- a/src/ApiGateway/Controllers/ObservationController.cs +++ b/src/ApiGateway/Controllers/ObservationController.cs @@ -10,6 +10,10 @@ public sealed class ObservationController(IHttpClientFactory clients) : Controll public Task RecordObservation(Guid patientId, CancellationToken cancellationToken) => ForwardAsync(HttpMethod.Post, $"api/patients/{patientId}/observations", cancellationToken); + [HttpPut("observations/{observationId:guid}")] + public Task UpdateObservation(Guid observationId, CancellationToken cancellationToken) => + ForwardAsync(HttpMethod.Put, $"api/observations/{observationId}", cancellationToken); + [HttpGet("observations/{observationId:guid}")] public Task GetObservation(Guid observationId, CancellationToken cancellationToken) => ForwardAsync(HttpMethod.Get, $"api/observations/{observationId}", cancellationToken); @@ -25,7 +29,7 @@ public sealed class ObservationController(IHttpClientFactory clients) : Controll private async Task ForwardAsync(HttpMethod method, string path, CancellationToken cancellationToken) { using var request = new HttpRequestMessage(method, path); - if (method == HttpMethod.Post) + if (method == HttpMethod.Post || method == HttpMethod.Put) { request.Content = new StreamContent(Request.Body); if (Request.ContentType is not null) diff --git a/src/ObservationService/Application/ObservationApplicationService.cs b/src/ObservationService/Application/ObservationApplicationService.cs index b1cfe11..d48a0b6 100644 --- a/src/ObservationService/Application/ObservationApplicationService.cs +++ b/src/ObservationService/Application/ObservationApplicationService.cs @@ -6,6 +6,16 @@ namespace ObservationService.Application; public sealed class ObservationApplicationService(ObservationRepository repository, IHttpClientFactory clients) { + public Task UpdateAsync(Guid observationId, UpdateObservationRequest request, + CancellationToken cancellationToken) + { + if (request.Systolic is null or <= 0 || request.Diastolic is null or <= 0 || request.Pulse is null or <= 0) + throw new ArgumentException("Positive measured values are required.", nameof(request)); + + return repository.UpdateValuesAsync(observationId, request.Systolic.Value, request.Diastolic.Value, + request.Pulse.Value, DateTime.SpecifyKind(DateTime.UtcNow, DateTimeKind.Unspecified), cancellationToken); + } + public async Task RecordAsync(Guid patientId, RecordObservationRequest request, CancellationToken cancellationToken) { diff --git a/src/ObservationService/Application/ObservationRepository.cs b/src/ObservationService/Application/ObservationRepository.cs index b34fee3..0f5dcb8 100644 --- a/src/ObservationService/Application/ObservationRepository.cs +++ b/src/ObservationService/Application/ObservationRepository.cs @@ -4,6 +4,8 @@ namespace ObservationService.Application; public interface ObservationRepository { + Task UpdateValuesAsync(Guid observationId, int systolic, int diastolic, int pulse, + DateTime updatedAt, CancellationToken cancellationToken); Task SaveAsync(BloodPressureObservation observation, CancellationToken cancellationToken); Task FindByIdAsync(Guid observationId, CancellationToken cancellationToken); Task FindLatestByPatientIdAsync(Guid patientId, CancellationToken cancellationToken); diff --git a/src/ObservationService/Contracts/UpdateObservationRequest.cs b/src/ObservationService/Contracts/UpdateObservationRequest.cs new file mode 100644 index 0000000..4c64e5c --- /dev/null +++ b/src/ObservationService/Contracts/UpdateObservationRequest.cs @@ -0,0 +1,15 @@ +using System.ComponentModel.DataAnnotations; + +namespace ObservationService.Contracts; + +public sealed record UpdateObservationRequest +{ + [Required, Range(1, int.MaxValue)] + public int? Systolic { get; init; } + + [Required, Range(1, int.MaxValue)] + public int? Diastolic { get; init; } + + [Required, Range(1, int.MaxValue)] + public int? Pulse { get; init; } +} diff --git a/src/ObservationService/Controllers/ObservationController.cs b/src/ObservationService/Controllers/ObservationController.cs index d6dbc85..ca8c742 100644 --- a/src/ObservationService/Controllers/ObservationController.cs +++ b/src/ObservationService/Controllers/ObservationController.cs @@ -31,6 +31,14 @@ public sealed class ObservationController(ObservationApplicationService service) } } + [HttpPut("observations/{observationId:guid}")] + public async Task> UpdateObservation(Guid observationId, UpdateObservationRequest request, + CancellationToken cancellationToken) + { + var observation = await service.UpdateAsync(observationId, request, cancellationToken); + return observation is null ? NotFound() : Ok(ToDto(observation)); + } + [HttpGet("observations/{observationId:guid}")] public async Task> GetObservation(Guid observationId, CancellationToken cancellationToken) { diff --git a/src/ObservationService/Persistence/SqlObservationRepository.cs b/src/ObservationService/Persistence/SqlObservationRepository.cs index ecdc62d..bc7479e 100644 --- a/src/ObservationService/Persistence/SqlObservationRepository.cs +++ b/src/ObservationService/Persistence/SqlObservationRepository.cs @@ -6,6 +6,22 @@ namespace ObservationService.Persistence; public sealed class SqlObservationRepository(ObservationDbContext db) : ObservationRepository { + public async Task UpdateValuesAsync(Guid observationId, int systolic, int diastolic, + int pulse, DateTime updatedAt, CancellationToken cancellationToken) + { + var observation = await db.Observations.SingleOrDefaultAsync(o => o.ObservationId == observationId, + cancellationToken); + if (observation is null) + return null; + + observation.Systolic = systolic; + observation.Diastolic = diastolic; + observation.Pulse = pulse; + observation.UpdatedAt = updatedAt; + await db.SaveChangesAsync(cancellationToken); + return observation; + } + public async Task SaveAsync(BloodPressureObservation observation, CancellationToken cancellationToken) { db.Observations.Add(observation); diff --git a/src/README.md b/src/README.md index 1c61640..c4d7b02 100644 --- a/src/README.md +++ b/src/README.md @@ -1,4 +1,4 @@ -# US-01–US-07: Patient profiles and blood-pressure observations +# US-01–US-08: Patient profiles and blood-pressure observations Implemented with .NET 10 LTS (see the [official download page](https://dotnet.microsoft.com/en-us/download/dotnet/10.0)), ASP.NET Core controllers, and PostgreSQL/EF Core. The API gateway, Patient/Profile service and Observation Service are separate executables and can run on separate machines. Each domain service owns its database. @@ -56,6 +56,10 @@ US-06 acceptance tests cover out-of-order measurements, patient-specific selecti US-07 tests cover all four acceptance scenarios, patient-specific filtering, persistence after restart, inclusive and equal bounds, offset normalization, and malformed/missing bounds without changing saved observations. No new dependencies or schema changes are required. +`PUT /api/observations/{observationId}` corrects the measured values using a JSON body containing required positive integer `systolic`, `diastolic`, and `pulse` fields. It returns `200` with the updated observation, preserving its identifier, patient, measurement time, context, note, and creation timestamp while updating the modification timestamp. Missing observations return `404` without insertion; invalid requests return `400` without changes. Corrections are visible through specific retrieval, latest retrieval, and history. The Observation Service owns the update; no patient-profile lookup or database schema change is required. + +US-08 tests cover the documented correction and rejection scenarios, correction of all three values, persistence after restart, preservation of other fields and creation time, modification timestamps, visibility in latest/history, unaffected other observations, and missing/malformed values. No new dependencies are introduced. + Authentication and access control are defined in US-13, US-14 and `Docs/Design/authentication-and-access-control-design.md`, and remain to be implemented as separate stories, as explicitly instructed. These endpoints currently have no access restrictions. ## Automated acceptance tests diff --git a/tests/Observation.AcceptanceTests/UpdateObservationTests.cs b/tests/Observation.AcceptanceTests/UpdateObservationTests.cs new file mode 100644 index 0000000..598a582 --- /dev/null +++ b/tests/Observation.AcceptanceTests/UpdateObservationTests.cs @@ -0,0 +1,121 @@ +using System.Net; +using System.Net.Http.Json; +using System.Text; +using System.Text.Json; +using Microsoft.EntityFrameworkCore; +using Xunit; + +namespace Observation.AcceptanceTests; + +public sealed class UpdateObservationTests(ObservationApiFixture fixture) : IClassFixture +{ + private sealed record Profile(Guid PatientId); + private sealed record Observation(Guid ObservationId, Guid PatientId, DateTime MeasuredAt, + int Systolic, int Diastolic, int Pulse, string? Context, string? Note); + + private async Task RecordAsync() + { + using var patient = await fixture.Client.PostAsJsonAsync("/api/patients", new + { + name = "Correction Example", dateOfBirth = "1980-01-01" + }); + Assert.Equal(HttpStatusCode.Created, patient.StatusCode); + var patientId = (await patient.Content.ReadFromJsonAsync())!.PatientId; + using var response = await fixture.Client.PostAsJsonAsync($"/api/patients/{patientId}/observations", new + { + measuredAt = "2026-09-12T08:00:00", systolic = 182, diastolic = 82, pulse = 68, + context = "Resting", note = "Before breakfast" + }); + Assert.Equal(HttpStatusCode.Created, response.StatusCode); + return (await response.Content.ReadFromJsonAsync())!; + } + + [Fact] + public async Task Correction_preserves_other_information_and_is_visible_in_all_retrievals_after_restart() + { + var original = await RecordAsync(); + var other = await RecordAsync(); + await using var db = fixture.CreateObservationDbContext(); + var before = await db.Observations.AsNoTracking().SingleAsync(o => o.ObservationId == original.ObservationId); + using var response = await fixture.Client.PutAsJsonAsync($"/api/observations/{original.ObservationId}", + new { systolic = 128, diastolic = 82, pulse = 68 }); + + Assert.Equal(HttpStatusCode.OK, response.StatusCode); + var expected = original with { Systolic = 128 }; + Assert.Equal(expected, await response.Content.ReadFromJsonAsync()); + await fixture.RestartHostsAsync(); + Assert.Equal(expected, await fixture.Client.GetFromJsonAsync($"/api/observations/{original.ObservationId}")); + Assert.Equal(expected, await fixture.Client.GetFromJsonAsync( + $"/api/patients/{original.PatientId}/observations/latest")); + var history = await fixture.Client.GetFromJsonAsync( + $"/api/patients/{original.PatientId}/observations?from=2026-09-12T00:00:00&to=2026-09-12T23:59:59"); + Assert.Equal(expected, Assert.Single(history!)); + Assert.Equal(other, await fixture.Client.GetFromJsonAsync($"/api/observations/{other.ObservationId}")); + var after = await db.Observations.AsNoTracking().SingleAsync(o => o.ObservationId == original.ObservationId); + Assert.Equal(before.CreatedAt, after.CreatedAt); + Assert.True(after.UpdatedAt > before.UpdatedAt); + } + + [Fact] + public async Task All_three_measured_values_can_be_corrected() + { + var original = await RecordAsync(); + using var response = await fixture.Client.PutAsJsonAsync($"/api/observations/{original.ObservationId}", + new { systolic = 120, diastolic = 80, pulse = 60 }); + Assert.Equal(HttpStatusCode.OK, response.StatusCode); + Assert.Equal(original with { Systolic = 120, Diastolic = 80, Pulse = 60 }, + await fixture.Client.GetFromJsonAsync($"/api/observations/{original.ObservationId}")); + } + + [Theory] + [InlineData(0, 82, 68)] + [InlineData(128, 0, 68)] + [InlineData(128, 82, 0)] + [InlineData(-1, 82, 68)] + [InlineData(128, -1, 68)] + [InlineData(128, 82, -1)] + public async Task Non_positive_values_are_rejected_without_changing_observation(int systolic, int diastolic, int pulse) + { + var original = await RecordAsync(); + await using var db = fixture.CreateObservationDbContext(); + var before = await db.Observations.AsNoTracking().SingleAsync(o => o.ObservationId == original.ObservationId); + using var response = await fixture.Client.PutAsJsonAsync($"/api/observations/{original.ObservationId}", + new { systolic, diastolic, pulse }); + Assert.Equal(HttpStatusCode.BadRequest, response.StatusCode); + Assert.Equal(original, await fixture.Client.GetFromJsonAsync($"/api/observations/{original.ObservationId}")); + var after = await db.Observations.AsNoTracking().SingleAsync(o => o.ObservationId == original.ObservationId); + Assert.Equal(before.UpdatedAt, after.UpdatedAt); + } + + [Theory] + [InlineData("{\"diastolic\":82,\"pulse\":68}")] + [InlineData("{\"systolic\":128,\"pulse\":68}")] + [InlineData("{\"systolic\":128,\"diastolic\":82}")] + [InlineData("{\"systolic\":null,\"diastolic\":82,\"pulse\":68}")] + [InlineData("{\"systolic\":\"invalid\",\"diastolic\":82,\"pulse\":68}")] + [InlineData("{")] + public async Task Missing_or_malformed_values_are_rejected_without_changes(string json) + { + var original = await RecordAsync(); + using var content = new StringContent(json, Encoding.UTF8, "application/json"); + using var response = await fixture.Client.PutAsync($"/api/observations/{original.ObservationId}", content); + Assert.Equal(HttpStatusCode.BadRequest, response.StatusCode); + using var problem = JsonDocument.Parse(await response.Content.ReadAsStringAsync()); + Assert.True(problem.RootElement.TryGetProperty("errors", out _)); + Assert.Equal(original, await fixture.Client.GetFromJsonAsync($"/api/observations/{original.ObservationId}")); + } + + [Fact] + public async Task Missing_observation_is_rejected_without_inserting_a_new_observation() + { + await using var db = fixture.CreateObservationDbContext(); + var count = await db.Observations.CountAsync(); + var observationId = Guid.NewGuid(); + using var response = await fixture.Client.PutAsJsonAsync($"/api/observations/{observationId}", + new { systolic = 128, diastolic = 82, pulse = 68 }); + Assert.Equal(HttpStatusCode.NotFound, response.StatusCode); + Assert.Equal(count, await db.Observations.CountAsync()); + using var retrieval = await fixture.Client.GetAsync($"/api/observations/{observationId}"); + Assert.Equal(HttpStatusCode.NotFound, retrieval.StatusCode); + } +}