|
4 | 4 | import static uk.nhs.adaptors.gp2gp.ehr.mapper.MedicationStatementExtractor.extractDispenseRequestQuantityTextFromQuantity; |
5 | 5 | import static uk.nhs.adaptors.gp2gp.ehr.mapper.MedicationStatementExtractor.extractEhrSupplyTypeCodeableConcept; |
6 | 6 | import static uk.nhs.adaptors.gp2gp.ehr.mapper.MedicationStatementExtractor.extractIdFromPlanMedicationRequestReference; |
7 | | -import static uk.nhs.adaptors.gp2gp.ehr.mapper.MedicationStatementExtractor.extractPrescriptionTypeCode; |
8 | 7 | import static uk.nhs.adaptors.gp2gp.ehr.mapper.MedicationStatementExtractor.extractRepeatValue; |
9 | 8 | import static uk.nhs.adaptors.gp2gp.ehr.mapper.MedicationStatementExtractor.extractStatusReasonStoppedAvailabilityTime; |
10 | 9 | import static uk.nhs.adaptors.gp2gp.ehr.mapper.MedicationStatementExtractor.extractStatusReasonStoppedCode; |
11 | 10 | import static uk.nhs.adaptors.gp2gp.ehr.mapper.MedicationStatementExtractor.extractStatusReasonStoppedText; |
12 | 11 | import static uk.nhs.adaptors.gp2gp.ehr.mapper.MedicationStatementExtractor.hasStatusReasonStopped; |
13 | | -import static uk.nhs.adaptors.gp2gp.ehr.mapper.MedicationStatementExtractor.prescriptionTypeTextIsNoInfoAvailable; |
| 12 | +import static uk.nhs.adaptors.gp2gp.ehr.utils.ExtensionMappingUtils.filterExtensionByUrl; |
14 | 13 |
|
15 | 14 | import java.util.Arrays; |
16 | 15 | import java.util.List; |
|
22 | 21 |
|
23 | 22 | import org.apache.commons.lang3.StringUtils; |
24 | 23 | import org.hl7.fhir.dstu3.model.Annotation; |
| 24 | +import org.hl7.fhir.dstu3.model.CodeableConcept; |
| 25 | +import org.hl7.fhir.dstu3.model.Coding; |
| 26 | +import org.hl7.fhir.dstu3.model.Extension; |
25 | 27 | import org.hl7.fhir.dstu3.model.Medication; |
26 | 28 | import org.hl7.fhir.dstu3.model.MedicationRequest; |
27 | 29 | import org.hl7.fhir.dstu3.model.Reference; |
@@ -72,6 +74,10 @@ public class MedicationStatementMapper { |
72 | 74 | Arrays.asList("delayed-prescribing", "repeat", "repeat-dispensing"); |
73 | 75 | private static final String ACUTE_REPEAT_VALUE = "0"; |
74 | 76 | private static final String INVALID_PRESCRIPTION_TYPE_MESSAGE = "Could not resolve Prescription Type of `%s` in %s"; |
| 77 | + private static final String PRESCRIPTION_TYPE_UNRECOGNISED = "Could not resolve Prescription Type with text of `%s` in %s"; |
| 78 | + private static final String NO_INFO_AVAILABLE = "No information available"; |
| 79 | + private static final String PRESCRIPTION_TYPE_URL = |
| 80 | + "https://fhir.nhs.uk/STU3/StructureDefinition/Extension-CareConnect-GPC-PrescriptionType-1"; |
75 | 81 |
|
76 | 82 | private final MessageContext messageContext; |
77 | 83 | private final CodeableConceptCdMapper codeableConceptCdMapper; |
@@ -246,20 +252,45 @@ private String buildStatusReasonStoppedAvailabilityTime(MedicationRequest medica |
246 | 252 |
|
247 | 253 | private String buildRepeatNumber(MedicationRequest medicationRequest) { |
248 | 254 | if (MedicationRequestIntent.PLAN.getDisplay().equals(medicationRequest.getIntent().getDisplay())) { |
249 | | - var prescriptionTypeCode = extractPrescriptionTypeCode(medicationRequest); |
250 | | - |
251 | | - if (ACUTE_PRESCRIPTION_TYPE_CODES.contains(prescriptionTypeCode)) { |
252 | | - return ACUTE_REPEAT_VALUE; |
253 | | - } else if (REPEAT_PRESCRIPTION_TYPE_CODES.contains(prescriptionTypeCode)) { |
254 | | - return extractRepeatValue(medicationRequest); |
255 | | - } else if (prescriptionTypeCode.isBlank() && prescriptionTypeTextIsNoInfoAvailable(medicationRequest)) { |
256 | | - return extractRepeatValue(medicationRequest); |
257 | | - } |
258 | | - throw new EhrMapperException(INVALID_PRESCRIPTION_TYPE_MESSAGE.formatted(prescriptionTypeCode, medicationRequest.getId())); |
| 255 | + Optional<CodeableConcept> prescriptionType = filterExtensionByUrl(medicationRequest, PRESCRIPTION_TYPE_URL) |
| 256 | + .map(Extension::getValue) |
| 257 | + .map(CodeableConcept.class::cast); |
| 258 | + |
| 259 | + var prescriptionTypeCode = prescriptionType |
| 260 | + .map(CodeableConcept::getCodingFirstRep) |
| 261 | + .map(Coding::getCode); |
| 262 | + |
| 263 | + return prescriptionTypeCode.isEmpty() |
| 264 | + ? buildRepeatNumberFromPrescriptionTypeText(medicationRequest, prescriptionType) |
| 265 | + : buildRepeatNumberFromPrescriptionTypeCode(medicationRequest, prescriptionTypeCode.get()); |
259 | 266 | } |
260 | 267 | return StringUtils.EMPTY; |
261 | 268 | } |
262 | 269 |
|
| 270 | + private static String buildRepeatNumberFromPrescriptionTypeCode(MedicationRequest medicationRequest, String prescriptionTypeCode) { |
| 271 | + if (ACUTE_PRESCRIPTION_TYPE_CODES.contains(prescriptionTypeCode)) { |
| 272 | + return ACUTE_REPEAT_VALUE; |
| 273 | + } else if (REPEAT_PRESCRIPTION_TYPE_CODES.contains(prescriptionTypeCode)) { |
| 274 | + return extractRepeatValue(medicationRequest); |
| 275 | + } |
| 276 | + throw new EhrMapperException(INVALID_PRESCRIPTION_TYPE_MESSAGE.formatted(prescriptionTypeCode, medicationRequest.getId())); |
| 277 | + } |
| 278 | + |
| 279 | + private static String buildRepeatNumberFromPrescriptionTypeText( |
| 280 | + MedicationRequest medicationRequest, |
| 281 | + Optional<CodeableConcept> prescriptionType |
| 282 | + ) { |
| 283 | + var prescriptionTypeText = prescriptionType |
| 284 | + .map(CodeableConcept::getText) |
| 285 | + .orElse(StringUtils.EMPTY); |
| 286 | + |
| 287 | + if (NO_INFO_AVAILABLE.equals(prescriptionTypeText)) { |
| 288 | + return extractRepeatValue(medicationRequest); |
| 289 | + } else { |
| 290 | + throw new EhrMapperException(PRESCRIPTION_TYPE_UNRECOGNISED.formatted(prescriptionTypeText, medicationRequest.getId())); |
| 291 | + } |
| 292 | + } |
| 293 | + |
263 | 294 | private String buildBasedOn(MedicationRequest medicationRequest) { |
264 | 295 | if (MedicationRequestIntent.ORDER.getDisplay().equals(medicationRequest.getIntent().getDisplay())) { |
265 | 296 | if (medicationRequest.hasBasedOn()) { |
|
0 commit comments