diff --git a/projects/manage-supervision-and-delius/src/integrationTest/kotlin/uk/gov/justice/digital/hmpps/UserIntegrationTest.kt b/projects/manage-supervision-and-delius/src/integrationTest/kotlin/uk/gov/justice/digital/hmpps/UserIntegrationTest.kt index ef4da26d9a..cddaf0bd8d 100644 --- a/projects/manage-supervision-and-delius/src/integrationTest/kotlin/uk/gov/justice/digital/hmpps/UserIntegrationTest.kt +++ b/projects/manage-supervision-and-delius/src/integrationTest/kotlin/uk/gov/justice/digital/hmpps/UserIntegrationTest.kt @@ -181,14 +181,14 @@ internal class UserIntegrationTest { val res = mockMvc .perform( post("/caseload/user/${user.username}/search").withToken() - .withJson(UserSearchFilter(nameOrCrn = null, nextContactCode = null, sentenceCode = "MAIN")) + .withJson(UserSearchFilter(nameOrCrn = null, nextContactCode = null, sentenceCode = "DFS")) ) .andExpect(status().isOk) .andReturn().response.contentAsJson() assertThat(res.caseload.size, equalTo(1)) assertThat(res.caseload[0].crn, equalTo("X000004")) - assertThat(res.caseload[0].latestSentence, equalTo("Murder")) + assertThat(res.caseload[0].latestSentence, equalTo("Default Sentence Type")) } @Test @@ -275,7 +275,7 @@ internal class UserIntegrationTest { assertThat(res.caseload[0].crn, equalTo("X000004")) assertThat(res.caseload[0].caseName.surname, equalTo("Surname")) assertThat(res.metaData?.contactTypes?.size, equalTo(3)) - assertThat(res.metaData?.sentenceTypes?.size, equalTo(2)) + assertThat(res.metaData?.sentenceTypes?.size, equalTo(1)) } @Test diff --git a/projects/manage-supervision-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/api/model/overview/NextAppointment.kt b/projects/manage-supervision-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/api/model/overview/NextAppointment.kt index 462457b31f..dc74d41ebf 100644 --- a/projects/manage-supervision-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/api/model/overview/NextAppointment.kt +++ b/projects/manage-supervision-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/api/model/overview/NextAppointment.kt @@ -8,6 +8,7 @@ data class NextAppointment( ) data class Appointment( + val id: Long, val date: ZonedDateTime, val description: String ) \ No newline at end of file diff --git a/projects/manage-supervision-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/api/model/user/StaffCase.kt b/projects/manage-supervision-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/api/model/user/StaffCase.kt index 9b4068d8ec..309bb162e8 100644 --- a/projects/manage-supervision-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/api/model/user/StaffCase.kt +++ b/projects/manage-supervision-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/api/model/user/StaffCase.kt @@ -10,5 +10,6 @@ data class StaffCase( val dob: LocalDate, val nextAppointment: Appointment? = null, val previousAppointment: Appointment? = null, - val latestSentence: String? = null + val latestSentence: String? = null, + val numberOfAdditionalSentences: Long ) diff --git a/projects/manage-supervision-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/integrations/delius/user/entity/User.kt b/projects/manage-supervision-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/integrations/delius/user/entity/User.kt index c0ede46540..e0c10f5a1d 100644 --- a/projects/manage-supervision-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/integrations/delius/user/entity/User.kt +++ b/projects/manage-supervision-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/integrations/delius/user/entity/User.kt @@ -11,8 +11,8 @@ import org.springframework.data.jpa.repository.JpaRepository import org.springframework.data.jpa.repository.Query import uk.gov.justice.digital.hmpps.exception.NotFoundException import uk.gov.justice.digital.hmpps.integrations.delius.overview.entity.ContactType -import uk.gov.justice.digital.hmpps.integrations.delius.overview.entity.MainOffence -import uk.gov.justice.digital.hmpps.integrations.delius.overview.entity.Offence +import uk.gov.justice.digital.hmpps.integrations.delius.overview.entity.Disposal +import uk.gov.justice.digital.hmpps.integrations.delius.overview.entity.DisposalType import java.time.LocalDate import java.time.ZonedDateTime @@ -249,10 +249,12 @@ data class Caseload( @Entity @Subselect( """ - select * from( - select e.*, + select sub.* + from + (select e.*, + count(e.event_id) over (partition by e.offender_id) as total_sentences, row_number() over (partition by e.offender_id order by cast(e.event_number as number) desc) as row_num - from event e join main_offence mo on mo.event_id = e.event_id + from event e join disposal d on d.event_id = e.event_id where e.soft_deleted = 0 and e.active_flag = 1 ) sub @@ -268,7 +270,10 @@ data class LatestSentence( @ManyToOne @JoinColumn(name = "event_id", referencedColumnName = "event_id", insertable = false, updatable = false) - val mainOffence: MainOffence? = null, + val disposal: Disposal? = null, + + @Column(name = "total_sentences") + val totalNumberOfSentences: Long, ) @Entity @@ -396,8 +401,8 @@ interface CaseloadRepository : JpaRepository { left join fetch c.previousAppointment pa left join fetch pa.type paType left join fetch c.latestSentence ls - left join fetch ls.mainOffence mo - left join fetch mo.offence moo + left join fetch ls.disposal d + left join fetch d.type dt where c.staff.code = :staffCode and (:nameOrCrn is null or upper(p.crn) like '%' || upper(:nameOrCrn) || '%' @@ -405,7 +410,7 @@ interface CaseloadRepository : JpaRepository { or upper(p.surname || ' ' || p.forename) like '%' || upper(:nameOrCrn) || '%' or upper(p.surname || ', ' || p.forename) like '%' || upper(:nameOrCrn) || '%') and (:nextContactCode is null or (upper(trim(naType.code)) = upper(trim(:nextContactCode)))) - and (:sentenceCode is null or (upper(trim(moo.code)) = upper(trim(:sentenceCode)))) + and (:sentenceCode is null or (upper(trim(dt.code)) = upper(trim(:sentenceCode)))) """ ) fun searchByStaffCode( @@ -438,20 +443,20 @@ interface CaseloadRepository : JpaRepository { @Query( """ - select distinct e.mainOffence.offence from Caseload c + select distinct e.disposal.type from Caseload c join Event e on e.personId = c.person.id and e.active = true and e.softDeleted = false - where e.mainOffence.offence is not null + where e.disposal is not null and c.staff.code = :staffCode - order by e.mainOffence.offence.description asc + order by e.disposal.type.description asc """ ) - fun findOffenceTypesForStaff(staffCode: String): List + fun findSentenceTypesForStaff(staffCode: String): List } enum class CaseloadOrderType(val sortColumn: String) { NEXT_CONTACT("naType.description"), LAST_CONTACT("paType.description"), - SENTENCE("moo.description"), + SENTENCE("dt.description"), SURNAME("p.surname"), NAME_OR_CRN("p.surname"), DOB("p.dateOfBirth") diff --git a/projects/manage-supervision-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/service/UserService.kt b/projects/manage-supervision-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/service/UserService.kt index 4bef434bf6..cdd5cd2519 100644 --- a/projects/manage-supervision-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/service/UserService.kt +++ b/projects/manage-supervision-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/service/UserService.kt @@ -47,7 +47,8 @@ class UserService( pageable ) val sentenceTypes = - caseloadRepository.findOffenceTypesForStaff(user.staff.code).map { KeyPair(it.code.trim(), it.description) } + caseloadRepository.findSentenceTypesForStaff(user.staff.code) + .map { KeyPair(it.code.trim(), it.description) } val contactTypes = caseloadRepository.findContactTypesForStaff(user.staff.code).map { KeyPair(it.code.trim(), it.description) } @@ -102,18 +103,21 @@ fun Caseload.toStaffCase() = StaffCase( crn = person.crn, nextAppointment = nextAppointment?.let { Appointment( + id = it.id, description = it.type.description, date = it.appointmentDatetime ) }, previousAppointment = previousAppointment?.let { Appointment( + id = it.id, description = it.type.description, date = it.appointmentDatetime ) }, dob = person.dateOfBirth, - latestSentence = latestSentence?.mainOffence?.offence?.description + latestSentence = latestSentence?.disposal?.type?.description, + numberOfAdditionalSentences = latestSentence?.let { it.totalNumberOfSentences - 1L } ?: 0L ) fun Caseload.toTeamCase() = TeamCase(