Skip to content

Commit

Permalink
PI-2534 Add endpoint to get staff by code (#4248)
Browse files Browse the repository at this point in the history
  • Loading branch information
marcus-bcl authored Sep 19, 2024
1 parent 423ac2f commit 899dd71
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -155,4 +155,14 @@ internal class IntegrationTest {

mockMvc.perform(get("/staff/byid/9999999").withToken()).andExpect(status().isNotFound)
}

@Test
fun `returns staff by code`() {
mockMvc
.perform(get("/staff/bycode/${StaffGenerator.DEFAULT.code}").withToken())
.andExpect(status().isOk)
.andExpect(jsonPath("$.username", equalTo("john-smith")))
.andExpect(jsonPath("$.email", equalTo("[email protected]")))
.andExpect(jsonPath("$.telephoneNumber", equalTo("10101010101")))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,34 +10,32 @@ import uk.gov.justice.digital.hmpps.service.StaffService

@RestController
@RequestMapping("staff")
@PreAuthorize("hasRole('PROBATION_API__CVL__CASE_DETAIL')")
class StaffResource(
private val staffService: StaffService
) {
@PreAuthorize("hasRole('PROBATION_API__CVL__CASE_DETAIL')")
@GetMapping("/{username}")
fun findStaff(@PathVariable username: String): Staff = staffService.findStaff(username)

@PreAuthorize("hasRole('PROBATION_API__CVL__CASE_DETAIL')")
@GetMapping("/byid/{id}")
fun findStaff(@PathVariable id: Long): Staff = staffService.findStaffById(id)

@PreAuthorize("hasRole('PROBATION_API__CVL__CASE_DETAIL')")
@GetMapping("/bycode/{code}")
fun findStaffByCode(@PathVariable code: String): Staff = staffService.findStaffByCode(code)

@GetMapping("/{boroughCode}/pdu-head")
fun findPDUHead(@PathVariable boroughCode: String): List<PDUHead> = staffService.findPDUHeads(boroughCode)

@PreAuthorize("hasRole('PROBATION_API__CVL__CASE_DETAIL')")
@PostMapping
fun findStaffForUsernames(@RequestBody usernames: List<String>): List<StaffName> =
staffService.findStaffForUsernames(
usernames
)

@PreAuthorize("hasRole('PROBATION_API__CVL__CASE_DETAIL')")
@GetMapping("/byid/{id}/caseload/managed-offenders")
fun getManagedOffenders(@PathVariable id: Long): List<ManagedOffender> =
staffService.getManagedOffendersByStaffId(id)

@PreAuthorize("hasRole('PROBATION_API__CVL__CASE_DETAIL')")
@GetMapping("/{staffCode}/caseload/managed-offenders")
fun getManagedOffenders(@PathVariable staffCode: String): List<ManagedOffender> =
staffService.getManagedOffenders(staffCode)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@ interface StaffRepository : JpaRepository<Staff, Long> {
@EntityGraph(attributePaths = ["user", "teams"])
fun findByUserUsername(username: String): Staff?

@EntityGraph(attributePaths = ["user", "teams"])
fun findByCode(code: String): Staff?

@EntityGraph(attributePaths = ["user", "teams"])
override fun findById(id: Long): Optional<Staff>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,13 @@ class StaffService(
private val boroughRepository: BoroughRepository,
private val caseloadRepository: CaseloadRepository,
) {
fun findStaff(username: String): Staff =
staffRepository.findByUserUsername(username)
?.let { ldapTemplate.populateUserDetails(it).asStaff() }
?: throw NotFoundException("Staff", "username", username)
fun findStaff(username: String): Staff = staffRepository.findByUserUsername(username)
?.let { ldapTemplate.populateUserDetails(it).asStaff() }
?: throw NotFoundException("Staff", "username", username)

fun findStaffByCode(code: String): Staff = staffRepository.findByCode(code)
?.let { ldapTemplate.populateUserDetails(it).asStaff() }
?: throw NotFoundException("Staff", "code", code)

fun findStaffById(id: Long): Staff {
val staff = staffRepository.findById(id).orElseThrow { NotFoundException("Staff", "id", id) }
Expand Down

0 comments on commit 899dd71

Please sign in to comment.