Implement US-08 partial observation and context corrections
This commit is contained in:
1 parent
ce03038ce1
commit
5aefe52853
10 files changed
+447
-11
No files matched your search
@@ -18,6 +18,10 @@ public sealed class ObservationController(IHttpClientFactory clients) : Controll
|
||||
public Task UpdateObservation(Guid observationId, CancellationToken cancellationToken) =>
|
||||
ForwardAsync(HttpMethod.Put, $"api/observations/{observationId}", cancellationToken);
|
||||
|
||||
[HttpPatch("observations/{observationId:guid}")]
|
||||
public Task PatchObservation(Guid observationId, CancellationToken cancellationToken) =>
|
||||
ForwardAsync(HttpMethod.Patch, $"api/observations/{observationId}", cancellationToken);
|
||||
|
||||
[HttpGet("observations/{observationId:guid}")]
|
||||
public Task GetObservation(Guid observationId, CancellationToken cancellationToken) =>
|
||||
ForwardAsync(HttpMethod.Get, $"api/observations/{observationId}", cancellationToken);
|
||||
@@ -33,7 +37,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 || method == HttpMethod.Put)
|
||||
if (method == HttpMethod.Post || method == HttpMethod.Put || method == HttpMethod.Patch)
|
||||
{
|
||||
request.Content = new StreamContent(Request.Body);
|
||||
if (Request.ContentType is not null)
|
||||
|
||||
@@ -16,8 +16,16 @@ public sealed class ObservationApplicationService(ObservationRepository reposito
|
||||
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);
|
||||
return PatchAsync(observationId, new PatchObservationRequest { Systolic = request.Systolic, Diastolic = request.Diastolic, Pulse = request.Pulse }, cancellationToken);
|
||||
}
|
||||
|
||||
public Task<BloodPressureObservation?> PatchAsync(Guid observationId, PatchObservationRequest request,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
Validator.ValidateObject(request, new ValidationContext(request), validateAllProperties: true);
|
||||
if (request.MeasurementContext is not null)
|
||||
Validator.ValidateObject(request.MeasurementContext, new ValidationContext(request.MeasurementContext), validateAllProperties: true);
|
||||
return repository.UpdateAsync(observationId, request, DateTime.SpecifyKind(DateTime.UtcNow, DateTimeKind.Unspecified), cancellationToken);
|
||||
}
|
||||
|
||||
public async Task<BloodPressureObservation?> RecordAsync(Guid patientId, RecordObservationRequest request,
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
using ObservationService.Contracts;
|
||||
using ObservationService.Domain;
|
||||
|
||||
namespace ObservationService.Application;
|
||||
@@ -5,7 +6,7 @@ namespace ObservationService.Application;
|
||||
public interface ObservationRepository
|
||||
{
|
||||
Task<bool> DeleteAsync(Guid observationId, CancellationToken cancellationToken);
|
||||
Task<BloodPressureObservation?> UpdateValuesAsync(Guid observationId, int systolic, int diastolic, int pulse,
|
||||
Task<BloodPressureObservation?> UpdateAsync(Guid observationId, PatchObservationRequest request,
|
||||
DateTime updatedAt, CancellationToken cancellationToken);
|
||||
Task<BloodPressureObservation> SaveAsync(BloodPressureObservation observation, CancellationToken cancellationToken);
|
||||
Task<BloodPressureObservation?> FindByIdAsync(Guid observationId, CancellationToken cancellationToken);
|
||||
|
||||
@@ -0,0 +1,87 @@
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace ObservationService.Contracts;
|
||||
|
||||
[JsonUnmappedMemberHandling(JsonUnmappedMemberHandling.Disallow)]
|
||||
public sealed record MeasurementContextPatch
|
||||
{
|
||||
private string? setting;
|
||||
[AllowedValues("Home", "Clinic", "Other", "Unknown", null)]
|
||||
public string? Setting
|
||||
{
|
||||
get => setting;
|
||||
init { setting = value; SettingSpecified = true; }
|
||||
}
|
||||
[JsonIgnore]
|
||||
public bool SettingSpecified { get; private set; }
|
||||
|
||||
private string? activityCondition;
|
||||
[AllowedValues("AtRest", "DuringActivity", "AfterActivity", "Unknown", null)]
|
||||
public string? ActivityCondition
|
||||
{
|
||||
get => activityCondition;
|
||||
init { activityCondition = value; ActivityConditionSpecified = true; }
|
||||
}
|
||||
[JsonIgnore]
|
||||
public bool ActivityConditionSpecified { get; private set; }
|
||||
|
||||
private decimal? restDurationMinutes;
|
||||
[Range(0, double.MaxValue)]
|
||||
public decimal? RestDurationMinutes
|
||||
{
|
||||
get => restDurationMinutes;
|
||||
init { restDurationMinutes = value; RestDurationMinutesSpecified = true; }
|
||||
}
|
||||
[JsonIgnore]
|
||||
public bool RestDurationMinutesSpecified { get; private set; }
|
||||
|
||||
private string? bodyPosition;
|
||||
public string? BodyPosition
|
||||
{
|
||||
get => bodyPosition;
|
||||
init { bodyPosition = value; BodyPositionSpecified = true; }
|
||||
}
|
||||
[JsonIgnore]
|
||||
public bool BodyPositionSpecified { get; private set; }
|
||||
|
||||
private string? measuredArm;
|
||||
public string? MeasuredArm
|
||||
{
|
||||
get => measuredArm;
|
||||
init { measuredArm = value; MeasuredArmSpecified = true; }
|
||||
}
|
||||
[JsonIgnore]
|
||||
public bool MeasuredArmSpecified { get; private set; }
|
||||
|
||||
private string? deviceDetails;
|
||||
public string? DeviceDetails
|
||||
{
|
||||
get => deviceDetails;
|
||||
init { deviceDetails = value; DeviceDetailsSpecified = true; }
|
||||
}
|
||||
[JsonIgnore]
|
||||
public bool DeviceDetailsSpecified { get; private set; }
|
||||
|
||||
private string? cuffDetails;
|
||||
public string? CuffDetails
|
||||
{
|
||||
get => cuffDetails;
|
||||
init { cuffDetails = value; CuffDetailsSpecified = true; }
|
||||
}
|
||||
[JsonIgnore]
|
||||
public bool CuffDetailsSpecified { get; private set; }
|
||||
|
||||
private string? additionalCircumstances;
|
||||
public string? AdditionalCircumstances
|
||||
{
|
||||
get => additionalCircumstances;
|
||||
init { additionalCircumstances = value; AdditionalCircumstancesSpecified = true; }
|
||||
}
|
||||
[JsonIgnore]
|
||||
public bool AdditionalCircumstancesSpecified { get; private set; }
|
||||
|
||||
[JsonIgnore]
|
||||
public bool HasChanges => SettingSpecified || ActivityConditionSpecified || RestDurationMinutesSpecified ||
|
||||
BodyPositionSpecified || MeasuredArmSpecified || DeviceDetailsSpecified || CuffDetailsSpecified || AdditionalCircumstancesSpecified;
|
||||
}
|
||||
@@ -0,0 +1,78 @@
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace ObservationService.Contracts;
|
||||
|
||||
[JsonUnmappedMemberHandling(JsonUnmappedMemberHandling.Disallow)]
|
||||
public sealed record PatchObservationRequest : IValidatableObject
|
||||
{
|
||||
private int? systolic;
|
||||
[Range(1, int.MaxValue)]
|
||||
public int? Systolic
|
||||
{
|
||||
get => systolic;
|
||||
init { systolic = value; SystolicSpecified = true; }
|
||||
}
|
||||
[JsonIgnore]
|
||||
public bool SystolicSpecified { get; private set; }
|
||||
|
||||
private int? diastolic;
|
||||
[Range(1, int.MaxValue)]
|
||||
public int? Diastolic
|
||||
{
|
||||
get => diastolic;
|
||||
init { diastolic = value; DiastolicSpecified = true; }
|
||||
}
|
||||
[JsonIgnore]
|
||||
public bool DiastolicSpecified { get; private set; }
|
||||
|
||||
private int? pulse;
|
||||
[Range(1, int.MaxValue)]
|
||||
public int? Pulse
|
||||
{
|
||||
get => pulse;
|
||||
init { pulse = value; PulseSpecified = true; }
|
||||
}
|
||||
[JsonIgnore]
|
||||
public bool PulseSpecified { get; private set; }
|
||||
|
||||
private MeasurementContextPatch? measurementContext;
|
||||
public MeasurementContextPatch? MeasurementContext
|
||||
{
|
||||
get => measurementContext;
|
||||
init { measurementContext = value; MeasurementContextSpecified = true; }
|
||||
}
|
||||
[JsonIgnore]
|
||||
public bool MeasurementContextSpecified { get; private set; }
|
||||
|
||||
private string? context;
|
||||
public string? Context
|
||||
{
|
||||
get => context;
|
||||
init { context = value; ContextSpecified = true; }
|
||||
}
|
||||
[JsonIgnore]
|
||||
public bool ContextSpecified { get; private set; }
|
||||
|
||||
private string? note;
|
||||
public string? Note
|
||||
{
|
||||
get => note;
|
||||
init { note = value; NoteSpecified = true; }
|
||||
}
|
||||
[JsonIgnore]
|
||||
public bool NoteSpecified { get; private set; }
|
||||
|
||||
public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
|
||||
{
|
||||
if (SystolicSpecified && Systolic is null)
|
||||
yield return new ValidationResult("Systolic cannot be cleared.", [nameof(Systolic)]);
|
||||
if (DiastolicSpecified && Diastolic is null)
|
||||
yield return new ValidationResult("Diastolic cannot be cleared.", [nameof(Diastolic)]);
|
||||
if (PulseSpecified && Pulse is null)
|
||||
yield return new ValidationResult("Pulse cannot be cleared.", [nameof(Pulse)]);
|
||||
if (!SystolicSpecified && !DiastolicSpecified && !PulseSpecified && !ContextSpecified && !NoteSpecified &&
|
||||
!(MeasurementContextSpecified && (MeasurementContext is null || MeasurementContext.HasChanges)))
|
||||
yield return new ValidationResult("Supply at least one supported field to update.");
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,9 @@
|
||||
using System.Text.Json.Serialization;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
|
||||
namespace ObservationService.Contracts;
|
||||
|
||||
[JsonUnmappedMemberHandling(JsonUnmappedMemberHandling.Disallow)]
|
||||
public sealed record UpdateObservationRequest
|
||||
{
|
||||
[Required, Range(1, int.MaxValue)]
|
||||
|
||||
@@ -43,6 +43,14 @@ public sealed class ObservationController(ObservationApplicationService service)
|
||||
return observation is null ? NotFound() : Ok(ToDto(observation));
|
||||
}
|
||||
|
||||
[HttpPatch("observations/{observationId:guid}")]
|
||||
public async Task<ActionResult<ObservationDto>> PatchObservation(Guid observationId, PatchObservationRequest request,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
var observation = await service.PatchAsync(observationId, request, cancellationToken);
|
||||
return observation is null ? NotFound() : Ok(ToDto(observation));
|
||||
}
|
||||
|
||||
[HttpGet("observations/{observationId:guid}")]
|
||||
public async Task<ActionResult<ObservationDto>> GetObservation(Guid observationId, CancellationToken cancellationToken)
|
||||
{
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
using ObservationService.Contracts;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using ObservationService.Application;
|
||||
using ObservationService.Domain;
|
||||
@@ -10,17 +11,39 @@ public sealed class SqlObservationRepository(ObservationDbContext db) : Observat
|
||||
await db.Observations.Where(o => o.ObservationId == observationId)
|
||||
.ExecuteDeleteAsync(cancellationToken) > 0;
|
||||
|
||||
public async Task<BloodPressureObservation?> UpdateValuesAsync(Guid observationId, int systolic, int diastolic,
|
||||
int pulse, DateTime updatedAt, CancellationToken cancellationToken)
|
||||
public async Task<BloodPressureObservation?> UpdateAsync(Guid observationId, PatchObservationRequest request,
|
||||
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;
|
||||
if (request.SystolicSpecified) observation.Systolic = request.Systolic!.Value;
|
||||
if (request.DiastolicSpecified) observation.Diastolic = request.Diastolic!.Value;
|
||||
if (request.PulseSpecified) observation.Pulse = request.Pulse!.Value;
|
||||
if (request.ContextSpecified) observation.Context = request.Context;
|
||||
if (request.NoteSpecified) observation.Note = request.Note;
|
||||
if (request.MeasurementContextSpecified)
|
||||
{
|
||||
if (request.MeasurementContext is null) observation.MeasurementContext = new();
|
||||
else
|
||||
{
|
||||
var patch = request.MeasurementContext;
|
||||
var current = observation.MeasurementContext;
|
||||
observation.MeasurementContext = current with
|
||||
{
|
||||
Setting = patch.SettingSpecified ? patch.Setting ?? "Unknown" : current.Setting,
|
||||
ActivityCondition = patch.ActivityConditionSpecified ? patch.ActivityCondition ?? "Unknown" : current.ActivityCondition,
|
||||
RestDurationMinutes = patch.RestDurationMinutesSpecified ? patch.RestDurationMinutes : current.RestDurationMinutes,
|
||||
BodyPosition = patch.BodyPositionSpecified ? patch.BodyPosition : current.BodyPosition,
|
||||
MeasuredArm = patch.MeasuredArmSpecified ? patch.MeasuredArm : current.MeasuredArm,
|
||||
DeviceDetails = patch.DeviceDetailsSpecified ? patch.DeviceDetails : current.DeviceDetails,
|
||||
CuffDetails = patch.CuffDetailsSpecified ? patch.CuffDetails : current.CuffDetails,
|
||||
AdditionalCircumstances = patch.AdditionalCircumstancesSpecified ? patch.AdditionalCircumstances : current.AdditionalCircumstances
|
||||
};
|
||||
}
|
||||
}
|
||||
observation.UpdatedAt = updatedAt;
|
||||
await db.SaveChangesAsync(cancellationToken);
|
||||
return observation;
|
||||
|
||||
+37
-2
@@ -106,7 +106,7 @@ Apply the `RecordedMeasurementContext` Observation migration before starting the
|
||||
updated service. EF Core maps the owned value to columns in the same observation
|
||||
table; no new table, service or dependency is introduced. Existing rows receive
|
||||
unknown categories and null details without changing their measurements, timestamps
|
||||
or text. Context filtering is implemented under US-07; context-only updates remain under US-08.
|
||||
or text. Context filtering is implemented under US-07; partial context updates are implemented under US-08.
|
||||
|
||||
US-05 context retrieval is covered independently of creation: dedicated tests seed
|
||||
saved observations directly and request them through the authenticated gateway.
|
||||
@@ -149,7 +149,42 @@ Existing Insights requests continue to consume the unchanged unfiltered array. N
|
||||
|
||||
`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.
|
||||
`PATCH /api/observations/{observationId}` accepts a partial JSON object. For example:
|
||||
|
||||
```json
|
||||
{
|
||||
"measurementContext": {
|
||||
"setting": "Clinic",
|
||||
"activityCondition": null,
|
||||
"deviceDetails": "Corrected device"
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
Only supplied fields change. Within `measurementContext`, setting/activity nulls
|
||||
clear the category to `Unknown`; null optional details clear them without changing
|
||||
other details. An explicit `measurementContext: null` clears the whole structured
|
||||
object to unknown categories and null details, while retaining legacy text and
|
||||
notes. Omission of the object preserves it. Optional `context` and `note` can also
|
||||
be changed or cleared with null. Optional `systolic`, `diastolic` and `pulse` support
|
||||
individual positive-integer corrections; they cannot be cleared. Empty patches or
|
||||
objects containing no supported changes return `400`. Creation time, measurement
|
||||
time, patient association and observation identifier cannot be changed by patches.
|
||||
|
||||
Successful patches return `200` with the updated observation and modification time.
|
||||
Validation occurs before any changes are saved; unsupported fields/categories, negative
|
||||
rest duration, non-positive/null numerical values and malformed input reject the
|
||||
whole request with `400` validation details. Missing observations return `404`
|
||||
without insertion. Ownership restrictions apply to the actual observation patient;
|
||||
professionals cannot patch even assigned patients. The existing `PUT` endpoint
|
||||
retains its complete numerical-correction contract and rejects unsupported fields
|
||||
rather than silently ignoring context supplied on that route. Use `PATCH` for context
|
||||
updates. Both routes use the same
|
||||
Observation-owned update operation and save changes atomically. Subsequent reads,
|
||||
latest observations and filtered history use current recorded context. US-11 trend
|
||||
integration remains to be verified when that endpoint is implemented.
|
||||
|
||||
US-08 tests cover the original full numerical corrections and rejection scenarios plus context-only/single-value patches, historical context additions, individual field corrections/clears, whole-context clearing, preservation of omitted data and creation time, persistence after restart, fresh filter membership, atomic rejection, missing observations and access restrictions. No new dependencies are introduced.
|
||||
|
||||
`DELETE /api/observations/{observationId}` removes the observation and returns `204 No Content`. A missing or previously deleted observation returns `404`. The Observation Service deletes the row from its own database in a single statement; deleted observations disappear from specific retrieval, history, and latest selection. Other observations and the patient profile are preserved.
|
||||
|
||||
|
||||
Reference in new issue
Block a user