Skip to content

Commit

Permalink
PI-2245 Add latestCourtAppearanceOutcome to SOC convictions response (#…
Browse files Browse the repository at this point in the history
…3869)

* PI-2245 Add latestCourtAppearanceOutcome to SOC convictions response

* Formatting changes

---------

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
  • Loading branch information
marcus-bcl and github-actions[bot] authored Jun 5, 2024
1 parent 0f923ee commit 426961b
Show file tree
Hide file tree
Showing 8 changed files with 57 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ class DataLoader(
ProbationAreaGenerator.NON_SELECTABLE_PA,
ProbationAreaGenerator.NON_SELECTABLE_BOROUGH,
ProbationAreaGenerator.NON_SELECTABLE_LDU,
CourtAppearanceGenerator.DEFAULT_OUTCOME,
CourtAppearanceGenerator.DEFAULT_CA_TYPE,
CourtAppearanceGenerator.DEFAULT_COURT,
CourtAppearanceGenerator.DEFAULT_PERSON,
Expand All @@ -46,6 +47,7 @@ class DataLoader(
ConvictionEventGenerator.OTHER_OFFENCE,
ConvictionEventGenerator.DISPOSAL_TYPE,
ConvictionEventGenerator.DISPOSAL,
ConvictionEventGenerator.COURT_APPEARANCE,
DetailsGenerator.INSTITUTION,
DetailsGenerator.RELIGION,
DetailsGenerator.NATIONALITY,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package uk.gov.justice.digital.hmpps.data.generator

import uk.gov.justice.digital.hmpps.data.generator.CourtAppearanceGenerator.DEFAULT_OUTCOME
import uk.gov.justice.digital.hmpps.entity.*
import java.time.LocalDate

Expand Down Expand Up @@ -54,6 +55,12 @@ object ConvictionEventGenerator {
DEFAULT_EVENT,
LocalDate.now()
)
val COURT_APPEARANCE = ConvictionCourtAppearanceEntity(
IdGenerator.getAndIncrement(),
DEFAULT_EVENT,
DEFAULT_OUTCOME,
LocalDate.now()
)

val PERSON_2 = ConvictionEventPerson(
IdGenerator.getAndIncrement(),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
package uk.gov.justice.digital.hmpps.data.generator

import uk.gov.justice.digital.hmpps.entity.Court
import uk.gov.justice.digital.hmpps.entity.CourtAppearanceEntity
import uk.gov.justice.digital.hmpps.entity.CourtAppearanceEventEntity
import uk.gov.justice.digital.hmpps.entity.CourtAppearancePerson
import uk.gov.justice.digital.hmpps.entity.ReferenceData
import uk.gov.justice.digital.hmpps.entity.*
import java.time.LocalDate

object CourtAppearanceGenerator {
Expand All @@ -26,6 +22,11 @@ object CourtAppearanceGenerator {
"T",
"Trial/Adjournment"
)
val DEFAULT_OUTCOME = ReferenceData(
IdGenerator.getAndIncrement(),
"505",
"Breach - Continued/Fine"
)
val DEFAULT_CA = CourtAppearanceEntity(
LocalDate.now(),
IdGenerator.getAndIncrement(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import org.springframework.test.web.servlet.MockMvc
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get
import org.springframework.test.web.servlet.result.MockMvcResultMatchers.status
import uk.gov.justice.digital.hmpps.data.generator.ConvictionEventGenerator
import uk.gov.justice.digital.hmpps.data.generator.CourtAppearanceGenerator
import uk.gov.justice.digital.hmpps.data.generator.KeyDateGenerator
import uk.gov.justice.digital.hmpps.model.*
import uk.gov.justice.digital.hmpps.test.MockMvcExtensions.andExpectJson
Expand Down Expand Up @@ -63,6 +64,7 @@ internal class ConvictionsIntegrationTest {
ConvictionEventGenerator.DEFAULT_EVENT.convictionDate,
ConvictionEventGenerator.DEFAULT_EVENT.referralDate,
ConvictionEventGenerator.DISPOSAL_TYPE.description,
CourtAppearanceGenerator.DEFAULT_OUTCOME.description,
listOf(
Offence(
ConvictionEventGenerator.MAIN_OFFENCE.id,
Expand Down Expand Up @@ -103,6 +105,7 @@ internal class ConvictionsIntegrationTest {
ConvictionEventGenerator.INACTIVE_EVENT.convictionDate,
ConvictionEventGenerator.INACTIVE_EVENT.referralDate,
"unknown",
"unknown",
listOf(),
null
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package uk.gov.justice.digital.hmpps.entity
import jakarta.persistence.*
import org.hibernate.annotations.Immutable
import org.hibernate.annotations.SQLRestriction
import org.springframework.data.domain.PageRequest
import org.springframework.data.jpa.repository.EntityGraph
import org.springframework.data.jpa.repository.JpaRepository
import org.springframework.data.jpa.repository.Query
Expand Down Expand Up @@ -163,6 +164,30 @@ class Offence(
val mainCategoryDescription: String,
)

@Entity
@Immutable
@Table(name = "court_appearance")
@SQLRestriction("soft_deleted = 0")
class ConvictionCourtAppearanceEntity(
@Id
@Column(name = "court_appearance_id")
val id: Long,

@JoinColumn(name = "event_id")
@ManyToOne
val convictionEventEntity: ConvictionEventEntity,

@ManyToOne
@JoinColumn(name = "outcome_id")
val outcome: ReferenceData,

@Column(name = "appearance_date")
val appearanceDate: LocalDate,

@Column(name = "soft_deleted", columnDefinition = "number", nullable = false)
val softDeleted: Boolean = false
)

interface ConvictionEventRepository : JpaRepository<ConvictionEventEntity, Long> {
@Query(
"""
Expand Down Expand Up @@ -214,6 +239,15 @@ interface ConvictionEventRepository : JpaRepository<ConvictionEventEntity, Long>
]
)
fun getAllByConvictionEventPersonNomsNumberAndActiveIsTrue(nomsNumber: String): List<ConvictionEventEntity>

@Query(
"""
select ca.outcome.description from ConvictionCourtAppearanceEntity ca
where ca.convictionEventEntity.id = :eventId
order by ca.appearanceDate desc
"""
)
fun findLatestCourtAppearanceOutcome(eventId: Long, pageRequest: PageRequest = PageRequest.of(0, 1)): String?
}

fun ConvictionEventRepository.getLatestConviction(personId: Long) =
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
package uk.gov.justice.digital.hmpps.entity

import jakarta.persistence.Column
import jakarta.persistence.Entity
import jakarta.persistence.Id
import jakarta.persistence.JoinColumn
import jakarta.persistence.ManyToOne
import jakarta.persistence.Table
import jakarta.persistence.*
import org.hibernate.annotations.Immutable
import org.hibernate.annotations.SQLRestriction
import org.springframework.data.jpa.repository.JpaRepository
Expand Down Expand Up @@ -105,7 +100,7 @@ interface CourtAppearanceRepository : JpaRepository<CourtAppearanceEntity, Long>
where ca.appearanceDate >= :dateFrom
and ca.courtAppearanceEventEntity.courtAppearancePerson.crn = :crn
order by ca.appearanceDate desc
"""
"""
)
fun findMostRecentCourtAppearancesByCrn(dateFrom: LocalDate, crn: String): List<CourtAppearanceEntity>

Expand All @@ -115,7 +110,7 @@ interface CourtAppearanceRepository : JpaRepository<CourtAppearanceEntity, Long>
where ca.appearanceDate >= :dateFrom
and ca.courtAppearanceEventEntity.courtAppearancePerson.nomsNumber = :nomsNumber
order by ca.appearanceDate desc
"""
"""
)
fun findMostRecentCourtAppearancesByNomsNumber(dateFrom: LocalDate, nomsNumber: String): List<CourtAppearanceEntity>
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ data class Conviction(
val convictionDate: LocalDate?,
val referralDate: LocalDate,
val outcome: String,
val latestCourtAppearanceOutcome: String,
val offences: List<Offence>,
val sentence: Sentence?
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ class ConvictionService(
convictionEventEntity.convictionDate,
convictionEventEntity.referralDate,
convictionEventEntity.disposal?.type?.description ?: "unknown",
convictionEventRepository.findLatestCourtAppearanceOutcome(convictionEventEntity.id) ?: "unknown",
offences,
convictionEventEntity.disposal?.asModel(custody)
)
Expand Down

0 comments on commit 426961b

Please sign in to comment.