diff --git a/src/main/java/uk/gov/hmcts/reform/iacaseapi/domain/handlers/presubmit/AipToLegalRepJourneyHandler.java b/src/main/java/uk/gov/hmcts/reform/iacaseapi/domain/handlers/presubmit/AipToLegalRepJourneyHandler.java index 3469790a52..bf22eacc95 100644 --- a/src/main/java/uk/gov/hmcts/reform/iacaseapi/domain/handlers/presubmit/AipToLegalRepJourneyHandler.java +++ b/src/main/java/uk/gov/hmcts/reform/iacaseapi/domain/handlers/presubmit/AipToLegalRepJourneyHandler.java @@ -1,9 +1,13 @@ package uk.gov.hmcts.reform.iacaseapi.domain.handlers.presubmit; import static java.util.Objects.requireNonNull; +import static org.apache.commons.lang3.StringUtils.isNotEmpty; import static uk.gov.hmcts.reform.iacaseapi.domain.entities.AsylumCaseFieldDefinition.JOURNEY_TYPE; import static uk.gov.hmcts.reform.iacaseapi.domain.entities.AsylumCaseFieldDefinition.PREV_JOURNEY_TYPE; import static uk.gov.hmcts.reform.iacaseapi.domain.entities.AsylumCaseFieldDefinition.PRE_CLARIFYING_STATE; +import static uk.gov.hmcts.reform.iacaseapi.domain.entities.AsylumCaseFieldDefinition.REMISSION_DECISION; +import static uk.gov.hmcts.reform.iacaseapi.domain.entities.AsylumCaseFieldDefinition.REMISSION_TYPE; +import static uk.gov.hmcts.reform.iacaseapi.domain.entities.ccd.field.YesOrNo.YES; import java.util.List; import java.util.Optional; @@ -12,6 +16,8 @@ import uk.gov.hmcts.reform.iacaseapi.domain.entities.AsylumCase; import uk.gov.hmcts.reform.iacaseapi.domain.entities.AsylumCaseFieldDefinition; import uk.gov.hmcts.reform.iacaseapi.domain.entities.ContactPreference; +import uk.gov.hmcts.reform.iacaseapi.domain.entities.RemissionDecision; +import uk.gov.hmcts.reform.iacaseapi.domain.entities.RemissionType; import uk.gov.hmcts.reform.iacaseapi.domain.entities.Subscriber; import uk.gov.hmcts.reform.iacaseapi.domain.entities.ccd.Event; import uk.gov.hmcts.reform.iacaseapi.domain.entities.ccd.State; @@ -20,7 +26,7 @@ import uk.gov.hmcts.reform.iacaseapi.domain.entities.ccd.callback.PreSubmitCallbackStage; import uk.gov.hmcts.reform.iacaseapi.domain.entities.ccd.field.IdValue; import uk.gov.hmcts.reform.iacaseapi.domain.entities.ccd.field.JourneyType; -import uk.gov.hmcts.reform.iacaseapi.domain.entities.ccd.field.YesOrNo; +import uk.gov.hmcts.reform.iacaseapi.domain.entities.ccd.field.PaymentStatus; import uk.gov.hmcts.reform.iacaseapi.domain.handlers.HandlerUtils; import uk.gov.hmcts.reform.iacaseapi.domain.handlers.PreSubmitCallbackStateHandler; @@ -63,9 +69,40 @@ public PreSubmitCallbackResponse handle(PreSubmitCallbackStage callb currentState = State.CASE_UNDER_REVIEW; } + updatePaymentServiceRequestDetails(asylumCase); + return new PreSubmitCallbackResponse<>(asylumCase, currentState); } + private void updatePaymentServiceRequestDetails(AsylumCase asylumCase) { + Optional paymentStatusOptional = asylumCase.read( + AsylumCaseFieldDefinition.PAYMENT_STATUS, PaymentStatus.class); + + if (paymentStatusOptional.isPresent() + && !PaymentStatus.PAID.equals(paymentStatusOptional.get()) + && hasNoRemission(asylumCase)) { + + asylumCase.write(AsylumCaseFieldDefinition.IS_SERVICE_REQUEST_TAB_VISIBLE_CONSIDERING_REMISSIONS, YES); + + Optional paymentReferenceOpt = asylumCase.read(AsylumCaseFieldDefinition.PAYMENT_REFERENCE); + + if (paymentReferenceOpt.isPresent() && isNotEmpty(paymentReferenceOpt.get())) { + asylumCase.write(AsylumCaseFieldDefinition.HAS_SERVICE_REQUEST_ALREADY, YES); + } + } + } + + private boolean hasNoRemission(AsylumCase asylumCase) { + Optional optRemissionType = asylumCase.read(REMISSION_TYPE, RemissionType.class); + Optional optionalRemissionDecision = + asylumCase.read(REMISSION_DECISION, RemissionDecision.class); + + return optRemissionType.isEmpty() + || optRemissionType.get() == RemissionType.NO_REMISSION + || (optionalRemissionDecision.isPresent() + && optionalRemissionDecision.get() != RemissionDecision.APPROVED); + } + private void updateAppellantContactDetails(AsylumCase asylumCase) { Optional>> subscriptionsOptional = asylumCase.read(AsylumCaseFieldDefinition.SUBSCRIPTIONS); @@ -74,7 +111,7 @@ private void updateAppellantContactDetails(AsylumCase asylumCase) { Subscriber subscriber = subscriptionsOptional.get().stream().findFirst().map(IdValue::getValue).orElse(null); if (subscriber != null) { - ContactPreference contactPreference = subscriber.getWantsEmail() == YesOrNo.YES + ContactPreference contactPreference = subscriber.getWantsEmail() == YES ? ContactPreference.WANTS_EMAIL : ContactPreference.WANTS_SMS; diff --git a/src/main/java/uk/gov/hmcts/reform/iacaseapi/domain/handlers/presubmit/AppealSubmitHandler.java b/src/main/java/uk/gov/hmcts/reform/iacaseapi/domain/handlers/presubmit/AppealSubmitHandler.java index c216b2e38d..0bcf168ca0 100644 --- a/src/main/java/uk/gov/hmcts/reform/iacaseapi/domain/handlers/presubmit/AppealSubmitHandler.java +++ b/src/main/java/uk/gov/hmcts/reform/iacaseapi/domain/handlers/presubmit/AppealSubmitHandler.java @@ -74,7 +74,7 @@ public PreSubmitCallbackResponse handle( Optional optRemissionType = asylumCase.read(REMISSION_TYPE, RemissionType.class); Optional optRemissionDecision = asylumCase.read(REMISSION_DECISION, RemissionDecision.class); - if (optRemissionType.isPresent() && optRemissionType.get().equals(RemissionType.NO_REMISSION)) { + if (optRemissionType.isEmpty() || optRemissionType.get().equals(RemissionType.NO_REMISSION)) { asylumCase.write(IS_SERVICE_REQUEST_TAB_VISIBLE_CONSIDERING_REMISSIONS, YesOrNo.YES); } else { asylumCase.write(IS_SERVICE_REQUEST_TAB_VISIBLE_CONSIDERING_REMISSIONS, YesOrNo.NO); diff --git a/src/test/java/uk/gov/hmcts/reform/iacaseapi/domain/handlers/presubmit/AipToLegalRepJourneyHandlerTest.java b/src/test/java/uk/gov/hmcts/reform/iacaseapi/domain/handlers/presubmit/AipToLegalRepJourneyHandlerTest.java index a165cb3858..323dbefd43 100644 --- a/src/test/java/uk/gov/hmcts/reform/iacaseapi/domain/handlers/presubmit/AipToLegalRepJourneyHandlerTest.java +++ b/src/test/java/uk/gov/hmcts/reform/iacaseapi/domain/handlers/presubmit/AipToLegalRepJourneyHandlerTest.java @@ -5,6 +5,7 @@ import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.mockito.Mockito.never; import static org.mockito.Mockito.reset; import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; @@ -21,15 +22,24 @@ import java.util.Arrays; import java.util.List; import java.util.Optional; +import java.util.stream.Stream; + +import org.apache.commons.lang3.StringUtils; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; import org.mockito.Mock; import org.mockito.junit.jupiter.MockitoExtension; import org.mockito.junit.jupiter.MockitoSettings; import org.mockito.quality.Strictness; import uk.gov.hmcts.reform.iacaseapi.domain.entities.AsylumCase; +import uk.gov.hmcts.reform.iacaseapi.domain.entities.AsylumCaseFieldDefinition; import uk.gov.hmcts.reform.iacaseapi.domain.entities.ContactPreference; +import uk.gov.hmcts.reform.iacaseapi.domain.entities.RemissionDecision; +import uk.gov.hmcts.reform.iacaseapi.domain.entities.RemissionType; import uk.gov.hmcts.reform.iacaseapi.domain.entities.Subscriber; import uk.gov.hmcts.reform.iacaseapi.domain.entities.SubscriberType; import uk.gov.hmcts.reform.iacaseapi.domain.entities.ccd.CaseDetails; @@ -40,6 +50,7 @@ import uk.gov.hmcts.reform.iacaseapi.domain.entities.ccd.callback.PreSubmitCallbackStage; import uk.gov.hmcts.reform.iacaseapi.domain.entities.ccd.field.IdValue; import uk.gov.hmcts.reform.iacaseapi.domain.entities.ccd.field.JourneyType; +import uk.gov.hmcts.reform.iacaseapi.domain.entities.ccd.field.PaymentStatus; import uk.gov.hmcts.reform.iacaseapi.domain.entities.ccd.field.YesOrNo; @MockitoSettings(strictness = Strictness.LENIENT) @@ -192,6 +203,65 @@ void should_update_appellant_contact_details_with_email_preference() { verify(asylumCase, times(1)).clear(SUBSCRIPTIONS); } + @ParameterizedTest + @MethodSource("providePaymentUpdateScenarios") + void should_update_payment_details_when_payment_is_pending_and_remission_rejected( + PaymentStatus paymentStatus, + String paymentReference, + RemissionType remissionType, + RemissionDecision remissionDecision, + boolean updateServiceRequestData) { + + when(asylumCase.read(AsylumCaseFieldDefinition.PAYMENT_STATUS, PaymentStatus.class)) + .thenReturn(Optional.of(paymentStatus)); + when(asylumCase.read(AsylumCaseFieldDefinition.PAYMENT_REFERENCE)) + .thenReturn(Optional.ofNullable(paymentReference)); + when(asylumCase.read(AsylumCaseFieldDefinition.REMISSION_TYPE, RemissionType.class)) + .thenReturn(Optional.ofNullable(remissionType)); + when(asylumCase.read(AsylumCaseFieldDefinition.REMISSION_DECISION, RemissionDecision.class)) + .thenReturn(Optional.ofNullable(remissionDecision)); + + PreSubmitCallbackResponse response = aipToLegalRepJourneyHandler.handle( + PreSubmitCallbackStage.ABOUT_TO_SUBMIT, callback, callbackResponse); + + assertNotNull(response); + assertEquals(asylumCase, response.getData()); + + if (updateServiceRequestData) { + verify(asylumCase, times(1)).write( + AsylumCaseFieldDefinition.IS_SERVICE_REQUEST_TAB_VISIBLE_CONSIDERING_REMISSIONS, YesOrNo.YES); + + if (StringUtils.isNotEmpty(paymentReference)) { + verify(asylumCase, times(1)) + .write(AsylumCaseFieldDefinition.HAS_SERVICE_REQUEST_ALREADY, YesOrNo.YES); + } + } else { + verify(asylumCase, never()) + .write(AsylumCaseFieldDefinition.HAS_SERVICE_REQUEST_ALREADY, YesOrNo.YES); + verify(asylumCase, never()).write( + AsylumCaseFieldDefinition.IS_SERVICE_REQUEST_TAB_VISIBLE_CONSIDERING_REMISSIONS, YesOrNo.YES); + } + } + + static Stream providePaymentUpdateScenarios() { + final String reference = "1111-1111-1111-1111"; + return Stream.of( + Arguments.of(PaymentStatus.PAYMENT_PENDING, reference, RemissionType.NO_REMISSION, null, true), + Arguments.of(PaymentStatus.PAYMENT_PENDING, reference, null, null, true), + Arguments.of(PaymentStatus.PAYMENT_PENDING, reference, RemissionType.HO_WAIVER_REMISSION, RemissionDecision.PARTIALLY_APPROVED, true), + Arguments.of(PaymentStatus.PAYMENT_PENDING, reference, null, null, true), + Arguments.of(PaymentStatus.PAYMENT_PENDING, reference, RemissionType.HO_WAIVER_REMISSION, RemissionDecision.REJECTED, true), + Arguments.of(PaymentStatus.PAYMENT_PENDING, null, RemissionType.NO_REMISSION, null, true), + Arguments.of(PaymentStatus.PAYMENT_PENDING, null, null, null, true), + Arguments.of(PaymentStatus.PAYMENT_PENDING, "", RemissionType.HO_WAIVER_REMISSION, RemissionDecision.PARTIALLY_APPROVED, true), + Arguments.of(PaymentStatus.PAYMENT_PENDING, "", null, null, true), + Arguments.of(PaymentStatus.PAYMENT_PENDING, reference, RemissionType.HO_WAIVER_REMISSION, RemissionDecision.REJECTED, true), + Arguments.of(PaymentStatus.PAID, reference, RemissionType.EXCEPTIONAL_CIRCUMSTANCES_REMISSION, RemissionDecision.APPROVED, false), + Arguments.of(PaymentStatus.PAYMENT_PENDING, reference, RemissionType.HELP_WITH_FEES, null, false), + Arguments.of(PaymentStatus.PAID, reference, RemissionType.NO_REMISSION, null, false) + ); + } + @Test void should_update_appellant_contact_details_with_sms_preference() { Subscriber subscriber = new Subscriber( diff --git a/src/test/java/uk/gov/hmcts/reform/iacaseapi/domain/handlers/presubmit/AppealSubmitHandlerTest.java b/src/test/java/uk/gov/hmcts/reform/iacaseapi/domain/handlers/presubmit/AppealSubmitHandlerTest.java index 7bd6b3b1fb..1cad2defc7 100644 --- a/src/test/java/uk/gov/hmcts/reform/iacaseapi/domain/handlers/presubmit/AppealSubmitHandlerTest.java +++ b/src/test/java/uk/gov/hmcts/reform/iacaseapi/domain/handlers/presubmit/AppealSubmitHandlerTest.java @@ -92,6 +92,28 @@ void should_make_service_request_tab_visible_if_no_remission() { assertEquals(YesOrNo.YES, yesOrNoCaptor.getValue()); } + @Test + void should_make_service_request_tab_visible_when_remission_type_does_not_exist() { + + when(callback.getEvent()).thenReturn(SUBMIT_APPEAL); + when(callback.getCaseDetails()).thenReturn(caseDetails); + when(caseDetails.getCaseData()).thenReturn(asylumCase); + when(asylumCase.read(PA_APPEAL_TYPE_PAYMENT_OPTION, String.class)).thenReturn(Optional.empty()); + when(asylumCase.read(JOURNEY_TYPE, JourneyType.class)).thenReturn(Optional.empty()); + when(asylumCase.read(APPEAL_OUT_OF_COUNTRY, YesOrNo.class)).thenReturn(Optional.of(YesOrNo.NO)); + when(asylumCase.read(REMISSION_TYPE, RemissionType.class)).thenReturn(Optional.empty()); + + appealSubmitHandler.handle(PreSubmitCallbackStage.ABOUT_TO_SUBMIT, callback); + + ArgumentCaptor tabVisibleFieldCaptor = ArgumentCaptor.forClass(AsylumCaseFieldDefinition.class); + ArgumentCaptor yesOrNoCaptor = ArgumentCaptor.forClass(YesOrNo.class); + verify(asylumCase, times(1)) + .write(tabVisibleFieldCaptor.capture(), yesOrNoCaptor.capture()); + + assertEquals(IS_SERVICE_REQUEST_TAB_VISIBLE_CONSIDERING_REMISSIONS, tabVisibleFieldCaptor.getValue()); + assertEquals(YesOrNo.YES, yesOrNoCaptor.getValue()); + } + @ParameterizedTest @EnumSource(value = RemissionDecision.class, names = {"PARTIALLY_APPROVED", "APPROVED"}) void should_make_service_request_tab_hidden_if_remission_not_rejected(RemissionDecision remissionDecision) {