Skip to content

Commit 7b89644

Browse files
Update invalid prescription type exception when mapping MedicationStatement (#1110)
* Update message to be clearer and more informative to the user. * Add test to ensure exception is thrown and the message is correct. * Add input JSON file for the above test
1 parent 78e9423 commit 7b89644

File tree

3 files changed

+72
-1
lines changed

3 files changed

+72
-1
lines changed

service/src/main/java/uk/nhs/adaptors/gp2gp/ehr/mapper/MedicationStatementMapper.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ public class MedicationStatementMapper {
7171
private static final List<String> REPEAT_PRESCRIPTION_TYPE_CODES =
7272
Arrays.asList("delayed-prescribing", "repeat", "repeat-dispensing");
7373
private static final String ACUTE_REPEAT_VALUE = "0";
74+
private static final String INVALID_PRESCRIPTION_TYPE_MESSAGE = "Could not resolve Prescription Type of `%s` in %s";
7475

7576
private final MessageContext messageContext;
7677
private final CodeableConceptCdMapper codeableConceptCdMapper;
@@ -254,7 +255,7 @@ private String buildRepeatNumber(MedicationRequest medicationRequest) {
254255
} else if (prescriptionTypeCode.isBlank() && prescriptionTypeTextIsNoInfoAvailable(medicationRequest)) {
255256
return extractRepeatValue(medicationRequest);
256257
}
257-
throw new EhrMapperException("Could not resolve Prescription Type for Repeat value");
258+
throw new EhrMapperException(INVALID_PRESCRIPTION_TYPE_MESSAGE.formatted(prescriptionTypeCode, medicationRequest.getId()));
258259
}
259260
return StringUtils.EMPTY;
260261
}

service/src/test/java/uk/nhs/adaptors/gp2gp/ehr/mapper/MedicationStatementMapperTest.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,19 @@ public void When_ConfidentialityServiceReturnsEmptyOptional_Expect_MessageDoesNo
373373
.doesNotContain(CONFIDENTIALITY_CODE);
374374
}
375375

376+
@Test
377+
void When_MedicationRequestPlanHasInvalidPrescriptionType_Expect_EhrMapperExceptionThrown() {
378+
final var jsonInput = ResourceTestFileUtils.getFileContent(TEST_FILE_DIRECTORY + "mr-plan-with-invalid-prescription-type.json");
379+
final var parsedMedicationRequest = new FhirParseService()
380+
.parseResource(jsonInput, MedicationRequest.class);
381+
382+
var exception = assertThrows(
383+
EhrMapperException.class,
384+
() -> medicationStatementMapper.mapMedicationRequestToMedicationStatement(parsedMedicationRequest));
385+
386+
assertThat(exception.getMessage()).isEqualTo("Could not resolve Prescription Type of `invalid-type` in MedicationRequest/789");
387+
}
388+
376389
private void assertXmlIsEqual(String outputString, String expected) {
377390

378391
Diff diff = DiffBuilder.compare(outputString).withTest(expected)
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
{
2+
"resourceType": "MedicationRequest",
3+
"id": "789",
4+
"meta": {
5+
"profile": [
6+
"https://fhir.nhs.uk/STU3/StructureDefinition/CareConnect-GPC-MedicationRequest-1"
7+
]
8+
},
9+
"extension": [
10+
{
11+
"url": "https://fhir.nhs.uk/STU3/StructureDefinition/Extension-CareConnect-GPC-PrescriptionType-1",
12+
"valueCodeableConcept": {
13+
"coding": [
14+
{
15+
"system": "https://fhir.nhs.uk/STU3/CodeSystem/CareConnect-PrescriptionType-1",
16+
"code": "invalid-type",
17+
"display": "Invalid Type"
18+
}
19+
]
20+
}
21+
}
22+
],
23+
"status": "completed",
24+
"intent": "plan",
25+
"medicationReference": {
26+
"reference": "Medication/20"
27+
},
28+
"authoredOn": "2022-10-04",
29+
"recorder": {
30+
"reference": "Practitioner/1332aa9c-28d6-11eb-adc1-0242ac120002"
31+
},
32+
"requester": {
33+
"agent": {
34+
"reference": "Organization/F85003"
35+
}
36+
},
37+
"dosageInstruction": [
38+
{
39+
"text": "One puff as needed. Max 10 doses per day"
40+
}
41+
],
42+
"dispenseRequest": {
43+
"validityPeriod": {
44+
"start": "2022-10-04",
45+
"end": "2022-11-01"
46+
},
47+
"quantity": {
48+
"value": 1,
49+
"unit": "dose"
50+
},
51+
"expectedSupplyDuration": {
52+
"value": 28,
53+
"system": "http://unitsofmeasure.org",
54+
"code": "d"
55+
}
56+
}
57+
}

0 commit comments

Comments
 (0)