Skip to content

Commit

Permalink
PI-1939 - add additional sentences
Browse files Browse the repository at this point in the history
  • Loading branch information
achimber-moj committed Mar 18, 2024
1 parent 47898a5 commit cb43430
Show file tree
Hide file tree
Showing 8 changed files with 112 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
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 DISQ = generateReferenceDate("DISQ", "Disqualified from Driving")
val FINE = generateReferenceDate("FINE", "Fine")

val SENTENCE_DISQ = generateSentence(length = 3, referenceData = DISQ)
val SENTENCE_FINE = generateSentence(amount = 500, referenceData = FINE)
fun generateSentence(
length: Long? = null,
amount: Long? = null,
notes: String? = null,
event: Event? = null,
referenceData: ReferenceData
) = AdditionalSentence(
IdGenerator.getAndIncrement(),
length,
amount,
notes,
false,
event,
referenceData
)

fun generateReferenceDate(code: String, description: String) =
ReferenceData(IdGenerator.getAndIncrement(), code, description)
}

Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ class SentenceIntegrationTest {
Offence("Assault", 1)
)
),
Conviction("Hull Court", "Birmingham Court", LocalDate.now())
Conviction("Hull Court", "Birmingham Court", LocalDate.now()),
listOf()
),
Sentence(
OffenceDetails(
Expand All @@ -50,7 +51,8 @@ class SentenceIntegrationTest {
"overview",
emptyList()
),
Conviction(null, null, null)
Conviction(null, null, null),
listOf()
)
)
)
Expand Down
Original file line number Diff line number Diff line change
@@ -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,
)
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@ package uk.gov.justice.digital.hmpps.api.model.sentence

data class Sentence(
val offence: OffenceDetails,
val conviction: Conviction? = null
val conviction: Conviction? = null,
val additionalSentences: List<AdditionalSentence>
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
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,

@Lob
@Column(name = "notes")
val notes: String? = null,

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

@ManyToOne
@JoinColumn(name = "event_id", nullable = false)
val event: Event? = null,

@ManyToOne
@JoinColumn(name = "additional_sentence_type_id")
val refData: ReferenceData? = null,

)
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,7 @@ interface EventSentenceRepository : JpaRepository<Event, Long> {
interface CourtAppearanceRepository: JpaRepository<CourtAppearance, Long> {
fun getFirstCourtAppearanceByEventIdOrderByDate(id: Long): CourtAppearance?
}

interface AdditionalSentenceRepository: JpaRepository<AdditionalSentence, Long> {
fun getAllById(id: Long): List<AdditionalSentence>
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,29 @@ package uk.gov.justice.digital.hmpps.service
import org.springframework.stereotype.Service
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 courtApperanceRepository: CourtAppearanceRepository
private val courtApperanceRepository: CourtAppearanceRepository,
private val additionalSentenceRepository: AdditionalSentenceRepository
) {
fun getMostRecentActiveEvent(crn: String): SentenceOverview {
val events = eventRepository.findActiveSentencesByCrn(crn)
return SentenceOverview(events.map {
val courtApperance = courtApperanceRepository.getFirstCourtAppearanceByEventIdOrderByDate(it.id)
it.toSentence(courtApperance)
val courtAppearance = courtApperanceRepository.getFirstCourtAppearanceByEventIdOrderByDate(it.id)
val additionalSentences = additionalSentenceRepository.getAllById(it.id)
it.toSentence(courtAppearance, additionalSentences)
})
}

fun Event.toSentence(courtAppearance: CourtAppearance?) = mainOffence?.let {
fun Event.toSentence(courtAppearance: CourtAppearance?, additionalSentences: List<ExtraSentence>) = mainOffence?.let {
mainOffence ->
Sentence(
(OffenceDetails(offence = Offence(mainOffence.offence.description, mainOffence.offenceCount),
Expand All @@ -31,8 +36,13 @@ class SentenceService(
}
)
),
Conviction(sentencingCourt = courtAppearance?.court?.name, responsibleCourt = court?.name, convictionDate = convictionDate)
Conviction(sentencingCourt = courtAppearance?.court?.name, responsibleCourt = court?.name, convictionDate = convictionDate),
additionalSentences.map { it.toAdditionalSentence() }
)
}


fun ExtraSentence.toAdditionalSentence(): AdditionalSentence =
AdditionalSentence(length, amount, notes, refData!!.description)

}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import uk.gov.justice.digital.hmpps.api.model.sentence.*
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
Expand All @@ -24,6 +25,9 @@ class SentenceServiceTest {
@Mock
lateinit var courtAppearanceRepository: CourtAppearanceRepository

@Mock
lateinit var additionalSentenceRepository: AdditionalSentenceRepository

@InjectMocks
lateinit var service: SentenceService

Expand Down Expand Up @@ -83,7 +87,8 @@ class SentenceServiceTest {
Conviction("Hull Court",
null,
null
)
),
listOf()
)
)
)
Expand Down

0 comments on commit cb43430

Please sign in to comment.