Implement US-06 latest blood-pressure observation retrieval

This commit is contained in:
Steffen committed 2026-10-02 09:15:09 +02:00
1 parent 05f57dcba3
commit df47bcbbd7
8 files changed
+97 -2

No files matched your search

@@ -14,6 +14,10 @@ public sealed class ObservationController(IHttpClientFactory clients) : Controll
public Task GetObservation(Guid observationId, CancellationToken cancellationToken) =>
ForwardAsync(HttpMethod.Get, $"api/observations/{observationId}", cancellationToken);
[HttpGet("patients/{patientId:guid}/observations/latest")]
public Task GetLatest(Guid patientId, CancellationToken cancellationToken) =>
ForwardAsync(HttpMethod.Get, $"api/patients/{patientId}/observations/latest", cancellationToken);
private async Task ForwardAsync(HttpMethod method, string path, CancellationToken cancellationToken)
{
using var request = new HttpRequestMessage(method, path);
@@ -37,4 +37,7 @@ public sealed class ObservationApplicationService(ObservationRepository reposito
public Task<BloodPressureObservation?> GetByIdAsync(Guid observationId, CancellationToken cancellationToken) =>
repository.FindByIdAsync(observationId, cancellationToken);
public Task<BloodPressureObservation?> GetLatestAsync(Guid patientId, CancellationToken cancellationToken) =>
repository.FindLatestByPatientIdAsync(patientId, cancellationToken);
}
@@ -6,4 +6,5 @@ public interface ObservationRepository
{
Task<BloodPressureObservation> SaveAsync(BloodPressureObservation observation, CancellationToken cancellationToken);
Task<BloodPressureObservation?> FindByIdAsync(Guid observationId, CancellationToken cancellationToken);
Task<BloodPressureObservation?> FindLatestByPatientIdAsync(Guid patientId, CancellationToken cancellationToken);
}
@@ -37,6 +37,13 @@ public sealed class ObservationController(ObservationApplicationService service)
return observation is null ? NotFound() : Ok(ToDto(observation));
}
[HttpGet("patients/{patientId:guid}/observations/latest")]
public async Task<ActionResult<ObservationDto>> GetLatest(Guid patientId, CancellationToken cancellationToken)
{
var observation = await service.GetLatestAsync(patientId, cancellationToken);
return observation is null ? NotFound() : Ok(ToDto(observation));
}
private static ObservationDto ToDto(BloodPressureObservation observation) =>
new(observation.ObservationId, observation.PatientId, observation.MeasuredAt, observation.Systolic,
observation.Diastolic, observation.Pulse, observation.Context, observation.Note);
@@ -15,4 +15,9 @@ public sealed class SqlObservationRepository(ObservationDbContext db) : Observat
public Task<BloodPressureObservation?> FindByIdAsync(Guid observationId, CancellationToken cancellationToken) =>
db.Observations.AsNoTracking().SingleOrDefaultAsync(o => o.ObservationId == observationId, cancellationToken);
public Task<BloodPressureObservation?> FindLatestByPatientIdAsync(Guid patientId, CancellationToken cancellationToken) =>
db.Observations.AsNoTracking().Where(o => o.PatientId == patientId)
.OrderByDescending(o => o.MeasuredAt).ThenByDescending(o => o.ObservationId)
.FirstOrDefaultAsync(cancellationToken);
}
+6 -2
View File
@@ -1,4 +1,4 @@
# US-01–US-04: Patient profiles and blood-pressure recording
# US-01–US-06: 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.
@@ -48,7 +48,11 @@ curl -i http://127.0.0.1:5080/api/patients \
The Observation Service checks patient existence by calling the gateway's patient retrieval API over HTTP before saving. It has no reference to Patient/Profile implementation code and no access to that service's database. Missing patients return `404`; validation failures return `400`. Patient lookup failures return `502` or `504` and do not save an observation. Measurement timestamps without an offset are stored as supplied; timestamps with an offset are normalized to UTC. PostgreSQL uses the diagram's `timestamp` columns without a time-zone component.
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. These endpoints currently have no access restrictions.
`GET /api/patients/{patientId}/observations/latest` returns `200` with that patient's observation having the greatest measurement timestamp, regardless of recording order. It returns `404` when no observation is available. Equal measurement timestamps are resolved consistently by observation identifier; US-06 does not prescribe which tied observation to return. Selection happens in the Observation Service's own database through the documented controller/application-service/repository layers.
US-06 acceptance tests cover out-of-order measurements, patient-specific selection, retrieval after restart, and no available observation even when another patient has recorded measurements. No new dependencies or schema changes are required.
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
@@ -0,0 +1,9 @@
# US-06 implementation order and authentication prerequisite
Resolved: the user explicitly instructed that authentication be deferred until its relevant user story. Implement US-06 using the existing API conventions; defer the authentication decisions below to US-13/US-14.
US-01–US-04 are committed. US-05 retrieval and both acceptance scenarios are already implemented and tested as part of US-04. The next unfinished sequential story is US-06 (latest blood-pressure observation).
US-13 requires authentication for all observation operations. Existing services and acceptance tests currently have no authentication. The newly incorporated authentication design explicitly leaves token selection, account-association persistence, profile-creation recovery, and gateway-to-service protection open and requires accepted decisions before implementation.
Before authentication implementation, also reconcile US-13's statement that every account has exactly one patient with the design's zero-profile initial state and US-14's healthcare-professional assignments. No authoritative documents have been modified.
@@ -0,0 +1,62 @@
using System.Net;
using System.Net.Http.Json;
using Xunit;
namespace Observation.AcceptanceTests;
public sealed class LatestObservationTests(ObservationApiFixture fixture) : IClassFixture<ObservationApiFixture>
{
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<Guid> CreatePatientAsync()
{
using var response = await fixture.Client.PostAsJsonAsync("/api/patients", new
{
name = "Latest Example", dateOfBirth = "1980-01-01"
});
Assert.Equal(HttpStatusCode.Created, response.StatusCode);
return (await response.Content.ReadFromJsonAsync<Profile>())!.PatientId;
}
private async Task<Observation> RecordAsync(Guid patientId, string measuredAt, int systolic, int diastolic, int pulse)
{
using var response = await fixture.Client.PostAsJsonAsync($"/api/patients/{patientId}/observations", new
{
measuredAt, systolic, diastolic, pulse, context = "Resting", note = "Before breakfast"
});
Assert.Equal(HttpStatusCode.Created, response.StatusCode);
return (await response.Content.ReadFromJsonAsync<Observation>())!;
}
[Fact]
public async Task Latest_is_selected_by_measurement_time_for_the_requested_patient_after_restart()
{
var patientId = await CreatePatientAsync();
await RecordAsync(patientId, "2026-09-10T08:00:00", 130, 84, 70);
var latest = await RecordAsync(patientId, "2026-09-12T08:00:00", 126, 80, 66);
await RecordAsync(patientId, "2026-09-11T08:00:00", 128, 82, 68);
var otherPatientId = await CreatePatientAsync();
var otherLatest = await RecordAsync(otherPatientId, "2026-09-13T08:00:00", 140, 90, 75);
await fixture.RestartHostsAsync();
Assert.Equal(latest, await fixture.Client.GetFromJsonAsync<Observation>(
$"/api/patients/{patientId}/observations/latest"));
Assert.Equal(otherLatest, await fixture.Client.GetFromJsonAsync<Observation>(
$"/api/patients/{otherPatientId}/observations/latest"));
}
[Fact]
public async Task Patient_without_observations_returns_not_found_even_when_another_patient_has_observations()
{
var patientId = await CreatePatientAsync();
await RecordAsync(await CreatePatientAsync(), "2026-09-12T08:00:00", 128, 82, 68);
using var response = await fixture.Client.GetAsync($"/api/patients/{patientId}/observations/latest");
Assert.Equal(HttpStatusCode.NotFound, response.StatusCode);
Assert.DoesNotContain("observationId", await response.Content.ReadAsStringAsync());
}
}