From d9d500496677ec68f753ecaef5cbb5530b0ab1ca Mon Sep 17 00:00:00 2001 From: stevomcallister <78762879+stevomcallister@users.noreply.github.com> Date: Fri, 6 Oct 2023 10:31:14 +0100 Subject: [PATCH] PI-1532 Added get staff for usernames endpoint (#2376) --- .../justice/digital/hmpps/IntegrationTest.kt | 29 +++++++++++++++++++ .../justice/digital/hmpps/api/model/Staff.kt | 5 ++++ .../hmpps/api/resource/StaffResource.kt | 7 +++++ .../delius/provider/entity/Staff.kt | 3 ++ .../digital/hmpps/service/StaffService.kt | 11 +++++++ 5 files changed, 55 insertions(+) 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 5a5bdf03c2..c3a9c933bd 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 @@ -12,11 +12,13 @@ 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 @@ -24,6 +26,7 @@ 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 @@ -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>(res) + assertThat( + staffNames, + equalTo( + listOf( + StaffGenerator.PDUHEAD.asStaffName(), + StaffGenerator.DEFAULT.asStaffName() + ) + ) + ) + } } 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 c733433996..206e0a42e5 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 @@ -13,3 +13,8 @@ data class PDUHead( val name: Name, val email: String? ) + +data class StaffName( + val name: Name, + val code: 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 508f8b3481..8959e0bde5 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 @@ -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 @@ -21,4 +24,8 @@ class StaffResource( @PreAuthorize("hasRole('CVL_CONTEXT')") @GetMapping("/{boroughCode}/pdu-head") fun findPDUHead(@PathVariable boroughCode: String): List = staffService.findPDUHeads(boroughCode) + + @PreAuthorize("hasRole('CVL_CONTEXT')") + @PostMapping + fun findStaffForUsernames(@RequestBody usernames: List): List = staffService.findStaffForUsernames(usernames) } 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 e32be119cc..9fbfb270b1 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 @@ -68,4 +68,7 @@ class StaffUser( interface StaffRepository : JpaRepository { @EntityGraph(attributePaths = ["user", "teams"]) fun findByUserUsername(username: String): Staff? + + @EntityGraph(attributePaths = ["user"]) + fun findByUserUsernameIn(usernames: List): List } 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 2ad6d69505..391e153ba8 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 @@ -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 @@ -34,6 +35,11 @@ class StaffService( pduHead.asPDUHead() } } ?: listOf() + + fun findStaffForUsernames(usernames: List): List = + staffRepository.findByUserUsernameIn(usernames).map { + it.asStaffName() + } } fun uk.gov.justice.digital.hmpps.integrations.delius.provider.entity.Staff.asStaff() = Staff( @@ -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 +)