|
| 1 | +package uk.nhs.adaptors.gp2gp.ehr; |
| 2 | + |
| 3 | +import lombok.AllArgsConstructor; |
| 4 | +import lombok.extern.slf4j.Slf4j; |
| 5 | +import org.hl7.fhir.dstu3.model.CodeableConcept; |
| 6 | +import org.hl7.fhir.dstu3.model.Coding; |
| 7 | +import org.hl7.fhir.dstu3.model.Meta; |
| 8 | +import org.hl7.fhir.dstu3.model.OperationOutcome; |
| 9 | +import org.hl7.fhir.dstu3.model.UriType; |
| 10 | +import org.springframework.beans.factory.annotation.Autowired; |
| 11 | +import org.springframework.http.HttpStatus; |
| 12 | +import org.springframework.http.ResponseEntity; |
| 13 | +import org.springframework.web.bind.annotation.PathVariable; |
| 14 | +import org.springframework.web.bind.annotation.PostMapping; |
| 15 | +import org.springframework.web.bind.annotation.RequestMapping; |
| 16 | +import org.springframework.web.bind.annotation.RestController; |
| 17 | +import uk.nhs.adaptors.gp2gp.common.service.FhirParseService; |
| 18 | +import uk.nhs.adaptors.gp2gp.common.service.RandomIdGeneratorService; |
| 19 | +import uk.nhs.adaptors.gp2gp.common.service.TimestampService; |
| 20 | +import uk.nhs.adaptors.gp2gp.common.task.TaskDispatcher; |
| 21 | +import uk.nhs.adaptors.gp2gp.ehr.model.EhrExtractStatus; |
| 22 | +import uk.nhs.adaptors.gp2gp.gpc.GetGpcStructuredTaskDefinition; |
| 23 | + |
| 24 | +import java.util.Collections; |
| 25 | + |
| 26 | +@Slf4j |
| 27 | +@RestController |
| 28 | +@AllArgsConstructor(onConstructor = @__(@Autowired)) |
| 29 | +@RequestMapping(path = "/ehr-resend") |
| 30 | +public class EhrResendController { |
| 31 | + |
| 32 | + private static final String OPERATION_OUTCOME_URL = "https://fhir.nhs.uk/STU3/StructureDefinition/GPConnect-OperationOutcome-1"; |
| 33 | + private static final String PRECONDITION_FAILED = "PRECONDITION_FAILED"; |
| 34 | + private static final String INVALID_IDENTIFIER_VALUE = "INVALID_IDENTIFIER_VALUE"; |
| 35 | + |
| 36 | + private EhrExtractStatusRepository ehrExtractStatusRepository; |
| 37 | + private TaskDispatcher taskDispatcher; |
| 38 | + private RandomIdGeneratorService randomIdGeneratorService; |
| 39 | + private final TimestampService timestampService; |
| 40 | + private final FhirParseService fhirParseService; |
| 41 | + |
| 42 | + @PostMapping("/{conversationId}") |
| 43 | + public ResponseEntity<String> scheduleEhrExtractResend(@PathVariable String conversationId) { |
| 44 | + EhrExtractStatus ehrExtractStatus = ehrExtractStatusRepository.findByConversationId(conversationId).orElseGet(() -> null); |
| 45 | + |
| 46 | + if (ehrExtractStatus == null) { |
| 47 | + var details = getCodeableConcept(INVALID_IDENTIFIER_VALUE); |
| 48 | + var diagnostics = "Provide a conversationId that exists and retry the operation"; |
| 49 | + |
| 50 | + var operationOutcome = createOperationOutcome(OperationOutcome.IssueType.VALUE, |
| 51 | + OperationOutcome.IssueSeverity.ERROR, |
| 52 | + details, |
| 53 | + diagnostics); |
| 54 | + var errorBody = fhirParseService.encodeToJson(operationOutcome); |
| 55 | + |
| 56 | + return new ResponseEntity<>(errorBody, HttpStatus.NOT_FOUND); |
| 57 | + } |
| 58 | + |
| 59 | + if (hasNoErrorsInEhrReceivedAcknowledgement(ehrExtractStatus) && ehrExtractStatus.getError() == null) { |
| 60 | + |
| 61 | + var details = getCodeableConcept(PRECONDITION_FAILED); |
| 62 | + var diagnostics = "The current resend operation is still in progress. Please wait for it to complete before retrying"; |
| 63 | + var operationOutcome = createOperationOutcome(OperationOutcome.IssueType.BUSINESSRULE, |
| 64 | + OperationOutcome.IssueSeverity.ERROR, |
| 65 | + details, |
| 66 | + diagnostics); |
| 67 | + var errorBody = fhirParseService.encodeToJson(operationOutcome); |
| 68 | + return new ResponseEntity<>(errorBody, HttpStatus.CONFLICT); |
| 69 | + } |
| 70 | + |
| 71 | + var updatedEhrExtractStatus = prepareEhrExtractStatusForNewResend(ehrExtractStatus); |
| 72 | + ehrExtractStatusRepository.save(updatedEhrExtractStatus); |
| 73 | + createGetGpcStructuredTask(updatedEhrExtractStatus); |
| 74 | + LOGGER.info("Scheduled GetGpcStructuredTask for resend of ConversationId: {}", conversationId); |
| 75 | + |
| 76 | + return new ResponseEntity<>(HttpStatus.ACCEPTED); |
| 77 | + } |
| 78 | + |
| 79 | + private static CodeableConcept getCodeableConcept(String codeableConceptCode) { |
| 80 | + return new CodeableConcept().addCoding( |
| 81 | + new Coding("https://fhir.nhs.uk/STU3/ValueSet/Spine-ErrorOrWarningCode-1", codeableConceptCode, null)); |
| 82 | + } |
| 83 | + |
| 84 | + private static boolean hasNoErrorsInEhrReceivedAcknowledgement(EhrExtractStatus ehrExtractStatus) { |
| 85 | + var ehrReceivedAcknowledgement = ehrExtractStatus.getEhrReceivedAcknowledgement(); |
| 86 | + if (ehrReceivedAcknowledgement == null) { |
| 87 | + return true; |
| 88 | + } |
| 89 | + |
| 90 | + var errors = ehrReceivedAcknowledgement.getErrors(); |
| 91 | + if (errors == null || errors.isEmpty()) { |
| 92 | + return true; |
| 93 | + } |
| 94 | + return false; |
| 95 | + } |
| 96 | + |
| 97 | + private EhrExtractStatus prepareEhrExtractStatusForNewResend(EhrExtractStatus ehrExtractStatus) { |
| 98 | + |
| 99 | + var now = timestampService.now(); |
| 100 | + ehrExtractStatus.setUpdatedAt(now); |
| 101 | + ehrExtractStatus.setMessageTimestamp(now); |
| 102 | + ehrExtractStatus.setEhrExtractCorePending(null); |
| 103 | + ehrExtractStatus.setGpcAccessDocument(null); |
| 104 | + ehrExtractStatus.setEhrContinue(null); |
| 105 | + ehrExtractStatus.setEhrReceivedAcknowledgement(null); |
| 106 | + |
| 107 | + return ehrExtractStatus; |
| 108 | + } |
| 109 | + |
| 110 | + private void createGetGpcStructuredTask(EhrExtractStatus ehrExtractStatus) { |
| 111 | + var getGpcStructuredTaskDefinition = GetGpcStructuredTaskDefinition.getGetGpcStructuredTaskDefinition(randomIdGeneratorService, |
| 112 | + ehrExtractStatus); |
| 113 | + taskDispatcher.createTask(getGpcStructuredTaskDefinition); |
| 114 | + } |
| 115 | + |
| 116 | + public static OperationOutcome createOperationOutcome( |
| 117 | + OperationOutcome.IssueType type, OperationOutcome.IssueSeverity severity, CodeableConcept details, String diagnostics) { |
| 118 | + var operationOutcome = new OperationOutcome(); |
| 119 | + Meta meta = new Meta(); |
| 120 | + meta.setProfile(Collections.singletonList(new UriType(OPERATION_OUTCOME_URL))); |
| 121 | + operationOutcome.setMeta(meta); |
| 122 | + operationOutcome.addIssue() |
| 123 | + .setCode(type) |
| 124 | + .setSeverity(severity) |
| 125 | + .setDetails(details) |
| 126 | + .setDiagnostics(diagnostics); |
| 127 | + return operationOutcome; |
| 128 | + } |
| 129 | + |
| 130 | +} |
0 commit comments