Skip to content

Commit

Permalink
PI-1484 - update and refactor existing API
Browse files Browse the repository at this point in the history
  • Loading branch information
anthony-britton-moj committed Sep 25, 2023
1 parent 75a494e commit 13a8288
Show file tree
Hide file tree
Showing 9 changed files with 53 additions and 110 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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)
}
}
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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)
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,8 @@ internal class IntegrationTest {
assertThat(
manager,
equalTo(
PersonGenerator.DEFAULT_RO.asManager().copy(username = "john-smith", email = "[email protected]")
PersonGenerator.DEFAULT_CM.asManager().copy(username = "john-smith", email = "[email protected]")
)
)
}

@Test
fun `returns 204 if active no responsible community officer`() {
mockMvc.perform(
get("/probation-case/N123456/responsible-community-manager")
.withOAuth2Token(wireMockServer)
).andExpect(status().isNoContent)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Manager> =
responsibleManagerService.findResponsibleCommunityManager(crn)?.let {
responsibleManagerService.findCommunityManager(crn)?.let {
ResponseEntity.ok().body(it)
} ?: ResponseEntity.noContent().build()
}
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -63,4 +46,11 @@ class PersonManager(
@Id
@Column(name = "offender_manager_id")
val id: Long
)
) {
fun isUnallocated() = staff.code.endsWith("U")
}

interface PersonManagerRepository : JpaRepository<PersonManager, Long> {
@EntityGraph(attributePaths = ["person", "provider", "team", "staff.user"])
fun findByPersonCrn(crn: String): PersonManager?
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<AttributesMapper<String?>>())
}
}

0 comments on commit 13a8288

Please sign in to comment.