diff --git a/projects/manage-supervision-and-delius/src/dev/kotlin/uk/gov/justice/digital/hmpps/data/DataLoader.kt b/projects/manage-supervision-and-delius/src/dev/kotlin/uk/gov/justice/digital/hmpps/data/DataLoader.kt index c01290ea12..788c821cf7 100644 --- a/projects/manage-supervision-and-delius/src/dev/kotlin/uk/gov/justice/digital/hmpps/data/DataLoader.kt +++ b/projects/manage-supervision-and-delius/src/dev/kotlin/uk/gov/justice/digital/hmpps/data/DataLoader.kt @@ -7,9 +7,7 @@ import org.springframework.boot.context.event.ApplicationReadyEvent import org.springframework.context.ApplicationListener import org.springframework.stereotype.Component import org.springframework.transaction.annotation.Transactional -import uk.gov.justice.digital.hmpps.data.generator.ContactGenerator -import uk.gov.justice.digital.hmpps.data.generator.PersonGenerator -import uk.gov.justice.digital.hmpps.data.generator.UserGenerator +import uk.gov.justice.digital.hmpps.data.generator.* import uk.gov.justice.digital.hmpps.data.generator.personalDetails.PersonDetailsGenerator import uk.gov.justice.digital.hmpps.user.AuditUserRepository @@ -42,11 +40,26 @@ class DataLoader( entityManager.persistCollection(PersonGenerator.PERSONAL_CIRCUMSTANCES) entityManager.persist(PersonGenerator.OVERVIEW) + entityManager.persist(CourtGenerator.BHAM) entityManager.persist(PersonGenerator.EVENT_1) entityManager.persist(PersonGenerator.EVENT_2) entityManager.persist(PersonGenerator.INACTIVE_EVENT_1) entityManager.persist(PersonGenerator.INACTIVE_EVENT_2) + entityManager.persist(AdditionalSentenceGenerator.REF_DISQ) + entityManager.persist(AdditionalSentenceGenerator.REF_FINE) + entityManager.persist( + AdditionalSentenceGenerator.generateSentence( + 3, + null, + null, + PersonGenerator.EVENT_1, + AdditionalSentenceGenerator.REF_DISQ + ) + ) + entityManager.persist(CourtGenerator.DEFAULT) + entityManager.persist(CourtAppearanceGenerator.generate()) + entityManager.persistAll( PersonGenerator.DEFAULT_DISPOSAL_TYPE, PersonGenerator.ACTIVE_ORDER, diff --git a/projects/manage-supervision-and-delius/src/dev/kotlin/uk/gov/justice/digital/hmpps/data/generator/AdditionalSentenceGenerator.kt b/projects/manage-supervision-and-delius/src/dev/kotlin/uk/gov/justice/digital/hmpps/data/generator/AdditionalSentenceGenerator.kt new file mode 100644 index 0000000000..d461600d64 --- /dev/null +++ b/projects/manage-supervision-and-delius/src/dev/kotlin/uk/gov/justice/digital/hmpps/data/generator/AdditionalSentenceGenerator.kt @@ -0,0 +1,35 @@ +package uk.gov.justice.digital.hmpps.data.generator + +import uk.gov.justice.digital.hmpps.integrations.delius.overview.entity.Event +import uk.gov.justice.digital.hmpps.integrations.delius.referencedata.entity.ReferenceData +import uk.gov.justice.digital.hmpps.integrations.delius.sentence.entity.AdditionalSentence + +object AdditionalSentenceGenerator { + + val REF_DISQ = generateReferenceData("DISQ", "Disqualified from Driving") + val REF_FINE = generateReferenceData("FINE", "Fine") + + val SENTENCE_DISQ = generateSentence(length = 3, referenceData = REF_DISQ, event = PersonGenerator.EVENT_1) + val SENTENCE_FINE = + generateSentence(amount = 500, referenceData = REF_FINE, notes = "fine notes", event = PersonGenerator.EVENT_2) + + fun generateSentence( + length: Long? = null, + amount: Long? = null, + notes: String? = null, + event: Event, + referenceData: ReferenceData + ) = AdditionalSentence( + IdGenerator.getAndIncrement(), + length, + amount, + notes, + false, + event, + referenceData + ) + + private fun generateReferenceData(code: String, description: String) = + ReferenceData(IdGenerator.getAndIncrement(), code, description) +} + diff --git a/projects/manage-supervision-and-delius/src/dev/kotlin/uk/gov/justice/digital/hmpps/data/generator/CourtAppearanceGenerator.kt b/projects/manage-supervision-and-delius/src/dev/kotlin/uk/gov/justice/digital/hmpps/data/generator/CourtAppearanceGenerator.kt new file mode 100644 index 0000000000..9e6485218a --- /dev/null +++ b/projects/manage-supervision-and-delius/src/dev/kotlin/uk/gov/justice/digital/hmpps/data/generator/CourtAppearanceGenerator.kt @@ -0,0 +1,23 @@ +package uk.gov.justice.digital.hmpps.data.generator + +import uk.gov.justice.digital.hmpps.integrations.delius.overview.entity.Event +import uk.gov.justice.digital.hmpps.integrations.delius.sentence.entity.Court +import uk.gov.justice.digital.hmpps.integrations.delius.sentence.entity.CourtAppearance +import java.time.LocalDate + +object CourtAppearanceGenerator { + + fun generate( + court: Court = CourtGenerator.DEFAULT, + date: LocalDate = LocalDate.now().minusMonths(5), + id: Long = IdGenerator.getAndIncrement(), + event: Event = PersonGenerator.EVENT_1 + ): CourtAppearance { + return CourtAppearance( + id, + date, + court, + event + ) + } +} diff --git a/projects/manage-supervision-and-delius/src/dev/kotlin/uk/gov/justice/digital/hmpps/data/generator/CourtGenerator.kt b/projects/manage-supervision-and-delius/src/dev/kotlin/uk/gov/justice/digital/hmpps/data/generator/CourtGenerator.kt new file mode 100644 index 0000000000..1a68ad65b3 --- /dev/null +++ b/projects/manage-supervision-and-delius/src/dev/kotlin/uk/gov/justice/digital/hmpps/data/generator/CourtGenerator.kt @@ -0,0 +1,15 @@ +package uk.gov.justice.digital.hmpps.data.generator + +import uk.gov.justice.digital.hmpps.integrations.delius.sentence.entity.Court + +object CourtGenerator { + val DEFAULT = Court( + IdGenerator.getAndIncrement(), + "Hull Court" + ) + + val BHAM = Court( + IdGenerator.getAndIncrement(), + "Birmingham Court" + ) +} diff --git a/projects/manage-supervision-and-delius/src/dev/kotlin/uk/gov/justice/digital/hmpps/data/generator/PersonGenerator.kt b/projects/manage-supervision-and-delius/src/dev/kotlin/uk/gov/justice/digital/hmpps/data/generator/PersonGenerator.kt index e7c7e34b13..904ca33f2d 100644 --- a/projects/manage-supervision-and-delius/src/dev/kotlin/uk/gov/justice/digital/hmpps/data/generator/PersonGenerator.kt +++ b/projects/manage-supervision-and-delius/src/dev/kotlin/uk/gov/justice/digital/hmpps/data/generator/PersonGenerator.kt @@ -3,6 +3,8 @@ package uk.gov.justice.digital.hmpps.data.generator import uk.gov.justice.digital.hmpps.data.generator.UserGenerator.USER import uk.gov.justice.digital.hmpps.integrations.delius.overview.entity.* import uk.gov.justice.digital.hmpps.integrations.delius.referencedata.entity.ReferenceData +import uk.gov.justice.digital.hmpps.integrations.delius.sentence.entity.Court +import uk.gov.justice.digital.hmpps.integrations.delius.sentence.entity.CourtAppearance import java.time.LocalDate import java.time.LocalDateTime import java.time.ZoneId @@ -11,7 +13,15 @@ import java.time.ZonedDateTime object PersonGenerator { val OVERVIEW = generateOverview("X000004") - val EVENT_1 = generateEvent(OVERVIEW, eventNumber = "7654321", notes = "overview", additionalOffences = emptyList()) + val OFFENDER_WITHOUT_EVENTS = generateOverview("X000005") + val EVENT_1 = generateEvent( + OVERVIEW, + eventNumber = "7654321", + notes = "overview", + additionalOffences = emptyList(), + court = CourtGenerator.BHAM, + convictionDate = LocalDate.now() + ) val EVENT_2 = generateEvent( OVERVIEW, eventNumber = "1234567", @@ -103,6 +113,8 @@ object PersonGenerator { fun generateEvent( person: Person, id: Long = IdGenerator.getAndIncrement(), + court: Court? = null, + convictionDate: LocalDate? = null, eventNumber: String, active: Boolean = true, inBreach: Boolean = false, @@ -114,6 +126,8 @@ object PersonGenerator { Event( id, person.id, + court, + convictionDate, eventNumber, disposal = disposal, inBreach = inBreach, diff --git a/projects/manage-supervision-and-delius/src/integrationTest/kotlin/uk/gov/justice/digital/hmpps/SentenceIntegrationTest.kt b/projects/manage-supervision-and-delius/src/integrationTest/kotlin/uk/gov/justice/digital/hmpps/SentenceIntegrationTest.kt index 791c01b977..607cc26621 100644 --- a/projects/manage-supervision-and-delius/src/integrationTest/kotlin/uk/gov/justice/digital/hmpps/SentenceIntegrationTest.kt +++ b/projects/manage-supervision-and-delius/src/integrationTest/kotlin/uk/gov/justice/digital/hmpps/SentenceIntegrationTest.kt @@ -8,9 +8,7 @@ import org.springframework.boot.test.context.SpringBootTest import org.springframework.test.web.servlet.MockMvc import org.springframework.test.web.servlet.request.MockMvcRequestBuilders import org.springframework.test.web.servlet.result.MockMvcResultMatchers -import uk.gov.justice.digital.hmpps.api.model.sentence.MainOffence -import uk.gov.justice.digital.hmpps.api.model.sentence.Offence -import uk.gov.justice.digital.hmpps.api.model.sentence.SentenceOverview +import uk.gov.justice.digital.hmpps.api.model.sentence.* import uk.gov.justice.digital.hmpps.data.generator.PersonGenerator import uk.gov.justice.digital.hmpps.test.MockMvcExtensions.contentAsJson import uk.gov.justice.digital.hmpps.test.MockMvcExtensions.withToken @@ -22,6 +20,18 @@ class SentenceIntegrationTest { @Autowired lateinit var mockMvc: MockMvc + @Test + fun `no active sentences`() { + val response = mockMvc + .perform(MockMvcRequestBuilders.get("/sentence/${PersonGenerator.OFFENDER_WITHOUT_EVENTS.crn}").withToken()) + .andExpect(MockMvcResultMatchers.status().isOk) + .andReturn().response.contentAsJson() + + val expected = SentenceOverview(listOf()) + + assertEquals(expected, response) + } + @Test fun `get active sentences`() { val response = mockMvc @@ -31,20 +41,31 @@ class SentenceIntegrationTest { val expected = SentenceOverview( listOf( - MainOffence( - Offence("Murder", 1), - LocalDate.now(), - "overview", - listOf( - Offence("Burglary", 1), - Offence("Assault", 1) + Sentence( + OffenceDetails( + Offence("Murder", 1), + LocalDate.now(), + "overview", + listOf( + Offence("Burglary", 1), + Offence("Assault", 1) + ) + ), + Conviction( + "Hull Court", + "Birmingham Court", + LocalDate.now(), + listOf(AdditionalSentence(3, null, null, "Disqualified from Driving")) ) ), - MainOffence( - Offence("Another Murder", 1), - LocalDate.now(), - "overview", - emptyList() + Sentence( + OffenceDetails( + Offence("Another Murder", 1), + LocalDate.now(), + "overview", + emptyList() + ), + Conviction(null, null, null, listOf()) ) ) ) diff --git a/projects/manage-supervision-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/api/model/sentence/AdditionalSentence.kt b/projects/manage-supervision-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/api/model/sentence/AdditionalSentence.kt new file mode 100644 index 0000000000..de11b32dbf --- /dev/null +++ b/projects/manage-supervision-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/api/model/sentence/AdditionalSentence.kt @@ -0,0 +1,8 @@ +package uk.gov.justice.digital.hmpps.api.model.sentence + +data class AdditionalSentence( + val length: Long?, + val amount: Long?, + val notes: String?, + val description: String, +) diff --git a/projects/manage-supervision-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/api/model/sentence/Conviction.kt b/projects/manage-supervision-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/api/model/sentence/Conviction.kt new file mode 100644 index 0000000000..c1ff81d123 --- /dev/null +++ b/projects/manage-supervision-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/api/model/sentence/Conviction.kt @@ -0,0 +1,10 @@ +package uk.gov.justice.digital.hmpps.api.model.sentence + +import java.time.LocalDate + +data class Conviction( + val sentencingCourt: String?, + val responsibleCourt: String?, + val convictionDate: LocalDate?, + val additionalSentences: List +) diff --git a/projects/manage-supervision-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/api/model/sentence/MainOffence.kt b/projects/manage-supervision-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/api/model/sentence/OffenceDetails.kt similarity index 63% rename from projects/manage-supervision-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/api/model/sentence/MainOffence.kt rename to projects/manage-supervision-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/api/model/sentence/OffenceDetails.kt index 0ff83bf9ae..a025c6883e 100644 --- a/projects/manage-supervision-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/api/model/sentence/MainOffence.kt +++ b/projects/manage-supervision-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/api/model/sentence/OffenceDetails.kt @@ -2,9 +2,9 @@ package uk.gov.justice.digital.hmpps.api.model.sentence import java.time.LocalDate -data class MainOffence( - val offence: Offence, - val dateOfOffence: LocalDate, +data class OffenceDetails( + val offence: Offence?, + val dateOfOffence: LocalDate?, val notes: String?, val additionalOffences: List ) diff --git a/projects/manage-supervision-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/api/model/sentence/Sentence.kt b/projects/manage-supervision-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/api/model/sentence/Sentence.kt new file mode 100644 index 0000000000..5cee7bdbb2 --- /dev/null +++ b/projects/manage-supervision-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/api/model/sentence/Sentence.kt @@ -0,0 +1,7 @@ +package uk.gov.justice.digital.hmpps.api.model.sentence + +data class Sentence( + val offence: OffenceDetails, + val conviction: Conviction? = null, + + ) \ No newline at end of file diff --git a/projects/manage-supervision-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/api/model/sentence/SentenceOverview.kt b/projects/manage-supervision-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/api/model/sentence/SentenceOverview.kt index 81ccdbb8a6..9370fcd053 100644 --- a/projects/manage-supervision-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/api/model/sentence/SentenceOverview.kt +++ b/projects/manage-supervision-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/api/model/sentence/SentenceOverview.kt @@ -1,6 +1,5 @@ package uk.gov.justice.digital.hmpps.api.model.sentence data class SentenceOverview( - val offence: List - + val sentences: List, ) diff --git a/projects/manage-supervision-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/integrations/delius/overview/entity/Event.kt b/projects/manage-supervision-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/integrations/delius/overview/entity/Event.kt index b0a04e36da..edb7ac683e 100644 --- a/projects/manage-supervision-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/integrations/delius/overview/entity/Event.kt +++ b/projects/manage-supervision-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/integrations/delius/overview/entity/Event.kt @@ -5,6 +5,7 @@ import org.hibernate.annotations.Immutable import org.hibernate.annotations.SQLRestriction import org.springframework.data.jpa.repository.JpaRepository import org.springframework.data.jpa.repository.Query +import uk.gov.justice.digital.hmpps.integrations.delius.sentence.entity.Court import java.time.LocalDate @Immutable @@ -19,6 +20,13 @@ class Event( @Column(name = "offender_id") val personId: Long, + @ManyToOne + @JoinColumn(name = "court_id") + val court: Court? = null, + + @Column(name = "conviction_date") + val convictionDate: LocalDate?, + @Column(name = "event_number") val eventNumber: String, diff --git a/projects/manage-supervision-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/integrations/delius/sentence/entity/AdditionalSentence.kt b/projects/manage-supervision-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/integrations/delius/sentence/entity/AdditionalSentence.kt new file mode 100644 index 0000000000..d10b3dd85b --- /dev/null +++ b/projects/manage-supervision-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/integrations/delius/sentence/entity/AdditionalSentence.kt @@ -0,0 +1,39 @@ +package uk.gov.justice.digital.hmpps.integrations.delius.sentence.entity + +import jakarta.persistence.* +import org.hibernate.annotations.Immutable +import org.hibernate.annotations.SQLRestriction +import uk.gov.justice.digital.hmpps.integrations.delius.overview.entity.Event +import uk.gov.justice.digital.hmpps.integrations.delius.referencedata.entity.ReferenceData + +@Immutable +@Entity +@Table(name = "additional_sentence") +@SQLRestriction("soft_deleted = 0") +class AdditionalSentence( + + @Id + @Column(name = "additional_sentence_id") + val id: Long, + + @Column(name = "length") + val length: Long? = null, + + @Column(name = "amount") + val amount: Long? = null, + + @Column(name = "notes", columnDefinition = "clob") + val notes: String? = null, + + @Column(columnDefinition = "number") + val softDeleted: Boolean = false, + + @ManyToOne + @JoinColumn(name = "event_id", nullable = false) + val event: Event, + + @ManyToOne + @JoinColumn(name = "additional_sentence_type_id", nullable = false) + val type: ReferenceData, + + ) \ No newline at end of file diff --git a/projects/manage-supervision-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/integrations/delius/sentence/entity/Court.kt b/projects/manage-supervision-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/integrations/delius/sentence/entity/Court.kt new file mode 100644 index 0000000000..34a34908ae --- /dev/null +++ b/projects/manage-supervision-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/integrations/delius/sentence/entity/Court.kt @@ -0,0 +1,19 @@ +package uk.gov.justice.digital.hmpps.integrations.delius.sentence.entity + +import jakarta.persistence.Column +import jakarta.persistence.Entity +import jakarta.persistence.Id +import org.hibernate.annotations.Immutable + +@Entity +@Immutable +class Court( + + @Id + @Column(name = "court_id") + val id: Long, + + @Column(name = "court_name", nullable = true) + val name: String? + +) \ No newline at end of file diff --git a/projects/manage-supervision-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/integrations/delius/sentence/entity/CourtAppearance.kt b/projects/manage-supervision-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/integrations/delius/sentence/entity/CourtAppearance.kt new file mode 100644 index 0000000000..43b029728b --- /dev/null +++ b/projects/manage-supervision-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/integrations/delius/sentence/entity/CourtAppearance.kt @@ -0,0 +1,30 @@ +package uk.gov.justice.digital.hmpps.integrations.delius.sentence.entity + +import jakarta.persistence.* +import org.hibernate.annotations.Immutable +import org.hibernate.annotations.SQLRestriction +import uk.gov.justice.digital.hmpps.integrations.delius.overview.entity.Event +import java.time.LocalDate + +@Entity +@Immutable +@SQLRestriction("soft_deleted = 0") +class CourtAppearance( + @Id + @Column(name = "court_appearance_id") + val id: Long, + + @Column(name = "appearance_date") + val date: LocalDate, + + @JoinColumn(name = "court_id") + @ManyToOne + val court: Court, + + @ManyToOne + @JoinColumn(name = "event_id") + val event: Event, + + @Column(name = "soft_deleted", columnDefinition = "NUMBER", nullable = false) + var softDeleted: Boolean = false +) diff --git a/projects/manage-supervision-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/integrations/delius/sentence/entity/Sentence.kt b/projects/manage-supervision-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/integrations/delius/sentence/entity/SentenceRepository.kt similarity index 61% rename from projects/manage-supervision-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/integrations/delius/sentence/entity/Sentence.kt rename to projects/manage-supervision-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/integrations/delius/sentence/entity/SentenceRepository.kt index e1217aa112..5daa1ee1a8 100644 --- a/projects/manage-supervision-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/integrations/delius/sentence/entity/Sentence.kt +++ b/projects/manage-supervision-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/integrations/delius/sentence/entity/SentenceRepository.kt @@ -1,33 +1,14 @@ package uk.gov.justice.digital.hmpps.integrations.delius.sentence.entity -import jakarta.persistence.Column -import jakarta.persistence.Entity -import jakarta.persistence.Id -import jakarta.persistence.Table -import org.hibernate.annotations.Immutable -import org.hibernate.annotations.SQLRestriction import org.springframework.data.jpa.repository.JpaRepository import org.springframework.data.jpa.repository.Query import uk.gov.justice.digital.hmpps.integrations.delius.overview.entity.Event interface EventSentenceRepository : JpaRepository { - - @Immutable - @Entity - @Table(name = "offender") - @SQLRestriction("soft_deleted = 0") - class Person( - @Id - @Column(name = "offender_id") - val id: Long, - - @Column(columnDefinition = "char(7)") - val crn: String - ) - @Query( "SELECT e FROM Event e " + "JOIN Person p ON p.id = e.personId " + + "LEFT JOIN FETCH e.court c " + "LEFT JOIN FETCH e.mainOffence m " + "LEFT JOIN FETCH e.additionalOffences ao " + "LEFT JOIN FETCH m.offence mo " + @@ -38,3 +19,11 @@ interface EventSentenceRepository : JpaRepository { fun findActiveSentencesByCrn(crn: String): List } +interface CourtAppearanceRepository : JpaRepository { + fun getFirstCourtAppearanceByEventIdOrderByDate(id: Long): CourtAppearance? +} + +interface AdditionalSentenceRepository : JpaRepository { + fun getAllByEventId(id: Long): List +} + diff --git a/projects/manage-supervision-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/service/SentenceService.kt b/projects/manage-supervision-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/service/SentenceService.kt index 37e140ec14..ad24c98538 100644 --- a/projects/manage-supervision-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/service/SentenceService.kt +++ b/projects/manage-supervision-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/service/SentenceService.kt @@ -1,33 +1,46 @@ package uk.gov.justice.digital.hmpps.service import org.springframework.stereotype.Service -import uk.gov.justice.digital.hmpps.api.model.sentence.MainOffence -import uk.gov.justice.digital.hmpps.api.model.sentence.Offence -import uk.gov.justice.digital.hmpps.api.model.sentence.SentenceOverview +import uk.gov.justice.digital.hmpps.api.model.sentence.* import uk.gov.justice.digital.hmpps.integrations.delius.overview.entity.Event +import uk.gov.justice.digital.hmpps.integrations.delius.sentence.entity.AdditionalSentenceRepository +import uk.gov.justice.digital.hmpps.integrations.delius.sentence.entity.CourtAppearance +import uk.gov.justice.digital.hmpps.integrations.delius.sentence.entity.CourtAppearanceRepository import uk.gov.justice.digital.hmpps.integrations.delius.sentence.entity.EventSentenceRepository +import uk.gov.justice.digital.hmpps.integrations.delius.sentence.entity.AdditionalSentence as ExtraSentence @Service class SentenceService( - private val eventRepository: EventSentenceRepository + private val eventRepository: EventSentenceRepository, + private val courtApperanceRepository: CourtAppearanceRepository, + private val additionalSentenceRepository: AdditionalSentenceRepository ) { fun getMostRecentActiveEvent(crn: String): SentenceOverview { val events = eventRepository.findActiveSentencesByCrn(crn) - - return SentenceOverview(events.map { it.toOffence() }) + return SentenceOverview(events.map { + val courtAppearance = courtApperanceRepository.getFirstCourtAppearanceByEventIdOrderByDate(it.id) + val additionalSentences = additionalSentenceRepository.getAllByEventId(it.id) + it.toSentence(courtAppearance, additionalSentences) + }) } - fun Event.toOffence() = mainOffence?.let { mainOffence -> - MainOffence(offence = Offence( - mainOffence.offence.description, mainOffence.offenceCount - ), - dateOfOffence = mainOffence.date, + fun Event.toSentence(courtAppearance: CourtAppearance?, additionalSentences: List) = Sentence( + OffenceDetails( + offence = mainOffence?.let { Offence(it.offence.description, it.offenceCount) }, + dateOfOffence = mainOffence?.date, notes = notes, additionalOffences = additionalOffences.map { - Offence( - description = it.offence.description, count = it.offenceCount ?: 0 - ) + Offence(description = it.offence.description, count = it.offenceCount ?: 0) } + ), + Conviction( + sentencingCourt = courtAppearance?.court?.name, + responsibleCourt = court?.name, + convictionDate = convictionDate, + additionalSentences.map { it.toAdditionalSentence() } ) - } + ) + + fun ExtraSentence.toAdditionalSentence(): AdditionalSentence = + AdditionalSentence(length, amount, notes, type.description) } \ No newline at end of file diff --git a/projects/manage-supervision-and-delius/src/test/kotlin/uk/gov/justice/digital/hmpps/service/SentenceServiceTest.kt b/projects/manage-supervision-and-delius/src/test/kotlin/uk/gov/justice/digital/hmpps/service/SentenceServiceTest.kt index a6d21c207b..5495c9f695 100644 --- a/projects/manage-supervision-and-delius/src/test/kotlin/uk/gov/justice/digital/hmpps/service/SentenceServiceTest.kt +++ b/projects/manage-supervision-and-delius/src/test/kotlin/uk/gov/justice/digital/hmpps/service/SentenceServiceTest.kt @@ -6,14 +6,14 @@ import org.junit.jupiter.api.extension.ExtendWith import org.mockito.InjectMocks import org.mockito.Mock import org.mockito.junit.jupiter.MockitoExtension -import org.mockito.kotlin.times -import org.mockito.kotlin.verify -import org.mockito.kotlin.verifyNoMoreInteractions -import org.mockito.kotlin.whenever -import uk.gov.justice.digital.hmpps.api.model.sentence.MainOffence -import uk.gov.justice.digital.hmpps.api.model.sentence.Offence -import uk.gov.justice.digital.hmpps.api.model.sentence.SentenceOverview +import org.mockito.kotlin.* +import uk.gov.justice.digital.hmpps.api.model.sentence.* +import uk.gov.justice.digital.hmpps.data.generator.AdditionalSentenceGenerator +import uk.gov.justice.digital.hmpps.data.generator.CourtAppearanceGenerator +import uk.gov.justice.digital.hmpps.data.generator.CourtGenerator import uk.gov.justice.digital.hmpps.data.generator.PersonGenerator +import uk.gov.justice.digital.hmpps.integrations.delius.sentence.entity.AdditionalSentenceRepository +import uk.gov.justice.digital.hmpps.integrations.delius.sentence.entity.CourtAppearanceRepository import uk.gov.justice.digital.hmpps.integrations.delius.sentence.entity.EventSentenceRepository import java.time.LocalDate @@ -23,6 +23,12 @@ class SentenceServiceTest { @Mock lateinit var eventRepository: EventSentenceRepository + @Mock + lateinit var courtAppearanceRepository: CourtAppearanceRepository + + @Mock + lateinit var additionalSentenceRepository: AdditionalSentenceRepository + @InjectMocks lateinit var service: SentenceService @@ -30,67 +36,79 @@ class SentenceServiceTest { fun `no active sentences`() { whenever(eventRepository.findActiveSentencesByCrn(PersonGenerator.OVERVIEW.crn)).thenReturn( - listOf(PersonGenerator.INACTIVE_EVENT_1) + listOf() ) val response = service.getMostRecentActiveEvent(PersonGenerator.OVERVIEW.crn) - assertEquals(SentenceOverview(listOf(null)), response) + assertEquals(SentenceOverview(listOf()), response) verify(eventRepository, times(1)).findActiveSentencesByCrn(PersonGenerator.OVERVIEW.crn) verifyNoMoreInteractions(eventRepository) + verifyNoInteractions(courtAppearanceRepository) + verifyNoInteractions(additionalSentenceRepository) } @Test fun `recent active sentences`() { + val event = PersonGenerator.generateEvent( + person = PersonGenerator.OVERVIEW, + active = true, + inBreach = true, + disposal = PersonGenerator.ACTIVE_ORDER, + eventNumber = "123457", + mainOffence = PersonGenerator.MAIN_OFFENCE_1, + notes = "overview", + additionalOffences = listOf(PersonGenerator.ADDITIONAL_OFFENCE_1) + ) + whenever(eventRepository.findActiveSentencesByCrn(PersonGenerator.OVERVIEW.crn)).thenReturn( listOf( - PersonGenerator.generateEvent( - person = PersonGenerator.OVERVIEW, - active = true, - inBreach = true, - disposal = PersonGenerator.ACTIVE_ORDER, - eventNumber = "123457", - mainOffence = PersonGenerator.MAIN_OFFENCE_1, - notes = "overview", - additionalOffences = listOf(PersonGenerator.ADDITIONAL_OFFENCE_1) - ), - PersonGenerator.generateEvent( - person = PersonGenerator.OVERVIEW, - active = true, - inBreach = true, - disposal = PersonGenerator.ACTIVE_ORDER, - eventNumber = "123456", - mainOffence = PersonGenerator.MAIN_OFFENCE_2, - notes = "overview", - additionalOffences = emptyList() - ) + event ) ) + whenever( + courtAppearanceRepository + .getFirstCourtAppearanceByEventIdOrderByDate(event.id) + ) + .thenReturn(CourtAppearanceGenerator.generate(CourtGenerator.DEFAULT)) + + whenever(additionalSentenceRepository.getAllByEventId(event.id)) + .thenReturn(listOf(AdditionalSentenceGenerator.SENTENCE_DISQ, AdditionalSentenceGenerator.SENTENCE_FINE)) + val response = service.getMostRecentActiveEvent(PersonGenerator.OVERVIEW.crn) val expected = SentenceOverview( listOf( - MainOffence( - Offence("Murder", 1), - LocalDate.now(), - "overview", - listOf(Offence("Burglary", 1)) - ), - MainOffence( - Offence("Another Murder", 1), - LocalDate.now(), - "overview", - emptyList() + Sentence( + OffenceDetails( + Offence("Murder", 1), + LocalDate.now(), + "overview", + listOf( + Offence("Burglary", 1) + ) + ), + Conviction( + "Hull Court", + null, + null, + listOf( + AdditionalSentence(3, null, null, "Disqualified from Driving"), + AdditionalSentence(null, 500, "fine notes", "Fine") + ) + ) ) ) ) assertEquals(expected, response) verify(eventRepository, times(1)).findActiveSentencesByCrn(PersonGenerator.OVERVIEW.crn) + verify(additionalSentenceRepository, times(1)).getAllByEventId(event.id) verifyNoMoreInteractions(eventRepository) + verifyNoMoreInteractions(additionalSentenceRepository) } } \ No newline at end of file