Skip to content

Commit f5bae3b

Browse files
update fee paid checking (#6232)
Co-authored-by: Alistair Osborne <alistair.osborne@justice.gov.uk>
1 parent 485b037 commit f5bae3b

File tree

2 files changed

+254
-24
lines changed

2 files changed

+254
-24
lines changed

service/src/main/java/uk/gov/hmcts/reform/fpl/service/JudicialService.java

Lines changed: 33 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import uk.gov.hmcts.reform.idam.client.models.UserDetails;
3030
import uk.gov.hmcts.reform.rd.client.JudicialApi;
3131
import uk.gov.hmcts.reform.rd.model.JudicialUserAppointment;
32+
import uk.gov.hmcts.reform.rd.model.JudicialUserAuthorisations;
3233
import uk.gov.hmcts.reform.rd.model.JudicialUserProfile;
3334
import uk.gov.hmcts.reform.rd.model.JudicialUserRequest;
3435

@@ -61,6 +62,7 @@
6162
import static uk.gov.hmcts.reform.fpl.enums.LegalAdviserRole.ALLOCATED_LEGAL_ADVISER;
6263
import static uk.gov.hmcts.reform.fpl.enums.LegalAdviserRole.HEARING_LEGAL_ADVISER;
6364
import static uk.gov.hmcts.reform.fpl.utils.ElementUtils.nullSafeCollection;
65+
import static uk.gov.hmcts.reform.fpl.utils.ElementUtils.nullSafeList;
6466
import static uk.gov.hmcts.reform.fpl.utils.JudgeAndLegalAdvisorHelper.formatJudgeTitleAndName;
6567
import static uk.gov.hmcts.reform.fpl.utils.RoleAssignmentUtils.buildRoleAssignment;
6668
import static uk.gov.hmcts.reform.rd.model.JudicialUserAppointment.APPOINTMENT_TYPE_FEE_PAID;
@@ -581,30 +583,46 @@ public boolean isCurrentUserFeePaidJudge() {
581583
UserDetails userDetails = userService.getUserDetails();
582584
List<JudicialUserProfile> judicialUserProfiles = getJudicialUserProfilesByIdamId(userDetails.getId());
583585

584-
return !judicialUserProfiles.isEmpty() && isFeePaidJudge(judicialUserProfiles.get(0));
586+
return !judicialUserProfiles.isEmpty() && isFeePaidJudge(judicialUserProfiles.getFirst());
585587
}
586588

587589
private boolean isFeePaidJudge(JudicialUserProfile judicialUserProfile) {
588590
LocalDate todayDate = time.now().toLocalDate();
589591

590-
List<String> feePaidAppointments =
592+
// get IDs of all active and authorised FPL appointments from authorization list
593+
List<String> authorisedAppointmentIds = nullSafeCollection(judicialUserProfile.getAuthorisations()).stream()
594+
.filter(authorisation -> authorisation.getServiceCodes().contains(SERVICE_CODE))
595+
.filter(authorisations ->
596+
isWithinDateRange(todayDate, authorisations.getStartDate(), authorisations.getEndDate()))
597+
.map(JudicialUserAuthorisations::getAppointmentId)
598+
.sorted()
599+
.toList();
600+
601+
// get all active and authorised FPL appointments and group by primary vs secondary role
602+
Map<Boolean, List<JudicialUserAppointment>> activeAppointmentIds =
591603
nullSafeCollection(judicialUserProfile.getAppointments()).stream()
604+
// check if active
592605
.filter(appointment ->
593-
APPOINTMENT_TYPE_FEE_PAID.equals(appointment.getAppointmentType())
594-
&& isWithinDateRange(todayDate, appointment.getStartDate(), appointment.getEndDate()))
595-
.map(JudicialUserAppointment::getAppointmentId)
596-
.toList();
597-
598-
if (!feePaidAppointments.isEmpty()) {
599-
return nullSafeCollection(judicialUserProfile.getAuthorisations()).stream()
600-
.anyMatch(authorisation ->
601-
nullSafeCollection(authorisation.getServiceCodes()).contains(SERVICE_CODE)
602-
&& isWithinDateRange(todayDate, authorisation.getStartDate(), authorisation.getEndDate())
603-
&& feePaidAppointments.contains(authorisation.getAppointmentId())
604-
);
606+
isWithinDateRange(todayDate, appointment.getStartDate(), appointment.getEndDate()))
607+
// check if authorised
608+
.filter(appointment ->
609+
authorisedAppointmentIds.contains(appointment.getAppointmentId()))
610+
// group by primary vs secondary role
611+
.collect(Collectors.groupingBy(appointment ->
612+
Boolean.valueOf(appointment.getIsPrincipalAppointment())));
613+
614+
List<JudicialUserAppointment> primaryAppointments = nullSafeList(activeAppointmentIds.get(Boolean.TRUE));
615+
List<JudicialUserAppointment> secondaryAppointments = nullSafeList(activeAppointmentIds.get(Boolean.FALSE));
616+
617+
if (primaryAppointments.isEmpty()) {
618+
log.info("No active primary appointment found for user {}", judicialUserProfile.getSidamId());
619+
} else if (primaryAppointments.size() > 1) {
620+
log.warn("More than one active primary appointment found for user {}.", judicialUserProfile.getSidamId());
605621
}
606622

607-
return false;
623+
// if there are no active primary appointments then we check secondary appointments for fee paid role
624+
return (isEmpty(primaryAppointments) ? secondaryAppointments : primaryAppointments).stream()
625+
.anyMatch(appointment -> APPOINTMENT_TYPE_FEE_PAID.equals(appointment.getAppointmentType()));
608626
}
609627

610628
private boolean isWithinDateRange(LocalDate todayDate, LocalDate startDate, LocalDate endDate) {

service/src/test/java/uk/gov/hmcts/reform/fpl/service/JudicialServiceTest.java

Lines changed: 221 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -876,7 +876,7 @@ void shouldReturnIdamUserFullNameIfJRDNotFound() {
876876
@Nested
877877
class IsFeePaidJudge {
878878
@Test
879-
void returnsTrueWhenCurrentUserHasActiveFeePaidAppointment() {
879+
void returnsTrueWhenCurrentUserHasActiveFeePaidAsPrimaryRole() {
880880
final LocalDateTime today = LocalDateTime.now();
881881
when(time.now()).thenReturn(today);
882882

@@ -891,12 +891,14 @@ void returnsTrueWhenCurrentUserHasActiveFeePaidAppointment() {
891891
JudicialUserAppointment.builder()
892892
.appointmentId("feePaidAppointmentId")
893893
.appointmentType(APPOINTMENT_TYPE_FEE_PAID)
894+
.isPrincipalAppointment(Boolean.TRUE.toString())
894895
.startDate(starDate)
895896
.endDate(endDate)
896897
.build(),
897898
JudicialUserAppointment.builder()
898899
.appointmentId("otherAppointmentId")
899-
.appointmentType("Other Appointment Type")
900+
.appointmentType("Salaried / Other Appointment Type")
901+
.isPrincipalAppointment(Boolean.FALSE.toString())
900902
.startDate(starDate)
901903
.endDate(endDate)
902904
.build()))
@@ -906,6 +908,12 @@ void returnsTrueWhenCurrentUserHasActiveFeePaidAppointment() {
906908
.serviceCodes(List.of(SERVICE_CODE))
907909
.startDate(starDate)
908910
.endDate(endDate)
911+
.build(),
912+
JudicialUserAuthorisations.builder()
913+
.appointmentId("otherAppointmentId")
914+
.serviceCodes(List.of(SERVICE_CODE))
915+
.startDate(starDate)
916+
.endDate(endDate)
909917
.build()
910918
))
911919
.build();
@@ -915,7 +923,7 @@ void returnsTrueWhenCurrentUserHasActiveFeePaidAppointment() {
915923
}
916924

917925
@Test
918-
void returnsFalseWhenNoFeePaidAppointment() {
926+
void returnsFalseWhenCurrentUserHasActiveFeePaidAsSecondaryRole() {
919927
final LocalDateTime today = LocalDateTime.now();
920928
when(time.now()).thenReturn(today);
921929

@@ -927,18 +935,173 @@ void returnsFalseWhenNoFeePaidAppointment() {
927935

928936
JudicialUserProfile profile = JudicialUserProfile.builder()
929937
.appointments(List.of(
938+
JudicialUserAppointment.builder()
939+
.appointmentId("feePaidAppointmentId")
940+
.appointmentType(APPOINTMENT_TYPE_FEE_PAID)
941+
.isPrincipalAppointment(Boolean.FALSE.toString())
942+
.startDate(starDate)
943+
.endDate(endDate)
944+
.build(),
945+
JudicialUserAppointment.builder()
946+
.appointmentId("otherAppointmentId")
947+
.appointmentType("Salaried / Other Appointment Type")
948+
.isPrincipalAppointment(Boolean.TRUE.toString())
949+
.startDate(starDate)
950+
.endDate(endDate)
951+
.build()))
952+
.authorisations(List.of(
953+
JudicialUserAuthorisations.builder()
954+
.appointmentId("feePaidAppointmentId")
955+
.serviceCodes(List.of(SERVICE_CODE))
956+
.startDate(starDate)
957+
.endDate(endDate)
958+
.build(),
959+
JudicialUserAuthorisations.builder()
960+
.appointmentId("otherAppointmentId")
961+
.serviceCodes(List.of(SERVICE_CODE))
962+
.startDate(starDate)
963+
.endDate(endDate)
964+
.build()
965+
))
966+
.build();
967+
when(underTest.getJudicialUserProfilesByIdamId("idamId")).thenReturn(List.of(profile));
968+
969+
assertThat(underTest.isCurrentUserFeePaidJudge()).isFalse();
970+
}
971+
972+
@Test
973+
void returnsTrueWhenCurrentUserHasActiveFeePaidAsSecondaryRoleButNoPrimaryRoleExist() {
974+
final LocalDateTime today = LocalDateTime.now();
975+
when(time.now()).thenReturn(today);
976+
977+
final LocalDate starDate = today.toLocalDate().minusDays(1);
978+
final LocalDate endDate = today.toLocalDate().plusDays(1);
979+
980+
UserDetails userDetails = UserDetails.builder().id("idamId").build();
981+
when(userService.getUserDetails()).thenReturn(userDetails);
982+
983+
JudicialUserProfile profile = JudicialUserProfile.builder()
984+
.appointments(List.of(
985+
JudicialUserAppointment.builder()
986+
.appointmentId("feePaidAppointmentId")
987+
.appointmentType(APPOINTMENT_TYPE_FEE_PAID)
988+
.isPrincipalAppointment(Boolean.FALSE.toString())
989+
.startDate(starDate)
990+
.endDate(endDate)
991+
.build(),
992+
JudicialUserAppointment.builder()
993+
.appointmentId("otherAppointmentId")
994+
.appointmentType("Salaried / Other Appointment Type")
995+
.isPrincipalAppointment(Boolean.FALSE.toString())
996+
.startDate(starDate)
997+
.endDate(endDate)
998+
.build()))
999+
.authorisations(List.of(
1000+
JudicialUserAuthorisations.builder()
1001+
.appointmentId("feePaidAppointmentId")
1002+
.serviceCodes(List.of(SERVICE_CODE))
1003+
.startDate(starDate)
1004+
.endDate(endDate)
1005+
.build(),
1006+
JudicialUserAuthorisations.builder()
1007+
.appointmentId("otherAppointmentId")
1008+
.serviceCodes(List.of(SERVICE_CODE))
1009+
.startDate(starDate)
1010+
.endDate(endDate)
1011+
.build()
1012+
))
1013+
.build();
1014+
when(underTest.getJudicialUserProfilesByIdamId("idamId")).thenReturn(List.of(profile));
1015+
1016+
assertThat(underTest.isCurrentUserFeePaidJudge()).isTrue();
1017+
}
1018+
1019+
@Test
1020+
void returnsTrueWhenCurrentUserHasActiveFeePaidAsPrimaryRoleWhenMultiplePrimaryRoleExist() {
1021+
final LocalDateTime today = LocalDateTime.now();
1022+
when(time.now()).thenReturn(today);
1023+
1024+
final LocalDate starDate = today.toLocalDate().minusDays(1);
1025+
final LocalDate endDate = today.toLocalDate().plusDays(1);
1026+
1027+
UserDetails userDetails = UserDetails.builder().id("idamId").build();
1028+
when(userService.getUserDetails()).thenReturn(userDetails);
1029+
1030+
JudicialUserProfile profile = JudicialUserProfile.builder()
1031+
.appointments(List.of(
1032+
JudicialUserAppointment.builder()
1033+
.appointmentId("feePaidAppointmentId")
1034+
.appointmentType(APPOINTMENT_TYPE_FEE_PAID)
1035+
.isPrincipalAppointment(Boolean.TRUE.toString())
1036+
.startDate(starDate)
1037+
.endDate(endDate)
1038+
.build(),
9301039
JudicialUserAppointment.builder()
9311040
.appointmentId("otherAppointmentId")
932-
.appointmentType("Other Appointment Type")
1041+
.appointmentType("Salaried / Other Appointment Type")
1042+
.isPrincipalAppointment(Boolean.TRUE.toString())
9331043
.startDate(starDate)
9341044
.endDate(endDate)
9351045
.build()))
9361046
.authorisations(List.of(
1047+
JudicialUserAuthorisations.builder()
1048+
.appointmentId("feePaidAppointmentId")
1049+
.serviceCodes(List.of(SERVICE_CODE))
1050+
.startDate(starDate)
1051+
.endDate(endDate)
1052+
.build(),
1053+
JudicialUserAuthorisations.builder()
1054+
.appointmentId("otherAppointmentId")
1055+
.serviceCodes(List.of(SERVICE_CODE))
1056+
.startDate(starDate)
1057+
.endDate(endDate)
1058+
.build()
1059+
))
1060+
.build();
1061+
when(underTest.getJudicialUserProfilesByIdamId("idamId")).thenReturn(List.of(profile));
1062+
1063+
assertThat(underTest.isCurrentUserFeePaidJudge()).isTrue();
1064+
}
1065+
1066+
@Test
1067+
void returnsFalseWhenNoFeePaidAppointmentAtAll() {
1068+
final LocalDateTime today = LocalDateTime.now();
1069+
when(time.now()).thenReturn(today);
1070+
1071+
final LocalDate starDate = today.toLocalDate().minusDays(1);
1072+
final LocalDate endDate = today.toLocalDate().plusDays(1);
1073+
1074+
UserDetails userDetails = UserDetails.builder().id("idamId").build();
1075+
when(userService.getUserDetails()).thenReturn(userDetails);
1076+
1077+
JudicialUserProfile profile = JudicialUserProfile.builder()
1078+
.appointments(List.of(
1079+
JudicialUserAppointment.builder()
1080+
.appointmentId("otherAppointmentId")
1081+
.appointmentType("Salaried / Other Appointment Type")
1082+
.isPrincipalAppointment(Boolean.TRUE.toString())
1083+
.startDate(starDate)
1084+
.endDate(endDate)
1085+
.build(),
1086+
JudicialUserAppointment.builder()
1087+
.appointmentId("otherAppointmentId2")
1088+
.appointmentType("Salaried / Other Appointment Type")
1089+
.isPrincipalAppointment(Boolean.FALSE.toString())
1090+
.startDate(starDate)
1091+
.endDate(endDate)
1092+
.build()
1093+
)).authorisations(List.of(
9371094
JudicialUserAuthorisations.builder()
9381095
.appointmentId("otherAppointmentId")
9391096
.serviceCodes(List.of(SERVICE_CODE))
9401097
.startDate(starDate)
9411098
.endDate(endDate)
1099+
.build(),
1100+
JudicialUserAuthorisations.builder()
1101+
.appointmentId("otherAppointmentId2")
1102+
.serviceCodes(List.of(SERVICE_CODE))
1103+
.startDate(starDate)
1104+
.endDate(endDate)
9421105
.build()
9431106
))
9441107
.build();
@@ -948,7 +1111,7 @@ void returnsFalseWhenNoFeePaidAppointment() {
9481111
}
9491112

9501113
@Test
951-
void returnsFalseWhenFeePaidAppointmentExpiredOrNotAppointedToFPL() {
1114+
void returnsFalseWhenFeePaidAppointmentExpired() {
9521115
final LocalDateTime today = LocalDateTime.now();
9531116
when(time.now()).thenReturn(today);
9541117

@@ -963,12 +1126,14 @@ void returnsFalseWhenFeePaidAppointmentExpiredOrNotAppointedToFPL() {
9631126
JudicialUserAppointment.builder()
9641127
.appointmentId("feePaidAppointmentId_expired")
9651128
.appointmentType(APPOINTMENT_TYPE_FEE_PAID)
1129+
.isPrincipalAppointment(Boolean.TRUE.toString())
9661130
.startDate(starDate.minusDays(10))
9671131
.endDate(endDate.minusDays(10))
9681132
.build(),
9691133
JudicialUserAppointment.builder()
970-
.appointmentId("feePaidAppointmentId_otherService")
971-
.appointmentType(APPOINTMENT_TYPE_FEE_PAID)
1134+
.appointmentId("otherAppointmentId")
1135+
.appointmentType("Salaried / Other Appointment Type")
1136+
.isPrincipalAppointment(Boolean.TRUE.toString())
9721137
.startDate(starDate)
9731138
.endDate(endDate)
9741139
.build()))
@@ -980,8 +1145,55 @@ void returnsFalseWhenFeePaidAppointmentExpiredOrNotAppointedToFPL() {
9801145
.endDate(endDate)
9811146
.build(),
9821147
JudicialUserAuthorisations.builder()
983-
.appointmentId("feePaidAppointmentId_otherService")
984-
.serviceCodes(List.of("Other_service"))
1148+
.appointmentId("otherAppointmentId")
1149+
.serviceCodes(List.of(SERVICE_CODE))
1150+
.startDate(starDate)
1151+
.endDate(endDate)
1152+
.build()
1153+
))
1154+
.build();
1155+
when(underTest.getJudicialUserProfilesByIdamId("idamId")).thenReturn(List.of(profile));
1156+
1157+
assertThat(underTest.isCurrentUserFeePaidJudge()).isFalse();
1158+
}
1159+
1160+
@Test
1161+
void returnsFalseWhenAuthorizationOfFeePaidAppointmentExpired() {
1162+
final LocalDateTime today = LocalDateTime.now();
1163+
when(time.now()).thenReturn(today);
1164+
1165+
final LocalDate starDate = today.toLocalDate().minusDays(1);
1166+
final LocalDate endDate = today.toLocalDate().plusDays(1);
1167+
1168+
UserDetails userDetails = UserDetails.builder().id("idamId").build();
1169+
when(userService.getUserDetails()).thenReturn(userDetails);
1170+
1171+
JudicialUserProfile profile = JudicialUserProfile.builder()
1172+
.appointments(List.of(
1173+
JudicialUserAppointment.builder()
1174+
.appointmentId("feePaidAppointmentId_expired")
1175+
.appointmentType(APPOINTMENT_TYPE_FEE_PAID)
1176+
.isPrincipalAppointment(Boolean.FALSE.toString())
1177+
.startDate(starDate)
1178+
.endDate(endDate)
1179+
.build(),
1180+
JudicialUserAppointment.builder()
1181+
.appointmentId("otherAppointmentId")
1182+
.appointmentType("Salaried / Other Appointment Type")
1183+
.isPrincipalAppointment(Boolean.FALSE.toString())
1184+
.startDate(starDate)
1185+
.endDate(endDate)
1186+
.build()))
1187+
.authorisations(List.of(
1188+
JudicialUserAuthorisations.builder()
1189+
.appointmentId("feePaidAppointmentId_expired")
1190+
.serviceCodes(List.of(SERVICE_CODE))
1191+
.startDate(starDate.minusDays(10))
1192+
.endDate(endDate.minusDays(10))
1193+
.build(),
1194+
JudicialUserAuthorisations.builder()
1195+
.appointmentId("otherAppointmentId")
1196+
.serviceCodes(List.of(SERVICE_CODE))
9851197
.startDate(starDate)
9861198
.endDate(endDate)
9871199
.build()

0 commit comments

Comments
 (0)