From 204286877aa934e8599fb21d48941fa3629a951e Mon Sep 17 00:00:00 2001 From: achimber-moj <161360519+achimber-moj@users.noreply.github.com> Date: Mon, 11 Nov 2024 09:02:45 +0000 Subject: [PATCH] MAN-156 - add release date to response (#4415) * MAN-156 - add release date to response * MAN-156 - add release date to response * Formatting changes --------- Co-authored-by: probation-integration-bot[bot] <177347787+probation-integration-bot[bot]@users.noreply.github.com> --- .../justice/digital/hmpps/data/DataLoader.kt | 4 ++ .../hmpps/data/generator/CustodyGenerator.kt | 34 +++++++++++ .../digital/hmpps/SentenceIntegrationTest.kt | 9 ++- .../digital/hmpps/api/model/overview/Order.kt | 1 + .../delius/sentence/entity/Custody.kt | 58 +++++++++++++++++++ .../digital/hmpps/service/SentenceService.kt | 17 ++++-- .../hmpps/service/SentenceServiceTest.kt | 5 +- 7 files changed, 122 insertions(+), 6 deletions(-) create mode 100644 projects/manage-supervision-and-delius/src/dev/kotlin/uk/gov/justice/digital/hmpps/data/generator/CustodyGenerator.kt create mode 100644 projects/manage-supervision-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/integrations/delius/sentence/entity/Custody.kt 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 294654bd71..596ae52c9f 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 @@ -169,6 +169,10 @@ class DataLoader( UnpaidWorkApptGenerator.UNPAID_WORK_DETAILS_1, UnpaidWorkApptGenerator.APPT1, UnpaidWorkApptGenerator.APPT2, + CustodyGenerator.CUSTODY_1, + CustodyGenerator.RELEASE_1, + CustodyGenerator.RELEASE_2, + CustodyGenerator.RELEASE_3 ) personalDetailsData() diff --git a/projects/manage-supervision-and-delius/src/dev/kotlin/uk/gov/justice/digital/hmpps/data/generator/CustodyGenerator.kt b/projects/manage-supervision-and-delius/src/dev/kotlin/uk/gov/justice/digital/hmpps/data/generator/CustodyGenerator.kt new file mode 100644 index 0000000000..05f11fed15 --- /dev/null +++ b/projects/manage-supervision-and-delius/src/dev/kotlin/uk/gov/justice/digital/hmpps/data/generator/CustodyGenerator.kt @@ -0,0 +1,34 @@ +package uk.gov.justice.digital.hmpps.data.generator + +import uk.gov.justice.digital.hmpps.data.generator.PersonGenerator.ACTIVE_ORDER +import uk.gov.justice.digital.hmpps.integrations.delius.sentence.entity.Custody +import uk.gov.justice.digital.hmpps.integrations.delius.sentence.entity.Release +import java.time.ZonedDateTime + +object CustodyGenerator { + val CUSTODY_1 = Custody(IdGenerator.getAndIncrement(), ACTIVE_ORDER.id, ACTIVE_ORDER, listOf(), false) + + val RELEASE_1 = Release( + IdGenerator.getAndIncrement(), + CUSTODY_1, + ZonedDateTime.now().minusDays(21), + ZonedDateTime.now().minusDays(28), + false + ) + + val RELEASE_2 = Release( + IdGenerator.getAndIncrement(), + CUSTODY_1, + ZonedDateTime.now().minusDays(14), + ZonedDateTime.now().minusDays(21), + false + ) + + val RELEASE_3 = Release( + IdGenerator.getAndIncrement(), + CUSTODY_1, + ZonedDateTime.now().minusDays(7), + ZonedDateTime.now().minusDays(14), + false + ) +} \ No newline at end of file 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 7e5ec629c7..cc6987f1e5 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 @@ -13,6 +13,7 @@ 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.CourtReportGenerator.COURT_DOCUMENT import uk.gov.justice.digital.hmpps.data.generator.CourtReportGenerator.EVENT_DOCUMENT +import uk.gov.justice.digital.hmpps.data.generator.CustodyGenerator.RELEASE_3 import uk.gov.justice.digital.hmpps.data.generator.LicenceConditionGenerator import uk.gov.justice.digital.hmpps.data.generator.LicenceConditionGenerator.LC_WITHOUT_NOTES import uk.gov.justice.digital.hmpps.data.generator.LicenceConditionGenerator.LC_WITH_1500_CHAR_NOTE @@ -110,7 +111,13 @@ class SentenceIntegrationTest { LocalDate.now(), listOf(AdditionalSentence(3, null, null, "Disqualified from Driving")) ), - Order("Default Sentence Type", 12, null, LocalDate.now().minusDays(14)), + Order( + "Default Sentence Type", + 12, + null, + releaseDate = RELEASE_3.date.toLocalDate(), + startDate = LocalDate.now().minusDays(14) + ), listOf( Requirement( "F", diff --git a/projects/manage-supervision-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/api/model/overview/Order.kt b/projects/manage-supervision-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/api/model/overview/Order.kt index 2029ed9de3..1c702c2b59 100644 --- a/projects/manage-supervision-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/api/model/overview/Order.kt +++ b/projects/manage-supervision-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/api/model/overview/Order.kt @@ -6,6 +6,7 @@ data class Order( val description: String, val length: Long?, val endDate: LocalDate?, + val releaseDate: LocalDate? = null, val startDate: LocalDate, val status: String? = null, val mainOffence: String? = null, diff --git a/projects/manage-supervision-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/integrations/delius/sentence/entity/Custody.kt b/projects/manage-supervision-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/integrations/delius/sentence/entity/Custody.kt new file mode 100644 index 0000000000..7d85ad6981 --- /dev/null +++ b/projects/manage-supervision-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/integrations/delius/sentence/entity/Custody.kt @@ -0,0 +1,58 @@ +package uk.gov.justice.digital.hmpps.integrations.delius.sentence.entity + +import jakarta.persistence.* +import org.hibernate.annotations.Immutable +import org.hibernate.annotations.SQLRestriction +import org.springframework.data.jpa.repository.JpaRepository +import uk.gov.justice.digital.hmpps.integrations.delius.overview.entity.Disposal +import java.time.ZonedDateTime + +@Entity +@Immutable +@SQLRestriction("soft_deleted = 0") +class Custody( + @Id + @Column(name = "custody_id") + val id: Long, + + @Column(name = "disposal_id") + val disposalId: Long, + + @OneToOne + @JoinColumn(name = "disposal_id", updatable = false, insertable = false) + val disposal: Disposal, + + @OneToMany(mappedBy = "custody") + val releases: List = listOf(), + + @Column(columnDefinition = "number") + val softDeleted: Boolean +) { + fun mostRecentRelease() = releases.maxWithOrNull(compareBy({ it.date }, { it.createdDateTime })) +} + +interface CustodyRepository : JpaRepository { + fun findAllByDisposalId(id: Long): List +} + +@Immutable +@Entity +@SQLRestriction("soft_deleted = 0") +class Release( + @Id + @Column(name = "release_id") + val id: Long, + + @ManyToOne + @JoinColumn(name = "custody_id") + val custody: Custody, + + @Column(name = "actual_release_date") + val date: ZonedDateTime, + + @Column(name = "created_datetime") + val createdDateTime: ZonedDateTime, + + @Column(columnDefinition = "number") + val softDeleted: Boolean +) 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 6c897e8567..b0682efe01 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 @@ -25,7 +25,8 @@ class SentenceService( private val documentRepository: DocumentRepository, private val offenderManagerRepository: OffenderManagerRepository, private val upwAppointmentRepository: UpwAppointmentRepository, - private val licenceConditionRepository: LicenceConditionRepository + private val licenceConditionRepository: LicenceConditionRepository, + private val custodyRepository: CustodyRepository, ) { fun getEvents(crn: String, eventNumber: String?): SentenceOverview { val person = personRepository.getPerson(crn) @@ -90,7 +91,7 @@ class SentenceService( requirements = requirementRepository.getRequirements(id, eventNumber) .map { it.toRequirement() }, courtDocuments = documentRepository.getCourtDocuments(id, eventNumber).map { it.toCourtDocument() }, - disposal?.id?.let { getUnpaidWorkTime(it) }, + unpaidWorkProgress = disposal?.id?.let { getUnpaidWorkTime(it) }, licenceConditions = disposal?.let { licenceConditionRepository.findAllByDisposalId(disposal.id).map { it.toLicenceCondition() @@ -102,8 +103,16 @@ class SentenceService( fun ExtraSentence.toAdditionalSentence(): AdditionalSentence = AdditionalSentence(length, amount, notes, type.description) - fun Disposal.toOrder() = - Order(description = type.description, length = length, startDate = date, endDate = expectedEndDate()) + fun Disposal.toOrder(): Order { + val sentence = custodyRepository.findAllByDisposalId(id).firstOrNull() + return Order( + description = type.description, + length = length, + startDate = date, + endDate = expectedEndDate(), + releaseDate = sentence?.mostRecentRelease()?.date?.toLocalDate() + ) + } fun RequirementDetails.toRequirement(): Requirement { val rar = getRar(id, code) 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 bb423c5d93..41899a741f 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 @@ -52,6 +52,9 @@ class SentenceServiceTest { @Mock lateinit var licenceConditionRepository: LicenceConditionRepository + @Mock + lateinit var custodyRepository: CustodyRepository + @InjectMocks lateinit var service: SentenceService @@ -220,7 +223,7 @@ class SentenceServiceTest { AdditionalSentence(null, 500, "fine notes", "Fine") ) ), - Order("Default Sentence Type", 12, null, LocalDate.now().minusDays(14)), + Order("Default Sentence Type", 12, null, startDate = LocalDate.now().minusDays(14)), listOf( Requirement( requirement1._code,