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

CIV-11158 Save hearing location in case data #3524

Merged
merged 9 commits into from
Nov 16, 2023
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@
import uk.gov.hmcts.reform.civil.callback.CaseEvent;
import uk.gov.hmcts.reform.civil.documentmanagement.model.CaseDocument;
import uk.gov.hmcts.reform.civil.model.CaseData;
import uk.gov.hmcts.reform.civil.model.common.DynamicList;
import uk.gov.hmcts.reform.civil.model.common.DynamicListElement;
import uk.gov.hmcts.reform.civil.model.common.Element;
import uk.gov.hmcts.reform.civil.referencedata.LocationRefDataService;
import uk.gov.hmcts.reform.civil.referencedata.model.LocationRefData;
import uk.gov.hmcts.reform.civil.service.docmosis.hearing.HearingNoticeHmcGenerator;
import uk.gov.hmcts.reform.civil.service.hearingnotice.HearingNoticeCamundaService;
import uk.gov.hmcts.reform.civil.utils.HearingFeeUtils;
Expand All @@ -30,6 +34,7 @@
import static uk.gov.hmcts.reform.civil.utils.DateUtils.convertFromUTC;
import static uk.gov.hmcts.reform.civil.utils.ElementUtils.element;
import static uk.gov.hmcts.reform.civil.utils.HmcDataUtils.getHearingDays;
import static uk.gov.hmcts.reform.civil.utils.HmcDataUtils.getLocationRefData;

@Service
@RequiredArgsConstructor
Expand All @@ -45,6 +50,7 @@ public class GenerateHearingNoticeHmcHandler extends CallbackHandler {
private final HearingsService hearingsService;
private final HearingNoticeHmcGenerator hearingNoticeHmcGenerator;
private final ObjectMapper objectMapper;
private final LocationRefDataService locationRefDataService;

@Override
protected Map<String, Callback> callbacks() {
Expand All @@ -65,17 +71,20 @@ private CallbackResponse generateHearingNotice(CallbackParams callbackParams) {
CaseData caseData = callbackParams.getCaseData();
CaseData.CaseDataBuilder<?, ?> caseDataBuilder = caseData.toBuilder();
String processInstanceId = caseData.getBusinessProcess().getProcessInstanceId();
String bearerToken = callbackParams.getParams().get(BEARER_TOKEN).toString();

var camundaVars = camundaService.getProcessVariables(processInstanceId);
var hearing = hearingsService.getHearingResponse(
callbackParams.getParams().get(BEARER_TOKEN).toString(),
bearerToken,
camundaVars.getHearingId()
);

var hearingStartDay = HmcDataUtils.getHearingStartDay(hearing);
var hearingStartDate = convertFromUTC(hearingStartDay.getHearingStartDateTime());
String hearingLocation = getHearingLocation(camundaVars.getHearingId(), hearing,
bearerToken, locationRefDataService);

buildDocument(callbackParams, caseDataBuilder, hearing);
buildDocument(callbackParams, caseDataBuilder, hearing, hearingLocation, camundaVars.getHearingId());

camundaService.setProcessVariables(
processInstanceId,
Expand All @@ -93,16 +102,22 @@ private CallbackResponse generateHearingNotice(CallbackParams callbackParams) {
.data(caseDataBuilder
.hearingDate(hearingStartDate.toLocalDate())
.hearingDueDate(HearingFeeUtils.calculateHearingDueDate(LocalDate.now(), hearingStartDate.toLocalDate()))
.hearingLocation(DynamicList.builder().value(DynamicListElement.builder()
.label(hearingLocation)
.build()).build())
.build().toMap(objectMapper))
.build();
}

private void buildDocument(CallbackParams callbackParams, CaseData.CaseDataBuilder<?, ?> caseDataBuilder, HearingGetResponse hearing) {
private void buildDocument(CallbackParams callbackParams, CaseData.CaseDataBuilder<?, ?> caseDataBuilder, HearingGetResponse hearing,
String hearingLocation, String hearingId) {
CaseData caseData = callbackParams.getCaseData();
List<CaseDocument> caseDocuments = hearingNoticeHmcGenerator.generate(
caseData,
hearing,
callbackParams.getParams().get(BEARER_TOKEN).toString()
callbackParams.getParams().get(BEARER_TOKEN).toString(),
hearingLocation,
hearingId
);
List<Element<CaseDocument>> systemGeneratedCaseDocuments = new ArrayList<>();
systemGeneratedCaseDocuments.add(element(caseDocuments.get(0)));
Expand All @@ -111,4 +126,17 @@ private void buildDocument(CallbackParams callbackParams, CaseData.CaseDataBuild
}
caseDataBuilder.hearingDocuments(systemGeneratedCaseDocuments);
}

private String getHearingLocation(String hearingId, HearingGetResponse hearing,
String bearerToken, LocationRefDataService locationRefDataService) {
LocationRefData hearingLocation = getLocationRefData(
hearingId,
HmcDataUtils.getHearingStartDay(hearing).getHearingVenueId(),
bearerToken,
locationRefDataService);
if (hearingLocation != null) {
return LocationRefDataService.getDisplayEntry(hearingLocation);
}
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import static java.util.Objects.nonNull;
import static uk.gov.hmcts.reform.civil.service.docmosis.DocmosisTemplates.HEARING_NOTICE_HMC;
import static uk.gov.hmcts.reform.civil.utils.HmcDataUtils.getHearingDaysText;
import static uk.gov.hmcts.reform.civil.utils.HmcDataUtils.getLocationRefData;
import static uk.gov.hmcts.reform.civil.utils.HmcDataUtils.getTotalHearingDurationText;

@Service
Expand All @@ -43,10 +44,10 @@ public class HearingNoticeHmcGenerator implements TemplateDataGenerator<HearingN
private final HearingFeesService hearingFeesService;
private final AssignCategoryId assignCategoryId;

public List<CaseDocument> generate(CaseData caseData, HearingGetResponse hearing, String authorisation) {
public List<CaseDocument> generate(CaseData caseData, HearingGetResponse hearing, String authorisation, String hearingLocation, String hearingId) {

List<CaseDocument> caseDocuments = new ArrayList<>();
HearingNoticeHmc templateData = getHearingNoticeTemplateData(caseData, hearing, authorisation);
HearingNoticeHmc templateData = getHearingNoticeTemplateData(caseData, hearing, authorisation, hearingLocation, hearingId);
DocmosisTemplates template = getTemplate(caseData);
DocmosisDocument document =
documentGeneratorService.generateDocmosisDocument(templateData, template);
Expand All @@ -61,23 +62,22 @@ public List<CaseDocument> generate(CaseData caseData, HearingGetResponse hearing
return caseDocuments;
}

public HearingNoticeHmc getHearingNoticeTemplateData(CaseData caseData, HearingGetResponse hearing, String bearerToken) {
public HearingNoticeHmc getHearingNoticeTemplateData(CaseData caseData, HearingGetResponse hearing, String bearerToken,
String hearingLocation, String hearingId) {
var paymentFailed = caseData.getHearingFeePaymentDetails() == null
|| caseData.getHearingFeePaymentDetails().getStatus().equals(PaymentStatus.FAILED);
var feeAmount = paymentFailed
? HearingUtils.formatHearingFee(HearingFeeUtils.calculateAndApplyFee(hearingFeesService, caseData, caseData.getAllocatedTrack())) : null;
var hearingDueDate = paymentFailed ? HearingFeeUtils
.calculateHearingDueDate(LocalDate.now(), HmcDataUtils.getHearingStartDay(hearing)
.getHearingStartDateTime().toLocalDate()) : null;
LocationRefData hearingLocation = getLocationRefData(
HmcDataUtils.getHearingStartDay(hearing).getHearingVenueId(),
bearerToken);

LocationRefData caseManagementLocation =
getLocationRefData(caseData.getCaseManagementLocation().getBaseLocation(), bearerToken);
getLocationRefData(hearingId, caseData.getCaseManagementLocation().getBaseLocation(), bearerToken, locationRefDataService);

return HearingNoticeHmc.builder()
.hearingSiteName(nonNull(caseManagementLocation) ? caseManagementLocation.getSiteName() : null)
.hearingLocation(LocationRefDataService.getDisplayEntry(hearingLocation))
.hearingLocation(hearingLocation)
.caseNumber(caseData.getCcdCaseReference())
.creationDate(LocalDate.now())
.hearingType(getHearingType(hearing))
Expand Down Expand Up @@ -107,13 +107,6 @@ private DocmosisTemplates getTemplate(CaseData caseData) {
return HEARING_NOTICE_HMC;
}

@Nullable
private LocationRefData getLocationRefData(String venueId, String bearerToken) {
List<LocationRefData> locations = locationRefDataService.getCourtLocationsForDefaultJudgments(bearerToken);
var matchedLocations = locations.stream().filter(loc -> loc.getEpimmsId().equals(venueId)).toList();
return matchedLocations.size() > 0 ? matchedLocations.get(0) : null;
}

private String getHearingType(HearingGetResponse hearing) {
if (hearing.getHearingDetails().getHearingType().contains("TRI")) {
return "trial";
Expand Down
15 changes: 15 additions & 0 deletions src/main/java/uk/gov/hmcts/reform/civil/utils/HmcDataUtils.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package uk.gov.hmcts.reform.civil.utils;

import org.jetbrains.annotations.Nullable;
import uk.gov.hmcts.reform.civil.referencedata.LocationRefDataService;
import uk.gov.hmcts.reform.civil.referencedata.model.LocationRefData;
import uk.gov.hmcts.reform.hmc.model.hearing.HearingDaySchedule;
import uk.gov.hmcts.reform.hmc.model.hearing.HearingGetResponse;
import uk.gov.hmcts.reform.hmc.model.hearings.CaseHearing;
Expand Down Expand Up @@ -204,4 +207,16 @@ public static boolean includesVideoHearing(HearingsResponse hearings) {
&& hearings.getCaseHearings().stream()
.filter(hearing -> includesVideoHearing(hearing)).count() > 0;
}

@Nullable
public static LocationRefData getLocationRefData(String hearingId, String venueId,
String bearerToken, LocationRefDataService locationRefDataService) {
List<LocationRefData> locations = locationRefDataService.getCourtLocationsForDefaultJudgments(bearerToken);
var matchedLocations = locations.stream().filter(loc -> loc.getEpimmsId().equals(venueId)).toList();
if (matchedLocations.size() > 0) {
return matchedLocations.get(0);
} else {
throw new IllegalArgumentException("Hearing location data not available for hearing " + hearingId);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
import uk.gov.hmcts.reform.civil.helpers.CaseDetailsConverter;
import uk.gov.hmcts.reform.civil.model.BusinessProcess;
import uk.gov.hmcts.reform.civil.model.CaseData;
import uk.gov.hmcts.reform.civil.referencedata.LocationRefDataService;
import uk.gov.hmcts.reform.civil.referencedata.model.LocationRefData;
import uk.gov.hmcts.reform.civil.sampledata.CaseDocumentBuilder;
import uk.gov.hmcts.reform.civil.service.docmosis.hearing.HearingNoticeHmcGenerator;
import uk.gov.hmcts.reform.civil.service.hearingnotice.HearingNoticeCamundaService;
Expand Down Expand Up @@ -62,6 +64,8 @@ public class GenerateHearingNoticeHmcHandlerTest extends BaseCallbackHandlerTest
private HearingNoticeCamundaService camundaService;
@MockBean
private HearingNoticeHmcGenerator hearingNoticeHmcGenerator;
@MockBean
private LocationRefDataService locationRefDataService;

private static Long CASE_ID = 1L;
private static String HEARING_ID = "1234";
Expand Down Expand Up @@ -108,9 +112,13 @@ public void shouldPopulateCamundaProcessVariables_andReturnExpectedCaseData() {
.caseId(CASE_ID)
.build();

List<LocationRefData> locations = List.of(LocationRefData.builder()
.epimmsId(EPIMS).build());
when(locationRefDataService.getCourtLocationsForDefaultJudgments(anyString()))
.thenReturn(locations);
when(camundaService.getProcessVariables(PROCESS_INSTANCE_ID)).thenReturn(inputVariables);
when(hearingsService.getHearingResponse(anyString(), anyString())).thenReturn(hearing);
when(hearingNoticeHmcGenerator.generate(eq(caseData), eq(hearing), anyString())).thenReturn(List.of(CASE_DOCUMENT));
when(hearingNoticeHmcGenerator.generate(eq(caseData), eq(hearing), anyString(), anyString(), anyString())).thenReturn(List.of(CASE_DOCUMENT));

CallbackParams params = callbackParamsOf(caseData, ABOUT_TO_SUBMIT);
params.getRequest().setEventId(GENERATE_HEARING_NOTICE_HMC.name());
Expand Down Expand Up @@ -168,9 +176,13 @@ public void shouldPopulateCamundaProcessVariables_andReturnExpectedCaseData_BstH
.caseId(CASE_ID)
.build();

List<LocationRefData> locations = List.of(LocationRefData.builder()
.epimmsId(EPIMS).build());
when(locationRefDataService.getCourtLocationsForDefaultJudgments(anyString()))
.thenReturn(locations);
when(camundaService.getProcessVariables(PROCESS_INSTANCE_ID)).thenReturn(inputVariables);
when(hearingsService.getHearingResponse(anyString(), anyString())).thenReturn(hearing);
when(hearingNoticeHmcGenerator.generate(eq(caseData), eq(hearing), anyString())).thenReturn(List.of(CASE_DOCUMENT));
when(hearingNoticeHmcGenerator.generate(eq(caseData), eq(hearing), anyString(), anyString(), anyString())).thenReturn(List.of(CASE_DOCUMENT));

CallbackParams params = callbackParamsOf(caseData, ABOUT_TO_SUBMIT);
params.getRequest().setEventId(GENERATE_HEARING_NOTICE_HMC.name());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,8 @@ void shouldGenerateHearingNoticeHmc_1v1_whenHearingFeeHasBeenPaid() {
.calculatedAmountInPence(new BigDecimal(123))
.build());

var actual = generator.getHearingNoticeTemplateData(caseData, hearing, BEARER_TOKEN);
var actual = generator.getHearingNoticeTemplateData(caseData, hearing, BEARER_TOKEN,
"SiteName - CourtAddress - Postcode", "hearingId");
var expected = HearingNoticeHmc.builder()
.caseNumber(caseData.getCcdCaseReference())
.creationDate(LocalDate.now())
Expand Down Expand Up @@ -212,7 +213,8 @@ void shouldGenerateHearingNoticeHmc_1v1_whenHearingFeeHasNotBeenPaid() {
.calculatedAmountInPence(new BigDecimal(123))
.build());

var actual = generator.getHearingNoticeTemplateData(caseData, hearing, BEARER_TOKEN);
var actual = generator.getHearingNoticeTemplateData(caseData, hearing, BEARER_TOKEN,
"SiteName - CourtAddress - Postcode", "hearingId");
var expected = HearingNoticeHmc.builder()
.caseNumber(caseData.getCcdCaseReference())
.creationDate(LocalDate.now())
Expand Down Expand Up @@ -265,7 +267,8 @@ void shouldGenerateHearingNoticeHmc_1v2DS_whenHearingFeeHasBeenPaid() {
.calculatedAmountInPence(new BigDecimal(123))
.build());

var actual = generator.getHearingNoticeTemplateData(caseData, hearing, BEARER_TOKEN);
var actual = generator.getHearingNoticeTemplateData(caseData, hearing, BEARER_TOKEN,
"SiteName - CourtAddress - Postcode", "hearingId");
var expected = HearingNoticeHmc.builder()
.caseNumber(caseData.getCcdCaseReference())
.creationDate(LocalDate.now())
Expand Down Expand Up @@ -322,7 +325,8 @@ void shouldGenerateHearingNoticeHmc_2v1_whenHearingFeeHasBeenPaid() {
.calculatedAmountInPence(new BigDecimal(123))
.build());

var actual = generator.getHearingNoticeTemplateData(caseData, hearing, BEARER_TOKEN);
var actual = generator.getHearingNoticeTemplateData(caseData, hearing, BEARER_TOKEN,
"SiteName - CourtAddress - Postcode", "hearingId");
var expected = HearingNoticeHmc.builder()
.caseNumber(caseData.getCcdCaseReference())
.creationDate(LocalDate.now())
Expand Down Expand Up @@ -377,7 +381,8 @@ void shouldReturnListOfExpectedCaseDocuments() {
.calculatedAmountInPence(new BigDecimal(123))
.build());

var actual = generator.generate(caseData, hearing, BEARER_TOKEN);
var actual = generator.generate(caseData, hearing, BEARER_TOKEN,
"SiteName - CourtAddress - Postcode", "hearingId");
var expected = List.of(CASE_DOCUMENT);

verify(documentManagementService)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import uk.gov.hmcts.reform.civil.referencedata.LocationRefDataService;
import uk.gov.hmcts.reform.civil.referencedata.model.LocationRefData;
import uk.gov.hmcts.reform.hmc.model.hearing.Attendees;
import uk.gov.hmcts.reform.hmc.model.hearing.CaseDetailsHearing;
import uk.gov.hmcts.reform.hmc.model.hearing.HearingDaySchedule;
Expand All @@ -20,10 +25,13 @@
import java.util.ArrayList;
import java.util.List;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.when;
import static uk.gov.hmcts.reform.civil.utils.HmcDataUtils.includesVideoHearing;
import static uk.gov.hmcts.reform.hmc.model.hearing.HearingSubChannel.INTER;
import static uk.gov.hmcts.reform.hmc.model.hearing.HearingSubChannel.VIDCVP;
Expand Down Expand Up @@ -947,4 +955,40 @@ void shouldReturnTrue_IfVideoHearingsExistOneDayWithinMultipleDays() {
assertTrue(actual);
}
}

@Nested
@ExtendWith(SpringExtension.class)
class GetHearingLocation {

@MockBean
private LocationRefDataService locationRefDataService;

@Test
void shouldReturnLocation_whenInvoked() {
List<LocationRefData> locations = List.of(LocationRefData.builder().epimmsId("venue").build());
when(locationRefDataService.getCourtLocationsForDefaultJudgments("authToken"))
.thenReturn(locations);
LocationRefData locationRefData = HmcDataUtils.getLocationRefData(
"HER123",
"venue",
"authToken",
locationRefDataService
);

assertThat(locationRefData).isEqualTo(LocationRefData.builder().epimmsId("venue").build());
}

@Test
void shouldThrowException_whenLocationIsNull() {
when(locationRefDataService.getCourtLocationsForDefaultJudgments("abc"))
.thenReturn(null);
assertThrows(
IllegalArgumentException.class,
() -> HmcDataUtils.getLocationRefData(
"HER123",
"abc",
"authToken",
locationRefDataService));
}
}
}
Loading