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 1dadb51a63..516ae6f853 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 @@ -29,11 +29,23 @@ class DataLoader( @Transactional override fun onApplicationEvent(are: ApplicationReadyEvent) { entityManager.persist(ProviderGenerator.DEFAULT_PROVIDER) + entityManager.persist(StaffGenerator.PDUHEAD) + entityManager.persist(StaffGenerator.DEFAULT_PDUSTAFF_USER) entityManager.persist(ProviderGenerator.DEFAULT_BOROUGH) entityManager.persist(ProviderGenerator.DEFAULT_DISTRICT) entityManager.persist(ProviderGenerator.DEFAULT_TEAM) + StaffGenerator.DEFAULT = StaffGenerator.generateStaff( + StaffGenerator.DEFAULT.code, + StaffGenerator.DEFAULT.forename, + StaffGenerator.DEFAULT.surname, + listOf(ProviderGenerator.DEFAULT_TEAM), + StaffGenerator.DEFAULT.middleName, + StaffGenerator.DEFAULT.user, + StaffGenerator.DEFAULT.id + ) entityManager.persist(StaffGenerator.DEFAULT) + entityManager.persist(StaffGenerator.DEFAULT_STAFF_USER) entityManager.persist(PersonGenerator.DEFAULT_PERSON) diff --git a/projects/create-and-vary-a-licence-and-delius/src/dev/kotlin/uk/gov/justice/digital/hmpps/data/generator/ProviderGenerator.kt b/projects/create-and-vary-a-licence-and-delius/src/dev/kotlin/uk/gov/justice/digital/hmpps/data/generator/ProviderGenerator.kt index a14e01fb45..5cd96d42b0 100644 --- a/projects/create-and-vary-a-licence-and-delius/src/dev/kotlin/uk/gov/justice/digital/hmpps/data/generator/ProviderGenerator.kt +++ b/projects/create-and-vary-a-licence-and-delius/src/dev/kotlin/uk/gov/justice/digital/hmpps/data/generator/ProviderGenerator.kt @@ -3,7 +3,9 @@ package uk.gov.justice.digital.hmpps.data.generator 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 +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.LocalDate object ProviderGenerator { val DEFAULT_PROVIDER = generateProvider("N01") @@ -14,14 +16,16 @@ object ProviderGenerator { fun generateProvider( code: String, description: String = "Description of $code", - id: Long = IdGenerator.getAndIncrement() - ) = Provider(code, description, id) + id: Long = IdGenerator.getAndIncrement(), + endDate: LocalDate? = null + ) = Provider(code, description, id, endDate) fun generateBorough( code: String, description: String = "Description of $code", - id: Long = IdGenerator.getAndIncrement() - ) = Borough(code, description, id) + id: Long = IdGenerator.getAndIncrement(), + pduHeads: List = listOf(StaffGenerator.PDUHEAD) + ) = Borough(code, description, id, pduHeads, DEFAULT_PROVIDER) fun generateDistrict( code: String, diff --git a/projects/create-and-vary-a-licence-and-delius/src/dev/kotlin/uk/gov/justice/digital/hmpps/data/generator/StaffGenerator.kt b/projects/create-and-vary-a-licence-and-delius/src/dev/kotlin/uk/gov/justice/digital/hmpps/data/generator/StaffGenerator.kt index 93050f504a..6a64ca5132 100644 --- a/projects/create-and-vary-a-licence-and-delius/src/dev/kotlin/uk/gov/justice/digital/hmpps/data/generator/StaffGenerator.kt +++ b/projects/create-and-vary-a-licence-and-delius/src/dev/kotlin/uk/gov/justice/digital/hmpps/data/generator/StaffGenerator.kt @@ -6,17 +6,19 @@ import uk.gov.justice.digital.hmpps.integrations.delius.provider.entity.Team import uk.gov.justice.digital.hmpps.set object StaffGenerator { - val DEFAULT = generateStaff("N01BDT1", "John", "Smith") + val PDUHEAD = generateStaff("N01BDT2", "Bob", "Smith") + val DEFAULT_PDUSTAFF_USER = generateStaffUser("bob-smith", PDUHEAD) + var DEFAULT = generateStaff("N01BDT1", "John", "Smith") val DEFAULT_STAFF_USER = generateStaffUser("john-smith", DEFAULT) fun generateStaff( code: String, forename: String, surname: String, + teams: List = listOf(), middleName: String? = null, user: StaffUser? = null, - id: Long = IdGenerator.getAndIncrement(), - teams: List = listOf(ProviderGenerator.DEFAULT_TEAM) + id: Long = IdGenerator.getAndIncrement() ) = Staff(code, forename, surname, middleName, user, id, teams).apply { user?.set("staff", this) } fun generateStaffUser( diff --git a/projects/create-and-vary-a-licence-and-delius/src/dev/resources/schema.ldif b/projects/create-and-vary-a-licence-and-delius/src/dev/resources/schema.ldif index 2047485182..72f78c7293 100644 --- a/projects/create-and-vary-a-licence-and-delius/src/dev/resources/schema.ldif +++ b/projects/create-and-vary-a-licence-and-delius/src/dev/resources/schema.ldif @@ -8,9 +8,16 @@ objectclass: top objectclass: organizationalUnit ou: Users -dn: cn=DefaultStaff,ou=Users,dc=moj,dc=com +dn: cn=john-smith,ou=Users,dc=moj,dc=com objectclass: top objectclass: inetOrgPerson cn: john-smith sn: Staff -mail: john.smith@moj.gov.uk \ No newline at end of file +mail: john.smith@moj.gov.uk + +dn: cn=bob-smith,ou=Users,dc=moj,dc=com +objectclass: top +objectclass: inetOrgPerson +cn: bob-smith +sn: Staff +mail: bob.smith@moj.gov.uk \ No newline at end of file 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 1298fc4035..5a5bdf03c2 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 @@ -15,11 +15,14 @@ import org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get import org.springframework.test.web.servlet.result.MockMvcResultMatchers.status import uk.gov.justice.digital.hmpps.api.model.Address import uk.gov.justice.digital.hmpps.api.model.Manager +import uk.gov.justice.digital.hmpps.api.model.PDUHead import uk.gov.justice.digital.hmpps.api.model.Staff import uk.gov.justice.digital.hmpps.data.generator.PersonGenerator +import uk.gov.justice.digital.hmpps.data.generator.ProviderGenerator import uk.gov.justice.digital.hmpps.data.generator.StaffGenerator import uk.gov.justice.digital.hmpps.security.withOAuth2Token import uk.gov.justice.digital.hmpps.service.asManager +import uk.gov.justice.digital.hmpps.service.asPDUHead import uk.gov.justice.digital.hmpps.service.asStaff import java.time.LocalDate @@ -116,4 +119,24 @@ internal class IntegrationTest { ) ) } + + @Test + fun `returns pdu heads`() { + val boroughCode = ProviderGenerator.DEFAULT_BOROUGH.code + + val res = mockMvc + .perform(get("/staff/$boroughCode/pdu-head").withOAuth2Token(wireMockServer)) + .andExpect(status().isOk) + .andReturn().response.contentAsString + + val pduHeads = objectMapper.readValue>(res) + assertThat( + pduHeads, + equalTo( + listOf( + StaffGenerator.PDUHEAD.asPDUHead().copy(email = "bob.smith@moj.gov.uk") + ) + ) + ) + } } diff --git a/projects/create-and-vary-a-licence-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/api/model/Staff.kt b/projects/create-and-vary-a-licence-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/api/model/Staff.kt index ec5f94097b..c733433996 100644 --- a/projects/create-and-vary-a-licence-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/api/model/Staff.kt +++ b/projects/create-and-vary-a-licence-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/api/model/Staff.kt @@ -8,3 +8,8 @@ data class Staff( val email: String?, val unallocated: Boolean ) + +data class PDUHead( + val name: Name, + val email: String? +) diff --git a/projects/create-and-vary-a-licence-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/api/resource/StaffResource.kt b/projects/create-and-vary-a-licence-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/api/resource/StaffResource.kt index 6fee441864..508f8b3481 100644 --- a/projects/create-and-vary-a-licence-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/api/resource/StaffResource.kt +++ b/projects/create-and-vary-a-licence-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/api/resource/StaffResource.kt @@ -5,6 +5,7 @@ import org.springframework.web.bind.annotation.GetMapping 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.PDUHead import uk.gov.justice.digital.hmpps.api.model.Staff import uk.gov.justice.digital.hmpps.service.StaffService @@ -16,4 +17,8 @@ class StaffResource( @PreAuthorize("hasRole('CVL_CONTEXT')") @GetMapping("/{username}") fun findStaff(@PathVariable username: String): Staff = staffService.findStaff(username) + + @PreAuthorize("hasRole('CVL_CONTEXT')") + @GetMapping("/{boroughCode}/pdu-head") + fun findPDUHead(@PathVariable boroughCode: String): List = staffService.findPDUHeads(boroughCode) } diff --git a/projects/create-and-vary-a-licence-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/integrations/delius/provider/entity/Provider.kt b/projects/create-and-vary-a-licence-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/integrations/delius/provider/entity/Provider.kt index 2220b7f020..e62a9d1220 100644 --- a/projects/create-and-vary-a-licence-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/integrations/delius/provider/entity/Provider.kt +++ b/projects/create-and-vary-a-licence-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/integrations/delius/provider/entity/Provider.kt @@ -4,9 +4,15 @@ import jakarta.persistence.Column import jakarta.persistence.Entity import jakarta.persistence.Id import jakarta.persistence.JoinColumn +import jakarta.persistence.JoinTable +import jakarta.persistence.ManyToMany import jakarta.persistence.ManyToOne +import jakarta.persistence.OneToOne import jakarta.persistence.Table import org.hibernate.annotations.Immutable +import org.springframework.data.jpa.repository.JpaRepository +import org.springframework.data.jpa.repository.Query +import java.time.LocalDate @Immutable @Entity @@ -19,7 +25,10 @@ class Provider( @Id @Column(name = "probation_area_id") - val id: Long + val id: Long, + + @Column(name = "end_date") + var endDate: LocalDate? = null ) @Immutable @@ -41,6 +50,8 @@ class Team( val id: Long ) +interface TeamRepository : JpaRepository + @Immutable @Entity @Table(name = "district") @@ -72,5 +83,28 @@ class Borough( @Id @Column(name = "borough_id") - val id: Long + val id: Long, + + @ManyToMany + @JoinTable( + name = "r_level_2_head_of_level_2", + joinColumns = [JoinColumn(name = "borough_id")], + inverseJoinColumns = [JoinColumn(name = "staff_id")] + ) + val pduHeads: List, + + @JoinColumn(name = "PROBATION_AREA_ID") + @OneToOne + val provider: Provider ) + +interface BoroughRepository : JpaRepository { + @Query( + """ + select b from Borough b + where b.code = :code + and (b.provider.endDate is null or b.provider.endDate > current_date) + """ + ) + fun findActiveByCode(code: String): Borough? +} diff --git a/projects/create-and-vary-a-licence-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/integrations/delius/provider/entity/Staff.kt b/projects/create-and-vary-a-licence-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/integrations/delius/provider/entity/Staff.kt index 34a34033d5..e32be119cc 100644 --- a/projects/create-and-vary-a-licence-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/integrations/delius/provider/entity/Staff.kt +++ b/projects/create-and-vary-a-licence-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/integrations/delius/provider/entity/Staff.kt @@ -39,7 +39,7 @@ class Staff( joinColumns = [JoinColumn(name = "staff_id")], inverseJoinColumns = [JoinColumn(name = "team_id")] ) - val teams: List + val teams: List? ) { fun isUnallocated() = code.endsWith("U") @@ -66,6 +66,6 @@ class StaffUser( } interface StaffRepository : JpaRepository { - @EntityGraph(attributePaths = ["user"]) + @EntityGraph(attributePaths = ["user", "teams"]) fun findByUserUsername(username: String): Staff? } diff --git a/projects/create-and-vary-a-licence-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/service/StaffService.kt b/projects/create-and-vary-a-licence-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/service/StaffService.kt index cb4febfc81..2ad6d69505 100644 --- a/projects/create-and-vary-a-licence-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/service/StaffService.kt +++ b/projects/create-and-vary-a-licence-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/service/StaffService.kt @@ -2,15 +2,20 @@ package uk.gov.justice.digital.hmpps.service import org.springframework.ldap.core.LdapTemplate import org.springframework.stereotype.Service +import uk.gov.justice.digital.hmpps.api.model.PDUHead import uk.gov.justice.digital.hmpps.api.model.Staff import uk.gov.justice.digital.hmpps.exception.NotFoundException +import uk.gov.justice.digital.hmpps.integrations.delius.provider.entity.BoroughRepository import uk.gov.justice.digital.hmpps.integrations.delius.provider.entity.StaffRepository +import uk.gov.justice.digital.hmpps.integrations.delius.provider.entity.TeamRepository import uk.gov.justice.digital.hmpps.ldap.findEmailByUsername @Service class StaffService( private val ldapTemplate: LdapTemplate, - private val staffRepository: StaffRepository + private val staffRepository: StaffRepository, + private val boroughRepository: BoroughRepository, + private val teamRepository: TeamRepository ) { fun findStaff(username: String): Staff = staffRepository.findByUserUsername(username)?.let { staff -> @@ -19,13 +24,28 @@ class StaffService( } staff.asStaff() } ?: throw NotFoundException("Staff", "username", username) + + fun findPDUHeads(boroughCode: String): List = + boroughRepository.findActiveByCode(boroughCode)?.pduHeads?.map { + it.let { pduHead -> + pduHead.user?.apply { + email = ldapTemplate.findEmailByUsername(username) + } + pduHead.asPDUHead() + } + } ?: listOf() } fun uk.gov.justice.digital.hmpps.integrations.delius.provider.entity.Staff.asStaff() = Staff( code, name(), - teams.map { it.asTeam() }, + teams?.map { it.asTeam() } ?: listOf(), user?.username, user?.email, isUnallocated() ) + +fun uk.gov.justice.digital.hmpps.integrations.delius.provider.entity.Staff.asPDUHead() = PDUHead( + name(), + user?.email +)