diff --git a/src/integration-test/java/uk/gov/hmcts/juror/api/moj/controller/DeferralMaintenanceControllerITest.java b/src/integration-test/java/uk/gov/hmcts/juror/api/moj/controller/DeferralMaintenanceControllerITest.java index e7c2df4dd..16f749340 100644 --- a/src/integration-test/java/uk/gov/hmcts/juror/api/moj/controller/DeferralMaintenanceControllerITest.java +++ b/src/integration-test/java/uk/gov/hmcts/juror/api/moj/controller/DeferralMaintenanceControllerITest.java @@ -57,6 +57,7 @@ import static org.springframework.http.HttpMethod.POST; import static org.springframework.http.HttpStatus.BAD_REQUEST; import static org.springframework.http.HttpStatus.NOT_FOUND; +import static org.springframework.http.HttpStatus.UNPROCESSABLE_ENTITY; import static uk.gov.hmcts.juror.api.moj.enumeration.PoolUtilisationDescription.CONFIRMED; import static uk.gov.hmcts.juror.api.moj.enumeration.PoolUtilisationDescription.NEEDED; import static uk.gov.hmcts.juror.api.moj.enumeration.PoolUtilisationDescription.SURPLUS; @@ -1097,6 +1098,21 @@ void courtUserChangeDateActivePool() { }); } + @Test + void courtUserChangeDateActivePoolAlreadyCheckedIn() { + final String courtJwt = createJwt(COURT_USER, OWNER_415); + + httpHeaders.set(HttpHeaders.AUTHORIZATION, courtJwt); + DeferralReasonRequestDto deferralReasonRequestDto = createDeferralReasonRequestDtoToActivePool(null); + RequestEntity requestEntity = + new RequestEntity<>(deferralReasonRequestDto, + httpHeaders, POST, URI.create(URL_PREFIX + JUROR_555555560)); + ResponseEntity response = template.exchange(requestEntity, + DeferralReasonRequestDto.class); + assertThat(response.getStatusCode()).as("Expect the status to be unprocessable entity") + .isEqualTo(UNPROCESSABLE_ENTITY); + } + private void verifyActivePoolOldRecordChangeDate(JurorPool jurorPool, String poolNumber) { assertThat(jurorPool.getPoolNumber()).isNotEqualTo(poolNumber); assertThat(jurorPool.getStatus().getStatusDesc()).isEqualTo("Deferred"); @@ -1605,6 +1621,19 @@ void bureauPostponeJurorPoolNumberDoesNotExist() { .isEqualTo(NOT_FOUND); } + @Test + @Sql({"/db/mod/truncate.sql", "/db/DeferralMaintenanceController_postponeJuror.sql"}) + void courtPostponeJurorAlreadyCheckedIn() { + setHeaders(COURT_USER, OWNER_415, UserType.COURT); + + ResponseEntity response = template.exchange(new RequestEntity<>( + createProcessJurorPostponementRequestDto(Collections.singletonList(JUROR_555555560)), + httpHeaders, POST, URI.create(URL)), MojException.BadRequest.class); + + assertThat(response.getStatusCode()).as("Expect the status to be unprocessable entity") + .isEqualTo(UNPROCESSABLE_ENTITY); + } + @Test void courtPostponeJurorNoJurorsInRequest() { setHeaders(COURT_USER, OWNER_415, UserType.COURT); @@ -1679,4 +1708,4 @@ protected String createJwt(String login, String owner, UserType userType) { .owner(owner) .build()); } -} \ No newline at end of file +} diff --git a/src/integration-test/java/uk/gov/hmcts/juror/api/moj/controller/DeferralRequestControllerITest.java b/src/integration-test/java/uk/gov/hmcts/juror/api/moj/controller/DeferralRequestControllerITest.java index 6927f53bc..89fb0ca97 100644 --- a/src/integration-test/java/uk/gov/hmcts/juror/api/moj/controller/DeferralRequestControllerITest.java +++ b/src/integration-test/java/uk/gov/hmcts/juror/api/moj/controller/DeferralRequestControllerITest.java @@ -209,6 +209,27 @@ public void grant_Deferral_unhappyPath_deferred_before() { .isEqualTo(HttpStatus.UNPROCESSABLE_ENTITY); } + @Test + @Sql({"/db/mod/truncate.sql", "/db/DeferralRequestController_createInitialPoolRecords.sql"}) + public void grant_Deferral_unhappyPath_appearances_exist() { + String jurorNumber = "222222222"; + String deferralReason = "B"; + + DeferralRequestDto requestDto = createGrantDeferralDecisionDto(jurorNumber, deferralReason); + httpHeaders.set(HttpHeaders.AUTHORIZATION, createJwtCourt("COURT_USER", Set.of(), "415")); + + requestDto.setAllowMultipleDeferrals(false); + ResponseEntity response = + restTemplate.exchange(new RequestEntity<>(requestDto, httpHeaders, HttpMethod.PUT, + URI.create("/api/v1/moj/deferral-response/juror/" + + jurorNumber)), + DeferralRequestDto.class); + + assertThat(response.getStatusCode()) + .as("Expect the HTTP PUT request to be OK") + .isEqualTo(HttpStatus.UNPROCESSABLE_ENTITY); + } + @Test @Sql({"/db/mod/truncate.sql", "/db/DeferralRequestController_createInitialPoolRecords.sql"}) public void grant_Deferral_Unhappy_MissingDate() { diff --git a/src/integration-test/java/uk/gov/hmcts/juror/api/moj/controller/JurorRecordControllerITest.java b/src/integration-test/java/uk/gov/hmcts/juror/api/moj/controller/JurorRecordControllerITest.java index b10765aa0..0236580cd 100644 --- a/src/integration-test/java/uk/gov/hmcts/juror/api/moj/controller/JurorRecordControllerITest.java +++ b/src/integration-test/java/uk/gov/hmcts/juror/api/moj/controller/JurorRecordControllerITest.java @@ -894,7 +894,7 @@ private void verifyJurorAndThirdPartyDetails(JurorDetailsResponseDto jurorDetail .isEqualTo("Responded"); assertThat(jurorDetails.getCommonDetails().getCourtName()) .as("Expect the Juror record to be in Chichester Crown Court") - .isEqualTo("THE CROWN COURT AT CHICHESTER"); + .isEqualTo("LEWES SITTING AT CHICHESTER"); assertThat(thirdParty.getThirdPartyFName()) .as("Expect the third party first name to be TPFIRSTNAME") .isEqualTo("TPFIRSTNAME"); @@ -952,7 +952,7 @@ private void verifyResponse(JurorDetailsResponseDto jurorDetails) { .isEqualTo("Responded"); assertThat(jurorDetails.getCommonDetails().getCourtName()) .as("Expect the Juror record to be in Chichester Crown Court") - .isEqualTo("THE CROWN COURT AT CHICHESTER"); + .isEqualTo("LEWES SITTING AT CHICHESTER"); assertThat(jurorDetails.getThirdParty()) .as("Expect the Juror third party details to be null") .isEqualTo(null); @@ -3378,6 +3378,7 @@ private void validateHappyResponse(ResponseEntity data; @@ -97,4 +101,4 @@ public JurorAttendanceResponseData(Appearance appearance) { this.hours = String.format("%.1f", hours); } } -} \ No newline at end of file +} diff --git a/src/main/java/uk/gov/hmcts/juror/api/moj/exception/MojException.java b/src/main/java/uk/gov/hmcts/juror/api/moj/exception/MojException.java index 4bd59f282..e92cc4180 100644 --- a/src/main/java/uk/gov/hmcts/juror/api/moj/exception/MojException.java +++ b/src/main/java/uk/gov/hmcts/juror/api/moj/exception/MojException.java @@ -95,7 +95,8 @@ public enum ErrorCode { COULD_NOT_FIND_ENOUGH_VOTERS, COULD_NOT_FIND_ENOUGH_ELIGIBLE_VOTERS, DAY_ALREADY_CONFIRMED, - CANNOT_DEFER_TO_EXISTING_POOL + CANNOT_DEFER_TO_EXISTING_POOL, + CANNOT_DEFER_JUROR_WITH_APPEARANCE } } diff --git a/src/main/java/uk/gov/hmcts/juror/api/moj/service/DeferralResponseServiceImpl.java b/src/main/java/uk/gov/hmcts/juror/api/moj/service/DeferralResponseServiceImpl.java index 5df31ac68..e5ae1d476 100644 --- a/src/main/java/uk/gov/hmcts/juror/api/moj/service/DeferralResponseServiceImpl.java +++ b/src/main/java/uk/gov/hmcts/juror/api/moj/service/DeferralResponseServiceImpl.java @@ -22,6 +22,8 @@ import uk.gov.hmcts.juror.api.moj.repository.JurorPoolRepository; import uk.gov.hmcts.juror.api.moj.repository.JurorRepository; import uk.gov.hmcts.juror.api.moj.repository.JurorStatusRepository; +import uk.gov.hmcts.juror.api.moj.service.deferralmaintenance.ManageDeferralsService; +import uk.gov.hmcts.juror.api.moj.service.jurormanagement.JurorAppearanceService; import uk.gov.hmcts.juror.api.moj.service.summonsmanagement.JurorResponseService; import uk.gov.hmcts.juror.api.moj.utils.JurorPoolUtils; import uk.gov.hmcts.juror.api.moj.utils.RepositoryUtils; @@ -56,6 +58,7 @@ public class DeferralResponseServiceImpl implements DeferralResponseService { private final JurorPoolService jurorPoolService; private final JurorStatusRepository jurorStatusRepository; private final JurorResponseService jurorResponseService; + private final JurorAppearanceService jurorAppearanceService; @Override @Transactional @@ -63,10 +66,13 @@ public void respondToDeferralRequest(BureauJwtPayload payload, DeferralRequestDt final String jurorNumber = deferralRequestDto.getJurorNumber(); final String owner = payload.getOwner(); + log.info("Begin process for response to deferral request for juror {}", jurorNumber); JurorPool jurorPool = jurorPoolService.getJurorPoolFromUser(jurorNumber); JurorPoolUtils.checkOwnershipForCurrentUser(jurorPool, owner); + ManageDeferralsService.checkIfJurorHasAttendances(jurorAppearanceService, jurorNumber); + checkExcusalCodeIsValid(deferralRequestDto.getDeferralReason()); Juror juror = jurorPool.getJuror(); diff --git a/src/main/java/uk/gov/hmcts/juror/api/moj/service/JurorRecordServiceImpl.java b/src/main/java/uk/gov/hmcts/juror/api/moj/service/JurorRecordServiceImpl.java index ce85f119b..684235984 100644 --- a/src/main/java/uk/gov/hmcts/juror/api/moj/service/JurorRecordServiceImpl.java +++ b/src/main/java/uk/gov/hmcts/juror/api/moj/service/JurorRecordServiceImpl.java @@ -1286,6 +1286,9 @@ public JurorAttendanceDetailsResponseDto getJurorAttendanceDetails(String locCod responseDto.setNonAttendances((int) jurorAttendanceDetails.stream() .filter(p -> AttendanceType.NON_ATTENDANCE.equals(p.getAttendanceType())).count()); + // the hasAttendances method does not care if appearance is confirmed or not + responseDto.setHasAppearances(jurorAppearanceService.hasAttendances(jurorNumber)); + responseDto.setOnCall(jurorPool.isOnCall()); responseDto.setNextDate(jurorPool.getNextDate()); diff --git a/src/main/java/uk/gov/hmcts/juror/api/moj/service/deferralmaintenance/ManageDeferralsService.java b/src/main/java/uk/gov/hmcts/juror/api/moj/service/deferralmaintenance/ManageDeferralsService.java index 197c22fde..a8ad652f9 100644 --- a/src/main/java/uk/gov/hmcts/juror/api/moj/service/deferralmaintenance/ManageDeferralsService.java +++ b/src/main/java/uk/gov/hmcts/juror/api/moj/service/deferralmaintenance/ManageDeferralsService.java @@ -9,10 +9,14 @@ import uk.gov.hmcts.juror.api.moj.controller.response.DeferralOptionsDto; import uk.gov.hmcts.juror.api.moj.controller.response.deferralmaintenance.DeferralResponseDto; import uk.gov.hmcts.juror.api.moj.domain.PoolRequest; +import uk.gov.hmcts.juror.api.moj.exception.MojException; +import uk.gov.hmcts.juror.api.moj.service.jurormanagement.JurorAppearanceService; import java.time.LocalDate; import java.util.List; +import static uk.gov.hmcts.juror.api.moj.exception.MojException.BusinessRuleViolation.ErrorCode.CANNOT_DEFER_JUROR_WITH_APPEARANCE; + public interface ManageDeferralsService { long getDeferralsCount(String owner, String locationCode, LocalDate attendanceDate); @@ -49,4 +53,12 @@ void changeJurorDeferralDate(BureauJwtPayload payload, String jurorNumber, DeferralResponseDto processJurorPostponement(BureauJwtPayload payload, ProcessJurorPostponementRequestDto processJurorRequestDto); + + static void checkIfJurorHasAttendances(JurorAppearanceService jurorAppearanceService, String jurorNumber) { + // check if the juror has already been checked in/out + if (jurorAppearanceService.hasAttendances(jurorNumber)) { + throw new MojException.BusinessRuleViolation("Juror has already been checked in/out", + CANNOT_DEFER_JUROR_WITH_APPEARANCE); + } + } } diff --git a/src/main/java/uk/gov/hmcts/juror/api/moj/service/deferralmaintenance/ManageDeferralsServiceImpl.java b/src/main/java/uk/gov/hmcts/juror/api/moj/service/deferralmaintenance/ManageDeferralsServiceImpl.java index 83c726462..4362b153f 100644 --- a/src/main/java/uk/gov/hmcts/juror/api/moj/service/deferralmaintenance/ManageDeferralsServiceImpl.java +++ b/src/main/java/uk/gov/hmcts/juror/api/moj/service/deferralmaintenance/ManageDeferralsServiceImpl.java @@ -58,6 +58,7 @@ import uk.gov.hmcts.juror.api.moj.service.PoolMemberSequenceService; import uk.gov.hmcts.juror.api.moj.service.PrintDataService; import uk.gov.hmcts.juror.api.moj.service.SummonsReplyMergeService; +import uk.gov.hmcts.juror.api.moj.service.jurormanagement.JurorAppearanceService; import uk.gov.hmcts.juror.api.moj.utils.DataUtils; import uk.gov.hmcts.juror.api.moj.utils.DateUtils; import uk.gov.hmcts.juror.api.moj.utils.JurorPoolUtils; @@ -116,6 +117,7 @@ public class ManageDeferralsServiceImpl implements ManageDeferralsService { private final JurorHistoryService jurorHistoryService; private final PrintDataService printDataService; private final JurorPoolService jurorPoolService; + private final JurorAppearanceService jurorAppearanceService; /** * When Jurors defer their service to a future date, a record gets added to the currently_deferred table. @@ -240,6 +242,8 @@ public void changeJurorDeferralDate(BureauJwtPayload payload, String jurorNumber JurorPoolUtils.checkOwnershipForCurrentUser(jurorPool, payload.getOwner()); + ManageDeferralsService.checkIfJurorHasAttendances(jurorAppearanceService, jurorNumber); + // if not empty then we need to move the juror to the active pool if (!StringUtils.isEmpty(deferralReasonDto.getPoolNumber())) { @@ -336,6 +340,10 @@ public DeferralResponseDto processJurorPostponement(BureauJwtPayload payload, final String reasonCode = request.getExcusalReasonCode(); log.info("Processing postponement request for juror(s): {}", request.jurorNumbers); + + request.jurorNumbers.forEach(jurorNumber -> + ManageDeferralsService.checkIfJurorHasAttendances(jurorAppearanceService, jurorNumber) + ); int countJurorsPostponed = 0; for (String jurorNumber : request.jurorNumbers) { diff --git a/src/main/java/uk/gov/hmcts/juror/api/moj/xerox/LetterBase.java b/src/main/java/uk/gov/hmcts/juror/api/moj/xerox/LetterBase.java index 48473dfba..71a7b1c4b 100644 --- a/src/main/java/uk/gov/hmcts/juror/api/moj/xerox/LetterBase.java +++ b/src/main/java/uk/gov/hmcts/juror/api/moj/xerox/LetterBase.java @@ -239,10 +239,10 @@ public enum LetterDataType { COURT_LOCATION_CODE(context -> context.getCourtLocation().getLocCode(), ContextType.COURT_LOCATION), COURT_NAME(context -> { String locCode = context.getCourtLocation().getLocCode(); - if (locCode != null && !List.of("400", "626").contains(locCode)) { - return "THE CROWN COURT AT " + context.getCourtLocation().getName(); + if (locCode != null && List.of("000","127","413","414","416","436","463","478").contains(locCode)) { + return "THE CROWN COURT AT" + context.getCourtLocation().getLocCourtName(); } - return context.getCourtLocation().getName(); + return context.getCourtLocation().getLocCourtName(); }, ContextType.COURT_LOCATION), COURT_ADDRESS1(context -> context.getCourtLocation().getAddress1(), ContextType.COURT_LOCATION), COURT_ADDRESS2(context -> context.getCourtLocation().getAddress2(), ContextType.COURT_LOCATION), diff --git a/src/main/resources/db/migrationv2/V2_36__util_report_updates_deferrals.sql b/src/main/resources/db/migrationv2/V2_36__util_report_updates_deferrals.sql new file mode 100644 index 000000000..50d310026 --- /dev/null +++ b/src/main/resources/db/migrationv2/V2_36__util_report_updates_deferrals.sql @@ -0,0 +1,38 @@ + +-- This function is used to generate a list of jurors who are in a pool during a given date range. +-- Updated the query to ignore appearances for deferred jurors before the reporting period. +CREATE OR REPLACE FUNCTION juror_mod.util_report_pool_members_list(p_loc_code text, p_start_date date, p_end_date date) + RETURNS TABLE(juror_number character varying, attendance_date date, return_date date, service_start_date date, service_end_date date, min_status integer) + LANGUAGE plpgsql +AS $function$ +begin + + return query select + jp.juror_number, + min(a.attendance_date) attendance_date, + min(case when jp.is_active = true then p.return_date else to_date('01/12/2099', 'dd/mm/yyyy') end ) return_date, + greatest(least(coalesce(min(a.attendance_date), min(case when jp.is_active = true then p.return_date else to_date('01/12/2099', 'dd/mm/yyyy') end )), + min(case when jp.is_active = true then p.return_date else to_date('01/12/2099','dd/mm/yyyy') end )), p_start_date) service_start, + -- figure out if an appearance occurred before service start date and use that if applicable + least(min(coalesce(case when jp.status = 10 then jp.transfer_date else date(j.completion_date) end, p_end_date)),p_end_date) service_end, + min(status) min_status + from juror_mod.juror_pool jp + join juror_mod.juror j + on jp.juror_number = j.juror_number + join juror_mod.pool p + on jp.pool_number = p.pool_no + left join juror_mod.appearance a + on jp.juror_number = a.juror_number + where p.loc_code = a.loc_code + and (((jp.status in (2,3,4,10,13) or (jp.status = 7 and a.attendance_date is not null and a.attendance_date between p_start_date and p_end_date)) and jp.is_active = true) + or (jp.status = 8 and a.attendance_date is not null and j.completion_date is not null)) + and (a.non_attendance is null or a.non_attendance = false) and (a.no_show is null or a.no_show = false) + and p.loc_code = p_loc_code + and ((jp.status = 10 and jp.transfer_date >= p_start_date) + or (jp.status <> 10 and (j.completion_date is null or j.completion_date >= p_start_date))) + group by jp.juror_number + having (least(coalesce(min(a.attendance_date), min(case when jp.is_active = true then p.return_date else to_date('01/12/2099', 'dd/mm/yyyy') end)), + min(case when jp.is_active = true then p.return_date else to_date('01/12/2099', 'dd/mm/yyyy') end)) <= p_end_date); +END; +$function$ +; \ No newline at end of file diff --git a/src/main/resources/db/migrationv2/V2_37__util_report_updates_trials.sql b/src/main/resources/db/migrationv2/V2_37__util_report_updates_trials.sql new file mode 100644 index 000000000..3276070ce --- /dev/null +++ b/src/main/resources/db/migrationv2/V2_37__util_report_updates_trials.sql @@ -0,0 +1,51 @@ + +-- updating the main report function to include jurors on trials who do not have an appearance record +-- they should be counted as sitting and attended if they are on a trial and the report date is within the trial dates +CREATE OR REPLACE FUNCTION juror_mod.util_report_main(p_loc_code text, p_start_date date, p_end_date date) + RETURNS TABLE(juror_number character varying, report_from date, report_to date, service_from date, service_to date, trial character varying, jury_from date, jury_to date, report_date date, within_service boolean, active_trial boolean, attendance boolean, holiday boolean, is_weekend boolean, working integer, sitting integer, attended integer) + LANGUAGE plpgsql +AS $function$ +begin + +return query select + pool_members.juror_number, + p_start_date as report_from, + p_end_date as report_to, + pool_members.service_from, + pool_members.service_to, + panel_members.trial, + panel_members.jury_from, + panel_members.jury_to, + pool_members.report_date, + pool_members.within_service, + panel_members.active_trial, + appearance.attendance, + pool_members.holiday, + juror_mod.util_is_weekend(pool_members.report_date) as is_weekend, + case when pool_members.within_service and ((pool_members.holiday = false and juror_mod.util_is_weekend(pool_members.report_date) = false) or appearance.attendance) = true then 1 else 0 end as working, + case when pool_members.within_service and panel_members.active_trial and (((appearance.attendance is null or appearance.attendance = false) and pool_members.holiday = false and juror_mod.util_is_weekend(pool_members.report_date) = false) or appearance.attendance) = true then 1 else 0 end as sitting, + case when appearance.attendance or case when pool_members.within_service and panel_members.active_trial and (((appearance.attendance is null or appearance.attendance = false) and pool_members.holiday = false and juror_mod.util_is_weekend(pool_members.report_date) = false) or appearance.attendance) = true then true else false end then 1 else 0 end as attended + + from + (select * from juror_mod.util_report_juror_service_list(p_loc_code, p_start_date, p_end_date)) as pool_members + left join (select * from juror_mod.util_trial_participation_list_dedup(p_loc_code, p_start_date, p_end_date)) as panel_members + on pool_members.juror_number = panel_members.juror_number and pool_members.report_date = panel_members.report_date + left join (select * from juror_mod.util_report_appearance_list(p_loc_code, p_start_date, p_end_date)) as appearance + on pool_members.juror_number = appearance.juror_number and pool_members.report_date = appearance.report_date + group by pool_members.juror_number, + pool_members.report_date, + pool_members.service_from, + pool_members.service_to, + panel_members.trial, + panel_members.jury_from, + panel_members.jury_to, + pool_members.report_date, + pool_members.within_service, + panel_members.active_trial, + appearance.attendance, + pool_members.holiday + order by pool_members.juror_number, pool_members.report_date; + +END; +$function$ +; diff --git a/src/main/resources/db/migrationv2/V2_38__update_court_location_loc_court_name.sql b/src/main/resources/db/migrationv2/V2_38__update_court_location_loc_court_name.sql new file mode 100644 index 000000000..72daa8168 --- /dev/null +++ b/src/main/resources/db/migrationv2/V2_38__update_court_location_loc_court_name.sql @@ -0,0 +1,37 @@ +-- Update the court location names to match the new names + + +UPDATE juror_mod.court_location SET loc_court_name = 'THE CROWN COURT AT PORTSMOUTH' WHERE loc_code = '447'; +UPDATE juror_mod.court_location SET loc_court_name = 'THE CROWN COURT AT NORWICH' WHERE loc_code = '443'; +UPDATE juror_mod.court_location SET loc_court_name = 'ROYAL COURTS OF JUSTICE' WHERE loc_code = '626'; +UPDATE juror_mod.court_location SET loc_court_name = 'NEWPORT(ISLE OF WIGHT)' WHERE loc_code = '478'; +UPDATE juror_mod.court_location SET loc_court_name = 'THE CROWN COURT AT SNARESBROOK' WHERE loc_code = '453'; +UPDATE juror_mod.court_location SET loc_court_name = 'THE CROWN COURT AT KINGSTON-U-HULL' WHERE loc_code = '403'; +UPDATE juror_mod.court_location SET loc_court_name = 'THE CROWN COURT AT HUNTINGDON' WHERE loc_code = '796'; +UPDATE juror_mod.court_location SET loc_court_name = 'THE CROWN COURT AT WOLVERHAMPTON' WHERE loc_code = '421'; +UPDATE juror_mod.court_location SET loc_court_name = 'THE CROWN COURT AT HOVE' WHERE loc_code = '799'; +UPDATE juror_mod.court_location SET loc_court_name = 'THE CROWN COURT AT BOLTON' WHERE loc_code = '470'; +UPDATE juror_mod.court_location SET loc_court_name = 'THE CROWN COURT AT BOURNEMOUTH' WHERE loc_code = '406'; +UPDATE juror_mod.court_location SET loc_court_name = 'THE CROWN COURT AT PRESTON' WHERE loc_code = '448'; +UPDATE juror_mod.court_location SET loc_court_name = 'THE CROWN COURT AT WINCHESTER' WHERE loc_code = '465'; +UPDATE juror_mod.court_location SET loc_court_name = 'LEWES SITTING AT CHICHESTER' WHERE loc_code = '416'; +UPDATE juror_mod.court_location SET loc_court_name = 'THE CROWN COURT AT INNER LONDON CROWN' WHERE loc_code = '440'; +UPDATE juror_mod.court_location SET loc_court_name = 'MANCHESTER, MINSHULL STREET' WHERE loc_code = '436'; +UPDATE juror_mod.court_location SET loc_court_name = 'THE CROWN COURT AT HAVERFORDWEST' WHERE loc_code = '761'; +UPDATE juror_mod.court_location SET loc_court_name = 'THE CROWN COURT AT SALISBURY' WHERE loc_code = '480'; +UPDATE juror_mod.court_location SET loc_court_name = 'WARWICKSHIRE JUSTICE CENTRE' WHERE loc_code = '000'; +UPDATE juror_mod.court_location SET loc_court_name = 'THE CROWN COURT AT CIRENCESTER' WHERE loc_code = '415'; +UPDATE juror_mod.court_location SET loc_court_name = 'THE CROWN COURT AT BURY ST EDMUNDS' WHERE loc_code = '754'; +UPDATE juror_mod.court_location SET loc_court_name = 'THE CROWN COURT AT CARMARTHEN' WHERE loc_code = '756'; +UPDATE juror_mod.court_location SET loc_court_name = 'THE CROWN COURT AT REDDITCH COUNTY COURT' WHERE loc_code = '797'; +UPDATE juror_mod.court_location SET loc_court_name = 'THE CROWN COURT AT MIDDLESEX GUILDHALL' WHERE loc_code = '464'; +UPDATE juror_mod.court_location SET loc_court_name = 'WARWICK SITTING AT LEAMINGTON SPA' WHERE loc_code = '463'; +UPDATE juror_mod.court_location SET loc_court_name = 'THE CROWN COURT AT BARROW-IN-FURNESS' WHERE loc_code = '751'; +UPDATE juror_mod.court_location SET loc_court_name = 'JURY CENTRAL SUMMONING BUREAU' WHERE loc_code = '400'; +UPDATE juror_mod.court_location SET loc_court_name = 'THE CROWN COURT AT KIDDERMINSTER' WHERE loc_code = '798'; +UPDATE juror_mod.court_location SET loc_court_name = 'BIRMINGHAM CIVIL JUSTICE CENTRE' WHERE loc_code = '127'; +UPDATE juror_mod.court_location SET loc_court_name = 'CHELMSFORD CROWN COURT' WHERE loc_code = '414'; +UPDATE juror_mod.court_location SET loc_court_name = 'THE CROWN COURT AT NEWCASTLE UPON TYNE' WHERE loc_code = '439'; +UPDATE juror_mod.court_location SET loc_court_name = 'THE CROWN COURT AT MAIDSTONE' WHERE loc_code = '434'; +UPDATE juror_mod.court_location SET loc_court_name = 'THE CROWN COURT AT NEWPORT S.WALES' WHERE loc_code = '441'; +UPDATE juror_mod.court_location SET loc_court_name = 'THE CENTRAL CRIMINAL CROWN COURT' WHERE loc_code = '413' diff --git a/src/main/resources/db/migrationv2/V2_39__housekeeping_juror_deletion_update.sql b/src/main/resources/db/migrationv2/V2_39__housekeeping_juror_deletion_update.sql new file mode 100644 index 000000000..70d680d5b --- /dev/null +++ b/src/main/resources/db/migrationv2/V2_39__housekeeping_juror_deletion_update.sql @@ -0,0 +1,243 @@ +-- DROP PROCEDURE juror_mod.housekeeping_juror_deletion(in varchar, in int4, in int4, inout text); + +CREATE OR REPLACE PROCEDURE juror_mod.housekeeping_juror_deletion(IN p_juror_number character varying, IN p_start_time_int integer, IN p_max_timeout integer, INOUT p_print_msg text) + LANGUAGE plpgsql +AS $procedure$ +DECLARE + v_text_var1 TEXT; + v_text_var2 TEXT; + v_text_var3 TEXT; + v_timed_out BOOLEAN; + audit_rows RECORD; + financial_audit_rows RECORD; + +BEGIN + p_print_msg := NULL; -- reset + + CREATE TEMPORARY TABLE IF NOT EXISTS deleted_financial_audit (financial_audit_id INTEGER, loc_code VARCHAR(3)); + + <> + -- Perform the deletion + BEGIN + + -- Juror child records + DELETE FROM juror_mod.bulk_print_data bpd WHERE bpd.juror_no = p_juror_number; + -- check if timeout has elapsed - if so exit loop + SELECT juror_mod.check_time_expired(p_start_time_int,p_max_timeout) INTO v_timed_out; + IF v_timed_out THEN + p_print_msg := 'ERROR:-> TIMED OUT'; + EXIT Deletes; + END IF; + + DELETE FROM juror_mod.payment_data pd WHERE pd.juror_number = p_juror_number; + -- check if timeout has elapsed - if so exit loop + SELECT juror_mod.check_time_expired(p_start_time_int,p_max_timeout) INTO v_timed_out; + IF v_timed_out THEN + p_print_msg := 'ERROR:-> TIMED OUT'; + EXIT Deletes; + END IF; + + WITH deleted + AS + ( + DELETE + FROM juror_mod.financial_audit_details_appearances fada + using juror_mod.financial_audit_details fad + where fad.id = fada.financial_audit_id and fad.juror_number = p_juror_number and fada.loc_code = fad.loc_code + RETURNING fada.financial_audit_id, fada.loc_code + ) + INSERT INTO deleted_financial_audit (financial_audit_id, loc_code) + SELECT d.financial_audit_id, d.loc_code + FROM deleted d; + -- check if timeout has elapsed - if so exit loop + SELECT juror_mod.check_time_expired(p_start_time_int,p_max_timeout) INTO v_timed_out; + IF v_timed_out THEN + p_print_msg := 'ERROR:-> TIMED OUT'; + EXIT Deletes; + END IF; + + + DELETE FROM juror_mod.message m WHERE m.juror_number = p_juror_number; + -- check if timeout has elapsed - if so exit loop + SELECT juror_mod.check_time_expired(p_start_time_int,p_max_timeout) INTO v_timed_out; + IF v_timed_out THEN + p_print_msg := 'ERROR:-> TIMED OUT'; + EXIT Deletes; + END IF; + + + DELETE FROM juror_mod.appearance_audit aa WHERE aa.juror_number = p_juror_number; + -- check if timeout has elapsed - if so exit loop + SELECT juror_mod.check_time_expired(p_start_time_int,p_max_timeout) INTO v_timed_out; + IF v_timed_out THEN + p_print_msg := 'ERROR:-> TIMED OUT'; + EXIT Deletes; + END IF; + + -- must remove juror_mod.financial_audit_details before juror_mod.appearance + if exists ( + SELECT 1 + FROM information_schema.tables + WHERE table_type = 'LOCAL TEMPORARY' + AND table_name = 'deleted_financial_audit' + ) then + -- clean up the financial audit details + DELETE + FROM juror_mod.financial_audit_details fad + USING deleted_financial_audit dfa + WHERE fad.id = dfa.financial_audit_id and fad.loc_code = dfa.loc_code and juror_number = p_juror_number; + + drop table deleted_financial_audit; + end if; + + -- check if timeout has elapsed - if so exit loop + SELECT juror_mod.check_time_expired(p_start_time_int,p_max_timeout) INTO v_timed_out; + IF v_timed_out THEN + p_print_msg := 'ERROR:-> TIMED OUT'; + EXIT Deletes; + END IF; + + DELETE FROM juror_mod.bureau_snapshot bss WHERE bss.juror_number = p_juror_number; + -- check if timeout has elapsed - if so exit loop + SELECT juror_mod.check_time_expired(p_start_time_int,p_max_timeout) INTO v_timed_out; + IF v_timed_out THEN + p_print_msg := 'ERROR:-> TIMED OUT'; + EXIT Deletes; + END IF; + + DELETE FROM juror_mod.juror_third_party tp WHERE tp.juror_number = p_juror_number; + -- check if timeout has elapsed - if so exit loop + SELECT juror_mod.check_time_expired(p_start_time_int,p_max_timeout) INTO v_timed_out; + IF v_timed_out THEN + p_print_msg := 'ERROR:-> TIMED OUT'; + EXIT Deletes; + END IF; + + DELETE FROM juror_mod.juror_third_party_audit tpa WHERE tpa.juror_number = p_juror_number; + -- check if timeout has elapsed - if so exit loop + SELECT juror_mod.check_time_expired(p_start_time_int,p_max_timeout) INTO v_timed_out; + IF v_timed_out THEN + p_print_msg := 'ERROR:-> TIMED OUT'; + EXIT Deletes; + END IF; + + DELETE FROM juror_mod.appearance a WHERE a.juror_number = p_juror_number; + -- check if timeout has elapsed - if so exit loop + SELECT juror_mod.check_time_expired(p_start_time_int,p_max_timeout) INTO v_timed_out; + IF v_timed_out THEN + p_print_msg := 'ERROR:-> TIMED OUT'; + EXIT Deletes; + END IF; + + DELETE FROM juror_mod.contact_log cl WHERE cl.juror_number = p_juror_number; + -- check if timeout has elapsed - if so exit loop + SELECT juror_mod.check_time_expired(p_start_time_int,p_max_timeout) INTO v_timed_out; + IF v_timed_out THEN + p_print_msg := 'ERROR:-> TIMED OUT'; + EXIT Deletes; + END IF; + + DELETE FROM juror_mod.user_juror_response_audit ujra WHERE ujra.juror_number = p_juror_number; + -- check if timeout has elapsed - if so exit loop + SELECT juror_mod.check_time_expired(p_start_time_int,p_max_timeout) INTO v_timed_out; + IF v_timed_out THEN + p_print_msg := 'DELETE FAILED - ERROR:-> TIMED OUT'; + END IF; + + + DELETE FROM juror_mod.juror_audit ja WHERE ja.juror_number = p_juror_number; + -- check if timeout has elapsed - if so exit loop + SELECT juror_mod.check_time_expired(p_start_time_int,p_max_timeout) INTO v_timed_out; + IF v_timed_out THEN + p_print_msg := 'ERROR:-> TIMED OUT'; + EXIT Deletes; + END IF; + + DELETE FROM juror_mod.pending_juror pj WHERE pj.juror_number = p_juror_number; + -- check if timeout has elapsed - if so exit loop + SELECT juror_mod.check_time_expired(p_start_time_int,p_max_timeout) INTO v_timed_out; + IF v_timed_out THEN + p_print_msg := 'ERROR:-> TIMED OUT'; + EXIT Deletes; + END IF; + + DELETE FROM juror_mod.juror_pool jp WHERE jp.juror_number = p_juror_number; + -- check if timeout has elapsed - if so exit loop + SELECT juror_mod.check_time_expired(p_start_time_int,p_max_timeout) INTO v_timed_out; + IF v_timed_out THEN + p_print_msg := 'ERROR:-> TIMED OUT'; + EXIT Deletes; + END IF; + + DELETE FROM juror_mod.juror_trial t WHERE t.juror_number = p_juror_number; + -- check if timeout has elapsed - if so exit loop + SELECT juror_mod.check_time_expired(p_start_time_int,p_max_timeout) INTO v_timed_out; + IF v_timed_out THEN + p_print_msg := 'ERROR:-> TIMED OUT'; + EXIT Deletes; + END IF; + + DELETE FROM juror_mod.juror_history jh WHERE jh.juror_number = p_juror_number; + -- check if timeout has elapsed - if so exit loop + SELECT juror_mod.check_time_expired(p_start_time_int,p_max_timeout) INTO v_timed_out; + IF v_timed_out THEN + p_print_msg := 'ERROR:-> TIMED OUT'; + EXIT Deletes; + END IF; + + delete from juror_mod.juror_reasonable_adjustment ra where ra.juror_number = p_juror_number; + SELECT juror_mod.check_time_expired(p_start_time_int,p_max_timeout) INTO v_timed_out; + IF v_timed_out THEN + p_print_msg := 'ERROR:-> TIMED OUT'; + EXIT Deletes; + END IF; + + delete from juror_mod.juror_response_cjs_employment cjs where cjs.juror_number = p_juror_number; + SELECT juror_mod.check_time_expired(p_start_time_int,p_max_timeout) INTO v_timed_out; + IF v_timed_out THEN + p_print_msg := 'ERROR:-> TIMED OUT'; + EXIT Deletes; + END IF; + + delete from juror_mod.juror_response_aud jra where jra.juror_number = p_juror_number; + SELECT juror_mod.check_time_expired(p_start_time_int,p_max_timeout) INTO v_timed_out; + IF v_timed_out THEN + p_print_msg := 'ERROR:-> TIMED OUT'; + EXIT Deletes; + END IF; + + delete from juror_mod.juror_response jr where jr.juror_number = p_juror_number; + SELECT juror_mod.check_time_expired(p_start_time_int,p_max_timeout) INTO v_timed_out; + IF v_timed_out THEN + p_print_msg := 'ERROR:-> TIMED OUT'; + EXIT Deletes; + END IF; + + -- Juror parent record + DELETE FROM juror_mod.juror j WHERE j.juror_number = p_juror_number; + -- check if timeout has elapsed - if so exit loop + SELECT juror_mod.check_time_expired(p_start_time_int,p_max_timeout) INTO v_timed_out; + IF v_timed_out THEN + p_print_msg := 'ERROR:-> TIMED OUT'; + EXIT Deletes; + END IF; + + + + -- upon error... + EXCEPTION + + WHEN OTHERS THEN + + GET STACKED DIAGNOSTICS v_text_var1 = MESSAGE_TEXT, + v_text_var2 = PG_EXCEPTION_DETAIL, + v_text_var3 = PG_EXCEPTION_HINT; + ROLLBACK; + p_print_msg := 'DELETE FAILED - ERROR:->' || v_text_var1 || '|' || v_text_var2 || '|' || v_text_var3; + + END; + + +END; +$procedure$ +; diff --git a/src/main/resources/db/migrationv2/V2_40__write_printfiles_update.sql b/src/main/resources/db/migrationv2/V2_40__write_printfiles_update.sql new file mode 100644 index 000000000..5ba43f817 --- /dev/null +++ b/src/main/resources/db/migrationv2/V2_40__write_printfiles_update.sql @@ -0,0 +1,116 @@ + +-- JM-7885 Update the printfile header record to show the total number of records in the file instead of the total for the form type +-- so that the correct count is given when multiple files of the same form type are generated due to exceeding the max records per file. + +CREATE OR REPLACE PROCEDURE juror_mod.write_printfiles(IN p_form_type character varying, IN p_rec_len integer, IN p_ext_date date, IN p_document_limit integer) + LANGUAGE plpgsql +AS $procedure$ +DECLARE + form_details RECORD; -- current record from bulk print data table. + v_filename VARCHAR(50); -- current filename + v_header VARCHAR(50); -- header for print file + v_row_id INTEGER; -- row id of current record from bulk print data + v_data text:=''; -- data to be written to file + v_detail_rec text; -- row of data from bulk print data + v_revision int8; -- sequence number for file name + v_total_remaining_records integer; -- total amount of remaining records to write + v_documents_to_write integer; -- total amount of documents to write to current file + v_records_count integer; -- total records to be extracted for printing + v_count integer; + +begin + v_count := 0; + select count(bpd.id) + from juror_mod.bulk_print_data bpd + WHERE bpd.form_type = p_form_type + AND bpd.extracted_flag = false + AND DATE(bpd.creation_date) <= p_ext_date + into v_total_remaining_records; + + v_records_count := v_total_remaining_records; + + -- Identify the next sequence number + SELECT NEXTVAL('juror_mod.print_file_count') INTO v_revision; + v_filename := 'JURY'||LPAD(v_revision::VARCHAR(22),4,'0')||'01.0001'; + + -- Calculate documents to write + if v_total_remaining_records - p_document_limit < 0 then + v_documents_to_write := v_total_remaining_records; + else + v_total_remaining_records := v_total_remaining_records - p_document_limit; + v_documents_to_write := p_document_limit; + end if; + + FOR form_details IN + SELECT ROW_NUMBER() OVER () as counter, + bpd.id, + REPLACE(REPLACE(bpd.detail_rec,CHR(10),' '),CHR(13),' ') detail_rec + FROM juror_mod.bulk_print_data bpd + WHERE bpd.form_type = p_form_type + AND bpd.extracted_flag = false + AND DATE(bpd.creation_date) <= p_ext_date + loop + + -- setting header for file first time only + IF v_header IS null or v_header = '' THEN + SELECT RPAD(' ' ||RPAD(p_form_type,16)||LPAD(v_documents_to_write::VARCHAR(6),6,'0')||LPAD(v_documents_to_write::VARCHAR(6),6,'0')||'50'||LPAD(p_rec_len::VARCHAR(8),8,'0'),256,' ') + INTO v_header; + v_data := '' || v_header || chr(10); + END if; + + IF v_count >= p_document_limit then + -- Create the record + INSERT INTO juror_mod.content_store(request_id,document_id,file_type,data) + VALUES (NEXTVAL('juror_mod.content_store_seq'),v_filename,'PRINT',v_data); + COMMIT; + + -- setting new file name as last file has filled up to the limit + SELECT NEXTVAL('juror_mod.print_file_count') INTO v_revision; + v_filename := 'JURY'||LPAD(v_revision::VARCHAR(22),4,'0')||'01.0001'; + + + -- Calculate documents to write + if v_total_remaining_records - p_document_limit < 0 then + v_documents_to_write := v_total_remaining_records; + else + v_total_remaining_records := v_total_remaining_records - p_document_limit; + v_documents_to_write := p_document_limit; + end if; + + -- create new header for new file + SELECT RPAD(' ' ||RPAD(p_form_type,16)||LPAD(v_documents_to_write::VARCHAR(6),6,'0')||LPAD(v_documents_to_write::VARCHAR(6),6,'0')||'50'||LPAD(p_rec_len::VARCHAR(8),8,'0'),256,' ') + INTO v_header; + + -- erasing data for new file + v_data := '' || v_header || chr(10); + v_count := 0; + + END IF; + + -- Form the data column details first + SELECT form_details.id, + form_details.detail_rec + INTO v_row_id, v_detail_rec; + + + -- If data is empty then the first line will be the header plus newline otherwise it will be the current data plus header plus newline + v_data := v_data||v_detail_rec||CHR(10); + + UPDATE juror_mod.bulk_print_data + SET extracted_flag = true + WHERE id = v_row_id; + + -- increment the loop counter so that the process can be stopped if it reaches the threshold + v_count := v_count + 1; + + END LOOP; + + -- flush data to content store if the limit hasn't been reached + IF v_data <> '' THEN + INSERT INTO juror_mod.content_store(request_id,document_id,file_type,data) + VALUES (NEXTVAL('juror_mod.content_store_seq'),v_filename,'PRINT',v_data); + COMMIT; + END IF; +END; +$procedure$ +; diff --git a/src/main/resources/db/migrationv2/V2_41__housekeeping_pool_deletion_update.sql b/src/main/resources/db/migrationv2/V2_41__housekeeping_pool_deletion_update.sql new file mode 100644 index 000000000..ea2eab352 --- /dev/null +++ b/src/main/resources/db/migrationv2/V2_41__housekeeping_pool_deletion_update.sql @@ -0,0 +1,89 @@ +-- DROP PROCEDURE juror_mod.housekeeping_pool_deletion(in int4, in int4, in int4, inout text); + +CREATE OR REPLACE PROCEDURE juror_mod.housekeeping_pool_deletion(IN p_max_threshold integer, IN p_start_time_int integer, IN p_max_timeout integer, INOUT p_print_msg text) + LANGUAGE plpgsql +AS $procedure$ +DECLARE + v_text_var1 TEXT; + v_text_var2 TEXT; + v_text_var3 TEXT; + v_timed_out BOOLEAN; + +BEGIN + <> + BEGIN + -- child records + WITH aged_pools + AS + ( + SELECT p.pool_no + FROM juror_mod.pool p + WHERE p.return_date < CURRENT_DATE - p_max_threshold + ) + DELETE + FROM juror_mod.pool_comments pc + USING aged_pools ap + WHERE pc.pool_no = ap.pool_no; + + -- check if timeout has elapsed - if so exit the process + SELECT juror_mod.check_time_expired(p_start_time_int,p_max_timeout) INTO v_timed_out; + IF v_timed_out THEN + p_print_msg := 'ERROR:-> TIMED OUT'; + EXIT Deletes; + END IF; + + WITH aged_pools + AS + ( + SELECT p.pool_no + FROM juror_mod.pool p + WHERE p.return_date < CURRENT_DATE - p_max_threshold + ) + DELETE + FROM juror_mod.pool_history ph + USING aged_pools ap + WHERE ph.pool_no = ap.pool_no; + + -- check if timeout has elapsed - if so exit the process + SELECT juror_mod.check_time_expired(p_start_time_int,p_max_timeout) INTO v_timed_out; + IF v_timed_out THEN + p_print_msg := 'ERROR:-> TIMED OUT'; + EXIT Deletes; + END IF; + + -- make sure we dont delete pools where there are juror_pool records still referring to them + with old_pools as ( + select pool_no + FROM juror_mod.pool p + WHERE p.return_date < CURRENT_DATE - p_max_threshold + ), exclude_pools as ( + select pool_no from old_pools op inner join juror_mod.juror_pool jp + on op.pool_no = jp.pool_number + ) + delete from juror_mod.pool p using old_pools op + where p.pool_no = op.pool_no and p.pool_no not in (select ep.pool_no from exclude_pools ep); + + + -- check if timeout has elapsed - if so exit the process + SELECT juror_mod.check_time_expired(p_start_time_int,p_max_timeout) INTO v_timed_out; + IF v_timed_out THEN + p_print_msg := 'ERROR:-> TIMED OUT'; + EXIT Deletes; + END IF; + + -- upon error... + EXCEPTION + + WHEN OTHERS THEN + + GET STACKED DIAGNOSTICS v_text_var1 = MESSAGE_TEXT, + v_text_var2 = PG_EXCEPTION_DETAIL, + v_text_var3 = PG_EXCEPTION_HINT; + + p_print_msg := 'DELETE FAILED - ERROR:->' || v_text_var1 || '|' || v_text_var2 || '|' || v_text_var3; + + RAISE NOTICE '%', p_print_msg; + END; +END; +$procedure$ +; diff --git a/src/main/resources/db/migrationv2/V2_42__update_court_location_loc_court_name_error.sql b/src/main/resources/db/migrationv2/V2_42__update_court_location_loc_court_name_error.sql new file mode 100644 index 000000000..6a3ec7a08 --- /dev/null +++ b/src/main/resources/db/migrationv2/V2_42__update_court_location_loc_court_name_error.sql @@ -0,0 +1 @@ +UPDATE juror_mod.court_location SET loc_court_name = 'THE CROWN COURT AT CHESTER' WHERE loc_code = '415'; diff --git a/src/main/resources/db/migrationv2/V2_43__housekeeping_trial_deletion_update.sql b/src/main/resources/db/migrationv2/V2_43__housekeeping_trial_deletion_update.sql new file mode 100644 index 000000000..7d630a19d --- /dev/null +++ b/src/main/resources/db/migrationv2/V2_43__housekeeping_trial_deletion_update.sql @@ -0,0 +1,80 @@ +-- DROP PROCEDURE juror_mod.housekeeping_trial_deletion(in int4, in int4, in int4, inout text); + +CREATE OR REPLACE PROCEDURE juror_mod.housekeeping_trial_deletion(IN p_max_threshold integer, IN p_start_time_int integer, IN p_max_timeout integer, INOUT p_print_msg text) + LANGUAGE plpgsql +AS $procedure$ +DECLARE + v_text_var1 TEXT; + v_text_var2 TEXT; + v_text_var3 TEXT; + v_timed_out BOOLEAN; + +BEGIN + <> + BEGIN + -- child records + + WITH aged_trials AS + ( + SELECT t.trial_number, t.loc_code + FROM juror_mod.trial t + WHERE t.trial_end_date < CURRENT_DATE - p_max_threshold + ), excluded_trials AS + ( + SELECT jt.trial_number, jt.loc_code + FROM juror_mod.juror_trial jt + INNER JOIN aged_trials agt ON jt.trial_number = agt.trial_number and jt.loc_code = agt.loc_code + ) + delete + FROM juror_mod.accused a + USING aged_trials agt + WHERE a.trial_no = agt.trial_number and a.owner = agt.loc_code + and agt.trial_number not in (select et.trial_number from excluded_trials et where et.loc_code = a.owner); + + -- check if timeout has elapsed - if so exit the process + SELECT juror_mod.check_time_expired(p_start_time_int,p_max_timeout) INTO v_timed_out; + IF v_timed_out THEN + p_print_msg := 'ERROR:-> TIMED OUT'; + EXIT Deletes; + END IF; + + -- delete the parent record where there is no reference to it from juror_trial + WITH aged_trials AS + ( + SELECT t.trial_number, t.loc_code + FROM juror_mod.trial t + WHERE t.trial_end_date < CURRENT_DATE - p_max_threshold + ), excluded_trials AS + ( + SELECT jt.trial_number, jt.loc_code + FROM juror_mod.juror_trial jt + INNER JOIN aged_trials agt ON jt.trial_number = agt.trial_number and jt.loc_code = agt.loc_code + ) + DELETE + FROM juror_mod.trial t + USING aged_trials agt + WHERE t.trial_number = agt.trial_number and t.loc_code = agt.loc_code + and agt.trial_number not in (select et.trial_number from excluded_trials et where et.loc_code = t.loc_code); + + -- check if timeout has elapsed - if so exit the process + SELECT juror_mod.check_time_expired(p_start_time_int,p_max_timeout) INTO v_timed_out; + IF v_timed_out THEN + p_print_msg := 'ERROR:-> TIMED OUT'; + EXIT Deletes; + END IF; + + -- upon error... + EXCEPTION + + WHEN OTHERS THEN + + GET STACKED DIAGNOSTICS v_text_var1 = MESSAGE_TEXT, + v_text_var2 = PG_EXCEPTION_DETAIL, + v_text_var3 = PG_EXCEPTION_HINT; + + p_print_msg := 'DELETE FAILED - ERROR:->' || v_text_var1 || '|' || v_text_var2 || '|' || v_text_var3; + + END; +END; +$procedure$ +; diff --git a/src/main/resources/db/migrationv2/V2_44__housekeeping_owner_restrict_list.sql b/src/main/resources/db/migrationv2/V2_44__housekeeping_owner_restrict_list.sql new file mode 100644 index 000000000..2aae0da40 --- /dev/null +++ b/src/main/resources/db/migrationv2/V2_44__housekeeping_owner_restrict_list.sql @@ -0,0 +1,84 @@ +-- This list is used to exclude owners from housekeeping. +-- set the value to 'YES' to include in housekeeping, 'NO' to exclude from housekeeping +-- this script sets loc code 428 (Battersea) to be included in housekeeping for the first run + +INSERT INTO juror_mod.hk_owner_restrict (id,description,value,last_updated) VALUES + (459,'excluding from housekeeping','NO','2024-10-21 17:50:24'), + (407,'excluding from housekeeping','NO','2024-10-21 17:50:24'), + (477,'excluding from housekeeping','NO','2024-10-21 17:50:24'), + (409,'excluding from housekeeping','NO','2024-10-21 17:50:24'), + (416,'excluding from housekeeping','NO','2024-10-21 17:50:24'), + (415,'excluding from housekeeping','NO','2024-10-21 17:50:24'), + (465,'excluding from housekeeping','NO','2024-10-21 17:50:24'), + (478,'excluding from housekeeping','NO','2024-10-21 17:50:24'), + (474,'excluding from housekeeping','NO','2024-10-21 17:50:24'), + (439,'excluding from housekeeping','NO','2024-10-21 17:50:24'), + (404,'excluding from housekeeping','NO','2024-10-21 17:50:24'), + (408,'excluding from housekeeping','NO','2024-10-21 17:50:24'), + (440,'excluding from housekeeping','NO','2024-10-21 17:50:24'), + (458,'excluding from housekeeping','NO','2024-10-21 17:50:24'), + (401,'excluding from housekeeping','NO','2024-10-21 17:50:24'), + (433,'excluding from housekeeping','NO','2024-10-21 17:50:24'), + (411,'excluding from housekeeping','NO','2024-10-21 17:50:24'), + (430,'excluding from housekeeping','NO','2024-10-21 17:50:24'), + (421,'excluding from housekeeping','NO','2024-10-21 17:50:24'), + (472,'excluding from housekeeping','NO','2024-10-21 17:50:24'), + (424,'excluding from housekeeping','NO','2024-10-21 17:50:24'), + (422,'excluding from housekeeping','NO','2024-10-21 17:50:24'), + (442,'excluding from housekeeping','NO','2024-10-21 17:50:24'), + (420,'excluding from housekeeping','NO','2024-10-21 17:50:24'), + (428,'including from housekeeping','YES','2024-10-21 17:50:24'), + (455,'excluding from housekeeping','NO','2024-10-21 17:50:24'), + (480,'excluding from housekeeping','NO','2024-10-21 17:50:24'), + (450,'excluding from housekeeping','NO','2024-10-21 17:50:24'), + (444,'excluding from housekeeping','NO','2024-10-21 17:50:24'), + (468,'excluding from housekeeping','NO','2024-10-21 17:50:24'), + (463,'excluding from housekeeping','NO','2024-10-21 17:50:24'), + (410,'excluding from housekeeping','NO','2024-10-21 17:50:24'), + (423,'excluding from housekeeping','NO','2024-10-21 17:50:24'), + (471,'excluding from housekeeping','NO','2024-10-21 17:50:24'), + (429,'excluding from housekeeping','NO','2024-10-21 17:50:24'), + (470,'excluding from housekeeping','NO','2024-10-21 17:50:24'), + (402,'excluding from housekeeping','NO','2024-10-21 17:50:24'), + (451,'excluding from housekeeping','NO','2024-10-21 17:50:24'), + (466,'excluding from housekeeping','NO','2024-10-21 17:50:24'), + (449,'excluding from housekeeping','NO','2024-10-21 17:50:24'), + (453,'excluding from housekeeping','NO','2024-10-21 17:50:24'), + (473,'excluding from housekeeping','NO','2024-10-21 17:50:24'), + (406,'excluding from housekeeping','NO','2024-10-21 17:50:24'), + (435,'excluding from housekeeping','NO','2024-10-21 17:50:24'), + (434,'excluding from housekeeping','NO','2024-10-21 17:50:24'), + (457,'excluding from housekeeping','NO','2024-10-21 17:50:24'), + (437,'excluding from housekeeping','NO','2024-10-21 17:50:24'), + (419,'excluding from housekeeping','NO','2024-10-21 17:50:24'), + (467,'excluding from housekeeping','NO','2024-10-21 17:50:24'), + (452,'excluding from housekeeping','NO','2024-10-21 17:50:24'), + (412,'excluding from housekeeping','NO','2024-10-21 17:50:24'), + (799,'excluding from housekeeping','NO','2024-10-21 17:50:24'), + (443,'excluding from housekeeping','NO','2024-10-21 17:50:24'), + (418,'excluding from housekeeping','NO','2024-10-21 17:50:24'), + (414,'excluding from housekeeping','NO','2024-10-21 17:50:24'), + (469,'excluding from housekeeping','NO','2024-10-21 17:50:24'), + (476,'excluding from housekeeping','NO','2024-10-21 17:50:24'), + (413,'excluding from housekeeping','NO','2024-10-21 17:50:24'), + (400,'excluding from housekeeping','NO','2024-10-21 17:50:24'), + (425,'excluding from housekeeping','NO','2024-10-21 17:50:24'), + (417,'excluding from housekeeping','NO','2024-10-21 17:50:24'), + (626,'excluding from housekeeping','NO','2024-10-21 17:50:24'), + (456,'excluding from housekeeping','NO','2024-10-21 17:50:24'), + (454,'excluding from housekeeping','NO','2024-10-21 17:50:24'), + (448,'excluding from housekeeping','NO','2024-10-21 17:50:24'), + (432,'excluding from housekeeping','NO','2024-10-21 17:50:24'), + (447,'excluding from housekeeping','NO','2024-10-21 17:50:24'), + (426,'excluding from housekeeping','NO','2024-10-21 17:50:24'), + (461,'excluding from housekeeping','NO','2024-10-21 17:50:24'), + (431,'excluding from housekeeping','NO','2024-10-21 17:50:24'), + (769,'excluding from housekeeping','NO','2024-10-21 17:50:24'), + (403,'excluding from housekeeping','NO','2024-10-21 17:50:24'), + (460,'excluding from housekeeping','NO','2024-10-21 17:50:24'), + (446,'excluding from housekeeping','NO','2024-10-21 17:50:24'), + (436,'excluding from housekeeping','NO','2024-10-21 17:50:24'), + (445,'excluding from housekeeping','NO','2024-10-21 17:50:24'), + (475,'excluding from housekeeping','NO','2024-10-21 17:50:24'), + (479,'excluding from housekeeping','NO','2024-10-21 17:50:24'), + (427,'excluding from housekeeping','NO','2024-10-21 17:50:24'); diff --git a/src/test/java/uk/gov/hmcts/juror/api/moj/service/DeferralResponseServiceImplTest.java b/src/test/java/uk/gov/hmcts/juror/api/moj/service/DeferralResponseServiceImplTest.java index 5ec940a91..49405bec0 100644 --- a/src/test/java/uk/gov/hmcts/juror/api/moj/service/DeferralResponseServiceImplTest.java +++ b/src/test/java/uk/gov/hmcts/juror/api/moj/service/DeferralResponseServiceImplTest.java @@ -24,6 +24,7 @@ import uk.gov.hmcts.juror.api.moj.repository.JurorPoolRepository; import uk.gov.hmcts.juror.api.moj.repository.JurorRepository; import uk.gov.hmcts.juror.api.moj.repository.JurorStatusRepository; +import uk.gov.hmcts.juror.api.moj.service.jurormanagement.JurorAppearanceService; import uk.gov.hmcts.juror.api.moj.service.summonsmanagement.JurorResponseService; import java.util.Collections; @@ -60,7 +61,8 @@ public class DeferralResponseServiceImplTest { private JurorPoolService jurorPoolService; @Mock private JurorResponseService jurorResponseService; - + @Mock + private JurorAppearanceService jurorAppearanceService; @InjectMocks private DeferralResponseServiceImpl deferralResponseService; diff --git a/src/test/java/uk/gov/hmcts/juror/api/moj/service/deferralmaintenance/ManageDeferralsServiceTest.java b/src/test/java/uk/gov/hmcts/juror/api/moj/service/deferralmaintenance/ManageDeferralsServiceTest.java index 599a2f3aa..88ee409a2 100644 --- a/src/test/java/uk/gov/hmcts/juror/api/moj/service/deferralmaintenance/ManageDeferralsServiceTest.java +++ b/src/test/java/uk/gov/hmcts/juror/api/moj/service/deferralmaintenance/ManageDeferralsServiceTest.java @@ -61,6 +61,7 @@ import uk.gov.hmcts.juror.api.moj.service.PoolMemberSequenceService; import uk.gov.hmcts.juror.api.moj.service.PrintDataService; import uk.gov.hmcts.juror.api.moj.service.SummonsReplyMergeService; +import uk.gov.hmcts.juror.api.moj.service.jurormanagement.JurorAppearanceService; import java.time.LocalDate; import java.time.LocalTime; @@ -142,6 +143,8 @@ class ManageDeferralsServiceTest { private WelshCourtLocationRepository welshCourtLocationRepository; @Mock private JurorPoolService jurorPoolService; + @Mock + private JurorAppearanceService jurorAppearanceService; @InjectMocks ManageDeferralsServiceImpl manageDeferralsService; diff --git a/src/test/java/uk/gov/hmcts/juror/api/moj/xerox/LetterTestUtils.java b/src/test/java/uk/gov/hmcts/juror/api/moj/xerox/LetterTestUtils.java index 66348e0e5..47fb50d01 100644 --- a/src/test/java/uk/gov/hmcts/juror/api/moj/xerox/LetterTestUtils.java +++ b/src/test/java/uk/gov/hmcts/juror/api/moj/xerox/LetterTestUtils.java @@ -134,7 +134,7 @@ static CourtLocation testCourtLocation() { courtLocation.setCourtAttendTime(LocalTime.of(10,0)); courtLocation.setLocCode("457"); courtLocation.setInsertIndicators("TWO WEEKS"); - courtLocation.setLocCourtName("SWANSEA CROWN COURT"); + courtLocation.setLocCourtName("THE CROWN COURT AT SWANSEA"); courtLocation.setName("SWANSEA"); courtLocation.setAddress1("THE LAW COURTS"); courtLocation.setAddress2("ST HELENS ROAD");