Implement US-08 blood-pressure observation corrections
This commit is contained in:
1 parent
0ea7866f82
commit
e4379ad2f4
8 files changed
+182
-2
No files matched your search
@@ -10,6 +10,10 @@ public sealed class ObservationController(IHttpClientFactory clients) : Controll
|
|||||||
public Task RecordObservation(Guid patientId, CancellationToken cancellationToken) =>
|
public Task RecordObservation(Guid patientId, CancellationToken cancellationToken) =>
|
||||||
ForwardAsync(HttpMethod.Post, $"api/patients/{patientId}/observations", 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}")]
|
[HttpGet("observations/{observationId:guid}")]
|
||||||
public Task GetObservation(Guid observationId, CancellationToken cancellationToken) =>
|
public Task GetObservation(Guid observationId, CancellationToken cancellationToken) =>
|
||||||
ForwardAsync(HttpMethod.Get, $"api/observations/{observationId}", 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)
|
private async Task ForwardAsync(HttpMethod method, string path, CancellationToken cancellationToken)
|
||||||
{
|
{
|
||||||
using var request = new HttpRequestMessage(method, path);
|
using var request = new HttpRequestMessage(method, path);
|
||||||
if (method == HttpMethod.Post)
|
if (method == HttpMethod.Post || method == HttpMethod.Put)
|
||||||
{
|
{
|
||||||
request.Content = new StreamContent(Request.Body);
|
request.Content = new StreamContent(Request.Body);
|
||||||
if (Request.ContentType is not null)
|
if (Request.ContentType is not null)
|
||||||
|
|||||||
@@ -6,6 +6,16 @@ namespace ObservationService.Application;
|
|||||||
|
|
||||||
public sealed class ObservationApplicationService(ObservationRepository repository, IHttpClientFactory clients)
|
public sealed class ObservationApplicationService(ObservationRepository repository, IHttpClientFactory clients)
|
||||||
{
|
{
|
||||||
|
public Task<BloodPressureObservation?> 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<BloodPressureObservation?> RecordAsync(Guid patientId, RecordObservationRequest request,
|
public async Task<BloodPressureObservation?> RecordAsync(Guid patientId, RecordObservationRequest request,
|
||||||
CancellationToken cancellationToken)
|
CancellationToken cancellationToken)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -4,6 +4,8 @@ namespace ObservationService.Application;
|
|||||||
|
|
||||||
public interface ObservationRepository
|
public interface ObservationRepository
|
||||||
{
|
{
|
||||||
|
Task<BloodPressureObservation?> UpdateValuesAsync(Guid observationId, int systolic, int diastolic, int pulse,
|
||||||
|
DateTime updatedAt, CancellationToken cancellationToken);
|
||||||
Task<BloodPressureObservation> SaveAsync(BloodPressureObservation observation, CancellationToken cancellationToken);
|
Task<BloodPressureObservation> SaveAsync(BloodPressureObservation observation, CancellationToken cancellationToken);
|
||||||
Task<BloodPressureObservation?> FindByIdAsync(Guid observationId, CancellationToken cancellationToken);
|
Task<BloodPressureObservation?> FindByIdAsync(Guid observationId, CancellationToken cancellationToken);
|
||||||
Task<BloodPressureObservation?> FindLatestByPatientIdAsync(Guid patientId, CancellationToken cancellationToken);
|
Task<BloodPressureObservation?> FindLatestByPatientIdAsync(Guid patientId, CancellationToken cancellationToken);
|
||||||
|
|||||||
@@ -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; }
|
||||||
|
}
|
||||||
@@ -31,6 +31,14 @@ public sealed class ObservationController(ObservationApplicationService service)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[HttpPut("observations/{observationId:guid}")]
|
||||||
|
public async Task<ActionResult<ObservationDto>> 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}")]
|
[HttpGet("observations/{observationId:guid}")]
|
||||||
public async Task<ActionResult<ObservationDto>> GetObservation(Guid observationId, CancellationToken cancellationToken)
|
public async Task<ActionResult<ObservationDto>> GetObservation(Guid observationId, CancellationToken cancellationToken)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -6,6 +6,22 @@ namespace ObservationService.Persistence;
|
|||||||
|
|
||||||
public sealed class SqlObservationRepository(ObservationDbContext db) : ObservationRepository
|
public sealed class SqlObservationRepository(ObservationDbContext db) : ObservationRepository
|
||||||
{
|
{
|
||||||
|
public async Task<BloodPressureObservation?> 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<BloodPressureObservation> SaveAsync(BloodPressureObservation observation, CancellationToken cancellationToken)
|
public async Task<BloodPressureObservation> SaveAsync(BloodPressureObservation observation, CancellationToken cancellationToken)
|
||||||
{
|
{
|
||||||
db.Observations.Add(observation);
|
db.Observations.Add(observation);
|
||||||
|
|||||||
+5
-1
@@ -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.
|
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.
|
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.
|
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
|
## Automated acceptance tests
|
||||||
|
|||||||
@@ -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<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<Observation> 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<Profile>())!.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<Observation>())!;
|
||||||
|
}
|
||||||
|
|
||||||
|
[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<Observation>());
|
||||||
|
await fixture.RestartHostsAsync();
|
||||||
|
Assert.Equal(expected, await fixture.Client.GetFromJsonAsync<Observation>($"/api/observations/{original.ObservationId}"));
|
||||||
|
Assert.Equal(expected, await fixture.Client.GetFromJsonAsync<Observation>(
|
||||||
|
$"/api/patients/{original.PatientId}/observations/latest"));
|
||||||
|
var history = await fixture.Client.GetFromJsonAsync<Observation[]>(
|
||||||
|
$"/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<Observation>($"/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<Observation>($"/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<Observation>($"/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<Observation>($"/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);
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in new issue
Block a user