From 6395bd4c9bfb2320011083d03671aab3eb2c29db Mon Sep 17 00:00:00 2001 From: Amardeep Chimber Date: Thu, 28 Mar 2024 11:10:39 +0000 Subject: [PATCH] PI-1939 - add rar details per requirement --- .../digital/hmpps/SentenceIntegrationTest.kt | 3 +- .../hmpps/api/model/sentence/Requirement.kt | 5 +- .../hmpps/api/model/sentence/Sentence.kt | 2 +- .../delius/overview/entity/Requirement.kt | 26 +++++++++- .../digital/hmpps/service/SentenceService.kt | 14 ++++-- .../hmpps/service/SentenceServiceTest.kt | 50 +++++++++++++++---- 6 files changed, 82 insertions(+), 18 deletions(-) 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 93e2757b9f..f8494b14c4 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 @@ -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 @@ -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( diff --git a/projects/manage-supervision-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/api/model/sentence/Requirement.kt b/projects/manage-supervision-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/api/model/sentence/Requirement.kt index 67bcb941af..8c55e0ba29 100644 --- a/projects/manage-supervision-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/api/model/sentence/Requirement.kt +++ b/projects/manage-supervision-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/api/model/sentence/Requirement.kt @@ -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 ) 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 index 7c8fd91d16..794fbf22dd 100644 --- 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 @@ -6,5 +6,5 @@ data class Sentence( val offenceDetails: OffenceDetails, val conviction: Conviction? = null, val order: Order? = null, - val requirements: List = listOf() + val requirements: List = listOf(), ) \ No newline at end of file diff --git a/projects/manage-supervision-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/integrations/delius/overview/entity/Requirement.kt b/projects/manage-supervision-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/integrations/delius/overview/entity/Requirement.kt index ce95e2eaf7..36dfea5e0f 100644 --- a/projects/manage-supervision-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/integrations/delius/overview/entity/Requirement.kt +++ b/projects/manage-supervision-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/integrations/delius/overview/entity/Requirement.kt @@ -44,6 +44,7 @@ interface RarDays { } interface RequirementDetails { + val id: Long val description: String? val codeDescription: String? val length: Long? @@ -77,7 +78,30 @@ interface RequirementRepository : JpaRepository { @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 + + @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 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 50fd47f0d6..7b33d6f779 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 @@ -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 = @@ -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) 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 5f6a5242ff..693a3280e5 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 @@ -75,6 +75,7 @@ class SentenceServiceTest { verifyNoMoreInteractions(personRepository) verifyNoInteractions(courtAppearanceRepository) verifyNoInteractions(additionalSentenceRepository) + verifyNoInteractions(requirementRepository) } @Test @@ -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( @@ -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))) ) ) ) @@ -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 + + } } \ No newline at end of file