Skip to content

Commit

Permalink
PI-1939 - refactor requirement section
Browse files Browse the repository at this point in the history
  • Loading branch information
achimber-moj committed Mar 27, 2024
1 parent 8783a45 commit f7daad0
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -238,11 +238,14 @@ object PersonGenerator {

fun generateRequirement(
disposal: Disposal,
length: Long? = null,
notes: String? = null,
subCategoryId: String? = null,
mainCategory: RequirementMainCategory = MAIN_CAT_F,
active: Boolean = true,
softDeleted: Boolean = false,
id: Long = IdGenerator.getAndIncrement()
) = Requirement(id, disposal, mainCategory, active, softDeleted)
) = Requirement(id, length, notes, subCategoryId, disposal, mainCategory, active, softDeleted)

fun generateDisposalType(
code: String,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package uk.gov.justice.digital.hmpps.api.model.sentence

data class Requirement(
val description: String,
val length: Long,
val progress: Long,
val notes: String
val description: String?,
val codeDescription: String?,
val length: Long?,
val notes: String?
)
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
package uk.gov.justice.digital.hmpps.api.model.sentence

import uk.gov.justice.digital.hmpps.api.model.overview.Order
import uk.gov.justice.digital.hmpps.api.model.overview.Rar

data class Sentence(
val offenceDetails: OffenceDetails,
val conviction: Conviction? = null,
val order: Order? = null,
val requirements: List<Requirement>
val requirements: List<Requirement> = listOf()
)
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,14 @@ class Requirement(
@Column(name = "rqmnt_id", nullable = false)
val id: Long,

val length: Long?,

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

@Column(name = "rqmnt_type_sub_category_id")
val subCategoryId: String?,

@ManyToOne
@JoinColumn(name = "disposal_id")
val disposal: Disposal? = null,
Expand All @@ -35,6 +43,13 @@ interface RarDays {
val type: String
}

interface RequirementDetails {
val description: String?
val codeDescription: String?
val length: Long?
val notes: String?
}

interface RequirementRepository : JpaRepository<Requirement, Long> {

@Query(
Expand All @@ -59,6 +74,26 @@ interface RequirementRepository : JpaRepository<Requirement, Long> {
""", nativeQuery = true
)
fun getRarDays(disposalId: Long): List<RarDays>

@Query(
"""
SELECT r."LENGTH", rrtmc.DESCRIPTION, rsrl.CODE_DESCRIPTION, r.RQMNT_NOTES
FROM rqmnt r
JOIN R_RQMNT_TYPE_MAIN_CATEGORY rrtmc
ON r.RQMNT_TYPE_MAIN_CATEGORY_ID = RRTMC.RQMNT_TYPE_MAIN_CATEGORY_ID
JOIN R_STANDARD_REFERENCE_LIST rsrl
ON RSRL.STANDARD_REFERENCE_LIST_ID = r.RQMNT_TYPE_SUB_CATEGORY_ID
JOIN DISPOSAL d
ON r.DISPOSAL_ID = d.DISPOSAL_ID
JOIN EVENT e
ON e.EVENT_ID = d.EVENT_ID
JOIN OFFENDER o
ON o.OFFENDER_ID = e.OFFENDER_ID
AND o.CRN = :crn
AND e.EVENT_NUMBER = :eventNumber
""", nativeQuery = true
)
fun getRequirements(crn: String, eventNumber: String) : List<RequirementDetails>
}

@Immutable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ import uk.gov.justice.digital.hmpps.api.model.overview.Order
import uk.gov.justice.digital.hmpps.api.model.overview.Rar
import uk.gov.justice.digital.hmpps.api.model.sentence.*
import uk.gov.justice.digital.hmpps.api.model.sentence.Offence
import uk.gov.justice.digital.hmpps.api.model.sentence.Requirement
import uk.gov.justice.digital.hmpps.integrations.delius.overview.entity.*
import uk.gov.justice.digital.hmpps.integrations.delius.overview.entity.RequirementDetails
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
Expand All @@ -29,11 +31,11 @@ class SentenceService(
sentences = events.map {
val courtAppearance = courtAppearanceRepository.getFirstCourtAppearanceByEventIdOrderByDate(it.id)
val additionalSentences = additionalSentenceRepository.getAllByEventId(it.id)
it.toSentence(courtAppearance, additionalSentences)
it.toSentence(courtAppearance, additionalSentences, crn)
})
}

fun Event.toSentence(courtAppearance: CourtAppearance?, additionalSentences: List<ExtraSentence>) = Sentence(
fun Event.toSentence(courtAppearance: CourtAppearance?, additionalSentences: List<ExtraSentence>, crn: String) = Sentence(
OffenceDetails(
eventNumber = eventNumber,
offence = mainOffence?.let { Offence(it.offence.description, it.offenceCount) },
Expand All @@ -50,7 +52,7 @@ class SentenceService(
additionalSentences.map { it.toAdditionalSentence() }
),
order = disposal?.toOrder(),
listOf()
requirements = disposal.let { requirementRepository.getRequirements(crn, eventNumber).map { it.toRequirement() } }
)

fun ExtraSentence.toAdditionalSentence(): AdditionalSentence =
Expand All @@ -61,6 +63,8 @@ class SentenceService(

fun Disposal.toOrder() = Order(description = type.description, length = length, startDate = date, endDate = expectedEndDate())

fun RequirementDetails.toRequirement() = Requirement(description, codeDescription, length, notes)

private fun getRar(disposalId: Long): Rar {
val rarDays = requirementRepository.getRarDays(disposalId)
val scheduledDays = rarDays.find { it.type == "SCHEDULED" }?.days ?: 0
Expand Down

0 comments on commit f7daad0

Please sign in to comment.