From 2dd1f93f36406441917e22fcbd98a9104e0d464f Mon Sep 17 00:00:00 2001 From: achimber-moj <161360519+achimber-moj@users.noreply.github.com> Date: Tue, 9 Apr 2024 17:03:04 +0100 Subject: [PATCH] Feature/pi 1939 mas probabtion history (#3596) * PI-1939 - add probation history Signed-off-by: Amardeep Chimber <amardeep.chimber@digital.justice.gov.uk> * PI-1939 - add probation history Signed-off-by: Amardeep Chimber <amardeep.chimber@digital.justice.gov.uk> * PI-1939 - add probation history Signed-off-by: Amardeep Chimber <amardeep.chimber@digital.justice.gov.uk> * PI-1939 - refactor variable names Signed-off-by: Amardeep Chimber <amardeep.chimber@digital.justice.gov.uk> * PI-1939 - add professional contact details Signed-off-by: Amardeep Chimber <amardeep.chimber@digital.justice.gov.uk> * PI-1939 - add professional contact details Signed-off-by: Amardeep Chimber <amardeep.chimber@digital.justice.gov.uk> * PI-1939 - add professional contact details Signed-off-by: Amardeep Chimber <amardeep.chimber@digital.justice.gov.uk> * PI-1939 - add professional contact details Signed-off-by: Amardeep Chimber <amardeep.chimber@digital.justice.gov.uk> * PI-1939 - update tests * PI-1939 - use offender_manager instead of perseonal_contact table for probabtion history Signed-off-by: Amardeep Chimber <amardeep.chimber@digital.justice.gov.uk> * Formatting changes * PI-1939 - add most recent termination date from inactive events to probation history Signed-off-by: Amardeep Chimber <amardeep.chimber@digital.justice.gov.uk> --------- Signed-off-by: Amardeep Chimber <amardeep.chimber@digital.justice.gov.uk> Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> --- .../justice/digital/hmpps/data/DataLoader.kt | 19 +++--- .../data/generator/CourtReportGenerator.kt | 64 +++++++++++++++++-- .../generator/OffenderManagerGenerator.kt | 12 ++++ .../hmpps/data/generator/PersonGenerator.kt | 19 +++++- .../personalDetails/PersonDetailsGenerator.kt | 59 ++--------------- .../digital/hmpps/SentenceIntegrationTest.kt | 11 ++-- .../api/controller/SentenceController.kt | 2 +- .../api/model/sentence/ProbationHistory.kt | 10 +++ .../api/model/sentence/SentenceOverview.kt | 1 + .../delius/overview/entity/Event.kt | 3 + .../entity/PersonalContactEntity.kt | 4 +- .../referencedata/entity/ReferenceData.kt | 2 +- .../entity/ReferenceDataLinkedList.kt | 22 +++++++ .../delius/sentence/entity/OffenderManager.kt | 24 +++++++ .../sentence/entity/SentenceRepository.kt | 9 ++- .../digital/hmpps/service/SentenceService.kt | 36 +++++++---- .../hmpps/service/SentenceServiceTest.kt | 47 +++++++------- 17 files changed, 228 insertions(+), 116 deletions(-) create mode 100644 projects/manage-supervision-and-delius/src/dev/kotlin/uk/gov/justice/digital/hmpps/data/generator/OffenderManagerGenerator.kt create mode 100644 projects/manage-supervision-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/api/model/sentence/ProbationHistory.kt create mode 100644 projects/manage-supervision-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/integrations/delius/referencedata/entity/ReferenceDataLinkedList.kt create mode 100644 projects/manage-supervision-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/integrations/delius/sentence/entity/OffenderManager.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 4ebbe0f69..6294cbc24 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 @@ -9,11 +9,7 @@ import org.springframework.stereotype.Component import org.springframework.transaction.annotation.Transactional import uk.gov.justice.digital.hmpps.data.generator.* import uk.gov.justice.digital.hmpps.data.generator.CourtAppearanceGenerator.COURT_APPEARANCE -import uk.gov.justice.digital.hmpps.data.generator.CourtReportGenerator.COURT_REPORT -import uk.gov.justice.digital.hmpps.data.generator.CourtReportGenerator.DEFAULT_TYPE import uk.gov.justice.digital.hmpps.data.generator.personalDetails.PersonDetailsGenerator -import uk.gov.justice.digital.hmpps.data.generator.personalDetails.PersonDetailsGenerator.COURT_DOCUMENT -import uk.gov.justice.digital.hmpps.data.generator.personalDetails.PersonDetailsGenerator.EVENT_DOCUMENT import uk.gov.justice.digital.hmpps.user.AuditUserRepository @Component @@ -71,15 +67,20 @@ class DataLoader( ) ) entityManager.persist(CourtGenerator.DEFAULT) - entityManager.persist(COURT_APPEARANCE) - entityManager.persist(DEFAULT_TYPE) - entityManager.persist(COURT_REPORT) + entityManager.persist(CourtReportGenerator.COURT_APPEARANCE) + entityManager.persist(CourtReportGenerator.DEFAULT_TYPE) + entityManager.persist(CourtReportGenerator.EVENT_DOCUMENT) + entityManager.persist(CourtReportGenerator.COURT_DOCUMENT) + entityManager.persist(CourtReportGenerator.COURT_REPORT) + entityManager.persist(COURT_APPEARANCE) - entityManager.persist(EVENT_DOCUMENT) - entityManager.persist(COURT_DOCUMENT) + entityManager.persist(CourtReportGenerator.DEFAULT_TYPE) + entityManager.persist(CourtReportGenerator.COURT_REPORT) entityManager.persistAll( + OffenderManagerGenerator.OFFENDER_MANAGER_ACTIVE, + OffenderManagerGenerator.OFFENDER_MANAGER_INACTIVE, PersonGenerator.DEFAULT_DISPOSAL_TYPE, PersonGenerator.ACTIVE_ORDER, PersonGenerator.INACTIVE_ORDER_1, diff --git a/projects/manage-supervision-and-delius/src/dev/kotlin/uk/gov/justice/digital/hmpps/data/generator/CourtReportGenerator.kt b/projects/manage-supervision-and-delius/src/dev/kotlin/uk/gov/justice/digital/hmpps/data/generator/CourtReportGenerator.kt index 6f4b39d1a..e5fc94599 100644 --- a/projects/manage-supervision-and-delius/src/dev/kotlin/uk/gov/justice/digital/hmpps/data/generator/CourtReportGenerator.kt +++ b/projects/manage-supervision-and-delius/src/dev/kotlin/uk/gov/justice/digital/hmpps/data/generator/CourtReportGenerator.kt @@ -1,15 +1,67 @@ package uk.gov.justice.digital.hmpps.data.generator -import uk.gov.justice.digital.hmpps.integrations.delius.sentence.entity.CourtAppearance +import uk.gov.justice.digital.hmpps.integrations.delius.personalDetails.entity.CourtReportDocument +import uk.gov.justice.digital.hmpps.integrations.delius.personalDetails.entity.EventDocument import uk.gov.justice.digital.hmpps.integrations.delius.sentence.entity.CourtReport import uk.gov.justice.digital.hmpps.integrations.delius.sentence.entity.CourtReportType +import java.time.ZonedDateTime object CourtReportGenerator { + val COURT_APPEARANCE = CourtAppearanceGenerator.generate() val DEFAULT_TYPE = CourtReportType(IdGenerator.getAndIncrement(), "Pre-Sentence Report - Fast") - val COURT_REPORT = generate(DEFAULT_TYPE, CourtAppearanceGenerator.COURT_APPEARANCE) - fun generate( - courtReportType: CourtReportType?, - courtAppearance: CourtAppearance - ) = CourtReport(IdGenerator.getAndIncrement(), courtReportType, courtAppearance) + val COURT_REPORT_TYPE = DEFAULT_TYPE + val COURT_REPORT = CourtReport(IdGenerator.getAndIncrement(), COURT_REPORT_TYPE, COURT_APPEARANCE) + + val COURT_DOCUMENT = generateCourtDocument( + PersonGenerator.OVERVIEW.id, + "A003", + "court report", + "DOCUMENT", + COURT_REPORT.courtReportId + ) + + val EVENT_DOCUMENT = generateEventDocument( + PersonGenerator.OVERVIEW.id, + "A004", + "event report", + "DOCUMENT", + PersonGenerator.EVENT_1.id + ) + + fun generateCourtDocument( + personId: Long, + alfrescoId: String, + name: String, + documentType: String, + primaryKeyId: Long? = null + ): CourtReportDocument { + val doc = CourtReportDocument() + doc.id = IdGenerator.getAndIncrement() + doc.lastUpdated = ZonedDateTime.now().minusDays(1) + doc.alfrescoId = alfrescoId + doc.name = name + doc.personId = personId + doc.primaryKeyId = primaryKeyId + doc.type = documentType + return doc + } + + fun generateEventDocument( + personId: Long, + alfrescoId: String, + name: String, + documentType: String, + primaryKeyId: Long? = null + ): EventDocument { + val doc = EventDocument() + doc.id = IdGenerator.getAndIncrement() + doc.lastUpdated = ZonedDateTime.now().minusDays(3) + doc.alfrescoId = alfrescoId + doc.name = name + doc.personId = personId + doc.primaryKeyId = primaryKeyId + doc.type = documentType + return doc + } } \ No newline at end of file diff --git a/projects/manage-supervision-and-delius/src/dev/kotlin/uk/gov/justice/digital/hmpps/data/generator/OffenderManagerGenerator.kt b/projects/manage-supervision-and-delius/src/dev/kotlin/uk/gov/justice/digital/hmpps/data/generator/OffenderManagerGenerator.kt new file mode 100644 index 000000000..e6127ae20 --- /dev/null +++ b/projects/manage-supervision-and-delius/src/dev/kotlin/uk/gov/justice/digital/hmpps/data/generator/OffenderManagerGenerator.kt @@ -0,0 +1,12 @@ +package uk.gov.justice.digital.hmpps.data.generator + +import uk.gov.justice.digital.hmpps.integrations.delius.sentence.entity.OffenderManager +import java.time.LocalDate + +object OffenderManagerGenerator { + + val OFFENDER_MANAGER_ACTIVE = + OffenderManager(IdGenerator.getAndIncrement(), PersonGenerator.OVERVIEW, null, 1234, null) + val OFFENDER_MANAGER_INACTIVE = + OffenderManager(IdGenerator.getAndIncrement(), PersonGenerator.OVERVIEW, null, 1234, LocalDate.now()) +} \ No newline at end of file diff --git a/projects/manage-supervision-and-delius/src/dev/kotlin/uk/gov/justice/digital/hmpps/data/generator/PersonGenerator.kt b/projects/manage-supervision-and-delius/src/dev/kotlin/uk/gov/justice/digital/hmpps/data/generator/PersonGenerator.kt index e27023e70..f18db116e 100644 --- a/projects/manage-supervision-and-delius/src/dev/kotlin/uk/gov/justice/digital/hmpps/data/generator/PersonGenerator.kt +++ b/projects/manage-supervision-and-delius/src/dev/kotlin/uk/gov/justice/digital/hmpps/data/generator/PersonGenerator.kt @@ -71,8 +71,8 @@ object PersonGenerator { val DEFAULT_DISPOSAL_TYPE = generateDisposalType("DFS", "Default Sentence Type", "NP", 0) val ACTIVE_ORDER = generateDisposal(EVENT_1, length = 12) - val INACTIVE_ORDER_1 = generateDisposal(INACTIVE_EVENT_1) - val INACTIVE_ORDER_2 = generateDisposal(INACTIVE_EVENT_2) + val INACTIVE_ORDER_1 = generateDisposal(INACTIVE_EVENT_1, LocalDate.of(2023, 4, 8)) + val INACTIVE_ORDER_2 = generateDisposal(INACTIVE_EVENT_2, LocalDate.of(2023, 4, 9)) val ADD_OFF_1 = generateOffence("Burglary", "ADD1") val ADDITIONAL_OFFENCE_1 = generateAdditionalOffence( @@ -284,6 +284,7 @@ object PersonGenerator { fun generateDisposal( event: Event, + terminationDate: LocalDate? = null, date: LocalDate = LocalDate.now().minusDays(14), length: Long? = null, type: DisposalType = DEFAULT_DISPOSAL_TYPE, @@ -292,7 +293,19 @@ object PersonGenerator { active: Boolean = true, softDeleted: Boolean = false, id: Long = IdGenerator.getAndIncrement() - ) = Disposal(event, date, length, type, null, enteredEndDate, notionalEndDate, active, softDeleted, id) + ) = Disposal( + event, + date, + length, + type, + null, + enteredEndDate, + notionalEndDate, + terminationDate, + active, + softDeleted, + id + ) fun generateOffence( description: String, diff --git a/projects/manage-supervision-and-delius/src/dev/kotlin/uk/gov/justice/digital/hmpps/data/generator/personalDetails/PersonDetailsGenerator.kt b/projects/manage-supervision-and-delius/src/dev/kotlin/uk/gov/justice/digital/hmpps/data/generator/personalDetails/PersonDetailsGenerator.kt index 8b826e0df..b53505cdc 100644 --- a/projects/manage-supervision-and-delius/src/dev/kotlin/uk/gov/justice/digital/hmpps/data/generator/personalDetails/PersonDetailsGenerator.kt +++ b/projects/manage-supervision-and-delius/src/dev/kotlin/uk/gov/justice/digital/hmpps/data/generator/personalDetails/PersonDetailsGenerator.kt @@ -5,7 +5,10 @@ import uk.gov.justice.digital.hmpps.data.generator.IdGenerator import uk.gov.justice.digital.hmpps.data.generator.PersonGenerator import uk.gov.justice.digital.hmpps.data.generator.UserGenerator.USER import uk.gov.justice.digital.hmpps.integrations.delius.overview.entity.* -import uk.gov.justice.digital.hmpps.integrations.delius.personalDetails.entity.* +import uk.gov.justice.digital.hmpps.integrations.delius.personalDetails.entity.ContactAddress +import uk.gov.justice.digital.hmpps.integrations.delius.personalDetails.entity.PersonAddress +import uk.gov.justice.digital.hmpps.integrations.delius.personalDetails.entity.PersonDocument +import uk.gov.justice.digital.hmpps.integrations.delius.personalDetails.entity.PersonalContactEntity import uk.gov.justice.digital.hmpps.integrations.delius.referencedata.entity.ReferenceData import java.time.LocalDate import java.time.ZonedDateTime @@ -117,6 +120,7 @@ object PersonDetailsGenerator { ) val RELATIONSHIP_TYPE = ReferenceData(IdGenerator.getAndIncrement(), "FM01", "Family Member") + val CONTACT_ADDRESS = generateContactAddress("31", "Test Steet", "Test town", "Test County", "NE1 56A") val PERSONAL_CONTACT_1 = PersonalContactEntity( IdGenerator.getAndIncrement(), @@ -129,6 +133,7 @@ object PersonDetailsGenerator { "0897676554", LocalDate.now(), LocalDate.now(), + LocalDate.now(), RELATIONSHIP_TYPE, CONTACT_ADDRESS, USER @@ -197,22 +202,6 @@ object PersonDetailsGenerator { val DOCUMENT_1 = generateDocument(PERSONAL_DETAILS.id, "A001", "induction.doc", "DOCUMENT") val DOCUMENT_2 = generateDocument(PERSONAL_DETAILS.id, "A002", "other.doc", "DOCUMENT") - val COURT_DOCUMENT = generateCourtDocument( - PersonGenerator.OVERVIEW.id, - "A003", - "court report", - "DOCUMENT", - COURT_REPORT.courtReportId - ) - - val EVENT_DOCUMENT = generateEventDocument( - PersonGenerator.OVERVIEW.id, - "A004", - "event report", - "DOCUMENT", - PersonGenerator.EVENT_1.id - ) - fun generateContactAddress( addressNumber: String, streetName: String, @@ -320,42 +309,6 @@ object PersonDetailsGenerator { return doc } - fun generateCourtDocument( - personId: Long, - alfrescoId: String, - name: String, - documentType: String, - primaryKeyId: Long? = null - ): CourtReportDocument { - val doc = CourtReportDocument() - doc.id = IdGenerator.getAndIncrement() - doc.lastUpdated = ZonedDateTime.now().minusDays(1) - doc.alfrescoId = alfrescoId - doc.name = name - doc.personId = personId - doc.primaryKeyId = primaryKeyId - doc.type = documentType - return doc - } - - fun generateEventDocument( - personId: Long, - alfrescoId: String, - name: String, - documentType: String, - primaryKeyId: Long? = null - ): EventDocument { - val doc = EventDocument() - doc.id = IdGenerator.getAndIncrement() - doc.lastUpdated = ZonedDateTime.now().minusDays(3) - doc.alfrescoId = alfrescoId - doc.name = name - doc.personId = personId - doc.primaryKeyId = primaryKeyId - doc.type = documentType - return doc - } - fun generateAlias(forename: String, secondName: String, surname: String, personId: Long) = Alias( id = IdGenerator.getAndIncrement(), forename = forename, 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 dc9fb1066..b8a3c91c5 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 @@ -7,14 +7,15 @@ import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMock import org.springframework.boot.test.context.SpringBootTest import org.springframework.test.web.servlet.MockMvc import org.springframework.test.web.servlet.request.MockMvcRequestBuilders +import org.springframework.test.web.servlet.result.MockMvcResultHandlers.print 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.CourtReportGenerator.COURT_DOCUMENT +import uk.gov.justice.digital.hmpps.data.generator.CourtReportGenerator.EVENT_DOCUMENT import uk.gov.justice.digital.hmpps.data.generator.PersonGenerator -import uk.gov.justice.digital.hmpps.data.generator.personalDetails.PersonDetailsGenerator.COURT_DOCUMENT -import uk.gov.justice.digital.hmpps.data.generator.personalDetails.PersonDetailsGenerator.EVENT_DOCUMENT import uk.gov.justice.digital.hmpps.test.MockMvcExtensions.contentAsJson import uk.gov.justice.digital.hmpps.test.MockMvcExtensions.withToken import java.time.LocalDate @@ -33,7 +34,7 @@ class SentenceIntegrationTest { .andReturn().response.contentAsJson<SentenceOverview>() val expected = SentenceOverview( - Name("Caroline", "Louise", "Bloggs"), listOf() + Name("Caroline", "Louise", "Bloggs"), listOf(), ProbationHistory(0, null, 0, 0) ) assertEquals(expected, response) @@ -44,6 +45,7 @@ class SentenceIntegrationTest { val response = mockMvc .perform(MockMvcRequestBuilders.get("/sentence/${PersonGenerator.OVERVIEW.crn}").withToken()) .andExpect(MockMvcResultMatchers.status().isOk) + .andDo(print()) .andReturn().response.contentAsJson<SentenceOverview>() val expected = SentenceOverview( @@ -86,7 +88,8 @@ class SentenceIntegrationTest { listOf(), listOf() ) - ) + ), + ProbationHistory(2, LocalDate.of(2023, 4, 9), 2, 1) ) assertEquals(expected, response) diff --git a/projects/manage-supervision-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/api/controller/SentenceController.kt b/projects/manage-supervision-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/api/controller/SentenceController.kt index 7bf940a55..613490007 100644 --- a/projects/manage-supervision-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/api/controller/SentenceController.kt +++ b/projects/manage-supervision-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/api/controller/SentenceController.kt @@ -17,5 +17,5 @@ class SentenceController(private val sentenceService: SentenceService) { @GetMapping @Operation(summary = "Display the most recent ‘Active Event’ ") - fun getOverview(@PathVariable crn: String) = sentenceService.getMostRecentActiveEvent(crn) + fun getOverview(@PathVariable crn: String) = sentenceService.getEvents(crn) } \ No newline at end of file diff --git a/projects/manage-supervision-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/api/model/sentence/ProbationHistory.kt b/projects/manage-supervision-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/api/model/sentence/ProbationHistory.kt new file mode 100644 index 000000000..02670ff45 --- /dev/null +++ b/projects/manage-supervision-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/api/model/sentence/ProbationHistory.kt @@ -0,0 +1,10 @@ +package uk.gov.justice.digital.hmpps.api.model.sentence + +import java.time.LocalDate + +data class ProbationHistory( + val numberOfTerminatedEvents: Int, + val dateOfMostRecentTerminatedEvent: LocalDate?, + val numberOfTerminatedEventBreaches: Int, + val numberOfProfessionalContacts: Long +) diff --git a/projects/manage-supervision-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/api/model/sentence/SentenceOverview.kt b/projects/manage-supervision-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/api/model/sentence/SentenceOverview.kt index f74511569..79749c0c0 100644 --- a/projects/manage-supervision-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/api/model/sentence/SentenceOverview.kt +++ b/projects/manage-supervision-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/api/model/sentence/SentenceOverview.kt @@ -5,4 +5,5 @@ import uk.gov.justice.digital.hmpps.api.model.Name data class SentenceOverview( val name: Name, val sentences: List<Sentence>, + val probationHistory: ProbationHistory, ) diff --git a/projects/manage-supervision-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/integrations/delius/overview/entity/Event.kt b/projects/manage-supervision-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/integrations/delius/overview/entity/Event.kt index da35ce15e..6edc38573 100644 --- a/projects/manage-supervision-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/integrations/delius/overview/entity/Event.kt +++ b/projects/manage-supervision-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/integrations/delius/overview/entity/Event.kt @@ -97,6 +97,9 @@ class Disposal( @Column(name = "notional_end_date") val notionalEndDate: LocalDate? = null, + @Column(name = "termination_date") + val terminationDate: LocalDate? = null, + @Column(name = "active_flag", columnDefinition = "number") val active: Boolean, diff --git a/projects/manage-supervision-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/integrations/delius/personalDetails/entity/PersonalContactEntity.kt b/projects/manage-supervision-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/integrations/delius/personalDetails/entity/PersonalContactEntity.kt index bdb2c3eb5..deb159c66 100644 --- a/projects/manage-supervision-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/integrations/delius/personalDetails/entity/PersonalContactEntity.kt +++ b/projects/manage-supervision-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/integrations/delius/personalDetails/entity/PersonalContactEntity.kt @@ -45,6 +45,9 @@ class PersonalContactEntity( @Column(name = "start_date") val startDate: LocalDate?, + @Column(name = "end_date") + val endDate: LocalDate?, + @Column(name = "last_updated_datetime") val lastUpdated: LocalDate, @@ -81,4 +84,3 @@ interface PersonalContactRepository : JpaRepository<PersonalContactEntity, Long> fun PersonalContactRepository.getContact(crn: String, id: Long): PersonalContactEntity = findById(crn, id) ?: throw NotFoundException("PersonalContact", "id", id) - diff --git a/projects/manage-supervision-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/integrations/delius/referencedata/entity/ReferenceData.kt b/projects/manage-supervision-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/integrations/delius/referencedata/entity/ReferenceData.kt index a44c51c26..8f79498a1 100644 --- a/projects/manage-supervision-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/integrations/delius/referencedata/entity/ReferenceData.kt +++ b/projects/manage-supervision-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/integrations/delius/referencedata/entity/ReferenceData.kt @@ -18,5 +18,5 @@ class ReferenceData( val code: String, @Column(name = "code_description", length = 500, nullable = false) - val description: String + val description: String, ) diff --git a/projects/manage-supervision-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/integrations/delius/referencedata/entity/ReferenceDataLinkedList.kt b/projects/manage-supervision-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/integrations/delius/referencedata/entity/ReferenceDataLinkedList.kt new file mode 100644 index 000000000..228b92b1e --- /dev/null +++ b/projects/manage-supervision-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/integrations/delius/referencedata/entity/ReferenceDataLinkedList.kt @@ -0,0 +1,22 @@ +package uk.gov.justice.digital.hmpps.integrations.delius.referencedata.entity + +import jakarta.persistence.* +import org.hibernate.annotations.Immutable +import java.io.Serializable + +@Entity +@Table(name = "r_linked_list") +@Immutable +class ReferenceDataLinkedList( + @Id + val id: ReferenceDataLinkedListId +) + +@Embeddable +class ReferenceDataLinkedListId( + @Column(name = "STANDARD_REFERENCE_DATA1") + val data1: Long, + + @Column(name = "STANDARD_REFERENCE_DATA2") + val data2: Long +) : Serializable \ No newline at end of file diff --git a/projects/manage-supervision-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/integrations/delius/sentence/entity/OffenderManager.kt b/projects/manage-supervision-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/integrations/delius/sentence/entity/OffenderManager.kt new file mode 100644 index 000000000..9431e25ec --- /dev/null +++ b/projects/manage-supervision-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/integrations/delius/sentence/entity/OffenderManager.kt @@ -0,0 +1,24 @@ +package uk.gov.justice.digital.hmpps.integrations.delius.sentence.entity + +import jakarta.persistence.* +import org.hibernate.annotations.Immutable +import uk.gov.justice.digital.hmpps.integrations.delius.overview.entity.Person +import java.time.LocalDate + +@Entity +@Immutable +class OffenderManager( + @Id + @Column(name = "offender_manager_id") + val id: Long, + + @ManyToOne + @JoinColumn(name = "offender_id") + val person: Person, + + val allocationStaffId: Long?, + + val probationAreaId: Long, + + val endDate: LocalDate? +) \ No newline at end of file diff --git a/projects/manage-supervision-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/integrations/delius/sentence/entity/SentenceRepository.kt b/projects/manage-supervision-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/integrations/delius/sentence/entity/SentenceRepository.kt index 293f3ee10..e5348b981 100644 --- a/projects/manage-supervision-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/integrations/delius/sentence/entity/SentenceRepository.kt +++ b/projects/manage-supervision-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/integrations/delius/sentence/entity/SentenceRepository.kt @@ -3,6 +3,7 @@ package uk.gov.justice.digital.hmpps.integrations.delius.sentence.entity import org.springframework.data.jpa.repository.JpaRepository import org.springframework.data.jpa.repository.Query import uk.gov.justice.digital.hmpps.integrations.delius.overview.entity.Event +import uk.gov.justice.digital.hmpps.integrations.delius.overview.entity.Person interface EventSentenceRepository : JpaRepository<Event, Long> { @Query( @@ -15,10 +16,9 @@ interface EventSentenceRepository : JpaRepository<Event, Long> { "LEFT JOIN FETCH m.offence mo " + "LEFT JOIN FETCH ao.offence aoo " + "WHERE e.personId = :id " + - "AND e.active = true " + "ORDER BY e.eventNumber DESC " ) - fun findActiveSentencesByPersonId(id: Long): List<Event> + fun findSentencesByPersonId(id: Long): List<Event> } interface CourtAppearanceRepository : JpaRepository<CourtAppearance, Long> { @@ -29,3 +29,8 @@ interface AdditionalSentenceRepository : JpaRepository<AdditionalSentence, Long> fun getAllByEventId(id: Long): List<AdditionalSentence> } +interface OffenderManagerRepository : JpaRepository<OffenderManager, Long> { + + fun countOffenderManagersByPersonAndEndDateIsNotNull(person: Person): Long +} + 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 ec7b9c072..325ac9a49 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 @@ -10,10 +10,8 @@ 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.personalDetails.entity.CourtDocumentDetails import uk.gov.justice.digital.hmpps.integrations.delius.personalDetails.entity.DocumentRepository -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 -import uk.gov.justice.digital.hmpps.integrations.delius.sentence.entity.EventSentenceRepository +import uk.gov.justice.digital.hmpps.integrations.delius.sentence.entity.* +import java.time.LocalDate import uk.gov.justice.digital.hmpps.integrations.delius.sentence.entity.AdditionalSentence as ExtraSentence @Service @@ -23,18 +21,27 @@ class SentenceService( private val additionalSentenceRepository: AdditionalSentenceRepository, private val personRepository: PersonRepository, private val requirementRepository: RequirementRepository, - private val documentRepository: DocumentRepository + private val documentRepository: DocumentRepository, + private val offenderManagerRepository: OffenderManagerRepository ) { - fun getMostRecentActiveEvent(crn: String): SentenceOverview { - val person = personRepository.getSummary(crn) - val events = eventRepository.findActiveSentencesByPersonId(person.id) + fun getEvents(crn: String): SentenceOverview { + val person = personRepository.getPerson(crn) + val (activeEvents, inactiveEvents) = eventRepository.findSentencesByPersonId(person.id).partition { it.active } + return SentenceOverview( name = person.toName(), - sentences = events.map { + sentences = activeEvents.map { val courtAppearance = courtAppearanceRepository.getFirstCourtAppearanceByEventIdOrderByDate(it.id) val additionalSentences = additionalSentenceRepository.getAllByEventId(it.id) it.toSentence(courtAppearance, additionalSentences, crn) - }) + }, + ProbationHistory( + inactiveEvents.count(), + getMostRecentTerminatedDateFromInactiveEvents(inactiveEvents), + inactiveEvents.count { it.inBreach }, + offenderManagerRepository.countOffenderManagersByPersonAndEndDateIsNotNull(person) + ) + ) } fun Event.toSentence(courtAppearance: CourtAppearance?, additionalSentences: List<ExtraSentence>, crn: String) = @@ -62,7 +69,7 @@ class SentenceService( fun ExtraSentence.toAdditionalSentence(): AdditionalSentence = AdditionalSentence(length, amount, notes, type.description) - fun PersonSummaryEntity.toName() = + fun Person.toName() = Name(forename, secondName, surname) fun Disposal.toOrder() = @@ -84,4 +91,11 @@ class SentenceService( } fun CourtDocumentDetails.toCourtDocument(): CourtDocument = CourtDocument(id, lastSaved, documentName) + + fun getMostRecentTerminatedDateFromInactiveEvents(events: List<Event>): LocalDate? { + if (events.isNotEmpty()) { + return events.sortedByDescending { it.disposal?.terminationDate }[0].disposal?.terminationDate + } + return null + } } \ No newline at end of file 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 99e900132..79764ae42 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 @@ -1,7 +1,6 @@ package uk.gov.justice.digital.hmpps.service import org.junit.jupiter.api.Assertions.assertEquals -import org.junit.jupiter.api.BeforeEach import org.junit.jupiter.api.Test import org.junit.jupiter.api.extension.ExtendWith import org.mockito.InjectMocks @@ -23,7 +22,7 @@ import uk.gov.justice.digital.hmpps.integrations.delius.personalDetails.entity.D import uk.gov.justice.digital.hmpps.integrations.delius.sentence.entity.AdditionalSentenceRepository import uk.gov.justice.digital.hmpps.integrations.delius.sentence.entity.CourtAppearanceRepository import uk.gov.justice.digital.hmpps.integrations.delius.sentence.entity.EventSentenceRepository -import uk.gov.justice.digital.hmpps.utils.Summary +import uk.gov.justice.digital.hmpps.integrations.delius.sentence.entity.OffenderManagerRepository import java.time.LocalDate @ExtendWith(MockitoExtension::class) @@ -47,34 +46,31 @@ class SentenceServiceTest { @Mock lateinit var documentRepository: DocumentRepository + @Mock + lateinit var offenderManagerRepository: OffenderManagerRepository + @InjectMocks lateinit var service: SentenceService - private lateinit var personSummary: Summary - - @BeforeEach - fun setup() { - personSummary = Summary( - id = 1, - forename = "TestName", - surname = "TestSurname", crn = "CRN", pnc = "PNC", dateOfBirth = LocalDate.now().minusYears(50) - ) - } - @Test fun `no active sentences`() { - whenever(personRepository.findSummary(PersonGenerator.OVERVIEW.crn)).thenReturn(personSummary) - whenever(eventRepository.findActiveSentencesByPersonId(personSummary.id)).thenReturn( + whenever(personRepository.findByCrn(PersonGenerator.OVERVIEW.crn)).thenReturn(PersonGenerator.OVERVIEW) + whenever(eventRepository.findSentencesByPersonId(PersonGenerator.OVERVIEW.id)).thenReturn( listOf() ) + whenever(offenderManagerRepository.countOffenderManagersByPersonAndEndDateIsNotNull(PersonGenerator.OVERVIEW)).thenReturn( + 0 + ) - val expected = SentenceOverview(Name("TestName", surname = "TestSurname"), listOf()) - val response = service.getMostRecentActiveEvent(PersonGenerator.OVERVIEW.crn) + val expected = + SentenceOverview(Name("Forename", "Middle1", "Surname"), listOf(), ProbationHistory(0, null, 0, 0)) + val response = service.getEvents(PersonGenerator.OVERVIEW.crn) assertEquals(expected, response) - verify(personRepository, times(1)).findSummary(PersonGenerator.OVERVIEW.crn) - verify(eventRepository, times(1)).findActiveSentencesByPersonId(personSummary.id) + + verify(personRepository, times(1)).findByCrn(PersonGenerator.OVERVIEW.crn) + verify(eventRepository, times(1)).findSentencesByPersonId(PersonGenerator.OVERVIEW.id) verifyNoMoreInteractions(eventRepository) verifyNoMoreInteractions(personRepository) @@ -106,9 +102,9 @@ class SentenceServiceTest { val scheduledRarDays = OverviewServiceTest.RarDays(2, "SCHEDULED") - whenever(personRepository.findSummary(PersonGenerator.OVERVIEW.crn)).thenReturn(personSummary) + whenever(personRepository.findByCrn(PersonGenerator.OVERVIEW.crn)).thenReturn(PersonGenerator.OVERVIEW) - whenever(eventRepository.findActiveSentencesByPersonId(personSummary.id)).thenReturn(listOf(event)) + whenever(eventRepository.findSentencesByPersonId(PersonGenerator.OVERVIEW.id)).thenReturn(listOf(event)) whenever(courtAppearanceRepository.getFirstCourtAppearanceByEventIdOrderByDate(event.id)) .thenReturn(CourtAppearanceGenerator.generate(CourtGenerator.DEFAULT)) @@ -132,10 +128,10 @@ class SentenceServiceTest { ) ) - val response = service.getMostRecentActiveEvent(PersonGenerator.OVERVIEW.crn) + val response = service.getEvents(PersonGenerator.OVERVIEW.crn) val expected = SentenceOverview( - Name("TestName", surname = "TestSurname"), + Name("Forename", "Middle1", "Surname"), listOf( Sentence( OffenceDetails( @@ -168,11 +164,12 @@ class SentenceServiceTest { ), listOf(CourtDocument(1, LocalDate.now(), "Pre Sentence Event")) ) - ) + ), + ProbationHistory(0, null, 0, 0) ) assertEquals(expected, response) - verify(eventRepository, times(1)).findActiveSentencesByPersonId(personSummary.id) + verify(eventRepository, times(1)).findSentencesByPersonId(PersonGenerator.OVERVIEW.id) verify(additionalSentenceRepository, times(1)).getAllByEventId(event.id) verify(courtAppearanceRepository, times(1)).getFirstCourtAppearanceByEventIdOrderByDate(event.id) verify(documentRepository, times(1)).getCourtDocuments(event.id, event.eventNumber)