Skip to content

Commit

Permalink
PI-1595 updated contact details to match Community API
Browse files Browse the repository at this point in the history
  • Loading branch information
anthony-britton-moj committed Oct 27, 2023
1 parent ceb6d26 commit 493915d
Show file tree
Hide file tree
Showing 11 changed files with 90 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -186,3 +186,4 @@ interface EventRepository : JpaRepository<Event, Long>
interface DisposalRepository : JpaRepository<Disposal, Long>
interface RegisterTypeRepository : JpaRepository<RegisterType, Long>
interface InstitutionRepository : JpaRepository<Institution, Long>
interface PersonManagerRepository : JpaRepository<PersonManager, Long>
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
}
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand All @@ -78,13 +81,13 @@ class PersonManager(
@Id
@Column(name = "offender_manager_id")
val id: Long
)
) : Manager

interface PersonRepository : JpaRepository<Person, Long> {
@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")
Expand All @@ -96,10 +99,3 @@ fun PersonRepository.getByNomsId(nomsId: String) =

fun PersonRepository.getByCrn(crn: String) =
findByCrn(crn) ?: throw NotFoundException("Person", "crn", crn)

interface PersonManagerRepository : JpaRepository<PersonManager, Long> {
fun findByPersonIdAndActiveTrue(personId: Long): PersonManager?
}

fun PersonManagerRepository.getCurrentCom(personId: Long) =
findByPersonIdAndActiveTrue(personId) ?: throw NotFoundException("PersonManager", "personId", personId)
Original file line number Diff line number Diff line change
@@ -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
}
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ class ProbationArea(
@Column(name = "code", columnDefinition = "char(3)")
val code: String,

val description: String,

@OneToOne
@JoinColumn(
name = "institution_id",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,32 @@ 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(
personId,
type,
dateTime.toLocalDate(),
dateTime,
com.providerId,
com.team.id,
com.staff.id
notes,
manager.probationArea.id,
manager.team.id,
manager.staff.id
)
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 493915d

Please sign in to comment.