Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NIAD-3170: Generating acute MedicationRequest[intent=order] which share the same MedicationRequest[intent=plan] #821

Merged
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add functionality so that when plans are duplicated, the prior medica…
…tion references are updated.

Added test for this functionality
Fix checkstyle issues
MartinWheelerMT committed Sep 27, 2024
commit 8b0761bc006b770a1a69dc61319d1eb5b3957217
Original file line number Diff line number Diff line change
@@ -31,6 +31,7 @@
import org.hl7.v3.RCMRMT030101UKEhrExtract;
import org.hl7.v3.RCMRMT030101UKMedicationStatement;
import org.hl7.v3.TS;
import org.jetbrains.annotations.NotNull;
import org.springframework.stereotype.Service;

import lombok.AllArgsConstructor;
@@ -73,7 +74,7 @@ public List<DomainResource> mapResources(RCMRMT030101UKEhrExtract ehrExtract, Pa
ordersGroupedByAcutePlan
.forEach((plan, orders) -> {
if (orders.size() > 1) {
sortOrdersByStartDate(orders);
sortOrdersByValidityPeriodStart(orders);

// the index starts at one as the earliest order remains referenced to the original plan
for (var index = 1; index < orders.size(); index++) {
@@ -88,6 +89,12 @@ public List<DomainResource> mapResources(RCMRMT030101UKEhrExtract ehrExtract, Pa
var basedOn = orders.get(index).getBasedOn();
updateBasedOnReferenceToReferenceDuplicatedPlan(basedOn, duplicatedPlanId);

// we need to set the prior medication ref to either the initial plan (when index == 1)
// or to id of the previously generated plan (that is the plan referenced index -1)
var previousOrderBasedOn = orders.get(index - 1).getBasedOn();
var previousBasedOnReference = getMedicationRequestBasedOnReference(previousOrderBasedOn);
previousBasedOnReference.ifPresent(duplicatedPlan::setPriorPrescription);

resources.add(duplicatedPlan);
}
}
@@ -100,13 +107,7 @@ public List<DomainResource> mapResources(RCMRMT030101UKEhrExtract ehrExtract, Pa
}

private static void updateBasedOnReferenceToReferenceDuplicatedPlan(List<Reference> basedOn, String duplicatedPlanId) {
var basedOnReference = basedOn
.stream()
.filter(reference ->
ResourceType.MedicationRequest.name()
.equals(reference.getReferenceElement().getResourceType())
)
.findFirst();
var basedOnReference = getMedicationRequestBasedOnReference(basedOn);

if (basedOnReference.isPresent()) {
var replacementIndex = basedOn.indexOf(basedOnReference.get());
@@ -117,7 +118,18 @@ private static void updateBasedOnReferenceToReferenceDuplicatedPlan(List<Referen
}
}

private void sortOrdersByStartDate(List<MedicationRequest> orders) {
private static @NotNull Optional<Reference> getMedicationRequestBasedOnReference(List<Reference> basedOn) {
return basedOn
.stream()
.filter(reference ->
ResourceType.MedicationRequest.name()
.equals(reference.getReferenceElement().getResourceType())
)
.findFirst();
}

// for Orders, GP Connect specification state that validity period will always have a start date.
private void sortOrdersByValidityPeriodStart(List<MedicationRequest> orders) {
orders.sort(Comparator.comparing(medicationRequest ->
medicationRequest.getDispenseRequest().getValidityPeriod().getStart()
));
Original file line number Diff line number Diff line change
@@ -402,7 +402,6 @@ void When_MultipleOrdersAreBasedOnOneAcutePlan_Expect_TheGeneratedPlanHasIdAndId
() -> assertThat(generatedPlan.getIdentifierFirstRep().getValue())
.isEqualTo(GENERATED_PLAN_ID)
);

}

@Test
@@ -436,8 +435,8 @@ void When_MultipleOrdersAreBasedOnOneAcutePlan_Expect_TheLatestOrderReferencesTh
var resources = medicationRequestMapper
.mapResources(ehrExtract, (Patient) new Patient().setId(PATIENT_ID), List.of(), PRACTISE_CODE);

var latestOrder = getMedicationRequestById(resources, LATEST_ORDER_ID);
var generatedPlan = getMedicationRequestById(resources, GENERATED_PLAN_ID);
var latestOrder = getMedicationRequestById(resources, LATEST_ORDER_ID);
var generatedPlan = getMedicationRequestById(resources, GENERATED_PLAN_ID);

assertAll(
() -> assertThat(latestOrder.getBasedOn().getFirst().getReferenceElement().getIdPart())
@@ -447,6 +446,28 @@ void When_MultipleOrdersAreBasedOnOneAcutePlan_Expect_TheLatestOrderReferencesTh
);
}

@Test
void When_MultipleOrdersAreBasedOnOneAcutePlan_Expect_TheLatestOrderPriorPrescriptionReferencesTheOriginalPlan() {
setupMultipleOrdersToOnePlanStubs(ACUTE_PRESCRIPTION_EXTENSION);
var ehrExtract = unmarshallEhrExtract(
"ehrExtract_MultipleSupplyPrescribeInFulfilmentOfSingleAcuteSupplyAuthorise.xml"
);

var resources = medicationRequestMapper
.mapResources(ehrExtract, (Patient) new Patient().setId(PATIENT_ID), List.of(), PRACTISE_CODE);

var generatedPlanReferenceElement = getMedicationRequestById(resources, GENERATED_PLAN_ID)
.getPriorPrescription()
.getReferenceElement();

assertAll(
() -> assertThat(generatedPlanReferenceElement.getResourceType())
.isEqualTo(ResourceType.MedicationRequest.name()),
() -> assertThat(generatedPlanReferenceElement.getIdPart())
.isEqualTo(INITIAL_PLAN_ID)
);
}

@Test
public void When_MedicationRequestMapperThrowsException_Expect_MedicationMapperContextToBeReset() {
var ehrExtract = unmarshallEhrExtract("ehrExtract1.xml");
@@ -512,7 +533,7 @@ private void assertReferenceIdIsPatientId(Reference reference) {
assertThat(medicationRequestId).isEqualTo(PATIENT_ID);
}

private MedicationRequest buildMedicationRequestOrder(String Id, String validityPeriodStartDate) {
private MedicationRequest buildMedicationRequestOrder(String id, String validityPeriodStartDate) {
return (MedicationRequest) new MedicationRequest()
.setIntent(MedicationRequestIntent.ORDER)
.addBasedOn(REFERENCE_TO_PLAN)
@@ -522,7 +543,7 @@ private MedicationRequest buildMedicationRequestOrder(String Id, String validity
new Period().setStartElement(DateFormatUtil.parseToDateTimeType(validityPeriodStartDate))
)
)
.setId(Id);
.setId(id);
}

private MedicationRequest buildMedicationRequestPlan(Extension extension) {
@@ -566,9 +587,9 @@ private void setupMultipleOrdersToOnePlanStubs(Extension planExtension) {
any(RCMRMT030101UKPrescribe.class),
any(String.class)
)
).thenReturn(
buildMedicationRequestOrder(LATEST_ORDER_ID, "20240102"),
buildMedicationRequestOrder(EARLIEST_ORDER_ID ,"20240101")
).thenReturn(buildMedicationRequestOrder(
LATEST_ORDER_ID, "20240102"),
buildMedicationRequestOrder(EARLIEST_ORDER_ID, "20240101")
);

lenient().when(idGeneratorService.generateUuid()).thenReturn(GENERATED_PLAN_ID);