diff --git a/src/main/java/uk/gov/hmcts/reform/iacaseapi/domain/handlers/presubmit/UpdateTribunalDecisionHandler.java b/src/main/java/uk/gov/hmcts/reform/iacaseapi/domain/handlers/presubmit/UpdateTribunalDecisionHandler.java index 5035c340c5..33bfbe5a21 100644 --- a/src/main/java/uk/gov/hmcts/reform/iacaseapi/domain/handlers/presubmit/UpdateTribunalDecisionHandler.java +++ b/src/main/java/uk/gov/hmcts/reform/iacaseapi/domain/handlers/presubmit/UpdateTribunalDecisionHandler.java @@ -7,12 +7,10 @@ import static uk.gov.hmcts.reform.iacaseapi.domain.entities.AsylumCaseFieldDefinition.ALL_SET_ASIDE_DOCS; import static uk.gov.hmcts.reform.iacaseapi.domain.entities.UpdateTribunalRules.UNDER_RULE_31; import static uk.gov.hmcts.reform.iacaseapi.domain.entities.UpdateTribunalRules.UNDER_RULE_32; -import static uk.gov.hmcts.reform.iacaseapi.domain.entities.ccd.field.YesOrNo.NO; import static uk.gov.hmcts.reform.iacaseapi.domain.entities.ccd.field.YesOrNo.YES; import java.util.ArrayList; import java.util.Collections; -import java.util.ArrayList; import java.util.Optional; import java.util.List; import org.springframework.stereotype.Component; @@ -80,12 +78,13 @@ public PreSubmitCallbackResponse handle(PreSubmitCallbackStage callb .updatedDecisionDate(dateProvider.now().toString()) .build(); + Optional maybeDecisionAndReasonSingleDocument = asylumCase + .read(DECISION_AND_REASON_DOCS_UPLOAD, Document.class); + if (asylumCase.read(UPDATE_TRIBUNAL_DECISION_AND_REASONS_FINAL_CHECK, YesOrNo.class) .map(flag -> flag.equals(YesOrNo.YES)).orElse(false)) { - Document correctedDecisionAndReasonsDoc = asylumCase - .read(DECISION_AND_REASON_DOCS_UPLOAD, Document.class) - .orElseThrow(() -> new IllegalStateException("decisionAndReasonDocsUpload is not present")); + Document correctedDecisionAndReasonsDoc = maybeDecisionAndReasonSingleDocument.orElseThrow(() -> new IllegalStateException("decisionAndReasonDocsUpload is not present")); String summariseChanges = asylumCase .read(SUMMARISE_TRIBUNAL_DECISION_AND_REASONS_DOCUMENT, String.class) @@ -94,34 +93,13 @@ public PreSubmitCallbackResponse handle(PreSubmitCallbackStage callb newDecisionAndReasons.setDocumentAndReasonsDocument(correctedDecisionAndReasonsDoc); newDecisionAndReasons.setDateDocumentAndReasonsDocumentUploaded(dateProvider.now().toString()); newDecisionAndReasons.setSummariseChanges(summariseChanges); - } - - Optional>> maybeExistingDecisionAndReasons = - asylumCase.read(CORRECTED_DECISION_AND_REASONS); - List> allCorrectedDecisions = - decisionAndReasonsAppender.append(newDecisionAndReasons, maybeExistingDecisionAndReasons.orElse(emptyList())); - - asylumCase.write(CORRECTED_DECISION_AND_REASONS, allCorrectedDecisions); - - final Optional decisionAndReasonsDoc = asylumCase.read(DECISION_AND_REASON_DOCS_UPLOAD, Document.class); - - YesOrNo isDecisionAndReasonDocumentBeingUpdated = asylumCase.read(AsylumCaseFieldDefinition.UPDATE_TRIBUNAL_DECISION_AND_REASONS_FINAL_CHECK, YesOrNo.class) - .orElse(NO); - - if (isDecisionAndReasonDocumentBeingUpdated.equals(NO)) { - if (decisionAndReasonsDoc.isPresent()) { - asylumCase.clear(DECISION_AND_REASON_DOCS_UPLOAD); - asylumCase.clear(SUMMARISE_TRIBUNAL_DECISION_AND_REASONS_DOCUMENT); - } - } - final Optional>> maybeDecisionAndReasonsDocuments = asylumCase.read(FINAL_DECISION_AND_REASONS_DOCUMENTS); + final Optional>> maybeDecisionAndReasonsDocuments = asylumCase.read(FINAL_DECISION_AND_REASONS_DOCUMENTS); - final List> existingDecisionAndReasonsDocuments = maybeDecisionAndReasonsDocuments.orElse(Collections.emptyList()); + final List> existingDecisionAndReasonsDocuments = maybeDecisionAndReasonsDocuments.orElse(Collections.emptyList()); - if (decisionAndReasonsDoc.isPresent()) { DocumentWithMetadata updatedDecisionAndReasonsDocument = documentReceiver.receive( - decisionAndReasonsDoc.get(), + correctedDecisionAndReasonsDoc, "", DocumentTag.UPDATED_FINAL_DECISION_AND_REASONS_PDF ); @@ -132,8 +110,22 @@ public PreSubmitCallbackResponse handle(PreSubmitCallbackStage callb ); asylumCase.write(FINAL_DECISION_AND_REASONS_DOCUMENTS, newUpdateTribunalDecisionDocs); + + } else { + + if (maybeDecisionAndReasonSingleDocument.isPresent()) { + asylumCase.clear(DECISION_AND_REASON_DOCS_UPLOAD); + asylumCase.clear(SUMMARISE_TRIBUNAL_DECISION_AND_REASONS_DOCUMENT); + + } } + Optional>> maybeExistingDecisionAndReasons = + asylumCase.read(CORRECTED_DECISION_AND_REASONS); + List> allCorrectedDecisions = + decisionAndReasonsAppender.append(newDecisionAndReasons, maybeExistingDecisionAndReasons.orElse(emptyList())); + + asylumCase.write(CORRECTED_DECISION_AND_REASONS, allCorrectedDecisions); asylumCase.write(UPDATE_TRIBUNAL_DECISION_DATE, dateProvider.now().toString()); } else if (isDecisionRule32(asylumCase)) { @@ -142,7 +134,7 @@ public PreSubmitCallbackResponse handle(PreSubmitCallbackStage callb final Document rule32Document = asylumCase - .read(RULE_32_NOTICE_DOCUMENT,Document.class) + .read(RULE_32_NOTICE_DOCUMENT, Document.class) .orElseThrow( () -> new IllegalStateException("Rule 32 notice document is not present")); @@ -194,4 +186,5 @@ private void setFtpaReheardCaseFlag(AsylumCase asylumCase) { asylumCase.write(STITCHING_STATUS,""); } } -} \ No newline at end of file +} + diff --git a/src/test/java/uk/gov/hmcts/reform/iacaseapi/domain/handlers/presubmit/UpdateTribunalDecisionHandlerTest.java b/src/test/java/uk/gov/hmcts/reform/iacaseapi/domain/handlers/presubmit/UpdateTribunalDecisionHandlerTest.java index dcebe49d72..a2f9e88dc8 100644 --- a/src/test/java/uk/gov/hmcts/reform/iacaseapi/domain/handlers/presubmit/UpdateTribunalDecisionHandlerTest.java +++ b/src/test/java/uk/gov/hmcts/reform/iacaseapi/domain/handlers/presubmit/UpdateTribunalDecisionHandlerTest.java @@ -22,7 +22,6 @@ import java.time.LocalDate; import java.util.Arrays; import java.util.ArrayList; -import java.util.Arrays; import java.util.List; import java.util.Optional; import org.junit.jupiter.api.BeforeEach; @@ -125,16 +124,6 @@ void should_write_updated_appeal_decision_dismissed() { when(asylumCase.read(TYPES_OF_UPDATE_TRIBUNAL_DECISION, DynamicList.class)) .thenReturn(Optional.of(dynamicList)); - when(asylumCase.read(DECISION_AND_REASON_DOCS_UPLOAD, Document.class)).thenReturn(Optional.of(decisionAndReasonsDocument)); - - LocalDate currentDate = LocalDate.now(); - when(dateProvider.now()).thenReturn(currentDate); - - final YesOrNo isDecisionAndReasonDocumentBeingUpdated = asylumCase.read(AsylumCaseFieldDefinition.UPDATE_TRIBUNAL_DECISION_AND_REASONS_FINAL_CHECK, YesOrNo.class) - .orElse(NO); - - decisionsAndReasonDoc = "someTestDoc"; - PreSubmitCallbackResponse callbackResponse = updateTribunalDecisionHandler.handle(PreSubmitCallbackStage.ABOUT_TO_SUBMIT, callback); @@ -145,13 +134,6 @@ void should_write_updated_appeal_decision_dismissed() { assertNotNull(callbackResponse); assertEquals(asylumCase, callbackResponse.getData()); - if (isDecisionAndReasonDocumentBeingUpdated.equals(NO) && decisionsAndReasonDoc != null) { - asylumCase.clear(DECISION_AND_REASON_DOCS_UPLOAD); - asylumCase.clear(SUMMARISE_TRIBUNAL_DECISION_AND_REASONS_DOCUMENT); - - verify(asylumCase, times(1)).write(UPDATED_APPEAL_DECISION, "Dismissed"); - verify(asylumCase, times(1)).write(UPDATE_TRIBUNAL_DECISION_DATE, currentDate.toString()); - } verify(asylumCase, times(1)).write(UPDATED_APPEAL_DECISION, "Dismissed"); verify(asylumCase, times(1)).write(CORRECTED_DECISION_AND_REASONS, allAppendedDecisionAndReasosn); assertThat(capturedDecision.getUpdatedDecisionDate()).isEqualTo(now.toString()); @@ -165,35 +147,21 @@ void should_write_updated_appeal_decision_allowed() { ); when(asylumCase.read(TYPES_OF_UPDATE_TRIBUNAL_DECISION, DynamicList.class)) .thenReturn(Optional.of(dynamicList)); - when(asylumCase.read(DECISION_AND_REASON_DOCS_UPLOAD, Document.class)).thenReturn(Optional.of(decisionAndReasonsDocument)); - - LocalDate currentDate = LocalDate.now(); - when(dateProvider.now()).thenReturn(currentDate); - - final YesOrNo isDecisionAndReasonDocumentBeingUpdated = asylumCase.read(AsylumCaseFieldDefinition.UPDATE_TRIBUNAL_DECISION_AND_REASONS_FINAL_CHECK, YesOrNo.class) - .orElse(NO); - - decisionsAndReasonDoc = "someTestDoc"; PreSubmitCallbackResponse callbackResponse = updateTribunalDecisionHandler.handle(PreSubmitCallbackStage.ABOUT_TO_SUBMIT, callback); - if (isDecisionAndReasonDocumentBeingUpdated.equals(NO) && decisionsAndReasonDoc != null) { - asylumCase.clear(DECISION_AND_REASON_DOCS_UPLOAD); - asylumCase.clear(SUMMARISE_TRIBUNAL_DECISION_AND_REASONS_DOCUMENT); - - verify(decisionAndReasonsAppender, times(1)) - .append(newDecisionCaptor.capture(), existingDecisionsCaptor.capture()); + verify(decisionAndReasonsAppender, times(1)) + .append(newDecisionCaptor.capture(), existingDecisionsCaptor.capture()); - final DecisionAndReasons capturedDecision = newDecisionCaptor.getValue(); - assertNotNull(callbackResponse); - assertEquals(asylumCase, callbackResponse.getData()); + final DecisionAndReasons capturedDecision = newDecisionCaptor.getValue(); + assertNotNull(callbackResponse); + assertEquals(asylumCase, callbackResponse.getData()); - verify(asylumCase, times(1)).write(UPDATED_APPEAL_DECISION, "Allowed"); - verify(asylumCase, times(1)).write(CORRECTED_DECISION_AND_REASONS, allAppendedDecisionAndReasosn); - verify(asylumCase, times(1)).write(UPDATE_TRIBUNAL_DECISION_DATE, currentDate.toString()); - assertThat(capturedDecision.getUpdatedDecisionDate()).isEqualTo(now.toString()); - } + verify(asylumCase, times(1)).write(UPDATED_APPEAL_DECISION, "Allowed"); + verify(asylumCase, times(1)).write(CORRECTED_DECISION_AND_REASONS, allAppendedDecisionAndReasosn); + verify(asylumCase, times(1)).write(UPDATE_TRIBUNAL_DECISION_DATE, now.toString()); + assertThat(capturedDecision.getUpdatedDecisionDate()).isEqualTo(now.toString()); } @Test @@ -229,7 +197,6 @@ void should_write_updated_decision_document_if_has_corrected_document() { assertThat(capturedDecision.getSummariseChanges()).isEqualTo(summarisedChanges); } - @Test void should_write_set_aside_documents_if_is_r32() { @@ -412,7 +379,7 @@ void should_not_allow_null_arguments() { } @Test - void should_append_update_tribunal_decision_document() { + void should_append_update_tribunal_decision_document_if_yes() { List decisionsAndReasonsDocumentsWithMetadata = Arrays.asList(decisionsAndReasonsDocumentWithMetadata); @@ -422,13 +389,12 @@ void should_append_update_tribunal_decision_document() { ); final List> finalDecisionAndReasonsDocuments = new ArrayList<>(); - when(asylumCase.read(TYPES_OF_UPDATE_TRIBUNAL_DECISION, DynamicList.class)) .thenReturn(Optional.of(dynamicList)); - - when(callback.getCaseDetails()).thenReturn(caseDetails); - when(caseDetails.getCaseData()).thenReturn(asylumCase); - when(callback.getEvent()).thenReturn(Event.UPDATE_TRIBUNAL_DECISION); + when(asylumCase.read(UPDATE_TRIBUNAL_DECISION_AND_REASONS_FINAL_CHECK, YesOrNo.class)) + .thenReturn(Optional.of(YesOrNo.YES)); + when(asylumCase.read(SUMMARISE_TRIBUNAL_DECISION_AND_REASONS_DOCUMENT, String.class)) + .thenReturn(Optional.of(summarisedChanges)); when(asylumCase.read(DECISION_AND_REASON_DOCS_UPLOAD, Document.class)) .thenReturn(Optional.of(correctedDecisionDocument)); when(asylumCase.read(FINAL_DECISION_AND_REASONS_DOCUMENTS)) @@ -449,8 +415,30 @@ void should_append_update_tribunal_decision_document() { verify(documentReceiver).receive(correctedDecisionDocument, "", DocumentTag.UPDATED_FINAL_DECISION_AND_REASONS_PDF); verify(documentsAppender).append(finalDecisionAndReasonsDocuments, Arrays.asList(decisionsAndReasonsDocumentWithMetadata)); - - verify(asylumCase, times(1)).read(DECISION_AND_REASON_DOCS_UPLOAD, Document.class); verify(asylumCase).write(FINAL_DECISION_AND_REASONS_DOCUMENTS, newUpdateTribunalDecisionDocs); } + + @Test + void should_not_append_update_tribunal_decision_document_if_no() { + + final DynamicList dynamicList = new DynamicList( + new Value("allowed", "Yes, change decision to Allowed"), + newArrayList() + ); + + when(asylumCase.read(TYPES_OF_UPDATE_TRIBUNAL_DECISION, DynamicList.class)) + .thenReturn(Optional.of(dynamicList)); + when(asylumCase.read(UPDATE_TRIBUNAL_DECISION_AND_REASONS_FINAL_CHECK, YesOrNo.class)) + .thenReturn(Optional.of(NO)); + when(asylumCase.read(DECISION_AND_REASON_DOCS_UPLOAD, Document.class)) + .thenReturn(Optional.of(correctedDecisionDocument)); + + PreSubmitCallbackResponse callbackResponse = + updateTribunalDecisionHandler.handle(PreSubmitCallbackStage.ABOUT_TO_SUBMIT, callback); + + assertNotNull(callbackResponse); + + verify(asylumCase).clear(DECISION_AND_REASON_DOCS_UPLOAD); + verify(asylumCase).clear(SUMMARISE_TRIBUNAL_DECISION_AND_REASONS_DOCUMENT); + } }