Skip to content

Commit

Permalink
MAN-156 - add release date to response (#4415)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
1 parent 7c39355 commit 2042868
Show file tree
Hide file tree
Showing 7 changed files with 122 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
@@ -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
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
@@ -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<Release> = listOf(),

@Column(columnDefinition = "number")
val softDeleted: Boolean
) {
fun mostRecentRelease() = releases.maxWithOrNull(compareBy({ it.date }, { it.createdDateTime }))
}

interface CustodyRepository : JpaRepository<Custody, Long> {
fun findAllByDisposalId(id: Long): List<Custody>
}

@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
)
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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()
Expand All @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ class SentenceServiceTest {
@Mock
lateinit var licenceConditionRepository: LicenceConditionRepository

@Mock
lateinit var custodyRepository: CustodyRepository

@InjectMocks
lateinit var service: SentenceService

Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 2042868

Please sign in to comment.