Skip to content

Commit

Permalink
PI-1939 - add rar details per requirement
Browse files Browse the repository at this point in the history
  • Loading branch information
achimber-moj committed Mar 28, 2024
1 parent 9b6be82 commit 6395bd4
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import org.springframework.test.web.servlet.request.MockMvcRequestBuilders
import org.springframework.test.web.servlet.result.MockMvcResultMatchers
import uk.gov.justice.digital.hmpps.api.model.Name
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.data.generator.PersonGenerator
import uk.gov.justice.digital.hmpps.test.MockMvcExtensions.contentAsJson
Expand Down Expand Up @@ -64,7 +65,7 @@ class SentenceIntegrationTest {
listOf(AdditionalSentence(3, null, null, "Disqualified from Driving"))
),
Order("Default Sentence Type", 12, null, LocalDate.now().minusDays(14)),
listOf(Requirement("Main", "High Intensity", 12, "my notes"))
listOf(Requirement("Main", "High Intensity", 12, "my notes", Rar(1,0, 1)))
),
Sentence(
OffenceDetails(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package uk.gov.justice.digital.hmpps.api.model.sentence

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

data class Requirement(
val description: String?,
val codeDescription: String?,
val length: Long?,
val notes: String?
val notes: String?,
val rar: Rar? = null
)
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ data class Sentence(
val offenceDetails: OffenceDetails,
val conviction: Conviction? = null,
val order: Order? = null,
val requirements: List<Requirement> = listOf()
val requirements: List<Requirement> = listOf(),
)
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ interface RarDays {
}

interface RequirementDetails {
val id: Long
val description: String?
val codeDescription: String?
val length: Long?
Expand Down Expand Up @@ -77,7 +78,30 @@ interface RequirementRepository : JpaRepository<Requirement, Long> {

@Query(
"""
SELECT r."LENGTH", rrtmc.description, rsrl.code_description AS codeDescription, r.rqmnt_notes AS notes
select count(r.rqmnt_id) as days, 'SCHEDULED' as type from contact c
join rqmnt r on r.rqmnt_id = c.rqmnt_id
join r_rqmnt_type_main_category mc on r.rqmnt_type_main_category_id = mc.rqmnt_type_main_category_id
where c.rar_activity = 'Y' and c.soft_deleted = 0
and (c.attended is null)
and (c.complied is null or c.complied = 'Y')
and mc.code = 'F' and r.active_flag = 1 and r.soft_deleted = 0
and r.disposal_id = :requirementId
union
select count(r.rqmnt_id) as days, 'COMPLETED' as type from contact c
join rqmnt r on r.rqmnt_id = c.rqmnt_id
join r_rqmnt_type_main_category mc on r.rqmnt_type_main_category_id = mc.rqmnt_type_main_category_id
where c.rar_activity = 'Y' and c.soft_deleted = 0
and (c.attended = 'Y')
and (c.complied is null or c.complied = 'Y')
and mc.code = 'F' and r.active_flag = 1 and r.soft_deleted = 0
and r.rqmnt_id = :requirementId
""", nativeQuery = true
)
fun getRarDaysByRequirementId(requirementId: Long): List<RarDays>

@Query(
"""
SELECT r.rqmnt_id as id, r."LENGTH", rrtmc.description, rsrl.code_description AS codeDescription, r.rqmnt_notes AS notes
FROM rqmnt r
JOIN r_rqmnt_type_main_category rrtmc
ON r.rqmnt_type_main_category_id = rrtmc.rqmnt_type_main_category_id
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class SentenceService(
additionalSentences.map { it.toAdditionalSentence() }
),
order = disposal?.toOrder(),
requirements = disposal.let { requirementRepository.getRequirements(crn, eventNumber).map { it.toRequirement() } }
requirements = disposal.let { requirementRepository.getRequirements(crn, eventNumber).map { it.toRequirement() } },
)

fun ExtraSentence.toAdditionalSentence(): AdditionalSentence =
Expand All @@ -63,10 +63,16 @@ class SentenceService(

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

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

private fun getRar(disposalId: Long): Rar {
val rarDays = requirementRepository.getRarDays(disposalId)
private fun getRar(requirementId: Long): Rar {
val rarDays = requirementRepository.getRarDaysByRequirementId(requirementId)
val scheduledDays = rarDays.find { it.type == "SCHEDULED" }?.days ?: 0
val completedDays = rarDays.find { it.type == "COMPLETED" }?.days ?: 0
return Rar(completed = completedDays, scheduled = scheduledDays)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ class SentenceServiceTest {
verifyNoMoreInteractions(personRepository)
verifyNoInteractions(courtAppearanceRepository)
verifyNoInteractions(additionalSentenceRepository)
verifyNoInteractions(requirementRepository)
}

@Test
Expand All @@ -91,22 +92,27 @@ class SentenceServiceTest {
additionalOffences = listOf(PersonGenerator.ADDITIONAL_OFFENCE_1)
)

val requirement = RequirementDetails(1, "Main", "High Intensity", 12, "new requirement")

val completedRarDays = OverviewServiceTest.RarDays(1, "COMPLETED")

val scheduledRarDays = OverviewServiceTest.RarDays(2, "SCHEDULED")

whenever(personRepository.findSummary(PersonGenerator.OVERVIEW.crn)).thenReturn(personSummary)
whenever(eventRepository.findActiveSentencesByPersonId(personSummary.id)).thenReturn(
listOf(
event
)
)

whenever(
courtAppearanceRepository
.getFirstCourtAppearanceByEventIdOrderByDate(event.id)
)
whenever(eventRepository.findActiveSentencesByPersonId(personSummary.id)).thenReturn(listOf(event))

whenever(courtAppearanceRepository.getFirstCourtAppearanceByEventIdOrderByDate(event.id))
.thenReturn(CourtAppearanceGenerator.generate(CourtGenerator.DEFAULT))

whenever(additionalSentenceRepository.getAllByEventId(event.id))
.thenReturn(listOf(AdditionalSentenceGenerator.SENTENCE_DISQ, AdditionalSentenceGenerator.SENTENCE_FINE))

whenever(requirementRepository.getRequirements(PersonGenerator.OVERVIEW.crn, event.eventNumber))
.thenReturn(listOf(requirement))

whenever(requirementRepository.getRarDaysByRequirementId(requirement._id)).thenReturn(listOf(completedRarDays, scheduledRarDays))

val response = service.getMostRecentActiveEvent(PersonGenerator.OVERVIEW.crn)

val expected = SentenceOverview(
Expand All @@ -132,7 +138,7 @@ class SentenceServiceTest {
)
),
Order("Default Sentence Type", 12, null, LocalDate.now().minusDays(14)),
listOf()
listOf(Requirement(requirement._description, requirement._codeDescription, requirement._length, requirement._notes, Rar(completedRarDays._days, scheduledRarDays._days, 3)))
)
)
)
Expand All @@ -146,4 +152,28 @@ class SentenceServiceTest {
verifyNoMoreInteractions(additionalSentenceRepository)
verifyNoMoreInteractions(courtAppearanceRepository)
}

data class RequirementDetails(
val _id: Long,
val _description: String?,
val _codeDescription: String?,
val _length: Long?,
val _notes: String?
) : uk.gov.justice.digital.hmpps.integrations.delius.overview.entity.RequirementDetails {
override val id: Long
get() = _id

override val description: String?
get() = _description

override val codeDescription: String?
get() = _codeDescription

override val length: Long?
get() = _length

override val notes: String?
get() = _notes

}
}

0 comments on commit 6395bd4

Please sign in to comment.