Skip to content

Commit

Permalink
MAN-26: Use sentence instead of offence (#4267)
Browse files Browse the repository at this point in the history
* MAN-26: Use sentence instead of offence
  • Loading branch information
pmcphee77 authored Sep 27, 2024
1 parent dcf9ad9 commit bafb324
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<StaffCaseload>()

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
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ data class NextAppointment(
)

data class Appointment(
val id: Long,
val date: ZonedDateTime,
val description: String
)
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -396,16 +401,16 @@ interface CaseloadRepository : JpaRepository<Caseload, Long> {
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) || '%'
or upper(p.forename || ' ' || p.surname) like '%' || upper(:nameOrCrn) || '%'
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(
Expand Down Expand Up @@ -438,20 +443,20 @@ interface CaseloadRepository : JpaRepository<Caseload, Long> {

@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<Offence>
fun findSentenceTypesForStaff(staffCode: String): List<DisposalType>
}

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")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) }

Expand Down Expand Up @@ -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(
Expand Down

0 comments on commit bafb324

Please sign in to comment.