From 493915dd420cdb6aea4b9c9a598c293dc27f5936 Mon Sep 17 00:00:00 2001 From: Anthony Britton Date: Fri, 27 Oct 2023 13:57:12 +0100 Subject: [PATCH] PI-1595 updated contact details to match Community API --- .../justice/digital/hmpps/data/DataLoader.kt | 3 +- .../hmpps/data/generator/PersonGenerator.kt | 5 ++- .../hmpps/data/generator/ProviderGenerator.kt | 7 +++- .../delius/contact/entity/Contact.kt | 4 ++ .../delius/person/entity/Person.kt | 24 +++++------ .../delius/provider/entity/Manager.kt | 7 ++++ .../delius/provider/entity/PrisonManager.kt | 8 ++-- .../delius/provider/entity/Team.kt | 2 + .../digital/hmpps/services/ContactService.kt | 20 +++++---- .../hmpps/services/PrisonManagerService.kt | 41 ++++++++++++++++++- .../mapping/ProbationRecordMappingKtTest.kt | 2 +- 11 files changed, 90 insertions(+), 33 deletions(-) create mode 100644 projects/manage-pom-cases-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/integrations/delius/provider/entity/Manager.kt diff --git a/projects/manage-pom-cases-and-delius/src/dev/kotlin/uk/gov/justice/digital/hmpps/data/DataLoader.kt b/projects/manage-pom-cases-and-delius/src/dev/kotlin/uk/gov/justice/digital/hmpps/data/DataLoader.kt index 65facf5e5b..f4afd04742 100644 --- a/projects/manage-pom-cases-and-delius/src/dev/kotlin/uk/gov/justice/digital/hmpps/data/DataLoader.kt +++ b/projects/manage-pom-cases-and-delius/src/dev/kotlin/uk/gov/justice/digital/hmpps/data/DataLoader.kt @@ -22,7 +22,7 @@ import uk.gov.justice.digital.hmpps.integrations.delius.allocation.entity.event. import uk.gov.justice.digital.hmpps.integrations.delius.allocation.entity.event.keydate.KeyDateRepository import uk.gov.justice.digital.hmpps.integrations.delius.contact.entity.ContactType import uk.gov.justice.digital.hmpps.integrations.delius.contact.entity.ContactTypeRepository -import uk.gov.justice.digital.hmpps.integrations.delius.person.entity.PersonManagerRepository +import uk.gov.justice.digital.hmpps.integrations.delius.person.entity.PersonManager import uk.gov.justice.digital.hmpps.integrations.delius.person.entity.PersonRepository import uk.gov.justice.digital.hmpps.integrations.delius.person.entity.registration.entity.RegisterType import uk.gov.justice.digital.hmpps.integrations.delius.person.entity.registration.entity.RegistrationRepository @@ -186,3 +186,4 @@ interface EventRepository : JpaRepository interface DisposalRepository : JpaRepository interface RegisterTypeRepository : JpaRepository interface InstitutionRepository : JpaRepository +interface PersonManagerRepository : JpaRepository diff --git a/projects/manage-pom-cases-and-delius/src/dev/kotlin/uk/gov/justice/digital/hmpps/data/generator/PersonGenerator.kt b/projects/manage-pom-cases-and-delius/src/dev/kotlin/uk/gov/justice/digital/hmpps/data/generator/PersonGenerator.kt index 5d0ecc8290..e3e47e0c5a 100644 --- a/projects/manage-pom-cases-and-delius/src/dev/kotlin/uk/gov/justice/digital/hmpps/data/generator/PersonGenerator.kt +++ b/projects/manage-pom-cases-and-delius/src/dev/kotlin/uk/gov/justice/digital/hmpps/data/generator/PersonGenerator.kt @@ -2,6 +2,7 @@ package uk.gov.justice.digital.hmpps.data.generator import uk.gov.justice.digital.hmpps.integrations.delius.person.entity.Person import uk.gov.justice.digital.hmpps.integrations.delius.person.entity.PersonManager +import uk.gov.justice.digital.hmpps.integrations.delius.provider.entity.ProbationArea import uk.gov.justice.digital.hmpps.integrations.delius.provider.entity.Staff import uk.gov.justice.digital.hmpps.integrations.delius.provider.entity.Team import uk.gov.justice.digital.hmpps.integrations.delius.reference.entity.ReferenceData @@ -51,11 +52,11 @@ object PersonManagerGenerator { fun generate( person: Person, - providerId: Long = ProviderGenerator.DEFAULT_PROVIDER.id, + provider: ProbationArea = ProviderGenerator.DEFAULT_PROVIDER, team: Team = ProviderGenerator.DEFAULT_TEAM, staff: Staff = ProviderGenerator.DEFAULT_STAFF, active: Boolean = true, softDeleted: Boolean = false, id: Long = IdGenerator.getAndIncrement() - ) = PersonManager(person, team, staff, providerId, active, softDeleted, id) + ) = PersonManager(person, team, staff, provider, active, softDeleted, id) } diff --git a/projects/manage-pom-cases-and-delius/src/dev/kotlin/uk/gov/justice/digital/hmpps/data/generator/ProviderGenerator.kt b/projects/manage-pom-cases-and-delius/src/dev/kotlin/uk/gov/justice/digital/hmpps/data/generator/ProviderGenerator.kt index ee17d8c5b3..a9dfea3fc5 100644 --- a/projects/manage-pom-cases-and-delius/src/dev/kotlin/uk/gov/justice/digital/hmpps/data/generator/ProviderGenerator.kt +++ b/projects/manage-pom-cases-and-delius/src/dev/kotlin/uk/gov/justice/digital/hmpps/data/generator/ProviderGenerator.kt @@ -18,7 +18,12 @@ object ProviderGenerator { val DEFAULT_STAFF = generateStaff("N03DEF0", "Default", "Staff", user = UserGenerator.DEFAULT_STAFF_USER) fun generateProvider(providerCode: String, prisonCode: String?, id: Long = IdGenerator.getAndIncrement()) = - ProbationArea(id, providerCode, prisonCode?.let { Institution(IdGenerator.getAndIncrement(), it) }) + ProbationArea( + id, + providerCode, + "Description of $providerCode", + prisonCode?.let { Institution(IdGenerator.getAndIncrement(), it) } + ) fun generateDistrict(code: String, description: String = "LDU $code", id: Long = IdGenerator.getAndIncrement()) = District(code, description, id) diff --git a/projects/manage-pom-cases-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/integrations/delius/contact/entity/Contact.kt b/projects/manage-pom-cases-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/integrations/delius/contact/entity/Contact.kt index f153df1b4a..1da94c78fb 100644 --- a/projects/manage-pom-cases-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/integrations/delius/contact/entity/Contact.kt +++ b/projects/manage-pom-cases-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/integrations/delius/contact/entity/Contact.kt @@ -7,6 +7,7 @@ import jakarta.persistence.GeneratedValue import jakarta.persistence.GenerationType import jakarta.persistence.Id import jakarta.persistence.JoinColumn +import jakarta.persistence.Lob import jakarta.persistence.ManyToOne import jakarta.persistence.SequenceGenerator import jakarta.persistence.Table @@ -41,6 +42,9 @@ class Contact( @Column(name = "contact_start_time") val startTime: ZonedDateTime, + @Lob + val notes: String?, + @Column(name = "probation_area_id") val providerId: Long, val teamId: Long, diff --git a/projects/manage-pom-cases-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/integrations/delius/person/entity/Person.kt b/projects/manage-pom-cases-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/integrations/delius/person/entity/Person.kt index 38489e4e1b..3fbc48eb5b 100644 --- a/projects/manage-pom-cases-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/integrations/delius/person/entity/Person.kt +++ b/projects/manage-pom-cases-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/integrations/delius/person/entity/Person.kt @@ -13,6 +13,8 @@ import org.springframework.data.jpa.repository.EntityGraph import org.springframework.data.jpa.repository.JpaRepository import org.springframework.data.jpa.repository.Query import uk.gov.justice.digital.hmpps.exception.NotFoundException +import uk.gov.justice.digital.hmpps.integrations.delius.provider.entity.Manager +import uk.gov.justice.digital.hmpps.integrations.delius.provider.entity.ProbationArea import uk.gov.justice.digital.hmpps.integrations.delius.provider.entity.Staff import uk.gov.justice.digital.hmpps.integrations.delius.provider.entity.Team import uk.gov.justice.digital.hmpps.integrations.delius.reference.entity.ReferenceData @@ -60,14 +62,15 @@ class PersonManager( @ManyToOne @JoinColumn(name = "team_id") - val team: Team, + override val team: Team, @ManyToOne @JoinColumn(name = "allocation_staff_id") - val staff: Staff, + override val staff: Staff, - @Column(name = "probation_area_id") - val providerId: Long, + @ManyToOne + @JoinColumn(name = "probation_area_id") + override val probationArea: ProbationArea, @Column(name = "active_flag", columnDefinition = "number") val active: Boolean = true, @@ -78,13 +81,13 @@ class PersonManager( @Id @Column(name = "offender_manager_id") val id: Long -) +) : Manager interface PersonRepository : JpaRepository { - @EntityGraph(attributePaths = ["currentTier", "managers.team.district", "managers.staff.user"]) + @EntityGraph(attributePaths = ["currentTier", "managers.team.district", "managers.staff.user", "managers.probationArea"]) fun findByNomsId(nomsId: String): Person? - @EntityGraph(attributePaths = ["currentTier", "managers.team.district", "managers.staff.user"]) + @EntityGraph(attributePaths = ["currentTier", "managers.team.district", "managers.staff.user", "managers.probationArea"]) fun findByCrn(crn: String): Person? @Query("select p.id from Person p where p.nomsId = :nomsId and p.softDeleted = false") @@ -96,10 +99,3 @@ fun PersonRepository.getByNomsId(nomsId: String) = fun PersonRepository.getByCrn(crn: String) = findByCrn(crn) ?: throw NotFoundException("Person", "crn", crn) - -interface PersonManagerRepository : JpaRepository { - fun findByPersonIdAndActiveTrue(personId: Long): PersonManager? -} - -fun PersonManagerRepository.getCurrentCom(personId: Long) = - findByPersonIdAndActiveTrue(personId) ?: throw NotFoundException("PersonManager", "personId", personId) diff --git a/projects/manage-pom-cases-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/integrations/delius/provider/entity/Manager.kt b/projects/manage-pom-cases-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/integrations/delius/provider/entity/Manager.kt new file mode 100644 index 0000000000..5bfe2f4108 --- /dev/null +++ b/projects/manage-pom-cases-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/integrations/delius/provider/entity/Manager.kt @@ -0,0 +1,7 @@ +package uk.gov.justice.digital.hmpps.integrations.delius.provider.entity + +interface Manager { + val probationArea: ProbationArea + val team: Team + val staff: Staff +} diff --git a/projects/manage-pom-cases-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/integrations/delius/provider/entity/PrisonManager.kt b/projects/manage-pom-cases-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/integrations/delius/provider/entity/PrisonManager.kt index 50a0e6f328..b08a946ba6 100644 --- a/projects/manage-pom-cases-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/integrations/delius/provider/entity/PrisonManager.kt +++ b/projects/manage-pom-cases-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/integrations/delius/provider/entity/PrisonManager.kt @@ -55,19 +55,19 @@ class PrisonManager( @ManyToOne @JoinColumn(name = "allocation_staff_id", nullable = false) - val staff: Staff, + override val staff: Staff, @ManyToOne @JoinColumn(name = "allocation_team_id", nullable = false) - val team: Team, + override val team: Team, @ManyToOne @JoinColumn(name = "probation_area_id", nullable = false) - val probationArea: ProbationArea, + override val probationArea: ProbationArea, @Column(columnDefinition = "number", nullable = false) val softDeleted: Boolean = false -) { +) : Manager { @Column var endDate: ZonedDateTime? = null set(value) { diff --git a/projects/manage-pom-cases-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/integrations/delius/provider/entity/Team.kt b/projects/manage-pom-cases-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/integrations/delius/provider/entity/Team.kt index 86628587e5..b1e5b2f927 100644 --- a/projects/manage-pom-cases-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/integrations/delius/provider/entity/Team.kt +++ b/projects/manage-pom-cases-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/integrations/delius/provider/entity/Team.kt @@ -65,6 +65,8 @@ class ProbationArea( @Column(name = "code", columnDefinition = "char(3)") val code: String, + val description: String, + @OneToOne @JoinColumn( name = "institution_id", diff --git a/projects/manage-pom-cases-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/services/ContactService.kt b/projects/manage-pom-cases-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/services/ContactService.kt index c247bfd30c..36c548a1ac 100644 --- a/projects/manage-pom-cases-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/services/ContactService.kt +++ b/projects/manage-pom-cases-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/services/ContactService.kt @@ -6,18 +6,21 @@ import uk.gov.justice.digital.hmpps.integrations.delius.contact.entity.ContactRe import uk.gov.justice.digital.hmpps.integrations.delius.contact.entity.ContactType import uk.gov.justice.digital.hmpps.integrations.delius.contact.entity.ContactTypeRepository import uk.gov.justice.digital.hmpps.integrations.delius.contact.entity.getByCode -import uk.gov.justice.digital.hmpps.integrations.delius.person.entity.PersonManagerRepository -import uk.gov.justice.digital.hmpps.integrations.delius.person.entity.getCurrentCom +import uk.gov.justice.digital.hmpps.integrations.delius.provider.entity.Manager import java.time.ZonedDateTime @Service class ContactService( - private val personManagerRepository: PersonManagerRepository, private val contactTypeRepository: ContactTypeRepository, private val contactRepository: ContactRepository ) { - fun createContact(personId: Long, typeCode: ContactType.Code, dateTime: ZonedDateTime) { - val com = personManagerRepository.getCurrentCom(personId) + fun createContact( + personId: Long, + typeCode: ContactType.Code, + dateTime: ZonedDateTime, + manager: Manager, + notes: String? + ) { val type = contactTypeRepository.getByCode(typeCode.value) contactRepository.save( Contact( @@ -25,9 +28,10 @@ class ContactService( type, dateTime.toLocalDate(), dateTime, - com.providerId, - com.team.id, - com.staff.id + notes, + manager.probationArea.id, + manager.team.id, + manager.staff.id ) ) } diff --git a/projects/manage-pom-cases-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/services/PrisonManagerService.kt b/projects/manage-pom-cases-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/services/PrisonManagerService.kt index 524c158fb1..1c12cbc7d2 100644 --- a/projects/manage-pom-cases-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/services/PrisonManagerService.kt +++ b/projects/manage-pom-cases-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/services/PrisonManagerService.kt @@ -3,6 +3,8 @@ package uk.gov.justice.digital.hmpps.services import org.springframework.stereotype.Service import org.springframework.transaction.annotation.Transactional import uk.gov.justice.digital.hmpps.api.model.Name +import uk.gov.justice.digital.hmpps.datetime.DeliusDateFormatter +import uk.gov.justice.digital.hmpps.datetime.DeliusDateTimeFormatter import uk.gov.justice.digital.hmpps.integrations.delius.contact.entity.ContactType import uk.gov.justice.digital.hmpps.integrations.delius.provider.entity.PrisonManager import uk.gov.justice.digital.hmpps.integrations.delius.provider.entity.PrisonManagerRepository @@ -58,6 +60,28 @@ class PrisonManagerService( private fun PrisonManager.hasChanged(probationArea: ProbationArea, team: Team, staff: Staff) = this.probationArea.id != probationArea.id || this.team.id != team.id || this.staff.id != staff.id + fun PrisonManager.allocationNotes() = + """ + |Transfer Reason: ${allocationReason.description} + |Transfer Date: ${DeliusDateFormatter.format(date)} + """.trimMargin() + + fun PrisonManager.transferNotes() = + """ + | + |From Establishment Provider: ${probationArea.description} + |From Team: ${team.description} + |From Officer: ${staff.surname}, ${staff.forename} + """.trimMargin() + + fun PrisonManager.responsibleOfficerDetails() = listOfNotNull( + "Responsible Officer Type: Prison Offender Manager", + "Responsible Officer: ${staff.surname}, ${staff.forename} (${team.description}, ${probationArea.description})", + "Start Date: ${DeliusDateTimeFormatter.format(date)}", + endDate?.let { "End Date: " + DeliusDateTimeFormatter.format(it) }, + "Allocation Reason: ${allocationReason.description}" + ).joinToString(System.lineSeparator()) + private fun PrisonManager?.changeTo( personId: Long, dateTime: ZonedDateTime, @@ -79,13 +103,26 @@ class PrisonManagerService( staff = staff, team = team ) - contactService.createContact(personId, allocationReasonCode.ctc, dateTime) + val notes = newPom.allocationNotes() + (this?.transferNotes() ?: "") + contactService.createContact(personId, allocationReasonCode.ctc, dateTime, newPom, notes) this?.apply { endDate = dateTime responsibleOfficer?.also { it.endDate = dateTime newPom.responsibleOfficer = ResponsibleOfficer(personId, newPom, dateTime) - contactService.createContact(personId, ContactType.Code.RESPONSIBLE_OFFICER_CHANGE, dateTime) + contactService.createContact( + personId, + ContactType.Code.RESPONSIBLE_OFFICER_CHANGE, + dateTime, + newPom, + """ + |New Details: + |${newPom.responsibleOfficerDetails()} + | + |Previous Details: + |${this.responsibleOfficerDetails()} + """.trimMargin() + ) } } newPom diff --git a/projects/manage-pom-cases-and-delius/src/test/kotlin/uk/gov/justice/digital/hmpps/services/mapping/ProbationRecordMappingKtTest.kt b/projects/manage-pom-cases-and-delius/src/test/kotlin/uk/gov/justice/digital/hmpps/services/mapping/ProbationRecordMappingKtTest.kt index 4025256dd3..25f57157ff 100644 --- a/projects/manage-pom-cases-and-delius/src/test/kotlin/uk/gov/justice/digital/hmpps/services/mapping/ProbationRecordMappingKtTest.kt +++ b/projects/manage-pom-cases-and-delius/src/test/kotlin/uk/gov/justice/digital/hmpps/services/mapping/ProbationRecordMappingKtTest.kt @@ -54,7 +54,7 @@ internal class ProbationRecordMappingKtTest { PersonGenerator.DEFAULT, ProviderGenerator.DEFAULT_TEAM, unallocated, - ProviderGenerator.DEFAULT_PROVIDER.id, + ProviderGenerator.DEFAULT_PROVIDER, id = 99 ) assertNull(om.manager().code)