Skip to content

Commit

Permalink
PI-1532 Added get staff for usernames endpoint (#2376)
Browse files Browse the repository at this point in the history
  • Loading branch information
stevomcallister authored Oct 6, 2023
1 parent b9f4543 commit d9d5004
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,21 @@ import org.springframework.boot.test.context.SpringBootTest
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT
import org.springframework.test.web.servlet.MockMvc
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post
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.api.model.StaffName
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 uk.gov.justice.digital.hmpps.service.asStaffName
import java.time.LocalDate

@AutoConfigureMockMvc
Expand Down Expand Up @@ -139,4 +142,30 @@ internal class IntegrationTest {
)
)
}

@Test
fun `returns staff names for usernames`() {
val usernames =
listOf(StaffGenerator.DEFAULT_PDUSTAFF_USER.username, StaffGenerator.DEFAULT_STAFF_USER.username)

val res = mockMvc
.perform(
post("/staff").withOAuth2Token(wireMockServer)
.contentType("application/json")
.content(objectMapper.writeValueAsString(usernames))
)
.andExpect(status().isOk)
.andReturn().response.contentAsString

val staffNames = objectMapper.readValue<List<StaffName>>(res)
assertThat(
staffNames,
equalTo(
listOf(
StaffGenerator.PDUHEAD.asStaffName(),
StaffGenerator.DEFAULT.asStaffName()
)
)
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,8 @@ data class PDUHead(
val name: Name,
val email: String?
)

data class StaffName(
val name: Name,
val code: String
)
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@ package uk.gov.justice.digital.hmpps.api.resource
import org.springframework.security.access.prepost.PreAuthorize
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.PathVariable
import org.springframework.web.bind.annotation.PostMapping
import org.springframework.web.bind.annotation.RequestBody
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.api.model.StaffName
import uk.gov.justice.digital.hmpps.service.StaffService

@RestController
Expand All @@ -21,4 +24,8 @@ class StaffResource(
@PreAuthorize("hasRole('CVL_CONTEXT')")
@GetMapping("/{boroughCode}/pdu-head")
fun findPDUHead(@PathVariable boroughCode: String): List<PDUHead> = staffService.findPDUHeads(boroughCode)

@PreAuthorize("hasRole('CVL_CONTEXT')")
@PostMapping
fun findStaffForUsernames(@RequestBody usernames: List<String>): List<StaffName> = staffService.findStaffForUsernames(usernames)
}
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,7 @@ class StaffUser(
interface StaffRepository : JpaRepository<Staff, Long> {
@EntityGraph(attributePaths = ["user", "teams"])
fun findByUserUsername(username: String): Staff?

@EntityGraph(attributePaths = ["user"])
fun findByUserUsernameIn(usernames: List<String>): List<Staff>
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ 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.api.model.StaffName
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
Expand Down Expand Up @@ -34,6 +35,11 @@ class StaffService(
pduHead.asPDUHead()
}
} ?: listOf()

fun findStaffForUsernames(usernames: List<String>): List<StaffName> =
staffRepository.findByUserUsernameIn(usernames).map {
it.asStaffName()
}
}

fun uk.gov.justice.digital.hmpps.integrations.delius.provider.entity.Staff.asStaff() = Staff(
Expand All @@ -49,3 +55,8 @@ fun uk.gov.justice.digital.hmpps.integrations.delius.provider.entity.Staff.asPDU
name(),
user?.email
)

fun uk.gov.justice.digital.hmpps.integrations.delius.provider.entity.Staff.asStaffName() = StaffName(
name(),
code
)

0 comments on commit d9d5004

Please sign in to comment.