diff --git a/src/ApiGateway/Controllers/ObservationController.cs b/src/ApiGateway/Controllers/ObservationController.cs index a6b50b2..4872c49 100644 --- a/src/ApiGateway/Controllers/ObservationController.cs +++ b/src/ApiGateway/Controllers/ObservationController.cs @@ -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); diff --git a/src/ObservationService/Application/ObservationApplicationService.cs b/src/ObservationService/Application/ObservationApplicationService.cs index 526a10c..5645e69 100644 --- a/src/ObservationService/Application/ObservationApplicationService.cs +++ b/src/ObservationService/Application/ObservationApplicationService.cs @@ -37,4 +37,7 @@ public sealed class ObservationApplicationService(ObservationRepository reposito public Task GetByIdAsync(Guid observationId, CancellationToken cancellationToken) => repository.FindByIdAsync(observationId, cancellationToken); + + public Task GetLatestAsync(Guid patientId, CancellationToken cancellationToken) => + repository.FindLatestByPatientIdAsync(patientId, cancellationToken); } diff --git a/src/ObservationService/Application/ObservationRepository.cs b/src/ObservationService/Application/ObservationRepository.cs index 073003c..e8ab61e 100644 --- a/src/ObservationService/Application/ObservationRepository.cs +++ b/src/ObservationService/Application/ObservationRepository.cs @@ -6,4 +6,5 @@ public interface ObservationRepository { Task SaveAsync(BloodPressureObservation observation, CancellationToken cancellationToken); Task FindByIdAsync(Guid observationId, CancellationToken cancellationToken); + Task FindLatestByPatientIdAsync(Guid patientId, CancellationToken cancellationToken); } diff --git a/src/ObservationService/Controllers/ObservationController.cs b/src/ObservationService/Controllers/ObservationController.cs index 8496626..50e9640 100644 --- a/src/ObservationService/Controllers/ObservationController.cs +++ b/src/ObservationService/Controllers/ObservationController.cs @@ -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> 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); diff --git a/src/ObservationService/Persistence/SqlObservationRepository.cs b/src/ObservationService/Persistence/SqlObservationRepository.cs index 20b6960..60677d5 100644 --- a/src/ObservationService/Persistence/SqlObservationRepository.cs +++ b/src/ObservationService/Persistence/SqlObservationRepository.cs @@ -15,4 +15,9 @@ public sealed class SqlObservationRepository(ObservationDbContext db) : Observat public Task FindByIdAsync(Guid observationId, CancellationToken cancellationToken) => db.Observations.AsNoTracking().SingleOrDefaultAsync(o => o.ObservationId == observationId, cancellationToken); + + public Task FindLatestByPatientIdAsync(Guid patientId, CancellationToken cancellationToken) => + db.Observations.AsNoTracking().Where(o => o.PatientId == patientId) + .OrderByDescending(o => o.MeasuredAt).ThenByDescending(o => o.ObservationId) + .FirstOrDefaultAsync(cancellationToken); } diff --git a/src/README.md b/src/README.md index 6a771f6..e0050e0 100644 --- a/src/README.md +++ b/src/README.md @@ -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 diff --git a/suggestions/us06-authentication-prerequisite.md b/suggestions/us06-authentication-prerequisite.md new file mode 100644 index 0000000..03b4dba --- /dev/null +++ b/suggestions/us06-authentication-prerequisite.md @@ -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. diff --git a/tests/Observation.AcceptanceTests/LatestObservationTests.cs b/tests/Observation.AcceptanceTests/LatestObservationTests.cs new file mode 100644 index 0000000..1123b97 --- /dev/null +++ b/tests/Observation.AcceptanceTests/LatestObservationTests.cs @@ -0,0 +1,62 @@ +using System.Net; +using System.Net.Http.Json; +using Xunit; + +namespace Observation.AcceptanceTests; + +public sealed class LatestObservationTests(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 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())!.PatientId; + } + + private async Task 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())!; + } + + [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( + $"/api/patients/{patientId}/observations/latest")); + Assert.Equal(otherLatest, await fixture.Client.GetFromJsonAsync( + $"/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()); + } +}