diff --git a/projects/soc-and-delius/src/dev/kotlin/uk/gov/justice/digital/hmpps/data/DataLoader.kt b/projects/soc-and-delius/src/dev/kotlin/uk/gov/justice/digital/hmpps/data/DataLoader.kt index 1b9c5c5a23..d017f057a2 100644 --- a/projects/soc-and-delius/src/dev/kotlin/uk/gov/justice/digital/hmpps/data/DataLoader.kt +++ b/projects/soc-and-delius/src/dev/kotlin/uk/gov/justice/digital/hmpps/data/DataLoader.kt @@ -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, @@ -46,6 +47,7 @@ class DataLoader( ConvictionEventGenerator.OTHER_OFFENCE, ConvictionEventGenerator.DISPOSAL_TYPE, ConvictionEventGenerator.DISPOSAL, + ConvictionEventGenerator.COURT_APPEARANCE, DetailsGenerator.INSTITUTION, DetailsGenerator.RELIGION, DetailsGenerator.NATIONALITY, diff --git a/projects/soc-and-delius/src/dev/kotlin/uk/gov/justice/digital/hmpps/data/generator/ConvictionEventGenerator.kt b/projects/soc-and-delius/src/dev/kotlin/uk/gov/justice/digital/hmpps/data/generator/ConvictionEventGenerator.kt index 081170b17d..85cf21671a 100644 --- a/projects/soc-and-delius/src/dev/kotlin/uk/gov/justice/digital/hmpps/data/generator/ConvictionEventGenerator.kt +++ b/projects/soc-and-delius/src/dev/kotlin/uk/gov/justice/digital/hmpps/data/generator/ConvictionEventGenerator.kt @@ -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 @@ -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(), diff --git a/projects/soc-and-delius/src/dev/kotlin/uk/gov/justice/digital/hmpps/data/generator/CourtAppearanceGenerator.kt b/projects/soc-and-delius/src/dev/kotlin/uk/gov/justice/digital/hmpps/data/generator/CourtAppearanceGenerator.kt index 759be5a4de..eac4cb2f4d 100644 --- a/projects/soc-and-delius/src/dev/kotlin/uk/gov/justice/digital/hmpps/data/generator/CourtAppearanceGenerator.kt +++ b/projects/soc-and-delius/src/dev/kotlin/uk/gov/justice/digital/hmpps/data/generator/CourtAppearanceGenerator.kt @@ -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 { @@ -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(), diff --git a/projects/soc-and-delius/src/integrationTest/kotlin/uk/gov/justice/digital/hmpps/ConvictionsIntegrationTest.kt b/projects/soc-and-delius/src/integrationTest/kotlin/uk/gov/justice/digital/hmpps/ConvictionsIntegrationTest.kt index e049c612f8..98564f9d43 100644 --- a/projects/soc-and-delius/src/integrationTest/kotlin/uk/gov/justice/digital/hmpps/ConvictionsIntegrationTest.kt +++ b/projects/soc-and-delius/src/integrationTest/kotlin/uk/gov/justice/digital/hmpps/ConvictionsIntegrationTest.kt @@ -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 @@ -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, @@ -103,6 +105,7 @@ internal class ConvictionsIntegrationTest { ConvictionEventGenerator.INACTIVE_EVENT.convictionDate, ConvictionEventGenerator.INACTIVE_EVENT.referralDate, "unknown", + "unknown", listOf(), null ) diff --git a/projects/soc-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/entity/ConvictionEventEntity.kt b/projects/soc-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/entity/ConvictionEventEntity.kt index fe319785bd..714d834d59 100644 --- a/projects/soc-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/entity/ConvictionEventEntity.kt +++ b/projects/soc-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/entity/ConvictionEventEntity.kt @@ -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 @@ -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 { @Query( """ @@ -214,6 +239,15 @@ interface ConvictionEventRepository : JpaRepository ] ) fun getAllByConvictionEventPersonNomsNumberAndActiveIsTrue(nomsNumber: String): List + + @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) = diff --git a/projects/soc-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/entity/CourtAppearanceEntity.kt b/projects/soc-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/entity/CourtAppearanceEntity.kt index 229312e9f2..d0623c7890 100644 --- a/projects/soc-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/entity/CourtAppearanceEntity.kt +++ b/projects/soc-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/entity/CourtAppearanceEntity.kt @@ -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 @@ -105,7 +100,7 @@ interface CourtAppearanceRepository : JpaRepository where ca.appearanceDate >= :dateFrom and ca.courtAppearanceEventEntity.courtAppearancePerson.crn = :crn order by ca.appearanceDate desc - """ + """ ) fun findMostRecentCourtAppearancesByCrn(dateFrom: LocalDate, crn: String): List @@ -115,7 +110,7 @@ interface CourtAppearanceRepository : JpaRepository where ca.appearanceDate >= :dateFrom and ca.courtAppearanceEventEntity.courtAppearancePerson.nomsNumber = :nomsNumber order by ca.appearanceDate desc - """ + """ ) fun findMostRecentCourtAppearancesByNomsNumber(dateFrom: LocalDate, nomsNumber: String): List } diff --git a/projects/soc-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/model/Conviction.kt b/projects/soc-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/model/Conviction.kt index 0bb28d5b00..c61e36e3a1 100644 --- a/projects/soc-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/model/Conviction.kt +++ b/projects/soc-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/model/Conviction.kt @@ -7,6 +7,7 @@ data class Conviction( val convictionDate: LocalDate?, val referralDate: LocalDate, val outcome: String, + val latestCourtAppearanceOutcome: String, val offences: List, val sentence: Sentence? ) diff --git a/projects/soc-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/service/ConvictionService.kt b/projects/soc-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/service/ConvictionService.kt index ef1d4d4006..3352966cc5 100644 --- a/projects/soc-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/service/ConvictionService.kt +++ b/projects/soc-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/service/ConvictionService.kt @@ -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) )