public async Task GivenExistingPatientAndHasChange_WhenRequestIsPrepared_ThenCorrectEntryComponentShouldBeCreated() { FhirTransactionContext context = CreateFhirTransactionContext(); var patient = new Patient() { Id = "patient1", Meta = new Meta() { VersionId = "v1", }, }; _fhirService.RetrievePatientAsync(Arg.Is(TestUtility.BuildIdentifierPredicate(string.Empty, DefaultPatientId)), DefaultCancellationToken) .Returns(patient); Patient updatingPatient = null; _patientSynchronizer.When(async synchronizer => await synchronizer.SynchronizeAsync(context, Arg.Any <Patient>(), isNewPatient: false, DefaultCancellationToken)).Do(callback => { updatingPatient = callback.ArgAt <Patient>(1); // Modify a property of patient so changes can be detected. updatingPatient.Gender = AdministrativeGender.Other; }); await _patientPipeline.PrepareRequestAsync(context, DefaultCancellationToken); FhirTransactionRequestEntry actualPatientEntry = context.Request.Patient; ValidationUtility.ValidateRequestEntryMinimumRequirementForWithChange(FhirTransactionRequestMode.Update, "Patient/patient1", Bundle.HTTPVerb.PUT, actualPatientEntry); Assert.Equal("W/\"v1\"", actualPatientEntry.Request.IfMatch); Assert.Same(updatingPatient, actualPatientEntry.Resource); }
/// <inheritdoc/> public async Task PrepareRequestAsync(FhirTransactionContext context, CancellationToken cancellationToken) { EnsureArg.IsNotNull(context, nameof(context)); DicomDataset dataset = context.ChangeFeedEntry.Metadata; if (dataset == null) { return; } if (!dataset.TryGetSingleValue(DicomTag.PatientID, out string patientId)) { throw new MissingRequiredDicomTagException(nameof(DicomTag.PatientID)); } var patientIdentifier = new Identifier(string.Empty, patientId); FhirTransactionRequestMode requestMode = FhirTransactionRequestMode.None; Patient existingPatient = await _fhirService.RetrievePatientAsync(patientIdentifier, cancellationToken); Patient patient = (Patient)existingPatient?.DeepCopy(); if (existingPatient == null) { patient = new Patient(); patient.Identifier.Add(patientIdentifier); requestMode = FhirTransactionRequestMode.Create; } _patientSynchronizer.Synchronize(dataset, patient); if (requestMode == FhirTransactionRequestMode.None && !existingPatient.IsExactly(patient)) { requestMode = FhirTransactionRequestMode.Update; } Bundle.RequestComponent request = requestMode switch { FhirTransactionRequestMode.Create => GenerateCreateRequest(patientIdentifier), FhirTransactionRequestMode.Update => GenerateUpdateRequest(patient), _ => null }; IResourceId resourceId = requestMode switch { FhirTransactionRequestMode.Create => new ClientResourceId(), _ => existingPatient.ToServerResourceId(), }; context.Request.Patient = new FhirTransactionRequestEntry( requestMode, request, resourceId, patient); }