Skip to content

Commit

Permalink
PI-1586 add contacts for POM Allocation and RO change (#2445)
Browse files Browse the repository at this point in the history
* PI-1586 add contacts for POM Allocation and RO change

* ktlintFormat

* update vars to vals
  • Loading branch information
anthony-britton-moj authored Oct 25, 2023
1 parent e6f8a44 commit 20d8f69
Show file tree
Hide file tree
Showing 11 changed files with 228 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ database:
- staff_team
- prison_offender_manager
- responsible_officer
- contact

audit:
username: ManagePomCasesAndDelius
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import org.springframework.data.jpa.repository.JpaRepository
import org.springframework.stereotype.Component
import uk.gov.justice.digital.hmpps.data.generator.CaseAllocationGenerator
import uk.gov.justice.digital.hmpps.data.generator.EventGenerator
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.PersonManagerGenerator
import uk.gov.justice.digital.hmpps.data.generator.ProviderGenerator
Expand All @@ -19,7 +20,9 @@ import uk.gov.justice.digital.hmpps.integrations.delius.allocation.entity.event.
import uk.gov.justice.digital.hmpps.integrations.delius.allocation.entity.event.Disposal
import uk.gov.justice.digital.hmpps.integrations.delius.allocation.entity.event.Event
import uk.gov.justice.digital.hmpps.integrations.delius.allocation.entity.event.keydate.KeyDateRepository
import uk.gov.justice.digital.hmpps.integrations.delius.person.entity.PersonManager
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.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 @@ -54,8 +57,8 @@ class DataLoader(
private val custodyRepository: CustodyRepository,
private val caseAllocationRepository: CaseAllocationRepository,
private val registrationRepository: RegistrationRepository,
private val keyDateRepository: KeyDateRepository

private val keyDateRepository: KeyDateRepository,
private val contactTypeRepository: ContactTypeRepository
) : ApplicationListener<ApplicationReadyEvent> {

@PostConstruct
Expand All @@ -76,6 +79,15 @@ class DataLoader(
RegistrationGenerator.TYPE_DASO
)
)
contactTypeRepository.saveAll(
ContactType.Code.entries.map {
ContactType(
it.value,
IdGenerator.getAndIncrement()
)
}
)

districtRepository.save(ProviderGenerator.DEFAULT_DISTRICT)
teamRepository.saveAll(PersonManagerGenerator.ALL.map { it.team } + ProviderGenerator.POM_TEAM)
val staffMap = staffRepository.saveAll(PersonManagerGenerator.ALL.map { it.staff }).associateBy { it.code }
Expand All @@ -98,9 +110,11 @@ class DataLoader(
personManagerRepository.saveAll(
PersonManagerGenerator.ALL.map {
PersonManagerGenerator.generate(
it.team,
staffMap[it.staff.code]!!,
it.person
team = it.team,
staff = staffMap[it.staff.code]!!,
person = it.person,
active = it.active,
softDeleted = it.softDeleted
)
}
)
Expand Down Expand Up @@ -168,7 +182,6 @@ class DataLoader(
interface ReferenceDataSetRepository : JpaRepository<ReferenceDataSet, Long>
interface StaffUserRepository : JpaRepository<StaffUser, Long>
interface DistrictRepository : JpaRepository<District, Long>
interface PersonManagerRepository : JpaRepository<PersonManager, Long>
interface EventRepository : JpaRepository<Event, Long>
interface DisposalRepository : JpaRepository<Disposal, Long>
interface RegisterTypeRepository : JpaRepository<RegisterType, Long>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,25 +30,32 @@ object PersonGenerator {

object PersonManagerGenerator {
val PREVIOUS = generate(
ProviderGenerator.generateTeam("N03PRE"),
ProviderGenerator.generateStaff("N03PRE1", "Previous", "Manager"),
person = PersonGenerator.DEFAULT,
team = ProviderGenerator.generateTeam("N03PRE"),
staff = ProviderGenerator.generateStaff("N03PRE1", "Previous", "Manager"),
active = false
)
val DELETED = generate(
ProviderGenerator.generateTeam("N03DEL"),
ProviderGenerator.generateStaff("N03DEL1", "Deleted", "Manager"),
person = PersonGenerator.DEFAULT,
team = ProviderGenerator.generateTeam("N03DEL"),
staff = ProviderGenerator.generateStaff("N03DEL1", "Deleted", "Manager"),
softDeleted = true
)
val DEFAULT = generate(ProviderGenerator.DEFAULT_TEAM, ProviderGenerator.DEFAULT_STAFF)
val DEFAULT = generate(
person = PersonGenerator.DEFAULT,
team = ProviderGenerator.DEFAULT_TEAM,
staff = ProviderGenerator.DEFAULT_STAFF
)

val ALL = listOf(DEFAULT, PREVIOUS, DELETED)

fun generate(
person: Person,
providerId: Long = ProviderGenerator.DEFAULT_PROVIDER.id,
team: Team = ProviderGenerator.DEFAULT_TEAM,
staff: Staff = ProviderGenerator.DEFAULT_STAFF,
person: Person = PersonGenerator.DEFAULT,
active: Boolean = true,
softDeleted: Boolean = false,
id: Long = IdGenerator.getAndIncrement()
) = PersonManager(person, team, staff, active, softDeleted, id)
) = PersonManager(person, team, staff, providerId, active, softDeleted, id)
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package uk.gov.justice.digital.hmpps
import com.github.tomakehurst.wiremock.WireMockServer
import org.hamcrest.MatcherAssert.assertThat
import org.hamcrest.Matchers.equalTo
import org.hamcrest.Matchers.hasItems
import org.junit.jupiter.api.Assertions.assertNotNull
import org.junit.jupiter.api.Assertions.assertNull
import org.junit.jupiter.api.MethodOrderer
Expand All @@ -17,6 +18,8 @@ import org.springframework.boot.test.context.SpringBootTest
import org.springframework.boot.test.mock.mockito.MockBean
import org.springframework.boot.test.mock.mockito.SpyBean
import uk.gov.justice.digital.hmpps.data.generator.PersonGenerator
import uk.gov.justice.digital.hmpps.integrations.delius.contact.entity.ContactRepository
import uk.gov.justice.digital.hmpps.integrations.delius.contact.entity.ContactType
import uk.gov.justice.digital.hmpps.integrations.delius.provider.entity.PrisonManagerRepository
import uk.gov.justice.digital.hmpps.integrations.delius.provider.entity.ResponsibleOfficer
import uk.gov.justice.digital.hmpps.integrations.delius.provider.entity.Staff
Expand Down Expand Up @@ -49,6 +52,9 @@ internal class AllocationMessagingIntegrationTest {
@Autowired
lateinit var prisonManagerRepository: PrisonManagerRepository

@Autowired
lateinit var contactRepository: ContactRepository

@Order(1)
@Test
fun `allocate first POM successfully`() {
Expand All @@ -70,6 +76,9 @@ internal class AllocationMessagingIntegrationTest {
assertThat(prisonManager?.staff?.forename, equalTo("John"))
assertThat(prisonManager?.staff?.surname, equalTo("Smith"))

val contacts = contactRepository.findAll().filter { it.personId == PersonGenerator.DEFAULT.id }
assertThat(contacts.map { it.type.code }, hasItems(ContactType.Code.POM_AUTO_ALLOCATION.value))

verify(telemetryService).trackEvent(
"POM Allocated",
mapOf(
Expand Down Expand Up @@ -114,6 +123,15 @@ internal class AllocationMessagingIntegrationTest {
assertNotNull(previousPom?.endDate)
assertNotNull(previousPom?.responsibleOfficer?.endDate)

val contacts = contactRepository.findAll().filter { it.personId == PersonGenerator.DEFAULT.id }
assertThat(
contacts.map { it.type.code },
hasItems(
ContactType.Code.POM_INTERNAL_ALLOCATION.value,
ContactType.Code.RESPONSIBLE_OFFICER_CHANGE.value
)
)

verify(telemetryService).trackEvent(
"POM Allocated",
mapOf(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
package uk.gov.justice.digital.hmpps.integrations.delius.contact.entity

import jakarta.persistence.Column
import jakarta.persistence.Entity
import jakarta.persistence.EntityListeners
import jakarta.persistence.GeneratedValue
import jakarta.persistence.GenerationType
import jakarta.persistence.Id
import jakarta.persistence.JoinColumn
import jakarta.persistence.ManyToOne
import jakarta.persistence.SequenceGenerator
import jakarta.persistence.Table
import jakarta.persistence.Version
import org.hibernate.annotations.Immutable
import org.springframework.data.annotation.CreatedBy
import org.springframework.data.annotation.CreatedDate
import org.springframework.data.annotation.LastModifiedBy
import org.springframework.data.annotation.LastModifiedDate
import org.springframework.data.jpa.domain.support.AuditingEntityListener
import org.springframework.data.jpa.repository.JpaRepository
import uk.gov.justice.digital.hmpps.exception.NotFoundException
import java.time.LocalDate
import java.time.ZonedDateTime

@Entity
@EntityListeners(AuditingEntityListener::class)
@Table(name = "contact")
@SequenceGenerator(name = "contact_id_seq", sequenceName = "contact_id_seq", allocationSize = 1)
class Contact(

@Column(name = "offender_id")
val personId: Long,

@ManyToOne
@JoinColumn(name = "contact_type_id")
val type: ContactType,

@Column(name = "contact_date")
val date: LocalDate,

@Column(name = "contact_start_time")
val startTime: ZonedDateTime,

@Column(name = "probation_area_id")
val providerId: Long,
val teamId: Long,
val staffId: Long,

@Column(columnDefinition = "number")
val softDeleted: Boolean = false,

@Id
@Column(name = "contact_id")
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "contact_id_seq")
val id: Long = 0
) {
@Version
@Column(name = "row_version")
val version: Long = 0

@CreatedDate
var createdDatetime: ZonedDateTime = ZonedDateTime.now()

@CreatedBy
var createdByUserId: Long = 0

@LastModifiedDate
var lastUpdatedDatetime: ZonedDateTime = ZonedDateTime.now()

@LastModifiedBy
var lastUpdatedUserId: Long = 0

val partitionAreaId: Long = 0
}

@Immutable
@Entity
@Table(name = "r_contact_type")
class ContactType(
val code: String,
@Id
@Column(name = "contact_type_id")
val id: Long
) {
enum class Code(val value: String) {
POM_AUTO_ALLOCATION("EPOMAT"),
POM_INTERNAL_ALLOCATION("EPOMIN"),
POM_EXTERNAL_ALLOCATION("EPOMEX"),
RESPONSIBLE_OFFICER_CHANGE("ROC")
}
}

interface ContactRepository : JpaRepository<Contact, Long>

interface ContactTypeRepository : JpaRepository<ContactType, Long> {
fun findByCode(code: String): ContactType?
}

fun ContactTypeRepository.getByCode(code: String) =
findByCode(code) ?: throw NotFoundException("ContactType", "code", code)
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ class PersonManager(
@JoinColumn(name = "allocation_staff_id")
val staff: Staff,

@Column(name = "probation_area_id")
val providerId: Long,

@Column(name = "active_flag", columnDefinition = "number")
val active: Boolean = true,

Expand Down Expand Up @@ -93,3 +96,10 @@ 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
Expand Up @@ -21,6 +21,7 @@ import org.springframework.data.annotation.LastModifiedDate
import org.springframework.data.jpa.domain.support.AuditingEntityListener
import org.springframework.data.jpa.repository.JpaRepository
import org.springframework.data.jpa.repository.Query
import uk.gov.justice.digital.hmpps.integrations.delius.contact.entity.ContactType
import uk.gov.justice.digital.hmpps.integrations.delius.reference.entity.ReferenceData
import java.time.ZonedDateTime

Expand Down Expand Up @@ -96,6 +97,12 @@ class PrisonManager(
@LastModifiedDate
@Column(nullable = false)
var lastUpdatedDatetime: ZonedDateTime = ZonedDateTime.now()

enum class AllocationReasonCode(val value: String, val ctc: ContactType.Code) {
AUTO("AUT", ContactType.Code.POM_AUTO_ALLOCATION),
INTERNAL("INA", ContactType.Code.POM_INTERNAL_ALLOCATION),
EXTERNAL("EXT", ContactType.Code.POM_EXTERNAL_ALLOCATION)
}
}

@Entity
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ class ReferenceDataSet(
val id: Long
) {
enum class Code(val value: String) {
KEY_DATE_TYPE("THROUGHCARE DATE TYPE"), POM_ALLOCATION_REASON("POM ALLOCATION REASON")
KEY_DATE_TYPE("THROUGHCARE DATE TYPE"),
POM_ALLOCATION_REASON("POM ALLOCATION REASON")
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package uk.gov.justice.digital.hmpps.services

import org.springframework.stereotype.Service
import uk.gov.justice.digital.hmpps.integrations.delius.contact.entity.Contact
import uk.gov.justice.digital.hmpps.integrations.delius.contact.entity.ContactRepository
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 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)
val type = contactTypeRepository.getByCode(typeCode.value)
contactRepository.save(
Contact(
personId,
type,
dateTime.toLocalDate(),
dateTime,
com.providerId,
com.team.id,
com.staff.id
)
)
}
}
Loading

0 comments on commit 20d8f69

Please sign in to comment.