From d7731f38067e87cc313096effad8935483bfd57d Mon Sep 17 00:00:00 2001 From: Marcus Aspin Date: Thu, 7 Nov 2024 16:52:03 +0000 Subject: [PATCH] PI-2622 Add ids to offences + add staff by code endpoint (#4403) * PI-2622 Add ids to offences + add staff by code endpoint * Add test + make username nullable --- .../justice/digital/hmpps/data/DataLoader.kt | 2 ++ .../hmpps/data/generator/StaffGenerator.kt | 1 + .../hmpps/StaffControllerIntegrationTest.kt | 22 +++++++++++++++ .../hmpps/controller/StaffController.kt | 15 +++++----- .../delius/person/offence/entity/Offence.kt | 28 +++++++++++++------ .../digital/hmpps/model/StaffResponse.kt | 2 +- .../digital/hmpps/service/StaffService.kt | 14 ++++++---- 7 files changed, 61 insertions(+), 23 deletions(-) diff --git a/projects/approved-premises-and-delius/src/dev/kotlin/uk/gov/justice/digital/hmpps/data/DataLoader.kt b/projects/approved-premises-and-delius/src/dev/kotlin/uk/gov/justice/digital/hmpps/data/DataLoader.kt index 64eba840df..d254ca2d3d 100644 --- a/projects/approved-premises-and-delius/src/dev/kotlin/uk/gov/justice/digital/hmpps/data/DataLoader.kt +++ b/projects/approved-premises-and-delius/src/dev/kotlin/uk/gov/justice/digital/hmpps/data/DataLoader.kt @@ -156,6 +156,8 @@ class DataLoader( staffRepository.save(StaffGenerator.CRU_WOMENS_ESTATE) staffUserRepository.save(StaffGenerator.CRU_WOMENS_ESTATE_USER) + staffRepository.save(StaffGenerator.STAFF_WITHOUT_USERNAME) + val personManagerStaff = StaffGenerator.generate(code = "N54A001") staffRepository.save(personManagerStaff) val person = PersonGenerator.DEFAULT diff --git a/projects/approved-premises-and-delius/src/dev/kotlin/uk/gov/justice/digital/hmpps/data/generator/StaffGenerator.kt b/projects/approved-premises-and-delius/src/dev/kotlin/uk/gov/justice/digital/hmpps/data/generator/StaffGenerator.kt index ac3223b28e..9d5c3879d8 100644 --- a/projects/approved-premises-and-delius/src/dev/kotlin/uk/gov/justice/digital/hmpps/data/generator/StaffGenerator.kt +++ b/projects/approved-premises-and-delius/src/dev/kotlin/uk/gov/justice/digital/hmpps/data/generator/StaffGenerator.kt @@ -25,6 +25,7 @@ object StaffGenerator { val CRU_WOMENS_ESTATE = generate( name = "CRU Womens Estate" ) + val STAFF_WITHOUT_USERNAME = generate() val DEFAULT_STAFF_USER = generateStaffUser("john-smith", DEFAULT_STAFF) val JIM_SNOW_USER = generateStaffUser("JIMSNOWLDAP", JIM_SNOW) diff --git a/projects/approved-premises-and-delius/src/integrationTest/kotlin/uk/gov/justice/digital/hmpps/StaffControllerIntegrationTest.kt b/projects/approved-premises-and-delius/src/integrationTest/kotlin/uk/gov/justice/digital/hmpps/StaffControllerIntegrationTest.kt index cadee9ab1a..8c83819a54 100644 --- a/projects/approved-premises-and-delius/src/integrationTest/kotlin/uk/gov/justice/digital/hmpps/StaffControllerIntegrationTest.kt +++ b/projects/approved-premises-and-delius/src/integrationTest/kotlin/uk/gov/justice/digital/hmpps/StaffControllerIntegrationTest.kt @@ -92,4 +92,26 @@ class StaffControllerIntegrationTest { assertThat(res.probationArea.description, equalTo(StaffGenerator.DEFAULT_STAFF.probationArea.description)) assertThat(res.active, equalTo(true)) } + + @Test + fun `get staff by code`() { + val staffCode = StaffGenerator.DEFAULT_STAFF.code + val res = mockMvc.perform(get("/staff?code=${staffCode}").withToken()) + .andExpect(status().isOk).andReturn().response.contentAsJson() + assertThat(res.username, equalTo(StaffGenerator.DEFAULT_STAFF.user!!.username)) + assertThat(res.name.surname, equalTo(StaffGenerator.DEFAULT_STAFF.surname)) + assertThat(res.name.forename, equalTo(StaffGenerator.DEFAULT_STAFF.forename)) + assertThat(res.code, equalTo(StaffGenerator.DEFAULT_STAFF.code)) + } + + @Test + fun `get staff without username by code`() { + val staffCode = StaffGenerator.STAFF_WITHOUT_USERNAME.code + val res = mockMvc.perform(get("/staff?code=${staffCode}").withToken()) + .andExpect(status().isOk).andReturn().response.contentAsJson() + assertThat(res.username, equalTo(null)) + assertThat(res.name.surname, equalTo(StaffGenerator.STAFF_WITHOUT_USERNAME.surname)) + assertThat(res.name.forename, equalTo(StaffGenerator.STAFF_WITHOUT_USERNAME.forename)) + assertThat(res.code, equalTo(StaffGenerator.STAFF_WITHOUT_USERNAME.code)) + } } diff --git a/projects/approved-premises-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/controller/StaffController.kt b/projects/approved-premises-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/controller/StaffController.kt index ee9dfcf3bf..cfc151476c 100644 --- a/projects/approved-premises-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/controller/StaffController.kt +++ b/projects/approved-premises-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/controller/StaffController.kt @@ -11,10 +11,10 @@ import org.springframework.web.bind.annotation.RestController import uk.gov.justice.digital.hmpps.service.StaffService @RestController +@PreAuthorize("hasRole('PROBATION_API__APPROVED_PREMISES__CASE_DETAIL')") class StaffController( private val staffService: StaffService ) { - @PreAuthorize("hasRole('PROBATION_API__APPROVED_PREMISES__CASE_DETAIL')") @Operation( summary = "List all members of staff that are keyworkers in the Approved Premises", description = """An Approved Premises is defined in Delius as part of reference data. @@ -32,14 +32,15 @@ class StaffController( @PageableDefault(value = 100) pageable: Pageable = Pageable.ofSize(100) ) = staffService.getStaffInApprovedPremises(code, keyWorker, pageable) - @PreAuthorize("hasRole('PROBATION_API__APPROVED_PREMISES__CASE_DETAIL')") - @Operation( - summary = "Get the staff name by username", - description = """Returns the Staff name associated with the given username. - """ - ) + @Operation(summary = "Get the staff details by username") @GetMapping(value = ["/staff/{userName}"]) fun getStaffByUsername( @PathVariable userName: String ) = staffService.getStaffByUsername(userName) + + @Operation(summary = "Get the staff details by code") + @GetMapping(value = ["/staff"], params = ["code"]) + fun getStaffByCode( + @RequestParam code: String + ) = staffService.getStaffByCode(code) } diff --git a/projects/approved-premises-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/integrations/delius/person/offence/entity/Offence.kt b/projects/approved-premises-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/integrations/delius/person/offence/entity/Offence.kt index cbd7f00c44..951cdae8f7 100644 --- a/projects/approved-premises-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/integrations/delius/person/offence/entity/Offence.kt +++ b/projects/approved-premises-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/integrations/delius/person/offence/entity/Offence.kt @@ -1,12 +1,6 @@ package uk.gov.justice.digital.hmpps.integrations.delius.person.offence.entity -import jakarta.persistence.Column -import jakarta.persistence.Entity -import jakarta.persistence.Id -import jakarta.persistence.JoinColumn -import jakarta.persistence.ManyToOne -import jakarta.persistence.OneToOne -import jakarta.persistence.Table +import jakarta.persistence.* import org.hibernate.annotations.Immutable import org.hibernate.annotations.SQLRestriction import org.springframework.data.jpa.repository.JpaRepository @@ -15,11 +9,13 @@ import uk.gov.justice.digital.hmpps.integrations.delius.approvedpremises.referra import java.time.LocalDate interface CaseOffence { + val id: Long val code: String val description: String val date: LocalDate? val main: Boolean val eventNumber: String + val eventId: Long } @Immutable @@ -91,11 +87,25 @@ class Offence( interface MainOffenceRepository : JpaRepository { @Query( """ - select mo.offence.code as code, mo.offence.description as description, mo.date as date, true as main, mo.event.number as eventNumber + select + mo.offence.id as id, + mo.offence.code as code, + mo.offence.description as description, + mo.date as date, + true as main, + mo.event.number as eventNumber, + mo.event.id as eventId from MainOffence mo where mo.event.personId = :personId and mo.event.active = true union all - select ao.offence.code, ao.offence.description, ao.date, false, ao.event.number + select + ao.offence.id, + ao.offence.code, + ao.offence.description, + ao.date, + false, + ao.event.number, + ao.event.id from AdditionalOffence ao where ao.event.personId = :personId and ao.event.active = true """ diff --git a/projects/approved-premises-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/model/StaffResponse.kt b/projects/approved-premises-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/model/StaffResponse.kt index 234bf2a07b..279c09cbc7 100644 --- a/projects/approved-premises-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/model/StaffResponse.kt +++ b/projects/approved-premises-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/model/StaffResponse.kt @@ -29,7 +29,7 @@ data class StaffDetail( val staffIdentifier: Long, val teams: List = emptyList(), val probationArea: ProbationArea, - val username: String, + val username: String?, val name: PersonName, val code: String, val active: Boolean diff --git a/projects/approved-premises-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/service/StaffService.kt b/projects/approved-premises-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/service/StaffService.kt index 57570a8f8d..daae5fa43b 100644 --- a/projects/approved-premises-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/service/StaffService.kt +++ b/projects/approved-premises-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/service/StaffService.kt @@ -10,6 +10,7 @@ import uk.gov.justice.digital.hmpps.integrations.delius.approvedpremises.Approve import uk.gov.justice.digital.hmpps.integrations.delius.staff.LdapUser import uk.gov.justice.digital.hmpps.integrations.delius.staff.Staff import uk.gov.justice.digital.hmpps.integrations.delius.staff.StaffRepository +import uk.gov.justice.digital.hmpps.integrations.delius.staff.getByCode import uk.gov.justice.digital.hmpps.ldap.findByUsername import uk.gov.justice.digital.hmpps.model.* @@ -42,11 +43,12 @@ class StaffService( fun getStaffByUsername(username: String) = staffRepository.findByUsername(username)?.toStaffDetail(ldapTemplate.findByUsername(username)) - ?: throw NotFoundException( - "Staff", - "username", - username - ) + ?: throw NotFoundException("Staff", "username", username) + + fun getStaffByCode(code: String) = + staffRepository.getByCode(code).let { + it.toStaffDetail(it.user?.username?.let { username -> ldapTemplate.findByUsername(username) }) + } fun Staff.toResponse(approvedPremisesCode: String) = StaffResponse( code = code, @@ -68,7 +70,7 @@ class StaffService( endDate = it.endDate ) }, - username = user!!.username, + username = user?.username, name = PersonName(forename, surname, middleName), code = code, probationArea = ProbationArea(