From 13a82885478f68a3fad8be9d9fa385ee360c8049 Mon Sep 17 00:00:00 2001 From: Anthony Britton Date: Mon, 25 Sep 2023 10:21:55 +0100 Subject: [PATCH] PI-1484 - update and refactor existing API --- .../justice/digital/hmpps/data/DataLoader.kt | 15 ++------ .../hmpps/data/generator/PersonGenerator.kt | 16 ++------- .../justice/digital/hmpps/IntegrationTest.kt | 10 +----- .../{ResponsibleManager.kt => Manager.kt} | 3 +- .../api/resource/ProbationCaseResource.kt | 6 ++-- ...ResponsibleOfficer.kt => PersonManager.kt} | 36 +++++++------------ .../entity/ResponsibleOfficerRepository.kt | 23 ------------ ...bleManagerService.kt => ManagerService.kt} | 31 ++++++++-------- .../service/ResponsibleManagerServiceTest.kt | 23 ++++++------ 9 files changed, 53 insertions(+), 110 deletions(-) rename projects/create-and-vary-a-licence-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/api/model/{ResponsibleManager.kt => Manager.kt} (91%) rename projects/create-and-vary-a-licence-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/integrations/delius/manager/entity/{ResponsibleOfficer.kt => PersonManager.kt} (77%) delete mode 100644 projects/create-and-vary-a-licence-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/integrations/delius/manager/entity/ResponsibleOfficerRepository.kt rename projects/create-and-vary-a-licence-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/service/{ResponsibleManagerService.kt => ManagerService.kt} (67%) diff --git a/projects/create-and-vary-a-licence-and-delius/src/dev/kotlin/uk/gov/justice/digital/hmpps/data/DataLoader.kt b/projects/create-and-vary-a-licence-and-delius/src/dev/kotlin/uk/gov/justice/digital/hmpps/data/DataLoader.kt index d4bfc3a046..eaf9c991e5 100644 --- a/projects/create-and-vary-a-licence-and-delius/src/dev/kotlin/uk/gov/justice/digital/hmpps/data/DataLoader.kt +++ b/projects/create-and-vary-a-licence-and-delius/src/dev/kotlin/uk/gov/justice/digital/hmpps/data/DataLoader.kt @@ -12,7 +12,6 @@ import uk.gov.justice.digital.hmpps.data.generator.ProviderGenerator import uk.gov.justice.digital.hmpps.data.generator.StaffGenerator import uk.gov.justice.digital.hmpps.data.generator.UserGenerator import uk.gov.justice.digital.hmpps.user.AuditUserRepository -import java.time.ZonedDateTime @Component @ConditionalOnProperty("seed.database") @@ -38,18 +37,8 @@ class DataLoader( entityManager.persist(PersonGenerator.DEFAULT_PERSON) entityManager.persist(PersonGenerator.DEFAULT_CM) - entityManager.persist(PersonGenerator.DEFAULT_RO) - val person = PersonGenerator.generatePerson("N123456") - val cm = PersonGenerator.generateManager() - entityManager.persist(person) - entityManager.persist(cm) - entityManager.persist( - PersonGenerator.generateResponsibleOfficer( - person, - cm, - endDate = ZonedDateTime.now().minusMinutes(20) - ) - ) + val person = PersonGenerator.generatePerson("N123456").also(entityManager::persist) + PersonGenerator.generateManager(person).also(entityManager::persist) } } diff --git a/projects/create-and-vary-a-licence-and-delius/src/dev/kotlin/uk/gov/justice/digital/hmpps/data/generator/PersonGenerator.kt b/projects/create-and-vary-a-licence-and-delius/src/dev/kotlin/uk/gov/justice/digital/hmpps/data/generator/PersonGenerator.kt index cc44bb54d3..e941bb3ffa 100644 --- a/projects/create-and-vary-a-licence-and-delius/src/dev/kotlin/uk/gov/justice/digital/hmpps/data/generator/PersonGenerator.kt +++ b/projects/create-and-vary-a-licence-and-delius/src/dev/kotlin/uk/gov/justice/digital/hmpps/data/generator/PersonGenerator.kt @@ -1,17 +1,14 @@ package uk.gov.justice.digital.hmpps.data.generator import uk.gov.justice.digital.hmpps.integrations.delius.manager.entity.PersonManager -import uk.gov.justice.digital.hmpps.integrations.delius.manager.entity.ResponsibleOfficer import uk.gov.justice.digital.hmpps.integrations.delius.person.entity.Person import uk.gov.justice.digital.hmpps.integrations.delius.provider.entity.Provider import uk.gov.justice.digital.hmpps.integrations.delius.provider.entity.Staff import uk.gov.justice.digital.hmpps.integrations.delius.provider.entity.Team -import java.time.ZonedDateTime object PersonGenerator { val DEFAULT_PERSON = generatePerson("T123456") - val DEFAULT_CM = generateManager() - val DEFAULT_RO = generateResponsibleOfficer() + val DEFAULT_CM = generateManager(DEFAULT_PERSON) fun generatePerson( crn: String, @@ -24,19 +21,12 @@ object PersonGenerator { ) fun generateManager( + person: Person, provider: Provider = ProviderGenerator.DEFAULT_PROVIDER, team: Team = ProviderGenerator.DEFAULT_TEAM, staff: Staff = StaffGenerator.DEFAULT, softDeleted: Boolean = false, active: Boolean = true, id: Long = IdGenerator.getAndIncrement() - ) = PersonManager(provider, team, staff, softDeleted, active, id) - - fun generateResponsibleOfficer( - person: Person = DEFAULT_PERSON, - communityManager: PersonManager = DEFAULT_CM, - startDate: ZonedDateTime = ZonedDateTime.now().minusDays(7), - endDate: ZonedDateTime? = null, - id: Long = IdGenerator.getAndIncrement() - ) = ResponsibleOfficer(person, communityManager, startDate, endDate, id) + ) = PersonManager(person, provider, team, staff, softDeleted, active, id) } diff --git a/projects/create-and-vary-a-licence-and-delius/src/integrationTest/kotlin/uk/gov/justice/digital/hmpps/IntegrationTest.kt b/projects/create-and-vary-a-licence-and-delius/src/integrationTest/kotlin/uk/gov/justice/digital/hmpps/IntegrationTest.kt index 1572b8fc74..d0e9a4eb99 100644 --- a/projects/create-and-vary-a-licence-and-delius/src/integrationTest/kotlin/uk/gov/justice/digital/hmpps/IntegrationTest.kt +++ b/projects/create-and-vary-a-licence-and-delius/src/integrationTest/kotlin/uk/gov/justice/digital/hmpps/IntegrationTest.kt @@ -43,16 +43,8 @@ internal class IntegrationTest { assertThat( manager, equalTo( - PersonGenerator.DEFAULT_RO.asManager().copy(username = "john-smith", email = "john.smith@moj.gov.uk") + PersonGenerator.DEFAULT_CM.asManager().copy(username = "john-smith", email = "john.smith@moj.gov.uk") ) ) } - - @Test - fun `returns 204 if active no responsible community officer`() { - mockMvc.perform( - get("/probation-case/N123456/responsible-community-manager") - .withOAuth2Token(wireMockServer) - ).andExpect(status().isNoContent) - } } diff --git a/projects/create-and-vary-a-licence-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/api/model/ResponsibleManager.kt b/projects/create-and-vary-a-licence-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/api/model/Manager.kt similarity index 91% rename from projects/create-and-vary-a-licence-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/api/model/ResponsibleManager.kt rename to projects/create-and-vary-a-licence-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/api/model/Manager.kt index aad9cf93ec..0882f01081 100644 --- a/projects/create-and-vary-a-licence-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/api/model/ResponsibleManager.kt +++ b/projects/create-and-vary-a-licence-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/api/model/Manager.kt @@ -6,7 +6,8 @@ data class Manager( val provider: Provider, val team: Team, val username: String?, - val email: String? + val email: String?, + val unallocated: Boolean ) data class Name(val forename: String, val middleName: String?, val surname: String) diff --git a/projects/create-and-vary-a-licence-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/api/resource/ProbationCaseResource.kt b/projects/create-and-vary-a-licence-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/api/resource/ProbationCaseResource.kt index a684ab5eef..611b60e892 100644 --- a/projects/create-and-vary-a-licence-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/api/resource/ProbationCaseResource.kt +++ b/projects/create-and-vary-a-licence-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/api/resource/ProbationCaseResource.kt @@ -7,15 +7,15 @@ import org.springframework.web.bind.annotation.PathVariable import org.springframework.web.bind.annotation.RequestMapping import org.springframework.web.bind.annotation.RestController import uk.gov.justice.digital.hmpps.api.model.Manager -import uk.gov.justice.digital.hmpps.service.ResponsibleManagerService +import uk.gov.justice.digital.hmpps.service.ManagerService @RestController @RequestMapping("probation-case/{crn}") -class ProbationCaseResource(private val responsibleManagerService: ResponsibleManagerService) { +class ProbationCaseResource(private val responsibleManagerService: ManagerService) { @PreAuthorize("hasRole('CVL_CONTEXT')") @GetMapping("responsible-community-manager") fun handle(@PathVariable crn: String): ResponseEntity = - responsibleManagerService.findResponsibleCommunityManager(crn)?.let { + responsibleManagerService.findCommunityManager(crn)?.let { ResponseEntity.ok().body(it) } ?: ResponseEntity.noContent().build() } diff --git a/projects/create-and-vary-a-licence-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/integrations/delius/manager/entity/ResponsibleOfficer.kt b/projects/create-and-vary-a-licence-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/integrations/delius/manager/entity/PersonManager.kt similarity index 77% rename from projects/create-and-vary-a-licence-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/integrations/delius/manager/entity/ResponsibleOfficer.kt rename to projects/create-and-vary-a-licence-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/integrations/delius/manager/entity/PersonManager.kt index 951e14a1a8..f434e53d4c 100644 --- a/projects/create-and-vary-a-licence-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/integrations/delius/manager/entity/ResponsibleOfficer.kt +++ b/projects/create-and-vary-a-licence-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/integrations/delius/manager/entity/PersonManager.kt @@ -8,40 +8,23 @@ import jakarta.persistence.ManyToOne import jakarta.persistence.Table import org.hibernate.annotations.Immutable import org.hibernate.annotations.Where +import org.springframework.data.jpa.repository.EntityGraph +import org.springframework.data.jpa.repository.JpaRepository import uk.gov.justice.digital.hmpps.integrations.delius.person.entity.Person import uk.gov.justice.digital.hmpps.integrations.delius.provider.entity.Provider import uk.gov.justice.digital.hmpps.integrations.delius.provider.entity.Staff import uk.gov.justice.digital.hmpps.integrations.delius.provider.entity.Team -import java.time.ZonedDateTime @Immutable @Entity -@Table(name = "responsible_officer") -class ResponsibleOfficer( +@Table(name = "offender_manager") +@Where(clause = "soft_deleted = 0 and active_flag = 1") +class PersonManager( @ManyToOne @JoinColumn(name = "offender_id") val person: Person, - @ManyToOne - @JoinColumn(name = "offender_manager_id") - val communityManager: PersonManager, - - val startDate: ZonedDateTime = ZonedDateTime.now(), - - val endDate: ZonedDateTime? = null, - - @Id - @Column(name = "responsible_officer_id") - private val id: Long -) - -@Immutable -@Entity -@Table(name = "offender_manager") -@Where(clause = "soft_deleted = 0 and active_flag = 1") -class PersonManager( - @ManyToOne @JoinColumn(name = "probation_area_id") val provider: Provider, @@ -63,4 +46,11 @@ class PersonManager( @Id @Column(name = "offender_manager_id") val id: Long -) +) { + fun isUnallocated() = staff.code.endsWith("U") +} + +interface PersonManagerRepository : JpaRepository { + @EntityGraph(attributePaths = ["person", "provider", "team", "staff.user"]) + fun findByPersonCrn(crn: String): PersonManager? +} diff --git a/projects/create-and-vary-a-licence-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/integrations/delius/manager/entity/ResponsibleOfficerRepository.kt b/projects/create-and-vary-a-licence-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/integrations/delius/manager/entity/ResponsibleOfficerRepository.kt deleted file mode 100644 index 187e16e0ae..0000000000 --- a/projects/create-and-vary-a-licence-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/integrations/delius/manager/entity/ResponsibleOfficerRepository.kt +++ /dev/null @@ -1,23 +0,0 @@ -package uk.gov.justice.digital.hmpps.integrations.delius.manager.entity - -import org.springframework.data.jpa.repository.JpaRepository -import org.springframework.data.jpa.repository.Query - -interface ResponsibleOfficerRepository : JpaRepository { - @Query( - """ - select ro from ResponsibleOfficer ro - join ro.person p - join fetch ro.communityManager cm - join fetch cm.provider - join fetch cm.team t - join fetch t.district d - join fetch d.borough b - join fetch cm.staff s - left join fetch s.user - where p.crn = :crn - and ro.endDate is null - """ - ) - fun findResponsibleOfficer(crn: String): ResponsibleOfficer? -} diff --git a/projects/create-and-vary-a-licence-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/service/ResponsibleManagerService.kt b/projects/create-and-vary-a-licence-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/service/ManagerService.kt similarity index 67% rename from projects/create-and-vary-a-licence-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/service/ResponsibleManagerService.kt rename to projects/create-and-vary-a-licence-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/service/ManagerService.kt index 087e90369b..1fd843772d 100644 --- a/projects/create-and-vary-a-licence-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/service/ResponsibleManagerService.kt +++ b/projects/create-and-vary-a-licence-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/service/ManagerService.kt @@ -4,8 +4,8 @@ import org.springframework.ldap.core.LdapTemplate import org.springframework.stereotype.Service import uk.gov.justice.digital.hmpps.api.model.Manager import uk.gov.justice.digital.hmpps.api.model.Name -import uk.gov.justice.digital.hmpps.integrations.delius.manager.entity.ResponsibleOfficer -import uk.gov.justice.digital.hmpps.integrations.delius.manager.entity.ResponsibleOfficerRepository +import uk.gov.justice.digital.hmpps.integrations.delius.manager.entity.PersonManager +import uk.gov.justice.digital.hmpps.integrations.delius.manager.entity.PersonManagerRepository import uk.gov.justice.digital.hmpps.integrations.delius.provider.entity.Borough import uk.gov.justice.digital.hmpps.integrations.delius.provider.entity.District import uk.gov.justice.digital.hmpps.integrations.delius.provider.entity.Provider @@ -14,26 +14,27 @@ import uk.gov.justice.digital.hmpps.integrations.delius.provider.entity.Team import uk.gov.justice.digital.hmpps.ldap.findEmailByUsername @Service -class ResponsibleManagerService( - private val responsibleOfficerRepository: ResponsibleOfficerRepository, - private val ldapTemplate: LdapTemplate +class ManagerService( + private val ldapTemplate: LdapTemplate, + private val personManagerRepository: PersonManagerRepository ) { - fun findResponsibleCommunityManager(crn: String): Manager? = - responsibleOfficerRepository.findResponsibleOfficer(crn)?.let { ro -> - ro.communityManager.staff.user?.apply { + fun findCommunityManager(crn: String): Manager? = + personManagerRepository.findByPersonCrn(crn)?.let { ro -> + ro.staff.user?.apply { email = ldapTemplate.findEmailByUsername(username) } ro.asManager() } } -fun ResponsibleOfficer.asManager() = Manager( - communityManager.staff.code, - communityManager.staff.name(), - communityManager.provider.asProvider(), - communityManager.team.asTeam(), - communityManager.staff.user?.username, - communityManager.staff.user?.email +fun PersonManager.asManager() = Manager( + staff.code, + staff.name(), + provider.asProvider(), + team.asTeam(), + staff.user?.username, + staff.user?.email, + isUnallocated() ) fun Staff.name() = Name(forename, middleName, surname) diff --git a/projects/create-and-vary-a-licence-and-delius/src/test/kotlin/uk/gov/justice/digital/hmpps/service/ResponsibleManagerServiceTest.kt b/projects/create-and-vary-a-licence-and-delius/src/test/kotlin/uk/gov/justice/digital/hmpps/service/ResponsibleManagerServiceTest.kt index 3f8ebeae3b..7c4a950439 100644 --- a/projects/create-and-vary-a-licence-and-delius/src/test/kotlin/uk/gov/justice/digital/hmpps/service/ResponsibleManagerServiceTest.kt +++ b/projects/create-and-vary-a-licence-and-delius/src/test/kotlin/uk/gov/justice/digital/hmpps/service/ResponsibleManagerServiceTest.kt @@ -15,27 +15,30 @@ import org.springframework.ldap.core.AttributesMapper import org.springframework.ldap.core.LdapTemplate import uk.gov.justice.digital.hmpps.data.generator.PersonGenerator import uk.gov.justice.digital.hmpps.data.generator.StaffGenerator -import uk.gov.justice.digital.hmpps.integrations.delius.manager.entity.ResponsibleOfficerRepository +import uk.gov.justice.digital.hmpps.integrations.delius.manager.entity.PersonManagerRepository @ExtendWith(MockitoExtension::class) internal class ResponsibleManagerServiceTest { - @Mock lateinit var responsibleOfficerRepository: ResponsibleOfficerRepository - @Mock lateinit var ldapTemplate: LdapTemplate + @Mock + lateinit var personManagerRepository: PersonManagerRepository - @InjectMocks lateinit var service: ResponsibleManagerService + @Mock + lateinit var ldapTemplate: LdapTemplate + + @InjectMocks + lateinit var service: ManagerService @Test fun `does not call ldap when no staff user`() { - val crn = "L123456" + val person = PersonGenerator.generatePerson("L123456") val staff = StaffGenerator.generateStaff("NoLdap", "No", "User") - val cm = PersonGenerator.generateManager(staff = staff) - val ro = PersonGenerator.generateResponsibleOfficer(communityManager = cm) + val cm = PersonGenerator.generateManager(person, staff = staff) - whenever(responsibleOfficerRepository.findResponsibleOfficer(crn)).thenReturn(ro) + whenever(personManagerRepository.findByPersonCrn(person.crn)).thenReturn(cm) - val res = service.findResponsibleCommunityManager(crn) - assertThat(res, equalTo(ro.asManager())) + val res = service.findCommunityManager(person.crn) + assertThat(res, equalTo(cm.asManager())) verify(ldapTemplate, never()).search(any(), any>()) } }