Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DFR-3186 Enable mid-event callbacks to handle specific event pages #1776

Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@
public enum CallbackType {

ABOUT_TO_START, MID_EVENT, ABOUT_TO_SUBMIT, SUBMITTED
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import uk.gov.hmcts.reform.ccd.client.model.AboutToStartOrSubmitCallbackResponse;
import uk.gov.hmcts.reform.ccd.client.model.CallbackRequest;
Expand Down Expand Up @@ -92,15 +93,16 @@ public ResponseEntity<GenericAboutToStartOrSubmitCallbackResponse> ccdAboutToSub
@ApiResponse(responseCode = "500", description = "Internal Server Error")})
public ResponseEntity<GenericAboutToStartOrSubmitCallbackResponse> ccdMidEvent(
@RequestHeader(value = AUTHORIZATION_HEADER) String authorisationToken,
@NotNull @RequestBody @Parameter(description = "CaseData") CallbackRequest callbackRequest) {
@NotNull @RequestBody @Parameter(description = "CaseData") CallbackRequest callbackRequest,
@RequestParam(required = false) String pageId) {

log.info("Mid Event Financial Remedy case callback `{}` received for Case ID: `{}`",
callbackRequest.getEventId(),
callbackRequest.getCaseDetails().getId());

validateCaseData(callbackRequest);

return performRequest(MID_EVENT, callbackRequest, authorisationToken);
return performRequest(MID_EVENT, callbackRequest, authorisationToken, pageId);
}

@PostMapping(path = "/ccdSubmittedEvent")
Expand Down Expand Up @@ -137,12 +139,18 @@ private ResponseEntity<GenericAboutToStartOrSubmitCallbackResponse> performReque
CallbackRequest callbackRequest,
String userAuthorisation) {

return performRequest(callbackType, callbackRequest, userAuthorisation, null);
}

private ResponseEntity<GenericAboutToStartOrSubmitCallbackResponse> performRequest(CallbackType callbackType,
CallbackRequest callbackRequest,
String userAuthorisation, String pageId) {

GenericAboutToStartOrSubmitCallbackResponse callbackResponse =
callbackDispatchService.dispatchToHandlers(callbackType, callbackRequest, userAuthorisation);
callbackDispatchService.dispatchToHandlers(callbackType, pageId, callbackRequest, userAuthorisation);

log.info("Financial Remedy Case CCD callback `{}` handled for Case ID: `{}`",
callbackRequest.getEventId(),
callbackRequest.getCaseDetails().getId());
log.info("Financial Remedy Case CCD callback `{} ({} | {})` handled for Case ID: `{}`",
callbackRequest.getEventId(), callbackType.name(), pageId, callbackRequest.getCaseDetails().getId());

return ok(callbackResponse);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,17 @@ public boolean canHandle(CallbackType callbackType, CaseType caseType, EventType
}

@Override
public GenericAboutToStartOrSubmitCallbackResponse<FinremCaseData> handle(FinremCallbackRequest callbackRequest,
public GenericAboutToStartOrSubmitCallbackResponse<FinremCaseData> handle(FinremCallbackRequest callbackRequestWithFinremCaseDetails,
String userAuthorisation) {
FinremCaseDetails caseDetails = callbackRequest.getCaseDetails();
log.info("Invoking contested event {} mid event callback for Case ID: {}",
EventType.AMEND_CONTESTED_APP_DETAILS, caseDetails.getId());
throw new UnsupportedOperationException("never reach this line");
}

@Override
public GenericAboutToStartOrSubmitCallbackResponse<FinremCaseData> handle(FinremCallbackRequest callbackRequestWithFinremCaseDetails,
String userAuthorisation, CallbackContext context) {
FinremCaseDetails caseDetails = callbackRequestWithFinremCaseDetails.getCaseDetails();
log.info("Invoking contested event {} mid event callback (PageId:{}) for Case ID: {}",
EventType.AMEND_CONTESTED_APP_DETAILS, context.getPageId(), caseDetails.getId());

FinremCaseData caseData = caseDetails.getData();
return GenericAboutToStartOrSubmitCallbackResponse.<FinremCaseData>builder()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package uk.gov.hmcts.reform.finrem.caseorchestration.handler;

import lombok.Builder;
import lombok.Data;
import uk.gov.hmcts.reform.finrem.caseorchestration.ccd.callback.CallbackType;

@Builder
@Data
public class CallbackContext {
private CallbackType callbackType;
private String pageId;
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,15 @@ public interface CallbackHandler<D> {

boolean canHandle(CallbackType callbackType, CaseType caseType, EventType eventType);

default boolean canHandle(CaseType caseType, EventType eventType, CallbackContext context) {
return canHandle(context.getCallbackType(), caseType, eventType);
}

GenericAboutToStartOrSubmitCallbackResponse<D> handle(CallbackRequest callbackRequest,
String userAuthorisation);

default GenericAboutToStartOrSubmitCallbackResponse<D> handle(CallbackRequest callbackRequest, String userAuthorisation,
CallbackContext context) {
return handle(callbackRequest, userAuthorisation);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ public GenericAboutToStartOrSubmitCallbackResponse<FinremCaseData> handle(Callba
public abstract GenericAboutToStartOrSubmitCallbackResponse<FinremCaseData> handle(FinremCallbackRequest callbackRequestWithFinremCaseDetails,
String userAuthorisation);

public GenericAboutToStartOrSubmitCallbackResponse<FinremCaseData> handle(FinremCallbackRequest callbackRequestWithFinremCaseDetails,
String userAuthorisation,
CallbackContext context) {
return handle(callbackRequestWithFinremCaseDetails, userAuthorisation);
}

private FinremCallbackRequest mapToFinremCallbackRequest(CallbackRequest callbackRequest) {
FinremCaseDetails finremCaseDetails = finremCaseDetailsMapper.mapToFinremCaseDetails(callbackRequest.getCaseDetails());
FinremCaseDetails finremCaseDetailsBefore = null;
Expand All @@ -43,12 +49,19 @@ private FinremCallbackRequest mapToFinremCallbackRequest(CallbackRequest callbac
return callbackRequestWithFinremCaseDetails;
}

@Override
public GenericAboutToStartOrSubmitCallbackResponse<FinremCaseData> handle(CallbackRequest callbackRequest, String userAuthorisation,
CallbackContext context) {
FinremCallbackRequest callbackRequestWithFinremCaseDetails =
mapToFinremCallbackRequest(callbackRequest);
return this.handle(callbackRequestWithFinremCaseDetails, userAuthorisation, context);
}

public void validateCaseData(FinremCallbackRequest callbackRequest) {
if (callbackRequest == null
|| callbackRequest.getCaseDetails() == null
|| callbackRequest.getCaseDetails().getData() == null) {
throw new InvalidCaseDataException(BAD_REQUEST.value(), "Missing data from callbackRequest.");
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import uk.gov.hmcts.reform.ccd.client.model.CallbackRequest;
import uk.gov.hmcts.reform.finrem.caseorchestration.ccd.callback.CallbackType;
import uk.gov.hmcts.reform.finrem.caseorchestration.controllers.GenericAboutToStartOrSubmitCallbackResponse;
import uk.gov.hmcts.reform.finrem.caseorchestration.handler.CallbackContext;
import uk.gov.hmcts.reform.finrem.caseorchestration.handler.CallbackHandler;
import uk.gov.hmcts.reform.finrem.caseorchestration.model.EventType;
import uk.gov.hmcts.reform.finrem.caseorchestration.model.ccd.CaseType;
Expand All @@ -29,7 +30,7 @@ public class CallbackDispatchService {
private final List<CallbackHandler> callbackHandlers;

@SuppressWarnings("java:S3740")
public GenericAboutToStartOrSubmitCallbackResponse dispatchToHandlers(CallbackType callbackType,
public GenericAboutToStartOrSubmitCallbackResponse dispatchToHandlers(CallbackType callbackType, String pageId,
CallbackRequest callbackRequest,
String userAuthorisation) {
requireNonNull(callbackRequest, "callback must not be null");
Expand All @@ -44,10 +45,11 @@ public GenericAboutToStartOrSubmitCallbackResponse dispatchToHandlers(CallbackTy
EventType eventType = getEventType(callbackRequest.getEventId());
boolean handled = false;
for (CallbackHandler callbackHandler : callbackHandlers) {
if (callbackHandler.canHandle(callbackType, caseType, eventType)) {
CallbackContext context = CallbackContext.builder().callbackType(callbackType).pageId(pageId).build();
if (callbackHandler.canHandle(caseType, eventType, context)) {

GenericAboutToStartOrSubmitCallbackResponse handlerCallbackResponse =
callbackHandler.handle(callbackRequest, userAuthorisation);
callbackHandler.handle(callbackRequest, userAuthorisation, context);

callbackResponse.setData(handlerCallbackResponse.getData());
List<String> errors = Optional.ofNullable(handlerCallbackResponse.getErrors()).orElse(Collections.emptyList());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ public void givenValidRequest_whenManageBarristerMidEvent_thenProcess() {
request.getCaseDetails().getData().put(APPLICANT_BARRISTER_COLLECTION, applicantBarristerCollection());

ResponseEntity<GenericAboutToStartOrSubmitCallbackResponse> response =
ccdCallbackController.ccdMidEvent(AUTH_TOKEN, request);
ccdCallbackController.ccdMidEvent(AUTH_TOKEN, request, null);

assertNotNull(response.getBody());
List errors = response.getBody().getErrors();
Expand All @@ -226,7 +226,7 @@ public void givenBarristerIsUnregistered_whenManageBarristerMidEvent_thenReturnC
request.getCaseDetails().getData().put(APPLICANT_BARRISTER_COLLECTION, applicantBarristerCollection());

ResponseEntity<GenericAboutToStartOrSubmitCallbackResponse> response =
ccdCallbackController.ccdMidEvent(AUTH_TOKEN, request);
ccdCallbackController.ccdMidEvent(AUTH_TOKEN, request, null);

assertNotNull(response.getBody());
List errors = response.getBody().getErrors();
Expand Down Expand Up @@ -255,7 +255,7 @@ public void givenBarristerAlreadyRepresentsOpposingLitigant_whenManageBarristerM
request.getCaseDetails().getData().put(APPLICANT_BARRISTER_COLLECTION, applicantBarristerCollection());

ResponseEntity<GenericAboutToStartOrSubmitCallbackResponse> response =
ccdCallbackController.ccdMidEvent(AUTH_TOKEN, request);
ccdCallbackController.ccdMidEvent(AUTH_TOKEN, request, null);

assertNotNull(response.getBody());
assertThat(response.getBody().getErrors().get(0), is("Barrister is already representing another party on this case"));
Expand Down