Skip to content

Commit

Permalink
PI-1531 Added get PDU heads endpoint (#2373)
Browse files Browse the repository at this point in the history
  • Loading branch information
stevomcallister authored Oct 5, 2023
1 parent 9bd722d commit b9f4543
Show file tree
Hide file tree
Showing 10 changed files with 127 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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<Staff> = listOf(StaffGenerator.PDUHEAD)
) = Borough(code, description, id, pduHeads, DEFAULT_PROVIDER)

fun generateDistrict(
code: String,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Team> = listOf(),
middleName: String? = null,
user: StaffUser? = null,
id: Long = IdGenerator.getAndIncrement(),
teams: List<Team> = listOf(ProviderGenerator.DEFAULT_TEAM)
id: Long = IdGenerator.getAndIncrement()
) = Staff(code, forename, surname, middleName, user, id, teams).apply { user?.set("staff", this) }

fun generateStaffUser(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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: [email protected]
mail: [email protected]

dn: cn=bob-smith,ou=Users,dc=moj,dc=com
objectclass: top
objectclass: inetOrgPerson
cn: bob-smith
sn: Staff
mail: [email protected]
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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<List<PDUHead>>(res)
assertThat(
pduHeads,
equalTo(
listOf(
StaffGenerator.PDUHEAD.asPDUHead().copy(email = "[email protected]")
)
)
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,8 @@ data class Staff(
val email: String?,
val unallocated: Boolean
)

data class PDUHead(
val name: Name,
val email: String?
)
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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<PDUHead> = staffService.findPDUHeads(boroughCode)
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -41,6 +50,8 @@ class Team(
val id: Long
)

interface TeamRepository : JpaRepository<Team, Long>

@Immutable
@Entity
@Table(name = "district")
Expand Down Expand Up @@ -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<Staff>,

@JoinColumn(name = "PROBATION_AREA_ID")
@OneToOne
val provider: Provider
)

interface BoroughRepository : JpaRepository<Borough?, Long?> {
@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?
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class Staff(
joinColumns = [JoinColumn(name = "staff_id")],
inverseJoinColumns = [JoinColumn(name = "team_id")]
)
val teams: List<Team>
val teams: List<Team>?

) {
fun isUnallocated() = code.endsWith("U")
Expand All @@ -66,6 +66,6 @@ class StaffUser(
}

interface StaffRepository : JpaRepository<Staff, Long> {
@EntityGraph(attributePaths = ["user"])
@EntityGraph(attributePaths = ["user", "teams"])
fun findByUserUsername(username: String): Staff?
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 ->
Expand All @@ -19,13 +24,28 @@ class StaffService(
}
staff.asStaff()
} ?: throw NotFoundException("Staff", "username", username)

fun findPDUHeads(boroughCode: String): List<PDUHead> =
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
)

0 comments on commit b9f4543

Please sign in to comment.